brotherton-erpnext/erpnext/patches/v4_2/party_model.py

118 lines
4.6 KiB
Python
Raw Normal View History

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import print_function, unicode_literals
import frappe
def execute():
frappe.reload_doc("accounts", "doctype", "account")
2014-11-28 05:56:35 +00:00
frappe.reload_doc("setup", "doctype", "company")
frappe.reload_doc("accounts", "doctype", "gl_entry")
2015-01-02 07:16:49 +00:00
frappe.reload_doc("accounts", "doctype", "journal_entry_account")
receivable_payable_accounts = create_receivable_payable_account()
if receivable_payable_accounts:
set_party_in_jv_and_gl_entry(receivable_payable_accounts)
delete_individual_party_account()
remove_customer_supplier_account_report()
def create_receivable_payable_account():
receivable_payable_accounts = frappe._dict()
def _create_account(args):
2015-05-27 10:44:00 +00:00
if args["parent_account"] and frappe.db.exists("Account", args["parent_account"]):
account_id = frappe.db.get_value("Account",
{"account_name": args["account_name"], "company": args["company"]})
if not account_id:
account = frappe.new_doc("Account")
account.is_group = 0
account.update(args)
account.insert()
2015-05-05 08:52:10 +00:00
account_id = account.name
2015-05-05 08:52:10 +00:00
frappe.db.set_value("Company", args["company"], ("default_receivable_account"
if args["account_type"]=="Receivable" else "default_payable_account"), account_id)
receivable_payable_accounts.setdefault(args["company"], {}).setdefault(args["account_type"], account_id)
for company in frappe.db.sql_list("select name from tabCompany"):
_create_account({
"account_name": "Debtors",
"account_type": "Receivable",
"company": company,
"parent_account": get_parent_account(company, "Customer")
})
_create_account({
"account_name": "Creditors",
"account_type": "Payable",
"company": company,
"parent_account": get_parent_account(company, "Supplier")
})
return receivable_payable_accounts
def get_parent_account(company, master_type):
2015-05-04 07:31:44 +00:00
parent_account = None
if "receivables_group" in frappe.db.get_table_columns("Company"):
parent_account = frappe.db.get_value("Company", company,
"receivables_group" if master_type=="Customer" else "payables_group")
if not parent_account:
parent_account = frappe.db.get_value("Account", {"company": company,
"account_name": "Accounts Receivable" if master_type=="Customer" else "Accounts Payable"})
if not parent_account:
parent_account = frappe.db.sql_list("""select parent_account from tabAccount
where company=%s and ifnull(master_type, '')=%s and ifnull(master_name, '')!='' limit 1""",
(company, master_type))
parent_account = parent_account[0][0] if parent_account else None
return parent_account
def set_party_in_jv_and_gl_entry(receivable_payable_accounts):
accounts = frappe.db.sql("""select name, master_type, master_name, company from `tabAccount`
where ifnull(master_type, '') in ('Customer', 'Supplier') and ifnull(master_name, '') != ''""", as_dict=1)
account_map = frappe._dict()
for d in accounts:
account_map.setdefault(d.name, d)
2014-10-21 10:46:30 +00:00
if not account_map:
return
for dt in ["Journal Entry Account", "GL Entry"]:
2015-05-04 07:31:44 +00:00
records = frappe.db.sql("""select name, account from `tab%s`
2015-05-05 10:30:21 +00:00
where account in (%s) and ifnull(party, '') = '' and docstatus < 2""" %
2014-11-28 05:56:35 +00:00
(dt, ", ".join(['%s']*len(account_map))), tuple(account_map.keys()), as_dict=1)
2015-03-25 09:21:05 +00:00
for i, d in enumerate(records):
account_details = account_map.get(d.account, {})
account_type = "Receivable" if account_details.get("master_type")=="Customer" else "Payable"
new_account = receivable_payable_accounts[account_details.get("company")][account_type]
frappe.db.sql("update `tab{0}` set account=%s, party_type=%s, party=%s where name=%s".format(dt),
(new_account, account_details.get("master_type"), account_details.get("master_name"), d.name))
2015-04-23 07:44:17 +00:00
2015-03-25 09:21:05 +00:00
if i%500 == 0:
frappe.db.commit()
def delete_individual_party_account():
2015-05-20 09:02:39 +00:00
frappe.db.sql("""delete from `tabAccount`
where ifnull(master_type, '') in ('Customer', 'Supplier')
and ifnull(master_name, '') != ''
and not exists(select gle.name from `tabGL Entry` gle
2015-05-20 09:09:39 +00:00
where gle.account = tabAccount.name)""")
2015-05-19 05:39:15 +00:00
2015-05-20 09:02:39 +00:00
accounts_not_deleted = frappe.db.sql_list("""select tabAccount.name from `tabAccount`
where ifnull(master_type, '') in ('Customer', 'Supplier')
and ifnull(master_name, '') != ''
and exists(select gle.name from `tabGL Entry` gle where gle.account = tabAccount.name)""")
2015-05-19 05:39:15 +00:00
if accounts_not_deleted:
print("Accounts not deleted: " + "\n".join(accounts_not_deleted))
2015-05-19 05:39:15 +00:00
def remove_customer_supplier_account_report():
for d in ["Customer Account Head", "Supplier Account Head"]:
frappe.delete_doc("Report", d)