ini ar fifo

This commit is contained in:
giovanni
2026-05-29 00:59:42 +07:00
parent 2da476b276
commit 8da2b7a3ab
21 changed files with 1010 additions and 209 deletions
@@ -48,6 +48,7 @@ type deliveryOrdersService struct {
ProjectFlockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository
ApprovalSvc commonSvc.ApprovalService
FifoStockV2Svc commonSvc.FifoStockV2Service
FifoPaymentSvc commonSvc.FifoPaymentService
}
func NewDeliveryOrdersService(
@@ -59,6 +60,7 @@ func NewDeliveryOrdersService(
projectFlockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository,
approvalSvc commonSvc.ApprovalService,
fifoStockV2Svc commonSvc.FifoStockV2Service,
fifoPaymentSvc commonSvc.FifoPaymentService,
validate *validator.Validate,
) DeliveryOrdersService {
return &deliveryOrdersService{
@@ -71,6 +73,22 @@ func NewDeliveryOrdersService(
ProjectFlockPopulationRepo: projectFlockPopulationRepo,
ApprovalSvc: approvalSvc,
FifoStockV2Svc: fifoStockV2Svc,
FifoPaymentSvc: fifoPaymentSvc,
}
}
// reallocateAfterDelivery refresh marketing.grand_total + reallocate FIFO untuk customer.
func (s *deliveryOrdersService) reallocateAfterDelivery(ctx context.Context, marketingID uint, customerID uint) {
if s.FifoPaymentSvc == nil {
return
}
if err := s.FifoPaymentSvc.RecomputeGrandTotal(ctx, nil, commonSvc.ParentKindMarketing, marketingID); err != nil {
utils.Log.Warnf("Failed to recompute grand_total for marketing %d: %+v", marketingID, err)
}
if customerID > 0 {
if err := s.FifoPaymentSvc.ReallocateForParty(ctx, nil, string(utils.PaymentPartyCustomer), customerID); err != nil {
utils.Log.Warnf("Failed to reallocate payments for customer %d: %+v", customerID, err)
}
}
}
@@ -418,6 +436,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
return nil, fiber.NewError(fiber.StatusBadRequest, "Delivery order already exists for this marketing")
}
var capturedCustomerID uint
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
@@ -428,6 +447,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
}
capturedCustomerID = marketing.CustomerId
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), req.MarketingId)
if err != nil {
@@ -519,6 +539,8 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order")
}
s.reallocateAfterDelivery(c.Context(), req.MarketingId, capturedCustomerID)
return s.getMarketingWithDeliveries(c, req.MarketingId)
}
@@ -547,6 +569,7 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
}
var capturedCustomerID uint
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
@@ -557,6 +580,7 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
}
capturedCustomerID = marketing.CustomerId
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), id)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
@@ -662,6 +686,8 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery order")
}
s.reallocateAfterDelivery(c.Context(), id, capturedCustomerID)
return s.getMarketingWithDeliveries(c, id)
}