mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
fix calculate age at closing data production
This commit is contained in:
@@ -101,6 +101,26 @@ func ToSalesDTO(e entity.MarketingDeliveryProduct) SalesDTO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToSalesAgeDTO(e entity.MarketingDeliveryProduct) SalesDTO {
|
||||||
|
|
||||||
|
productFlags := make([]string, len(e.MarketingProduct.ProductWarehouse.Product.Flags))
|
||||||
|
for i, f := range e.MarketingProduct.ProductWarehouse.Product.Flags {
|
||||||
|
productFlags[i] = f.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
var category string
|
||||||
|
if e.MarketingProduct.ProductWarehouse.ProjectFlockKandang != nil {
|
||||||
|
category = e.MarketingProduct.ProductWarehouse.ProjectFlockKandang.ProjectFlock.Category
|
||||||
|
}
|
||||||
|
|
||||||
|
ageInDay, _ := calculateAgeFromChickin(e.MarketingProduct.ProductWarehouse.ProjectFlockKandang, e.DeliveryDate, productFlags, category)
|
||||||
|
|
||||||
|
return SalesDTO{
|
||||||
|
Age: ageInDay,
|
||||||
|
Qty: e.UsageQty,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ToSummaryDto(e []entity.MarketingDeliveryProduct) SummaryDTO {
|
func ToSummaryDto(e []entity.MarketingDeliveryProduct) SummaryDTO {
|
||||||
|
|
||||||
var totalSalesPrice, totalActualPrice, sumSales, sumActual float64
|
var totalSalesPrice, totalActualPrice, sumSales, sumActual float64
|
||||||
|
|||||||
@@ -841,7 +841,7 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
|
|||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch FCR standard data")
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch FCR standard data")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
age, err := s.calculateAverageSalesAge(c.Context(), projectFlockID)
|
age, err := s.calculateAverageSalesAge(c.Context(), projectFlockID, kandangID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to calculate sales age for project flock %d: %+v", projectFlockID, err)
|
s.Log.Errorf("Failed to calculate sales age for project flock %d: %+v", projectFlockID, err)
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales age data")
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales age data")
|
||||||
@@ -1028,38 +1028,24 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
|
|||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s closingService) calculateAverageSalesAge(ctx context.Context, projectFlockID uint) (float64, error) {
|
func (s closingService) calculateAverageSalesAge(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint) (float64, error) {
|
||||||
deliveryProducts, err := s.MarketingDeliveryProductRepo.GetDeliveryProductsByProjectFlockID(ctx, projectFlockID, func(db *gorm.DB) *gorm.DB {
|
penjualan, err := s.MarketingDeliveryProductRepo.GetClosingPenjualanForAgeChickDataProduction(ctx, projectFlockID, projectFlockKandangID)
|
||||||
return db.
|
|
||||||
Preload("MarketingProduct").
|
|
||||||
Preload("MarketingProduct.ProductWarehouse").
|
|
||||||
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang").
|
|
||||||
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang.Chickins")
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
acumulateAgeQty := 0.0
|
||||||
var (
|
totalQty := 0.0
|
||||||
totalQty float64
|
for _, v := range penjualan {
|
||||||
totalAgeWeeks float64
|
sale := dto.ToSalesAgeDTO(v)
|
||||||
)
|
acumulateAgeQty += float64(sale.Age) * sale.Qty
|
||||||
|
totalQty += sale.Qty
|
||||||
for _, product := range deliveryProducts {
|
}
|
||||||
if product.UsageQty == 0 {
|
if totalQty > 0 {
|
||||||
continue
|
averageAge := acumulateAgeQty / totalQty
|
||||||
}
|
return averageAge, nil
|
||||||
projectFlockKandang := product.MarketingProduct.ProductWarehouse.ProjectFlockKandang
|
|
||||||
ageWeeks := dto.CalculateAgeFromChickinDataProduksi(projectFlockKandang, product.DeliveryDate)
|
|
||||||
totalAgeWeeks += float64(ageWeeks) * product.UsageQty
|
|
||||||
totalQty += product.UsageQty
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if totalQty == 0 {
|
return 0, err
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return totalAgeWeeks / totalQty, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s closingService) determineProductionWeek(ctx context.Context, projectFlockKandangIDs []uint) (int, error) {
|
func (s closingService) determineProductionWeek(ctx context.Context, projectFlockKandangIDs []uint) (int, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ type MarketingDeliveryProductRepository interface {
|
|||||||
UpdateFifoFields(ctx context.Context, id uint, usageQty, pendingQty float64) error
|
UpdateFifoFields(ctx context.Context, id uint, usageQty, pendingQty float64) error
|
||||||
GetUsageQty(ctx context.Context, id uint) (float64, error)
|
GetUsageQty(ctx context.Context, id uint) (float64, error)
|
||||||
ResetFifoFields(ctx context.Context, id uint) error
|
ResetFifoFields(ctx context.Context, id uint) error
|
||||||
|
GetClosingPenjualanForAgeChickDataProduction(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint) ([]entity.MarketingDeliveryProduct, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type MarketingDeliveryProductRepositoryImpl struct {
|
type MarketingDeliveryProductRepositoryImpl struct {
|
||||||
@@ -93,6 +94,46 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetClosingPenjualan(ctx context
|
|||||||
return deliveryProducts, nil
|
return deliveryProducts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *MarketingDeliveryProductRepositoryImpl) GetClosingPenjualanForAgeChickDataProduction(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint) ([]entity.MarketingDeliveryProduct, error) {
|
||||||
|
var deliveryProducts []entity.MarketingDeliveryProduct
|
||||||
|
|
||||||
|
db := r.DB().WithContext(ctx).
|
||||||
|
Joins("JOIN marketing_products ON marketing_products.id = marketing_delivery_products.marketing_product_id").
|
||||||
|
Joins("JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id").
|
||||||
|
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||||
|
Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = 'products'").
|
||||||
|
Joins("JOIN project_flock_kandangs ON project_flock_kandangs.id = product_warehouses.project_flock_kandang_id").
|
||||||
|
Where("project_flock_kandangs.project_flock_id = ?", projectFlockID).
|
||||||
|
Where("flags.name IN (?)", []string{
|
||||||
|
string(utils.FlagAyamAfkir),
|
||||||
|
string(utils.FlagAyamCulling),
|
||||||
|
string(utils.FlagPullet),
|
||||||
|
string(utils.FlagLayer),
|
||||||
|
}).
|
||||||
|
Where("marketing_delivery_products.delivery_date IS NOT NULL").
|
||||||
|
Distinct("marketing_delivery_products.*")
|
||||||
|
|
||||||
|
if projectFlockKandangID != nil {
|
||||||
|
db = db.Where("product_warehouses.project_flock_kandang_id = ?", *projectFlockKandangID)
|
||||||
|
}
|
||||||
|
|
||||||
|
db = db.
|
||||||
|
Preload("MarketingProduct").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.Product").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.Product.ProductCategory").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.Product.Flags").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang.Chickins").
|
||||||
|
Order("marketing_delivery_products.delivery_date DESC")
|
||||||
|
|
||||||
|
if err := db.Find(&deliveryProducts).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return deliveryProducts, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *MarketingDeliveryProductRepositoryImpl) GetClosingPenjualanByCategory(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint, category string) ([]entity.MarketingDeliveryProduct, error) {
|
func (r *MarketingDeliveryProductRepositoryImpl) GetClosingPenjualanByCategory(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint, category string) ([]entity.MarketingDeliveryProduct, error) {
|
||||||
var deliveryProducts []entity.MarketingDeliveryProduct
|
var deliveryProducts []entity.MarketingDeliveryProduct
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user