diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index 5c77af8f678fb23961528591ea72469b8c059e23..f9cdc112758756279ee69615125b90d6753dbf9c 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -714,7 +714,8 @@ class GrossProfitGenerator(object): def get_average_buying_rate(self, row, item_code): args = row - if item_code not in self.average_buying_rate: + key = (item_code, row.warehouse) + if key not in self.average_buying_rate: args.update( { "voucher_type": row.parenttype, @@ -728,9 +729,9 @@ class GrossProfitGenerator(object): args.update({"serial_and_batch_bundle": row.serial_and_batch_bundle}) average_buying_rate = get_incoming_rate(args) - self.average_buying_rate[item_code] = flt(average_buying_rate) + self.average_buying_rate[key] = flt(average_buying_rate) - return self.average_buying_rate[item_code] + return self.average_buying_rate[key] def get_last_purchase_rate(self, item_code, row): purchase_invoice = frappe.qb.DocType("Purchase Invoice") diff --git a/erpnext/accounts/report/gross_profit/test_gross_profit.py b/erpnext/accounts/report/gross_profit/test_gross_profit.py index 1484212c70d902388bb4c3af10c87a98061b374c..65b489d984b7d8abbed5674623cc7e120294340b 100644 --- a/erpnext/accounts/report/gross_profit/test_gross_profit.py +++ b/erpnext/accounts/report/gross_profit/test_gross_profit.py @@ -86,7 +86,12 @@ class TestGrossProfit(FrappeTestCase): self.customer = customer.name def create_sales_invoice( - self, qty=1, rate=100, posting_date=nowdate(), do_not_save=False, do_not_submit=False # noqa + self, + qty=1, + rate=100, + posting_date=nowdate(), + do_not_save=False, + do_not_submit=False, # noqa ): """ Helper function to populate default values in sales invoice @@ -557,4 +562,51 @@ class TestGrossProfit(FrappeTestCase): "gross_profit_%": 12.5, } gp_entry = [x for x in data if x.parent_invoice == sinv.name] - self.assertDictContainsSubset(expected_entry, gp_entry[0]) + self.assertEqual(gp_entry[0], gp_entry[0] | expected_entry) + + def test_valuation_rate_without_previous_sle(self): + """ + Test Valuation rate calculation when stock ledger is empty and invoices are against different warehouses + """ + stock_settings = frappe.get_doc("Stock Settings") + stock_settings.valuation_method = "FIFO" + stock_settings.save() + + item = create_item( + item_code="_Test Wirebound Notebook", + is_stock_item=1, + ) + item.allow_negative_stock = True + item.save() + self.item = item.item_code + + item.reload() + item.valuation_rate = 1900 + item.save() + sinv1 = self.create_sales_invoice(qty=1, rate=2000, posting_date=nowdate(), do_not_submit=True) + sinv1.update_stock = 1 + sinv1.set_warehouse = self.warehouse + sinv1.items[0].warehouse = self.warehouse + sinv1.save().submit() + + item.reload() + item.valuation_rate = 1800 + item.save() + sinv2 = self.create_sales_invoice(qty=1, rate=2000, posting_date=nowdate(), do_not_submit=True) + sinv2.update_stock = 1 + sinv2.set_warehouse = self.finished_warehouse + sinv2.items[0].warehouse = self.finished_warehouse + sinv2.save().submit() + + filters = frappe._dict( + company=self.company, from_date=nowdate(), to_date=nowdate(), group_by="Invoice" + ) + columns, data = execute(filters=filters) + + item_from_sinv1 = [x for x in data if x.parent_invoice == sinv1.name] + self.assertEqual(len(item_from_sinv1), 1) + self.assertEqual(1900, item_from_sinv1[0].valuation_rate) + + item_from_sinv2 = [x for x in data if x.parent_invoice == sinv2.name] + self.assertEqual(len(item_from_sinv2), 1) + self.assertEqual(1800, item_from_sinv2[0].valuation_rate)