[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

@ -435,20 +435,20 @@ class PurchaseInvoice(BuyingController):
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

@ -634,20 +634,20 @@ class SalesInvoice(SellingController):
}, 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

View File

@ -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,12 +210,16 @@ 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'])])
@ -221,15 +227,12 @@ class StatusUpdater(Document):
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`
@ -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

@ -15,7 +15,7 @@ def execute():
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`
@ -23,11 +23,11 @@ def execute():
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

@ -269,21 +269,21 @@ class DeliveryNote(SellingController):
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)
@ -320,7 +320,7 @@ def update_billed_amount_based_on_so(so_detail):
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)

View File

@ -440,19 +440,19 @@ class PurchaseReceipt(BuyingController):
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)
@ -482,7 +482,7 @@ def update_billed_amount_based_on_po(po_detail):
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)