[refactor] changed set_modified, change_modified to update_modified like in the framework

This commit is contained in:
Anand Doshi 2016-01-06 16:32:06 +05:30
parent 1eea26fc5c
commit 6b71ef5843
9 changed files with 109 additions and 100 deletions

View File

@ -151,14 +151,14 @@ class PurchaseInvoice(BuyingController):
against_accounts = [] against_accounts = []
stock_items = self.get_stock_items() stock_items = self.get_stock_items()
for item in self.get("items"): for item in self.get("items"):
# in case of auto inventory accounting, # in case of auto inventory accounting,
# against expense account is always "Stock Received But Not Billed" # against expense account is always "Stock Received But Not Billed"
# for a stock item and if not epening entry and not drop-ship entry # for a stock item and if not epening entry and not drop-ship entry
if auto_accounting_for_stock and item.item_code in stock_items \ if auto_accounting_for_stock and item.item_code in stock_items \
and self.is_opening == 'No' and (not item.po_detail or and self.is_opening == 'No' and (not item.po_detail or
not frappe.db.get_value("Purchase Order Item", item.po_detail, "delivered_by_supplier")): not frappe.db.get_value("Purchase Order Item", item.po_detail, "delivered_by_supplier")):
item.expense_account = stock_not_billed_account item.expense_account = stock_not_billed_account
item.cost_center = None item.cost_center = None
@ -410,7 +410,7 @@ class PurchaseInvoice(BuyingController):
self.update_prevdoc_status() self.update_prevdoc_status()
self.update_billing_status_for_zero_amount_refdoc("Purchase Order") self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
self.update_billing_status_in_pr() self.update_billing_status_in_pr()
self.make_gl_entries_on_cancel() self.make_gl_entries_on_cancel()
self.update_project() self.update_project()
@ -434,21 +434,21 @@ class PurchaseInvoice(BuyingController):
"fiscal_year": self.fiscal_year, "name": ("!=", self.name), "docstatus": ("<", 2)}) "fiscal_year": self.fiscal_year, "name": ("!=", self.name), "docstatus": ("<", 2)})
if pi: if pi:
frappe.throw("Supplier Invoice No exists in Purchase Invoice {0}".format(pi)) frappe.throw("Supplier Invoice No exists in Purchase Invoice {0}".format(pi))
def update_billing_status_in_pr(self, set_modified=True): def update_billing_status_in_pr(self, update_modified=True):
updated_pr = [] updated_pr = []
for d in self.get("items"): for d in self.get("items"):
if d.pr_detail: if d.pr_detail:
billed_amt = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item` billed_amt = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item`
where pr_detail=%s and docstatus=1""", d.pr_detail) where pr_detail=%s and docstatus=1""", d.pr_detail)
billed_amt = billed_amt and billed_amt[0][0] or 0 billed_amt = billed_amt and billed_amt[0][0] or 0
frappe.db.set_value("Purchase Receipt Item", d.pr_detail, "billed_amt", billed_amt) frappe.db.set_value("Purchase Receipt Item", d.pr_detail, "billed_amt", billed_amt, update_modified=update_modified)
updated_pr.append(d.purchase_receipt) updated_pr.append(d.purchase_receipt)
elif d.po_detail: elif d.po_detail:
updated_pr += update_billed_amount_based_on_po(d.po_detail) updated_pr += update_billed_amount_based_on_po(d.po_detail, update_modified)
for pr in set(updated_pr): for pr in set(updated_pr):
frappe.get_doc("Purchase Receipt", pr).update_billing_percentage(set_modified=set_modified) frappe.get_doc("Purchase Receipt", pr).update_billing_percentage(update_modified=update_modified)
@frappe.whitelist() @frappe.whitelist()
def get_expense_account(doctype, txt, searchfield, start, page_len, filters): def get_expense_account(doctype, txt, searchfield, start, page_len, filters):

View File

@ -112,7 +112,7 @@ class SalesInvoice(SellingController):
self.update_against_document_in_jv() self.update_against_document_in_jv()
self.update_time_log_batch(self.name) self.update_time_log_batch(self.name)
def before_cancel(self): def before_cancel(self):
self.update_time_log_batch(None) self.update_time_log_batch(None)
@ -385,7 +385,7 @@ class SalesInvoice(SellingController):
def validate_warehouse(self): def validate_warehouse(self):
super(SalesInvoice, self).validate_warehouse() super(SalesInvoice, self).validate_warehouse()
for d in self.get('items'): for d in self.get('items'):
if not d.warehouse: if not d.warehouse:
frappe.throw(_("Warehouse required at Row No {0}").format(d.idx)) frappe.throw(_("Warehouse required at Row No {0}").format(d.idx))
@ -414,7 +414,7 @@ class SalesInvoice(SellingController):
if self.c_form_applicable == 'Yes' and self.c_form_no: if self.c_form_applicable == 'Yes' and self.c_form_no:
msgprint(_("Please remove this Invoice {0} from C-Form {1}") msgprint(_("Please remove this Invoice {0} from C-Form {1}")
.format(self.name, self.c_form_no), raise_exception = 1) .format(self.name, self.c_form_no), raise_exception = 1)
def validate_dropship_item(self): def validate_dropship_item(self):
for item in self.items: for item in self.items:
if item.sales_order: if item.sales_order:
@ -633,21 +633,21 @@ class SalesInvoice(SellingController):
"cost_center": self.write_off_cost_center "cost_center": self.write_off_cost_center
}, write_off_account_currency) }, write_off_account_currency)
) )
def update_billing_status_in_dn(self, set_modified=True): def update_billing_status_in_dn(self, update_modified=True):
updated_delivery_notes = [] updated_delivery_notes = []
for d in self.get("items"): for d in self.get("items"):
if d.dn_detail: if d.dn_detail:
billed_amt = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item` billed_amt = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item`
where dn_detail=%s and docstatus=1""", d.dn_detail) where dn_detail=%s and docstatus=1""", d.dn_detail)
billed_amt = billed_amt and billed_amt[0][0] or 0 billed_amt = billed_amt and billed_amt[0][0] or 0
frappe.db.set_value("Delivery Note Item", d.dn_detail, "billed_amt", billed_amt) frappe.db.set_value("Delivery Note Item", d.dn_detail, "billed_amt", billed_amt, update_modified=update_modified)
updated_delivery_notes.append(d.delivery_note) updated_delivery_notes.append(d.delivery_note)
elif d.so_detail: elif d.so_detail:
updated_delivery_notes += update_billed_amount_based_on_so(d.so_detail) updated_delivery_notes += update_billed_amount_based_on_so(d.so_detail, update_modified)
for dn in set(updated_delivery_notes): for dn in set(updated_delivery_notes):
frappe.get_doc("Delivery Note", dn).update_billing_percentage(set_modified=set_modified) frappe.get_doc("Delivery Note", dn).update_billing_percentage(update_modified=update_modified)
def get_list_context(context=None): def get_list_context(context=None):
from erpnext.controllers.website_list_for_contact import get_list_context from erpnext.controllers.website_list_for_contact import get_list_context
@ -717,4 +717,4 @@ def make_delivery_note(source_name, target_doc=None):
@frappe.whitelist() @frappe.whitelist()
def make_sales_return(source_name, target_doc=None): def make_sales_return(source_name, target_doc=None):
from erpnext.controllers.sales_and_purchase_return import make_return_doc from erpnext.controllers.sales_and_purchase_return import make_return_doc
return make_return_doc("Sales Invoice", source_name, target_doc) return make_return_doc("Sales Invoice", source_name, target_doc)

View File

@ -81,7 +81,7 @@ class StatusUpdater(Document):
def set_status(self, update=False, status=None, update_modified=True): def set_status(self, update=False, status=None, update_modified=True):
if self.is_new(): if self.is_new():
return return
if self.doctype in status_map: if self.doctype in status_map:
_status = self.status _status = self.status
@ -104,9 +104,9 @@ class StatusUpdater(Document):
if self.status != _status and self.status not in ("Submitted", "Cancelled"): if self.status != _status and self.status not in ("Submitted", "Cancelled"):
self.add_comment("Label", _(self.status)) self.add_comment("Label", _(self.status))
if update: if update:
frappe.db.set_value(self.doctype, self.name, "status", self.status, frappe.db.set_value(self.doctype, self.name, "status", self.status,
update_modified=update_modified) update_modified=update_modified)
def validate_qty(self): def validate_qty(self):
@ -167,10 +167,10 @@ class StatusUpdater(Document):
throw(_("{0} must be reduced by {1} or you should increase overflow tolerance") throw(_("{0} must be reduced by {1} or you should increase overflow tolerance")
.format(_(item["target_ref_field"].title()), item["reduce_by"])) .format(_(item["target_ref_field"].title()), item["reduce_by"]))
def update_qty(self, change_modified=True): def update_qty(self, update_modified=True):
"""Updates qty or amount at row level """Updates qty or amount at row level
:param change_modified: If true, updates `modified` and `modified_by` for target parent doc :param update_modified: If true, updates `modified` and `modified_by` for target parent doc
""" """
for args in self.status_updater: for args in self.status_updater:
# condition to include current record (if submit or no if cancel) # condition to include current record (if submit or no if cancel)
@ -179,17 +179,19 @@ class StatusUpdater(Document):
else: else:
args['cond'] = ' and parent!="%s"' % self.name.replace('"', '\"') args['cond'] = ' and parent!="%s"' % self.name.replace('"', '\"')
self._update_children(args) self._update_children(args, update_modified)
if "percent_join_field" in args: if "percent_join_field" in args:
self._update_percent_field_in_targets(args, change_modified) self._update_percent_field_in_targets(args, update_modified)
def _update_children(self, args): def _update_children(self, args, update_modified):
"""Update quantities or amount in child table""" """Update quantities or amount in child table"""
for d in self.get_all_children(): for d in self.get_all_children():
if d.doctype != args['source_dt']: if d.doctype != args['source_dt']:
continue continue
self._update_modified(args, update_modified)
# updates qty in the child table # updates qty in the child table
args['detail_id'] = d.get(args['join_field']) args['detail_id'] = d.get(args['join_field'])
@ -208,29 +210,30 @@ class StatusUpdater(Document):
if not args.get("extra_cond"): args["extra_cond"] = "" if not args.get("extra_cond"): args["extra_cond"] = ""
frappe.db.sql("""update `tab%(target_dt)s` frappe.db.sql("""update `tab%(target_dt)s`
set %(target_field)s = (select ifnull(sum(%(source_field)s), 0) set %(target_field)s = (
from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s" (select ifnull(sum(%(source_field)s), 0)
and (docstatus=1 %(cond)s) %(extra_cond)s) %(second_source_condition)s from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s"
and (docstatus=1 %(cond)s) %(extra_cond)s)
%(second_source_condition)s
)
%(update_modified)s
where name='%(detail_id)s'""" % args) where name='%(detail_id)s'""" % args)
def _update_percent_field_in_targets(self, args, change_modified=True): def _update_percent_field_in_targets(self, args, update_modified=True):
"""Update percent field in parent transaction""" """Update percent field in parent transaction"""
distinct_transactions = set([d.get(args['percent_join_field']) distinct_transactions = set([d.get(args['percent_join_field'])
for d in self.get_all_children(args['source_dt'])]) for d in self.get_all_children(args['source_dt'])])
for name in distinct_transactions: for name in distinct_transactions:
if name: if name:
args['name'] = name args['name'] = name
self._update_percent_field(args, change_modified) self._update_percent_field(args, update_modified)
def _update_percent_field(self, args, change_modified=True): def _update_percent_field(self, args, update_modified=True):
"""Update percent field in parent transaction""" """Update percent field in parent transaction"""
args['set_modified'] = '' self._update_modified(args, update_modified)
if change_modified:
args['set_modified'] = ', modified = now(), modified_by = "{0}"'\
.format(frappe.db.escape(frappe.session.user))
if args.get('target_parent_field'): if args.get('target_parent_field'):
frappe.db.sql("""update `tab%(target_parent_dt)s` frappe.db.sql("""update `tab%(target_parent_dt)s`
set %(target_parent_field)s = round( set %(target_parent_field)s = round(
@ -238,7 +241,7 @@ class StatusUpdater(Document):
ifnull(sum(if(%(target_ref_field)s > %(target_field)s, %(target_field)s, %(target_ref_field)s)), 0) ifnull(sum(if(%(target_ref_field)s > %(target_field)s, %(target_field)s, %(target_ref_field)s)), 0)
/ sum(%(target_ref_field)s) * 100 / sum(%(target_ref_field)s) * 100
from `tab%(target_dt)s` where parent="%(name)s"), 0), 2) from `tab%(target_dt)s` where parent="%(name)s"), 0), 2)
%(set_modified)s %(update_modified)s
where name='%(name)s'""" % args) where name='%(name)s'""" % args)
# update field # update field
@ -249,11 +252,17 @@ class StatusUpdater(Document):
'Fully %(keyword)s', 'Partly %(keyword)s')) 'Fully %(keyword)s', 'Partly %(keyword)s'))
where name='%(name)s'""" % args) where name='%(name)s'""" % args)
if change_modified: if update_modified:
target = frappe.get_doc(args["target_parent_dt"], args["name"]) target = frappe.get_doc(args["target_parent_dt"], args["name"])
target.set_status(update=True) target.set_status(update=True)
target.notify_update() target.notify_update()
def _update_modified(self, args, update_modified):
args['update_modified'] = ''
if update_modified:
args['update_modified'] = ', modified = now(), modified_by = "{0}"'\
.format(frappe.db.escape(frappe.session.user))
def update_billing_status_for_zero_amount_refdoc(self, ref_dt): def update_billing_status_for_zero_amount_refdoc(self, ref_dt):
ref_fieldname = ref_dt.lower().replace(" ", "_") ref_fieldname = ref_dt.lower().replace(" ", "_")
zero_amount_refdoc = [] zero_amount_refdoc = []

View File

@ -313,7 +313,7 @@ class StockController(AccountsController):
for w in warehouses: for w in warehouses:
validate_warehouse_company(w, self.company) validate_warehouse_company(w, self.company)
def update_billing_percentage(self, set_modified=True): def update_billing_percentage(self, update_modified=True):
self._update_percent_field({ self._update_percent_field({
"target_dt": self.doctype + " Item", "target_dt": self.doctype + " Item",
"target_parent_dt": self.doctype, "target_parent_dt": self.doctype,
@ -321,7 +321,7 @@ class StockController(AccountsController):
"target_ref_field": "amount", "target_ref_field": "amount",
"target_field": "billed_amt", "target_field": "billed_amt",
"name": self.name, "name": self.name,
}, set_modified) }, update_modified)
def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for_items=None, def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for_items=None,
warehouse_account=None): warehouse_account=None):

View File

@ -13,6 +13,6 @@ def execute():
where purchase_order=%s and docstatus=1""", po[0]) where purchase_order=%s and docstatus=1""", po[0])
if invoices: if invoices:
for inv in invoices: for inv in invoices:
frappe.get_doc("Purchase Invoice", inv[0]).update_qty(change_modified=False) frappe.get_doc("Purchase Invoice", inv[0]).update_qty(update_modified=False)
else: else:
frappe.db.sql("""update `tabPurchase Order` set per_billed=0 where name=%s""", po[0]) frappe.db.sql("""update `tabPurchase Order` set per_billed=0 where name=%s""", po[0])

View File

@ -14,4 +14,4 @@ def execute():
{"patch_date": not_null_patch_date}): {"patch_date": not_null_patch_date}):
doc = frappe.get_doc(doctype, name) doc = frappe.get_doc(doctype, name)
doc.update_qty(change_modified=False) doc.update_qty(update_modified=False)

View File

@ -8,26 +8,26 @@ def execute():
for dt in ("Delivery Note", "Purchase Receipt"): for dt in ("Delivery Note", "Purchase Receipt"):
frappe.reload_doctype(dt) frappe.reload_doctype(dt)
frappe.reload_doctype(dt + " Item") frappe.reload_doctype(dt + " Item")
# Update billed_amt in DN and PR which are not against any order # Update billed_amt in DN and PR which are not against any order
for d in frappe.db.sql("""select name from `tabDelivery Note Item` for d in frappe.db.sql("""select name from `tabDelivery Note Item`
where (so_detail is null or so_detail = '') and docstatus=1""", as_dict=1): where (so_detail is null or so_detail = '') and docstatus=1""", as_dict=1):
billed_amt = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item` billed_amt = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item`
where dn_detail=%s and docstatus=1""", d.name) where dn_detail=%s and docstatus=1""", d.name)
billed_amt = billed_amt and billed_amt[0][0] or 0 billed_amt = billed_amt and billed_amt[0][0] or 0
frappe.db.set_value("Delivery Note Item", d.name, "billed_amt", billed_amt) frappe.db.set_value("Delivery Note Item", d.name, "billed_amt", billed_amt, update_modified=False)
# Update billed_amt in DN and PR which are not against any order # Update billed_amt in DN and PR which are not against any order
for d in frappe.db.sql("""select name from `tabPurchase Receipt Item` for d in frappe.db.sql("""select name from `tabPurchase Receipt Item`
where (prevdoc_detail_docname is null or prevdoc_detail_docname = '') and docstatus=1""", as_dict=1): where (prevdoc_detail_docname is null or prevdoc_detail_docname = '') and docstatus=1""", as_dict=1):
billed_amt = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item` billed_amt = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item`
where pr_detail=%s and docstatus=1""", d.name) where pr_detail=%s and docstatus=1""", d.name)
billed_amt = billed_amt and billed_amt[0][0] or 0 billed_amt = billed_amt and billed_amt[0][0] or 0
frappe.db.set_value("Purchase Receipt Item", d.name, "billed_amt", billed_amt) frappe.db.set_value("Purchase Receipt Item", d.name, "billed_amt", billed_amt, update_modified=False)
# Update billed amt which are against order or invoice # Update billed amt which are against order or invoice
# Update billing status for all # Update billing status for all
for d in frappe.db.sql("select name from `tab{0}` where docstatus=1".format(dt), as_dict=1): for d in frappe.db.sql("select name from `tab{0}` where docstatus=1".format(dt), as_dict=1):
doc = frappe.get_doc(dt, d.name) doc = frappe.get_doc(dt, d.name)
doc.update_billing_status(set_modified=False) doc.update_billing_status(update_modified=False)
doc.set_status(update=True, update_modified=False) doc.set_status(update=True, update_modified=False)

View File

@ -61,8 +61,8 @@ class DeliveryNote(SellingController):
'extra_cond': """ and exists (select name from `tabDelivery Note` where name=`tabDelivery Note Item`.parent and is_return=1)""" 'extra_cond': """ and exists (select name from `tabDelivery Note` where name=`tabDelivery Note Item`.parent and is_return=1)"""
}] }]
def onload(self): def onload(self):
self.set_onload("has_return_entry", len(frappe.db.exists({"doctype": "Delivery Note", self.set_onload("has_return_entry", len(frappe.db.exists({"doctype": "Delivery Note",
"is_return": 1, "return_against": self.name, "docstatus": 1}))) "is_return": 1, "return_against": self.name, "docstatus": 1})))
def before_print(self): def before_print(self):
@ -162,7 +162,7 @@ class DeliveryNote(SellingController):
def validate_warehouse(self): def validate_warehouse(self):
super(DeliveryNote, self).validate_warehouse() super(DeliveryNote, self).validate_warehouse()
for d in self.get_item_list(): for d in self.get_item_list():
if frappe.db.get_value("Item", d['item_code'], "is_stock_item") == 1: if frappe.db.get_value("Item", d['item_code'], "is_stock_item") == 1:
if not d['warehouse']: if not d['warehouse']:
@ -268,48 +268,48 @@ class DeliveryNote(SellingController):
self.set_status(update=True, status=status) self.set_status(update=True, status=status)
self.notify_update() self.notify_update()
clear_doctype_notifications(self) clear_doctype_notifications(self)
def update_billing_status(self, set_modified=True): def update_billing_status(self, update_modified=True):
updated_delivery_notes = [self.name] updated_delivery_notes = [self.name]
for d in self.get("items"): for d in self.get("items"):
if d.si_detail and not d.so_detail: if d.si_detail and not d.so_detail:
frappe.db.set(d, 'billed_amt', d.amount) d.db_set('billed_amt', d.amount, update_modified=update_modified)
elif d.so_detail: elif d.so_detail:
updated_delivery_notes += update_billed_amount_based_on_so(d.so_detail) updated_delivery_notes += update_billed_amount_based_on_so(d.so_detail, update_modified)
for dn in set(updated_delivery_notes): for dn in set(updated_delivery_notes):
dn_doc = self if (dn == self.name) else frappe.get_doc("Delivery Note", dn) dn_doc = self if (dn == self.name) else frappe.get_doc("Delivery Note", dn)
dn_doc.update_billing_percentage(set_modified=set_modified) dn_doc.update_billing_percentage(update_modified=update_modified)
self.load_from_db() self.load_from_db()
def update_billed_amount_based_on_so(so_detail): def update_billed_amount_based_on_so(so_detail, update_modified=True):
# Billed against Sales Order directly # Billed against Sales Order directly
billed_against_so = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item` billed_against_so = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item`
where so_detail=%s and (dn_detail is null or dn_detail = '') and docstatus=1""", so_detail) where so_detail=%s and (dn_detail is null or dn_detail = '') and docstatus=1""", so_detail)
billed_against_so = billed_against_so and billed_against_so[0][0] or 0 billed_against_so = billed_against_so and billed_against_so[0][0] or 0
# Get all Delivery Note Item rows against the Sales Order Item row # Get all Delivery Note Item rows against the Sales Order Item row
dn_details = frappe.db.sql("""select dn_item.name, dn_item.amount, dn_item.si_detail, dn_item.parent dn_details = frappe.db.sql("""select dn_item.name, dn_item.amount, dn_item.si_detail, dn_item.parent
from `tabDelivery Note Item` dn_item, `tabDelivery Note` dn from `tabDelivery Note Item` dn_item, `tabDelivery Note` dn
where dn.name=dn_item.parent and dn_item.so_detail=%s where dn.name=dn_item.parent and dn_item.so_detail=%s
and dn.docstatus=1 and dn.is_return = 0 and dn.docstatus=1 and dn.is_return = 0
order by dn.posting_date asc, dn.posting_time asc, dn.name asc""", so_detail, as_dict=1) order by dn.posting_date asc, dn.posting_time asc, dn.name asc""", so_detail, as_dict=1)
updated_dn = [] updated_dn = []
for dnd in dn_details: for dnd in dn_details:
billed_amt_agianst_dn = 0 billed_amt_agianst_dn = 0
# If delivered against Sales Invoice # If delivered against Sales Invoice
if dnd.si_detail: if dnd.si_detail:
billed_amt_agianst_dn = flt(dnd.amount) billed_amt_agianst_dn = flt(dnd.amount)
billed_against_so -= billed_amt_agianst_dn billed_against_so -= billed_amt_agianst_dn
else: else:
# Get billed amount directly against Delivery Note # Get billed amount directly against Delivery Note
billed_amt_agianst_dn = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item` billed_amt_agianst_dn = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item`
where dn_detail=%s and docstatus=1""", dnd.name) where dn_detail=%s and docstatus=1""", dnd.name)
billed_amt_agianst_dn = billed_amt_agianst_dn and billed_amt_agianst_dn[0][0] or 0 billed_amt_agianst_dn = billed_amt_agianst_dn and billed_amt_agianst_dn[0][0] or 0
# Distribute billed amount directly against SO between DNs based on FIFO # Distribute billed amount directly against SO between DNs based on FIFO
if billed_against_so and billed_amt_agianst_dn < dnd.amount: if billed_against_so and billed_amt_agianst_dn < dnd.amount:
pending_to_bill = flt(dnd.amount) - billed_amt_agianst_dn pending_to_bill = flt(dnd.amount) - billed_amt_agianst_dn
@ -319,11 +319,11 @@ def update_billed_amount_based_on_so(so_detail):
else: else:
billed_amt_agianst_dn += billed_against_so billed_amt_agianst_dn += billed_against_so
billed_against_so = 0 billed_against_so = 0
frappe.db.set_value("Delivery Note Item", dnd.name, "billed_amt", billed_amt_agianst_dn) frappe.db.set_value("Delivery Note Item", dnd.name, "billed_amt", billed_amt_agianst_dn, update_modified=update_modified)
updated_dn.append(dnd.parent) updated_dn.append(dnd.parent)
return updated_dn return updated_dn
def get_list_context(context=None): def get_list_context(context=None):

View File

@ -242,7 +242,7 @@ class PurchaseReceipt(BuyingController):
self.update_prevdoc_status() self.update_prevdoc_status()
self.update_ordered_qty() self.update_ordered_qty()
self.update_billing_status() self.update_billing_status()
if not self.is_return: if not self.is_return:
@ -282,7 +282,7 @@ class PurchaseReceipt(BuyingController):
self.update_prevdoc_status() self.update_prevdoc_status()
# Must be called after updating received qty in PO # Must be called after updating received qty in PO
self.update_ordered_qty() self.update_ordered_qty()
self.update_billing_status() self.update_billing_status()
if not self.is_return: if not self.is_return:
@ -439,39 +439,39 @@ class PurchaseReceipt(BuyingController):
self.set_status(update=True, status = status) self.set_status(update=True, status = status)
self.notify_update() self.notify_update()
clear_doctype_notifications(self) clear_doctype_notifications(self)
def update_billing_status(self, set_modified=True): def update_billing_status(self, update_modified=True):
updated_pr = [self.name] updated_pr = [self.name]
for d in self.get("items"): for d in self.get("items"):
if d.prevdoc_detail_docname: if d.prevdoc_detail_docname:
updated_pr += update_billed_amount_based_on_po(d.prevdoc_detail_docname) updated_pr += update_billed_amount_based_on_po(d.prevdoc_detail_docname, update_modified)
for pr in set(updated_pr): for pr in set(updated_pr):
pr_doc = self if (pr == self.name) else frappe.get_doc("Purchase Receipt", pr) pr_doc = self if (pr == self.name) else frappe.get_doc("Purchase Receipt", pr)
pr_doc.update_billing_percentage(set_modified=set_modified) pr_doc.update_billing_percentage(update_modified=update_modified)
self.load_from_db() self.load_from_db()
def update_billed_amount_based_on_po(po_detail): def update_billed_amount_based_on_po(po_detail, update_modified=True):
# Billed against Sales Order directly # Billed against Sales Order directly
billed_against_po = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item` billed_against_po = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item`
where po_detail=%s and (pr_detail is null or pr_detail = '') and docstatus=1""", po_detail) where po_detail=%s and (pr_detail is null or pr_detail = '') and docstatus=1""", po_detail)
billed_against_po = billed_against_po and billed_against_po[0][0] or 0 billed_against_po = billed_against_po and billed_against_po[0][0] or 0
# Get all Delivery Note Item rows against the Sales Order Item row # Get all Delivery Note Item rows against the Sales Order Item row
pr_details = frappe.db.sql("""select pr_item.name, pr_item.amount, pr_item.parent pr_details = frappe.db.sql("""select pr_item.name, pr_item.amount, pr_item.parent
from `tabPurchase Receipt Item` pr_item, `tabPurchase Receipt` pr from `tabPurchase Receipt Item` pr_item, `tabPurchase Receipt` pr
where pr.name=pr_item.parent and pr_item.prevdoc_detail_docname=%s where pr.name=pr_item.parent and pr_item.prevdoc_detail_docname=%s
and pr.docstatus=1 and pr.is_return = 0 and pr.docstatus=1 and pr.is_return = 0
order by pr.posting_date asc, pr.posting_time asc, pr.name asc""", po_detail, as_dict=1) order by pr.posting_date asc, pr.posting_time asc, pr.name asc""", po_detail, as_dict=1)
updated_pr = [] updated_pr = []
for pr_item in pr_details: for pr_item in pr_details:
# Get billed amount directly against Purchase Receipt # Get billed amount directly against Purchase Receipt
billed_amt_agianst_pr = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item` billed_amt_agianst_pr = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item`
where pr_detail=%s and docstatus=1""", pr_item.name) where pr_detail=%s and docstatus=1""", pr_item.name)
billed_amt_agianst_pr = billed_amt_agianst_pr and billed_amt_agianst_pr[0][0] or 0 billed_amt_agianst_pr = billed_amt_agianst_pr and billed_amt_agianst_pr[0][0] or 0
# Distribute billed amount directly against PO between PRs based on FIFO # Distribute billed amount directly against PO between PRs based on FIFO
if billed_against_po and billed_amt_agianst_pr < pr_item.amount: if billed_against_po and billed_amt_agianst_pr < pr_item.amount:
pending_to_bill = flt(pr_item.amount) - billed_amt_agianst_pr pending_to_bill = flt(pr_item.amount) - billed_amt_agianst_pr
@ -481,11 +481,11 @@ def update_billed_amount_based_on_po(po_detail):
else: else:
billed_amt_agianst_pr += billed_against_po billed_amt_agianst_pr += billed_against_po
billed_against_po = 0 billed_against_po = 0
frappe.db.set_value("Purchase Receipt Item", pr_item.name, "billed_amt", billed_amt_agianst_pr) frappe.db.set_value("Purchase Receipt Item", pr_item.name, "billed_amt", billed_amt_agianst_pr, update_modified=update_modified)
updated_pr.append(pr_item.parent) updated_pr.append(pr_item.parent)
return updated_pr return updated_pr
@frappe.whitelist() @frappe.whitelist()