fix: fetch ple for all party types

This commit is contained in:
Gursheen Anand 2023-07-28 11:41:03 +05:30
parent c47a37c3ab
commit fd5c4e0a64
2 changed files with 56 additions and 44 deletions

View File

@ -7,7 +7,7 @@ from erpnext.accounts.report.accounts_receivable.accounts_receivable import Rece
def execute(filters=None):
args = {
"party_type": "Supplier",
"account_type": "Payable",
"naming_by": ["Buying Settings", "supp_master_name"],
}
return ReceivablePayableReport(filters).run(args)

View File

@ -34,7 +34,7 @@ from erpnext.accounts.utils import get_currency_precision
def execute(filters=None):
args = {
"party_type": "Customer",
"account_type": "Receivable",
"naming_by": ["Selling Settings", "cust_master_name"],
}
return ReceivablePayableReport(filters).run(args)
@ -70,8 +70,11 @@ class ReceivablePayableReport(object):
"Company", self.filters.get("company"), "default_currency"
)
self.currency_precision = get_currency_precision() or 2
self.dr_or_cr = "debit" if self.filters.party_type == "Customer" else "credit"
self.party_type = self.filters.party_type
self.dr_or_cr = "debit" if self.filters.account_type == "Receivable" else "credit"
self.account_type = self.filters.account_type
self.party_type = frappe.db.get_all(
"Party Type", {"account_type": self.account_type}, pluck="name"
)
self.party_details = {}
self.invoices = set()
self.skip_total_row = 0
@ -197,6 +200,7 @@ class ReceivablePayableReport(object):
# no invoice, this is an invoice / stand-alone payment / credit note
row = self.voucher_balance.get((ple.voucher_type, ple.voucher_no, ple.party))
row.party_type = ple.party_type
return row
def update_voucher_balance(self, ple):
@ -207,8 +211,9 @@ class ReceivablePayableReport(object):
return
# amount in "Party Currency", if its supplied. If not, amount in company currency
if self.filters.get(scrub(self.party_type)):
amount = ple.amount_in_account_currency
for party_type in self.party_type:
if self.filters.get(scrub(party_type)):
amount = ple.amount_in_account_currency
else:
amount = ple.amount
amount_in_account_currency = ple.amount_in_account_currency
@ -362,7 +367,7 @@ class ReceivablePayableReport(object):
def get_invoice_details(self):
self.invoice_details = frappe._dict()
if self.party_type == "Customer":
if self.account_type == "Receivable":
si_list = frappe.db.sql(
"""
select name, due_date, po_no
@ -390,7 +395,7 @@ class ReceivablePayableReport(object):
d.sales_person
)
if self.party_type == "Supplier":
if self.account_type == "Payable":
for pi in frappe.db.sql(
"""
select name, due_date, bill_no, bill_date
@ -421,12 +426,10 @@ class ReceivablePayableReport(object):
# customer / supplier name
party_details = self.get_party_details(row.party) or {}
row.update(party_details)
if row.voucher_type == "Expense Claim":
row.party_type = "Employee"
else:
row.party_type = self.party_type
if self.filters.get(scrub(self.filters.party_type)):
row.currency = row.account_currency
for party_type in self.party_type:
if self.filters.get(scrub(party_type)):
row.currency = row.account_currency
break
else:
row.currency = self.company_currency
@ -552,7 +555,7 @@ class ReceivablePayableReport(object):
where
payment_entry.docstatus < 2
and payment_entry.posting_date > %s
and payment_entry.party_type = %s
and payment_entry.party_type in %s
""",
(self.filters.report_date, self.party_type),
as_dict=1,
@ -562,11 +565,11 @@ class ReceivablePayableReport(object):
if self.filters.get("party"):
amount_field = (
"jea.debit_in_account_currency - jea.credit_in_account_currency"
if self.party_type == "Supplier"
if self.account_type == "Payable"
else "jea.credit_in_account_currency - jea.debit_in_account_currency"
)
else:
amount_field = "jea.debit - " if self.party_type == "Supplier" else "jea.credit"
amount_field = "jea.debit - " if self.account_type == "Payable" else "jea.credit"
return frappe.db.sql(
"""
@ -584,7 +587,7 @@ class ReceivablePayableReport(object):
where
je.docstatus < 2
and je.posting_date > %s
and jea.party_type = %s
and jea.party_type in %s
and jea.reference_name is not null and jea.reference_name != ''
group by je.name, jea.reference_name
having future_amount > 0
@ -623,13 +626,17 @@ class ReceivablePayableReport(object):
row.future_ref = ", ".join(row.future_ref)
def get_return_entries(self):
doctype = "Sales Invoice" if self.party_type == "Customer" else "Purchase Invoice"
doctype = "Sales Invoice" if self.account_type == "Receivable" else "Purchase Invoice"
filters = {"is_return": 1, "docstatus": 1, "company": self.filters.company}
party_field = scrub(self.filters.party_type)
if self.filters.get(party_field):
filters.update({party_field: self.filters.get(party_field)})
or_filters = {}
for party_type in self.party_type:
party_field = scrub(party_type)
if self.filters.get(party_field):
or_filters.update({party_field: self.filters.get(party_field)})
self.return_entries = frappe._dict(
frappe.get_all(doctype, filters, ["name", "return_against"], as_list=1)
frappe.get_all(
doctype, filters=filters, or_filters=or_filters, fields=["name", "return_against"], as_list=1
)
)
def set_ageing(self, row):
@ -720,6 +727,7 @@ class ReceivablePayableReport(object):
)
.where(ple.delinked == 0)
.where(Criterion.all(self.qb_selection_filter))
.where(Criterion.any(self.or_filters))
)
if self.filters.get("group_by_party"):
@ -750,19 +758,18 @@ class ReceivablePayableReport(object):
def prepare_conditions(self):
self.qb_selection_filter = []
party_type_field = scrub(self.party_type)
if self.party_type == "Supplier":
self.qb_selection_filter.append(self.ple.party_type.isin([self.party_type, "Employee"]))
else:
self.qb_selection_filter.append(self.ple.party_type == self.party_type)
self.or_filters = []
for party_type in self.party_type:
party_type_field = scrub(party_type)
self.or_filters.append(self.ple.party_type == party_type)
self.add_common_filters(party_type_field=party_type_field)
self.add_common_filters(party_type_field=party_type_field)
if party_type_field == "customer":
self.add_customer_filters()
if party_type_field == "customer":
self.add_customer_filters()
elif party_type_field == "supplier":
self.add_supplier_filters()
elif party_type_field == "supplier":
self.add_supplier_filters()
if self.filters.cost_center:
self.get_cost_center_conditions()
@ -791,11 +798,10 @@ class ReceivablePayableReport(object):
self.qb_selection_filter.append(self.ple.account == self.filters.party_account)
else:
# get GL with "receivable" or "payable" account_type
account_type = "Receivable" if self.party_type == "Customer" else "Payable"
accounts = [
d.name
for d in frappe.get_all(
"Account", filters={"account_type": account_type, "company": self.filters.company}
"Account", filters={"account_type": self.account_type, "company": self.filters.company}
)
]
@ -885,7 +891,7 @@ class ReceivablePayableReport(object):
def get_party_details(self, party):
if not party in self.party_details:
if self.party_type == "Customer":
if self.account_type == "Receivable":
fields = ["customer_name", "territory", "customer_group", "customer_primary_contact"]
if self.filters.get("sales_partner"):
@ -921,7 +927,7 @@ class ReceivablePayableReport(object):
width=180,
)
self.add_column(
label="Receivable Account" if self.party_type == "Customer" else "Payable Account",
label=self.account_type + " Account",
fieldname="party_account",
fieldtype="Link",
options="Account",
@ -929,13 +935,19 @@ class ReceivablePayableReport(object):
)
if self.party_naming_by == "Naming Series":
if self.account_type == "Payable":
label = "Supplier Name"
fieldname = "supplier_name"
else:
label = "Customer Name"
fieldname = "customer_name"
self.add_column(
_("{0} Name").format(self.party_type),
fieldname=scrub(self.party_type) + "_name",
label=label,
fieldname=fieldname,
fieldtype="Data",
)
if self.party_type == "Customer":
if self.account_type == "Receivable":
self.add_column(
_("Customer Contact"),
fieldname="customer_primary_contact",
@ -955,7 +967,7 @@ class ReceivablePayableReport(object):
self.add_column(label="Due Date", fieldtype="Date")
if self.party_type == "Supplier":
if self.account_type == "Payable":
self.add_column(label=_("Bill No"), fieldname="bill_no", fieldtype="Data")
self.add_column(label=_("Bill Date"), fieldname="bill_date", fieldtype="Date")
@ -965,7 +977,7 @@ class ReceivablePayableReport(object):
self.add_column(_("Invoiced Amount"), fieldname="invoiced")
self.add_column(_("Paid Amount"), fieldname="paid")
if self.party_type == "Customer":
if self.account_type == "Receivable":
self.add_column(_("Credit Note"), fieldname="credit_note")
else:
# note: fieldname is still `credit_note`
@ -983,7 +995,7 @@ class ReceivablePayableReport(object):
self.add_column(label=_("Future Payment Amount"), fieldname="future_amount")
self.add_column(label=_("Remaining Balance"), fieldname="remaining_balance")
if self.filters.party_type == "Customer":
if self.filters.account_type == "Receivable":
self.add_column(label=_("Customer LPO"), fieldname="po_no", fieldtype="Data")
# comma separated list of linked delivery notes
@ -1004,7 +1016,7 @@ class ReceivablePayableReport(object):
if self.filters.sales_partner:
self.add_column(label=_("Sales Partner"), fieldname="default_sales_partner", fieldtype="Data")
if self.filters.party_type == "Supplier":
if self.filters.account_type == "Payable":
self.add_column(
label=_("Supplier Group"),
fieldname="supplier_group",