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
@@ -64,6 +64,7 @@ type purchaseService struct {
ApprovalSvc commonSvc.ApprovalService
ExpenseBridge PurchaseExpenseBridge
FifoStockV2Svc commonSvc.FifoStockV2Service
FifoPaymentSvc commonSvc.FifoPaymentService
DocumentSvc commonSvc.DocumentService
approvalWorkflow approvalutils.ApprovalWorkflowKey
}
@@ -91,6 +92,7 @@ func NewPurchaseService(
approvalSvc commonSvc.ApprovalService,
expenseBridge PurchaseExpenseBridge,
fifoStockV2Svc commonSvc.FifoStockV2Service,
fifoPaymentSvc commonSvc.FifoPaymentService,
documentSvc commonSvc.DocumentService,
) PurchaseService {
return &purchaseService{
@@ -105,6 +107,7 @@ func NewPurchaseService(
ApprovalSvc: approvalSvc,
ExpenseBridge: expenseBridge,
FifoStockV2Svc: fifoStockV2Svc,
FifoPaymentSvc: fifoPaymentSvc,
DocumentSvc: documentSvc,
approvalWorkflow: utils.ApprovalWorkflowPurchase,
}
@@ -1406,6 +1409,16 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
return nil, err
}
// Refresh purchase.grand_total + reallocate payment FIFO untuk supplier (new debt baru emerges).
if s.FifoPaymentSvc != nil && receivingAction == entity.ApprovalActionApproved {
if err := s.FifoPaymentSvc.RecomputeGrandTotal(c.Context(), nil, commonSvc.ParentKindPurchase, purchase.Id); err != nil {
s.Log.Warnf("Failed to recompute grand_total for purchase %d: %+v", purchase.Id, err)
}
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), nil, string(utils.PaymentPartySupplier), uint(purchase.SupplierId)); err != nil {
s.Log.Warnf("Failed to reallocate payments for supplier %d: %+v", purchase.SupplierId, err)
}
}
return updated, nil
}