[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 = []
stock_items = self.get_stock_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"
# 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 \
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")):
item.expense_account = stock_not_billed_account
item.cost_center = None
@ -410,7 +410,7 @@ class PurchaseInvoice(BuyingController):
self.update_prevdoc_status()
self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
self.update_billing_status_in_pr()
self.make_gl_entries_on_cancel()
self.update_project()
@ -434,21 +434,21 @@ class PurchaseInvoice(BuyingController):
"fiscal_year": self.fiscal_year, "name": ("!=", self.name), "docstatus": ("<", 2)})
if 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 = []
for d in self.get("items"):
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)
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)
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):
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()
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_time_log_batch(self.name)
def before_cancel(self):
self.update_time_log_batch(None)
@ -385,7 +385,7 @@ class SalesInvoice(SellingController):
def validate_warehouse(self):
super(SalesInvoice, self).validate_warehouse()
for d in self.get('items'):
if not d.warehouse:
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:
msgprint(_("Please remove this Invoice {0} from C-Form {1}")
.format(self.name, self.c_form_no), raise_exception = 1)
def validate_dropship_item(self):
for item in self.items:
if item.sales_order:
@ -633,21 +633,21 @@ class SalesInvoice(SellingController):
"cost_center": self.write_off_cost_center
}, 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 = []
for d in self.get("items"):
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)
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)
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):
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):
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()
def make_sales_return(source_name, target_doc=None):
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):
if self.is_new():
return
if self.doctype in status_map:
_status = self.status
@ -104,9 +104,9 @@ class StatusUpdater(Document):
if self.status != _status and self.status not in ("Submitted", "Cancelled"):
self.add_comment("Label", _(self.status))
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)
def validate_qty(self):
@ -167,10 +167,10 @@ class StatusUpdater(Document):
throw(_("{0} must be reduced by {1} or you should increase overflow tolerance")
.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
: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:
# condition to include current record (if submit or no if cancel)
@ -179,17 +179,19 @@ class StatusUpdater(Document):
else:
args['cond'] = ' and parent!="%s"' % self.name.replace('"', '\"')
self._update_children(args)
self._update_children(args, update_modified)
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"""
for d in self.get_all_children():
if d.doctype != args['source_dt']:
continue
self._update_modified(args, update_modified)
# updates qty in the child table
args['detail_id'] = d.get(args['join_field'])
@ -208,29 +210,30 @@ class StatusUpdater(Document):
if not args.get("extra_cond"): args["extra_cond"] = ""
frappe.db.sql("""update `tab%(target_dt)s`
set %(target_field)s = (select ifnull(sum(%(source_field)s), 0)
from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s"
and (docstatus=1 %(cond)s) %(extra_cond)s) %(second_source_condition)s
set %(target_field)s = (
(select ifnull(sum(%(source_field)s), 0)
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)
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"""
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 name in distinct_transactions:
if 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"""
args['set_modified'] = ''
if change_modified:
args['set_modified'] = ', modified = now(), modified_by = "{0}"'\
.format(frappe.db.escape(frappe.session.user))
self._update_modified(args, update_modified)
if args.get('target_parent_field'):
frappe.db.sql("""update `tab%(target_parent_dt)s`
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)
/ sum(%(target_ref_field)s) * 100
from `tab%(target_dt)s` where parent="%(name)s"), 0), 2)
%(set_modified)s
%(update_modified)s
where name='%(name)s'""" % args)
# update field
@ -249,11 +252,17 @@ class StatusUpdater(Document):
'Fully %(keyword)s', 'Partly %(keyword)s'))
where name='%(name)s'""" % args)
if change_modified:
if update_modified:
target = frappe.get_doc(args["target_parent_dt"], args["name"])
target.set_status(update=True)
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):
ref_fieldname = ref_dt.lower().replace(" ", "_")
zero_amount_refdoc = []

View File

@ -313,7 +313,7 @@ class StockController(AccountsController):
for w in warehouses:
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({
"target_dt": self.doctype + " Item",
"target_parent_dt": self.doctype,
@ -321,7 +321,7 @@ class StockController(AccountsController):
"target_ref_field": "amount",
"target_field": "billed_amt",
"name": self.name,
}, set_modified)
}, update_modified)
def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for_items=None,
warehouse_account=None):

View File

@ -13,6 +13,6 @@ def execute():
where purchase_order=%s and docstatus=1""", po[0])
if 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:
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}):
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"):
frappe.reload_doctype(dt)
frappe.reload_doctype(dt + " Item")
# 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):
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)
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
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):
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)
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 billing status for all
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.update_billing_status(set_modified=False)
doc.set_status(update=True, update_modified=False)
doc.update_billing_status(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)"""
}]
def onload(self):
self.set_onload("has_return_entry", len(frappe.db.exists({"doctype": "Delivery Note",
def onload(self):
self.set_onload("has_return_entry", len(frappe.db.exists({"doctype": "Delivery Note",
"is_return": 1, "return_against": self.name, "docstatus": 1})))
def before_print(self):
@ -162,7 +162,7 @@ class DeliveryNote(SellingController):
def validate_warehouse(self):
super(DeliveryNote, self).validate_warehouse()
for d in self.get_item_list():
if frappe.db.get_value("Item", d['item_code'], "is_stock_item") == 1:
if not d['warehouse']:
@ -268,48 +268,48 @@ class DeliveryNote(SellingController):
self.set_status(update=True, status=status)
self.notify_update()
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]
for d in self.get("items"):
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:
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):
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()
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_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)
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
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
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
order by dn.posting_date asc, dn.posting_time asc, dn.name asc""", so_detail, as_dict=1)
updated_dn = []
for dnd in dn_details:
billed_amt_agianst_dn = 0
# If delivered against Sales Invoice
if dnd.si_detail:
billed_amt_agianst_dn = flt(dnd.amount)
billed_against_so -= billed_amt_agianst_dn
else:
# 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)
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
if billed_against_so and billed_amt_agianst_dn < dnd.amount:
pending_to_bill = flt(dnd.amount) - billed_amt_agianst_dn
@ -319,11 +319,11 @@ def update_billed_amount_based_on_so(so_detail):
else:
billed_amt_agianst_dn += billed_against_so
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)
return updated_dn
def get_list_context(context=None):

View File

@ -242,7 +242,7 @@ class PurchaseReceipt(BuyingController):
self.update_prevdoc_status()
self.update_ordered_qty()
self.update_billing_status()
if not self.is_return:
@ -282,7 +282,7 @@ class PurchaseReceipt(BuyingController):
self.update_prevdoc_status()
# Must be called after updating received qty in PO
self.update_ordered_qty()
self.update_billing_status()
if not self.is_return:
@ -439,39 +439,39 @@ class PurchaseReceipt(BuyingController):
self.set_status(update=True, status = status)
self.notify_update()
clear_doctype_notifications(self)
def update_billing_status(self, set_modified=True):
def update_billing_status(self, update_modified=True):
updated_pr = [self.name]
for d in self.get("items"):
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):
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()
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_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)
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
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
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
order by pr.posting_date asc, pr.posting_time asc, pr.name asc""", po_detail, as_dict=1)
updated_pr = []
for pr_item in pr_details:
# 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)
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
if billed_against_po and billed_amt_agianst_pr < pr_item.amount:
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:
billed_amt_agianst_pr += billed_against_po
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)
return updated_pr
@frappe.whitelist()