Refactor party type (#13831)

This commit is contained in:
rohitwaghchaure 2018-05-16 11:02:26 +05:30 committed by Rushabh Mehta
parent e42192bb6c
commit e8358f33a3
15 changed files with 152 additions and 82 deletions

View File

@ -79,6 +79,16 @@ def is_perpetual_inventory_enabled(company):
return frappe.local.enable_perpetual_inventory[company] return frappe.local.enable_perpetual_inventory[company]
def get_party_account_type(party_type):
if not hasattr(frappe.local, 'party_account_types'):
frappe.local.party_account_types = {}
if not party_type in frappe.local.party_account_types:
frappe.local.party_account_types[party_type] = frappe.db.get_value("Party Type",
party_type, "account_type") or ''
return frappe.local.party_account_types[party_type]
def get_region(company=None): def get_region(company=None):
'''Return the default country based on flag, company or global settings '''Return the default country based on flag, company or global settings

View File

@ -158,9 +158,14 @@ erpnext.accounts.JournalEntry = frappe.ui.form.Controller.extend({
}; };
}); });
me.frm.set_query("party_type", "accounts", function() { me.frm.set_query("party_type", "accounts", function(doc, cdt, cdn) {
const row = locals[cdt][cdn];
return { return {
query: "erpnext.setup.doctype.party_type.party_type.get_party_type" query: "erpnext.setup.doctype.party_type.party_type.get_party_type",
filters: {
'account': row.account
}
} }
}); });

View File

@ -12,10 +12,8 @@ frappe.ui.form.on('Payment Entry', {
setup: function(frm) { setup: function(frm) {
frm.set_query("paid_from", function() { frm.set_query("paid_from", function() {
var party_account_type = in_list(["Customer", "Student"], frm.doc.party_type) ?
"Receivable" : "Payable";
var account_types = in_list(["Pay", "Internal Transfer"], frm.doc.payment_type) ? var account_types = in_list(["Pay", "Internal Transfer"], frm.doc.payment_type) ?
["Bank", "Cash"] : party_account_type; ["Bank", "Cash"] : [frappe.boot.party_account_types[frm.doc.party_type]];
return { return {
filters: { filters: {
@ -29,16 +27,14 @@ frappe.ui.form.on('Payment Entry', {
frm.set_query("party_type", function() { frm.set_query("party_type", function() {
return{ return{
"filters": { "filters": {
"name": ["in",["Customer","Supplier", "Employee", "Student"]], "name": ["in", Object.keys(frappe.boot.party_account_types)],
} }
} }
}); });
frm.set_query("paid_to", function() { frm.set_query("paid_to", function() {
var party_account_type = in_list(["Customer", "Student"], frm.doc.party_type) ?
"Receivable" : "Payable";
var account_types = in_list(["Receive", "Internal Transfer"], frm.doc.payment_type) ? var account_types = in_list(["Receive", "Internal Transfer"], frm.doc.payment_type) ?
["Bank", "Cash"] : party_account_type; ["Bank", "Cash"] : [frappe.boot.party_account_types[frm.doc.party_type]];
return { return {
filters: { filters: {
@ -86,9 +82,9 @@ frappe.ui.form.on('Payment Entry', {
}); });
frm.set_query("reference_name", "references", function(doc, cdt, cdn) { frm.set_query("reference_name", "references", function(doc, cdt, cdn) {
child = locals[cdt][cdn]; const child = locals[cdt][cdn];
filters = {"docstatus": 1, "company": doc.company}; const filters = {"docstatus": 1, "company": doc.company};
party_type_doctypes = ['Sales Invoice', 'Sales Order', 'Purchase Invoice', const party_type_doctypes = ['Sales Invoice', 'Sales Order', 'Purchase Invoice',
'Purchase Order', 'Expense Claim', 'Fees']; 'Purchase Order', 'Expense Claim', 'Fees'];
if (in_list(party_type_doctypes, child.reference_doctype)) { if (in_list(party_type_doctypes, child.reference_doctype)) {

View File

@ -160,9 +160,9 @@ class PaymentEntry(AccountsController):
if not frappe.db.exists(self.party_type, self.party): if not frappe.db.exists(self.party_type, self.party):
frappe.throw(_("Invalid {0}: {1}").format(self.party_type, self.party)) frappe.throw(_("Invalid {0}: {1}").format(self.party_type, self.party))
if self.party_account and self.party_type != "Employee": if self.party_account:
party_account_type = "Receivable" if self.party_type in ("Customer", "Student") else "Payable" self.validate_account_type(self.party_account,
self.validate_account_type(self.party_account, [party_account_type]) [erpnext.get_party_account_type(self.party_type)])
def validate_bank_accounts(self): def validate_bank_accounts(self):
if self.payment_type in ("Pay", "Internal Transfer"): if self.payment_type in ("Pay", "Internal Transfer"):
@ -413,7 +413,6 @@ class PaymentEntry(AccountsController):
else: else:
against_account = self.paid_from against_account = self.paid_from
party_gl_dict = self.get_gl_dict({ party_gl_dict = self.get_gl_dict({
"account": self.party_account, "account": self.party_account,
"party_type": self.party_type, "party_type": self.party_type,
@ -422,7 +421,7 @@ class PaymentEntry(AccountsController):
"account_currency": self.party_account_currency "account_currency": self.party_account_currency
}) })
dr_or_cr = "credit" if self.party_type in ["Customer", "Student"] else "debit" dr_or_cr = "credit" if erpnext.get_party_account_type(self.party_type) == 'Receivable' else "debit"
for d in self.get("references"): for d in self.get("references"):
gle = party_gl_dict.copy() gle = party_gl_dict.copy()

View File

@ -25,7 +25,9 @@ erpnext.accounts.PaymentReconciliationController = frappe.ui.form.Controller.ext
var me = this; var me = this;
this.frm.set_query("party_type", function() { this.frm.set_query("party_type", function() {
return { return {
query: "erpnext.setup.doctype.party_type.party_type.get_party_type" "filters": {
"name": ["in", Object.keys(frappe.boot.party_account_types)],
}
} }
}); });
@ -37,7 +39,7 @@ erpnext.accounts.PaymentReconciliationController = frappe.ui.form.Controller.ext
filters: { filters: {
"company": me.frm.doc.company, "company": me.frm.doc.company,
"is_group": 0, "is_group": 0,
"account_type": (me.frm.doc.party_type == "Customer" ? "Receivable" : "Payable") "account_type": frappe.boot.party_account_types[me.frm.doc.party_type]
} }
}; };
} }

View File

@ -2,7 +2,7 @@
# For license information, please see license.txt # For license information, please see license.txt
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe, erpnext
from frappe.utils import flt from frappe.utils import flt
from frappe import msgprint, _ from frappe import msgprint, _
from frappe.model.document import Document from frappe.model.document import Document
@ -30,8 +30,8 @@ class PaymentReconciliation(Document):
return payment_entries return payment_entries
def get_jv_entries(self): def get_jv_entries(self):
dr_or_cr = "credit_in_account_currency" if self.party_type == "Customer" \ dr_or_cr = ("credit_in_account_currency" if erpnext.get_party_account_type(self.party_type) == 'Receivable'
else "debit_in_account_currency" else "debit_in_account_currency")
bank_account_condition = "t2.against_account like %(bank_cash_account)s" \ bank_account_condition = "t2.against_account like %(bank_cash_account)s" \
if self.bank_cash_account else "1=1" if self.bank_cash_account else "1=1"
@ -104,8 +104,8 @@ class PaymentReconciliation(Document):
self.get_invoice_entries() self.get_invoice_entries()
self.validate_invoice() self.validate_invoice()
dr_or_cr = "credit_in_account_currency" \ dr_or_cr = ("credit_in_account_currency"
if self.party_type == "Customer" else "debit_in_account_currency" if erpnext.get_party_account_type(self.party_type) == 'Receivable' else "debit_in_account_currency")
lst = [] lst = []
for e in self.get('payments'): for e in self.get('payments'):
@ -173,11 +173,8 @@ class PaymentReconciliation(Document):
def check_condition(self): def check_condition(self):
cond = " and posting_date >= '{0}'".format(frappe.db.escape(self.from_date)) if self.from_date else "" cond = " and posting_date >= '{0}'".format(frappe.db.escape(self.from_date)) if self.from_date else ""
cond += " and posting_date <= '{0}'".format(frappe.db.escape(self.to_date)) if self.to_date else "" cond += " and posting_date <= '{0}'".format(frappe.db.escape(self.to_date)) if self.to_date else ""
dr_or_cr = ("debit_in_account_currency" if erpnext.get_party_account_type(self.party_type) == 'Receivable'
if self.party_type == "Customer": else "credit_in_account_currency")
dr_or_cr = "debit_in_account_currency"
else:
dr_or_cr = "credit_in_account_currency"
if self.minimum_amount: if self.minimum_amount:
cond += " and `{0}` >= {1}".format(dr_or_cr, flt(self.minimum_amount)) cond += " and `{0}` >= {1}".format(dr_or_cr, flt(self.minimum_amount))

View File

@ -46,9 +46,10 @@ frappe.query_reports["Trial Balance for Party"] = {
{ {
"fieldname":"party_type", "fieldname":"party_type",
"label": __("Party Type"), "label": __("Party Type"),
"fieldtype": "Select", "fieldtype": "Link",
"options": ["Customer", "Supplier"], "options": "Party Type",
"default": "Customer" "default": "Customer",
"reqd": 1
}, },
{ {
"fieldname":"party", "fieldname":"party",

View File

@ -7,7 +7,6 @@ from frappe import _
from frappe.utils import flt, cint from frappe.utils import flt, cint
from erpnext.accounts.report.trial_balance.trial_balance import validate_filters from erpnext.accounts.report.trial_balance.trial_balance import validate_filters
def execute(filters=None): def execute(filters=None):
validate_filters(filters) validate_filters(filters)
@ -19,7 +18,10 @@ def execute(filters=None):
return columns, data return columns, data
def get_data(filters, show_party_name): def get_data(filters, show_party_name):
party_name_field = "customer_name" if filters.get("party_type")=="Customer" else "supplier_name" party_name_field = "{0}_name".format(frappe.scrub(filters.get('party_type')))
if filters.get('party_type') == 'Student':
party_name_field = 'first_name'
party_filters = {"name": filters.get("party")} if filters.get("party") else {} party_filters = {"name": filters.get("party")} if filters.get("party") else {}
parties = frappe.get_all(filters.get("party_type"), fields = ["name", party_name_field], parties = frappe.get_all(filters.get("party_type"), fields = ["name", party_name_field],
filters = party_filters, order_by="name") filters = party_filters, order_by="name")
@ -211,6 +213,8 @@ def get_columns(filters, show_party_name):
def is_party_name_visible(filters): def is_party_name_visible(filters):
show_party_name = False show_party_name = False
if filters.get('party_type') in ['Customer', 'Supplier']:
if filters.get("party_type") == "Customer": if filters.get("party_type") == "Customer":
party_naming_by = frappe.db.get_single_value("Selling Settings", "cust_master_name") party_naming_by = frappe.db.get_single_value("Selling Settings", "cust_master_name")
else: else:
@ -218,5 +222,7 @@ def is_party_name_visible(filters):
if party_naming_by == "Naming Series": if party_naming_by == "Naming Series":
show_party_name = True show_party_name = True
else:
show_party_name = True
return show_party_name return show_party_name

View File

@ -3,7 +3,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe, erpnext
import frappe.defaults import frappe.defaults
from frappe.utils import nowdate, cstr, flt, cint, now, getdate from frappe.utils import nowdate, cstr, flt, cint, now, getdate
from frappe import throw, _ from frappe import throw, _
@ -322,7 +322,9 @@ def check_if_advance_entry_modified(args):
and t1.name = %(voucher_no)s and t2.name = %(voucher_detail_no)s and t1.name = %(voucher_no)s and t2.name = %(voucher_detail_no)s
and t1.docstatus=1 """.format(dr_or_cr = args.get("dr_or_cr")), args) and t1.docstatus=1 """.format(dr_or_cr = args.get("dr_or_cr")), args)
else: else:
party_account_field = "paid_from" if args.party_type == "Customer" else "paid_to" party_account_field = ("paid_from"
if erpnext.get_party_account_type(args.party_type) == 'Receivable' else "paid_to")
if args.voucher_detail_no: if args.voucher_detail_no:
ret = frappe.db.sql("""select t1.name ret = frappe.db.sql("""select t1.name
from `tabPayment Entry` t1, `tabPayment Entry Reference` t2 from `tabPayment Entry` t1, `tabPayment Entry Reference` t2
@ -574,14 +576,14 @@ def get_outstanding_invoices(party_type, party, account, condition=None):
outstanding_invoices = [] outstanding_invoices = []
precision = frappe.get_precision("Sales Invoice", "outstanding_amount") precision = frappe.get_precision("Sales Invoice", "outstanding_amount")
if party_type in ("Customer", "Student"): if erpnext.get_party_account_type(party_type) == 'Receivable':
dr_or_cr = "debit_in_account_currency - credit_in_account_currency" dr_or_cr = "debit_in_account_currency - credit_in_account_currency"
payment_dr_or_cr = "payment_gl_entry.credit_in_account_currency - payment_gl_entry.debit_in_account_currency" payment_dr_or_cr = "payment_gl_entry.credit_in_account_currency - payment_gl_entry.debit_in_account_currency"
else: else:
dr_or_cr = "credit_in_account_currency - debit_in_account_currency" dr_or_cr = "credit_in_account_currency - debit_in_account_currency"
payment_dr_or_cr = "payment_gl_entry.debit_in_account_currency - payment_gl_entry.credit_in_account_currency" payment_dr_or_cr = "payment_gl_entry.debit_in_account_currency - payment_gl_entry.credit_in_account_currency"
invoice = 'Sales Invoice' if party_type == 'Customer' else 'Purchase Invoice' invoice = 'Sales Invoice' if erpnext.get_party_account_type(party_type) == 'Receivable' else 'Purchase Invoice'
invoice_list = frappe.db.sql(""" invoice_list = frappe.db.sql("""
select select
voucher_no, voucher_type, posting_date, ifnull(sum({dr_or_cr}), 0) as invoice_amount, voucher_no, voucher_type, posting_date, ifnull(sum({dr_or_cr}), 0) as invoice_amount,

View File

@ -523,6 +523,7 @@ erpnext.patches.v11_0.update_department_lft_rgt
erpnext.patches.v11_0.add_default_email_template_for_leave erpnext.patches.v11_0.add_default_email_template_for_leave
erpnext.patches.v11_0.set_default_email_template_in_hr erpnext.patches.v11_0.set_default_email_template_in_hr
erpnext.patches.v10_0.taxes_issue_with_pos erpnext.patches.v10_0.taxes_issue_with_pos
erpnext.patches.v11_0.update_account_type_in_party_type
erpnext.patches.v10_1.transfer_subscription_to_auto_repeat erpnext.patches.v10_1.transfer_subscription_to_auto_repeat
erpnext.patches.v10_1.drop_old_subscription_records erpnext.patches.v10_1.drop_old_subscription_records
erpnext.patches.v11_0.update_brand_in_item_price erpnext.patches.v11_0.update_brand_in_item_price

View File

@ -0,0 +1,13 @@
# Copyright (c) 2017, Frappe and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe
def execute():
frappe.reload_doc('setup', 'doctype', 'party_type')
party_types = {'Customer': 'Receivable', 'Supplier': 'Payable',
'Employee': 'Payable', 'Member': 'Receivable', 'Shareholder': 'Payable', 'Student': 'Receivable'}
for party_type, account_type in party_types.items():
frappe.db.set_value('Party Type', party_type, 'account_type', account_type)

View File

@ -42,6 +42,39 @@
"reqd": 1, "reqd": 1,
"search_index": 0, "search_index": 0,
"set_only_once": 0, "set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "account_type",
"fieldtype": "Select",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 1,
"in_standard_filter": 0,
"label": "Account Type",
"length": 0,
"no_copy": 0,
"options": "Payable\nReceivable",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0 "unique": 0
} }
], ],
@ -55,7 +88,7 @@
"issingle": 0, "issingle": 0,
"istable": 0, "istable": 0,
"max_attachments": 0, "max_attachments": 0,
"modified": "2017-11-23 17:46:27.075001", "modified": "2018-04-26 13:00:49.457439",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Setup", "module": "Setup",
"name": "Party Type", "name": "Party Type",
@ -64,7 +97,6 @@
"permissions": [ "permissions": [
{ {
"amend": 0, "amend": 0,
"apply_user_permissions": 0,
"cancel": 0, "cancel": 0,
"create": 0, "create": 0,
"delete": 0, "delete": 0,
@ -84,7 +116,6 @@
}, },
{ {
"amend": 0, "amend": 0,
"apply_user_permissions": 0,
"cancel": 0, "cancel": 0,
"create": 0, "create": 0,
"delete": 0, "delete": 0,
@ -104,7 +135,6 @@
}, },
{ {
"amend": 0, "amend": 0,
"apply_user_permissions": 0,
"cancel": 0, "cancel": 0,
"create": 0, "create": 0,
"delete": 0, "delete": 0,

View File

@ -11,10 +11,15 @@ class PartyType(Document):
@frappe.whitelist() @frappe.whitelist()
def get_party_type(doctype, txt, searchfield, start, page_len, filters): def get_party_type(doctype, txt, searchfield, start, page_len, filters):
cond = ''
if filters and filters.get('account'):
account_type = frappe.db.get_value('Account', filters.get('account'), 'account_type')
cond = "and account_type = '%s'" % account_type
return frappe.db.sql("""select name from `tabParty Type` return frappe.db.sql("""select name from `tabParty Type`
where `{key}` LIKE %(txt)s where `{key}` LIKE %(txt)s {cond}
order by name limit %(start)s, %(page_len)s""" order by name limit %(start)s, %(page_len)s"""
.format(key=searchfield), { .format(key=searchfield, cond=cond), {
'txt': "%%%s%%" % frappe.db.escape(txt), 'txt': "%%%s%%" % frappe.db.escape(txt),
'start': start, 'page_len': page_len 'start': start, 'page_len': page_len
}) })

View File

@ -198,12 +198,12 @@ def install(country=None):
{'doctype': "Email Account", "email_id": "support@example.com", "append_to": "Issue"}, {'doctype': "Email Account", "email_id": "support@example.com", "append_to": "Issue"},
{'doctype': "Email Account", "email_id": "jobs@example.com", "append_to": "Job Applicant"}, {'doctype': "Email Account", "email_id": "jobs@example.com", "append_to": "Job Applicant"},
{'doctype': "Party Type", "party_type": "Customer"}, {'doctype': "Party Type", "party_type": "Customer", "account_type": "Receivable"},
{'doctype': "Party Type", "party_type": "Supplier"}, {'doctype': "Party Type", "party_type": "Supplier", "account_type": "Payable"},
{'doctype': "Party Type", "party_type": "Employee"}, {'doctype': "Party Type", "party_type": "Employee", "account_type": "Payable"},
{'doctype': "Party Type", "party_type": "Member"}, {'doctype': "Party Type", "party_type": "Member", "account_type": "Receivable"},
{'doctype': "Party Type", "party_type": "Shareholder"}, {'doctype': "Party Type", "party_type": "Shareholder", "account_type": "Payable"},
{'doctype': "Party Type", "party_type": "Student"}, {'doctype': "Party Type", "party_type": "Student", "account_type": "Receivable"},
{'doctype': "Opportunity Type", "name": "Hub"}, {'doctype': "Opportunity Type", "name": "Hub"},
{'doctype': "Opportunity Type", "name": _("Sales")}, {'doctype': "Opportunity Type", "name": _("Sales")},

View File

@ -36,6 +36,9 @@ def boot_session(bootinfo):
default_letter_head, default_bank_account, enable_perpetual_inventory from `tabCompany`""", default_letter_head, default_bank_account, enable_perpetual_inventory from `tabCompany`""",
as_dict=1, update={"doctype":":Company"}) as_dict=1, update={"doctype":":Company"})
party_account_types = frappe.db.sql(""" select name, ifnull(account_type, '') from `tabParty Type`""")
bootinfo.party_account_types = frappe._dict(party_account_types)
def load_country_and_currency(bootinfo): def load_country_and_currency(bootinfo):
country = frappe.db.get_default("country") country = frappe.db.get_default("country")
if country and frappe.db.exists("Country", country): if country and frappe.db.exists("Country", country):