mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
Fix(BE-278):fixing total price in purchase
This commit is contained in:
@@ -17,10 +17,10 @@ func Routes(router fiber.Router, purchaseService service.PurchaseService, userSe
|
||||
|
||||
route.Get("/",m.RequirePermissions(m.P_PurchaseGetAll), ctrl.GetAll)
|
||||
route.Get("/:id",m.RequirePermissions(m.P_PurchaseGetOne), ctrl.GetOne)
|
||||
route.Post("/",m.RequirePermissions(m.P_PurchaseCreateOne), ctrl.CreateOne)
|
||||
route.Post("/:id/approvals/staff",m.RequirePermissions(m.P_PurchaseApprovalStaff), ctrl.ApproveStaffPurchase)
|
||||
route.Post("/:id/approvals/manager",m.RequirePermissions(m.P_PurchaseApprovalManager), ctrl.ApproveManagerPurchase)
|
||||
route.Post("/:id/receipts",m.RequirePermissions(m.P_PurchaseReceive), ctrl.ReceiveProducts)
|
||||
route.Delete("/:id",m.RequirePermissions(m.P_RecordingDeleteOne), ctrl.DeletePurchase)
|
||||
route.Delete("/:id/items",m.RequirePermissions(m.P_PurchaseItemDeleteOne), ctrl.DeleteItems)
|
||||
route.Post("/", ctrl.CreateOne)
|
||||
route.Post("/:id/approvals/staff", ctrl.ApproveStaffPurchase)
|
||||
route.Post("/:id/approvals/manager", ctrl.ApproveManagerPurchase)
|
||||
route.Post("/:id/receipts",ctrl.ReceiveProducts)
|
||||
route.Delete("/:id", ctrl.DeletePurchase)
|
||||
route.Delete("/:id/items", ctrl.DeleteItems)
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
|
||||
return nil, nil, utils.Internal("Failed to get warehouse")
|
||||
}
|
||||
if warehouse.KandangId == nil || *warehouse.KandangId == 0 {
|
||||
return nil, nil, utils.BadRequest(fmt.Sprintf("Warehouse %d is not linked to a kandang", id))
|
||||
return nil, nil, utils.BadRequest(fmt.Sprintf("%s is not linked to a kandang", warehouse.Name))
|
||||
}
|
||||
var pfkID *uint
|
||||
if s.ProjectFlockKandangRepo != nil {
|
||||
@@ -258,7 +258,7 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
|
||||
idCopy := uint(pfk.Id)
|
||||
pfkID = &idCopy
|
||||
} else if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil, utils.BadRequest(fmt.Sprintf("Warehouse %d has no active project flock", id))
|
||||
return nil, nil, utils.BadRequest(fmt.Sprintf("%s has no active project flock", warehouse.Name))
|
||||
} else if err != nil {
|
||||
s.Log.Errorf("Failed to validate project flock for warehouse %d: %+v", id, err)
|
||||
return nil, nil, utils.Internal("Failed to validate project flock")
|
||||
@@ -794,6 +794,7 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
deltas := make(map[uint]float64)
|
||||
affected := make(map[uint]struct{})
|
||||
updates := make([]rPurchase.PurchaseReceivingUpdate, 0, len(prepared))
|
||||
priceUpdates := make([]rPurchase.PurchasePricingUpdate, 0, len(prepared))
|
||||
fifoAdds := make([]struct {
|
||||
itemID uint
|
||||
pwID uint
|
||||
@@ -862,6 +863,14 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
}
|
||||
|
||||
updates = append(updates, update)
|
||||
|
||||
if item.Price > 0 && prep.receivedQty >= 0 {
|
||||
priceUpdates = append(priceUpdates, rPurchase.PurchasePricingUpdate{
|
||||
ItemID: item.Id,
|
||||
Price: item.Price,
|
||||
TotalPrice: item.Price * prep.receivedQty,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if err := repoTx.UpdateReceivingDetails(c.Context(), purchase.Id, updates); err != nil {
|
||||
@@ -876,6 +885,12 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
return err
|
||||
}
|
||||
|
||||
if len(priceUpdates) > 0 {
|
||||
if err := repoTx.UpdatePricing(c.Context(), purchase.Id, priceUpdates); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Update due_date based on earliest received date when receiving approved.
|
||||
if earliestReceived != nil {
|
||||
due := earliestReceived.AddDate(0, 0, purchase.CreditTerm)
|
||||
|
||||
Reference in New Issue
Block a user