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
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2014-10-16 13:32:58 +00:00
|
|
|
from frappe import _, scrub
|
2015-09-11 10:52:37 +00:00
|
|
|
from frappe.utils import getdate, nowdate, flt, cint
|
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
|
|
|
|
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"
|
|
|
|
|
|
|
|
for label in ("Invoiced Amount", "Paid Amount", credit_or_debit_note, "Outstanding Amount"):
|
2015-08-31 11:32:04 +00:00
|
|
|
columns.append({
|
|
|
|
"label": label,
|
|
|
|
"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"
|
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"]),
|
|
|
|
"{range3}-{above}".format(range3=cint(self.filters["range3"])+ 1, above=_("Above"))):
|
2015-08-31 11:32:04 +00:00
|
|
|
columns.append({
|
|
|
|
"label": label,
|
|
|
|
"fieldtype": "Currency",
|
|
|
|
"options": "currency",
|
|
|
|
"width": 120
|
|
|
|
})
|
2014-01-03 07:14:00 +00:00
|
|
|
|
2016-04-11 06:03:14 +00:00
|
|
|
columns.append({
|
|
|
|
"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-02-07 13:02:51 +00:00
|
|
|
|
|
|
|
columns += [
|
|
|
|
_("PDC/LC Date") + ":Date:110",
|
|
|
|
_("PDC/LC Ref") + ":Data:110",
|
|
|
|
_("PDC/LC Amount") + ":Currency/currency:130",
|
|
|
|
_("Remaining Balance") + ":Currency/currency:130"
|
|
|
|
]
|
|
|
|
|
|
|
|
if args.get('party_type') == 'Customer':
|
|
|
|
columns += [_("Customer LPO") + ":Data:100"]
|
|
|
|
columns += [_("Delivery Note") + ":Data:100"]
|
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
if args.get("party_type") == "Customer":
|
2017-04-24 11:33:50 +00:00
|
|
|
columns += [
|
2017-10-17 06:33:02 +00:00
|
|
|
_("Territory") + ":Link/Territory:80",
|
2017-04-24 11:33:50 +00:00
|
|
|
_("Customer Group") + ":Link/Customer Group:120"
|
|
|
|
]
|
2014-10-16 13:32:58 +00:00
|
|
|
if args.get("party_type") == "Supplier":
|
|
|
|
columns += [_("Supplier Type") + ":Link/Supplier Type: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
|
2014-01-22 10:06:44 +00:00
|
|
|
currency_precision = get_currency_precision() or 2
|
2014-10-20 11:21:48 +00:00
|
|
|
dr_or_cr = "debit" if args.get("party_type") == "Customer" else "credit"
|
2014-10-30 10:19:17 +00:00
|
|
|
|
2018-02-07 13:02:51 +00:00
|
|
|
dn_details = get_dn_details(args.get("party_type"))
|
|
|
|
voucher_details = self.get_voucher_details(args.get("party_type"), dn_details)
|
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')
|
|
|
|
|
2015-09-17 07:53:02 +00:00
|
|
|
company_currency = frappe.db.get_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-03-28 09:55:12 +00:00
|
|
|
pdc_details = get_pdc_details(args.get("party_type"), self.filters.report_date)
|
2018-02-07 13:02:51 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
for gle in self.get_entries_till(self.filters.report_date, args.get("party_type")):
|
2014-10-20 11:21:48 +00:00
|
|
|
if self.is_receivable_or_payable(gle, dr_or_cr, future_vouchers):
|
2017-10-17 06:33:02 +00:00
|
|
|
outstanding_amount, credit_note_amount = self.get_outstanding_amount(gle,
|
2017-06-07 06:26:53 +00:00
|
|
|
self.filters.report_date, dr_or_cr, return_entries, currency_precision)
|
2014-10-16 13:32:58 +00:00
|
|
|
if abs(outstanding_amount) > 0.1/10**currency_precision:
|
2014-09-11 13:57:24 +00:00
|
|
|
row = [gle.posting_date, gle.party]
|
|
|
|
|
2014-10-30 10:19:17 +00:00
|
|
|
# customer / supplier name
|
2014-10-16 13:32:58 +00:00
|
|
|
if party_naming_by == "Naming Series":
|
|
|
|
row += [self.get_party_name(gle.party_type, gle.party)]
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-30 10:19:17 +00:00
|
|
|
# get due date
|
2017-12-21 06:07:18 +00:00
|
|
|
due_date = voucher_details.get(gle.voucher_no, {}).get("due_date", "")
|
2014-10-30 10:19:17 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
row += [gle.voucher_type, gle.voucher_no, due_date]
|
|
|
|
|
2014-10-30 10:19:17 +00:00
|
|
|
# get supplier bill details
|
2014-10-16 13:32:58 +00:00
|
|
|
if args.get("party_type") == "Supplier":
|
2014-10-20 11:21:48 +00:00
|
|
|
row += [
|
2014-10-30 10:19:17 +00:00
|
|
|
voucher_details.get(gle.voucher_no, {}).get("bill_no", ""),
|
|
|
|
voucher_details.get(gle.voucher_no, {}).get("bill_date", "")
|
2014-10-20 11:21:48 +00:00
|
|
|
]
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2014-10-30 10:19:17 +00:00
|
|
|
# invoiced and paid amounts
|
|
|
|
invoiced_amount = gle.get(dr_or_cr) if (gle.get(dr_or_cr) > 0) else 0
|
2017-06-07 06:26:53 +00:00
|
|
|
paid_amt = invoiced_amount - outstanding_amount - credit_note_amount
|
|
|
|
row += [invoiced_amount, paid_amt, credit_note_amount, outstanding_amount]
|
2014-10-30 10:19:17 +00:00
|
|
|
|
|
|
|
# ageing data
|
|
|
|
entry_date = due_date if self.filters.ageing_based_on == "Due Date" else gle.posting_date
|
|
|
|
row += get_ageing_data(cint(self.filters.range1), cint(self.filters.range2),
|
2014-10-16 13:32:58 +00:00
|
|
|
cint(self.filters.range3), self.age_as_on, entry_date, outstanding_amount)
|
|
|
|
|
2016-09-23 10:53:43 +00:00
|
|
|
# issue 6371-Ageing buckets should not have amounts if due date is not reached
|
2017-06-07 06:26:53 +00:00
|
|
|
if self.filters.ageing_based_on == "Due Date" \
|
|
|
|
and getdate(due_date) > getdate(self.filters.report_date):
|
2016-09-23 10:53:43 +00:00
|
|
|
row[-1]=row[-2]=row[-3]=row[-4]=0
|
|
|
|
|
2016-04-11 06:03:14 +00:00
|
|
|
if self.filters.get(scrub(args.get("party_type"))):
|
|
|
|
row.append(gle.account_currency)
|
|
|
|
else:
|
|
|
|
row.append(company_currency)
|
|
|
|
|
2018-02-23 10:50:46 +00:00
|
|
|
pdc = pdc_details.get((gle.voucher_no, gle.party), {})
|
2018-03-28 09:55:12 +00:00
|
|
|
|
2018-02-07 13:02:51 +00:00
|
|
|
remaining_balance = outstanding_amount - flt(pdc.get("pdc_amount"))
|
|
|
|
row += [pdc.get("pdc_date"), pdc.get("pdc_ref"),
|
|
|
|
flt(pdc.get("pdc_amount")), remaining_balance]
|
|
|
|
|
|
|
|
if args.get('party_type') == 'Customer':
|
|
|
|
# customer LPO
|
|
|
|
row += [voucher_details.get(gle.voucher_no, {}).get("po_no")]
|
|
|
|
|
|
|
|
# Delivery Note
|
|
|
|
row += [voucher_details.get(gle.voucher_no, {}).get("delivery_note")]
|
|
|
|
|
2014-10-30 10:19:17 +00:00
|
|
|
# customer territory / supplier type
|
2014-10-16 13:32:58 +00:00
|
|
|
if args.get("party_type") == "Customer":
|
2017-04-24 11:33:50 +00:00
|
|
|
row += [self.get_territory(gle.party), self.get_customer_group(gle.party)]
|
2014-10-16 13:32:58 +00:00
|
|
|
if args.get("party_type") == "Supplier":
|
2015-08-31 11:32:04 +00:00
|
|
|
row += [self.get_supplier_type(gle.party)]
|
2015-09-30 11:25:26 +00:00
|
|
|
|
2015-09-17 07:53:02 +00:00
|
|
|
row.append(gle.remarks)
|
2013-11-07 15:14:30 +00:00
|
|
|
data.append(row)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
return data
|
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
|
2014-10-16 13:32:58 +00:00
|
|
|
return list(set([(e.voucher_type, e.voucher_no) for e in self.get_gl_entries(party_type)
|
2013-11-07 15:14:30 +00:00
|
|
|
if getdate(e.posting_date) > report_date]))
|
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
|
2017-08-15 02:53:51 +00:00
|
|
|
return (e for e in self.get_gl_entries(party_type) if getdate(e.posting_date) <= report_date)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def is_receivable_or_payable(self, gle, dr_or_cr, future_vouchers):
|
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
|
|
|
|
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"
|
2017-10-17 06:33:02 +00:00
|
|
|
return [d.name for d in frappe.get_all(doctype, filters={"is_return": 1, "docstatus": 1})]
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2017-06-07 06:26:53 +00:00
|
|
|
def get_outstanding_amount(self, gle, report_date, dr_or_cr, return_entries, currency_precision):
|
|
|
|
payment_amount, credit_note_amount = 0.0, 0.0
|
|
|
|
reverse_dr_or_cr = "credit" if dr_or_cr=="debit" else "debit"
|
2017-10-17 06:33:02 +00:00
|
|
|
|
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):
|
2017-12-21 06:07:18 +00:00
|
|
|
if getdate(e.posting_date) <= report_date and e.name!=gle.name:
|
2017-06-07 06:26:53 +00:00
|
|
|
amount = flt(e.get(reverse_dr_or_cr)) - flt(e.get(dr_or_cr))
|
|
|
|
if e.voucher_no not in return_entries:
|
|
|
|
payment_amount += amount
|
|
|
|
else:
|
|
|
|
credit_note_amount += amount
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2017-06-07 06:26:53 +00:00
|
|
|
outstanding_amount = flt((flt(gle.get(dr_or_cr)) - flt(gle.get(reverse_dr_or_cr)) \
|
|
|
|
- payment_amount - credit_note_amount), currency_precision)
|
|
|
|
credit_note_amount = flt(credit_note_amount, currency_precision)
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2017-06-07 06:26:53 +00:00
|
|
|
return outstanding_amount, credit_note_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 ""
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_supplier_type(self, party_name):
|
|
|
|
return self.get_party_map("Supplier").get(party_name, {}).get("supplier_type") 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":
|
2017-04-24 11:33:50 +00:00
|
|
|
select_fields = "name, customer_name, territory, customer_group"
|
2014-10-16 13:32:58 +00:00
|
|
|
elif party_type == "Supplier":
|
2017-04-24 11:33:50 +00:00
|
|
|
select_fields = "name, supplier_name, supplier_type"
|
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-02-07 13:02:51 +00:00
|
|
|
def get_voucher_details(self, party_type, dn_details):
|
2014-10-20 11:21:48 +00:00
|
|
|
voucher_details = frappe._dict()
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
if party_type == "Customer":
|
2018-02-07 13:02:51 +00:00
|
|
|
for si in frappe.db.sql("""select name, due_date, po_no
|
2015-06-14 12:19:29 +00:00
|
|
|
from `tabSales Invoice` where docstatus=1""", as_dict=1):
|
2018-02-07 13:02:51 +00:00
|
|
|
si['delivery_note'] = dn_details.get(si.name)
|
2015-06-14 12:19:29 +00:00
|
|
|
voucher_details.setdefault(si.name, si)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
if party_type == "Supplier":
|
|
|
|
for pi in frappe.db.sql("""select name, due_date, bill_no, bill_date
|
|
|
|
from `tabPurchase Invoice` where docstatus=1""", as_dict=1):
|
|
|
|
voucher_details.setdefault(pi.name, pi)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-20 11:21:48 +00:00
|
|
|
return voucher_details
|
2014-10-16 13:32:58 +00:00
|
|
|
|
|
|
|
def get_gl_entries(self, party_type):
|
2013-11-07 15:14:30 +00:00
|
|
|
if not hasattr(self, "gl_entries"):
|
2014-10-16 13:32:58 +00:00
|
|
|
conditions, values = self.prepare_conditions(party_type)
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2015-08-31 11:32:04 +00:00
|
|
|
if self.filters.get(scrub(party_type)):
|
2016-09-29 07:33:10 +00:00
|
|
|
select_fields = "sum(debit_in_account_currency) as debit, sum(credit_in_account_currency) as credit"
|
2015-08-31 11:32:04 +00:00
|
|
|
else:
|
2016-09-29 07:33:10 +00:00
|
|
|
select_fields = "sum(debit) as debit, sum(credit) as credit"
|
2015-09-11 10:52:37 +00:00
|
|
|
|
2016-09-29 07:33:10 +00:00
|
|
|
self.gl_entries = frappe.db.sql("""select name, posting_date, account, party_type, party,
|
2017-12-21 06:07:18 +00:00
|
|
|
voucher_type, voucher_no, against_voucher_type, against_voucher,
|
2016-09-29 07:33:10 +00:00
|
|
|
account_currency, remarks, {0}
|
2015-08-31 11:32:04 +00:00
|
|
|
from `tabGL Entry`
|
2015-11-16 13:35:46 +00:00
|
|
|
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"""
|
2015-08-31 11:32:04 +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
|
|
|
|
2018-04-22 21:27:10 +00:00
|
|
|
if self.filters.finance_book:
|
2018-05-09 09:40:29 +00:00
|
|
|
conditions.append("finance_book in (%s, '')")
|
2018-04-22 21:27:10 +00:00
|
|
|
values.append(self.filters.finance_book)
|
|
|
|
else:
|
|
|
|
conditions.append("ifnull(finance_book,'')=%s")
|
|
|
|
values.append('')
|
|
|
|
|
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":
|
|
|
|
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-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-01-29 07:04:19 +00:00
|
|
|
conditions.append("""party in (select parent
|
|
|
|
from `tabSales Team` where sales_person=%s and parenttype = 'Customer')""")
|
2018-01-24 12:46:08 +00:00
|
|
|
values.append(self.filters.get("sales_person"))
|
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
|
|
|
|
2016-05-26 09:55:52 +00:00
|
|
|
def get_chart_data(self, columns, data):
|
2016-05-20 06:13:30 +00:00
|
|
|
ageing_columns = columns[self.ageing_col_idx_start : self.ageing_col_idx_start+4]
|
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:
|
2017-10-31 07:28:48 +00:00
|
|
|
rows.append(
|
|
|
|
{
|
|
|
|
'values': d[self.ageing_col_idx_start : self.ageing_col_idx_start+4]
|
|
|
|
}
|
|
|
|
)
|
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
|
|
|
|
2014-10-03 09:57:53 +00:00
|
|
|
def get_ageing_data(first_range, second_range, third_range, age_as_on, entry_date, outstanding_amount):
|
2013-11-07 15:34:01 +00:00
|
|
|
# [0-30, 30-60, 60-90, 90-above]
|
|
|
|
outstanding_range = [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
|
2014-10-03 09:57:53 +00:00
|
|
|
for i, days in enumerate([first_range, second_range, third_range]):
|
2013-11-07 15:34:01 +00:00
|
|
|
if age <= days:
|
|
|
|
index = i
|
|
|
|
break
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:34:01 +00:00
|
|
|
if index is None: index = 3
|
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()
|
|
|
|
|
|
|
|
for pdc in frappe.db.sql("""
|
|
|
|
select
|
|
|
|
pref.reference_name as invoice_no, pent.party, pent.party_type,
|
|
|
|
max(pent.reference_date) as pdc_date, sum(ifnull(pref.allocated_amount,0)) as pdc_amount,
|
|
|
|
GROUP_CONCAT(pent.reference_no SEPARATOR ', ') as pdc_ref
|
|
|
|
from
|
|
|
|
`tabPayment Entry` as pent inner join `tabPayment Entry Reference` as pref
|
|
|
|
on
|
|
|
|
(pref.parent = pent.name)
|
|
|
|
where
|
2018-03-28 09:55:12 +00:00
|
|
|
pent.docstatus < 2 and pent.reference_date >= %s
|
2018-02-07 13:02:51 +00:00
|
|
|
and pent.party_type = %s
|
2018-03-28 09:55:12 +00:00
|
|
|
group by pent.party, pref.reference_name""", (report_date, party_type), as_dict=1):
|
2018-02-23 10:50:46 +00:00
|
|
|
pdc_details.setdefault((pdc.invoice_no, pdc.party), pdc)
|
|
|
|
|
|
|
|
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-02-12 06:25:07 +00:00
|
|
|
for pdc in frappe.db.sql("""
|
|
|
|
select
|
|
|
|
jea.reference_name as invoice_no, jea.party, jea.party_type,
|
2018-02-23 10:50:46 +00:00
|
|
|
max(je.cheque_date) as pdc_date, sum(ifnull({0},0)) as pdc_amount,
|
2018-02-12 06:25:07 +00:00
|
|
|
GROUP_CONCAT(je.cheque_no SEPARATOR ', ') as pdc_ref
|
|
|
|
from
|
|
|
|
`tabJournal Entry` as je inner join `tabJournal Entry Account` as jea
|
|
|
|
on
|
|
|
|
(jea.parent = je.name)
|
|
|
|
where
|
2018-03-28 09:55:12 +00:00
|
|
|
je.docstatus < 2 and je.cheque_date >= %s
|
2018-02-12 06:25:07 +00:00
|
|
|
and jea.party_type = %s
|
2018-03-28 09:55:12 +00:00
|
|
|
group by jea.party, jea.reference_name""".format(amount_field), (report_date, party_type), as_dict=1):
|
2018-02-23 10:50:46 +00:00
|
|
|
if (pdc.invoice_no, pdc.party) in pdc_details:
|
|
|
|
pdc_details[(pdc.invoice_no, pdc.party)]["pdc_amount"] += pdc.pdc_amount
|
|
|
|
else:
|
|
|
|
pdc_details.setdefault((pdc.invoice_no, pdc.party), pdc)
|
2018-02-12 06:25:07 +00:00
|
|
|
|
2018-02-07 13:02:51 +00:00
|
|
|
return pdc_details
|
|
|
|
|
|
|
|
def get_dn_details(party_type):
|
|
|
|
dn_details = frappe._dict()
|
|
|
|
|
|
|
|
if party_type == "Customer":
|
|
|
|
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 != '' group by parent
|
|
|
|
Union
|
|
|
|
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 != '' group by against_sales_invoice""", as_dict=1):
|
|
|
|
dn_details.setdefault(si.parent, si.dn)
|
|
|
|
|
|
|
|
return dn_details
|