From 01d3142b9dc9b0b8fc236a08629ef2e881be2875 Mon Sep 17 00:00:00 2001 From: Magno Costa Date: Fri, 8 Sep 2023 18:03:33 -0300 Subject: [PATCH] [REF+IMP] stock_picking_invoicing: Tests, create methods to avoid duplicate code and included tests for the cases when Price Unit are Informed by User, Sellers and Pricelist. --- .../tests/test_picking_invoicing.py | 379 ++++++------------ 1 file changed, 133 insertions(+), 246 deletions(-) diff --git a/stock_picking_invoicing/tests/test_picking_invoicing.py b/stock_picking_invoicing/tests/test_picking_invoicing.py index a9bf29d49f1c..724b4c275cb1 100644 --- a/stock_picking_invoicing/tests/test_picking_invoicing.py +++ b/stock_picking_invoicing/tests/test_picking_invoicing.py @@ -91,6 +91,47 @@ def setUpClass(cls): } ) + # Test PriceList and Sellers Price + product_price_test = cls.env.ref("product.product_product_6") + cls.price_list = cls.env.ref("product.list0") + cls.price_list.item_ids.create( + { + "compute_price": "fixed", + "fixed_price": 1234.0, + "applied_on": "0_product_variant", + "product_id": product_price_test.id, + } + ) + # Data has duplicate line with 2 Prices for same Partner, + # just remove one for the test. + seller_to_delete = product_price_test.seller_ids.filtered( + lambda l: l.price == 785.0 + and l.partner_id == cls.env.ref("base.res_partner_4") + ) + seller_to_delete.unlink() + + def picking_move_state(self, picking): + picking.action_confirm() + # Check product availability + picking.action_assign() + # Force product availability + for move in picking.move_ids_without_package: + move.quantity_done = move.product_uom_qty + picking.button_validate() + + def wizard_create_invoice(self, pickings): + wizard_obj = self.invoice_wizard.with_context( + active_ids=pickings.ids, + active_model=pickings._name, + ) + fields_list = wizard_obj.fields_get().keys() + wizard_values = wizard_obj.default_get(fields_list) + # One invoice per partner but group products + wizard_values.update({"group": "partner_product"}) + wizard = wizard_obj.create(wizard_values) + wizard.onchange_group() + wizard.action_generate() + def test_0_picking_out_invoicing(self): # setting Agrolait type to default, because it's 'contact' in demo data nb_invoice_before = self.invoice_model.search_count([]) @@ -115,25 +156,10 @@ def test_0_picking_out_invoicing(self): } new_move = self.move_model.create(move_vals) new_move._onchange_product_id() - picking.set_to_be_invoiced() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + picking.set_as_2binvoiced() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, - active_model=picking._name, - active_id=picking.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) self.assertEqual(picking.invoice_state, "invoiced") @@ -151,6 +177,10 @@ def test_0_picking_out_invoicing(self): assert inv_line.account_id, "Error to map Account in Invoice Line." assert inv_line.tax_ids, "Error to map Sale Tax in Invoice Line." assert inv_line.product_uom_id, "Error to map Product UOM in Invoice Line." + assert inv_line.price_unit, "Error in Price Unit" + assert ( + inv_line.move_line_ids + ), "Error, there no relation between Invoice Line and Stock Move Line." for mv_line in inv_line.move_line_ids: self.assertEqual( mv_line.id, @@ -180,14 +210,12 @@ def test_1_picking_out_invoicing(self): } new_move = self.move_model.create(move_vals) new_move._onchange_product_id() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + picking._onchange_invoice_state() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") + + # Test Wizard Error + # self.wizard_create_invoice(picking) wizard_obj = self.invoice_wizard.with_context( active_ids=picking.ids, active_model=picking._name, @@ -226,26 +254,19 @@ def test_2_picking_out_invoicing(self): } new_move = self.move_model.create(move_vals) new_move._onchange_product_id() - picking.set_to_be_invoiced() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + picking.set_as_2binvoiced() + + # Test Set Not Invoice + picking.set_as_not_billable() + self.assertEqual(picking.invoice_state, "none") + # Test Set Invoiced + picking.set_as_invoiced() + self.assertEqual(picking.invoice_state, "invoiced") + + picking.set_as_2binvoiced() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, - active_model=picking._name, - active_id=picking.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard_values.update({"group": "partner"}) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) # invoice = picking.invoice_ids[0] @@ -290,25 +311,10 @@ def test_3_picking_out_invoicing(self): } new_move = self.move_model.create(move_vals) new_move._onchange_product_id() - picking.set_to_be_invoiced() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + picking.set_as_2binvoiced() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, - active_model=picking._name, - active_id=picking.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) self.assertEqual(picking.invoice_state, "invoiced") @@ -356,13 +362,17 @@ def test_4_picking_out_invoicing_backorder(self): } new_move = self.move_model.create(move_vals) new_move._onchange_product_id() - picking.set_to_be_invoiced() + picking.set_as_2binvoiced() + # Test BackOrder need to open Wizard + # self.picking_move_state(picking) picking.action_confirm() # Check product availability picking.action_assign() # Force product availability for move in picking.move_ids_without_package: move.quantity_done = move.product_uom_qty / 2.0 + # Test Price Unit informed by User + move.price_unit = 345.0 backorder_action = picking.button_validate() Form( @@ -377,16 +387,7 @@ def test_4_picking_out_invoicing_backorder(self): self.assertEqual(backorder.invoice_state, "2binvoiced") self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, - active_model=picking._name, - active_id=picking.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) self.assertEqual(picking.invoice_state, "invoiced") @@ -404,6 +405,10 @@ def test_4_picking_out_invoicing_backorder(self): "Error to link stock.move with invoice.line.", ) self.assertTrue(inv_line.tax_ids, "Error to map Sale Tax in invoice.line.") + # Test Price Unit informed by user + self.assertEqual( + inv_line.price_unit, 345.0, "Error in Price Unit informed by User." + ) def test_picking_cancel(self): """ @@ -432,26 +437,10 @@ def test_picking_cancel(self): } new_move = self.move_model.create(move_vals) new_move._onchange_product_id() - picking.set_to_be_invoiced() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + picking.set_as_2binvoiced() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, - active_model=picking._name, - active_id=picking.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard_values.update({"group": "partner"}) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) self.assertEqual(picking.invoice_state, "invoiced") @@ -491,26 +480,10 @@ def test_picking_invoice_refund(self): } new_move = self.move_model.create(move_vals) new_move._onchange_product_id() - picking.set_to_be_invoiced() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + picking.set_as_2binvoiced() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, - active_model=picking._name, - active_id=picking.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard_values.update({"group": "partner"}) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) self.assertEqual(picking.invoice_state, "invoiced") @@ -562,27 +535,10 @@ def test_picking_invoicing_by_product1(self): new_move2 = self.move_model.create(move_vals2) new_move._onchange_product_id() new_move2._onchange_product_id() - picking.set_to_be_invoiced() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + picking.set_as_2binvoiced() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, - active_model=picking._name, - active_id=picking.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - # One invoice per partner but group products - wizard_values.update({"group": "partner_product"}) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) self.assertEqual(picking.invoice_state, "invoiced") @@ -637,36 +593,14 @@ def test_picking_invoicing_by_product2(self): new_move2 = self.move_model.create(move_vals2) new_move._onchange_product_id() new_move2._onchange_product_id() - picking.set_to_be_invoiced() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() - picking2.set_to_be_invoiced() - picking2.action_confirm() - # Check product availability - picking2.action_assign() - # Force product availability - for move in picking2.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking2.button_validate() + picking.set_as_2binvoiced() + self.picking_move_state(picking) + picking2.set_as_2binvoiced() + self.picking_move_state(picking2) self.assertEqual(picking.state, "done") self.assertEqual(picking2.state, "done") pickings = picking | picking2 - wizard_obj = self.invoice_wizard.with_context( - active_ids=pickings.ids, - active_model=pickings._name, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - # One invoice per partner but group products - wizard_values.update({"group": "partner_product"}) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(pickings) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) self.assertEqual(len(invoice), 1) @@ -732,36 +666,16 @@ def test_picking_invoicing_by_product3(self): new_move2 = self.move_model.create(move_vals2) new_move._onchange_product_id() new_move2._onchange_product_id() - picking.set_to_be_invoiced() - picking.action_confirm() - # Check product availability - picking.action_assign() - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() - picking2.set_to_be_invoiced() - picking2.action_confirm() - # Check product availability - picking2.action_assign() - # Force product availability - for move in picking2.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking2.button_validate() + picking.set_as_2binvoiced() + self.picking_move_state(picking) + + picking2.set_as_2binvoiced() + self.picking_move_state(picking2) self.assertEqual(picking.state, "done") self.assertEqual(picking2.state, "done") pickings = picking | picking2 - wizard_obj = self.invoice_wizard.with_context( - active_ids=pickings.ids, - active_model=pickings._name, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - # One invoice per partner but group products - wizard_values.update({"group": "partner_product"}) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(pickings) + domain = [("picking_ids", "in", [picking.id, picking2.id])] invoices = self.invoice_model.search(domain) self.assertEqual(len(invoices), 2) @@ -797,21 +711,9 @@ def test_return_customer_picking(self): Test Return Customer Picking and Invoice created. """ picking = self.env.ref("stock_picking_invoicing.stock_picking_invoicing_2") - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, - active_model=picking._name, - active_id=picking.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) # Confirm Invoice @@ -841,26 +743,9 @@ def test_return_customer_picking(self): for line in picking_devolution.move_ids: self.assertEqual(line.invoice_state, "2binvoiced") - picking_devolution.action_confirm() - picking_devolution.action_assign() - # Force product availability - for move in picking_devolution.move_ids_without_package: - move.quantity_done = move.product_uom_qty - # Test case where the user inform the field, value - # infomed should has preferency over the PriceList or Sellers - move.price_unit = 11.1 - picking_devolution.button_validate() + self.picking_move_state(picking_devolution) self.assertEqual(picking_devolution.state, "done", "Change state fail.") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking_devolution.ids, - active_model=picking_devolution._name, - active_id=picking_devolution.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking_devolution) domain = [("picking_ids", "=", picking_devolution.id)] invoice_devolution = self.invoice_model.search(domain) # Confirm Return Invoice @@ -880,21 +765,19 @@ def test_return_supplier_picking(self): Test Return Supplier Picking and Invoice created. """ picking = self.env.ref("stock_picking_invoicing.stock_picking_invoicing_7") - # Force product availability - for move in picking.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking.button_validate() + self.picking_move_state(picking) self.assertEqual(picking.state, "done") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking.ids, active_model=picking._name, active_id=picking.id - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + self.wizard_create_invoice(picking) domain = [("picking_ids", "=", picking.id)] invoice = self.invoice_model.search(domain) + for line in invoice.invoice_line_ids: + for seller in line.product_id.seller_ids: + if seller.partner_id == invoice.partner_id: + self.assertEqual( + seller.price, + line.price_unit, + "Product Price in invoice line should be the same of Seller Price.", + ) # Confirm Invoice invoice.action_post() self.assertEqual(invoice.state, "posted", "Invoice should be in state Posted") @@ -922,23 +805,10 @@ def test_return_supplier_picking(self): for line in picking_devolution.move_ids: self.assertEqual(line.invoice_state, "2binvoiced") - picking_devolution.action_confirm() - picking_devolution.action_assign() - # Force product availability - for move in picking_devolution.move_ids_without_package: - move.quantity_done = move.product_uom_qty - picking_devolution.button_validate() + self.picking_move_state(picking_devolution) self.assertEqual(picking_devolution.state, "done", "Change state fail.") - wizard_obj = self.invoice_wizard.with_context( - active_ids=picking_devolution.ids, - active_model=picking_devolution._name, - active_id=picking_devolution.id, - ) - fields_list = wizard_obj.fields_get().keys() - wizard_values = wizard_obj.default_get(fields_list) - wizard = wizard_obj.create(wizard_values) - wizard.onchange_group() - wizard.action_generate() + + self.wizard_create_invoice(picking_devolution) domain = [("picking_ids", "=", picking_devolution.id)] invoice_devolution = self.invoice_model.search(domain) # Confirm Return Invoice @@ -952,3 +822,20 @@ def test_return_supplier_picking(self): "in_refund", "Invoice Type should be In Refund", ) + + def test_get_price_from_pricelist(self): + """Test get Price from PriceList.""" + picking = self.env.ref("stock_picking_invoicing.stock_picking_invoicing_1") + self.picking_move_state(picking) + self.assertEqual(picking.state, "done") + self.wizard_create_invoice(picking) + domain = [("picking_ids", "=", picking.id)] + invoice = self.invoice_model.search(domain) + self.assertEqual(picking.invoice_state, "invoiced") + for inv_line in invoice.invoice_line_ids: + if inv_line.product_id == self.env.ref("product.product_product_6"): + self.assertEqual( + inv_line.price_unit, + 1234.0, + "Error to get sale Price from Price List.", + )