From 6dbf1ba5fcaa5a32103ebb27f5c17d332efd6013 Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Wed, 13 Sep 2023 15:51:20 +0200 Subject: [PATCH] Get move cost price from lot when to location is a production #161279 --- stock.py | 13 +++ tests/scenario_production_lot_cost.rst | 132 +++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 tests/scenario_production_lot_cost.rst diff --git a/stock.py b/stock.py index 4464050..44e5baf 100644 --- a/stock.py +++ b/stock.py @@ -104,3 +104,16 @@ class Move(metaclass=PoolMeta): def __setup__(cls): super(Move, cls).__setup__() cls.lot.context['from_move'] = Eval('id') + + def get_cost_price(self, product_cost_price=None): + pool = Pool() + Lot = pool.get('stock.lot') + + cost_price = super().get_cost_price(product_cost_price=None) + + if self.lot and self.to_location.type == 'production': + with Transaction().set_context(date=self.effective_date): + lot = Lot(self.lot.id) # Need to reinstantiate to ensure the context is correct + if lot.cost_price is not None: + return lot.cost_price + return cost_price diff --git a/tests/scenario_production_lot_cost.rst b/tests/scenario_production_lot_cost.rst new file mode 100644 index 0000000..24111b3 --- /dev/null +++ b/tests/scenario_production_lot_cost.rst @@ -0,0 +1,132 @@ +==================================== +Stock Lot Number Production Scenario +==================================== + +Imports:: + + >>> from decimal import Decimal + + >>> from proteus import Model + >>> from trytond.tests.tools import activate_modules + >>> from trytond.modules.company.tests.tools import ( + ... create_company, get_company) + +Activate modules:: + + >>> config = activate_modules(['stock_lot_cost', 'production']) + + >>> ProductTemplate = Model.get('product.template') + >>> Production = Model.get('production') + >>> Production = Model.get('production') + >>> Sequence = Model.get('ir.sequence') + >>> SequenceType = Model.get('ir.sequence.type') + >>> UoM = Model.get('product.uom') + +Create company:: + + >>> _ = create_company() + >>> company = get_company() + +Create lot sequence:: + + >>> sequence_type, = SequenceType.find( + ... [('name', '=', "Stock Lot")], limit=1) + >>> sequence = Sequence(name="Lot", sequence_type=sequence_type) + >>> sequence.save() + +Create product:: + + >>> unit, = UoM.find([('name', '=', 'Unit')]) + + >>> template = ProductTemplate() + >>> template.name = "Product" + >>> template.default_uom = unit + >>> template.type = 'goods' + >>> template.producible = True + >>> template.list_price = Decimal('10.0000') + >>> template.lot_required = ['storage'] + >>> template.lot_sequence = sequence + >>> template.save() + >>> product, = template.products + +Get stock locations:: + + >>> Location = Model.get('stock.location') + >>> warehouse_loc, = Location.find([('code', '=', 'WH')]) + >>> supplier_loc, = Location.find([('code', '=', 'SUP')]) + >>> customer_loc, = Location.find([('code', '=', 'CUS')]) + >>> output_loc, = Location.find([('code', '=', 'OUT')]) + >>> storage_loc, = Location.find([('code', '=', 'STO')]) + +Create lot:: + + >>> Lot = Model.get('stock.lot') + >>> lot = Lot() + >>> lot.number = 'LOT' + >>> lot.product = product + >>> lot.save() + +Create supplier moves move:: + + >>> Move = Model.get('stock.move') + >>> move1_in = Move() + >>> move1_in.from_location = supplier_loc + >>> move1_in.to_location = storage_loc + >>> move1_in.product = product + >>> move1_in.company = company + >>> move1_in.lot = lot + >>> move1_in.quantity = 100 + >>> move1_in.unit_price = Decimal('1') + >>> move1_in.currency = company.currency + >>> move1_in.save() + >>> move1_in.click('do') + + >>> move2_in = Move() + >>> move2_in.from_location = supplier_loc + >>> move2_in.to_location = storage_loc + >>> move2_in.product = product + >>> move2_in.company = company + >>> move2_in.lot = lot + >>> move2_in.quantity = 100 + >>> move2_in.unit_price = Decimal('2') + >>> move2_in.currency = company.currency + >>> move2_in.save() + >>> move2_in.click('do') + +Check the lot costs:: + + >>> lot = Lot(lot.id) + >>> lot.cost_price + Decimal('1.5000') + >>> lot.total_cost + Decimal('300.0000') + +Make a production:: + + >>> production = Production() + >>> input = production.inputs.new() + >>> input.from_location = production.warehouse.storage_location + >>> input.to_location = production.location + >>> input.product = product + >>> input.quantity = 1 + >>> input.unit_price = Decimal(0) + >>> input.currency = production.company.currency + >>> output = production.outputs.new() + >>> output.from_location = production.location + >>> output.to_location = production.warehouse.storage_location + >>> output.product = product + >>> output.quantity = 1 + >>> output.unit_price = Decimal(0) + >>> output.currency = production.company.currency + >>> production.click('wait') + + >>> production.cost == Decimal('0.0000') + True + + >>> input, = production.inputs + >>> input.lot = lot + >>> input.save() + + >>> production = Production(production.id) + >>> production.cost == Decimal('1.5000') + True