mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
adjust common hpp v2
This commit is contained in:
@@ -89,11 +89,21 @@ type HppV2ManualDepreciationInputRow struct {
|
||||
Note *string
|
||||
}
|
||||
|
||||
type HppV2FarmDepreciationSnapshotRow struct {
|
||||
ID uint
|
||||
ProjectFlockID uint
|
||||
PeriodDate time.Time
|
||||
DepreciationPercentEffective float64
|
||||
DepreciationValue float64
|
||||
PulletCostDayNTotal float64
|
||||
}
|
||||
|
||||
type HppV2CostRepository interface {
|
||||
GetProjectFlockKandangContext(ctx context.Context, projectFlockKandangId uint) (*HppV2ProjectFlockKandangContext, error)
|
||||
GetProjectFlockKandangIDs(ctx context.Context, projectFlockId uint) ([]uint, error)
|
||||
GetLatestTransferInputByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint, period time.Time) (*HppV2LatestTransferInputRow, error)
|
||||
GetManualDepreciationInputByProjectFlockID(ctx context.Context, projectFlockID uint) (*HppV2ManualDepreciationInputRow, error)
|
||||
GetFarmDepreciationSnapshotByProjectFlockIDAndPeriod(ctx context.Context, projectFlockID uint, periodDate time.Time) (*HppV2FarmDepreciationSnapshotRow, error)
|
||||
GetEarliestChickInDateByProjectFlockID(ctx context.Context, projectFlockID uint) (*time.Time, error)
|
||||
GetDepreciationPercents(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, error)
|
||||
ListUsageCostRowsByProductFlags(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string, date *time.Time) ([]HppV2UsageCostRow, error)
|
||||
@@ -239,6 +249,29 @@ func (r *HppV2RepositoryImpl) GetManualDepreciationInputByProjectFlockID(
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
func (r *HppV2RepositoryImpl) GetFarmDepreciationSnapshotByProjectFlockIDAndPeriod(
|
||||
ctx context.Context,
|
||||
projectFlockID uint,
|
||||
periodDate time.Time,
|
||||
) (*HppV2FarmDepreciationSnapshotRow, error) {
|
||||
var row HppV2FarmDepreciationSnapshotRow
|
||||
err := r.db.WithContext(ctx).
|
||||
Table("farm_depreciation_snapshots").
|
||||
Select("id, project_flock_id, period_date, depreciation_percent_effective, depreciation_value, pullet_cost_day_n_total").
|
||||
Where("project_flock_id = ?", projectFlockID).
|
||||
Where("period_date = DATE(?)", periodDate).
|
||||
Limit(1).
|
||||
Take(&row).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
func (r *HppV2RepositoryImpl) GetEarliestChickInDateByProjectFlockID(ctx context.Context, projectFlockID uint) (*time.Time, error) {
|
||||
type row struct {
|
||||
ChickInDate *time.Time
|
||||
@@ -327,11 +360,11 @@ func (r *HppV2RepositoryImpl) ListUsageCostRowsByProductFlags(
|
||||
COALESCE(pi.product_id, ast_pw.product_id, 0) AS source_product_id,
|
||||
COALESCE(pi_prod.name, ast_prod.name, '') AS source_product_name,
|
||||
COALESCE(SUM(sa.qty), 0) AS qty,
|
||||
CASE
|
||||
COALESCE(MAX(CASE
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(pi.price, 0)
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(ast.price, 0)
|
||||
ELSE 0
|
||||
END AS unit_price,
|
||||
END), 0) AS unit_price,
|
||||
COALESCE(SUM(sa.qty * CASE
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(pi.price, 0)
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(ast.price, 0)
|
||||
@@ -367,12 +400,7 @@ func (r *HppV2RepositoryImpl) ListUsageCostRowsByProductFlags(
|
||||
sa.stockable_type,
|
||||
sa.stockable_id,
|
||||
COALESCE(pi.product_id, ast_pw.product_id, 0),
|
||||
COALESCE(pi_prod.name, ast_prod.name, ''),
|
||||
CASE
|
||||
WHEN sa.stockable_type = '` + stockablePurchase + `' THEN COALESCE(pi.price, 0)
|
||||
WHEN sa.stockable_type = '` + stockableAdjustment + `' THEN COALESCE(ast.price, 0)
|
||||
ELSE 0
|
||||
END
|
||||
COALESCE(pi_prod.name, ast_prod.name, '')
|
||||
`).
|
||||
Order("MIN(r.record_datetime) ASC, sa.stockable_type ASC, sa.stockable_id ASC").
|
||||
Scan(&rows).Error
|
||||
@@ -417,7 +445,7 @@ func (r *HppV2RepositoryImpl) ListAdjustmentCostRowsByProductFlags(
|
||||
Joins("JOIN products AS p ON p.id = pw.product_id").
|
||||
Joins("JOIN warehouses AS w ON w.id = pw.warehouse_id").
|
||||
Where("pw.project_flock_kandang_id IN ?", projectFlockKandangIDs).
|
||||
Where("ast.created_at <= ?", *date).
|
||||
// Where("ast.created_at <= ?", *date).
|
||||
Where("COALESCE(ast.total_qty, 0) > 0").
|
||||
Where("EXISTS (SELECT 1 FROM flags f WHERE f.flagable_id = pw.product_id AND f.flagable_type = ? AND f.name IN ?)", entity.FlagableTypeProduct, flagNames).
|
||||
Order("ast.created_at ASC, ast.id ASC").
|
||||
@@ -450,6 +478,15 @@ func (r *HppV2RepositoryImpl) ListChickinCostRowsByProductFlags(
|
||||
stockableTransferToLaying := fifo.StockableKeyTransferToLayingIn.String()
|
||||
usableProjectChickin := fifo.UsableKeyProjectChickin.String()
|
||||
usableStockTransferOut := fifo.UsableKeyStockTransferOut.String()
|
||||
unitPriceExpr := fmt.Sprintf(`
|
||||
CASE
|
||||
WHEN sa.stockable_type = '%s' THEN COALESCE(pi.price, 0)
|
||||
WHEN sa.stockable_type = '%s' THEN COALESCE(ast.price, 0)
|
||||
WHEN sa.stockable_type = '%s' THEN COALESCE(spi.price, sast.price, 0)
|
||||
WHEN sa.stockable_type = '%s' THEN COALESCE(tpi.price, tast.price, 0)
|
||||
ELSE 0
|
||||
END
|
||||
`, stockablePurchase, stockableAdjustment, stockableTransferIn, stockableTransferToLaying)
|
||||
|
||||
rows := make([]HppV2ChickinCostRow, 0)
|
||||
query := r.db.WithContext(ctx).
|
||||
@@ -479,30 +516,9 @@ func (r *HppV2RepositoryImpl) ListChickinCostRowsByProductFlags(
|
||||
''
|
||||
) AS source_product_name,
|
||||
COALESCE(SUM(sa.qty), 0) AS qty,
|
||||
CASE
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(pi.price, 0)
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(ast.price, 0)
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(spi.price, sast.price, 0)
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(tpi.price, tast.price, 0)
|
||||
ELSE 0
|
||||
END AS unit_price,
|
||||
COALESCE(SUM(sa.qty * CASE
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(pi.price, 0)
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(ast.price, 0)
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(spi.price, sast.price, 0)
|
||||
WHEN sa.stockable_type = ? THEN COALESCE(tpi.price, tast.price, 0)
|
||||
ELSE 0
|
||||
END), 0) AS total_cost
|
||||
`,
|
||||
stockablePurchase,
|
||||
stockableAdjustment,
|
||||
stockableTransferIn,
|
||||
stockableTransferToLaying,
|
||||
stockablePurchase,
|
||||
stockableAdjustment,
|
||||
stockableTransferIn,
|
||||
stockableTransferToLaying,
|
||||
).
|
||||
`+unitPriceExpr+` AS unit_price,
|
||||
COALESCE(SUM(sa.qty * (`+unitPriceExpr+`)), 0) AS total_cost
|
||||
`).
|
||||
Joins(
|
||||
"JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = pc.id AND sa.status = ? AND sa.allocation_purpose = ?",
|
||||
usableProjectChickin,
|
||||
@@ -563,7 +579,7 @@ func (r *HppV2RepositoryImpl) ListChickinCostRowsByProductFlags(
|
||||
}
|
||||
|
||||
err := query.
|
||||
Group(`
|
||||
Group(fmt.Sprintf(`
|
||||
pc.id,
|
||||
pc.project_flock_kandang_id,
|
||||
pc.chick_in_date,
|
||||
@@ -587,14 +603,8 @@ func (r *HppV2RepositoryImpl) ListChickinCostRowsByProductFlags(
|
||||
sast_prod.name,
|
||||
''
|
||||
),
|
||||
CASE
|
||||
WHEN sa.stockable_type = '` + stockablePurchase + `' THEN COALESCE(pi.price, 0)
|
||||
WHEN sa.stockable_type = '` + stockableAdjustment + `' THEN COALESCE(ast.price, 0)
|
||||
WHEN sa.stockable_type = '` + stockableTransferIn + `' THEN COALESCE(spi.price, sast.price, 0)
|
||||
WHEN sa.stockable_type = '` + stockableTransferToLaying + `' THEN COALESCE(tpi.price, tast.price, 0)
|
||||
ELSE 0
|
||||
END
|
||||
`).
|
||||
%s
|
||||
`, unitPriceExpr)).
|
||||
Order("pc.chick_in_date ASC, pc.id ASC, sa.stockable_type ASC, sa.stockable_id ASC").
|
||||
Scan(&rows).Error
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user