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
+24 -24
View File
@@ -35,17 +35,17 @@ type OrderServiceImpl struct {
tableRepo repository.TableRepositoryInterface
orderIngredientTransactionService *OrderIngredientTransactionService
orderIngredientTransactionProcessor processor.OrderIngredientTransactionProcessor
productIngredientRepo repository.ProductIngredientRepository
productRecipeRepo repository.ProductRecipeRepository
txManager *repository.TxManager
}
func NewOrderServiceImpl(orderProcessor processor.OrderProcessor, tableRepo repository.TableRepositoryInterface, orderIngredientTransactionService *OrderIngredientTransactionService, orderIngredientTransactionProcessor processor.OrderIngredientTransactionProcessor, productIngredientRepo repository.ProductIngredientRepository, txManager *repository.TxManager) *OrderServiceImpl {
func NewOrderServiceImpl(orderProcessor processor.OrderProcessor, tableRepo repository.TableRepositoryInterface, orderIngredientTransactionService *OrderIngredientTransactionService, orderIngredientTransactionProcessor processor.OrderIngredientTransactionProcessor, productRecipeRepo repository.ProductRecipeRepository, txManager *repository.TxManager) *OrderServiceImpl {
return &OrderServiceImpl{
orderProcessor: orderProcessor,
tableRepo: tableRepo,
orderIngredientTransactionService: orderIngredientTransactionService,
orderIngredientTransactionProcessor: orderIngredientTransactionProcessor,
productIngredientRepo: productIngredientRepo,
productRecipeRepo: productRecipeRepo,
txManager: txManager,
}
}
@@ -112,18 +112,18 @@ func (s *OrderServiceImpl) createIngredientTransactions(ctx context.Context, ord
var allTransactions []*contract.CreateOrderIngredientTransactionRequest
for _, orderItem := range orderItems {
// Get product ingredients for this product
productIngredients, err := s.productIngredientRepo.GetByProductID(ctx, orderItem.ProductID, organizationID)
// Get product recipes for this product
productRecipes, err := s.productRecipeRepo.GetByProductID(ctx, orderItem.ProductID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get product ingredients for product %s: %w", orderItem.ProductID, err)
return nil, fmt.Errorf("failed to get product recipes for product %s: %w", orderItem.ProductID, err)
}
if len(productIngredients) == 0 {
continue // Skip if no ingredients
if len(productRecipes) == 0 {
continue // Skip if no recipes
}
// Calculate waste quantities
transactions, err := s.calculateWasteQuantities(productIngredients, float64(orderItem.Quantity))
transactions, err := s.calculateWasteQuantities(productRecipes, float64(orderItem.Quantity))
if err != nil {
return nil, fmt.Errorf("failed to calculate waste quantities for product %s: %w", err)
}
@@ -646,17 +646,17 @@ func (s *OrderServiceImpl) handleTableReleaseOnVoid(ctx context.Context, orderID
func (s *OrderServiceImpl) createOrderIngredientTransactions(ctx context.Context, order *models.Order, orderItems []*models.OrderItem) error {
for _, orderItem := range orderItems {
productIngredients, err := s.productIngredientRepo.GetByProductID(ctx, orderItem.ProductID, order.OrganizationID)
productRecipes, err := s.productRecipeRepo.GetByProductID(ctx, orderItem.ProductID, order.OrganizationID)
if err != nil {
return fmt.Errorf("failed to get product ingredients for product %s: %w", orderItem.ProductID, err)
return fmt.Errorf("failed to get product recipes for product %s: %w", orderItem.ProductID, err)
}
if len(productIngredients) == 0 {
continue // Skip if no ingredients
if len(productRecipes) == 0 {
continue // Skip if no recipes
}
// Calculate waste quantities using the utility function
transactions, err := s.calculateWasteQuantities(productIngredients, float64(orderItem.Quantity))
transactions, err := s.calculateWasteQuantities(productRecipes, float64(orderItem.Quantity))
if err != nil {
return fmt.Errorf("failed to calculate waste quantities for product %s: %w", orderItem.ProductID, err)
}
@@ -681,20 +681,20 @@ func (s *OrderServiceImpl) createOrderIngredientTransactions(ctx context.Context
return nil
}
// calculateWasteQuantities calculates gross, net, and waste quantities for product ingredients
func (s *OrderServiceImpl) calculateWasteQuantities(productIngredients []*entities.ProductIngredient, quantity float64) ([]*contract.CreateOrderIngredientTransactionRequest, error) {
if len(productIngredients) == 0 {
// calculateWasteQuantities calculates gross, net, and waste quantities for product recipes
func (s *OrderServiceImpl) calculateWasteQuantities(productRecipes []*entities.ProductRecipe, quantity float64) ([]*contract.CreateOrderIngredientTransactionRequest, error) {
if len(productRecipes) == 0 {
return []*contract.CreateOrderIngredientTransactionRequest{}, nil
}
transactions := make([]*contract.CreateOrderIngredientTransactionRequest, 0, len(productIngredients))
transactions := make([]*contract.CreateOrderIngredientTransactionRequest, 0, len(productRecipes))
for _, pi := range productIngredients {
for _, pr := range productRecipes {
// 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
@@ -702,12 +702,12 @@ func (s *OrderServiceImpl) calculateWasteQuantities(productIngredients []*entiti
// Get unit name from ingredient
unitName := "unit" // default
if pi.Ingredient != nil && pi.Ingredient.Unit != nil {
unitName = pi.Ingredient.Unit.Name
if pr.Ingredient != nil && pr.Ingredient.Unit != nil {
unitName = pr.Ingredient.Unit.Name
}
transaction := &contract.CreateOrderIngredientTransactionRequest{
IngredientID: pi.IngredientID,
IngredientID: pr.IngredientID,
GrossQty: util.RoundToDecimalPlaces(grossQty, 3),
NetQty: util.RoundToDecimalPlaces(netQty, 3),
WasteQty: util.RoundToDecimalPlaces(wasteQty, 3),