diff --git a/modules/purchase_trade/service.py b/modules/purchase_trade/service.py index 6562ea7..c38a5b4 100644 --- a/modules/purchase_trade/service.py +++ b/modules/purchase_trade/service.py @@ -334,6 +334,16 @@ class ContractFactory: with Transaction().set_context( _purchase_trade_skip_quantity_consistency=True): lot.save() + logger.info( + "CREATE_CONTRACT_LOT_SAVED type=%s line=%s lot=%s qty=%s " + "source_lot=%s source_lqt=%s shipment=%s", + type_, + getattr(line, 'id', None), + getattr(lot, 'id', None), + getattr(lot, 'lot_quantity', None), + getattr(source.get('lot'), 'id', None), + getattr(source.get('lqt'), 'id', None), + source.get('shipment_origin')) vlot = source['lot'] shipment_origin = source.get('shipment_origin') @@ -342,6 +352,14 @@ class ContractFactory: if type_ == 'Purchase': if cls._attach_or_split_source_lot_qt(source, lot, type_): return + logger.info( + "CREATE_CONTRACT_LOT_FALLBACK type=%s new_lot=%s " + "source_lot=%s qty=%s shipment=%s", + type_, + getattr(lot, 'id', None), + getattr(vlot, 'id', None), + qt, + shipment_origin) if not lot.updateVirtualPart(qt, shipment_origin, vlot): lot.createVirtualPart(qt, shipment_origin, vlot) @@ -350,6 +368,14 @@ class ContractFactory: else: if cls._attach_or_split_source_lot_qt(source, lot, type_): return + logger.info( + "CREATE_CONTRACT_LOT_FALLBACK type=%s new_lot=%s " + "source_lot=%s qty=%s shipment=%s", + type_, + getattr(lot, 'id', None), + getattr(vlot, 'id', None), + qt, + shipment_origin) if not vlot.updateVirtualPart(qt, shipment_origin, lot): vlot.createVirtualPart(qt, shipment_origin, lot) @@ -367,12 +393,35 @@ class ContractFactory: if not source_lqt: source_lqt = cls._find_source_lot_qt(source, type_) if not source_lqt: + logger.info( + "CREATE_CONTRACT_ATTACH_SKIP no source_lqt type=%s " + "new_lot=%s source_lot=%s qty=%s shipment=%s", + type_, + getattr(new_virtual_lot, 'id', None), + getattr(source.get('lot'), 'id', None), + quantity, + source.get('shipment_origin')) return False remaining = cls._normalize_quantity(source_lqt.lot_quantity) - quantity if remaining < 0: + logger.info( + "CREATE_CONTRACT_ATTACH_SKIP insufficient source_lqt=%s " + "available=%s qty=%s", + getattr(source_lqt, 'id', None), + getattr(source_lqt, 'lot_quantity', None), + quantity) return False + logger.info( + "CREATE_CONTRACT_ATTACH type=%s source_lqt=%s new_lot=%s " + "source_lot=%s qty=%s remaining=%s", + type_, + getattr(source_lqt, 'id', None), + getattr(new_virtual_lot, 'id', None), + getattr(source.get('lot'), 'id', None), + quantity, + remaining) if remaining: matched_lqt = LotQt() matched_lqt.lot_p = ( @@ -423,10 +472,34 @@ class ContractFactory: cls._add_shipment_domain(domain, source.get('shipment_origin')) domain.append(('lot_quantity', '!=', 0)) lqts = LotQt.search(domain) + if not lqts and source.get('shipment_origin'): + domain = domain[:2] + [ + ('lot_quantity', '!=', 0), + ] + lqts = LotQt.search(domain) quantity = cls._normalize_quantity(source['quantity']) + for lqt in lqts: + if cls._normalize_quantity(lqt.lot_quantity) == quantity: + return lqt for lqt in lqts: if cls._normalize_quantity(lqt.lot_quantity) >= quantity: return lqt + logger.info( + "CREATE_CONTRACT_FIND_SOURCE_LQT_EMPTY type=%s lot=%s qty=%s " + "shipment=%s candidates=%s", + type_, + getattr(lot, 'id', None), + quantity, + source.get('shipment_origin'), + [ + ( + getattr(lqt, 'id', None), + getattr(lqt, 'lot_quantity', None), + getattr(getattr(lqt, 'lot_p', None), 'id', None), + getattr(getattr(lqt, 'lot_s', None), 'id', None), + ) + for lqt in lqts + ]) return None @staticmethod