Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(minor): improve sales invoice submission (backport #27916) #28102

Merged
3 changes: 3 additions & 0 deletions erpnext/accounts/doctype/pricing_rule/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def get_pricing_rules(args, doc=None):
pricing_rules = []
values = {}

if not frappe.db.exists('Pricing Rule', {'disable': 0, args.transaction_type: 1}):
return

for apply_on in ['Item Code', 'Item Group', 'Brand']:
pricing_rules.extend(_get_pricing_rules(apply_on, args, values))
if pricing_rules and not apply_multiple_pricing_rules(pricing_rules):
Expand Down
33 changes: 26 additions & 7 deletions erpnext/accounts/doctype/sales_invoice/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2026,22 +2026,23 @@ def update_multi_mode_option(doc, pos_profile):
def append_payment(payment_mode):
payment = doc.append('payments', {})
payment.default = payment_mode.default
payment.mode_of_payment = payment_mode.parent
payment.mode_of_payment = payment_mode.mop
payment.account = payment_mode.default_account
payment.type = payment_mode.type

doc.set('payments', [])
invalid_modes = []
for pos_payment_method in pos_profile.get('payments'):
pos_payment_method = pos_payment_method.as_dict()
mode_of_payments = [d.mode_of_payment for d in pos_profile.get('payments')]
mode_of_payments_info = get_mode_of_payments_info(mode_of_payments, doc.company)

payment_mode = get_mode_of_payment_info(pos_payment_method.mode_of_payment, doc.company)
for row in pos_profile.get('payments'):
payment_mode = mode_of_payments_info.get(row.mode_of_payment)
if not payment_mode:
invalid_modes.append(get_link_to_form("Mode of Payment", pos_payment_method.mode_of_payment))
invalid_modes.append(get_link_to_form("Mode of Payment", row.mode_of_payment))
continue

payment_mode[0].default = pos_payment_method.default
append_payment(payment_mode[0])
payment_mode.default = row.default
append_payment(payment_mode)

if invalid_modes:
if invalid_modes == 1:
Expand All @@ -2057,6 +2058,24 @@ def get_all_mode_of_payments(doc):
where mpa.parent = mp.name and mpa.company = %(company)s and mp.enabled = 1""",
{'company': doc.company}, as_dict=1)

def get_mode_of_payments_info(mode_of_payments, company):
data = frappe.db.sql(
"""
select
mpa.default_account, mpa.parent as mop, mp.type as type
from
`tabMode of Payment Account` mpa,`tabMode of Payment` mp
where
mpa.parent = mp.name and
mpa.company = %s and
mp.enabled = 1 and
mp.name in (%s)
group by
mp.name
""", (company, mode_of_payments), as_dict=1)

return {row.get('mop'): row for row in data}

def get_mode_of_payment_info(mode_of_payment, company):
return frappe.db.sql("""
select mpa.default_account, mpa.parent, mp.type as type
Expand Down
38 changes: 22 additions & 16 deletions erpnext/stock/get_item_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ def get_item_details(args, doc=None, for_validate=False, overwrite_warehouse=Tru
out.update(get_bin_details(args.item_code, args.get("from_warehouse")))

elif out.get("warehouse"):
out.update(get_bin_details(args.item_code, out.warehouse, args.company))
if doc and doc.get('doctype') == 'Purchase Order':
# calculate company_total_stock only for po
bin_details = get_bin_details(args.item_code, out.warehouse, args.company)
else:
bin_details = get_bin_details(args.item_code, out.warehouse)

out.update(bin_details)

# update args with out, if key or value not exists
for key, value in iteritems(out):
Expand Down Expand Up @@ -485,8 +491,9 @@ def get_item_tax_template(args, item, out):
"item_tax_template": None
}
"""
item_tax_template = args.get("item_tax_template")
item_tax_template = _get_item_tax_template(args, item.taxes, out)
item_tax_template = None
if item.taxes:
item_tax_template = _get_item_tax_template(args, item.taxes, out)

if not item_tax_template:
item_group = item.item_group
Expand All @@ -502,17 +509,17 @@ def _get_item_tax_template(args, taxes, out=None, for_validate=False):
taxes_with_no_validity = []

for tax in taxes:
tax_company = frappe.get_value("Item Tax Template", tax.item_tax_template, 'company')
if (tax.valid_from or tax.maximum_net_rate) and tax_company == args['company']:
# In purchase Invoice first preference will be given to supplier invoice date
# if supplier date is not present then posting date
validation_date = args.get('transaction_date') or args.get('bill_date') or args.get('posting_date')

if getdate(tax.valid_from) <= getdate(validation_date) \
and is_within_valid_range(args, tax):
taxes_with_validity.append(tax)
else:
if tax_company == args['company']:
tax_company = frappe.get_cached_value("Item Tax Template", tax.item_tax_template, 'company')
if tax_company == args['company']:
if (tax.valid_from or tax.maximum_net_rate):
# In purchase Invoice first preference will be given to supplier invoice date
# if supplier date is not present then posting date
validation_date = args.get('transaction_date') or args.get('bill_date') or args.get('posting_date')

if getdate(tax.valid_from) <= getdate(validation_date) \
and is_within_valid_range(args, tax):
taxes_with_validity.append(tax)
else:
taxes_with_no_validity.append(tax)

if taxes_with_validity:
Expand Down Expand Up @@ -890,8 +897,7 @@ def get_pos_profile_item_details(company, args, pos_profile=None, update_data=Fa
res[fieldname] = pos_profile.get(fieldname)

if res.get("warehouse"):
res.actual_qty = get_bin_details(args.item_code,
res.warehouse).get("actual_qty")
res.actual_qty = get_bin_details(args.item_code, res.warehouse).get("actual_qty")

return res

Expand Down
3 changes: 1 addition & 2 deletions erpnext/stock/stock_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,11 @@ def set_as_cancel(voucher_type, voucher_no):
(now(), frappe.session.user, voucher_type, voucher_no))

def make_entry(args, allow_negative_stock=False, via_landed_cost_voucher=False):
args.update({"doctype": "Stock Ledger Entry"})
args["doctype"] = "Stock Ledger Entry"
sle = frappe.get_doc(args)
sle.flags.ignore_permissions = 1
sle.allow_negative_stock=allow_negative_stock
sle.via_landed_cost_voucher = via_landed_cost_voucher
sle.insert()
sle.submit()
return sle

Expand Down