mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
adjust calculate total price create sales order for telur and convertion peti and qty
This commit is contained in:
@@ -141,6 +141,12 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
if item.ConvertionUnit != nil && !utils.IsValidConvertionUnit(*item.ConvertionUnit) {
|
if item.ConvertionUnit != nil && !utils.IsValidConvertionUnit(*item.ConvertionUnit) {
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Unit konversi tidak valid")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Unit konversi tidak valid")
|
||||||
}
|
}
|
||||||
|
if item.MarketingType == string(utils.MarketingTypeTelur) &&
|
||||||
|
item.ConvertionUnit != nil &&
|
||||||
|
*item.ConvertionUnit == string(utils.ConvertionUnitPeti) &&
|
||||||
|
(item.WeightPerConvertion == nil || *item.WeightPerConvertion <= 0) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "weight_per_convertion wajib diisi dan > 0 untuk TELUR dengan convertion_unit PETI")
|
||||||
|
}
|
||||||
if err := m.EnsureProductWarehouseAccess(c, s.MarketingRepo.DB(), item.ProductWarehouseId); err != nil {
|
if err := m.EnsureProductWarehouseAccess(c, s.MarketingRepo.DB(), item.ProductWarehouseId); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -308,6 +314,12 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
|||||||
if item.ConvertionUnit != nil && !utils.IsValidConvertionUnit(*item.ConvertionUnit) {
|
if item.ConvertionUnit != nil && !utils.IsValidConvertionUnit(*item.ConvertionUnit) {
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Unit konversi tidak valid")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Unit konversi tidak valid")
|
||||||
}
|
}
|
||||||
|
if item.MarketingType == string(utils.MarketingTypeTelur) &&
|
||||||
|
item.ConvertionUnit != nil &&
|
||||||
|
*item.ConvertionUnit == string(utils.ConvertionUnitPeti) &&
|
||||||
|
(item.WeightPerConvertion == nil || *item.WeightPerConvertion <= 0) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "weight_per_convertion wajib diisi dan > 0 untuk TELUR dengan convertion_unit PETI")
|
||||||
|
}
|
||||||
if err := m.EnsureProductWarehouseAccess(c, s.MarketingRepo.DB(), item.ProductWarehouseId); err != nil {
|
if err := m.EnsureProductWarehouseAccess(c, s.MarketingRepo.DB(), item.ProductWarehouseId); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -386,7 +398,15 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
|||||||
for _, rp := range req.MarketingProducts {
|
for _, rp := range req.MarketingProducts {
|
||||||
if old, ok := oldByPW[rp.ProductWarehouseId]; ok {
|
if old, ok := oldByPW[rp.ProductWarehouseId]; ok {
|
||||||
|
|
||||||
totalWeight, totalPrice := s.calculatePriceByMarketingType(rp.MarketingType, rp.Qty, rp.AvgWeight, rp.UnitPrice, rp.Week)
|
totalWeight, totalPrice := s.calculatePriceByMarketingType(
|
||||||
|
rp.MarketingType,
|
||||||
|
rp.Qty,
|
||||||
|
rp.AvgWeight,
|
||||||
|
rp.UnitPrice,
|
||||||
|
rp.Week,
|
||||||
|
rp.ConvertionUnit,
|
||||||
|
rp.WeightPerConvertion,
|
||||||
|
)
|
||||||
|
|
||||||
deliveryProduct, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id)
|
deliveryProduct, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id)
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
@@ -750,7 +770,15 @@ func (s salesOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]e
|
|||||||
|
|
||||||
func (s *salesOrdersService) createMarketingProductWithDelivery(ctx context.Context, marketingId uint, marketingType string, rp validation.CreateMarketingProduct, marketingProductRepo repository.MarketingProductRepository, invDeliveryRepo repository.MarketingDeliveryProductRepository) error {
|
func (s *salesOrdersService) createMarketingProductWithDelivery(ctx context.Context, marketingId uint, marketingType string, rp validation.CreateMarketingProduct, marketingProductRepo repository.MarketingProductRepository, invDeliveryRepo repository.MarketingDeliveryProductRepository) error {
|
||||||
|
|
||||||
totalWeight, totalPrice := s.calculatePriceByMarketingType(marketingType, rp.Qty, rp.AvgWeight, rp.UnitPrice, rp.Week)
|
totalWeight, totalPrice := s.calculatePriceByMarketingType(
|
||||||
|
marketingType,
|
||||||
|
rp.Qty,
|
||||||
|
rp.AvgWeight,
|
||||||
|
rp.UnitPrice,
|
||||||
|
rp.Week,
|
||||||
|
rp.ConvertionUnit,
|
||||||
|
rp.WeightPerConvertion,
|
||||||
|
)
|
||||||
|
|
||||||
marketingProduct := &entity.MarketingProduct{
|
marketingProduct := &entity.MarketingProduct{
|
||||||
MarketingId: marketingId,
|
MarketingId: marketingId,
|
||||||
@@ -787,7 +815,7 @@ func (s *salesOrdersService) createMarketingProductWithDelivery(ctx context.Cont
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *salesOrdersService) calculatePriceByMarketingType(marketingType string, qty, avgWeight, unitPrice float64, week *int) (totalWeight, totalPrice float64) {
|
func (s *salesOrdersService) calculatePriceByMarketingType(marketingType string, qty, avgWeight, unitPrice float64, week *int, convertionUnit *string, weightPerConvertion *float64) (totalWeight, totalPrice float64) {
|
||||||
if marketingType == string(utils.MarketingTypeTrading) {
|
if marketingType == string(utils.MarketingTypeTrading) {
|
||||||
totalWeight = 0
|
totalWeight = 0
|
||||||
totalPrice = math.Round(qty*unitPrice*100) / 100
|
totalPrice = math.Round(qty*unitPrice*100) / 100
|
||||||
@@ -796,6 +824,21 @@ func (s *salesOrdersService) calculatePriceByMarketingType(marketingType string,
|
|||||||
totalPrice = math.Round(unitPrice*float64(*week)*qty*100) / 100
|
totalPrice = math.Round(unitPrice*float64(*week)*qty*100) / 100
|
||||||
} else {
|
} else {
|
||||||
totalWeight = math.Round(qty*avgWeight*100) / 100
|
totalWeight = math.Round(qty*avgWeight*100) / 100
|
||||||
|
|
||||||
|
if marketingType == string(utils.MarketingTypeTelur) && convertionUnit != nil {
|
||||||
|
switch *convertionUnit {
|
||||||
|
case string(utils.ConvertionUnitQty):
|
||||||
|
totalPrice = math.Round(qty*unitPrice*100) / 100
|
||||||
|
return totalWeight, totalPrice
|
||||||
|
case string(utils.ConvertionUnitPeti):
|
||||||
|
if weightPerConvertion != nil && *weightPerConvertion > 0 {
|
||||||
|
totalPeti := totalWeight / *weightPerConvertion
|
||||||
|
totalPrice = math.Round(totalPeti*unitPrice*100) / 100
|
||||||
|
return totalWeight, totalPrice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
totalPrice = math.Round(totalWeight*unitPrice*100) / 100
|
totalPrice = math.Round(totalWeight*unitPrice*100) / 100
|
||||||
}
|
}
|
||||||
return totalWeight, totalPrice
|
return totalWeight, totalPrice
|
||||||
|
|||||||
Reference in New Issue
Block a user