Skip to content

Commit

Permalink
chore: Warn users about multiple warehouse accounts
Browse files Browse the repository at this point in the history
- Get distinct accounts that warehouse value has been booked against
- If same account as the one being set, ignore
- If not same account or multiple accounts: warn user that it makes it harder to track mismatches
  • Loading branch information
marination committed Apr 26, 2022
1 parent 8d20da9 commit 44331f4
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions erpnext/stock/doctype/warehouse/warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def onload(self):
self.set_onload("account", account)
load_address_and_contact(self)

def validate(self):
self.warn_about_multiple_warehouse_account()

def on_update(self):
self.update_nsm_model()

Expand Down Expand Up @@ -70,6 +73,53 @@ def on_trash(self):
self.update_nsm_model()
self.unlink_from_items()

def warn_about_multiple_warehouse_account(self):
"If Warehouse value is split across multiple accounts, warn."

def get_accounts_where_value_is_booked(name):
sle = frappe.qb.DocType("Stock Ledger Entry")
gle = frappe.qb.DocType("GL Entry")
ac = frappe.qb.DocType("Account")

return (
frappe.qb.from_(sle)
.join(gle)
.on(sle.voucher_no == gle.voucher_no)
.join(ac)
.on(ac.name == gle.account)
.select(gle.account)
.distinct()
.where((sle.warehouse == name) & (ac.root_type == "Asset"))
.orderby(sle.creation)
.run(as_dict=True)
)

if self.is_new():
return

old_wh_account = frappe.db.get_value("Warehouse", self.name, "account")

# WH account is being changed or set get all accounts against which wh value is booked
if self.account != old_wh_account:
accounts = get_accounts_where_value_is_booked(self.name)
accounts = [d.account for d in accounts]

if not accounts or (len(accounts) == 1 and self.account in accounts):
# if same singular account has stock value booked ignore
return

warning = _("Warehouse's Stock Value has already been booked in the following accounts:")
account_str = "<br>" + ", ".join(frappe.bold(ac) for ac in accounts)
reason = "<br><br>" + _(
"Booking stock value across multiple accounts will make it harder to track stock and account value."
)

frappe.msgprint(
warning + account_str + reason,
title=_("Multiple Warehouse Accounts"),
indicator="orange",
)

def check_if_sle_exists(self):
return frappe.db.exists("Stock Ledger Entry", {"warehouse": self.name})

Expand Down

0 comments on commit 44331f4

Please sign in to comment.