diff --git a/addons/point_of_sale/models/stock_picking.py b/addons/point_of_sale/models/stock_picking.py index c64bf4d5f29..74689a2a546 100644 --- a/addons/point_of_sale/models/stock_picking.py +++ b/addons/point_of_sale/models/stock_picking.py @@ -120,30 +120,39 @@ class StockPicking(models.Model): def _action_done(self): res = super()._action_done() - if self.pos_order_id.shipping_date and not self.pos_order_id.to_invoice: - order_cost = sum(line.total_cost for line in self.pos_order_id.lines) - move_vals = { - 'journal_id': self.pos_order_id.sale_journal.id, - 'date': self.pos_order_id.date_order, - 'ref': self.pos_order_id.name, - 'line_ids': [ - (0, 0, { - 'name': self.pos_order_id.name, - 'account_id': self.product_id.categ_id.property_account_income_categ_id.id, - 'debit': order_cost, - 'credit': 0.0, - }), - (0, 0, { - 'name': self.pos_order_id.name, - 'account_id': self.product_id.categ_id.property_account_expense_categ_id.id, - 'debit': 0.0, - 'credit': order_cost, + for rec in self: + if rec.pos_order_id.shipping_date and not rec.pos_order_id.to_invoice: + cost_per_account = defaultdict(lambda: 0.0) + for line in rec.pos_order_id.lines: + if line.product_id.type != 'product': + continue + out = line.product_id.categ_id.property_stock_account_output_categ_id + exp = line.product_id._get_product_accounts()['expense'] + cost_per_account[(out, exp)] += line.total_cost + move_vals = [] + for (out_acc, exp_acc), cost in cost_per_account.items(): + move_vals.append({ + 'journal_id': rec.pos_order_id.sale_journal.id, + 'date': rec.pos_order_id.date_order, + 'ref': rec.pos_order_id.name, + 'line_ids': [ + (0, 0, { + 'name': rec.pos_order_id.name, + 'account_id': exp_acc.id, + 'debit': cost, + 'credit': 0.0, + }), + (0, 0, { + 'name': rec.pos_order_id.name, + 'account_id': out_acc.id, + 'debit': 0.0, + 'credit': cost, + }), + ], }) - ] - } - move = self.env['account.move'].create(move_vals) - self.pos_order_id.write({'account_move': move.id}) - move.action_post() + move = self.env['account.move'].create(move_vals) + rec.pos_order_id.write({'account_move': move.id}) + move.action_post() return res class StockPickingType(models.Model): diff --git a/addons/point_of_sale/tests/test_anglo_saxon.py b/addons/point_of_sale/tests/test_anglo_saxon.py index 86ccc582f58..5b9e2a759be 100644 --- a/addons/point_of_sale/tests/test_anglo_saxon.py +++ b/addons/point_of_sale/tests/test_anglo_saxon.py @@ -283,13 +283,20 @@ class TestAngloSaxonFlow(TestAngloSaxonCommon): aml_output = aml.filtered(lambda l: l.account_id.id == account_output.id) aml_expense = aml.filtered(lambda l: l.account_id.id == expense_account.id) + self.assertEqual(len(aml_output), 3, "There should be 3 output account move lines") + # 2 moves in POS journal (Pos order + manual entry at delivery) + self.assertEqual(aml_output[:2].move_id.journal_id, self.pos_config.journal_id) + # 1 move in stock journal (delivery from stock layers) + self.assertEqual(aml_output[2].move_id.journal_id, self.category.property_stock_journal) #Check the lines created after the picking validation - self.assertEqual(aml_output[1].debit, self.product.standard_price, "Cost of Good Sold entry missing or mismatching") - self.assertEqual(aml_output[1].credit, 0.0, "Cost of Good Sold entry missing or mismatching") - self.assertEqual(aml_expense[0].credit, self.product.standard_price, "Cost of Good Sold entry missing or mismatching") - self.assertEqual(aml_expense[0].debit, 0.0, "Cost of Good Sold entry missing or mismatching") - #Check the lines created by the PoS session + self.assertEqual(aml_output[0].credit, self.product.standard_price, "Cost of Good Sold entry missing or mismatching") self.assertEqual(aml_output[0].debit, 0.0, "Cost of Good Sold entry missing or mismatching") - self.assertEqual(aml_output[0].credit, 0.0, "Cost of Good Sold entry missing or mismatching") + self.assertEqual(aml_output[2].debit, self.product.standard_price, "Cost of Good Sold entry missing or mismatching") + self.assertEqual(aml_output[2].credit, 0.0, "Cost of Good Sold entry missing or mismatching") + self.assertEqual(aml_expense[0].debit, self.product.standard_price, "Cost of Good Sold entry missing or mismatching") + self.assertEqual(aml_expense[0].credit, 0.0, "Cost of Good Sold entry missing or mismatching") + #Check the lines created by the PoS session + self.assertEqual(aml_output[1].debit, 0.0, "Cost of Good Sold entry missing or mismatching") + self.assertEqual(aml_output[1].credit, 0.0, "Cost of Good Sold entry missing or mismatching") self.assertEqual(aml_expense[1].credit, 0.0, "Cost of Good Sold entry missing or mismatching") self.assertEqual(aml_expense[1].debit, 0.0, "Cost of Good Sold entry missing or mismatching")