diff --git a/src/calculation_time_constrains/service.py b/src/calculation_time_constrains/service.py index 5859ce7..5071cc8 100644 --- a/src/calculation_time_constrains/service.py +++ b/src/calculation_time_constrains/service.py @@ -192,6 +192,21 @@ class SparePartsService: return projected_stock + async def reduce_stock(self, db_session, assetnum: str): + requirements_query = select(ScopeEquipmentPart).where( + ScopeEquipmentPart.assetnum == assetnum + ) + + requirements = await db_session.execute(requirements_query) + requirements = requirements.scalars().all() + + for requirement in requirements: + sparepart_id = requirement.sparepart_id + quantity_needed = requirement.required_stock + + if sparepart_id in self.spare_parts_db: + self.spare_parts_db[sparepart_id]["stock"] -= quantity_needed + async def check_spare_parts_availability( self, db_session: DbSession, @@ -221,7 +236,8 @@ class SparePartsService: if sparepart_id not in self.spare_parts_db: raise Exception(f"Spare part {sparepart_id} not found in database") - spare_part = self.spare_parts_db[sparepart_id]["data"] + spare_part = self.spare_parts_db[sparepart_id] + spare_part_data = spare_part["data"] available_stock = self.calculate_stock_at_date(sparepart_id, overhaul_date) if available_stock < quantity_needed: @@ -229,18 +245,14 @@ class SparePartsService: shortage = quantity_needed - available_stock procurement_cost = { "sparepart_id": str(sparepart_id), - "sparepart_name": spare_part.name, + "sparepart_name": spare_part_data.name, "quantity": shortage, - "cost_per_unit": spare_part.cost_per_stock, - "total_cost": shortage * spare_part.cost_per_stock, - "description": f"Insufficient projected stock for {spare_part.name} on {overhaul_date} (need: {quantity_needed}, projected: {available_stock})" + "cost_per_unit": spare_part_data.cost_per_stock, + "total_cost": shortage * spare_part_data.cost_per_stock, + "description": f"Insufficient projected stock for {spare_part_data.name} on {overhaul_date} (need: {quantity_needed}, projected: {available_stock})" } procurement_costs.append(procurement_cost) all_available = False - else: - spare_part.stock -= quantity_needed - - return all_available, procurement_costs class OverhaulCalculator: @@ -418,6 +430,8 @@ class OverhaulCalculator: max_interval=max_interval ) + #reduce sparepart stock + await self.spare_parts_service.reduce_stock(db_session, equipment.assetnum) # Aggregate costs corrective_costs = [r["corrective_cost"] for r in all_results]