From 87973a6c9f7365551793ec9a6d9f298dad3529f8 Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Thu, 22 Jan 2026 14:15:03 +0700 Subject: [PATCH] HOTFIX[BE]: update total price calculation based on product flags for delivery and sales orders --- .../salesorder_product.repository.go | 5 +- .../services/deliveryorder.service.go | 52 ++++++++++++++-- .../marketing/services/salesorder.service.go | 59 +++++++++++++++++-- 3 files changed, 106 insertions(+), 10 deletions(-) diff --git a/internal/modules/marketing/repositories/salesorder_product.repository.go b/internal/modules/marketing/repositories/salesorder_product.repository.go index 4d5eb43f..95003939 100644 --- a/internal/modules/marketing/repositories/salesorder_product.repository.go +++ b/internal/modules/marketing/repositories/salesorder_product.repository.go @@ -26,7 +26,10 @@ func NewMarketingProductRepository(db *gorm.DB) MarketingProductRepository { func (r *MarketingProductRepositoryImpl) GetByMarketingID(ctx context.Context, marketingID uint) ([]entity.MarketingProduct, error) { var products []entity.MarketingProduct - if err := r.DB().WithContext(ctx).Where("marketing_id = ?", marketingID).Find(&products).Error; err != nil { + if err := r.DB().WithContext(ctx). + Preload("ProductWarehouse.Product.Flags"). + Where("marketing_id = ?", marketingID). + Find(&products).Error; err != nil { return nil, err } if len(products) == 0 { diff --git a/internal/modules/marketing/services/deliveryorder.service.go b/internal/modules/marketing/services/deliveryorder.service.go index a521e5bc..b4e3eea0 100644 --- a/internal/modules/marketing/services/deliveryorder.service.go +++ b/internal/modules/marketing/services/deliveryorder.service.go @@ -247,9 +247,27 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery itemDeliveryDate = &parsedDate } - // Hitung total_weight dan total_price otomatis + // Cek apakah product punya flag PAKAN atau OVK + isPakanOrOVK := false + if foundMarketingProduct.ProductWarehouse.Product.Id != 0 && len(foundMarketingProduct.ProductWarehouse.Product.Flags) > 0 { + for _, flag := range foundMarketingProduct.ProductWarehouse.Product.Flags { + if flag.Name == string(utils.FlagPakan) || flag.Name == string(utils.FlagOVK) { + isPakanOrOVK = true + break + } + } + } + + // Hitung total_weight dan total_price berdasarkan flag totalWeight := requestedProduct.Qty * requestedProduct.AvgWeight - totalPrice := requestedProduct.UnitPrice * totalWeight + var totalPrice float64 + if isPakanOrOVK { + // PAKAN atau OVK: qty × unit_price + totalPrice = requestedProduct.Qty * requestedProduct.UnitPrice + } else { + // Produk lain: total_weight × unit_price + totalPrice = totalWeight * requestedProduct.UnitPrice + } deliveryProduct.ProductWarehouseId = foundMarketingProduct.ProductWarehouseId deliveryProduct.UnitPrice = requestedProduct.UnitPrice @@ -361,9 +379,27 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO oldRequestedQty := deliveryProduct.UsageQty + deliveryProduct.PendingQty - // Hitung total_weight dan total_price otomatis + // Cek apakah product punya flag PAKAN atau OVK + isPakanOrOVK := false + if foundMarketingProduct.ProductWarehouse.Product.Id != 0 && len(foundMarketingProduct.ProductWarehouse.Product.Flags) > 0 { + for _, flag := range foundMarketingProduct.ProductWarehouse.Product.Flags { + if flag.Name == string(utils.FlagPakan) || flag.Name == string(utils.FlagOVK) { + isPakanOrOVK = true + break + } + } + } + + // Hitung total_weight dan total_price berdasarkan flag totalWeight := requestedProduct.Qty * requestedProduct.AvgWeight - totalPrice := requestedProduct.UnitPrice * totalWeight + var totalPrice float64 + if isPakanOrOVK { + // PAKAN atau OVK: qty × unit_price + totalPrice = requestedProduct.Qty * requestedProduct.UnitPrice + } else { + // Produk lain: total_weight × unit_price + totalPrice = totalWeight * requestedProduct.UnitPrice + } deliveryProduct.ProductWarehouseId = foundMarketingProduct.ProductWarehouseId deliveryProduct.UnitPrice = requestedProduct.UnitPrice @@ -435,7 +471,13 @@ func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gor } if pw == nil || pw.Quantity < requestedQty { - return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock. Available: %.2f, Requested: %.2f", func() float64 { if pw != nil { return pw.Quantity } else { return 0 } }(), requestedQty)) + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock. Available: %.2f, Requested: %.2f", func() float64 { + if pw != nil { + return pw.Quantity + } else { + return 0 + } + }(), requestedQty)) } if err := deliveryProductRepo.UpdateFifoFields(ctx, deliveryProduct.Id, requestedQty, 0); err != nil { diff --git a/internal/modules/marketing/services/salesorder.service.go b/internal/modules/marketing/services/salesorder.service.go index e73184dd..e2cfcabb 100644 --- a/internal/modules/marketing/services/salesorder.service.go +++ b/internal/modules/marketing/services/salesorder.service.go @@ -292,9 +292,35 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u for _, rp := range req.MarketingProducts { if old, ok := oldByPW[rp.ProductWarehouseId]; ok { - // Hitung total_weight dan total_price otomatis + // Get product untuk cek flag PAKAN atau OVK + productWarehouse, err := s.ProductWarehouseRepo.GetByID(c.Context(), rp.ProductWarehouseId, func(db *gorm.DB) *gorm.DB { + return db.Preload("Product.Flags") + }) + if err != nil { + return err + } + + // Cek apakah product punya flag PAKAN atau OVK + isPakanOrOVK := false + if productWarehouse.Product.Id != 0 && len(productWarehouse.Product.Flags) > 0 { + for _, flag := range productWarehouse.Product.Flags { + if flag.Name == string(utils.FlagPakan) || flag.Name == string(utils.FlagOVK) { + isPakanOrOVK = true + break + } + } + } + + // Hitung total_weight dan total_price berdasarkan flag totalWeight := rp.Qty * rp.AvgWeight - totalPrice := rp.UnitPrice * totalWeight + var totalPrice float64 + if isPakanOrOVK { + // PAKAN atau OVK: qty × unit_price + totalPrice = rp.Qty * rp.UnitPrice + } else { + // Produk lain: total_weight × unit_price + totalPrice = totalWeight * rp.UnitPrice + } updateBody := map[string]any{ "product_warehouse_id": rp.ProductWarehouseId, @@ -592,9 +618,34 @@ func (s salesOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]e func (s *salesOrdersService) createMarketingProductWithDelivery(ctx context.Context, marketingId uint, rp validation.CreateMarketingProduct, marketingProductRepo repository.MarketingProductRepository, invDeliveryRepo repository.MarketingDeliveryProductRepository) error { - // Hitung total_weight dan total_price otomatis + // Get product untuk cek flag PAKAN atau OVK + productWarehouse, err := s.ProductWarehouseRepo.GetByID(ctx, rp.ProductWarehouseId, func(db *gorm.DB) *gorm.DB { + return db.Preload("Product.Flags") + }) + if err != nil { + return err + } + + // Cek apakah product punya flag PAKAN atau OVK + isPakanOrOVK := false + if productWarehouse.Product.Id != 0 && len(productWarehouse.Product.Flags) > 0 { + for _, flag := range productWarehouse.Product.Flags { + if flag.Name == string(utils.FlagPakan) || flag.Name == string(utils.FlagOVK) { + isPakanOrOVK = true + break + } + } + } + totalWeight := rp.Qty * rp.AvgWeight - totalPrice := rp.UnitPrice * totalWeight + var totalPrice float64 + if isPakanOrOVK { + // PAKAN atau OVK: qty × unit_price + totalPrice = rp.Qty * rp.UnitPrice + } else { + // Produk lain: total_weight × unit_price + totalPrice = totalWeight * rp.UnitPrice + } marketingProduct := &entity.MarketingProduct{ MarketingId: marketingId,