Update billing percentage and status from SI/PI in SO/PO, when net total is zero
diff --git a/accounts/doctype/accounts_settings/accounts_settings.py b/accounts/doctype/accounts_settings/accounts_settings.py
index a6e9938..2475fda 100644
--- a/accounts/doctype/accounts_settings/accounts_settings.py
+++ b/accounts/doctype/accounts_settings/accounts_settings.py
@@ -6,6 +6,7 @@
from __future__ import unicode_literals
import webnotes
from webnotes import _
+from webnotes.utils import cint
class DocType:
def __init__(self, d, dl):
@@ -14,7 +15,12 @@
def on_update(self):
webnotes.conn.set_default("auto_accounting_for_stock", self.doc.auto_accounting_for_stock)
- if self.doc.auto_accounting_for_stock:
+ if cint(self.doc.auto_accounting_for_stock):
+ # set default perpetual account in company
+ for company in webnotes.conn.sql("select name from tabCompany"):
+ webnotes.bean("Company", company[0]).save()
+
+ # Create account head for warehouses
warehouse_list = webnotes.conn.sql("select name, company from tabWarehouse", as_dict=1)
warehouse_with_no_company = [d.name for d in warehouse_list if not d.company]
if warehouse_with_no_company:
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index 06b7a3a..fcd6846 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -302,6 +302,7 @@
self.make_gl_entries()
self.update_against_document_in_jv()
self.update_prevdoc_status()
+ self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
def make_gl_entries(self):
auto_accounting_for_stock = \
@@ -421,7 +422,7 @@
remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_voucher")
self.update_prevdoc_status()
-
+ self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
self.make_cancel_gl_entries()
def on_update(self):
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index bfba30f..a39702b 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -88,6 +88,7 @@
self.update_status_updater_args()
self.update_prevdoc_status()
+ self.update_billing_status_for_zero_amount_refdoc("Sales Order")
# this sequence because outstanding may get -ve
self.make_gl_entries()
@@ -114,6 +115,7 @@
self.update_status_updater_args()
self.update_prevdoc_status()
+ self.update_billing_status_for_zero_amount_refdoc("Sales Order")
self.make_cancel_gl_entries()
diff --git a/controllers/status_updater.py b/controllers/status_updater.py
index a285c47..1743887 100644
--- a/controllers/status_updater.py
+++ b/controllers/status_updater.py
@@ -232,6 +232,43 @@
'Fully %(keyword)s', 'Partly %(keyword)s'))
where name='%(name)s'""" % args)
+
+ def update_billing_status_for_zero_amount_refdoc(self, ref_dt):
+ ref_fieldname = ref_dt.lower().replace(" ", "_")
+ zero_amount_refdoc = []
+ all_zero_amount_refdoc = webnotes.conn.sql_list("""select name from `tab%s`
+ where docstatus=1 and net_total = 0""" % ref_dt)
+
+ for item in self.doclist.get({"parentfield": "entries"}):
+ if item.fields.get(ref_fieldname) \
+ and item.fields.get(ref_fieldname) in all_zero_amount_refdoc \
+ and item.fields.get(ref_fieldname) not in zero_amount_refdoc:
+ zero_amount_refdoc.append(item.fields[ref_fieldname])
+
+ if zero_amount_refdoc:
+ self.update_biling_status(zero_amount_refdoc, ref_dt, ref_fieldname)
+
+ def update_biling_status(self, zero_amount_refdoc, ref_dt, ref_fieldname):
+ for ref_dn in zero_amount_refdoc:
+ ref_doc_qty = flt(webnotes.conn.sql("""select sum(ifnull(qty, 0)) from `tab%s Item`
+ where parent=%s""" % (ref_dt, '%s'), (ref_dn))[0][0])
+
+ billed_qty = flt(webnotes.conn.sql("""select sum(ifnull(qty, 0))
+ from `tab%s Item` where %s=%s and docstatus=1""" %
+ (self.doc.doctype, ref_fieldname, '%s'), (ref_dn))[0][0])
+
+ per_billed = ((ref_doc_qty if billed_qty > ref_doc_qty else billed_qty)\
+ / ref_doc_qty)*100
+ webnotes.conn.set_value(ref_dt, ref_dn, "per_billed", per_billed)
+
+ from webnotes.model.meta import has_field
+ if has_field(ref_dt, "billing_status"):
+ if per_billed < 0.001: billing_status = "Not Billed"
+ elif per_billed >= 99.99: billing_status = "Fully Billed"
+ else: billing_status = "Partly Billed"
+
+ webnotes.conn.set_value(ref_dt, ref_dn, "billing_status", billing_status)
+
def get_tolerance_for(item_code, item_tolerance={}, global_tolerance=None):
"""
Returns the tolerance for the item, if not set, returns global tolerance