update purchase triger to expense

This commit is contained in:
ragilap
2025-12-06 21:06:53 +07:00
parent ee2db748ea
commit 2d3f7f7ef9
2 changed files with 104 additions and 116 deletions
@@ -110,9 +110,9 @@ func (s *purchaseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
offset := (params.Page - 1) * params.Limit
createdFrom, createdTo, err := parseQueryDates(params.CreatedFrom, params.CreatedTo)
createdFrom, createdTo, err := utils.ParseDateRangeForQuery(params.CreatedFrom, params.CreatedTo)
if err != nil {
return nil, 0, err
return nil, 0, fiber.NewError(fiber.StatusBadRequest, err.Error())
}
purchases, total, err := s.PurchaseRepo.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
@@ -233,9 +233,9 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
if warehouse, ok := warehouseCache[id]; ok {
return warehouse, nil
}
warehouse, err := s.WarehouseRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
return db.Preload("Area").Preload("Location")
})
warehouse, err := s.WarehouseRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
return db.Preload("Area").Preload("Location")
})
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -299,22 +299,22 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
purchase := &entity.Purchase{
SupplierId: uint(req.SupplierID),
DueDate: dueDate,
Notes: req.Notes,
CreatedBy: uint(actorID),
DueDate: dueDate,
Notes: req.Notes,
CreatedBy: uint(actorID),
}
items := make([]*entity.PurchaseItem, 0, len(aggregated))
emptyVehicle := ""
for _, item := range aggregated {
items = append(items, &entity.PurchaseItem{
ProductId: item.productId,
WarehouseId: item.warehouseId,
SubQty: item.subQty,
TotalQty: 0,
TotalUsed: 0,
Price: 0,
TotalPrice: 0,
ProductId: item.productId,
WarehouseId: item.warehouseId,
SubQty: item.subQty,
TotalQty: 0,
TotalUsed: 0,
Price: 0,
TotalPrice: 0,
VehicleNumber: &emptyVehicle,
})
}
@@ -856,13 +856,13 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
for _, prep := range prepared {
date := prep.receivedDate
payload := ExpenseReceivingPayload{
PurchaseItemID: prep.item.Id,
ProductID: prep.item.ProductId,
WarehouseID: uint(prep.warehouseID),
SupplierID: prep.supplierID,
PurchaseItemID: prep.item.Id,
ProductID: prep.item.ProductId,
WarehouseID: uint(prep.warehouseID),
SupplierID: prep.supplierID,
TransportPerItem: prep.transportPerItem,
ReceivedQty: prep.receivedQty,
ReceivedDate: &date,
ReceivedQty: prep.receivedQty,
ReceivedDate: &date,
}
receivingPayloads = append(receivingPayloads, payload)
}
@@ -1090,49 +1090,6 @@ func (s *purchaseService) approvalServiceForDB(db *gorm.DB) commonSvc.ApprovalSe
return nil
}
func (s *purchaseService) attachLatestApprovals(ctx context.Context, items []entity.Purchase) error {
if len(items) == 0 || s.ApprovalSvc == nil {
return nil
}
ids := make([]uint, 0, len(items))
visited := make(map[uint]struct{}, len(items))
for _, item := range items {
if item.Id == 0 {
continue
}
if _, ok := visited[item.Id]; ok {
continue
}
visited[item.Id] = struct{}{}
ids = append(ids, uint(item.Id))
}
if len(ids) == 0 {
return nil
}
latestMap, err := s.ApprovalSvc.LatestByTargets(ctx, utils.ApprovalWorkflowPurchase, ids, func(db *gorm.DB) *gorm.DB {
return db.Preload("ActionUser")
})
if err != nil {
return err
}
for i := range items {
if items[i].Id == 0 {
continue
}
if approval, ok := latestMap[uint(items[i].Id)]; ok {
items[i].LatestApproval = approval
} else {
items[i].LatestApproval = nil
}
}
return nil
}
func (s *purchaseService) notifyExpenseItemsReceived(c *fiber.Ctx, purchaseID uint, payloads []ExpenseReceivingPayload) error {
if s.ExpenseBridge == nil || purchaseID == 0 || len(payloads) == 0 {
return nil
@@ -1237,9 +1194,9 @@ func (s *purchaseService) buildStaffAdjustmentPayload(
update.TotalQty = &qtyCopy
}
updates = append(updates, update)
delete(requestItems, item.Id)
}
updates = append(updates, update)
delete(requestItems, item.Id)
}
if len(requestItems) > 0 {
return nil, fiber.NewError(fiber.StatusBadRequest, "Found pricing data for items that do not belong to this purchase")
}
@@ -1293,19 +1250,19 @@ func (s *purchaseService) buildStaffAdjustmentPayload(
}
newItem := &entity.PurchaseItem{
PurchaseId: purchase.Id,
ProductId: payload.ProductID,
WarehouseId: payload.WarehouseID,
SubQty: qty,
TotalQty: 0,
TotalUsed: 0,
Price: payload.Price,
TotalPrice: totalPrice,
PurchaseId: purchase.Id,
ProductId: payload.ProductID,
WarehouseId: payload.WarehouseID,
SubQty: qty,
TotalQty: 0,
TotalUsed: 0,
Price: payload.Price,
TotalPrice: totalPrice,
VehicleNumber: &emptyVehicle,
}
newItems = append(newItems, newItem)
existingCombos[key] = struct{}{}
}
newItems = append(newItems, newItem)
existingCombos[key] = struct{}{}
}
if len(updates) == 0 && len(newItems) == 0 {
return nil, fiber.NewError(fiber.StatusBadRequest, "Purchase has no items to process")
@@ -1356,14 +1313,6 @@ func (s *purchaseService) attachLatestApproval(ctx context.Context, item *entity
return nil
}
func parseQueryDates(fromStr, toStr string) (*time.Time, *time.Time, error) {
fromPtr, toPtr, err := utils.ParseDateRangeForQuery(fromStr, toStr)
if err != nil {
return nil, nil, fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return fromPtr, toPtr, nil
}
func parseApprovalActionInput(raw string) (entity.ApprovalAction, error) {
value := strings.ToUpper(strings.TrimSpace(raw))
switch value {