diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index bc44bc80f4..53eb73191d 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -223,7 +223,7 @@ class PurchaseInvoice(BuyingController): self.validate_item_code() self.validate_warehouse() if auto_accounting_for_stock: - warehouse_account = get_warehouse_account_map() + warehouse_account = get_warehouse_account_map(self.company) for item in self.get("items"): # in case of auto inventory accounting, @@ -374,7 +374,8 @@ class PurchaseInvoice(BuyingController): if repost_future_gle and cint(self.update_stock) and self.auto_accounting_for_stock: from erpnext.controllers.stock_controller import update_gl_entries_after items, warehouses = self.get_items_and_warehouses() - update_gl_entries_after(self.posting_date, self.posting_time, warehouses, items) + update_gl_entries_after(self.posting_date, self.posting_time, + warehouses, items, company = self.company) elif self.docstatus == 2 and cint(self.update_stock) and self.auto_accounting_for_stock: delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name) @@ -433,7 +434,7 @@ class PurchaseInvoice(BuyingController): stock_items = self.get_stock_items() expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation") if self.update_stock and self.auto_accounting_for_stock: - warehouse_account = get_warehouse_account_map() + warehouse_account = get_warehouse_account_map(self.company) voucher_wise_stock_value = {} if self.update_stock: diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 9539b0ac3a..61e1224596 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -695,7 +695,8 @@ class SalesInvoice(SellingController): if repost_future_gle and cint(self.update_stock) \ and cint(auto_accounting_for_stock): items, warehouses = self.get_items_and_warehouses() - update_gl_entries_after(self.posting_date, self.posting_time, warehouses, items) + update_gl_entries_after(self.posting_date, self.posting_time, + warehouses, items, company = self.company) elif self.docstatus == 2 and cint(self.update_stock) \ and cint(auto_accounting_for_stock): from erpnext.accounts.general_ledger import delete_gl_entries diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index d4e1840eb9..2de5104183 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -544,14 +544,14 @@ def fix_total_debit_credit(): (dr_or_cr, dr_or_cr, '%s', '%s', '%s', dr_or_cr), (d.diff, d.voucher_type, d.voucher_no)) -def get_stock_and_account_difference(account_list=None, posting_date=None): +def get_stock_and_account_difference(account_list=None, posting_date=None, company=None): from erpnext.stock.utils import get_stock_value_on from erpnext.stock import get_warehouse_account_map if not posting_date: posting_date = nowdate() difference = {} - warehouse_account = get_warehouse_account_map() + warehouse_account = get_warehouse_account_map(company) for warehouse, account_data in iteritems(warehouse_account): if account_data.get('account') in account_list: diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index 0a3cd3499b..d1ffd7db66 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -26,7 +26,7 @@ class StockController(AccountsController): delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name) if cint(erpnext.is_perpetual_inventory_enabled(self.company)): - warehouse_account = get_warehouse_account_map() + warehouse_account = get_warehouse_account_map(self.company) if self.docstatus==1: if not gl_entries: @@ -36,7 +36,7 @@ class StockController(AccountsController): if repost_future_gle: items, warehouses = self.get_items_and_warehouses() update_gl_entries_after(self.posting_date, self.posting_time, warehouses, items, - warehouse_account) + warehouse_account, company=self.company) elif self.doctype in ['Purchase Receipt', 'Purchase Invoice'] and self.docstatus == 1: gl_entries = [] gl_entries = self.get_asset_gl_entry(gl_entries) @@ -46,7 +46,7 @@ class StockController(AccountsController): default_cost_center=None): if not warehouse_account: - warehouse_account = get_warehouse_account_map() + warehouse_account = get_warehouse_account_map(self.company) sle_map = self.get_stock_ledger_details() voucher_details = self.get_voucher_details(default_expense_account, default_cost_center, sle_map) @@ -199,7 +199,8 @@ class StockController(AccountsController): def make_adjustment_entry(self, expected_gle, voucher_obj): from erpnext.accounts.utils import get_stock_and_account_difference account_list = [d.account for d in expected_gle] - acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date) + acc_diff = get_stock_and_account_difference(account_list, + expected_gle[0].posting_date, self.company) cost_center = self.get_company_default("cost_center") stock_adjustment_account = self.get_company_default("stock_adjustment_account") @@ -361,13 +362,13 @@ class StockController(AccountsController): frappe.get_doc("Blanket Order", blanket_order).update_ordered_qty() def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for_items=None, - warehouse_account=None): + warehouse_account=None, company=None): def _delete_gl_entries(voucher_type, voucher_no): frappe.db.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) if not warehouse_account: - warehouse_account = get_warehouse_account_map() + warehouse_account = get_warehouse_account_map(company) future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, for_warehouses, for_items) gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date) diff --git a/erpnext/patches/v7_0/repost_future_gle_for_purchase_invoice.py b/erpnext/patches/v7_0/repost_future_gle_for_purchase_invoice.py index 22f9db6b2b..9e21fb699b 100644 --- a/erpnext/patches/v7_0/repost_future_gle_for_purchase_invoice.py +++ b/erpnext/patches/v7_0/repost_future_gle_for_purchase_invoice.py @@ -10,14 +10,15 @@ from erpnext.controllers.stock_controller import update_gl_entries_after def execute(): company_list = frappe.db.sql_list("""Select name from tabCompany where enable_perpetual_inventory = 1""") frappe.reload_doc('accounts', 'doctype', 'sales_invoice') - - frappe.reload_doctype("Purchase Invoice") + + frappe.reload_doctype("Purchase Invoice") wh_account = get_warehouse_account_map() - + for pi in frappe.get_all("Purchase Invoice", fields=["name", "company"], filters={"docstatus": 1, "update_stock": 1}): if pi.company in company_list: pi_doc = frappe.get_doc("Purchase Invoice", pi.name) items, warehouses = pi_doc.get_items_and_warehouses() - update_gl_entries_after(pi_doc.posting_date, pi_doc.posting_time, warehouses, items, wh_account) - + update_gl_entries_after(pi_doc.posting_date, pi_doc.posting_time, + warehouses, items, wh_account, company = pi.company) + frappe.db.commit() \ No newline at end of file diff --git a/erpnext/stock/__init__.py b/erpnext/stock/__init__.py index ea3d1036d2..32a03e7373 100644 --- a/erpnext/stock/__init__.py +++ b/erpnext/stock/__init__.py @@ -8,16 +8,21 @@ install_docs = [ {"doctype":"Role", "role_name":"Stock User", "name":"Stock User"}, {"doctype":"Role", "role_name":"Quality Manager", "name":"Quality Manager"}, {"doctype":"Item Group", "item_group_name":"All Item Groups", "is_group": 1}, - {"doctype":"Item Group", "item_group_name":"Default", + {"doctype":"Item Group", "item_group_name":"Default", "parent_item_group":"All Item Groups", "is_group": 0}, ] -def get_warehouse_account_map(): +def get_warehouse_account_map(company=None): if not frappe.flags.warehouse_account_map or frappe.flags.in_test: warehouse_account = frappe._dict() + filters = {} + if company: + filters['company'] = company + for d in frappe.get_all('Warehouse', fields = ["name", "account", "parent_warehouse", "company"], + filters = filters, order_by="lft, rgt"): if not d.account: d.account = get_warehouse_account(d, warehouse_account) @@ -57,6 +62,6 @@ def get_warehouse_account(warehouse, warehouse_account=None): frappe.throw(_("Please set Account in Warehouse {0} or Default Inventory Account in Company {1}") .format(warehouse.name, warehouse.company)) return account - + def get_company_default_inventory_account(company): return frappe.get_cached_value('Company', company, 'default_inventory_account')