mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-18 12:39:18 +00:00
fix: performance issue of sales invoice while save/submit (#19598)
* fix: performace issue of sales invoice while save/submit * Cached price list data, item group child data, added indexing for blanket order
This commit is contained in:
committed by
Nabin Hait
parent
238521c2bd
commit
a85ddf2fb4
@@ -5,15 +5,17 @@ from __future__ import unicode_literals
|
||||
import frappe, erpnext
|
||||
import json
|
||||
from frappe import _, throw
|
||||
from frappe.utils import today, flt, cint, fmt_money, formatdate, getdate, add_days, add_months, get_last_day, nowdate
|
||||
from erpnext.stock.get_item_details import get_conversion_factor
|
||||
from frappe.utils import (today, flt, cint, fmt_money, formatdate,
|
||||
getdate, add_days, add_months, get_last_day, nowdate, get_link_to_form)
|
||||
from erpnext.stock.get_item_details import get_conversion_factor, get_item_details
|
||||
from erpnext.setup.utils import get_exchange_rate
|
||||
from erpnext.accounts.utils import get_fiscal_years, validate_fiscal_year, get_account_currency
|
||||
from erpnext.utilities.transaction_base import TransactionBase
|
||||
from erpnext.buying.utils import update_last_purchase_rate
|
||||
from erpnext.controllers.sales_and_purchase_return import validate_return
|
||||
from erpnext.accounts.party import get_party_account_currency, validate_party_frozen_disabled
|
||||
from erpnext.accounts.doctype.pricing_rule.utils import validate_pricing_rules
|
||||
from erpnext.accounts.doctype.pricing_rule.utils import (apply_pricing_rule_on_transaction,
|
||||
apply_pricing_rule_for_free_items, get_applied_pricing_rules)
|
||||
from erpnext.exceptions import InvalidCurrency
|
||||
from six import text_type
|
||||
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_accounting_dimensions
|
||||
@@ -101,7 +103,7 @@ class AccountsController(TransactionBase):
|
||||
|
||||
validate_regional(self)
|
||||
if self.doctype != 'Material Request':
|
||||
validate_pricing_rules(self)
|
||||
apply_pricing_rule_on_transaction(self)
|
||||
|
||||
def validate_invoice_documents_schedule(self):
|
||||
self.validate_payment_schedule_dates()
|
||||
@@ -232,7 +234,6 @@ class AccountsController(TransactionBase):
|
||||
|
||||
def set_missing_item_details(self, for_validate=False):
|
||||
"""set missing item values"""
|
||||
from erpnext.stock.get_item_details import get_item_details
|
||||
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
|
||||
|
||||
if hasattr(self, "items"):
|
||||
@@ -244,7 +245,6 @@ class AccountsController(TransactionBase):
|
||||
document_type = "{} Item".format(self.doctype)
|
||||
parent_dict.update({"document_type": document_type})
|
||||
|
||||
self.set('pricing_rules', [])
|
||||
# party_name field used for customer in quotation
|
||||
if self.doctype == "Quotation" and self.quotation_to == "Customer" and parent_dict.get("party_name"):
|
||||
parent_dict.update({"customer": parent_dict.get("party_name")})
|
||||
@@ -264,7 +264,7 @@ class AccountsController(TransactionBase):
|
||||
if self.get("is_subcontracted"):
|
||||
args["is_subcontracted"] = self.is_subcontracted
|
||||
|
||||
ret = get_item_details(args, self, overwrite_warehouse=False)
|
||||
ret = get_item_details(args, self, for_validate=True, overwrite_warehouse=False)
|
||||
|
||||
for fieldname, value in ret.items():
|
||||
if item.meta.get_field(fieldname) and value is not None:
|
||||
@@ -285,24 +285,42 @@ class AccountsController(TransactionBase):
|
||||
if self.doctype in ["Purchase Invoice", "Sales Invoice"] and item.meta.get_field('is_fixed_asset'):
|
||||
item.set('is_fixed_asset', ret.get('is_fixed_asset', 0))
|
||||
|
||||
if ret.get("pricing_rules") and not ret.get("validate_applied_rule", 0):
|
||||
# if user changed the discount percentage then set user's discount percentage ?
|
||||
item.set("pricing_rules", ret.get("pricing_rules"))
|
||||
item.set("discount_percentage", ret.get("discount_percentage"))
|
||||
item.set("discount_amount", ret.get("discount_amount"))
|
||||
if ret.get("pricing_rule_for") == "Rate":
|
||||
item.set("price_list_rate", ret.get("price_list_rate"))
|
||||
|
||||
if item.get("price_list_rate"):
|
||||
item.rate = flt(item.price_list_rate *
|
||||
(1.0 - (flt(item.discount_percentage) / 100.0)), item.precision("rate"))
|
||||
|
||||
if item.get('discount_amount'):
|
||||
item.rate = item.price_list_rate - item.discount_amount
|
||||
if ret.get("pricing_rules"):
|
||||
self.apply_pricing_rule_on_items(item, ret)
|
||||
|
||||
if self.doctype == "Purchase Invoice":
|
||||
self.set_expense_account(for_validate)
|
||||
|
||||
def apply_pricing_rule_on_items(self, item, pricing_rule_args):
|
||||
if not pricing_rule_args.get("validate_applied_rule", 0):
|
||||
# if user changed the discount percentage then set user's discount percentage ?
|
||||
if pricing_rule_args.get("price_or_product_discount") == 'Price':
|
||||
item.set("pricing_rules", pricing_rule_args.get("pricing_rules"))
|
||||
item.set("discount_percentage", pricing_rule_args.get("discount_percentage"))
|
||||
item.set("discount_amount", pricing_rule_args.get("discount_amount"))
|
||||
if pricing_rule_args.get("pricing_rule_for") == "Rate":
|
||||
item.set("price_list_rate", pricing_rule_args.get("price_list_rate"))
|
||||
|
||||
if item.get("price_list_rate"):
|
||||
item.rate = flt(item.price_list_rate *
|
||||
(1.0 - (flt(item.discount_percentage) / 100.0)), item.precision("rate"))
|
||||
|
||||
if item.get('discount_amount'):
|
||||
item.rate = item.price_list_rate - item.discount_amount
|
||||
|
||||
elif pricing_rule_args.get('free_item'):
|
||||
apply_pricing_rule_for_free_items(self, pricing_rule_args)
|
||||
|
||||
elif pricing_rule_args.get("validate_applied_rule"):
|
||||
for pricing_rule in get_applied_pricing_rules(item):
|
||||
pricing_rule_doc = frappe.get_cached_doc("Pricing Rule", pricing_rule)
|
||||
for field in ['discount_percentage', 'discount_amount', 'rate']:
|
||||
if item.get(field) < pricing_rule_doc.get(field):
|
||||
title = get_link_to_form("Pricing Rule", pricing_rule)
|
||||
|
||||
frappe.msgprint(_("Row {0}: user has not applied the rule {1} on the item {2}")
|
||||
.format(item.idx, frappe.bold(title), frappe.bold(item.item_code)))
|
||||
|
||||
def set_taxes(self):
|
||||
if not self.meta.get_field("taxes"):
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user