This commit is contained in:
Aditya Siregar
2025-09-13 02:17:51 +07:00
parent 75ec5274d2
commit 4f6208e479
17 changed files with 398 additions and 207 deletions
@@ -28,20 +28,20 @@ type OrderIngredientTransactionProcessor interface {
type OrderIngredientTransactionProcessorImpl struct {
orderIngredientTransactionRepo OrderIngredientTransactionRepository
productIngredientRepo ProductIngredientRepository
productRecipeRepo ProductRecipeRepository
ingredientRepo IngredientRepository
unitRepo UnitRepository
}
func NewOrderIngredientTransactionProcessorImpl(
orderIngredientTransactionRepo OrderIngredientTransactionRepository,
productIngredientRepo ProductIngredientRepository,
productRecipeRepo ProductRecipeRepository,
ingredientRepo IngredientRepository,
unitRepo UnitRepository,
) OrderIngredientTransactionProcessor {
return &OrderIngredientTransactionProcessorImpl{
orderIngredientTransactionRepo: orderIngredientTransactionRepo,
productIngredientRepo: productIngredientRepo,
productRecipeRepo: productRecipeRepo,
ingredientRepo: ingredientRepo,
unitRepo: unitRepo,
}
@@ -334,36 +334,36 @@ func (p *OrderIngredientTransactionProcessorImpl) BulkCreateOrderIngredientTrans
}
func (p *OrderIngredientTransactionProcessorImpl) CalculateWasteQuantities(ctx context.Context, productID uuid.UUID, quantity float64, organizationID uuid.UUID) ([]*models.CreateOrderIngredientTransactionRequest, error) {
// Get product ingredients
productIngredients, err := p.productIngredientRepo.GetByProductID(ctx, productID, organizationID)
// Get product recipes
productRecipes, err := p.productRecipeRepo.GetByProductID(ctx, productID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get product ingredients: %w", err)
return nil, fmt.Errorf("failed to get product recipes: %w", err)
}
if len(productIngredients) == 0 {
if len(productRecipes) == 0 {
return []*models.CreateOrderIngredientTransactionRequest{}, nil
}
// Get ingredient details for unit information
ingredientMap := make(map[uuid.UUID]*entities.Ingredient)
for _, pi := range productIngredients {
ingredient, err := p.ingredientRepo.GetByID(ctx, pi.IngredientID, organizationID)
for _, pr := range productRecipes {
ingredient, err := p.ingredientRepo.GetByID(ctx, pr.IngredientID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get ingredient %s: %w", pi.IngredientID, err)
return nil, fmt.Errorf("failed to get ingredient %s: %w", pr.IngredientID, err)
}
ingredientMap[pi.IngredientID] = ingredient
ingredientMap[pr.IngredientID] = ingredient
}
// Calculate quantities for each ingredient
transactions := make([]*models.CreateOrderIngredientTransactionRequest, 0, len(productIngredients))
for _, pi := range productIngredients {
ingredient := ingredientMap[pi.IngredientID]
transactions := make([]*models.CreateOrderIngredientTransactionRequest, 0, len(productRecipes))
for _, pr := range productRecipes {
ingredient := ingredientMap[pr.IngredientID]
// Calculate net quantity (actual quantity needed for the product)
netQty := pi.Quantity * quantity
netQty := pr.Quantity * quantity
// Calculate gross quantity (including waste)
wasteMultiplier := 1 + (pi.WastePercentage / 100)
wasteMultiplier := 1 + (pr.WastePercentage / 100)
grossQty := netQty * wasteMultiplier
// Calculate waste quantity
@@ -379,7 +379,7 @@ func (p *OrderIngredientTransactionProcessorImpl) CalculateWasteQuantities(ctx c
}
transaction := &models.CreateOrderIngredientTransactionRequest{
IngredientID: pi.IngredientID,
IngredientID: pr.IngredientID,
GrossQty: util.RoundToDecimalPlaces(grossQty, 3),
NetQty: util.RoundToDecimalPlaces(netQty, 3),
WasteQty: util.RoundToDecimalPlaces(wasteQty, 3),