2013-11-20 17:18:56 +00:00
|
|
|
# Copyright (c) 2013, Web Notes 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
|
|
|
|
from frappe import _
|
2014-10-03 09:57:53 +00:00
|
|
|
from frappe.utils import getdate, nowdate, flt, cint
|
2013-04-22 07:44:52 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
class AccountsReceivableReport(object):
|
|
|
|
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
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
def run(self):
|
2014-02-26 07:05:33 +00:00
|
|
|
customer_naming_by = frappe.db.get_value("Selling Settings", None, "cust_master_name")
|
2014-01-03 07:51:38 +00:00
|
|
|
return self.get_columns(customer_naming_by), self.get_data(customer_naming_by)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2014-01-03 07:51:38 +00:00
|
|
|
def get_columns(self, customer_naming_by):
|
2014-09-11 13:57:24 +00:00
|
|
|
columns = [_("Posting Date") + ":Date:80", _("Customer") + ":Link/Customer:200"]
|
2014-01-03 07:28:04 +00:00
|
|
|
|
2014-01-03 07:51:38 +00:00
|
|
|
if customer_naming_by == "Naming Series":
|
|
|
|
columns += ["Customer 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-09-11 13:57:24 +00:00
|
|
|
_("Due Date") + ":Date:80", _("Invoiced Amount") + ":Currency:100",
|
|
|
|
_("Payment Received") + ":Currency:100", _("Outstanding Amount") + ":Currency:100",
|
2014-10-03 09:57:53 +00:00
|
|
|
_("Age") + ":Int:50", "0-" + self.filters.range1 + ":Currency:100",
|
2014-10-21 07:44:55 +00:00
|
|
|
self.filters.range1 + "-" + self.filters.range2 + ":Currency:100",
|
|
|
|
self.filters.range2 + "-" + self.filters.range3 + ":Currency:100",
|
2014-10-03 09:57:53 +00:00
|
|
|
self.filters.range3 + _("-Above") + ":Currency:100",
|
2014-09-11 13:57:24 +00:00
|
|
|
_("Territory") + ":Link/Territory:80", _("Remarks") + "::200"
|
|
|
|
]
|
2014-01-03 07:14:00 +00:00
|
|
|
|
|
|
|
return columns
|
|
|
|
|
2014-01-03 07:51:38 +00:00
|
|
|
def get_data(self, customer_naming_by):
|
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
|
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
data = []
|
|
|
|
future_vouchers = self.get_entries_after(self.filters.report_date)
|
|
|
|
for gle in self.get_entries_till(self.filters.report_date):
|
|
|
|
if self.is_receivable(gle, future_vouchers):
|
|
|
|
outstanding_amount = self.get_outstanding_amount(gle, self.filters.report_date)
|
2014-01-22 10:06:44 +00:00
|
|
|
if abs(outstanding_amount) > 0.1/10**currency_precision:
|
2013-11-07 15:14:30 +00:00
|
|
|
due_date = self.get_due_date(gle)
|
|
|
|
invoiced_amount = gle.debit if (gle.debit > 0) else 0
|
|
|
|
payment_received = invoiced_amount - outstanding_amount
|
2014-01-03 07:14:00 +00:00
|
|
|
|
2014-09-11 13:57:24 +00:00
|
|
|
row = [gle.posting_date, gle.party]
|
2014-01-03 07:51:38 +00:00
|
|
|
if customer_naming_by == "Naming Series":
|
2014-09-11 13:57:24 +00:00
|
|
|
row += [self.get_customer_name(gle.party)]
|
|
|
|
|
|
|
|
row += [gle.voucher_type, gle.voucher_no, due_date, invoiced_amount,
|
|
|
|
payment_received, outstanding_amount]
|
|
|
|
|
|
|
|
entry_date = due_date if self.filters.ageing_based_on == "Due Date" else gle.posting_date
|
2014-10-03 09:57:53 +00:00
|
|
|
row += get_ageing_data(cint(self.filters.range1), cint(self.filters.range2), \
|
|
|
|
cint(self.filters.range3), self.age_as_on, entry_date, outstanding_amount) + \
|
2014-09-11 13:57:24 +00:00
|
|
|
[self.get_territory(gle.account), 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
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
def get_entries_after(self, report_date):
|
|
|
|
# returns a distinct list
|
|
|
|
return list(set([(e.voucher_type, e.voucher_no) for e in self.get_gl_entries()
|
|
|
|
if getdate(e.posting_date) > report_date]))
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
def get_entries_till(self, report_date):
|
|
|
|
# returns a generator
|
2014-09-11 13:57:24 +00:00
|
|
|
return (e for e in self.get_gl_entries()
|
2013-11-07 15:14:30 +00:00
|
|
|
if getdate(e.posting_date) <= report_date)
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
def is_receivable(self, gle, 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-10 07:43:39 +00:00
|
|
|
# against sales order
|
|
|
|
(gle.against_voucher_type == "Sales Order") or
|
2014-10-21 07:44:55 +00:00
|
|
|
|
2014-01-09 10:19:26 +00:00
|
|
|
# sales invoice
|
2014-09-11 13:57:24 +00:00
|
|
|
(gle.against_voucher==gle.voucher_no and gle.debit > 0) or
|
|
|
|
|
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
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
def get_outstanding_amount(self, gle, report_date):
|
|
|
|
payment_received = 0.0
|
2014-09-11 13:57:24 +00:00
|
|
|
for e in self.get_gl_entries_for(gle.party, 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:
|
|
|
|
payment_received += (flt(e.credit) - flt(e.debit))
|
2014-01-09 10:19:26 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
return flt(gle.debit) - flt(gle.credit) - payment_received
|
2014-09-11 13:57:24 +00:00
|
|
|
|
|
|
|
def get_customer_name(self, customer):
|
|
|
|
return self.get_customer_map().get(customer, {}).get("customer_name") or ""
|
|
|
|
|
|
|
|
def get_territory(self, customer):
|
|
|
|
return self.get_customer_map().get(customer, {}).get("territory") or ""
|
|
|
|
|
|
|
|
def get_customer_map(self):
|
|
|
|
if not hasattr(self, "customer_map"):
|
|
|
|
self.customer_map = dict(((r.name, r) for r in frappe.db.sql("""select
|
|
|
|
name, customer_name, territory from `tabCustomer`""", as_dict=True)))
|
|
|
|
|
|
|
|
return self.customer_map
|
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
def get_due_date(self, gle):
|
|
|
|
if not hasattr(self, "invoice_due_date_map"):
|
|
|
|
# TODO can be restricted to posting date
|
2014-02-26 07:05:33 +00:00
|
|
|
self.invoice_due_date_map = dict(frappe.db.sql("""select name, due_date
|
2013-11-07 15:14:30 +00:00
|
|
|
from `tabSales Invoice` where docstatus=1"""))
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
return gle.voucher_type == "Sales Invoice" \
|
|
|
|
and self.invoice_due_date_map.get(gle.voucher_no) or ""
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
def get_gl_entries(self):
|
|
|
|
if not hasattr(self, "gl_entries"):
|
|
|
|
conditions, values = self.prepare_conditions()
|
2014-02-26 07:05:33 +00:00
|
|
|
self.gl_entries = frappe.db.sql("""select * from `tabGL Entry`
|
2014-09-11 13:57:24 +00:00
|
|
|
where docstatus < 2 and party_type='Customer' {0}
|
|
|
|
order by posting_date, party""".format(conditions), values, as_dict=True)
|
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
return self.gl_entries
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
def prepare_conditions(self):
|
|
|
|
conditions = [""]
|
|
|
|
values = {}
|
2014-09-11 13:57:24 +00:00
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
if self.filters.company:
|
|
|
|
conditions.append("company=%(company)s")
|
|
|
|
values["company"] = self.filters.company
|
2014-09-11 13:57:24 +00:00
|
|
|
|
|
|
|
if self.filters.customer:
|
|
|
|
conditions.append("party=%(customer)s")
|
|
|
|
values["customer"] = self.filters.customer
|
|
|
|
|
2013-11-07 15:14:30 +00:00
|
|
|
return " and ".join(conditions), values
|
2014-09-11 13:57:24 +00:00
|
|
|
|
|
|
|
def get_gl_entries_for(self, party, against_voucher_type, against_voucher):
|
2013-11-07 15:14:30 +00:00
|
|
|
if not hasattr(self, "gl_entries_map"):
|
|
|
|
self.gl_entries_map = {}
|
|
|
|
for gle in self.get_gl_entries():
|
|
|
|
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):
|
2013-11-07 15:34:01 +00:00
|
|
|
return AccountsReceivableReport(filters).run()
|
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
|