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
|
2014-10-03 09:57:53 +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])
|
|
|
|
return self.get_columns(party_naming_by, args), self.get_data(party_naming_by, args)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_columns(self, party_naming_by, args):
|
|
|
|
columns = [_("Posting Date") + ":Date:80", _(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
|
|
|
|
2014-09-12 06:28:52 +00:00
|
|
|
columns += [_("Voucher Type") + "::110", _("Voucher No") + ":Dynamic Link/Voucher Type:120",
|
2014-10-16 13:32:58 +00:00
|
|
|
_("Due Date") + ":Date:80"]
|
|
|
|
|
|
|
|
if args.get("party_type") == "Supplier":
|
|
|
|
columns += [_("Bill No") + "::80", _("Bill Date") + ":Date:80"]
|
|
|
|
|
|
|
|
columns += [_("Invoiced Amount") + ":Currency:100", _("Paid Amount") + ":Currency:100",
|
|
|
|
_("Outstanding Amount") + ":Currency:100", _("Age") + ":Int:50",
|
2014-12-01 11:31:44 +00:00
|
|
|
"0-" + str(self.filters.range1) + ":Currency:100",
|
|
|
|
str(self.filters.range1) + "-" + str(self.filters.range2) + ":Currency:100",
|
|
|
|
str(self.filters.range2) + "-" + str(self.filters.range3) + ":Currency:100",
|
|
|
|
str(self.filters.range3) + _("-Above") + ":Currency:100"
|
2014-09-11 13:57:24 +00:00
|
|
|
]
|
2014-01-03 07:14:00 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
if args.get("party_type") == "Customer":
|
|
|
|
columns += [_("Territory") + ":Link/Territory:80"]
|
|
|
|
if args.get("party_type") == "Supplier":
|
|
|
|
columns += [_("Supplier Type") + ":Link/Supplier Type:80"]
|
|
|
|
columns += [_("Remarks") + "::200"]
|
|
|
|
|
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
|
|
|
|
2014-10-20 11:21:48 +00:00
|
|
|
voucher_details = self.get_voucher_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"))
|
2014-01-03 07:14:00 +00:00
|
|
|
|
2014-10-30 10:19:17 +00:00
|
|
|
data = []
|
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):
|
|
|
|
outstanding_amount = self.get_outstanding_amount(gle, self.filters.report_date, dr_or_cr)
|
2014-10-16 13:32:58 +00:00
|
|
|
if abs(outstanding_amount) > 0.1/10**currency_precision:
|
2014-10-30 10:19:17 +00:00
|
|
|
|
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
|
|
|
|
due_date = voucher_details.get(gle.voucher_no, {}).get("due_date", "")
|
|
|
|
|
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
|
|
|
|
paid_amt = invoiced_amount - outstanding_amount
|
|
|
|
row += [invoiced_amount, paid_amt, outstanding_amount]
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
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":
|
|
|
|
row += [self.get_territory(gle.party), gle.remarks]
|
|
|
|
if args.get("party_type") == "Supplier":
|
|
|
|
row += [self.get_supplier_type(gle.party), gle.remarks]
|
2014-01-03 07:14:00 +00:00
|
|
|
|
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
|
2014-10-16 13:32:58 +00:00
|
|
|
return (e 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 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)
|
|
|
|
)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
def get_outstanding_amount(self, gle, report_date, dr_or_cr):
|
|
|
|
payment_amount = 0.0
|
|
|
|
for e in self.get_gl_entries_for(gle.party, gle.party_type, gle.voucher_type, gle.voucher_no):
|
2013-11-07 15:14:30 +00:00
|
|
|
if getdate(e.posting_date) <= report_date and e.name!=gle.name:
|
2014-10-20 11:21:48 +00:00
|
|
|
payment_amount += (flt(e.credit if gle.party_type == "Customer" else e.debit) - flt(e.get(dr_or_cr)))
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2014-10-20 11:21:48 +00:00
|
|
|
return flt(gle.get(dr_or_cr)) - flt(gle.credit if gle.party_type == "Customer" else gle.debit) - 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 ""
|
|
|
|
|
|
|
|
def get_territory(self, party_name):
|
|
|
|
return self.get_party_map("Customer").get(party_name, {}).get("territory") 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":
|
|
|
|
self.party_map = dict(((r.name, r) for r in frappe.db.sql("""select {0}, {1}, {2} from `tab{3}`"""
|
|
|
|
.format("name", "customer_name", "territory", party_type), as_dict=True)))
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
elif party_type == "Supplier":
|
|
|
|
self.party_map = dict(((r.name, r) for r in frappe.db.sql("""select {0}, {1}, {2} from `tab{3}`"""
|
|
|
|
.format("name", "supplier_name", "supplier_type", 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
|
|
|
|
2014-10-20 11:21:48 +00:00
|
|
|
def get_voucher_details(self):
|
|
|
|
voucher_details = frappe._dict()
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-10-20 11:21:48 +00:00
|
|
|
for si in frappe.db.sql("""select name, due_date
|
|
|
|
from `tabSales Invoice` where docstatus=1""", as_dict=1):
|
|
|
|
voucher_details.setdefault(si.name, si)
|
2014-10-16 13:32:58 +00:00
|
|
|
|
2014-10-20 11:21:48 +00:00
|
|
|
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)
|
2014-02-26 07:05:33 +00:00
|
|
|
self.gl_entries = frappe.db.sql("""select * from `tabGL Entry`
|
2015-04-21 11:46:28 +00:00
|
|
|
where docstatus < 2 {0} order by posting_date, party"""
|
|
|
|
.format(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-17 10:25:19 +00:00
|
|
|
values = []
|
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
|
|
|
|
2014-10-16 13:32:58 +00:00
|
|
|
if self.filters.get(party_type_field):
|
2015-04-21 11:46:28 +00:00
|
|
|
conditions.append("party_type=%s and party=%s")
|
|
|
|
values += [party_type, self.filters.get(party_type_field)]
|
|
|
|
|
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, [])
|
|
|
|
|
|
|
|
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
|