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
|
|
|
|
|
2013-04-22 07:44:52 +00:00
|
|
|
from __future__ import unicode_literals
|
2018-05-17 17:59:37 +00:00
|
|
|
import frappe, erpnext
|
2014-10-16 13:32:58 +00:00
|
|
|
from frappe import _, scrub
|
2018-12-07 02:45:05 +00:00
|
|
|
from frappe.utils import getdate, nowdate, flt, cint, formatdate, cstr
|
2019-07-07 15:54:45 +00:00
|
|
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_accounting_dimensions
|
2013-04-22 07:44:52 +00:00
|
|
|
|
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):
|
|
|
|
party_naming_by = frappe.db.get_value(args.get("naming_by")[0], None, args.get("naming_by")[1])
|
2016-05-20 06:13:30 +00:00
|
|
|
columns = self.get_columns(party_naming_by, args)
|
|
|
|
data = self.get_data(party_naming_by, args)
|
2016-05-26 09:55:52 +00:00
|
|
|
chart = self.get_chart_data(columns, data)
|
|
|
|
return columns, data, None, chart
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_columns(self, party_naming_by, args):
|
2018-03-27 13:15:42 +00:00
|
|
|
columns = []
|
|
|
|
columns.append({
|
|
|
|
"label": _("Posting Date"),
|
|
|
|
"fieldtype": "Date",
|
|
|
|
"fieldname": "posting_date",
|
|
|
|
"width": 90
|
|
|
|
})
|
|
|
|
|
|
|
|
columns += [_(args.get("party_type")) + ":Link/" + args.get("party_type") + ":200"]
|
2014-01-03 07:28:04 +00:00
|
|
|
|
2019-01-04 05:24:24 +00:00
|
|
|
if args.get("party_type") == 'Customer':
|
|
|
|
columns.append({
|
|
|
|
"label": _("Customer Contact"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"fieldname": "contact",
|
|
|
|
"options":"Contact",
|
|
|
|
"width": 100
|
|
|
|
})
|
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
if party_naming_by == "Naming Series":
|
|
|
|
columns += [args.get("party_type") + " Name::110"]
|
2014-01-03 07:14:00 +00:00
|
|
|
|
2018-03-27 13:15:42 +00:00
|
|
|
columns.append({
|
|
|
|
"label": _("Voucher Type"),
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"fieldname": "voucher_type",
|
|
|
|
"width": 110
|
|
|
|
})
|
|
|
|
|
|
|
|
columns.append({
|
|
|
|
"label": _("Voucher No"),
|
|
|
|
"fieldtype": "Dynamic Link",
|
|
|
|
"fieldname": "voucher_no",
|
|
|
|
"width": 110,
|
|
|
|
"options": "voucher_type",
|
|
|
|
})
|
|
|
|
|
|
|
|
columns += [_("Due Date") + ":Date:80"]
|
2014-10-16 13:32:58 +00:00
|
|
|
|
|
|
|
if args.get("party_type") == "Supplier":
|
|
|
|
columns += [_("Bill No") + "::80", _("Bill Date") + ":Date:80"]
|
|
|
|
|
2017-06-07 06:26:53 +00:00
|
|
|
credit_or_debit_note = "Credit Note" if args.get("party_type") == "Customer" else "Debit Note"
|
|
|
|
|
2018-11-12 09:15:33 +00:00
|
|
|
if self.filters.based_on_payment_terms:
|
|
|
|
columns.append({
|
2019-05-30 17:03:02 +00:00
|
|
|
"label": _("Payment Term"),
|
2018-11-12 09:15:33 +00:00
|
|
|
"fieldname": "payment_term",
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"width": 120
|
|
|
|
})
|
|
|
|
columns.append({
|
2019-05-30 17:03:02 +00:00
|
|
|
"label": _("Invoice Grand Total"),
|
2018-11-13 09:37:05 +00:00
|
|
|
"fieldname": "invoice_grand_total",
|
2018-11-12 09:15:33 +00:00
|
|
|
"fieldtype": "Currency",
|
|
|
|
"options": "currency",
|
|
|
|
"width": 120
|
|
|
|
})
|
|
|
|
|
2017-06-07 06:26:53 +00:00
|
|
|
for label in ("Invoiced Amount", "Paid Amount", credit_or_debit_note, "Outstanding Amount"):
|
2015-08-31 11:32:04 +00:00
|
|
|
columns.append({
|
2019-05-30 16:56:52 +00:00
|
|
|
"label": _(label),
|
2018-06-17 05:51:10 +00:00
|
|
|
"fieldname": frappe.scrub(label),
|
2015-08-31 11:32:04 +00:00
|
|
|
"fieldtype": "Currency",
|
|
|
|
"options": "currency",
|
|
|
|
"width": 120
|
|
|
|
})
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2015-10-12 10:17:07 +00:00
|
|
|
columns += [_("Age (Days)") + ":Int:80"]
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2016-05-20 06:13:30 +00:00
|
|
|
self.ageing_col_idx_start = len(columns)
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2015-09-30 11:25:26 +00:00
|
|
|
if not "range1" in self.filters:
|
|
|
|
self.filters["range1"] = "30"
|
|
|
|
if not "range2" in self.filters:
|
|
|
|
self.filters["range2"] = "60"
|
|
|
|
if not "range3" in self.filters:
|
|
|
|
self.filters["range3"] = "90"
|
2019-05-23 09:57:47 +00:00
|
|
|
if not "range4" in self.filters:
|
|
|
|
self.filters["range4"] = "120"
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2016-08-08 06:17:13 +00:00
|
|
|
for label in ("0-{range1}".format(range1=self.filters["range1"]),
|
2016-08-22 07:27:09 +00:00
|
|
|
"{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"]),
|
2019-05-23 09:57:47 +00:00
|
|
|
"{range3}-{range4}".format(range3=cint(self.filters["range3"])+ 1, range4=self.filters["range4"]),
|
|
|
|
"{range4}-{above}".format(range4=cint(self.filters["range4"])+ 1, above=_("Above"))):
|
2015-08-31 11:32:04 +00:00
|
|
|
columns.append({
|
|
|
|
"label": label,
|
2018-10-15 05:38:04 +00:00
|
|
|
"fieldname":label,
|
2015-08-31 11:32:04 +00:00
|
|
|
"fieldtype": "Currency",
|
|
|
|
"options": "currency",
|
|
|
|
"width": 120
|
|
|
|
})
|
2014-01-03 07:14:00 +00:00
|
|
|
|
2018-10-31 12:30:09 +00:00
|
|
|
columns += [
|
|
|
|
{
|
2016-04-11 06:03:14 +00:00
|
|
|
"fieldname": "currency",
|
|
|
|
"label": _("Currency"),
|
2016-08-17 12:54:30 +00:00
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Currency",
|
2016-04-11 06:03:14 +00:00
|
|
|
"width": 100
|
2018-10-31 12:30:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "pdc/lc_ref",
|
|
|
|
"label": _("PDC/LC Ref"),
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"width": 110
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "pdc/lc_amount",
|
|
|
|
"label": _("PDC/LC Amount"),
|
|
|
|
"fieldtype": "Currency",
|
2018-11-13 09:37:05 +00:00
|
|
|
"options": "currency",
|
2018-10-31 12:30:09 +00:00
|
|
|
"width": 130
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "remaining_balance",
|
|
|
|
"label": _("Remaining Balance"),
|
|
|
|
"fieldtype": "Currency",
|
2018-11-13 09:37:05 +00:00
|
|
|
"options": "currency",
|
2018-10-31 12:30:09 +00:00
|
|
|
"width": 130
|
|
|
|
}]
|
2018-02-07 13:02:51 +00:00
|
|
|
|
|
|
|
if args.get('party_type') == 'Customer':
|
2017-04-24 11:33:50 +00:00
|
|
|
columns += [
|
2018-12-10 12:26:42 +00:00
|
|
|
{
|
|
|
|
"label": _("Customer LPO"),
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"fieldname": "po_no",
|
|
|
|
"width": 100,
|
|
|
|
},
|
|
|
|
_("Delivery Note") + ":Data:100",
|
2017-10-17 06:33:02 +00:00
|
|
|
_("Territory") + ":Link/Territory:80",
|
2018-12-10 12:26:42 +00:00
|
|
|
_("Customer Group") + ":Link/Customer Group:120",
|
|
|
|
{
|
|
|
|
"label": _("Sales Person"),
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"fieldname": "sales_person",
|
|
|
|
"width": 120,
|
|
|
|
}
|
2017-04-24 11:33:50 +00:00
|
|
|
]
|
2014-10-16 13:32:58 +00:00
|
|
|
if args.get("party_type") == "Supplier":
|
2018-04-19 13:07:29 +00:00
|
|
|
columns += [_("Supplier Group") + ":Link/Supplier Group:80"]
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2016-04-11 06:03:14 +00:00
|
|
|
columns.append(_("Remarks") + "::200")
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2014-01-03 07:14:00 +00:00
|
|
|
return columns
|
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_data(self, party_naming_by, args):
|
2014-01-24 16:14:36 +00:00
|
|
|
from erpnext.accounts.utils import get_currency_precision
|
2018-11-13 09:37:05 +00:00
|
|
|
self.currency_precision = get_currency_precision() or 2
|
2018-11-12 05:47:39 +00:00
|
|
|
self.dr_or_cr = "debit" if args.get("party_type") == "Customer" else "credit"
|
2014-10-30 10:19:17 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
future_vouchers = self.get_entries_after(self.filters.report_date, args.get("party_type"))
|
2015-09-30 11:25:26 +00:00
|
|
|
|
2016-11-10 13:43:20 +00:00
|
|
|
if not self.filters.get("company"):
|
|
|
|
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")
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2017-06-07 06:26:53 +00:00
|
|
|
return_entries = self.get_return_entries(args.get("party_type"))
|
2015-09-30 11:25:26 +00:00
|
|
|
|
2014-10-30 10:19:17 +00:00
|
|
|
data = []
|
2018-11-12 05:47:39 +00:00
|
|
|
self.pdc_details = get_pdc_details(args.get("party_type"), self.filters.report_date)
|
2018-08-14 05:21:13 +00:00
|
|
|
gl_entries_data = self.get_entries_till(self.filters.report_date, args.get("party_type"))
|
|
|
|
|
|
|
|
if gl_entries_data:
|
|
|
|
voucher_nos = [d.voucher_no for d in gl_entries_data] or []
|
|
|
|
dn_details = get_dn_details(args.get("party_type"), voucher_nos)
|
2018-11-12 05:47:39 +00:00
|
|
|
self.voucher_details = get_voucher_details(args.get("party_type"), voucher_nos, dn_details)
|
|
|
|
|
2019-01-21 04:13:56 +00:00
|
|
|
if self.filters.based_on_payment_terms and gl_entries_data:
|
2018-11-12 05:47:39 +00:00
|
|
|
self.payment_term_map = self.get_payment_term_detail(voucher_nos)
|
2018-08-14 05:21:13 +00:00
|
|
|
|
|
|
|
for gle in gl_entries_data:
|
2019-07-03 05:04:31 +00:00
|
|
|
if self.is_receivable_or_payable(gle, self.dr_or_cr, future_vouchers, return_entries):
|
2018-11-13 06:41:04 +00:00
|
|
|
outstanding_amount, credit_note_amount, payment_amount = self.get_outstanding_amount(
|
2018-11-13 09:37:05 +00:00
|
|
|
gle,self.filters.report_date, self.dr_or_cr, return_entries)
|
2018-12-19 12:42:58 +00:00
|
|
|
temp_outstanding_amt = outstanding_amount
|
|
|
|
temp_credit_note_amt = credit_note_amount
|
|
|
|
|
2018-11-13 09:37:05 +00:00
|
|
|
if abs(outstanding_amount) > 0.1/10**self.currency_precision:
|
2018-11-13 06:41:04 +00:00
|
|
|
if self.filters.based_on_payment_terms and self.payment_term_map.get(gle.voucher_no):
|
2018-11-12 05:47:39 +00:00
|
|
|
for d in self.payment_term_map.get(gle.voucher_no):
|
2018-12-17 12:15:39 +00:00
|
|
|
# Allocate payment amount based on payment terms(FIFO order)
|
2018-12-07 02:45:05 +00:00
|
|
|
payment_amount, d.payment_amount = self.allocate_based_on_fifo(payment_amount, d.payment_term_amount)
|
|
|
|
|
|
|
|
term_outstanding_amount = d.payment_term_amount - d.payment_amount
|
2018-12-17 12:15:39 +00:00
|
|
|
|
|
|
|
# Allocate credit note based on payment terms(FIFO order)
|
2018-12-07 02:45:05 +00:00
|
|
|
credit_note_amount, d.credit_note_amount = self.allocate_based_on_fifo(credit_note_amount, term_outstanding_amount)
|
|
|
|
|
|
|
|
term_outstanding_amount -= d.credit_note_amount
|
|
|
|
|
|
|
|
row_outstanding = term_outstanding_amount
|
2018-12-17 12:15:39 +00:00
|
|
|
# Allocate PDC based on payment terms(FIFO order)
|
|
|
|
d.pdc_details, d.pdc_amount = self.allocate_pdc_amount_in_fifo(gle, row_outstanding)
|
2018-12-07 02:45:05 +00:00
|
|
|
|
|
|
|
if term_outstanding_amount > 0:
|
|
|
|
row = self.prepare_row(party_naming_by, args, gle, term_outstanding_amount,
|
|
|
|
d.credit_note_amount, d.due_date, d.payment_amount , d.payment_term_amount,
|
|
|
|
d.description, d.pdc_amount, d.pdc_details)
|
2018-11-12 05:47:39 +00:00
|
|
|
data.append(row)
|
2018-12-17 09:43:33 +00:00
|
|
|
|
|
|
|
if credit_note_amount:
|
2018-12-19 12:42:58 +00:00
|
|
|
row = self.prepare_row_without_payment_terms(party_naming_by, args, gle, temp_outstanding_amt,
|
|
|
|
temp_credit_note_amt)
|
2018-12-17 09:43:33 +00:00
|
|
|
data.append(row)
|
|
|
|
|
2018-11-13 06:41:04 +00:00
|
|
|
else:
|
2018-12-17 12:15:39 +00:00
|
|
|
row = self.prepare_row_without_payment_terms(party_naming_by, args, gle, outstanding_amount,
|
|
|
|
credit_note_amount)
|
2018-11-12 05:47:39 +00:00
|
|
|
data.append(row)
|
|
|
|
return data
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2018-12-17 12:15:39 +00:00
|
|
|
def allocate_pdc_amount_in_fifo(self, gle, row_outstanding):
|
|
|
|
pdc_list = self.pdc_details.get((gle.voucher_no, gle.party), [])
|
|
|
|
|
|
|
|
pdc_details = []
|
|
|
|
pdc_amount = 0
|
|
|
|
for pdc in pdc_list:
|
|
|
|
if row_outstanding <= pdc.pdc_amount:
|
|
|
|
pdc_amount += row_outstanding
|
|
|
|
pdc.pdc_amount -= row_outstanding
|
|
|
|
if row_outstanding and pdc.pdc_ref and pdc.pdc_date:
|
|
|
|
pdc_details.append(cstr(pdc.pdc_ref) + "/" + formatdate(pdc.pdc_date))
|
|
|
|
row_outstanding = 0
|
|
|
|
|
|
|
|
else:
|
|
|
|
pdc_amount = pdc.pdc_amount
|
|
|
|
if pdc.pdc_amount and pdc.pdc_ref and pdc.pdc_date:
|
|
|
|
pdc_details.append(cstr(pdc.pdc_ref) + "/" + formatdate(pdc.pdc_date))
|
|
|
|
pdc.pdc_amount = 0
|
|
|
|
row_outstanding -= pdc_amount
|
|
|
|
|
|
|
|
return pdc_details, pdc_amount
|
|
|
|
|
|
|
|
def prepare_row_without_payment_terms(self, party_naming_by, args, gle, outstanding_amount, credit_note_amount):
|
|
|
|
pdc_list = self.pdc_details.get((gle.voucher_no, gle.party), [])
|
|
|
|
pdc_amount = 0
|
|
|
|
pdc_details = []
|
|
|
|
for d in pdc_list:
|
|
|
|
pdc_amount += flt(d.pdc_amount)
|
|
|
|
if pdc_amount and d.pdc_ref and d.pdc_date:
|
|
|
|
pdc_details.append(cstr(d.pdc_ref) + "/" + formatdate(d.pdc_date))
|
|
|
|
|
|
|
|
row = self.prepare_row(party_naming_by, args, gle, outstanding_amount,
|
|
|
|
credit_note_amount, pdc_amount=pdc_amount, pdc_details=pdc_details)
|
|
|
|
|
|
|
|
return row
|
|
|
|
|
|
|
|
|
2018-12-07 02:45:05 +00:00
|
|
|
def allocate_based_on_fifo(self, total_amount, row_amount):
|
|
|
|
allocated_amount = 0
|
|
|
|
if row_amount <= total_amount:
|
|
|
|
allocated_amount = row_amount
|
|
|
|
total_amount -= row_amount
|
|
|
|
else:
|
|
|
|
allocated_amount = total_amount
|
|
|
|
total_amount = 0
|
|
|
|
|
|
|
|
return total_amount, allocated_amount
|
|
|
|
|
2018-12-06 09:29:54 +00:00
|
|
|
def prepare_row(self, party_naming_by, args, gle, outstanding_amount, credit_note_amount,
|
2018-12-07 02:45:05 +00:00
|
|
|
due_date=None, paid_amt=None, payment_term_amount=None, payment_term=None, pdc_amount=None, pdc_details=None):
|
2018-11-12 05:47:39 +00:00
|
|
|
row = [gle.posting_date, gle.party]
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
# customer / supplier name
|
|
|
|
if party_naming_by == "Naming Series":
|
|
|
|
row += [self.get_party_name(gle.party_type, gle.party)]
|
2014-10-30 10:19:17 +00:00
|
|
|
|
2019-01-04 05:24:24 +00:00
|
|
|
if args.get("party_type") == 'Customer':
|
|
|
|
row += [self.get_customer_contact(gle.party_type, gle.party)]
|
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
# get due date
|
|
|
|
if not due_date:
|
|
|
|
due_date = self.voucher_details.get(gle.voucher_no, {}).get("due_date", "")
|
|
|
|
bill_date = self.voucher_details.get(gle.voucher_no, {}).get("bill_date", "")
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
row += [gle.voucher_type, gle.voucher_no, due_date]
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
# get supplier bill details
|
|
|
|
if args.get("party_type") == "Supplier":
|
|
|
|
row += [
|
|
|
|
self.voucher_details.get(gle.voucher_no, {}).get("bill_no", ""),
|
|
|
|
self.voucher_details.get(gle.voucher_no, {}).get("bill_date", "")
|
|
|
|
]
|
2014-10-30 10:19:17 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
# invoiced and paid amounts
|
|
|
|
invoiced_amount = gle.get(self.dr_or_cr) if (gle.get(self.dr_or_cr) > 0) else 0
|
2018-08-07 07:31:11 +00:00
|
|
|
|
2018-11-12 09:15:33 +00:00
|
|
|
if self.filters.based_on_payment_terms:
|
2018-11-13 09:37:05 +00:00
|
|
|
row+=[payment_term, invoiced_amount]
|
|
|
|
if payment_term_amount:
|
|
|
|
invoiced_amount = payment_term_amount
|
2018-11-12 09:15:33 +00:00
|
|
|
|
2018-12-07 02:45:05 +00:00
|
|
|
if not payment_term_amount:
|
2018-11-12 05:47:39 +00:00
|
|
|
paid_amt = invoiced_amount - outstanding_amount - credit_note_amount
|
|
|
|
row += [invoiced_amount, paid_amt, credit_note_amount, outstanding_amount]
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
# ageing data
|
|
|
|
if self.filters.ageing_based_on == "Due Date":
|
2018-12-06 09:29:54 +00:00
|
|
|
entry_date = due_date
|
|
|
|
elif self.filters.ageing_based_on == "Supplier Invoice Date":
|
2018-11-12 05:47:39 +00:00
|
|
|
entry_date = bill_date
|
|
|
|
else:
|
|
|
|
entry_date = gle.posting_date
|
2018-04-26 12:32:37 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
row += get_ageing_data(cint(self.filters.range1), cint(self.filters.range2),
|
2019-05-23 09:57:47 +00:00
|
|
|
cint(self.filters.range3), cint(self.filters.range4), self.age_as_on, entry_date, outstanding_amount)
|
2018-08-07 07:31:11 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
# issue 6371-Ageing buckets should not have amounts if due date is not reached
|
|
|
|
if self.filters.ageing_based_on == "Due Date" \
|
|
|
|
and getdate(due_date) > getdate(self.filters.report_date):
|
2019-05-23 09:57:47 +00:00
|
|
|
row[-1]=row[-2]=row[-3]=row[-4]=row[-5]=0
|
2018-04-26 12:32:37 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
if self.filters.ageing_based_on == "Supplier Invoice Date" \
|
|
|
|
and getdate(bill_date) > getdate(self.filters.report_date):
|
2016-04-11 06:03:14 +00:00
|
|
|
|
2019-05-23 09:57:47 +00:00
|
|
|
row[-1]=row[-2]=row[-3]=row[-4]=row[-5]=0
|
2018-03-28 09:55:12 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
if self.filters.get(scrub(args.get("party_type"))):
|
|
|
|
row.append(gle.account_currency)
|
|
|
|
else:
|
|
|
|
row.append(self.company_currency)
|
2018-02-07 13:02:51 +00:00
|
|
|
|
2018-12-07 02:45:05 +00:00
|
|
|
remaining_balance = outstanding_amount - flt(pdc_amount)
|
|
|
|
pdc_details = ", ".join(pdc_details)
|
|
|
|
row += [pdc_details, pdc_amount, remaining_balance]
|
2018-02-07 13:02:51 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
if args.get('party_type') == 'Customer':
|
|
|
|
# customer LPO
|
|
|
|
row += [self.voucher_details.get(gle.voucher_no, {}).get("po_no")]
|
2015-09-30 11:25:26 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
# Delivery Note
|
|
|
|
row += [self.voucher_details.get(gle.voucher_no, {}).get("delivery_note")]
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
# customer territory / supplier group
|
|
|
|
if args.get("party_type") == "Customer":
|
2018-12-19 13:17:36 +00:00
|
|
|
row += [self.get_territory(gle.party), self.get_customer_group(gle.party),
|
|
|
|
self.voucher_details.get(gle.voucher_no, {}).get("sales_person")]
|
2018-11-12 05:47:39 +00:00
|
|
|
if args.get("party_type") == "Supplier":
|
|
|
|
row += [self.get_supplier_group(gle.party)]
|
|
|
|
|
|
|
|
row.append(gle.remarks)
|
|
|
|
|
|
|
|
return row
|
2014-01-03 07:14:00 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_entries_after(self, report_date, party_type):
|
2013-11-07 15:14:30 +00:00
|
|
|
# returns a distinct list
|
2018-08-14 05:21:13 +00:00
|
|
|
return list(set([(e.voucher_type, e.voucher_no) for e in self.get_gl_entries(party_type, report_date, for_future=True)]))
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_entries_till(self, report_date, party_type):
|
2013-11-07 15:14:30 +00:00
|
|
|
# returns a generator
|
2018-08-14 05:21:13 +00:00
|
|
|
return self.get_gl_entries(party_type, report_date)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
def is_receivable_or_payable(self, gle, dr_or_cr, future_vouchers, return_entries):
|
2014-01-09 10:19:26 +00:00
|
|
|
return (
|
|
|
|
# advance
|
2014-09-11 13:57:24 +00:00
|
|
|
(not gle.against_voucher) or
|
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
# against sales order/purchase order
|
|
|
|
(gle.against_voucher_type in ["Sales Order", "Purchase Order"]) or
|
2014-10-21 07:44:55 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
# sales invoice/purchase invoice
|
|
|
|
(gle.against_voucher==gle.voucher_no and gle.get(dr_or_cr) > 0) or
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
# standalone credit notes
|
|
|
|
(gle.against_voucher==gle.voucher_no and gle.voucher_no in return_entries and not return_entries.get(gle.voucher_no)) or
|
|
|
|
|
2014-01-09 10:19:26 +00:00
|
|
|
# entries adjusted with future vouchers
|
|
|
|
((gle.against_voucher_type, gle.against_voucher) in future_vouchers)
|
|
|
|
)
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2017-06-07 06:26:53 +00:00
|
|
|
def get_return_entries(self, party_type):
|
|
|
|
doctype = "Sales Invoice" if party_type=="Customer" else "Purchase Invoice"
|
2019-07-03 05:04:31 +00:00
|
|
|
return_entries = frappe._dict(frappe.get_all(doctype,
|
|
|
|
filters={"is_return": 1, "docstatus": 1}, fields=["name", "return_against"], as_list=1))
|
|
|
|
return return_entries
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2018-11-13 09:37:05 +00:00
|
|
|
def get_outstanding_amount(self, gle, report_date, dr_or_cr, return_entries):
|
2017-06-07 06:26:53 +00:00
|
|
|
payment_amount, credit_note_amount = 0.0, 0.0
|
|
|
|
reverse_dr_or_cr = "credit" if dr_or_cr=="debit" else "debit"
|
2014-10-16 13:32:58 +00:00
|
|
|
for e in self.get_gl_entries_for(gle.party, gle.party_type, gle.voucher_type, gle.voucher_no):
|
2019-07-03 05:04:31 +00:00
|
|
|
if getdate(e.posting_date) <= report_date \
|
|
|
|
and (e.name!=gle.name or (e.voucher_no in return_entries and not return_entries.get(e.voucher_no))):
|
|
|
|
|
2018-11-13 09:37:05 +00:00
|
|
|
amount = flt(e.get(reverse_dr_or_cr), self.currency_precision) - flt(e.get(dr_or_cr), self.currency_precision)
|
2017-06-07 06:26:53 +00:00
|
|
|
if e.voucher_no not in return_entries:
|
|
|
|
payment_amount += amount
|
|
|
|
else:
|
|
|
|
credit_note_amount += amount
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
voucher_amount = flt(gle.get(dr_or_cr), self.currency_precision) - flt(gle.get(reverse_dr_or_cr), self.currency_precision)
|
|
|
|
if gle.voucher_no in return_entries and not return_entries.get(gle.voucher_no):
|
|
|
|
voucher_amount = 0
|
2018-09-19 13:33:40 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
outstanding_amount = flt((voucher_amount - payment_amount - credit_note_amount), self.currency_precision)
|
2018-11-13 09:37:05 +00:00
|
|
|
credit_note_amount = flt(credit_note_amount, self.currency_precision)
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
return outstanding_amount, credit_note_amount, payment_amount
|
2014-10-16 13:32:58 +00:00
|
|
|
|
|
|
|
def get_party_name(self, party_type, party_name):
|
|
|
|
return self.get_party_map(party_type).get(party_name, {}).get("customer_name" if party_type == "Customer" else "supplier_name") or ""
|
|
|
|
|
2019-01-04 05:24:24 +00:00
|
|
|
def get_customer_contact(self, party_type, party_name):
|
|
|
|
return self.get_party_map(party_type).get(party_name, {}).get("customer_primary_contact")
|
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_territory(self, party_name):
|
|
|
|
return self.get_party_map("Customer").get(party_name, {}).get("territory") or ""
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2017-04-24 11:33:50 +00:00
|
|
|
def get_customer_group(self, party_name):
|
|
|
|
return self.get_party_map("Customer").get(party_name, {}).get("customer_group") or ""
|
2014-01-09 10:19:26 +00:00
|
|
|
|
2018-04-19 13:07:29 +00:00
|
|
|
def get_supplier_group(self, party_name):
|
|
|
|
return self.get_party_map("Supplier").get(party_name, {}).get("supplier_group") or ""
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_party_map(self, party_type):
|
|
|
|
if not hasattr(self, "party_map"):
|
|
|
|
if party_type == "Customer":
|
2019-01-04 05:24:24 +00:00
|
|
|
select_fields = "name, customer_name, territory, customer_group, customer_primary_contact"
|
2014-10-16 13:32:58 +00:00
|
|
|
elif party_type == "Supplier":
|
2018-04-19 13:07:29 +00:00
|
|
|
select_fields = "name, supplier_name, supplier_group"
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2017-04-24 11:33:50 +00:00
|
|
|
self.party_map = dict(((r.name, r) for r in frappe.db.sql("select {0} from `tab{1}`"
|
|
|
|
.format(select_fields, party_type), as_dict=True)))
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
return self.party_map
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2018-08-14 05:21:13 +00:00
|
|
|
def get_gl_entries(self, party_type, date=None, for_future=False):
|
|
|
|
conditions, values = self.prepare_conditions(party_type)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2018-08-14 05:21:13 +00:00
|
|
|
if self.filters.get(scrub(party_type)):
|
|
|
|
select_fields = "sum(debit_in_account_currency) as debit, sum(credit_in_account_currency) as credit"
|
|
|
|
else:
|
|
|
|
select_fields = "sum(debit) as debit, sum(credit) as credit"
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2018-08-14 05:21:13 +00:00
|
|
|
if date and not for_future:
|
|
|
|
conditions += " and posting_date <= '%s'" % date
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2018-08-14 05:21:13 +00:00
|
|
|
if date and for_future:
|
|
|
|
conditions += " and posting_date > '%s'" % date
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2018-08-14 05:21:13 +00:00
|
|
|
self.gl_entries = frappe.db.sql("""
|
|
|
|
select
|
|
|
|
name, posting_date, account, party_type, party, voucher_type, voucher_no,
|
|
|
|
against_voucher_type, against_voucher, account_currency, remarks, {0}
|
|
|
|
from
|
|
|
|
`tabGL Entry`
|
|
|
|
where
|
|
|
|
docstatus < 2 and party_type=%s and (party is not null and party != '') {1}
|
2017-12-21 06:07:18 +00:00
|
|
|
group by voucher_type, voucher_no, against_voucher_type, against_voucher, party
|
2015-09-25 05:03:03 +00:00
|
|
|
order by posting_date, party"""
|
2018-08-14 05:21:13 +00:00
|
|
|
.format(select_fields, conditions), values, as_dict=True)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
return self.gl_entries
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def prepare_conditions(self, party_type):
|
2013-11-07 15:14:30 +00:00
|
|
|
conditions = [""]
|
2015-04-28 11:15:20 +00:00
|
|
|
values = [party_type]
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
party_type_field = scrub(party_type)
|
|
|
|
|
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:
|
2018-05-17 17:59:37 +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
|
|
|
|
2017-04-24 11:33:50 +00:00
|
|
|
if party_type_field=="customer":
|
2019-02-19 11:41:50 +00:00
|
|
|
account_type = "Receivable"
|
2017-04-24 11:33:50 +00:00
|
|
|
if self.filters.get("customer_group"):
|
2017-10-17 06:33:02 +00:00
|
|
|
lft, rgt = frappe.db.get_value("Customer Group",
|
2017-04-24 11:33:50 +00:00
|
|
|
self.filters.get("customer_group"), ["lft", "rgt"])
|
2017-10-17 06:33:02 +00:00
|
|
|
|
|
|
|
conditions.append("""party in (select name from tabCustomer
|
|
|
|
where exists(select name from `tabCustomer Group` where lft >= {0} and rgt <= {1}
|
2018-01-29 07:05:19 +00:00
|
|
|
and name=tabCustomer.customer_group))""".format(lft, rgt))
|
2018-12-06 09:29:54 +00:00
|
|
|
|
2018-01-24 12:46:08 +00:00
|
|
|
if self.filters.get("territory"):
|
|
|
|
lft, rgt = frappe.db.get_value("Territory",
|
|
|
|
self.filters.get("territory"), ["lft", "rgt"])
|
|
|
|
|
|
|
|
conditions.append("""party in (select name from tabCustomer
|
|
|
|
where exists(select name from `tabTerritory` where lft >= {0} and rgt <= {1}
|
|
|
|
and name=tabCustomer.territory))""".format(lft, rgt))
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2018-01-09 10:09:35 +00:00
|
|
|
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"))
|
2017-04-24 11:33:50 +00:00
|
|
|
|
2018-01-24 12:46:08 +00:00
|
|
|
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"))
|
|
|
|
|
|
|
|
if self.filters.get("sales_person"):
|
2018-12-10 12:26:42 +00:00
|
|
|
lft, rgt = frappe.db.get_value("Sales Person",
|
|
|
|
self.filters.get("sales_person"), ["lft", "rgt"])
|
|
|
|
|
|
|
|
conditions.append("""exists(select name from `tabSales Team` steam where
|
|
|
|
steam.sales_person in (select name from `tabSales Person` where lft >= {0} and rgt <= {1})
|
|
|
|
and ((steam.parent = voucher_no and steam.parenttype = voucher_type)
|
|
|
|
or (steam.parent = against_voucher and steam.parenttype = against_voucher_type)
|
|
|
|
or (steam.parent = party and steam.parenttype = 'Customer')))""".format(lft, rgt))
|
2018-08-20 12:28:28 +00:00
|
|
|
|
2019-02-19 11:41:50 +00:00
|
|
|
elif party_type_field=="supplier":
|
|
|
|
account_type = "Payable"
|
2018-08-23 05:40:15 +00:00
|
|
|
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"))
|
2018-12-06 09:29:54 +00:00
|
|
|
|
2019-03-18 06:23:04 +00:00
|
|
|
if self.filters.get("cost_center"):
|
|
|
|
lft, rgt = frappe.get_cached_value("Cost Center",
|
|
|
|
self.filters.get("cost_center"), ['lft', 'rgt'])
|
|
|
|
|
|
|
|
conditions.append("""cost_center in (select name from `tabCost Center` where
|
|
|
|
lft >= {0} and rgt <= {1})""".format(lft, rgt))
|
|
|
|
|
2019-07-01 06:24:11 +00:00
|
|
|
if self.filters.company:
|
|
|
|
accounts = [d.name for d in frappe.get_all("Account",
|
|
|
|
filters={"account_type": account_type, "company": self.filters.company})]
|
|
|
|
conditions.append("account in (%s)" % ','.join(['%s'] *len(accounts)))
|
|
|
|
values += accounts
|
2019-02-19 11:41:50 +00:00
|
|
|
|
2019-07-07 15:54:45 +00:00
|
|
|
accounting_dimensions = get_accounting_dimensions()
|
|
|
|
|
|
|
|
if accounting_dimensions:
|
|
|
|
for dimension in accounting_dimensions:
|
|
|
|
if self.filters.get(dimension):
|
|
|
|
conditions.append("{0} = %s".format(dimension))
|
|
|
|
values.append(self.filters.get(dimension))
|
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
return " and ".join(conditions), values
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_gl_entries_for(self, party, party_type, against_voucher_type, against_voucher):
|
2013-11-07 15:14:30 +00:00
|
|
|
if not hasattr(self, "gl_entries_map"):
|
|
|
|
self.gl_entries_map = {}
|
2014-10-16 13:32:58 +00:00
|
|
|
for gle in self.get_gl_entries(party_type):
|
2013-11-07 15:14:30 +00:00
|
|
|
if gle.against_voucher_type and gle.against_voucher:
|
2014-09-11 13:57:24 +00:00
|
|
|
self.gl_entries_map.setdefault(gle.party, {})\
|
2013-11-07 15:14:30 +00:00
|
|
|
.setdefault(gle.against_voucher_type, {})\
|
|
|
|
.setdefault(gle.against_voucher, [])\
|
|
|
|
.append(gle)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
|
|
|
return self.gl_entries_map.get(party, {})\
|
2013-11-07 15:14:30 +00:00
|
|
|
.get(against_voucher_type, {})\
|
|
|
|
.get(against_voucher, [])
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2018-11-12 05:47:39 +00:00
|
|
|
def get_payment_term_detail(self, voucher_nos):
|
|
|
|
payment_term_map = frappe._dict()
|
2018-12-07 02:45:05 +00:00
|
|
|
payment_terms_details = frappe.db.sql(""" select si.name,
|
2018-11-13 09:37:05 +00:00
|
|
|
party_account_currency, currency, si.conversion_rate,
|
|
|
|
ps.due_date, ps.payment_amount, ps.description
|
|
|
|
from `tabSales Invoice` si, `tabPayment Schedule` ps
|
2018-12-06 09:29:54 +00:00
|
|
|
where si.name = ps.parent and
|
2018-12-24 10:33:24 +00:00
|
|
|
si.docstatus = 1 and si.company = %s and
|
2018-12-07 02:45:05 +00:00
|
|
|
si.name in (%s) order by ps.due_date
|
2018-12-20 08:41:20 +00:00
|
|
|
""" % (frappe.db.escape(self.filters.company), ','.join(['%s'] *len(voucher_nos))),
|
2018-12-07 02:45:05 +00:00
|
|
|
(tuple(voucher_nos)), as_dict = 1)
|
|
|
|
|
|
|
|
for d in payment_terms_details:
|
|
|
|
if self.filters.get("customer") and d.currency == d.party_account_currency:
|
|
|
|
payment_term_amount = d.payment_amount
|
|
|
|
else:
|
|
|
|
payment_term_amount = flt(flt(d.payment_amount) * flt(d.conversion_rate), self.currency_precision)
|
2018-11-12 05:47:39 +00:00
|
|
|
|
2018-12-07 02:45:05 +00:00
|
|
|
payment_term_map.setdefault(d.name, []).append(frappe._dict({
|
|
|
|
"due_date": d.due_date,
|
|
|
|
"payment_term_amount": payment_term_amount,
|
|
|
|
"description": d.description
|
|
|
|
}))
|
2018-11-12 05:47:39 +00:00
|
|
|
return payment_term_map
|
|
|
|
|
2016-05-26 09:55:52 +00:00
|
|
|
def get_chart_data(self, columns, data):
|
2019-05-23 09:57:47 +00:00
|
|
|
ageing_columns = columns[self.ageing_col_idx_start : self.ageing_col_idx_start+5]
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2016-05-27 06:51:28 +00:00
|
|
|
rows = []
|
2016-05-20 06:13:30 +00:00
|
|
|
for d in data:
|
2019-07-03 13:52:21 +00:00
|
|
|
values = d[self.ageing_col_idx_start : self.ageing_col_idx_start+5]
|
|
|
|
precision = cint(frappe.db.get_default("float_precision")) or 2
|
|
|
|
formatted_values = [frappe.utils.rounded(val, precision) for val in values]
|
2017-10-31 07:28:48 +00:00
|
|
|
rows.append(
|
|
|
|
{
|
2019-07-03 13:52:21 +00:00
|
|
|
'values': formatted_values
|
2017-10-31 07:28:48 +00:00
|
|
|
}
|
|
|
|
)
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2016-05-20 06:13:30 +00:00
|
|
|
return {
|
|
|
|
"data": {
|
2017-10-31 07:28:48 +00:00
|
|
|
'labels': [d.get("label") for d in ageing_columns],
|
|
|
|
'datasets': rows
|
2016-05-26 09:55:52 +00:00
|
|
|
},
|
2017-10-17 06:33:02 +00:00
|
|
|
"type": 'percentage'
|
2016-05-20 06:13:30 +00:00
|
|
|
}
|
2013-11-07 15:14:30 +00:00
|
|
|
|
|
|
|
def execute(filters=None):
|
2014-10-16 13:32:58 +00:00
|
|
|
args = {
|
|
|
|
"party_type": "Customer",
|
|
|
|
"naming_by": ["Selling Settings", "cust_master_name"],
|
|
|
|
}
|
|
|
|
return ReceivablePayableReport(filters).run(args)
|
2014-01-03 07:51:38 +00:00
|
|
|
|
2019-05-23 09:57:47 +00:00
|
|
|
def get_ageing_data(first_range, second_range, third_range,
|
|
|
|
fourth_range, age_as_on, entry_date, outstanding_amount):
|
|
|
|
# [0-30, 30-60, 60-90, 90-120, 120-above]
|
|
|
|
outstanding_range = [0.0, 0.0, 0.0, 0.0, 0.0]
|
2014-10-03 09:57:53 +00:00
|
|
|
|
2013-11-07 15:39:06 +00:00
|
|
|
if not (age_as_on and entry_date):
|
2013-11-07 15:34:01 +00:00
|
|
|
return [0] + outstanding_range
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-11 07:17:49 +00:00
|
|
|
age = (getdate(age_as_on) - getdate(entry_date)).days or 0
|
2013-11-07 15:34:01 +00:00
|
|
|
index = None
|
2019-05-23 09:57:47 +00:00
|
|
|
for i, days in enumerate([first_range, second_range, third_range, fourth_range]):
|
2013-11-07 15:34:01 +00:00
|
|
|
if age <= days:
|
|
|
|
index = i
|
|
|
|
break
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2019-05-23 09:57:47 +00:00
|
|
|
if index is None: index = 4
|
2013-11-11 07:17:49 +00:00
|
|
|
outstanding_range[index] = outstanding_amount
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-20 17:18:56 +00:00
|
|
|
return [age] + outstanding_range
|
2018-02-07 13:02:51 +00:00
|
|
|
|
2018-03-28 09:55:12 +00:00
|
|
|
def get_pdc_details(party_type, report_date):
|
2018-02-07 13:02:51 +00:00
|
|
|
pdc_details = frappe._dict()
|
2018-12-07 02:45:05 +00:00
|
|
|
pdc_via_pe = frappe.db.sql("""
|
2018-02-07 13:02:51 +00:00
|
|
|
select
|
|
|
|
pref.reference_name as invoice_no, pent.party, pent.party_type,
|
2018-12-07 02:45:05 +00:00
|
|
|
pent.posting_date as pdc_date, ifnull(pref.allocated_amount,0) as pdc_amount,
|
|
|
|
pent.reference_no as pdc_ref
|
2018-02-07 13:02:51 +00:00
|
|
|
from
|
|
|
|
`tabPayment Entry` as pent inner join `tabPayment Entry Reference` as pref
|
|
|
|
on
|
|
|
|
(pref.parent = pent.name)
|
|
|
|
where
|
2018-12-06 09:29:54 +00:00
|
|
|
pent.docstatus < 2 and pent.posting_date > %s
|
2018-02-07 13:02:51 +00:00
|
|
|
and pent.party_type = %s
|
2018-12-07 02:45:05 +00:00
|
|
|
""", (report_date, party_type), as_dict=1)
|
|
|
|
|
|
|
|
for pdc in pdc_via_pe:
|
|
|
|
pdc_details.setdefault((pdc.invoice_no, pdc.party), []).append(pdc)
|
|
|
|
|
2018-02-23 10:50:46 +00:00
|
|
|
if scrub(party_type):
|
2018-03-28 09:55:12 +00:00
|
|
|
amount_field = ("jea.debit_in_account_currency"
|
|
|
|
if party_type == 'Supplier' else "jea.credit_in_account_currency")
|
2018-02-23 10:50:46 +00:00
|
|
|
else:
|
|
|
|
amount_field = "jea.debit + jea.credit"
|
2018-02-07 13:02:51 +00:00
|
|
|
|
2018-12-07 02:45:05 +00:00
|
|
|
pdc_via_je = frappe.db.sql("""
|
2018-02-12 06:25:07 +00:00
|
|
|
select
|
|
|
|
jea.reference_name as invoice_no, jea.party, jea.party_type,
|
2018-12-07 02:45:05 +00:00
|
|
|
je.posting_date as pdc_date, ifnull({0},0) as pdc_amount,
|
|
|
|
je.cheque_no as pdc_ref
|
2018-02-12 06:25:07 +00:00
|
|
|
from
|
|
|
|
`tabJournal Entry` as je inner join `tabJournal Entry Account` as jea
|
|
|
|
on
|
|
|
|
(jea.parent = je.name)
|
|
|
|
where
|
2018-10-16 05:57:23 +00:00
|
|
|
je.docstatus < 2 and je.posting_date > %s
|
2018-02-12 06:25:07 +00:00
|
|
|
and jea.party_type = %s
|
2018-12-07 02:45:05 +00:00
|
|
|
""".format(amount_field), (report_date, party_type), as_dict=1)
|
|
|
|
|
|
|
|
for pdc in pdc_via_je:
|
|
|
|
pdc_details.setdefault((pdc.invoice_no, pdc.party), []).append(pdc)
|
2018-02-12 06:25:07 +00:00
|
|
|
|
2018-02-07 13:02:51 +00:00
|
|
|
return pdc_details
|
|
|
|
|
2018-08-14 05:21:13 +00:00
|
|
|
def get_dn_details(party_type, voucher_nos):
|
2018-02-07 13:02:51 +00:00
|
|
|
dn_details = frappe._dict()
|
|
|
|
|
|
|
|
if party_type == "Customer":
|
2018-08-14 05:21:13 +00:00
|
|
|
for si in frappe.db.sql("""
|
|
|
|
select
|
|
|
|
parent, GROUP_CONCAT(delivery_note SEPARATOR ', ') as dn
|
|
|
|
from
|
|
|
|
`tabSales Invoice Item`
|
|
|
|
where
|
|
|
|
docstatus=1 and delivery_note is not null and delivery_note != ''
|
|
|
|
and parent in (%s) group by parent
|
|
|
|
""" %(','.join(['%s'] * len(voucher_nos))), tuple(voucher_nos) , as_dict=1):
|
|
|
|
dn_details.setdefault(si.parent, si.dn)
|
|
|
|
|
|
|
|
for si in frappe.db.sql("""
|
|
|
|
select
|
|
|
|
against_sales_invoice as parent, GROUP_CONCAT(parent SEPARATOR ', ') as dn
|
|
|
|
from
|
|
|
|
`tabDelivery Note Item`
|
|
|
|
where
|
|
|
|
docstatus=1 and against_sales_invoice is not null and against_sales_invoice != ''
|
|
|
|
and against_sales_invoice in (%s)
|
|
|
|
group by against_sales_invoice
|
|
|
|
""" %(','.join(['%s'] * len(voucher_nos))), tuple(voucher_nos) , as_dict=1):
|
|
|
|
if si.parent in dn_details:
|
|
|
|
dn_details[si.parent] += ', %s' %(si.dn)
|
|
|
|
else:
|
2018-02-07 13:02:51 +00:00
|
|
|
dn_details.setdefault(si.parent, si.dn)
|
|
|
|
|
|
|
|
return dn_details
|
2018-08-14 05:21:13 +00:00
|
|
|
|
|
|
|
def get_voucher_details(party_type, voucher_nos, dn_details):
|
|
|
|
voucher_details = frappe._dict()
|
|
|
|
|
|
|
|
if party_type == "Customer":
|
2018-12-10 12:26:42 +00:00
|
|
|
for si in frappe.db.sql("""
|
|
|
|
select inv.name, inv.due_date, inv.po_no, GROUP_CONCAT(steam.sales_person SEPARATOR ', ') as sales_person
|
|
|
|
from `tabSales Invoice` inv
|
|
|
|
left join `tabSales Team` steam on steam.parent = inv.name and steam.parenttype = 'Sales Invoice'
|
|
|
|
where inv.docstatus=1 and inv.name in (%s)
|
|
|
|
group by inv.name
|
2018-08-14 05:21:13 +00:00
|
|
|
""" %(','.join(['%s'] *len(voucher_nos))), (tuple(voucher_nos)), as_dict=1):
|
|
|
|
si['delivery_note'] = dn_details.get(si.name)
|
|
|
|
voucher_details.setdefault(si.name, si)
|
|
|
|
|
|
|
|
if party_type == "Supplier":
|
|
|
|
for pi in frappe.db.sql("""select name, due_date, bill_no, bill_date
|
|
|
|
from `tabPurchase Invoice` where docstatus = 1 and name in (%s)
|
|
|
|
""" %(','.join(['%s'] *len(voucher_nos))), (tuple(voucher_nos)), as_dict=1):
|
|
|
|
voucher_details.setdefault(pi.name, pi)
|
|
|
|
|
|
|
|
for pi in frappe.db.sql("""select name, due_date, bill_no, bill_date from
|
|
|
|
`tabJournal Entry` where docstatus = 1 and bill_no is not NULL and name in (%s)
|
|
|
|
""" %(','.join(['%s'] *len(voucher_nos))), (tuple(voucher_nos)), as_dict=1):
|
|
|
|
voucher_details.setdefault(pi.name, pi)
|
|
|
|
|
|
|
|
return voucher_details
|