2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd.
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
from collections import OrderedDict
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _, scrub
|
2019-09-03 10:37:46 +00:00
|
|
|
from frappe.utils import cint, cstr, flt, getdate, nowdate
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-03-17 05:23:24 +00:00
|
|
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
|
|
|
get_accounting_dimensions,
|
|
|
|
get_dimension_with_children,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2019-09-03 10:37:46 +00:00
|
|
|
from erpnext.accounts.utils import get_currency_precision
|
2013-04-22 07:44:52 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
# This report gives a summary of all Outstanding Invoices considering the following
|
|
|
|
|
|
|
|
# 1. Invoice can be booked via Sales/Purchase Invoice or Journal Entry
|
|
|
|
# 2. Report handles both receivable and payable
|
|
|
|
# 3. Key balances for each row are "Invoiced Amount", "Paid Amount", "Credit/Debit Note Amount", "Oustanding Amount"
|
|
|
|
# 4. For explicit payment terms in invoice (example: 30% advance, 30% on delivery, 40% post delivery),
|
|
|
|
# the invoice will be broken up into multiple rows, one for each payment term
|
|
|
|
# 5. If there are payments after the report date (post dated), these will be updated in additional columns
|
|
|
|
# for future amount
|
|
|
|
# 6. Configurable Ageing Groups (0-30, 30-60 etc) can be set via filters
|
|
|
|
# 7. For overpayment against an invoice with payment terms, there will be an additional row
|
|
|
|
# 8. Invoice details like Sales Persons, Delivery Notes are also fetched comma separated
|
|
|
|
# 9. Report amounts are in "Party Currency" if party is selected, or company currency for multi-party
|
|
|
|
# 10. This reports is based on all GL Entries that are made against account_type "Receivable" or "Payable"
|
|
|
|
|
|
|
|
def execute(filters=None):
|
|
|
|
args = {
|
|
|
|
"party_type": "Customer",
|
|
|
|
"naming_by": ["Selling Settings", "cust_master_name"],
|
|
|
|
}
|
|
|
|
return ReceivablePayableReport(filters).run(args)
|
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
class ReceivablePayableReport(object):
|
2013-11-07 15:14:30 +00:00
|
|
|
def __init__(self, filters=None):
|
2014-02-14 10:17:51 +00:00
|
|
|
self.filters = frappe._dict(filters or {})
|
2013-11-07 15:14:30 +00:00
|
|
|
self.filters.report_date = getdate(self.filters.report_date or nowdate())
|
|
|
|
self.age_as_on = getdate(nowdate()) \
|
|
|
|
if self.filters.report_date > getdate(nowdate()) \
|
|
|
|
else self.filters.report_date
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def run(self, args):
|
2019-09-03 10:37:46 +00:00
|
|
|
self.filters.update(args)
|
|
|
|
self.set_defaults()
|
|
|
|
self.party_naming_by = frappe.db.get_value(args.get("naming_by")[0], None, args.get("naming_by")[1])
|
|
|
|
self.get_columns()
|
|
|
|
self.get_data()
|
|
|
|
self.get_chart_data()
|
2020-02-18 10:37:34 +00:00
|
|
|
return self.columns, self.data, None, self.chart, None, self.skip_total_row
|
2019-09-03 10:37:46 +00:00
|
|
|
|
|
|
|
def set_defaults(self):
|
2016-11-10 13:43:20 +00:00
|
|
|
if not self.filters.get("company"):
|
2019-09-03 10:37:46 +00:00
|
|
|
self.filters.company = frappe.db.get_single_value('Global Defaults', 'default_company')
|
2018-11-13 06:41:04 +00:00
|
|
|
self.company_currency = frappe.get_cached_value('Company', self.filters.get("company"), "default_currency")
|
2019-09-03 10:37:46 +00:00
|
|
|
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.party_details = {}
|
|
|
|
self.invoices = set()
|
2020-02-18 10:37:34 +00:00
|
|
|
self.skip_total_row = 0
|
|
|
|
|
|
|
|
if self.filters.get('group_by_party'):
|
|
|
|
self.previous_party=''
|
|
|
|
self.total_row_map = {}
|
|
|
|
self.skip_total_row = 1
|
2019-09-03 10:37:46 +00:00
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
self.get_gl_entries()
|
2019-12-04 10:00:58 +00:00
|
|
|
self.get_sales_invoices_or_customers_based_on_sales_person()
|
2019-09-03 10:37:46 +00:00
|
|
|
self.voucher_balance = OrderedDict()
|
|
|
|
self.init_voucher_balance() # invoiced, paid, credit_note, outstanding
|
|
|
|
|
|
|
|
# Build delivery note map against all sales invoices
|
|
|
|
self.build_delivery_note_map()
|
|
|
|
|
|
|
|
# Get invoice details like bill_no, due_date etc for all invoices
|
|
|
|
self.get_invoice_details()
|
|
|
|
|
|
|
|
# fetch future payments against invoices
|
|
|
|
self.get_future_payments()
|
|
|
|
|
2019-09-24 14:22:33 +00:00
|
|
|
# Get return entries
|
|
|
|
self.get_return_entries()
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
self.data = []
|
|
|
|
for gle in self.gl_entries:
|
|
|
|
self.update_voucher_balance(gle)
|
|
|
|
|
|
|
|
self.build_data()
|
|
|
|
|
|
|
|
def init_voucher_balance(self):
|
|
|
|
# build all keys, since we want to exclude vouchers beyond the report date
|
|
|
|
for gle in self.gl_entries:
|
|
|
|
# get the balance object for voucher_type
|
|
|
|
key = (gle.voucher_type, gle.voucher_no, gle.party)
|
|
|
|
if not key in self.voucher_balance:
|
|
|
|
self.voucher_balance[key] = frappe._dict(
|
|
|
|
voucher_type = gle.voucher_type,
|
|
|
|
voucher_no = gle.voucher_no,
|
|
|
|
party = gle.party,
|
|
|
|
posting_date = gle.posting_date,
|
2019-09-24 14:22:33 +00:00
|
|
|
account_currency = gle.account_currency,
|
2021-09-09 06:27:29 +00:00
|
|
|
remarks = gle.remarks if self.filters.get("show_remarks") else None,
|
2019-09-03 10:37:46 +00:00
|
|
|
invoiced = 0.0,
|
|
|
|
paid = 0.0,
|
|
|
|
credit_note = 0.0,
|
2021-11-29 18:23:28 +00:00
|
|
|
outstanding = 0.0,
|
|
|
|
invoiced_in_account_currency = 0.0,
|
|
|
|
paid_in_account_currency = 0.0,
|
|
|
|
credit_note_in_account_currency = 0.0,
|
|
|
|
outstanding_in_account_currency = 0.0
|
2019-09-03 10:37:46 +00:00
|
|
|
)
|
|
|
|
self.get_invoices(gle)
|
|
|
|
|
2020-02-18 10:37:34 +00:00
|
|
|
if self.filters.get('group_by_party'):
|
|
|
|
self.init_subtotal_row(gle.party)
|
|
|
|
|
|
|
|
if self.filters.get('group_by_party'):
|
|
|
|
self.init_subtotal_row('Total')
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_invoices(self, gle):
|
|
|
|
if gle.voucher_type in ('Sales Invoice', 'Purchase Invoice'):
|
2019-12-04 10:00:58 +00:00
|
|
|
if self.filters.get("sales_person"):
|
|
|
|
if gle.voucher_no in self.sales_person_records.get("Sales Invoice", []) \
|
|
|
|
or gle.party in self.sales_person_records.get("Customer", []):
|
|
|
|
self.invoices.add(gle.voucher_no)
|
|
|
|
else:
|
|
|
|
self.invoices.add(gle.voucher_no)
|
2019-09-03 10:37:46 +00:00
|
|
|
|
2020-02-18 10:37:34 +00:00
|
|
|
def init_subtotal_row(self, party):
|
|
|
|
if not self.total_row_map.get(party):
|
|
|
|
self.total_row_map.setdefault(party, {
|
|
|
|
'party': party,
|
|
|
|
'bold': 1
|
|
|
|
})
|
|
|
|
|
|
|
|
for field in self.get_currency_fields():
|
|
|
|
self.total_row_map[party][field] = 0.0
|
|
|
|
|
|
|
|
def get_currency_fields(self):
|
|
|
|
return ['invoiced', 'paid', 'credit_note', 'outstanding', 'range1',
|
|
|
|
'range2', 'range3', 'range4', 'range5']
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def update_voucher_balance(self, gle):
|
|
|
|
# get the row where this balance needs to be updated
|
|
|
|
# if its a payment, it will return the linked invoice or will be considered as advance
|
|
|
|
row = self.get_voucher_balance(gle)
|
2019-12-04 10:00:58 +00:00
|
|
|
if not row: return
|
2019-09-03 10:37:46 +00:00
|
|
|
# gle_balance will be the total "debit - credit" for receivable type reports and
|
|
|
|
# and vice-versa for payable type reports
|
|
|
|
gle_balance = self.get_gle_balance(gle)
|
2021-11-29 18:23:28 +00:00
|
|
|
gle_balance_in_account_currency = self.get_gle_balance_in_account_currency(gle)
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if gle_balance > 0:
|
|
|
|
if gle.voucher_type in ('Journal Entry', 'Payment Entry') and gle.against_voucher:
|
|
|
|
# debit against sales / purchase invoice
|
|
|
|
row.paid -= gle_balance
|
2021-11-29 18:23:28 +00:00
|
|
|
row.paid_in_account_currency -= gle_balance_in_account_currency
|
2019-09-03 10:37:46 +00:00
|
|
|
else:
|
|
|
|
# invoice
|
|
|
|
row.invoiced += gle_balance
|
2021-11-29 18:23:28 +00:00
|
|
|
row.invoiced_in_account_currency += gle_balance_in_account_currency
|
2019-09-03 10:37:46 +00:00
|
|
|
else:
|
|
|
|
# payment or credit note for receivables
|
|
|
|
if self.is_invoice(gle):
|
|
|
|
# stand alone debit / credit note
|
|
|
|
row.credit_note -= gle_balance
|
2021-11-29 18:23:28 +00:00
|
|
|
row.credit_note_in_account_currency -= gle_balance_in_account_currency
|
2019-09-03 10:37:46 +00:00
|
|
|
else:
|
|
|
|
# advance / unlinked payment or other adjustment
|
|
|
|
row.paid -= gle_balance
|
2021-11-29 18:23:28 +00:00
|
|
|
row.paid_in_account_currency -= gle_balance_in_account_currency
|
|
|
|
|
2020-11-09 14:50:05 +00:00
|
|
|
if gle.cost_center:
|
|
|
|
row.cost_center = str(gle.cost_center)
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2020-02-18 10:37:34 +00:00
|
|
|
def update_sub_total_row(self, row, party):
|
|
|
|
total_row = self.total_row_map.get(party)
|
|
|
|
|
|
|
|
for field in self.get_currency_fields():
|
|
|
|
total_row[field] += row.get(field, 0.0)
|
|
|
|
|
|
|
|
def append_subtotal_row(self, party):
|
|
|
|
sub_total_row = self.total_row_map.get(party)
|
2020-06-03 16:21:21 +00:00
|
|
|
|
|
|
|
if sub_total_row:
|
|
|
|
self.data.append(sub_total_row)
|
|
|
|
self.data.append({})
|
|
|
|
self.update_sub_total_row(sub_total_row, 'Total')
|
2020-02-18 10:37:34 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_voucher_balance(self, gle):
|
2019-12-04 10:00:58 +00:00
|
|
|
if self.filters.get("sales_person"):
|
|
|
|
against_voucher = gle.against_voucher or gle.voucher_no
|
|
|
|
if not (gle.party in self.sales_person_records.get("Customer", []) or \
|
|
|
|
against_voucher in self.sales_person_records.get("Sales Invoice", [])):
|
|
|
|
return
|
2018-12-19 12:42:58 +00:00
|
|
|
|
2019-12-04 10:00:58 +00:00
|
|
|
voucher_balance = None
|
2019-09-03 10:37:46 +00:00
|
|
|
if gle.against_voucher:
|
|
|
|
# find invoice
|
2019-09-24 14:22:33 +00:00
|
|
|
against_voucher = gle.against_voucher
|
|
|
|
|
|
|
|
# If payment is made against credit note
|
|
|
|
# and credit note is made against a Sales Invoice
|
|
|
|
# then consider the payment against original sales invoice.
|
|
|
|
if gle.against_voucher_type in ('Sales Invoice', 'Purchase Invoice'):
|
|
|
|
if gle.against_voucher in self.return_entries:
|
|
|
|
return_against = self.return_entries.get(gle.against_voucher)
|
|
|
|
if return_against:
|
|
|
|
against_voucher = return_against
|
|
|
|
|
|
|
|
voucher_balance = self.voucher_balance.get((gle.against_voucher_type, against_voucher, gle.party))
|
2018-12-07 02:45:05 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if not voucher_balance:
|
|
|
|
# no invoice, this is an invoice / stand-alone payment / credit note
|
|
|
|
voucher_balance = self.voucher_balance.get((gle.voucher_type, gle.voucher_no, gle.party))
|
2018-12-17 12:15:39 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
return voucher_balance
|
2018-12-07 02:45:05 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def build_data(self):
|
|
|
|
# set outstanding for all the accumulated balances
|
|
|
|
# as we can use this to filter out invoices without outstanding
|
|
|
|
for key, row in self.voucher_balance.items():
|
|
|
|
row.outstanding = flt(row.invoiced - row.paid - row.credit_note, self.currency_precision)
|
2021-11-29 18:23:28 +00:00
|
|
|
row.outstanding_in_account_currency = flt(row.invoiced_in_account_currency - row.paid_in_account_currency - \
|
|
|
|
row.credit_note_in_account_currency, self.currency_precision)
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
row.invoice_grand_total = row.invoiced
|
2021-11-29 18:23:28 +00:00
|
|
|
|
|
|
|
if (abs(row.outstanding) > 1.0/10 ** self.currency_precision) and \
|
|
|
|
(abs(row.outstanding_in_account_currency) > 1.0/10 ** self.currency_precision):
|
2019-09-03 10:37:46 +00:00
|
|
|
# non-zero oustanding, we must consider this row
|
2018-12-07 02:45:05 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if self.is_invoice(row) and self.filters.based_on_payment_terms:
|
|
|
|
# is an invoice, allocate based on fifo
|
|
|
|
# adds a list `payment_terms` which contains new rows for each term
|
|
|
|
self.allocate_outstanding_based_on_payment_terms(row)
|
2018-12-17 09:43:33 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if row.payment_terms:
|
|
|
|
# make separate rows for each payment term
|
|
|
|
for d in row.payment_terms:
|
|
|
|
if d.outstanding > 0:
|
|
|
|
self.append_row(d)
|
2018-12-17 09:43:33 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
# if there is overpayment, add another row
|
|
|
|
self.allocate_extra_payments_or_credits(row)
|
2018-11-13 06:41:04 +00:00
|
|
|
else:
|
2019-09-03 10:37:46 +00:00
|
|
|
self.append_row(row)
|
|
|
|
else:
|
|
|
|
self.append_row(row)
|
|
|
|
|
2020-02-18 10:37:34 +00:00
|
|
|
if self.filters.get('group_by_party'):
|
|
|
|
self.append_subtotal_row(self.previous_party)
|
2020-06-03 16:21:21 +00:00
|
|
|
if self.data:
|
|
|
|
self.data.append(self.total_row_map.get('Total'))
|
2020-02-18 10:37:34 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def append_row(self, row):
|
|
|
|
self.allocate_future_payments(row)
|
|
|
|
self.set_invoice_details(row)
|
|
|
|
self.set_party_details(row)
|
|
|
|
self.set_ageing(row)
|
2020-02-18 10:37:34 +00:00
|
|
|
|
|
|
|
if self.filters.get('group_by_party'):
|
|
|
|
self.update_sub_total_row(row, row.party)
|
|
|
|
if self.previous_party and (self.previous_party != row.party):
|
|
|
|
self.append_subtotal_row(self.previous_party)
|
|
|
|
self.previous_party = row.party
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
self.data.append(row)
|
|
|
|
|
|
|
|
def set_invoice_details(self, row):
|
2019-11-13 12:28:10 +00:00
|
|
|
invoice_details = self.invoice_details.get(row.voucher_no, {})
|
|
|
|
if row.due_date:
|
|
|
|
invoice_details.pop("due_date", None)
|
|
|
|
row.update(invoice_details)
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if row.voucher_type == 'Sales Invoice':
|
|
|
|
if self.filters.show_delivery_notes:
|
|
|
|
self.set_delivery_notes(row)
|
|
|
|
|
|
|
|
if self.filters.show_sales_person and row.sales_team:
|
|
|
|
row.sales_person = ", ".join(row.sales_team)
|
|
|
|
del row['sales_team']
|
|
|
|
|
|
|
|
def set_delivery_notes(self, row):
|
|
|
|
delivery_notes = self.delivery_notes.get(row.voucher_no, [])
|
|
|
|
if delivery_notes:
|
|
|
|
row.delivery_notes = ', '.join(delivery_notes)
|
|
|
|
|
|
|
|
def build_delivery_note_map(self):
|
|
|
|
if self.invoices and self.filters.show_delivery_notes:
|
|
|
|
self.delivery_notes = frappe._dict()
|
|
|
|
|
|
|
|
# delivery note link inside sales invoice
|
|
|
|
si_against_dn = frappe.db.sql("""
|
|
|
|
select parent, delivery_note
|
|
|
|
from `tabSales Invoice Item`
|
|
|
|
where docstatus=1 and parent in (%s)
|
|
|
|
""" % (','.join(['%s'] * len(self.invoices))), tuple(self.invoices), as_dict=1)
|
|
|
|
|
|
|
|
for d in si_against_dn:
|
|
|
|
if d.delivery_note:
|
|
|
|
self.delivery_notes.setdefault(d.parent, set()).add(d.delivery_note)
|
|
|
|
|
|
|
|
dn_against_si = frappe.db.sql("""
|
|
|
|
select distinct parent, against_sales_invoice
|
|
|
|
from `tabDelivery Note Item`
|
|
|
|
where against_sales_invoice in (%s)
|
|
|
|
""" % (','.join(['%s'] * len(self.invoices))), tuple(self.invoices) , as_dict=1)
|
|
|
|
|
|
|
|
for d in dn_against_si:
|
|
|
|
self.delivery_notes.setdefault(d.against_sales_invoice, set()).add(d.parent)
|
|
|
|
|
|
|
|
def get_invoice_details(self):
|
|
|
|
self.invoice_details = frappe._dict()
|
|
|
|
if self.party_type == "Customer":
|
|
|
|
si_list = frappe.db.sql("""
|
|
|
|
select name, due_date, po_no
|
|
|
|
from `tabSales Invoice`
|
|
|
|
where posting_date <= %s
|
|
|
|
""",self.filters.report_date, as_dict=1)
|
|
|
|
for d in si_list:
|
|
|
|
self.invoice_details.setdefault(d.name, d)
|
|
|
|
|
|
|
|
# Get Sales Team
|
|
|
|
if self.filters.show_sales_person:
|
|
|
|
sales_team = frappe.db.sql("""
|
|
|
|
select parent, sales_person
|
|
|
|
from `tabSales Team`
|
|
|
|
where parenttype = 'Sales Invoice'
|
|
|
|
""", as_dict=1)
|
|
|
|
for d in sales_team:
|
|
|
|
self.invoice_details.setdefault(d.parent, {})\
|
|
|
|
.setdefault('sales_team', []).append(d.sales_person)
|
|
|
|
|
|
|
|
if self.party_type == "Supplier":
|
|
|
|
for pi in frappe.db.sql("""
|
|
|
|
select name, due_date, bill_no, bill_date
|
|
|
|
from `tabPurchase Invoice`
|
|
|
|
where posting_date <= %s
|
|
|
|
""", self.filters.report_date, as_dict=1):
|
|
|
|
self.invoice_details.setdefault(pi.name, pi)
|
|
|
|
|
|
|
|
# Invoices booked via Journal Entries
|
|
|
|
journal_entries = frappe.db.sql("""
|
|
|
|
select name, due_date, bill_no, bill_date
|
|
|
|
from `tabJournal Entry`
|
|
|
|
where posting_date <= %s
|
|
|
|
""", self.filters.report_date, as_dict=1)
|
|
|
|
|
|
|
|
for je in journal_entries:
|
|
|
|
if je.bill_no:
|
|
|
|
self.invoice_details.setdefault(je.name, je)
|
|
|
|
|
|
|
|
def set_party_details(self, row):
|
|
|
|
# customer / supplier name
|
2019-12-23 10:31:38 +00:00
|
|
|
party_details = self.get_party_details(row.party) or {}
|
2019-09-03 10:37:46 +00:00
|
|
|
row.update(party_details)
|
|
|
|
if self.filters.get(scrub(self.filters.party_type)):
|
|
|
|
row.currency = row.account_currency
|
2018-12-07 02:45:05 +00:00
|
|
|
else:
|
2019-09-03 10:37:46 +00:00
|
|
|
row.currency = self.company_currency
|
2018-12-07 02:45:05 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def allocate_outstanding_based_on_payment_terms(self, row):
|
|
|
|
self.get_payment_terms(row)
|
|
|
|
for term in row.payment_terms:
|
2014-10-30 10:19:17 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
# update "paid" and "oustanding" for this term
|
2020-04-23 10:37:36 +00:00
|
|
|
if not term.paid:
|
|
|
|
self.allocate_closing_to_term(row, term, 'paid')
|
2019-01-04 05:24:24 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
# update "credit_note" and "oustanding" for this term
|
|
|
|
if term.outstanding:
|
|
|
|
self.allocate_closing_to_term(row, term, 'credit_note')
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2020-04-23 10:37:36 +00:00
|
|
|
row.payment_terms = sorted(row.payment_terms, key=lambda x: x['due_date'])
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_payment_terms(self, row):
|
|
|
|
# build payment_terms for row
|
|
|
|
payment_terms_details = frappe.db.sql("""
|
|
|
|
select
|
|
|
|
si.name, si.party_account_currency, si.currency, si.conversion_rate,
|
2021-05-04 06:56:49 +00:00
|
|
|
ps.due_date, ps.payment_term, ps.payment_amount, ps.description, ps.paid_amount, ps.discounted_amount
|
2019-09-03 10:37:46 +00:00
|
|
|
from `tab{0}` si, `tabPayment Schedule` ps
|
|
|
|
where
|
|
|
|
si.name = ps.parent and
|
|
|
|
si.name = %s
|
2020-04-23 10:37:36 +00:00
|
|
|
order by ps.paid_amount desc, due_date
|
2019-09-03 10:37:46 +00:00
|
|
|
""".format(row.voucher_type), row.voucher_no, as_dict = 1)
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2014-10-30 10:19:17 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
original_row = frappe._dict(row)
|
|
|
|
row.payment_terms = []
|
2018-08-07 07:31:11 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
# If no or single payment terms, no need to split the row
|
|
|
|
if len(payment_terms_details) <= 1:
|
|
|
|
return
|
2018-11-12 09:15:33 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
for d in payment_terms_details:
|
|
|
|
term = frappe._dict(original_row)
|
|
|
|
self.append_payment_term(row, d, term)
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def append_payment_term(self, row, d, term):
|
2019-11-29 11:26:27 +00:00
|
|
|
if (self.filters.get("customer") or self.filters.get("supplier")) and d.currency == d.party_account_currency:
|
2019-09-03 10:37:46 +00:00
|
|
|
invoiced = d.payment_amount
|
2018-11-12 05:47:39 +00:00
|
|
|
else:
|
2019-09-03 10:37:46 +00:00
|
|
|
invoiced = flt(flt(d.payment_amount) * flt(d.conversion_rate), self.currency_precision)
|
|
|
|
|
|
|
|
row.payment_terms.append(term.update({
|
|
|
|
"due_date": d.due_date,
|
|
|
|
"invoiced": invoiced,
|
|
|
|
"invoice_grand_total": row.invoiced,
|
2021-05-04 06:56:49 +00:00
|
|
|
"payment_term": d.description or d.payment_term,
|
2021-03-31 09:33:53 +00:00
|
|
|
"paid": d.paid_amount + d.discounted_amount,
|
2019-09-03 10:37:46 +00:00
|
|
|
"credit_note": 0.0,
|
2021-03-31 09:33:53 +00:00
|
|
|
"outstanding": invoiced - d.paid_amount - d.discounted_amount
|
2019-09-03 10:37:46 +00:00
|
|
|
}))
|
|
|
|
|
2020-04-23 10:37:36 +00:00
|
|
|
if d.paid_amount:
|
2021-03-31 09:33:53 +00:00
|
|
|
row['paid'] -= d.paid_amount + d.discounted_amount
|
2020-04-23 10:37:36 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def allocate_closing_to_term(self, row, term, key):
|
|
|
|
if row[key]:
|
|
|
|
if row[key] > term.outstanding:
|
|
|
|
term[key] = term.outstanding
|
|
|
|
row[key] -= term.outstanding
|
|
|
|
else:
|
|
|
|
term[key] = row[key]
|
|
|
|
row[key] = 0
|
|
|
|
term.outstanding -= term[key]
|
|
|
|
|
|
|
|
def allocate_extra_payments_or_credits(self, row):
|
|
|
|
# allocate extra payments / credits
|
|
|
|
additional_row = None
|
|
|
|
for key in ('paid', 'credit_note'):
|
|
|
|
if row[key] > 0:
|
|
|
|
if not additional_row:
|
|
|
|
additional_row = frappe._dict(row)
|
|
|
|
additional_row.invoiced = 0.0
|
|
|
|
additional_row[key] = row[key]
|
|
|
|
|
|
|
|
if additional_row:
|
|
|
|
additional_row.outstanding = additional_row.invoiced - additional_row.paid - additional_row.credit_note
|
|
|
|
self.append_row(additional_row)
|
|
|
|
|
|
|
|
def get_future_payments(self):
|
|
|
|
if self.filters.show_future_payments:
|
|
|
|
self.future_payments = frappe._dict()
|
|
|
|
future_payments = list(self.get_future_payments_from_payment_entry())
|
|
|
|
future_payments += list(self.get_future_payments_from_journal_entry())
|
|
|
|
if future_payments:
|
|
|
|
for d in future_payments:
|
|
|
|
if d.future_amount and d.invoice_no:
|
|
|
|
self.future_payments.setdefault((d.invoice_no, d.party), []).append(d)
|
|
|
|
|
|
|
|
def get_future_payments_from_payment_entry(self):
|
|
|
|
return frappe.db.sql("""
|
|
|
|
select
|
|
|
|
ref.reference_name as invoice_no,
|
|
|
|
payment_entry.party,
|
|
|
|
payment_entry.party_type,
|
|
|
|
payment_entry.posting_date as future_date,
|
|
|
|
ref.allocated_amount as future_amount,
|
|
|
|
payment_entry.reference_no as future_ref
|
|
|
|
from
|
|
|
|
`tabPayment Entry` as payment_entry inner join `tabPayment Entry Reference` as ref
|
|
|
|
on
|
|
|
|
(ref.parent = payment_entry.name)
|
|
|
|
where
|
2019-09-11 13:09:49 +00:00
|
|
|
payment_entry.docstatus < 2
|
2019-09-03 10:37:46 +00:00
|
|
|
and payment_entry.posting_date > %s
|
|
|
|
and payment_entry.party_type = %s
|
|
|
|
""", (self.filters.report_date, self.party_type), as_dict=1)
|
|
|
|
|
|
|
|
def get_future_payments_from_journal_entry(self):
|
|
|
|
if self.filters.get('party'):
|
|
|
|
amount_field = ("jea.debit_in_account_currency - jea.credit_in_account_currency"
|
|
|
|
if self.party_type == 'Supplier' else "jea.credit_in_account_currency - jea.debit_in_account_currency")
|
2018-11-12 05:47:39 +00:00
|
|
|
else:
|
2019-09-03 10:37:46 +00:00
|
|
|
amount_field = ("jea.debit - " if self.party_type == 'Supplier' else "jea.credit")
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
return frappe.db.sql("""
|
|
|
|
select
|
|
|
|
jea.reference_name as invoice_no,
|
|
|
|
jea.party,
|
|
|
|
jea.party_type,
|
|
|
|
je.posting_date as future_date,
|
|
|
|
sum({0}) as future_amount,
|
|
|
|
je.cheque_no as future_ref
|
|
|
|
from
|
|
|
|
`tabJournal Entry` as je inner join `tabJournal Entry Account` as jea
|
|
|
|
on
|
|
|
|
(jea.parent = je.name)
|
|
|
|
where
|
2019-09-11 13:09:49 +00:00
|
|
|
je.docstatus < 2
|
2019-09-03 10:37:46 +00:00
|
|
|
and je.posting_date > %s
|
|
|
|
and jea.party_type = %s
|
|
|
|
and jea.reference_name is not null and jea.reference_name != ''
|
|
|
|
group by je.name, jea.reference_name
|
|
|
|
having future_amount > 0
|
|
|
|
""".format(amount_field), (self.filters.report_date, self.party_type), as_dict=1)
|
|
|
|
|
|
|
|
def allocate_future_payments(self, row):
|
|
|
|
# future payments are captured in additional columns
|
|
|
|
# this method allocates pending future payments against a voucher to
|
|
|
|
# the current row (which could be generated from payment terms)
|
|
|
|
if not self.filters.show_future_payments:
|
|
|
|
return
|
|
|
|
|
|
|
|
row.remaining_balance = row.outstanding
|
|
|
|
row.future_amount = 0.0
|
|
|
|
for future in self.future_payments.get((row.voucher_no, row.party), []):
|
|
|
|
if row.remaining_balance > 0 and future.future_amount:
|
|
|
|
if future.future_amount > row.outstanding:
|
|
|
|
row.future_amount = row.outstanding
|
|
|
|
future.future_amount = future.future_amount - row.outstanding
|
|
|
|
row.remaining_balance = 0
|
|
|
|
else:
|
|
|
|
row.future_amount += future.future_amount
|
|
|
|
future.future_amount = 0
|
|
|
|
row.remaining_balance = row.outstanding - row.future_amount
|
2018-09-19 13:33:40 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
row.setdefault('future_ref', []).append(cstr(future.future_ref) + '/' + cstr(future.future_date))
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if row.future_ref:
|
|
|
|
row.future_ref = ', '.join(row.future_ref)
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2019-09-24 14:22:33 +00:00
|
|
|
def get_return_entries(self):
|
|
|
|
doctype = "Sales Invoice" if self.party_type == "Customer" else "Purchase Invoice"
|
|
|
|
filters={
|
|
|
|
'is_return': 1,
|
|
|
|
'docstatus': 1
|
|
|
|
}
|
|
|
|
party_field = scrub(self.filters.party_type)
|
|
|
|
if self.filters.get(party_field):
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def set_ageing(self, row):
|
|
|
|
if self.filters.ageing_based_on == "Due Date":
|
2021-12-06 09:35:20 +00:00
|
|
|
# use posting date as a fallback for advances posted via journal and payment entry
|
|
|
|
# when ageing viewed by due date
|
|
|
|
entry_date = row.due_date or row.posting_date
|
2019-09-03 10:37:46 +00:00
|
|
|
elif self.filters.ageing_based_on == "Supplier Invoice Date":
|
|
|
|
entry_date = row.bill_date
|
|
|
|
else:
|
|
|
|
entry_date = row.posting_date
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
self.get_ageing_data(entry_date, row)
|
2019-01-04 05:24:24 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
# ageing buckets should not have amounts if due date is not reached
|
|
|
|
if getdate(entry_date) > getdate(self.filters.report_date):
|
|
|
|
row.range1 = row.range2 = row.range3 = row.range4 = row.range5 = 0.0
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2021-08-22 12:40:51 +00:00
|
|
|
row.total_due = row.range1 + row.range2 + row.range3 + row.range4 + row.range5
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_ageing_data(self, entry_date, row):
|
|
|
|
# [0-30, 30-60, 60-90, 90-120, 120-above]
|
2020-05-27 14:59:10 +00:00
|
|
|
row.range1 = row.range2 = row.range3 = row.range4 = row.range5 = 0.0
|
2014-01-09 10:19:26 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if not (self.age_as_on and entry_date):
|
|
|
|
return
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
row.age = (getdate(self.age_as_on) - getdate(entry_date)).days or 0
|
|
|
|
index = None
|
2019-09-19 12:03:18 +00:00
|
|
|
|
|
|
|
if not (self.filters.range1 and self.filters.range2 and self.filters.range3 and self.filters.range4):
|
|
|
|
self.filters.range1, self.filters.range2, self.filters.range3, self.filters.range4 = 30, 60, 90, 120
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
for i, days in enumerate([self.filters.range1, self.filters.range2, self.filters.range3, self.filters.range4]):
|
2020-05-11 06:44:46 +00:00
|
|
|
if cint(row.age) <= cint(days):
|
2019-09-03 10:37:46 +00:00
|
|
|
index = i
|
|
|
|
break
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if index is None: index = 4
|
|
|
|
row['range' + str(index+1)] = row.outstanding
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_gl_entries(self):
|
|
|
|
# get all the GL entries filtered by the given filters
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
conditions, values = self.prepare_conditions()
|
2020-02-18 10:37:34 +00:00
|
|
|
order_by = self.get_order_by_condition()
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2020-05-27 07:16:12 +00:00
|
|
|
if self.filters.show_future_payments:
|
|
|
|
values.insert(2, self.filters.report_date)
|
|
|
|
|
|
|
|
date_condition = """AND (posting_date <= %s
|
|
|
|
OR (against_voucher IS NULL AND DATE(creation) <= %s))"""
|
|
|
|
else:
|
|
|
|
date_condition = "AND posting_date <=%s"
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if self.filters.get(scrub(self.party_type)):
|
|
|
|
select_fields = "debit_in_account_currency as debit, credit_in_account_currency as credit"
|
2018-08-14 05:21:13 +00:00
|
|
|
else:
|
2019-09-03 10:37:46 +00:00
|
|
|
select_fields = "debit, credit"
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2021-11-29 18:23:28 +00:00
|
|
|
doc_currency_fields = "debit_in_account_currency, credit_in_account_currency"
|
|
|
|
|
2021-09-09 06:27:29 +00:00
|
|
|
remarks = ", remarks" if self.filters.get("show_remarks") else ""
|
|
|
|
|
2018-08-14 05:21:13 +00:00
|
|
|
self.gl_entries = frappe.db.sql("""
|
|
|
|
select
|
2020-11-09 14:50:05 +00:00
|
|
|
name, posting_date, account, party_type, party, voucher_type, voucher_no, cost_center,
|
2021-11-29 18:23:28 +00:00
|
|
|
against_voucher_type, against_voucher, account_currency, {0}, {1} {remarks}
|
2018-08-14 05:21:13 +00:00
|
|
|
from
|
|
|
|
`tabGL Entry`
|
|
|
|
where
|
2019-09-03 10:37:46 +00:00
|
|
|
docstatus < 2
|
2021-06-09 12:26:41 +00:00
|
|
|
and is_cancelled = 0
|
2019-09-03 10:37:46 +00:00
|
|
|
and party_type=%s
|
|
|
|
and (party is not null and party != '')
|
2021-11-29 18:23:28 +00:00
|
|
|
{2} {3} {4}"""
|
|
|
|
.format(select_fields, doc_currency_fields, date_condition, conditions, order_by, remarks=remarks), values, as_dict=True)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2019-12-04 10:00:58 +00:00
|
|
|
def get_sales_invoices_or_customers_based_on_sales_person(self):
|
|
|
|
if self.filters.get("sales_person"):
|
|
|
|
lft, rgt = frappe.db.get_value("Sales Person",
|
|
|
|
self.filters.get("sales_person"), ["lft", "rgt"])
|
|
|
|
|
|
|
|
records = frappe.db.sql("""
|
|
|
|
select distinct parent, parenttype
|
|
|
|
from `tabSales Team` steam
|
|
|
|
where parenttype in ('Customer', 'Sales Invoice')
|
|
|
|
and exists(select name from `tabSales Person` where lft >= %s and rgt <= %s and name = steam.sales_person)
|
|
|
|
""", (lft, rgt), as_dict=1)
|
|
|
|
|
|
|
|
self.sales_person_records = frappe._dict()
|
|
|
|
for d in records:
|
|
|
|
self.sales_person_records.setdefault(d.parenttype, set()).add(d.parent)
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def prepare_conditions(self):
|
2013-11-07 15:14:30 +00:00
|
|
|
conditions = [""]
|
2019-09-03 10:37:46 +00:00
|
|
|
values = [self.party_type, self.filters.report_date]
|
|
|
|
party_type_field = scrub(self.party_type)
|
|
|
|
|
|
|
|
self.add_common_filters(conditions, values, party_type_field)
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if party_type_field=="customer":
|
|
|
|
self.add_customer_filters(conditions, values)
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
elif party_type_field=="supplier":
|
|
|
|
self.add_supplier_filters(conditions, values)
|
|
|
|
|
2020-09-18 07:56:46 +00:00
|
|
|
if self.filters.cost_center:
|
|
|
|
self.get_cost_center_conditions(conditions)
|
|
|
|
|
2019-09-04 05:34:27 +00:00
|
|
|
self.add_accounting_dimensions_filters(conditions, values)
|
2019-09-03 10:37:46 +00:00
|
|
|
return " and ".join(conditions), values
|
|
|
|
|
2020-09-18 07:56:46 +00:00
|
|
|
def get_cost_center_conditions(self, conditions):
|
|
|
|
lft, rgt = frappe.db.get_value("Cost Center", self.filters.cost_center, ["lft", "rgt"])
|
|
|
|
cost_center_list = [center.name for center in frappe.get_list("Cost Center", filters = {'lft': (">=", lft), 'rgt': ("<=", rgt)})]
|
|
|
|
|
|
|
|
cost_center_string = '", "'.join(cost_center_list)
|
|
|
|
conditions.append('cost_center in ("{0}")'.format(cost_center_string))
|
|
|
|
|
2020-02-18 10:37:34 +00:00
|
|
|
def get_order_by_condition(self):
|
|
|
|
if self.filters.get('group_by_party'):
|
|
|
|
return "order by party, posting_date"
|
|
|
|
else:
|
|
|
|
return "order by posting_date, party"
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def add_common_filters(self, conditions, values, party_type_field):
|
2013-11-07 15:14:30 +00:00
|
|
|
if self.filters.company:
|
2015-04-17 10:25:19 +00:00
|
|
|
conditions.append("company=%s")
|
|
|
|
values.append(self.filters.company)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2019-04-16 13:46:14 +00:00
|
|
|
if self.filters.finance_book:
|
2019-09-03 10:37:46 +00:00
|
|
|
conditions.append("ifnull(finance_book, '') in (%s, '')")
|
2018-04-22 21:27:10 +00:00
|
|
|
values.append(self.filters.finance_book)
|
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
if self.filters.get(party_type_field):
|
2015-04-28 11:15:20 +00:00
|
|
|
conditions.append("party=%s")
|
2015-09-11 10:52:37 +00:00
|
|
|
values.append(self.filters.get(party_type_field))
|
2015-04-21 11:46:28 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
# 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})]
|
2020-08-03 15:11:25 +00:00
|
|
|
|
|
|
|
if accounts:
|
|
|
|
conditions.append("account in (%s)" % ','.join(['%s'] *len(accounts)))
|
|
|
|
values += accounts
|
2019-09-03 10:37:46 +00:00
|
|
|
|
|
|
|
def add_customer_filters(self, conditions, values):
|
|
|
|
if self.filters.get("customer_group"):
|
|
|
|
conditions.append(self.get_hierarchical_filters('Customer Group', 'customer_group'))
|
|
|
|
|
|
|
|
if self.filters.get("territory"):
|
|
|
|
conditions.append(self.get_hierarchical_filters('Territory', 'territory'))
|
|
|
|
|
|
|
|
if self.filters.get("payment_terms_template"):
|
|
|
|
conditions.append("party in (select name from tabCustomer where payment_terms=%s)")
|
|
|
|
values.append(self.filters.get("payment_terms_template"))
|
|
|
|
|
|
|
|
if self.filters.get("sales_partner"):
|
|
|
|
conditions.append("party in (select name from tabCustomer where default_sales_partner=%s)")
|
|
|
|
values.append(self.filters.get("sales_partner"))
|
|
|
|
|
|
|
|
def add_supplier_filters(self, conditions, values):
|
|
|
|
if self.filters.get("supplier_group"):
|
|
|
|
conditions.append("""party in (select name from tabSupplier
|
|
|
|
where supplier_group=%s)""")
|
|
|
|
values.append(self.filters.get("supplier_group"))
|
|
|
|
|
|
|
|
if self.filters.get("payment_terms_template"):
|
|
|
|
conditions.append("party in (select name from tabSupplier where payment_terms=%s)")
|
|
|
|
values.append(self.filters.get("payment_terms_template"))
|
|
|
|
|
|
|
|
def get_hierarchical_filters(self, doctype, key):
|
|
|
|
lft, rgt = frappe.db.get_value(doctype, self.filters.get(key), ["lft", "rgt"])
|
|
|
|
|
|
|
|
return """party in (select name from tabCustomer
|
|
|
|
where exists(select name from `tab{doctype}` where lft >= {lft} and rgt <= {rgt}
|
|
|
|
and name=tabCustomer.{key}))""".format(
|
|
|
|
doctype=doctype, lft=lft, rgt=rgt, key=key)
|
|
|
|
|
|
|
|
def add_accounting_dimensions_filters(self, conditions, values):
|
2020-03-17 05:23:24 +00:00
|
|
|
accounting_dimensions = get_accounting_dimensions(as_list=False)
|
2018-12-06 09:29:54 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if accounting_dimensions:
|
|
|
|
for dimension in accounting_dimensions:
|
2020-03-17 05:23:24 +00:00
|
|
|
if self.filters.get(dimension.fieldname):
|
|
|
|
if frappe.get_cached_value('DocType', dimension.document_type, 'is_tree'):
|
|
|
|
self.filters[dimension.fieldname] = get_dimension_with_children(dimension.document_type,
|
|
|
|
self.filters.get(dimension.fieldname))
|
|
|
|
conditions.append("{0} in %s".format(dimension.fieldname))
|
|
|
|
values.append(tuple(self.filters.get(dimension.fieldname)))
|
2018-01-24 12:46:08 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_gle_balance(self, gle):
|
|
|
|
# get the balance of the GL (debit - credit) or reverse balance based on report type
|
|
|
|
return gle.get(self.dr_or_cr) - self.get_reverse_balance(gle)
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2021-11-29 18:23:28 +00:00
|
|
|
def get_gle_balance_in_account_currency(self, gle):
|
|
|
|
# get the balance of the GL (debit - credit) or reverse balance based on report type
|
|
|
|
return gle.get(self.dr_or_cr + '_in_account_currency') - self.get_reverse_balance_in_account_currency(gle)
|
|
|
|
|
|
|
|
def get_reverse_balance_in_account_currency(self, gle):
|
|
|
|
return gle.get('debit_in_account_currency' if self.dr_or_cr=='credit' else 'credit_in_account_currency')
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_reverse_balance(self, gle):
|
|
|
|
# get "credit" balance if report type is "debit" and vice versa
|
|
|
|
return gle.get('debit' if self.dr_or_cr=='credit' else 'credit')
|
2017-04-24 11:33:50 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def is_invoice(self, gle):
|
|
|
|
if gle.voucher_type in ('Sales Invoice', 'Purchase Invoice'):
|
|
|
|
return True
|
2018-01-24 12:46:08 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_party_details(self, party):
|
|
|
|
if not party in self.party_details:
|
|
|
|
if self.party_type == 'Customer':
|
|
|
|
self.party_details[party] = frappe.db.get_value('Customer', party, ['customer_name',
|
|
|
|
'territory', 'customer_group', 'customer_primary_contact'], as_dict=True)
|
|
|
|
else:
|
|
|
|
self.party_details[party] = frappe.db.get_value('Supplier', party, ['supplier_name',
|
|
|
|
'supplier_group'], as_dict=True)
|
2018-12-10 12:26:42 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
return self.party_details[party]
|
2018-08-20 12:28:28 +00:00
|
|
|
|
2018-12-06 09:29:54 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_columns(self):
|
|
|
|
self.columns = []
|
|
|
|
self.add_column('Posting Date', fieldtype='Date')
|
|
|
|
self.add_column(label=_(self.party_type), fieldname='party',
|
|
|
|
fieldtype='Link', options=self.party_type, width=180)
|
2019-07-15 20:22:03 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if self.party_naming_by == "Naming Series":
|
|
|
|
self.add_column(_('{0} Name').format(self.party_type),
|
|
|
|
fieldname = scrub(self.party_type) + '_name', fieldtype='Data')
|
2019-03-18 06:23:04 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if self.party_type == 'Customer':
|
|
|
|
self.add_column(_("Customer Contact"), fieldname='customer_primary_contact',
|
|
|
|
fieldtype='Link', options='Contact')
|
2019-03-18 06:23:04 +00:00
|
|
|
|
2020-11-09 14:50:05 +00:00
|
|
|
self.add_column(label=_('Cost Center'), fieldname='cost_center', fieldtype='Data')
|
2019-09-03 10:37:46 +00:00
|
|
|
self.add_column(label=_('Voucher Type'), fieldname='voucher_type', fieldtype='Data')
|
|
|
|
self.add_column(label=_('Voucher No'), fieldname='voucher_no', fieldtype='Dynamic Link',
|
|
|
|
options='voucher_type', width=180)
|
2021-09-09 06:27:29 +00:00
|
|
|
|
|
|
|
if self.filters.show_remarks:
|
|
|
|
self.add_column(label=_('Remarks'), fieldname='remarks', fieldtype='Text', width=200),
|
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
self.add_column(label='Due Date', fieldtype='Date')
|
2019-02-19 11:41:50 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if self.party_type == "Supplier":
|
|
|
|
self.add_column(label=_('Bill No'), fieldname='bill_no', fieldtype='Data')
|
|
|
|
self.add_column(label=_('Bill Date'), fieldname='bill_date', fieldtype='Date')
|
2019-07-07 15:54:45 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
if self.filters.based_on_payment_terms:
|
|
|
|
self.add_column(label=_('Payment Term'), fieldname='payment_term', fieldtype='Data')
|
|
|
|
self.add_column(label=_('Invoice Grand Total'), fieldname='invoice_grand_total')
|
2018-11-12 05:47:39 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
self.add_column(_('Invoiced Amount'), fieldname='invoiced')
|
|
|
|
self.add_column(_('Paid Amount'), fieldname='paid')
|
|
|
|
if self.party_type == "Customer":
|
|
|
|
self.add_column(_('Credit Note'), fieldname='credit_note')
|
|
|
|
else:
|
|
|
|
# note: fieldname is still `credit_note`
|
|
|
|
self.add_column(_('Debit Note'), fieldname='credit_note')
|
|
|
|
self.add_column(_('Outstanding Amount'), fieldname='outstanding')
|
|
|
|
|
|
|
|
self.setup_ageing_columns()
|
|
|
|
|
|
|
|
self.add_column(label=_('Currency'), fieldname='currency', fieldtype='Link', options='Currency', width=80)
|
|
|
|
|
|
|
|
if self.filters.show_future_payments:
|
|
|
|
self.add_column(label=_('Future Payment Ref'), fieldname='future_ref', fieldtype='Data')
|
|
|
|
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':
|
|
|
|
self.add_column(label=_('Customer LPO'), fieldname='po_no', fieldtype='Data')
|
|
|
|
|
|
|
|
# comma separated list of linked delivery notes
|
|
|
|
if self.filters.show_delivery_notes:
|
|
|
|
self.add_column(label=_('Delivery Notes'), fieldname='delivery_notes', fieldtype='Data')
|
|
|
|
self.add_column(label=_('Territory'), fieldname='territory', fieldtype='Link',
|
|
|
|
options='Territory')
|
|
|
|
self.add_column(label=_('Customer Group'), fieldname='customer_group', fieldtype='Link',
|
|
|
|
options='Customer Group')
|
|
|
|
if self.filters.show_sales_person:
|
|
|
|
self.add_column(label=_('Sales Person'), fieldname='sales_person', fieldtype='Data')
|
|
|
|
|
|
|
|
if self.filters.party_type == "Supplier":
|
|
|
|
self.add_column(label=_('Supplier Group'), fieldname='supplier_group', fieldtype='Link',
|
|
|
|
options='Supplier Group')
|
|
|
|
|
|
|
|
def add_column(self, label, fieldname=None, fieldtype='Currency', options=None, width=120):
|
|
|
|
if not fieldname: fieldname = scrub(label)
|
|
|
|
if fieldtype=='Currency': options='currency'
|
|
|
|
if fieldtype=='Date': width = 90
|
|
|
|
|
|
|
|
self.columns.append(dict(
|
|
|
|
label=label,
|
|
|
|
fieldname=fieldname,
|
|
|
|
fieldtype=fieldtype,
|
|
|
|
options=options,
|
|
|
|
width=width
|
|
|
|
))
|
|
|
|
|
|
|
|
def setup_ageing_columns(self):
|
|
|
|
# for charts
|
|
|
|
self.ageing_column_labels = []
|
|
|
|
self.add_column(label=_('Age (Days)'), fieldname='age', fieldtype='Int', width=80)
|
|
|
|
|
|
|
|
for i, label in enumerate(["0-{range1}".format(range1=self.filters["range1"]),
|
|
|
|
"{range1}-{range2}".format(range1=cint(self.filters["range1"])+ 1, range2=self.filters["range2"]),
|
|
|
|
"{range2}-{range3}".format(range2=cint(self.filters["range2"])+ 1, range3=self.filters["range3"]),
|
|
|
|
"{range3}-{range4}".format(range3=cint(self.filters["range3"])+ 1, range4=self.filters["range4"]),
|
|
|
|
"{range4}-{above}".format(range4=cint(self.filters["range4"])+ 1, above=_("Above"))]):
|
|
|
|
self.add_column(label=label, fieldname='range' + str(i+1))
|
|
|
|
self.ageing_column_labels.append(label)
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
def get_chart_data(self):
|
2016-05-27 06:51:28 +00:00
|
|
|
rows = []
|
2019-09-03 10:37:46 +00:00
|
|
|
for row in self.data:
|
2020-02-18 10:37:34 +00:00
|
|
|
row = frappe._dict(row)
|
|
|
|
if not cint(row.bold):
|
|
|
|
values = [row.range1, row.range2, row.range3, row.range4, row.range5]
|
|
|
|
precision = cint(frappe.db.get_default("float_precision")) or 2
|
|
|
|
rows.append({
|
|
|
|
'values': [flt(val, precision) for val in values]
|
|
|
|
})
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2019-09-03 10:37:46 +00:00
|
|
|
self.chart = {
|
2016-05-20 06:13:30 +00:00
|
|
|
"data": {
|
2019-09-03 10:37:46 +00:00
|
|
|
'labels': self.ageing_column_labels,
|
2017-10-31 07:28:48 +00:00
|
|
|
'datasets': rows
|
2016-05-26 09:55:52 +00:00
|
|
|
},
|
2017-10-17 06:33:02 +00:00
|
|
|
"type": 'percentage'
|
2019-09-04 05:34:27 +00:00
|
|
|
}
|