diff --git a/internal/processor/purchase_order_processor.go b/internal/processor/purchase_order_processor.go index 7d87e90..169ffe9 100644 --- a/internal/processor/purchase_order_processor.go +++ b/internal/processor/purchase_order_processor.go @@ -107,7 +107,7 @@ func (p *PurchaseOrderProcessorImpl) CreatePurchaseOrder(ctx context.Context, or // Calculate total amount totalAmount := 0.0 for _, item := range req.Items { - totalAmount += item.Amount + totalAmount += calculatePurchaseOrderItemTotal(item.Quantity, item.Amount) } // Create purchase order entity @@ -277,7 +277,7 @@ func (p *PurchaseOrderProcessorImpl) UpdatePurchaseOrder(ctx context.Context, id UnitID: unitID, Amount: amount, } - totalAmount += amount + totalAmount += calculatePurchaseOrderItemTotal(quantity, amount) } // Delete and recreate only after all replacement items are valid. @@ -447,7 +447,7 @@ func (p *PurchaseOrderProcessorImpl) UpdatePurchaseOrderStatus(ctx context.Conte // Calculate unit cost in ingredient's base unit unitCost := 0.0 if quantityToAdd > 0 { - unitCost = item.Amount / quantityToAdd + unitCost = calculatePurchaseOrderItemTotal(item.Quantity, item.Amount) / quantityToAdd } // Create inventory movement for ingredient purchase @@ -506,3 +506,11 @@ func (p *PurchaseOrderProcessorImpl) validatePurchaseCategory(ctx context.Contex return category, nil } + +func calculatePurchaseOrderItemTotal(quantity *float64, amount float64) float64 { + if quantity == nil { + return amount + } + + return *quantity * amount +}