From fe22686a8f05fbb76cd3fdf81bb16fa2e04e6684 Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Thu, 28 Dec 2017 16:11:27 +0530 Subject: [PATCH] Update last purchase rate from the purchase invoice (#12189) --- .../doctype/purchase_invoice/purchase_invoice.py | 4 ++++ .../doctype/purchase_order/purchase_order.py | 11 +++++------ erpnext/controllers/buying_controller.py | 14 +++++++++++++- .../doctype/purchase_receipt/purchase_receipt.py | 12 +++++------- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 07c136bf710..07853d0e4a7 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -277,6 +277,8 @@ class PurchaseInvoice(BuyingController): .format(item.purchase_receipt)) def on_submit(self): + super(PurchaseInvoice, self).on_submit() + self.check_prev_docstatus() self.update_status_updater_args() @@ -606,6 +608,8 @@ class PurchaseInvoice(BuyingController): )) def on_cancel(self): + super(PurchaseInvoice, self).on_cancel() + self.check_for_closed_status() self.update_status_updater_args() diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py index fac70aa0f54..df65ef92975 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/purchase_order.py @@ -11,8 +11,7 @@ from erpnext.controllers.buying_controller import BuyingController from erpnext.stock.doctype.item.item import get_last_purchase_details from erpnext.stock.stock_balance import update_bin_qty, get_ordered_qty from frappe.desk.notifications import clear_doctype_notifications -from erpnext.buying.utils import (validate_for_items, check_for_closed_status, - update_last_purchase_rate) +from erpnext.buying.utils import validate_for_items, check_for_closed_status form_grid_templates = { @@ -189,6 +188,8 @@ class PurchaseOrder(BuyingController): clear_doctype_notifications(self) def on_submit(self): + super(PurchaseOrder, self).on_submit() + if self.is_against_so(): self.update_status_updater() @@ -199,9 +200,9 @@ class PurchaseOrder(BuyingController): frappe.get_doc('Authorization Control').validate_approving_authority(self.doctype, self.company, self.base_grand_total) - update_last_purchase_rate(self, is_submit = 1) - def on_cancel(self): + super(PurchaseOrder, self).on_cancel() + if self.is_against_so(): self.update_status_updater() @@ -218,8 +219,6 @@ class PurchaseOrder(BuyingController): self.update_requested_qty() self.update_ordered_qty() - update_last_purchase_rate(self, is_submit = 0) - def on_update(self): pass diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py index fcf6a809f0e..e450f0bb593 100644 --- a/erpnext/controllers/buying_controller.py +++ b/erpnext/controllers/buying_controller.py @@ -8,7 +8,7 @@ from frappe.utils import flt,cint, cstr, getdate from erpnext.accounts.party import get_party_details from erpnext.stock.get_item_details import get_conversion_factor -from erpnext.buying.utils import validate_for_items +from erpnext.buying.utils import validate_for_items, update_last_purchase_rate from erpnext.stock.stock_ledger import get_valuation_rate from erpnext.controllers.stock_controller import StockController @@ -409,6 +409,18 @@ class BuyingController(StockController): "actual_qty": -1*flt(d.consumed_qty), })) + def on_submit(self): + if self.get('is_return'): + return + + update_last_purchase_rate(self, is_submit = 1) + + def on_cancel(self): + if self.get('is_return'): + return + + update_last_purchase_rate(self, is_submit = 0) + def validate_schedule_date(self): if not self.schedule_date: self.schedule_date = min([d.schedule_date for d in self.get("items")]) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index e49f9937a56..29f3553deb0 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -12,7 +12,7 @@ from frappe.utils import getdate from erpnext.controllers.buying_controller import BuyingController from erpnext.accounts.utils import get_account_currency from frappe.desk.notifications import clear_doctype_notifications -from erpnext.buying.utils import check_for_closed_status, update_last_purchase_rate +from erpnext.buying.utils import check_for_closed_status form_grid_templates = { "items": "templates/form_grid/item_grid.html" @@ -111,6 +111,8 @@ class PurchaseReceipt(BuyingController): # on submit def on_submit(self): + super(PurchaseReceipt, self).on_submit() + # Check for Approving Authority frappe.get_doc('Authorization Control').validate_approving_authority(self.doctype, self.company, self.base_grand_total) @@ -119,9 +121,6 @@ class PurchaseReceipt(BuyingController): if self.per_billed < 100: self.update_billing_status() - if not self.is_return: - update_last_purchase_rate(self, 1) - # Updating stock ledger should always be called after updating prevdoc status, # because updating ordered qty in bin depends upon updated ordered qty in PO self.update_stock_ledger() @@ -140,6 +139,8 @@ class PurchaseReceipt(BuyingController): frappe.throw(_("Purchase Invoice {0} is already submitted").format(self.submit_rv[0][0])) def on_cancel(self): + super(PurchaseReceipt, self).on_cancel() + self.check_for_closed_status() # Check if Purchase Invoice has been submitted against current Purchase Order submitted = frappe.db.sql("""select t1.name @@ -152,9 +153,6 @@ class PurchaseReceipt(BuyingController): self.update_prevdoc_status() self.update_billing_status() - if not self.is_return: - update_last_purchase_rate(self, 0) - # Updating stock ledger should always be called after updating prevdoc status, # because updating ordered qty in bin depends upon updated ordered qty in PO self.update_stock_ledger()