@@ -19,12 +19,23 @@ def allocate(self, line: OrderLine) -> str:
1919 b for b in sorted (self .batches ) if b .can_allocate (line )
2020 )
2121 batch .allocate (line )
22+ self .events .append (events .Allocated (
23+ line .orderid , line .sku , line .qty , batch .reference
24+ ))
2225 self .version_number += 1
2326 return batch .reference
2427 except StopIteration :
2528 self .events .append (events .OutOfStock (line .sku ))
2629 return None
2730
31+ def change_batch_quantity (self , ref : str , qty : int ):
32+ batch = next (b for b in self .batches if b .reference == ref )
33+ batch ._purchased_quantity = qty
34+ while batch .available_quantity < 0 :
35+ line = batch .deallocate_one ()
36+ self .events .append (
37+ events .Deallocated (line .orderid , line .sku , line .qty )
38+ )
2839
2940@dataclass (unsafe_hash = True )
3041class OrderLine :
@@ -65,9 +76,8 @@ def allocate(self, line: OrderLine):
6576 if self .can_allocate (line ):
6677 self ._allocations .add (line )
6778
68- def deallocate (self , line : OrderLine ):
69- if line in self ._allocations :
70- self ._allocations .remove (line )
79+ def deallocate_one (self ) -> OrderLine :
80+ return self ._allocations .pop ()
7181
7282 @property
7383 def allocated_quantity (self ) -> int :
0 commit comments