Compare commits

...

20 Commits

Author SHA1 Message Date
giovanni 90efd0ba5a add command to fix reconcile fifo; fix fifo stock v2 2026-05-31 16:25:16 +07:00
Giovanni Gabriel Septriadi 09b1f19d19 Merge branch 'rc/01' into 'production'
Rc/01

See merge request mbugroup/lti-api!578
2026-05-30 03:14:16 +00:00
Giovanni Gabriel Septriadi 672f80a3ba Merge branch 'fix/week-recording' into 'rc/01'
Fix/week recording

See merge request mbugroup/lti-api!577
2026-05-30 03:05:50 +00:00
giovanni 0f12c706b0 fix calculate week create recording 2026-05-30 10:01:22 +07:00
Giovanni Gabriel Septriadi d26c4e9e1a Merge branch 'rc/01' into 'production'
Rc/01

See merge request mbugroup/lti-api!573
2026-05-29 16:28:37 +00:00
Giovanni Gabriel Septriadi be7f3ac82a Merge branch 'feat/trf-dep' into 'rc/01'
Feat/trf dep

See merge request mbugroup/lti-api!575
2026-05-29 15:45:33 +00:00
giovanni a46edc4498 add adjustment depresiasi calculation and percentage depresiasi 2026-05-29 21:48:20 +07:00
Giovanni Gabriel Septriadi 254ce509fb Merge branch 'feat/db' into 'rc/01'
add command for cleanup relesed stock allocations

See merge request mbugroup/lti-api!572
2026-05-29 11:48:36 +00:00
Giovanni Gabriel Septriadi 8624030b39 Merge branch 'fix/nomor-po' into 'rc/01'
normalize data po number to pr number and fix logic to fill field PO number

See merge request mbugroup/lti-api!571
2026-05-29 11:47:51 +00:00
giovanni b4fbef702a Merge branch 'production' into feat/transfer-laying 2026-05-29 16:04:46 +07:00
giovanni 0410169746 normalize data po number to pr number and fix logic to fill field PO number 2026-05-29 16:01:44 +07:00
giovanni bbc7f0f6e9 add command for cleanup relesed stock allocations 2026-05-29 15:02:32 +07:00
Giovanni Gabriel Septriadi 6264b0f08d Merge branch 'feat/fifo-ar' into 'production'
Feat/fifo ar

See merge request mbugroup/lti-api!567
2026-05-29 02:38:43 +00:00
giovanni e6fe4d77eb Merge branch 'fix/jamali' into feat/fifo-ar 2026-05-29 01:54:22 +07:00
giovanni 8ee87a73b7 fix 2026-05-29 01:49:58 +07:00
Giovanni Gabriel Septriadi 8fc41ee8e9 Merge branch 'fix/jamali' into 'production'
Fix/jamali

See merge request mbugroup/lti-api!565
2026-05-28 18:04:30 +00:00
giovanni 8da2b7a3ab ini ar fifo 2026-05-29 00:59:42 +07:00
giovanni 7846487254 add migration for drift stock logs 2026-05-28 20:59:41 +07:00
giovanni 0ed67955a6 new file migration 2026-05-28 19:17:51 +07:00
giovanni 679d835fbb add migration for normalize jamali non aktif to gudang farm jamali 2026-05-28 17:27:46 +07:00
47 changed files with 2558 additions and 405 deletions
@@ -0,0 +1,304 @@
// Command cleanup-released-stock-allocations menghapus baris stock_allocations
// dengan status='RELEASED' yang sudah lewat masa retensi.
//
// Baris RELEASED muncul dari operasi Rollback / Reflow FIFO v2. Closing reports
// dan flow bisnis hanya membaca status='ACTIVE', sehingga RELEASED rows aman
// dihapus setelah masa retensi tertentu (default 90 hari).
//
// Cara pakai:
//
// go run ./cmd/cleanup-released-stock-allocations/ # dry-run
// go run ./cmd/cleanup-released-stock-allocations/ -apply # apply (90 hari)
// go run ./cmd/cleanup-released-stock-allocations/ -apply -retention-days=30
// go run ./cmd/cleanup-released-stock-allocations/ -apply -batch-size=5000
// go run ./cmd/cleanup-released-stock-allocations/ -apply -skip-vacuum
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"gitlab.com/mbugroup/lti-api.git/internal/config"
"gitlab.com/mbugroup/lti-api.git/internal/database"
"gitlab.com/mbugroup/lti-api.git/internal/entities"
"gorm.io/gorm"
)
const (
outputTable = "table"
outputJSON = "json"
)
type options struct {
Apply bool
Output string
DBSSLMode string
RetentionDays int
BatchSize int
SkipVacuum bool
}
type sizeStat struct {
TableSize string `json:"table_size" gorm:"column:table_size"`
TotalSize string `json:"total_size" gorm:"column:total_size"`
RowCount int64 `json:"row_count" gorm:"column:row_count"`
}
type runSummary struct {
Mode string `json:"mode"`
RetentionDays int `json:"retention_days"`
CutoffTime string `json:"cutoff_time"`
BatchSize int `json:"batch_size"`
CandidateRows int64 `json:"candidate_rows"`
DeletedRows int64 `json:"deleted_rows,omitempty"`
BatchesExecuted int `json:"batches_executed,omitempty"`
BeforeSize sizeStat `json:"before_size"`
AfterSize sizeStat `json:"after_size,omitempty"`
DurationSeconds float64 `json:"duration_seconds"`
VacuumExecuted bool `json:"vacuum_executed"`
OverallStatus string `json:"overall_status"`
}
func main() {
opts, err := parseFlags()
if err != nil {
log.Fatalf("invalid flags: %v", err)
}
if opts.DBSSLMode != "" {
config.DBSSLMode = opts.DBSSLMode
}
ctx := context.Background()
db := database.Connect(config.DBHost, config.DBName)
start := time.Now()
cutoff := time.Now().Add(-time.Duration(opts.RetentionDays) * 24 * time.Hour)
summary := runSummary{
RetentionDays: opts.RetentionDays,
CutoffTime: cutoff.UTC().Format(time.RFC3339),
BatchSize: opts.BatchSize,
OverallStatus: "PASS",
}
// Ambil ukuran tabel sebelum cleanup
before, err := fetchSizeStat(ctx, db)
if err != nil {
log.Fatalf("failed to fetch initial size: %v", err)
}
summary.BeforeSize = before
// Hitung kandidat row
candidate, err := countCandidates(ctx, db, cutoff)
if err != nil {
log.Fatalf("failed to count candidates: %v", err)
}
summary.CandidateRows = candidate
if candidate == 0 {
summary.Mode = modeLabel(opts.Apply)
summary.DurationSeconds = time.Since(start).Seconds()
fmt.Printf("No RELEASED rows older than %d days found. Nothing to do.\n", opts.RetentionDays)
render(opts.Output, summary)
return
}
if !opts.Apply {
summary.Mode = "DRY_RUN"
summary.DurationSeconds = time.Since(start).Seconds()
render(opts.Output, summary)
fmt.Println()
fmt.Println("Re-run with -apply to actually delete the rows above.")
return
}
summary.Mode = "APPLY"
deleted, batches, err := applyCleanup(ctx, db, cutoff, opts.BatchSize)
summary.DeletedRows = deleted
summary.BatchesExecuted = batches
if err != nil {
summary.OverallStatus = "FAIL"
render(opts.Output, summary)
log.Fatalf("apply failed after %d batches (%d rows deleted): %v", batches, deleted, err)
}
// VACUUM ANALYZE supaya space benar-benar dibebaskan ke OS
if !opts.SkipVacuum {
if err := runVacuum(ctx, db); err != nil {
// VACUUM gagal jangan-mengaborkan, log saja
log.Printf("WARN: VACUUM ANALYZE gagal: %v", err)
} else {
summary.VacuumExecuted = true
}
}
after, err := fetchSizeStat(ctx, db)
if err != nil {
log.Printf("WARN: gagal ambil ukuran tabel setelah cleanup: %v", err)
} else {
summary.AfterSize = after
}
summary.DurationSeconds = time.Since(start).Seconds()
render(opts.Output, summary)
}
func parseFlags() (*options, error) {
var opts options
flag.BoolVar(&opts.Apply, "apply", false, "Apply deletion (omit for dry-run)")
flag.StringVar(&opts.Output, "output", outputTable, "Output format: table or json")
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Database sslmode override")
flag.IntVar(&opts.RetentionDays, "retention-days", 90, "Keep RELEASED rows newer than N days")
flag.IntVar(&opts.BatchSize, "batch-size", 10000, "Rows deleted per transaction")
flag.BoolVar(&opts.SkipVacuum, "skip-vacuum", false, "Skip VACUUM ANALYZE after cleanup")
flag.Parse()
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
opts.DBSSLMode = strings.TrimSpace(opts.DBSSLMode)
if opts.Output == "" {
opts.Output = outputTable
}
if opts.Output != outputTable && opts.Output != outputJSON {
return nil, fmt.Errorf("unsupported --output=%s", opts.Output)
}
if opts.RetentionDays < 0 {
return nil, fmt.Errorf("retention-days must be >= 0, got %d", opts.RetentionDays)
}
if opts.BatchSize <= 0 {
return nil, fmt.Errorf("batch-size must be > 0, got %d", opts.BatchSize)
}
return &opts, nil
}
func countCandidates(ctx context.Context, db *gorm.DB, cutoff time.Time) (int64, error) {
var count int64
err := db.WithContext(ctx).
Table("stock_allocations").
Where("status = ?", entities.StockAllocationStatusReleased).
Where("released_at IS NOT NULL AND released_at < ?", cutoff).
Count(&count).Error
return count, err
}
func applyCleanup(ctx context.Context, db *gorm.DB, cutoff time.Time, batchSize int) (int64, int, error) {
var totalDeleted int64
batches := 0
for {
var affected int64
err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// Pakai CTE supaya LIMIT bisa dipakai bersama DELETE di PostgreSQL.
// `released_at IS NOT NULL` defensif — rows lama dari migrasi mungkin
// NULL meski status=RELEASED.
res := tx.Exec(`
DELETE FROM stock_allocations
WHERE id IN (
SELECT id FROM stock_allocations
WHERE status = ?
AND released_at IS NOT NULL
AND released_at < ?
ORDER BY id ASC
LIMIT ?
)
`, entities.StockAllocationStatusReleased, cutoff, batchSize)
if res.Error != nil {
return res.Error
}
affected = res.RowsAffected
return nil
})
if err != nil {
return totalDeleted, batches, err
}
if affected == 0 {
break
}
totalDeleted += affected
batches++
log.Printf("batch %d: deleted %d rows (running total: %d)", batches, affected, totalDeleted)
if affected < int64(batchSize) {
break
}
}
return totalDeleted, batches, nil
}
func runVacuum(ctx context.Context, db *gorm.DB) error {
// VACUUM tidak bisa di-jalankan dalam transaksi.
// gorm SkipDefaultTransaction sudah true, tapi tetap aman menggunakan raw DB.
sqlDB, err := db.DB()
if err != nil {
return err
}
_, err = sqlDB.ExecContext(ctx, "VACUUM ANALYZE stock_allocations")
return err
}
func fetchSizeStat(ctx context.Context, db *gorm.DB) (sizeStat, error) {
var stat sizeStat
err := db.WithContext(ctx).Raw(`
SELECT
pg_size_pretty(pg_relation_size('stock_allocations')) AS table_size,
pg_size_pretty(pg_total_relation_size('stock_allocations')) AS total_size,
(SELECT COUNT(*) FROM stock_allocations)::bigint AS row_count
`).Scan(&stat).Error
return stat, err
}
func modeLabel(apply bool) string {
if apply {
return "APPLY"
}
return "DRY_RUN"
}
func render(mode string, summary runSummary) {
if mode == outputJSON {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
_ = enc.Encode(summary)
return
}
fmt.Printf("=== Cleanup RELEASED stock_allocations ===\n")
fmt.Printf("Mode : %s\n", summary.Mode)
fmt.Printf("Retention days : %d (cutoff < %s)\n", summary.RetentionDays, summary.CutoffTime)
fmt.Printf("Batch size : %d\n", summary.BatchSize)
fmt.Printf("Candidate rows : %d\n", summary.CandidateRows)
fmt.Printf("\n--- Before ---\n")
fmt.Printf("Total rows : %d\n", summary.BeforeSize.RowCount)
fmt.Printf("Table size : %s\n", summary.BeforeSize.TableSize)
fmt.Printf("Total size (idx) : %s\n", summary.BeforeSize.TotalSize)
if summary.Mode == "APPLY" {
fmt.Printf("\n--- Apply ---\n")
fmt.Printf("Deleted rows : %d\n", summary.DeletedRows)
fmt.Printf("Batches executed : %d\n", summary.BatchesExecuted)
fmt.Printf("VACUUM executed : %v\n", summary.VacuumExecuted)
if summary.AfterSize.RowCount > 0 || summary.AfterSize.TableSize != "" {
fmt.Printf("\n--- After ---\n")
fmt.Printf("Total rows : %d\n", summary.AfterSize.RowCount)
fmt.Printf("Table size : %s\n", summary.AfterSize.TableSize)
fmt.Printf("Total size (idx) : %s\n", summary.AfterSize.TotalSize)
}
}
fmt.Printf("\nDuration : %.2fs\n", summary.DurationSeconds)
fmt.Printf("Overall status : %s\n", summary.OverallStatus)
}
+484
View File
@@ -0,0 +1,484 @@
// Command reconcile-fifo-total-used memperbaiki "phantom total_used" pada
// stockable lot FIFO v2 (recording_eggs, stock_transfer_details, dst.).
//
// LATAR BELAKANG
// Sebelum fix di population_allocation.go, ReleaseByUsable melepas SEMUA alokasi
// CONSUME sebuah usable (termasuk RECORDING_EGG / STOCK_TRANSFER_IN) tanpa
// men-decrement total_used stockable-nya. Akibatnya total_used "nyangkut" lebih
// besar dari jumlah alokasi ACTIVE yang membackup-nya (phantom) → available
// dihitung 0 padahal stok fisik ada → Delivery Order telur nyangkut di pending.
//
// PERBAIKAN
// Sumber kebenaran konsumsi = stock_allocations status ACTIVE & purpose CONSUME.
// Command ini menyetel ulang total_used setiap lot = SUM(alokasi ACTIVE CONSUME
// untuk lot itu), lalu menjalankan FIFO v2 Reflow per (PW, flag group) sehingga
// pending dialokasi ulang ke stok yang kini available dan product_warehouses.qty
// dihitung ulang.
//
// PENTING: jalankan command ini SETELAH fix kode (population_allocation.go)
// ter-deploy, dan SEBELUM mengaktifkan blok over-sell telur.
//
// Cara pakai:
//
// go run ./cmd/reconcile-fifo-total-used/ -pw=1292 # dry-run 1 PW
// go run ./cmd/reconcile-fifo-total-used/ -pw=1292 -apply # apply 1 PW
// go run ./cmd/reconcile-fifo-total-used/ -pw=1292,1296,1268 -apply
// go run ./cmd/reconcile-fifo-total-used/ -pw=1292 -apply -output=json
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
"time"
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
"gitlab.com/mbugroup/lti-api.git/internal/config"
"gitlab.com/mbugroup/lti-api.git/internal/database"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
)
const (
outputTable = "table"
outputJSON = "json"
)
var identifierRe = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
type options struct {
Apply bool
Output string
DBSSLMode string
PWs []uint
}
// stockableRule menggambarkan satu jenis stockable (mis. RECORDING_EGG) beserta
// tabel & kolom yang dipakai FIFO v2 untuk melacak stok masuk.
type stockableRule struct {
LegacyTypeKey string
SourceTable string
SourceIDColumn string
UsedQuantityCol string
ProductWarehouseCol string
QuantityCol string
ScopeSQL string
}
type pwResult struct {
ProductWarehouseID uint `json:"product_warehouse_id"`
Product string `json:"product"`
Warehouse string `json:"warehouse"`
FlagGroups []string `json:"flag_groups"`
QtyBefore float64 `json:"qty_before"`
TotalUsedBefore float64 `json:"total_used_before"`
ActiveConsume float64 `json:"active_consume"`
Phantom float64 `json:"phantom"`
PendingBefore float64 `json:"pending_before"`
QtyAfter float64 `json:"qty_after,omitempty"`
PendingAfter float64 `json:"pending_after,omitempty"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
}
type runSummary struct {
Mode string `json:"mode"`
TargetPWs []uint `json:"target_pws"`
Results []pwResult `json:"results"`
DurationSeconds float64 `json:"duration_seconds"`
OverallStatus string `json:"overall_status"`
}
func main() {
opts, err := parseFlags()
if err != nil {
log.Fatalf("invalid flags: %v", err)
}
if opts.DBSSLMode != "" {
config.DBSSLMode = opts.DBSSLMode
}
ctx := context.Background()
db := database.Connect(config.DBHost, config.DBName)
// Quiet the per-query GORM logging; this command emits its own summary and
// the reflow step would otherwise produce a very noisy query log.
db = db.Session(&gorm.Session{Logger: gormlogger.Default.LogMode(gormlogger.Silent)})
logger := logrus.New()
logger.SetLevel(logrus.WarnLevel)
svc := commonSvc.NewFifoStockV2Service(db, logger)
start := time.Now()
stockableRules, err := loadStockableRules(ctx, db)
if err != nil {
log.Fatalf("failed to load stockable route rules: %v", err)
}
pendingRules, err := loadUsablePendingRules(ctx, db)
if err != nil {
log.Fatalf("failed to load usable route rules: %v", err)
}
summary := runSummary{
Mode: modeLabel(opts.Apply),
TargetPWs: opts.PWs,
OverallStatus: "PASS",
}
for _, pw := range opts.PWs {
res := reconcilePW(ctx, db, svc, pw, stockableRules, pendingRules, opts.Apply)
if res.Status == "FAIL" {
summary.OverallStatus = "FAIL"
}
summary.Results = append(summary.Results, res)
}
summary.DurationSeconds = time.Since(start).Seconds()
render(opts.Output, summary)
if !opts.Apply {
fmt.Println("\nDry-run only. Re-run with -apply to reset total_used and reflow the PW(s) above.")
}
if summary.OverallStatus == "FAIL" {
os.Exit(1)
}
}
// reconcilePW mengukur kondisi PW, lalu (jika -apply) menyetel ulang total_used
// tiap lot dan menjalankan reflow, semuanya dalam satu transaksi.
func reconcilePW(
ctx context.Context,
db *gorm.DB,
svc commonSvc.FifoStockV2Service,
pw uint,
stockableRules []stockableRule,
pendingRules []stockableRule,
apply bool,
) pwResult {
res := pwResult{ProductWarehouseID: pw, Status: "OK"}
if name, wh, err := loadPWIdentity(ctx, db, pw); err != nil {
res.Status = "FAIL"
res.Error = fmt.Sprintf("load identity: %v", err)
return res
} else {
res.Product, res.Warehouse = name, wh
}
flagGroups, err := loadFlagGroups(ctx, db, pw)
if err != nil {
res.Status = "FAIL"
res.Error = fmt.Sprintf("load flag groups: %v", err)
return res
}
res.FlagGroups = flagGroups
res.QtyBefore, _ = loadQty(ctx, db, pw)
res.TotalUsedBefore, _ = sumStockableUsed(ctx, db, pw, stockableRules)
res.ActiveConsume, _ = loadActiveConsume(ctx, db, pw)
res.PendingBefore, _ = sumPending(ctx, db, pw, pendingRules)
res.Phantom = res.TotalUsedBefore - res.ActiveConsume
if !apply {
return res
}
err = db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
for _, rule := range stockableRules {
if err := recomputeUsed(ctx, tx, rule, pw); err != nil {
return fmt.Errorf("recompute %s: %w", rule.LegacyTypeKey, err)
}
}
for _, fg := range flagGroups {
if _, err := svc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
FlagGroupCode: fg,
ProductWarehouseID: pw,
Tx: tx,
}); err != nil {
return fmt.Errorf("reflow flag_group=%s: %w", fg, err)
}
}
return nil
})
if err != nil {
res.Status = "FAIL"
res.Error = err.Error()
return res
}
res.QtyAfter, _ = loadQty(ctx, db, pw)
res.PendingAfter, _ = sumPending(ctx, db, pw, pendingRules)
return res
}
func recomputeUsed(ctx context.Context, tx *gorm.DB, rule stockableRule, pw uint) error {
q := fmt.Sprintf(`
UPDATE %s t
SET %s = COALESCE((
SELECT SUM(sa.qty) FROM stock_allocations sa
WHERE sa.stockable_type = ?
AND sa.stockable_id = t.%s
AND sa.status = 'ACTIVE'
AND sa.allocation_purpose = 'CONSUME'
), 0)
WHERE t.%s = ?`, rule.SourceTable, rule.UsedQuantityCol, rule.SourceIDColumn, rule.ProductWarehouseCol)
if strings.TrimSpace(rule.ScopeSQL) != "" {
q += " AND (" + rule.ScopeSQL + ")"
}
return tx.WithContext(ctx).Exec(q, rule.LegacyTypeKey, pw).Error
}
// ---- loaders ----
func loadStockableRules(ctx context.Context, db *gorm.DB) ([]stockableRule, error) {
type row struct {
LegacyTypeKey string `gorm:"column:legacy_type_key"`
SourceTable string `gorm:"column:source_table"`
SourceIDColumn string `gorm:"column:source_id_column"`
UsedQuantityCol string `gorm:"column:used_quantity_col"`
ProductWarehouseCol string `gorm:"column:product_warehouse_col"`
QuantityCol string `gorm:"column:quantity_col"`
ScopeSQL string `gorm:"column:scope_sql"`
}
var rows []row
err := db.WithContext(ctx).
Table("fifo_stock_v2_route_rules").
Select("DISTINCT legacy_type_key, source_table, source_id_column, COALESCE(used_quantity_col,'') AS used_quantity_col, product_warehouse_col, COALESCE(quantity_col,'') AS quantity_col, COALESCE(scope_sql,'') AS scope_sql").
Where("lane = ? AND is_active = TRUE", "STOCKABLE").
Where("used_quantity_col IS NOT NULL AND used_quantity_col <> ''").
Scan(&rows).Error
if err != nil {
return nil, err
}
out := make([]stockableRule, 0, len(rows))
seen := map[string]bool{}
for _, r := range rows {
if !validIdentifiers(r.SourceTable, r.SourceIDColumn, r.UsedQuantityCol, r.ProductWarehouseCol) {
return nil, fmt.Errorf("unsafe identifier in route rule %s (table=%s used=%s pw=%s)", r.LegacyTypeKey, r.SourceTable, r.UsedQuantityCol, r.ProductWarehouseCol)
}
key := r.LegacyTypeKey + "|" + r.SourceTable + "|" + r.UsedQuantityCol + "|" + r.ProductWarehouseCol
if seen[key] {
continue
}
seen[key] = true
out = append(out, stockableRule(r))
}
return out, nil
}
func loadUsablePendingRules(ctx context.Context, db *gorm.DB) ([]stockableRule, error) {
type row struct {
SourceTable string `gorm:"column:source_table"`
ProductWarehouseCol string `gorm:"column:product_warehouse_col"`
PendingCol string `gorm:"column:pending_quantity_col"`
ScopeSQL string `gorm:"column:scope_sql"`
}
var rows []row
err := db.WithContext(ctx).
Table("fifo_stock_v2_route_rules").
Select("DISTINCT source_table, product_warehouse_col, pending_quantity_col, COALESCE(scope_sql,'') AS scope_sql").
Where("lane = ? AND is_active = TRUE", "USABLE").
Where("pending_quantity_col IS NOT NULL AND pending_quantity_col <> ''").
Scan(&rows).Error
if err != nil {
return nil, err
}
out := make([]stockableRule, 0, len(rows))
seen := map[string]bool{}
for _, r := range rows {
if !validIdentifiers(r.SourceTable, r.ProductWarehouseCol, r.PendingCol) {
return nil, fmt.Errorf("unsafe identifier in usable rule (table=%s pw=%s pending=%s)", r.SourceTable, r.ProductWarehouseCol, r.PendingCol)
}
key := r.SourceTable + "|" + r.PendingCol + "|" + r.ProductWarehouseCol
if seen[key] {
continue
}
seen[key] = true
out = append(out, stockableRule{
SourceTable: r.SourceTable,
ProductWarehouseCol: r.ProductWarehouseCol,
UsedQuantityCol: r.PendingCol, // reuse field as the column to SUM
ScopeSQL: r.ScopeSQL,
})
}
return out, nil
}
func loadPWIdentity(ctx context.Context, db *gorm.DB, pw uint) (string, string, error) {
type row struct {
Product string `gorm:"column:product"`
Warehouse string `gorm:"column:warehouse"`
}
var out row
err := db.WithContext(ctx).
Table("product_warehouses pw").
Select("p.name AS product, w.name AS warehouse").
Joins("JOIN products p ON p.id = pw.product_id").
Joins("JOIN warehouses w ON w.id = pw.warehouse_id").
Where("pw.id = ?", pw).
Take(&out).Error
return out.Product, out.Warehouse, err
}
func loadFlagGroups(ctx context.Context, db *gorm.DB, pw uint) ([]string, error) {
var groups []string
err := db.WithContext(ctx).
Table("stock_allocations").
Distinct("flag_group_code").
Where("product_warehouse_id = ? AND flag_group_code IS NOT NULL AND flag_group_code <> ''", pw).
Order("flag_group_code ASC").
Scan(&groups).Error
return groups, err
}
func loadQty(ctx context.Context, db *gorm.DB, pw uint) (float64, error) {
var v float64
err := db.WithContext(ctx).
Table("product_warehouses").
Select("COALESCE(qty,0)").
Where("id = ?", pw).
Scan(&v).Error
return v, err
}
func loadActiveConsume(ctx context.Context, db *gorm.DB, pw uint) (float64, error) {
var v float64
err := db.WithContext(ctx).
Table("stock_allocations").
Select("COALESCE(SUM(qty),0)").
Where("product_warehouse_id = ? AND status = 'ACTIVE' AND allocation_purpose = 'CONSUME'", pw).
Scan(&v).Error
return v, err
}
func sumStockableUsed(ctx context.Context, db *gorm.DB, pw uint, rules []stockableRule) (float64, error) {
total := 0.0
for _, rule := range rules {
v, err := sumColumn(ctx, db, rule.SourceTable, rule.UsedQuantityCol, rule.ProductWarehouseCol, rule.ScopeSQL, pw)
if err != nil {
return total, err
}
total += v
}
return total, nil
}
func sumPending(ctx context.Context, db *gorm.DB, pw uint, rules []stockableRule) (float64, error) {
total := 0.0
for _, rule := range rules {
v, err := sumColumn(ctx, db, rule.SourceTable, rule.UsedQuantityCol, rule.ProductWarehouseCol, rule.ScopeSQL, pw)
if err != nil {
return total, err
}
total += v
}
return total, nil
}
func sumColumn(ctx context.Context, db *gorm.DB, table, col, pwCol, scope string, pw uint) (float64, error) {
q := fmt.Sprintf("SELECT COALESCE(SUM(%s),0) FROM %s WHERE %s = ?", col, table, pwCol)
if strings.TrimSpace(scope) != "" {
q += " AND (" + scope + ")"
}
var v float64
err := db.WithContext(ctx).Raw(q, pw).Scan(&v).Error
return v, err
}
// ---- flags / render ----
func parseFlags() (*options, error) {
var opts options
var pwsRaw string
flag.BoolVar(&opts.Apply, "apply", false, "Apply the reconciliation (omit for dry-run)")
flag.StringVar(&opts.Output, "output", outputTable, "Output format: table or json")
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Database sslmode override")
flag.StringVar(&pwsRaw, "pw", "", "Comma-separated product_warehouse ids to reconcile (required)")
flag.Parse()
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
if opts.Output == "" {
opts.Output = outputTable
}
if opts.Output != outputTable && opts.Output != outputJSON {
return nil, fmt.Errorf("unsupported --output=%s", opts.Output)
}
pwsRaw = strings.TrimSpace(pwsRaw)
if pwsRaw == "" {
return nil, fmt.Errorf("-pw is required (e.g. -pw=1292 or -pw=1292,1296)")
}
for _, part := range strings.Split(pwsRaw, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
id, err := strconv.ParseUint(part, 10, 64)
if err != nil || id == 0 {
return nil, fmt.Errorf("invalid product_warehouse id %q", part)
}
opts.PWs = append(opts.PWs, uint(id))
}
if len(opts.PWs) == 0 {
return nil, fmt.Errorf("no valid product_warehouse ids parsed from -pw")
}
return &opts, nil
}
func validIdentifiers(ids ...string) bool {
for _, id := range ids {
if !identifierRe.MatchString(id) {
return false
}
}
return true
}
func modeLabel(apply bool) string {
if apply {
return "APPLY"
}
return "DRY_RUN"
}
func render(mode string, summary runSummary) {
if mode == outputJSON {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
_ = enc.Encode(summary)
return
}
fmt.Printf("=== Reconcile FIFO total_used ===\n")
fmt.Printf("Mode : %s\n", summary.Mode)
for _, r := range summary.Results {
fmt.Printf("\n--- PW %d (%s @ %s) [%s] ---\n", r.ProductWarehouseID, r.Product, r.Warehouse, r.Status)
if r.Error != "" {
fmt.Printf("ERROR : %s\n", r.Error)
}
fmt.Printf("Flag groups : %s\n", strings.Join(r.FlagGroups, ", "))
fmt.Printf("qty (before) : %.3f\n", r.QtyBefore)
fmt.Printf("Σ total_used : %.3f\n", r.TotalUsedBefore)
fmt.Printf("Σ active CONSUME: %.3f\n", r.ActiveConsume)
fmt.Printf("PHANTOM : %.3f (total_used yang akan dilepas)\n", r.Phantom)
fmt.Printf("pending (before): %.3f\n", r.PendingBefore)
if summary.Mode == "APPLY" && r.Status == "OK" {
fmt.Printf("qty (after) : %.3f\n", r.QtyAfter)
fmt.Printf("pending (after) : %.3f\n", r.PendingAfter)
}
}
fmt.Printf("\nDuration : %.2fs\n", summary.DurationSeconds)
fmt.Printf("Overall status : %s\n", summary.OverallStatus)
}
@@ -112,7 +112,7 @@ type HppV2CostRepository interface {
GetFarmDepreciationSnapshotByProjectFlockIDAndPeriod(ctx context.Context, projectFlockID uint, periodDate time.Time) (*HppV2FarmDepreciationSnapshotRow, error) GetFarmDepreciationSnapshotByProjectFlockIDAndPeriod(ctx context.Context, projectFlockID uint, periodDate time.Time) (*HppV2FarmDepreciationSnapshotRow, error)
GetEarliestChickInDateByProjectFlockID(ctx context.Context, projectFlockID uint) (*time.Time, error) GetEarliestChickInDateByProjectFlockID(ctx context.Context, projectFlockID uint) (*time.Time, error)
GetChickinPopulationByPFKForFarm(ctx context.Context, projectFlockID uint) (map[uint]float64, error) GetChickinPopulationByPFKForFarm(ctx context.Context, projectFlockID uint) (map[uint]float64, error)
GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, error) GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, map[string]*time.Time, error)
ListUsageCostRowsByProductFlags(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string, date *time.Time) ([]HppV2UsageCostRow, error) ListUsageCostRowsByProductFlags(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string, date *time.Time) ([]HppV2UsageCostRow, error)
ListAdjustmentCostRowsByProductFlags(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string, date *time.Time) ([]HppV2AdjustmentCostRow, error) ListAdjustmentCostRowsByProductFlags(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string, date *time.Time) ([]HppV2AdjustmentCostRow, error)
ListExpenseRealizationRowsByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time, ekspedisi bool) ([]HppV2ExpenseCostRow, error) ListExpenseRealizationRowsByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time, ekspedisi bool) ([]HppV2ExpenseCostRow, error)
@@ -466,28 +466,30 @@ func (r *HppV2RepositoryImpl) GetMultiplicationPercentages(
ctx context.Context, ctx context.Context,
houseTypes []string, houseTypes []string,
maxDay int, maxDay int,
) (map[string]map[int]float64, error) { ) (map[string]map[int]float64, map[string]*time.Time, error) {
result := make(map[string]map[int]float64) result := make(map[string]map[int]float64)
effectiveDates := make(map[string]*time.Time)
if len(houseTypes) == 0 || maxDay <= 0 { if len(houseTypes) == 0 || maxDay <= 0 {
return result, nil return result, effectiveDates, nil
} }
type row struct { type row struct {
HouseType string HouseType string
Day int Day int
MultiplicationPercentage float64 MultiplicationPercentage float64
EffectiveDate *time.Time
} }
rows := make([]row, 0) rows := make([]row, 0)
err := r.db.WithContext(ctx).Raw(` err := r.db.WithContext(ctx).Raw(`
SELECT DISTINCT ON (house_type::text, day) SELECT DISTINCT ON (house_type::text, day)
house_type::text AS house_type, day, multiplication_percentage house_type::text AS house_type, day, multiplication_percentage, effective_date
FROM house_depreciation_standards FROM house_depreciation_standards
WHERE house_type::text IN ? AND day <= ? WHERE house_type::text IN ? AND day <= ?
ORDER BY house_type, day, effective_date DESC NULLS LAST ORDER BY house_type, day, effective_date DESC NULLS LAST
`, houseTypes, maxDay).Scan(&rows).Error `, houseTypes, maxDay).Scan(&rows).Error
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
for _, item := range rows { for _, item := range rows {
@@ -495,9 +497,12 @@ func (r *HppV2RepositoryImpl) GetMultiplicationPercentages(
result[item.HouseType] = make(map[int]float64) result[item.HouseType] = make(map[int]float64)
} }
result[item.HouseType][item.Day] = item.MultiplicationPercentage result[item.HouseType][item.Day] = item.MultiplicationPercentage
if _, tracked := effectiveDates[item.HouseType]; !tracked {
effectiveDates[item.HouseType] = item.EffectiveDate
}
} }
return result, nil return result, effectiveDates, nil
} }
func (r *HppV2RepositoryImpl) ListUsageCostRowsByProductFlags( func (r *HppV2RepositoryImpl) ListUsageCostRowsByProductFlags(
@@ -0,0 +1,393 @@
package service
import (
"context"
"fmt"
"math"
"strings"
"time"
"github.com/sirupsen/logrus"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"gorm.io/gorm"
)
// ParentKind enumerasi parent yang punya grand_total dari SUM children.
type ParentKind string
const (
ParentKindPurchase ParentKind = "PURCHASE"
ParentKindMarketing ParentKind = "MARKETING"
ParentKindExpense ParentKind = "EXPENSE"
)
// AllocationKind enumerasi sub-row anak target FIFO allocation.
type AllocationKind string
const (
AllocKindPurchaseItem AllocationKind = "PURCHASE_ITEM"
AllocKindMarketingDeliveryProduct AllocationKind = "MDP"
AllocKindExpenseRealization AllocationKind = "EXPENSE_REALIZATION"
)
// fifoEpsilon untuk float comparison saat FIFO matching.
const fifoEpsilon = 0.001
// FifoPaymentService meng-orchestrate FIFO allocation antara payments dan
// sub-row anak (purchase_items / marketing_delivery_products / expense_realizations).
type FifoPaymentService interface {
// ReallocateForParty wipe allocations untuk semua payment party tsb,
// lalu re-FIFO dari history (sort children by date ASC, payments by payment_date ASC).
// Caller WAJIB pass tx untuk konsistensi dengan mutasi upstream.
ReallocateForParty(ctx context.Context, tx *gorm.DB, partyType string, partyID uint) error
// RecomputeGrandTotal refresh parent.grand_total = SUM children eligible amount.
RecomputeGrandTotal(ctx context.Context, tx *gorm.DB, kind ParentKind, parentID uint) error
}
type fifoPaymentService struct {
db *gorm.DB
logger *logrus.Logger
}
func NewFifoPaymentService(db *gorm.DB, logger *logrus.Logger) FifoPaymentService {
if logger == nil {
logger = logrus.StandardLogger()
}
return &fifoPaymentService{db: db, logger: logger}
}
func (s *fifoPaymentService) txOrDB(tx *gorm.DB) *gorm.DB {
if tx != nil {
return tx
}
return s.db
}
type childRow struct {
Kind AllocationKind
ChildID uint64
Amount float64
Remaining float64
}
type paymentRow struct {
ID uint
Nominal float64
Date time.Time
}
// ReallocateForParty acquire advisory lock then perform full re-FIFO.
// Jika tx nil, function buka transaction sendiri (advisory lock harus dalam TX).
func (s *fifoPaymentService) ReallocateForParty(ctx context.Context, tx *gorm.DB, partyType string, partyID uint) error {
if partyID == 0 {
return nil
}
party := strings.ToUpper(strings.TrimSpace(partyType))
if party != string(utils.PaymentPartyCustomer) && party != string(utils.PaymentPartySupplier) {
return fmt.Errorf("fifoPayment: invalid party_type %q", partyType)
}
if tx == nil {
return s.db.WithContext(ctx).Transaction(func(innerTx *gorm.DB) error {
return s.reallocateInTx(ctx, innerTx, party, partyID)
})
}
return s.reallocateInTx(ctx, tx, party, partyID)
}
func (s *fifoPaymentService) reallocateInTx(ctx context.Context, tx *gorm.DB, party string, partyID uint) error {
db := tx.WithContext(ctx)
// Advisory lock per (party_type, party_id) — 1-arg form (bigint).
// Postgres 2-arg form butuh kedua param int4, sedangkan party_id bisa lebih besar.
lockKey := fmt.Sprintf("payment_alloc:%s:%d", party, partyID)
if err := db.Exec("SELECT pg_advisory_xact_lock(hashtext(?)::bigint)", lockKey).Error; err != nil {
return fmt.Errorf("fifoPayment: advisory lock: %w", err)
}
// Wipe existing allocations untuk semua payment party tsb
if err := db.Exec(`
DELETE FROM payment_allocations
WHERE payment_id IN (
SELECT id FROM payments
WHERE party_type = ? AND party_id = ? AND deleted_at IS NULL
)
`, party, partyID).Error; err != nil {
return fmt.Errorf("fifoPayment: wipe allocations: %w", err)
}
children, err := s.fetchChildren(ctx, db, party, partyID)
if err != nil {
return err
}
if len(children) == 0 {
return nil
}
// Fetch SEMUA payments termasuk SALDO_AWAL agar allocation tercatat di DB
// (SaldoAwal opening credit harus consume oldest debts; tanpa allocation row,
// debt yang ter-cover SaldoAwal akan tampak "Belum Lunas" di report).
payments, err := s.fetchAllPayments(ctx, db, party, partyID)
if err != nil {
return err
}
// Greedy: per payment, alokasi ke children tertua dengan remaining > 0
allocs := make([]entity.PaymentAllocation, 0, len(payments))
now := time.Now()
for _, pay := range payments {
remaining := pay.Nominal
if remaining <= fifoEpsilon {
continue
}
for i := range children {
if remaining <= fifoEpsilon {
break
}
if children[i].Remaining <= fifoEpsilon {
continue
}
used := math.Min(remaining, children[i].Remaining)
children[i].Remaining -= used
remaining -= used
alloc := entity.PaymentAllocation{
PaymentId: pay.ID,
Amount: used,
AllocatedAt: now,
}
switch children[i].Kind {
case AllocKindPurchaseItem:
id := uint(children[i].ChildID)
alloc.PurchaseItemId = &id
case AllocKindMarketingDeliveryProduct:
id := uint(children[i].ChildID)
alloc.MarketingDeliveryProductId = &id
case AllocKindExpenseRealization:
id := children[i].ChildID
alloc.ExpenseRealizationId = &id
}
allocs = append(allocs, alloc)
}
}
if len(allocs) == 0 {
return nil
}
// Batch insert allocations
if err := db.CreateInBatches(&allocs, 500).Error; err != nil {
return fmt.Errorf("fifoPayment: insert allocations: %w", err)
}
return nil
}
// fetchChildren return eligible sub-rows sorted by date ASC, id ASC.
func (s *fifoPaymentService) fetchChildren(ctx context.Context, db *gorm.DB, party string, partyID uint) ([]childRow, error) {
if party == string(utils.PaymentPartySupplier) {
return s.fetchSupplierChildren(ctx, db, partyID)
}
return s.fetchCustomerChildren(ctx, db, partyID)
}
func (s *fifoPaymentService) fetchSupplierChildren(ctx context.Context, db *gorm.DB, supplierID uint) ([]childRow, error) {
// purchase_items eligible: purchases approval latest step >= Receiving (4), action != REJECTED, received_date IS NOT NULL
var purchaseRows []chronoRow
purchaseSQL := `
SELECT 'PURCHASE_ITEM' AS kind,
pi.id::BIGINT AS child_id,
pi.total_price AS amount,
pi.received_date AS sort_date,
pi.id::BIGINT AS sort_id
FROM purchase_items pi
JOIN purchases p ON p.id = pi.purchase_id
JOIN LATERAL (
SELECT a.step_number, a.action
FROM approvals a
WHERE a.approvable_type = ? AND a.approvable_id = p.id
ORDER BY a.action_at DESC, a.id DESC
LIMIT 1
) la ON TRUE
WHERE p.supplier_id = ?
AND p.deleted_at IS NULL
AND pi.received_date IS NOT NULL
AND la.step_number >= ?
AND (la.action IS NULL OR la.action <> ?)
AND pi.total_price > 0
ORDER BY pi.received_date ASC, pi.id ASC
`
if err := db.WithContext(ctx).Raw(purchaseSQL,
string(utils.ApprovalWorkflowPurchase),
supplierID,
uint16(utils.PurchaseStepReceiving),
string(entity.ApprovalActionRejected),
).Scan(&purchaseRows).Error; err != nil {
return nil, fmt.Errorf("fifoPayment: fetch purchase items: %w", err)
}
// expense_realizations via expense_nonstocks → expenses, approval latest step >= Realisasi (5)
// Sort pakai e.transaction_date (bukan realization_date) supaya FIFO match dengan tanggal yang
// dipakai report sebagai "tanggal dokumen" — user assume FIFO = lunasi yang transaction_date paling tua dulu.
var expenseRows []chronoRow
expenseSQL := `
SELECT 'EXPENSE_REALIZATION' AS kind,
er.id::BIGINT AS child_id,
(er.qty * er.price) AS amount,
e.transaction_date AS sort_date,
er.id::BIGINT AS sort_id
FROM expense_realizations er
JOIN expense_nonstocks en ON en.id = er.expense_nonstock_id
JOIN expenses e ON e.id = en.expense_id
JOIN LATERAL (
SELECT a.step_number, a.action
FROM approvals a
WHERE a.approvable_type = ? AND a.approvable_id = e.id
ORDER BY a.action_at DESC, a.id DESC
LIMIT 1
) la ON TRUE
WHERE e.supplier_id = ?
AND e.deleted_at IS NULL
AND la.step_number >= ?
AND (la.action IS NULL OR la.action <> ?)
AND (er.qty * er.price) > 0
ORDER BY e.transaction_date ASC, e.id ASC, er.id ASC
`
if err := db.WithContext(ctx).Raw(expenseSQL,
string(utils.ApprovalWorkflowExpense),
supplierID,
uint16(utils.ExpenseStepRealisasi),
string(entity.ApprovalActionRejected),
).Scan(&expenseRows).Error; err != nil {
return nil, fmt.Errorf("fifoPayment: fetch expense realizations: %w", err)
}
// Merge in chronological order (kedua list sudah sorted; merge stable)
merged := mergeSortedByDate(purchaseRows, expenseRows)
out := make([]childRow, 0, len(merged))
for _, r := range merged {
out = append(out, childRow{
Kind: AllocationKind(r.Kind),
ChildID: r.ChildID,
Amount: r.Amount,
Remaining: r.Amount,
})
}
return out, nil
}
func (s *fifoPaymentService) fetchCustomerChildren(ctx context.Context, db *gorm.DB, customerID uint) ([]childRow, error) {
var mdpRows []chronoRow
sql := `
SELECT 'MDP' AS kind,
mdp.id::BIGINT AS child_id,
mdp.total_price AS amount,
mdp.delivery_date AS sort_date,
mdp.id::BIGINT AS sort_id
FROM marketing_delivery_products mdp
JOIN marketing_products mp ON mp.id = mdp.marketing_product_id
JOIN marketings m ON m.id = mp.marketing_id
WHERE m.customer_id = ?
AND m.deleted_at IS NULL
AND mdp.delivery_date IS NOT NULL
AND mdp.total_price > 0
ORDER BY mdp.delivery_date ASC, mdp.id ASC
`
if err := db.WithContext(ctx).Raw(sql, customerID).Scan(&mdpRows).Error; err != nil {
return nil, fmt.Errorf("fifoPayment: fetch marketing delivery products: %w", err)
}
out := make([]childRow, 0, len(mdpRows))
for _, r := range mdpRows {
out = append(out, childRow{
Kind: AllocationKind(r.Kind),
ChildID: r.ChildID,
Amount: r.Amount,
Remaining: r.Amount,
})
}
return out, nil
}
// fetchAllPayments return SEMUA payments (termasuk SALDO_AWAL) sort by payment_date ASC, id ASC.
// SALDO_AWAL diperlakukan sebagai payment tertua agar opening credit otomatis consume oldest debts via FIFO.
func (s *fifoPaymentService) fetchAllPayments(ctx context.Context, db *gorm.DB, party string, partyID uint) ([]paymentRow, error) {
var rows []paymentRow
sql := `
SELECT id, nominal, payment_date AS date
FROM payments
WHERE party_type = ? AND party_id = ?
AND deleted_at IS NULL
AND nominal > 0
ORDER BY payment_date ASC, id ASC
`
if err := db.WithContext(ctx).Raw(sql, party, partyID).Scan(&rows).Error; err != nil {
return nil, fmt.Errorf("fifoPayment: fetch payments: %w", err)
}
return rows, nil
}
// RecomputeGrandTotal refresh parent.grand_total dari SUM children eligible amount.
func (s *fifoPaymentService) RecomputeGrandTotal(ctx context.Context, tx *gorm.DB, kind ParentKind, parentID uint) error {
db := s.txOrDB(tx).WithContext(ctx)
if parentID == 0 {
return nil
}
switch kind {
case ParentKindPurchase:
return db.Exec(`
UPDATE purchases p
SET grand_total = COALESCE((SELECT SUM(total_price) FROM purchase_items WHERE purchase_id = p.id), 0)
WHERE p.id = ?
`, parentID).Error
case ParentKindMarketing:
return db.Exec(`
UPDATE marketings m
SET grand_total = COALESCE((
SELECT SUM(mdp.total_price)
FROM marketing_delivery_products mdp
JOIN marketing_products mp ON mp.id = mdp.marketing_product_id
WHERE mp.marketing_id = m.id AND mdp.delivery_date IS NOT NULL
), 0)
WHERE m.id = ?
`, parentID).Error
case ParentKindExpense:
return db.Exec(`
UPDATE expenses e
SET grand_total = COALESCE((
SELECT SUM(er.qty * er.price)
FROM expense_realizations er
JOIN expense_nonstocks en ON en.id = er.expense_nonstock_id
WHERE en.expense_id = e.id
), 0)
WHERE e.id = ?
`, parentID).Error
default:
return fmt.Errorf("fifoPayment: unknown parent kind %q", kind)
}
}
// chronoRow row antara untuk merge sort children.
type chronoRow struct {
Kind string
ChildID uint64
Amount float64
SortDate time.Time
SortID uint64
}
func mergeSortedByDate(a, b []chronoRow) []chronoRow {
out := make([]chronoRow, 0, len(a)+len(b))
i, j := 0, 0
for i < len(a) && j < len(b) {
if a[i].SortDate.Before(b[j].SortDate) ||
(a[i].SortDate.Equal(b[j].SortDate) && a[i].SortID < b[j].SortID) {
out = append(out, a[i])
i++
} else {
out = append(out, b[j])
j++
}
}
out = append(out, a[i:]...)
out = append(out, b[j:]...)
return out
}
@@ -1390,7 +1390,7 @@ func (s *hppV2Service) buildNormalTransferDepreciationPart(
} }
houseType := NormalizeDepreciationHouseType(contextRow.HouseType) houseType := NormalizeDepreciationHouseType(contextRow.HouseType)
multiplicationByHouseType, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, scheduleDay) multiplicationByHouseType, effectiveDates, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, scheduleDay)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1407,6 +1407,11 @@ func (s *hppV2Service) buildNormalTransferDepreciationPart(
totalValueAfter := pulletCostDayN * multiplicationPercentage totalValueAfter := pulletCostDayN * multiplicationPercentage
depreciationPercent := (1.0 - multiplicationPercentage) * 100.0 depreciationPercent := (1.0 - multiplicationPercentage) * 100.0
var standardEffectiveDate string
if ed, ok := effectiveDates[houseType]; ok && ed != nil {
standardEffectiveDate = formatDateOnly(*ed)
}
return &HppV2ComponentPart{ return &HppV2ComponentPart{
Code: hppV2PartDepreciationNormal, Code: hppV2PartDepreciationNormal,
Title: "Normal Transfer", Title: "Normal Transfer",
@@ -1422,6 +1427,8 @@ func (s *hppV2Service) buildNormalTransferDepreciationPart(
"origin_date": formatDateOnly(*originDate), "origin_date": formatDateOnly(*originDate),
"transfer_date": formatDateOnly(transferInput.TransferDate), "transfer_date": formatDateOnly(transferInput.TransferDate),
"source_project_flock_id": transferInput.SourceProjectFlockID, "source_project_flock_id": transferInput.SourceProjectFlockID,
"standard_effective_date": standardEffectiveDate,
"kandang_population": transferInput.TransferQty,
}, },
References: []HppV2Reference{ References: []HppV2Reference{
{ {
@@ -1492,7 +1499,7 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
} }
houseType := NormalizeDepreciationHouseType(contextRow.HouseType) houseType := NormalizeDepreciationHouseType(contextRow.HouseType)
multiplicationByHouseType, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, reportScheduleDay) multiplicationByHouseType, effectiveDates, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, reportScheduleDay)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1511,6 +1518,11 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
depreciationPercent := (1.0 - multiplicationPercentage) * 100.0 depreciationPercent := (1.0 - multiplicationPercentage) * 100.0
_ = totalPulletCost _ = totalPulletCost
var standardEffectiveDate string
if ed, ok := effectiveDates[houseType]; ok && ed != nil {
standardEffectiveDate = formatDateOnly(*ed)
}
return &HppV2ComponentPart{ return &HppV2ComponentPart{
Code: hppV2PartDepreciationCutover, Code: hppV2PartDepreciationCutover,
Title: "Manual Cut-over", Title: "Manual Cut-over",
@@ -1530,6 +1542,8 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
"cutover_date": formatDateOnly(manualInput.CutoverDate), "cutover_date": formatDateOnly(manualInput.CutoverDate),
"manual_input_id": manualInput.ID, "manual_input_id": manualInput.ID,
"project_flock_kandang": projectFlockKandangId, "project_flock_kandang": projectFlockKandangId,
"standard_effective_date": standardEffectiveDate,
"kandang_population": kandangPopulation,
}, },
References: []HppV2Reference{ References: []HppV2Reference{
{ {
@@ -103,8 +103,9 @@ func (s *hppV2RepoStub) GetDepreciationPercents(_ context.Context, houseTypes []
// GetMultiplicationPercentages — alias yang sama dengan GetDepreciationPercents untuk match // GetMultiplicationPercentages — alias yang sama dengan GetDepreciationPercents untuk match
// interface HppV2CostRepository (interface dipakai method name baru ini). // interface HppV2CostRepository (interface dipakai method name baru ini).
func (s *hppV2RepoStub) GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, error) { func (s *hppV2RepoStub) GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, map[string]*time.Time, error) {
return s.GetDepreciationPercents(ctx, houseTypes, maxDay) vals, err := s.GetDepreciationPercents(ctx, houseTypes, maxDay)
return vals, make(map[string]*time.Time), err
} }
// GetChickinPopulationByPFKForFarm — return populasi per PFK dari satu project flock. // GetChickinPopulationByPFKForFarm — return populasi per PFK dari satu project flock.
@@ -45,7 +45,16 @@ func ReleasePopulationConsumptionByUsable(
} }
} }
return stockAllocationRepo.ReleaseByUsable(ctx, usableType, usableID, nil, nil) // Only release the PROJECT_FLOCK_POPULATION allocations here. Releasing the
// other CONSUME allocations of this usable (RECORDING_EGG, STOCK_TRANSFER_IN,
// PURCHASE_ITEMS, etc.) would orphan their stockable total_used because this
// path only restores total_used_qty for population lots — leaving the FIFO
// stock counters permanently inflated (phantom stock). Those stock
// allocations are owned by the FIFO Reflow/Rollback path, which decrements
// total_used correctly via adjustStockableUsedQuantity.
return stockAllocationRepo.ReleaseByUsable(ctx, usableType, usableID, nil, func(db *gorm.DB) *gorm.DB {
return db.Where("stockable_type = ?", fifo.StockableKeyProjectFlockPopulation.String())
})
} }
func AllocatePopulationConsumption( func AllocatePopulationConsumption(
@@ -1,3 +0,0 @@
-- Down migration: tidak ada cara restore TRUNCATE. Snapshot akan auto-regenerate on demand.
-- File kosong sengaja: rollback safe karena snapshot dianggap cache yang bisa di-regenerate.
SELECT 1;
@@ -1,10 +0,0 @@
-- Truncate semua farm_depreciation_snapshots agar di-recompute dengan logic baru:
-- 1. Multi-transfer per target kandang sekarang menghasilkan multiple parts (1 per transfer)
-- 2. Economic cutoff date sudah diupdate dari 19 minggu ke 25 minggu
-- 3. Format `components` JSON tetap kompatibel — yang berubah adalah jumlah entries (lebih banyak
-- untuk kandang multi-transfer)
--
-- Snapshot akan otomatis di-regenerate saat user request `/api/reports/expense/depreciation`
-- untuk period yang relevan.
TRUNCATE TABLE farm_depreciation_snapshots;
@@ -0,0 +1,99 @@
BEGIN;
-- ============================================================
-- Rollback dynamic via audit snapshots di schema `migration_audit.jamali_w10_*`.
-- Semua reverse dibaca dari snapshot yang dibuat oleh UP migration —
-- tidak ada IDs/qty yang hardcode. Robust terhadap data drift antara
-- dump time dan UP apply time (misalnya row baru warehouse_id=10
-- yang muncul setelah dump diambil).
--
-- LIMITASI: FK relinks di stock_logs / stock_allocations / recording_eggs /
-- marketing_products / dll. TIDAK direverse di sini (skip audit per-row
-- untuk hemat storage ~40MB). Setelah down, 9 PW W10 yang di-restore
-- akan kosong dari child rows (semua child masih pointing ke W25 PW
-- yang sebelumnya menerima merge). Untuk rollback penuh, restore DB
-- dari backup pre-migration.
-- ============================================================
-- Guard: pastikan audit tables ada (kalau tidak, fail-loud)
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = 'migration_audit'
AND table_name = 'jamali_w10_pw_deleted_snapshot'
) THEN
RAISE EXCEPTION 'Audit table migration_audit.jamali_w10_* tidak ditemukan. UP migration belum dijalankan atau audit sudah di-drop. Restore dari DB backup jika perlu.';
END IF;
END $$;
-- 1. Un-soft-delete warehouse 10 (kalau memang di-softdelete oleh UP)
UPDATE warehouses w
SET deleted_at = NULL, updated_at = NOW()
FROM migration_audit.jamali_w10_warehouse_softdeleted a
WHERE w.id = a.id;
-- 2. Un-soft-delete stock_transfers self-loop yang disoft-delete UP step 7.1
UPDATE stock_transfers st
SET deleted_at = NULL, updated_at = NOW()
FROM migration_audit.jamali_w10_st_softdeleted a
WHERE st.id = a.id;
-- 3. Reverse stock_transfers redirect (CASE-based dari snapshot was_from_w10/was_to_w10)
UPDATE stock_transfers st
SET from_warehouse_id = CASE WHEN a.was_from_w10 THEN 10 ELSE st.from_warehouse_id END,
to_warehouse_id = CASE WHEN a.was_to_w10 THEN 10 ELSE st.to_warehouse_id END,
updated_at = NOW()
FROM migration_audit.jamali_w10_st_redirected a
WHERE st.id = a.id;
-- 3b. Self-loop transfers (W10<->W25 awal) juga punya from_warehouse_id=25 atau
-- to_warehouse_id=25 setelah UP step 7.2. Karena snapshot jamali_w10_st_softdeleted
-- punya kolom from_warehouse_id & to_warehouse_id asli, pakai itu untuk reverse.
UPDATE stock_transfers st
SET from_warehouse_id = 10, updated_at = NOW()
FROM migration_audit.jamali_w10_st_softdeleted a
WHERE st.id = a.id AND a.from_warehouse_id = 10;
UPDATE stock_transfers st
SET to_warehouse_id = 10, updated_at = NOW()
FROM migration_audit.jamali_w10_st_softdeleted a
WHERE st.id = a.id AND a.to_warehouse_id = 10;
-- 4. Reverse purchase_items.warehouse_id 25 -> 10
UPDATE purchase_items
SET warehouse_id = 10
WHERE id IN (SELECT id FROM migration_audit.jamali_w10_purchase_items);
-- 5. Reverse W10-only PW (warehouse_id 25 -> 10, restore pfk asli dari snapshot)
UPDATE product_warehouses pw
SET warehouse_id = 10, project_flock_kandang_id = a.original_pfk
FROM migration_audit.jamali_w10_pw_w10only_snapshot a
WHERE pw.id = a.id;
-- 6. Subtract qty dari W25 PW (reverse merge)
-- WARNING: kalau W25 qty sudah dikonsumsi pasca-UP (sales/recording/dll),
-- hasil bisa negatif. Tidak ada CHECK constraint di product_warehouses.qty,
-- jadi silent. Operator harus verifikasi manual post-down:
-- SELECT id, qty FROM product_warehouses WHERE qty < 0;
UPDATE product_warehouses pw
SET qty = pw.qty - a.merged_qty
FROM migration_audit.jamali_w10_qty_merge a
WHERE pw.id = a.target_pw_id;
-- 7. Re-INSERT 9 W10 PW rows yang di-DELETE oleh UP (PK asli + qty asli)
INSERT INTO product_warehouses (id, product_id, warehouse_id, qty, project_flock_kandang_id)
SELECT id, product_id, 10, qty, project_flock_kandang_id
FROM migration_audit.jamali_w10_pw_deleted_snapshot;
-- 8. Cleanup audit tables (drop satu per satu, tidak wildcard untuk safety)
DROP TABLE migration_audit.jamali_w10_pw_deleted_snapshot;
DROP TABLE migration_audit.jamali_w10_qty_merge;
DROP TABLE migration_audit.jamali_w10_pw_w10only_snapshot;
DROP TABLE migration_audit.jamali_w10_st_softdeleted;
DROP TABLE migration_audit.jamali_w10_st_redirected;
DROP TABLE migration_audit.jamali_w10_purchase_items;
DROP TABLE migration_audit.jamali_w10_warehouse_softdeleted;
-- Schema migration_audit dipertahankan (bisa dipakai migration lain di masa depan)
COMMIT;
@@ -0,0 +1,241 @@
BEGIN;
-- ============================================================
-- Normalisasi warehouse 10 (Jamali NON_AKTIF) -> 25 (Gudang Farm Jamali)
-- Background: Dua warehouse LOKASI di area & lokasi sama (area_id=6,
-- location_id=16). W10 sudah ditandai NON_AKTIF tapi masih punya 13
-- product_warehouses, 3,590 stock_logs, ~790K stock_allocations,
-- 332 marketing_products, 17 purchase_items, dan 14 stock_transfers.
-- Migration ini konsolidasikan semua relasi ke W25 lalu soft-delete W10.
--
-- Klasifikasi data:
-- A. 9 product_warehouses W10 overlap dengan W25 (sama product_id, pfk=NULL)
-- -> merge qty ke W25, relink semua FK ke product_warehouses.id,
-- lalu DELETE W10 PW rows.
-- B. 4 product_warehouses W10-only -> UPDATE warehouse_id=25.
-- Rows 1188/1189/1190 punya pfk=98 (anomali LOKASI, seharusnya NULL
-- per aturan di CLAUDE.md [2026-05-06]) -> normalisasi sekalian.
-- C. 17 purchase_items.warehouse_id=10 -> UPDATE 25 (no unique conflict).
-- D. 3 stock_transfers W10<->W25 (PND-LTI-00107/00109/00119) akan jadi
-- self-loop W25<->W25 setelah merge -> soft-delete.
-- E. 12 stock_transfers EGG_FARM_CUTOVER to_warehouse_id=10 -> UPDATE 25.
-- F. warehouse_id=10 sendiri -> soft-delete.
--
-- UP membuat 7 snapshot table di schema `migration_audit.jamali_w10_*`
-- sebelum mutasi. DOWN baca snapshot itu untuk reverse dynamic (tidak
-- hardcode IDs/qty), sehingga apapun yang ada di production saat UP
-- dijalankan akan ter-audit dan ter-reverse. FK relinks
-- (stock_logs/stock_allocations/dll) TIDAK di-audit (storage ~40MB)
-- — limitation: tidak bisa di-reverse DOWN, full rollback = DB backup.
-- ============================================================
-- STEP -1: Buat schema audit + snapshot tables (idempotent rerun via DROP IF EXISTS)
CREATE SCHEMA IF NOT EXISTS migration_audit;
DROP TABLE IF EXISTS migration_audit.jamali_w10_pw_deleted_snapshot;
DROP TABLE IF EXISTS migration_audit.jamali_w10_qty_merge;
DROP TABLE IF EXISTS migration_audit.jamali_w10_pw_w10only_snapshot;
DROP TABLE IF EXISTS migration_audit.jamali_w10_st_softdeleted;
DROP TABLE IF EXISTS migration_audit.jamali_w10_st_redirected;
DROP TABLE IF EXISTS migration_audit.jamali_w10_purchase_items;
DROP TABLE IF EXISTS migration_audit.jamali_w10_warehouse_softdeleted;
-- Snapshot 9 W10 PW yang akan di-DELETE (overlap dgn W25, pfk=NULL)
CREATE TABLE migration_audit.jamali_w10_pw_deleted_snapshot AS
SELECT pw10.id, pw10.product_id, pw10.qty, pw10.project_flock_kandang_id
FROM product_warehouses pw10
JOIN product_warehouses pw25
ON pw25.product_id = pw10.product_id
AND pw25.warehouse_id = 25
AND pw25.project_flock_kandang_id IS NULL
WHERE pw10.warehouse_id = 10 AND pw10.project_flock_kandang_id IS NULL;
-- Snapshot qty delta per W25 target (untuk reverse subtract)
CREATE TABLE migration_audit.jamali_w10_qty_merge AS
SELECT pw25.id AS target_pw_id, pw10.id AS source_pw_id, pw10.qty AS merged_qty
FROM product_warehouses pw10
JOIN product_warehouses pw25
ON pw25.product_id = pw10.product_id
AND pw25.warehouse_id = 25
AND pw25.project_flock_kandang_id IS NULL
WHERE pw10.warehouse_id = 10 AND pw10.project_flock_kandang_id IS NULL;
-- Snapshot W10-only PW (yang akan di-UPDATE warehouse_id 10->25)
CREATE TABLE migration_audit.jamali_w10_pw_w10only_snapshot AS
SELECT pw10.id, pw10.project_flock_kandang_id AS original_pfk
FROM product_warehouses pw10
WHERE pw10.warehouse_id = 10
AND pw10.id NOT IN (SELECT id FROM migration_audit.jamali_w10_pw_deleted_snapshot);
-- Snapshot stock_transfers yang akan di-soft-delete (self-loop W10<->W25)
-- Simpan from/to_warehouse_id asli supaya DOWN bisa reverse direction tepat
CREATE TABLE migration_audit.jamali_w10_st_softdeleted AS
SELECT id, movement_number, from_warehouse_id, to_warehouse_id
FROM stock_transfers
WHERE deleted_at IS NULL
AND ((from_warehouse_id = 10 AND to_warehouse_id = 25)
OR (from_warehouse_id = 25 AND to_warehouse_id = 10));
-- Snapshot stock_transfers yang akan di-UPDATE (W10<->other, bukan self-loop)
CREATE TABLE migration_audit.jamali_w10_st_redirected AS
SELECT id,
(from_warehouse_id = 10) AS was_from_w10,
(to_warehouse_id = 10) AS was_to_w10
FROM stock_transfers
WHERE deleted_at IS NULL
AND (from_warehouse_id = 10 OR to_warehouse_id = 10)
AND id NOT IN (SELECT id FROM migration_audit.jamali_w10_st_softdeleted);
-- Snapshot purchase_items IDs (cheap, ~17 rows)
CREATE TABLE migration_audit.jamali_w10_purchase_items AS
SELECT id FROM purchase_items WHERE warehouse_id = 10;
-- Snapshot warehouses soft-delete flag (1 row, kalau memang masih aktif)
CREATE TABLE migration_audit.jamali_w10_warehouse_softdeleted AS
SELECT id FROM warehouses WHERE id = 10 AND deleted_at IS NULL;
-- STEP 0: Pre-check sanity (idempotent guards)
DO $$
DECLARE v_count INT;
BEGIN
SELECT COUNT(*) INTO v_count FROM warehouses
WHERE id IN (10, 25) AND type = 'LOKASI' AND area_id = 6 AND location_id = 16;
IF v_count <> 2 THEN
RAISE EXCEPTION 'Pre-check: warehouse 10/25 schema mismatch (got % rows)', v_count;
END IF;
SELECT COUNT(*) INTO v_count FROM purchase_items a
JOIN purchase_items b ON a.purchase_id = b.purchase_id
AND a.product_id = b.product_id
AND a.id <> b.id
WHERE a.warehouse_id = 10 AND b.warehouse_id = 25;
IF v_count > 0 THEN
RAISE EXCEPTION 'Pre-check: % purchase_items unique conflict (purchase_id,product_id)', v_count;
END IF;
END $$;
-- STEP 1: Merge qty W10 -> W25 untuk overlap (pfk=NULL)
UPDATE product_warehouses pw25
SET qty = pw25.qty + pw10.qty
FROM product_warehouses pw10
WHERE pw10.warehouse_id = 10 AND pw10.project_flock_kandang_id IS NULL
AND pw25.warehouse_id = 25 AND pw25.project_flock_kandang_id IS NULL
AND pw25.product_id = pw10.product_id;
-- STEP 2: Build temp mapping (W10 PW id -> W25 PW id) untuk overlap saja
CREATE TEMP TABLE _pw_map ON COMMIT DROP AS
SELECT pw10.id AS old_id, pw25.id AS new_id
FROM product_warehouses pw10
JOIN product_warehouses pw25
ON pw25.product_id = pw10.product_id
AND pw25.warehouse_id = 25
AND pw25.project_flock_kandang_id IS NULL
WHERE pw10.warehouse_id = 10 AND pw10.project_flock_kandang_id IS NULL;
CREATE INDEX ON _pw_map(old_id);
-- STEP 3: Relink semua FK ke product_warehouses.id (hanya rows di _pw_map)
UPDATE stock_logs SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE stock_logs.product_warehouse_id = m.old_id;
UPDATE stock_allocations SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE stock_allocations.product_warehouse_id = m.old_id;
UPDATE recording_eggs SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE recording_eggs.product_warehouse_id = m.old_id;
UPDATE recording_stocks SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE recording_stocks.product_warehouse_id = m.old_id;
UPDATE recording_depletions SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE recording_depletions.product_warehouse_id = m.old_id;
UPDATE recording_depletions SET source_product_warehouse_id = m.new_id
FROM _pw_map m WHERE recording_depletions.source_product_warehouse_id = m.old_id;
UPDATE adjustment_stocks SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE adjustment_stocks.product_warehouse_id = m.old_id;
UPDATE marketing_products SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE marketing_products.product_warehouse_id = m.old_id;
UPDATE marketing_delivery_products SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE marketing_delivery_products.product_warehouse_id = m.old_id;
UPDATE project_chickins SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE project_chickins.product_warehouse_id = m.old_id;
UPDATE project_chickin_details SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE project_chickin_details.product_warehouse_id = m.old_id;
UPDATE project_flock_populations SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE project_flock_populations.product_warehouse_id = m.old_id;
UPDATE laying_transfers SET source_product_warehouse_id = m.new_id
FROM _pw_map m WHERE laying_transfers.source_product_warehouse_id = m.old_id;
UPDATE laying_transfer_sources SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE laying_transfer_sources.product_warehouse_id = m.old_id;
UPDATE laying_transfer_targets SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE laying_transfer_targets.product_warehouse_id = m.old_id;
UPDATE stock_transfer_details SET source_product_warehouse_id = m.new_id
FROM _pw_map m WHERE stock_transfer_details.source_product_warehouse_id = m.old_id;
UPDATE stock_transfer_details SET dest_product_warehouse_id = m.new_id
FROM _pw_map m WHERE stock_transfer_details.dest_product_warehouse_id = m.old_id;
UPDATE purchase_items SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE purchase_items.product_warehouse_id = m.old_id;
-- FIFO v2 tables (kosong di dump 2026-05-25, defensive)
UPDATE fifo_stock_v2_operation_log SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE fifo_stock_v2_operation_log.product_warehouse_id = m.old_id;
UPDATE fifo_stock_v2_reflow_checkpoints SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE fifo_stock_v2_reflow_checkpoints.product_warehouse_id = m.old_id;
UPDATE fifo_stock_v2_shadow_allocations SET product_warehouse_id = m.new_id
FROM _pw_map m WHERE fifo_stock_v2_shadow_allocations.product_warehouse_id = m.old_id;
-- STEP 4: Hard-delete W10 PW yang sudah merged (9 rows expected)
DELETE FROM product_warehouses WHERE id IN (SELECT old_id FROM _pw_map);
-- STEP 5: Sisa W10 PW (4 rows: 1188/1189/1190/1196) -> warehouse_id=25,
-- pfk dinormalisasi ke NULL sekalian (LOKASI rule)
UPDATE product_warehouses
SET warehouse_id = 25, project_flock_kandang_id = NULL
WHERE warehouse_id = 10;
-- STEP 6: purchase_items.warehouse_id (17 rows)
UPDATE purchase_items SET warehouse_id = 25 WHERE warehouse_id = 10;
-- STEP 7: stock_transfers
-- 7.1 Soft-delete self-loop (W10<->W25 akan jadi W25<->W25)
UPDATE stock_transfers
SET deleted_at = NOW(), updated_at = NOW()
WHERE deleted_at IS NULL
AND ((from_warehouse_id = 10 AND to_warehouse_id = 25)
OR (from_warehouse_id = 25 AND to_warehouse_id = 10));
-- 7.2 Sisa W10<->other -> 25 (12 EGG_FARM_CUTOVER ke W10)
UPDATE stock_transfers SET from_warehouse_id = 25, updated_at = NOW() WHERE from_warehouse_id = 10;
UPDATE stock_transfers SET to_warehouse_id = 25, updated_at = NOW() WHERE to_warehouse_id = 10;
-- STEP 8: Soft-delete warehouse 10 sendiri
UPDATE warehouses SET deleted_at = NOW(), updated_at = NOW()
WHERE id = 10 AND deleted_at IS NULL;
-- STEP 9: Post-check (fail-fast jika ada residu)
DO $$
DECLARE v_count INT;
BEGIN
SELECT COUNT(*) INTO v_count FROM product_warehouses WHERE warehouse_id = 10;
IF v_count <> 0 THEN RAISE EXCEPTION 'product_warehouses W10 residual %', v_count; END IF;
SELECT COUNT(*) INTO v_count FROM purchase_items WHERE warehouse_id = 10;
IF v_count <> 0 THEN RAISE EXCEPTION 'purchase_items W10 residual %', v_count; END IF;
SELECT COUNT(*) INTO v_count FROM stock_transfers
WHERE deleted_at IS NULL AND (from_warehouse_id = 10 OR to_warehouse_id = 10);
IF v_count <> 0 THEN RAISE EXCEPTION 'stock_transfers W10 residual %', v_count; END IF;
END $$;
COMMIT;
@@ -0,0 +1,29 @@
BEGIN;
-- ============================================================
-- Rollback stock_log drift fix: DELETE corrective rows yang di-insert UP.
-- IDs ditarik dari audit table `migration_audit.jamali_w10_stocklog_corrections`.
-- Setelah delete, `last_stock_log.stock` kembali ke nilai pre-fix (drift muncul lagi).
-- ============================================================
-- Guard: audit table harus ada
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = 'migration_audit'
AND table_name = 'jamali_w10_stocklog_corrections'
) THEN
RAISE EXCEPTION
'Audit table migration_audit.jamali_w10_stocklog_corrections tidak ditemukan. UP belum dijalankan atau audit sudah di-drop.';
END IF;
END $$;
-- DELETE corrective stock_logs yang di-insert oleh UP
DELETE FROM stock_logs
WHERE id IN (SELECT stock_log_id FROM migration_audit.jamali_w10_stocklog_corrections);
-- Cleanup audit table
DROP TABLE migration_audit.jamali_w10_stocklog_corrections;
COMMIT;
@@ -0,0 +1,111 @@
BEGIN;
-- ============================================================
-- Fix stock_log drift pasca-merge warehouse Jamali (NON_AKTIF) -> Gudang Farm Jamali.
-- Follow-up migration setelah 20260528121631_normalize_warehouse_jamali_10_to_25.
--
-- Setelah merge, `stock_logs.stock` (running ledger) drift dari
-- `product_warehouses.qty` karena: pre-existing drift di W10 + W25 sources,
-- plus FIFO reflow yang trigger pasca-merge (Recording-Edit) recompute
-- pw.qty tapi stock_logs tidak ikut update.
--
-- Migration ini insert 1 ADJUSTMENT stock_log corrective per PW yang drift
-- supaya `last_stock_log.stock = pw.qty`. Logic ekivalen dengan
-- `cmd/fix-stock-log-drift`.
--
-- Karakteristik dynamic:
-- - Tidak hardcode PW IDs atau drift values
-- - Iterate via merge target + W10-only kept PWs (data-driven dari snapshot)
-- - Per PW: hitung drift runtime, skip kalau negligible (< 0.001) atau no logs
-- - Track stock_log IDs yang di-insert untuk DOWN reverse
-- ============================================================
-- Guard: previous migration (normalisasi) audit harus ada
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = 'migration_audit'
AND table_name = 'jamali_w10_qty_merge'
) THEN
RAISE EXCEPTION
'Migration 20260528121631 (normalize_warehouse_jamali) belum dijalankan atau audit-nya sudah di-drop. Apply UP-nya dulu sebelum migration ini.';
END IF;
END $$;
-- Audit table untuk track stock_log IDs yang di-insert (untuk DOWN reverse)
DROP TABLE IF EXISTS migration_audit.jamali_w10_stocklog_corrections;
CREATE TABLE migration_audit.jamali_w10_stocklog_corrections (
stock_log_id BIGINT NOT NULL PRIMARY KEY,
product_warehouse_id BIGINT NOT NULL,
drift NUMERIC(15,3) NOT NULL,
inserted_at TIMESTAMPTZ DEFAULT NOW()
);
-- Insert corrective ADJUSTMENT stock_log untuk tiap PW yang drift
DO $$
DECLARE
rec RECORD;
v_last_log_stock NUMERIC(15,3);
v_drift NUMERIC(15,3);
v_new_log_id BIGINT;
v_inserts INT := 0;
BEGIN
FOR rec IN (
SELECT pw.id AS pw_id, pw.qty AS qty
FROM product_warehouses pw
WHERE pw.id IN (
-- Merge target W25 PWs (9 rows)
SELECT target_pw_id FROM migration_audit.jamali_w10_qty_merge
UNION
-- W10-only PWs yang di-update warehouse_id 10->25 (4 rows)
SELECT id FROM migration_audit.jamali_w10_pw_w10only_snapshot
)
) LOOP
-- Ambil stock akhir di stock_logs ledger
SELECT stock INTO v_last_log_stock
FROM stock_logs
WHERE product_warehouse_id = rec.pw_id
ORDER BY id DESC
LIMIT 1;
-- PW tanpa stock_logs entry (mis. 1188/1189/1190 ayam) -> skip
IF v_last_log_stock IS NULL THEN
CONTINUE;
END IF;
v_drift := rec.qty - v_last_log_stock;
-- Drift negligible -> skip
IF ABS(v_drift) < 0.001 THEN
CONTINUE;
END IF;
-- Insert corrective ADJUSTMENT stock_log
INSERT INTO stock_logs (
product_warehouse_id, loggable_type, loggable_id,
notes, increase, decrease, stock, created_by, created_at
) VALUES (
rec.pw_id,
'ADJUSTMENT',
0,
'Koreksi stock_log drift pasca-merge warehouse Jamali (migration 20260528123243)',
CASE WHEN v_drift > 0 THEN v_drift ELSE 0 END,
CASE WHEN v_drift < 0 THEN -v_drift ELSE 0 END,
rec.qty,
1,
NOW()
) RETURNING id INTO v_new_log_id;
-- Track ke audit table untuk DOWN
INSERT INTO migration_audit.jamali_w10_stocklog_corrections (
stock_log_id, product_warehouse_id, drift
) VALUES (v_new_log_id, rec.pw_id, v_drift);
v_inserts := v_inserts + 1;
END LOOP;
RAISE NOTICE 'Inserted % corrective stock_logs to align ledger with pw.qty', v_inserts;
END $$;
COMMIT;
@@ -0,0 +1,3 @@
ALTER TABLE marketings DROP COLUMN IF EXISTS grand_total;
ALTER TABLE expenses DROP COLUMN IF EXISTS grand_total;
ALTER TABLE purchases DROP COLUMN IF EXISTS grand_total;
@@ -0,0 +1,42 @@
-- Marketing belum punya grand_total. Tambahkan dengan DEFAULT 0.
ALTER TABLE marketings ADD COLUMN grand_total NUMERIC(15, 3) NOT NULL DEFAULT 0;
-- Expense grand_total sebelumnya di-drop di migration 20251125055613. Re-add.
ALTER TABLE expenses ADD COLUMN grand_total NUMERIC(15, 3) NOT NULL DEFAULT 0;
ALTER TABLE purchases ADD COLUMN grand_total NUMERIC(15, 3) NOT NULL DEFAULT 0;
-- Backfill nilai grand_total dari children:
-- marketings.grand_total = SUM marketing_delivery_products.total_price (WHERE delivery_date IS NOT NULL)
UPDATE marketings m
SET grand_total = COALESCE(s.t, 0)
FROM (
SELECT mp.marketing_id AS marketing_id, SUM(mdp.total_price) AS t
FROM marketing_delivery_products mdp
JOIN marketing_products mp ON mp.id = mdp.marketing_product_id
WHERE mdp.delivery_date IS NOT NULL
GROUP BY mp.marketing_id
) s
WHERE s.marketing_id = m.id;
-- expenses.grand_total = SUM(expense_realizations.qty * expense_realizations.price) via expense_nonstocks
UPDATE expenses e
SET grand_total = COALESCE(s.t, 0)
FROM (
SELECT en.expense_id AS expense_id, SUM(er.qty * er.price) AS t
FROM expense_realizations er
JOIN expense_nonstocks en ON en.id = er.expense_nonstock_id
GROUP BY en.expense_id
) s
WHERE s.expense_id = e.id;
-- purchases.grand_total sudah ada sejak migration 20251104084555.
-- Recompute juga untuk safety supaya konsisten dengan SUM purchase_items.total_price.
UPDATE purchases p
SET grand_total = COALESCE(s.t, 0)
FROM (
SELECT purchase_id, SUM(total_price) AS t
FROM purchase_items
GROUP BY purchase_id
) s
WHERE s.purchase_id = p.id;
@@ -0,0 +1,5 @@
DROP INDEX IF EXISTS idx_payments_party_active;
DROP INDEX IF EXISTS idx_mdp_delivery_date_partial;
DROP INDEX IF EXISTS idx_purchase_items_received_date_partial;
DROP TABLE IF EXISTS payment_allocations;
@@ -0,0 +1,27 @@
-- Tabel payment_allocations menyimpan hasil FIFO matching antara payment dengan
-- sub-row anak (purchase_item / marketing_delivery_product / expense_realization).
-- Setiap allocation row HARUS terhubung ke tepat 1 child via 3 nullable FK
-- (polymorphic-via-multiple-nullable-FK; lebih aman dari single polymorphic kolom).
CREATE TABLE IF NOT EXISTS payment_allocations (
id BIGSERIAL PRIMARY KEY,
payment_id BIGINT NOT NULL REFERENCES payments(id) ON DELETE CASCADE,
purchase_item_id BIGINT NULL REFERENCES purchase_items(id) ON DELETE CASCADE,
marketing_delivery_product_id BIGINT NULL REFERENCES marketing_delivery_products(id) ON DELETE CASCADE,
expense_realization_id BIGINT NULL REFERENCES expense_realizations(id) ON DELETE CASCADE,
amount NUMERIC(15, 3) NOT NULL CHECK (amount > 0),
allocated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chk_payment_alloc_exactly_one CHECK (
num_nonnulls(purchase_item_id, marketing_delivery_product_id, expense_realization_id) = 1
)
);
CREATE INDEX IF NOT EXISTS idx_payment_alloc_payment ON payment_allocations (payment_id);
CREATE INDEX IF NOT EXISTS idx_payment_alloc_purchase_item ON payment_allocations (purchase_item_id) WHERE purchase_item_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_payment_alloc_mdp ON payment_allocations (marketing_delivery_product_id) WHERE marketing_delivery_product_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_payment_alloc_realization ON payment_allocations (expense_realization_id) WHERE expense_realization_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_payment_alloc_allocated_at ON payment_allocations (allocated_at);
-- Helper partial indexes untuk FIFO loop performance
CREATE INDEX IF NOT EXISTS idx_purchase_items_received_date_partial ON purchase_items (received_date) WHERE received_date IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_mdp_delivery_date_partial ON marketing_delivery_products (delivery_date) WHERE delivery_date IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_payments_party_active ON payments (party_type, party_id, payment_date) WHERE deleted_at IS NULL;
@@ -0,0 +1,4 @@
-- Rollback backfill: hapus semua allocations dan drop function.
TRUNCATE payment_allocations;
DROP FUNCTION IF EXISTS fn_fifo_backfill_party(TEXT, BIGINT);
@@ -0,0 +1,170 @@
-- Backfill payment_allocations untuk data historis via FIFO simulation.
-- Seluruh migration ini berjalan dalam 1 transaction (golang-migrate default).
-- Jika ada party yang gagal di tengah loop, seluruh backfill ROLLBACK otomatis.
-- Fungsi inti: FIFO greedy untuk 1 party (supplier/customer).
-- Algoritma:
-- 1. Hapus payment_allocations existing untuk party tsb (idempotent).
-- 2. Kumpulkan eligible children sort by date ASC ke array (kind, id, amount, remaining).
-- 3. Konsumsi creditCarry (SUM payment SALDO_AWAL) ke children tertua — TIDAK insert allocation row.
-- 4. Loop payments (selain SALDO_AWAL) ORDER BY payment_date ASC: greedy alokasi ke child tertua dengan remaining > 0.
-- 5. Sisa nominal payment tidak insert row (otomatis credit balance untuk dokumen baru).
CREATE OR REPLACE FUNCTION fn_fifo_backfill_party(
p_party_type TEXT,
p_party_id BIGINT
) RETURNS VOID AS $func$
DECLARE
v_party_type TEXT := UPPER(p_party_type);
v_payment RECORD;
v_child RECORD;
v_remaining NUMERIC(15, 3);
v_used NUMERIC(15, 3);
v_eps CONSTANT NUMERIC(15, 3) := 0.001;
BEGIN
-- Acquire advisory lock untuk anti-race (1-arg form: hashtext returns int4, cast ke bigint)
PERFORM pg_advisory_xact_lock(hashtext('payment_alloc:' || v_party_type || ':' || p_party_id::text)::bigint);
-- Hapus allocations existing untuk party tsb (idempotent ulang-jalan)
DELETE FROM payment_allocations pa
USING payments p
WHERE pa.payment_id = p.id
AND p.party_type = v_party_type
AND p.party_id = p_party_id;
-- TEMP table untuk antrian children (sort sudah ada di INSERT...SELECT ORDER BY)
CREATE TEMP TABLE IF NOT EXISTS _children_queue (
seq BIGSERIAL PRIMARY KEY,
kind TEXT NOT NULL, -- 'PURCHASE_ITEM' / 'MDP' / 'EXPENSE_REALIZATION'
child_id BIGINT NOT NULL,
amount NUMERIC(15, 3) NOT NULL,
remaining NUMERIC(15, 3) NOT NULL
) ON COMMIT DROP;
TRUNCATE _children_queue;
IF v_party_type = 'SUPPLIER' THEN
-- purchase_items eligible: received_date IS NOT NULL, approval latest step >= 4 (Receiving), action != REJECTED
INSERT INTO _children_queue (kind, child_id, amount, remaining)
SELECT 'PURCHASE_ITEM', pi.id, pi.total_price, pi.total_price
FROM purchase_items pi
JOIN purchases p ON p.id = pi.purchase_id
JOIN LATERAL (
SELECT a.step_number, a.action
FROM approvals a
WHERE a.approvable_type = 'PURCHASES' AND a.approvable_id = p.id
ORDER BY a.action_at DESC, a.id DESC
LIMIT 1
) la ON true
WHERE p.supplier_id = p_party_id
AND p.deleted_at IS NULL
AND pi.received_date IS NOT NULL
AND la.step_number >= 4
AND (la.action IS NULL OR la.action <> 'REJECTED')
AND pi.total_price > 0
ORDER BY pi.received_date ASC, pi.id ASC;
-- expense_realizations eligible: parent expense approval latest step >= 5 (Realisasi), action != REJECTED.
-- Sort pakai e.transaction_date supaya FIFO konsisten dengan tanggal yang di-display di report.
INSERT INTO _children_queue (kind, child_id, amount, remaining)
SELECT 'EXPENSE_REALIZATION', er.id, (er.qty * er.price), (er.qty * er.price)
FROM expense_realizations er
JOIN expense_nonstocks en ON en.id = er.expense_nonstock_id
JOIN expenses e ON e.id = en.expense_id
JOIN LATERAL (
SELECT a.step_number, a.action
FROM approvals a
WHERE a.approvable_type = 'EXPENSES' AND a.approvable_id = e.id
ORDER BY a.action_at DESC, a.id DESC
LIMIT 1
) la ON true
WHERE e.supplier_id = p_party_id
AND e.deleted_at IS NULL
AND la.step_number >= 5
AND (la.action IS NULL OR la.action <> 'REJECTED')
AND (er.qty * er.price) > 0
ORDER BY e.transaction_date ASC, e.id ASC, er.id ASC;
ELSIF v_party_type = 'CUSTOMER' THEN
-- marketing_delivery_products eligible: delivery_date IS NOT NULL (match current report behavior, tidak filter approval)
INSERT INTO _children_queue (kind, child_id, amount, remaining)
SELECT 'MDP', mdp.id, mdp.total_price, mdp.total_price
FROM marketing_delivery_products mdp
JOIN marketing_products mp ON mp.id = mdp.marketing_product_id
JOIN marketings m ON m.id = mp.marketing_id
WHERE m.customer_id = p_party_id
AND m.deleted_at IS NULL
AND mdp.delivery_date IS NOT NULL
AND mdp.total_price > 0
ORDER BY mdp.delivery_date ASC, mdp.id ASC;
ELSE
RETURN;
END IF;
-- Skip jika tidak ada children eligible
IF NOT EXISTS (SELECT 1 FROM _children_queue) THEN
RETURN;
END IF;
-- Loop SEMUA payments termasuk SALDO_AWAL ORDER BY payment_date ASC, id ASC.
-- SALDO_AWAL diperlakukan sebagai payment tertua sehingga opening credit otomatis
-- consume oldest debts via FIFO. Tanpa allocation row, debt yang ter-cover SaldoAwal
-- akan tampak "Belum Lunas" di report.
FOR v_payment IN
SELECT id, nominal
FROM payments
WHERE party_type = v_party_type
AND party_id = p_party_id
AND deleted_at IS NULL
AND nominal > v_eps
ORDER BY payment_date ASC, id ASC
LOOP
v_remaining := v_payment.nominal;
-- Greedy alokasi ke children tertua dengan remaining > 0
FOR v_child IN
SELECT seq, kind, child_id, remaining
FROM _children_queue
WHERE remaining > v_eps
ORDER BY seq ASC
LOOP
EXIT WHEN v_remaining <= v_eps;
-- v_child.remaining is snapshot at cursor open; re-fetch latest to avoid drift in same payment iter
SELECT remaining INTO v_used FROM _children_queue WHERE seq = v_child.seq;
IF v_used <= v_eps THEN
CONTINUE;
END IF;
v_used := LEAST(v_remaining, v_used);
UPDATE _children_queue SET remaining = remaining - v_used WHERE seq = v_child.seq;
v_remaining := v_remaining - v_used;
IF v_child.kind = 'PURCHASE_ITEM' THEN
INSERT INTO payment_allocations (payment_id, purchase_item_id, amount, allocated_at)
VALUES (v_payment.id, v_child.child_id, v_used, NOW());
ELSIF v_child.kind = 'MDP' THEN
INSERT INTO payment_allocations (payment_id, marketing_delivery_product_id, amount, allocated_at)
VALUES (v_payment.id, v_child.child_id, v_used, NOW());
ELSIF v_child.kind = 'EXPENSE_REALIZATION' THEN
INSERT INTO payment_allocations (payment_id, expense_realization_id, amount, allocated_at)
VALUES (v_payment.id, v_child.child_id, v_used, NOW());
END IF;
END LOOP;
END LOOP;
END;
$func$ LANGUAGE plpgsql;
-- Invoke per-party. Gagal di satu party → entire transaction ROLLBACK.
DO $do$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT DISTINCT party_type, party_id
FROM payments
WHERE deleted_at IS NULL
AND party_id IS NOT NULL
LOOP
PERFORM fn_fifo_backfill_party(r.party_type, r.party_id);
END LOOP;
END;
$do$;
@@ -0,0 +1,8 @@
-- IRREVERSIBLE migration: po_number lama (counter-based) tidak di-backup
-- saat UP karena user secara eksplisit pilih "tanpa backup table".
-- Down ini hanya raise notice supaya operator sadar harus restore dari
-- DB-level backup terpisah kalau memang perlu rollback.
DO $$
BEGIN
RAISE NOTICE 'WARNING: Migration 20260529143940_normalize_po_number_to_pr_pattern is irreversible. Original counter-based PO numbers were not backed up. Restore from DB-level backup if rollback is required.';
END $$;
@@ -0,0 +1,87 @@
BEGIN;
-- ============================================================
-- Normalize purchases.po_number agar mengikuti pr_number (swap prefix).
-- Contoh: pr_number='PR-LTI-0050' -> po_number='PO-LTI-0050'
--
-- Konteks: sebelumnya pr_number dan po_number punya counter sequential
-- terpisah (lihat purchase.repository.go NextPrNumber / NextPoNumber yang
-- dihapus seiring migration ini), sehingga selalu diverge. Setelah
-- perubahan code (ApproveManagerPurchase derive PO dari PR), historis
-- perlu di-backfill supaya konsisten.
--
-- Juga update expenses.po_number (snapshot dari expense_bridge.go)
-- supaya konsisten dengan purchases.
--
-- Constraint uq_purchases_po_number adalah NOT DEFERRABLE (per-row check),
-- jadi single UPDATE bulk gagal di swap-conflict (contoh: row A mau jadi
-- 'PO-LTI-0700' tapi row B masih punya 'PO-LTI-0700' -> error 23505).
-- Solusi: capture target ke temp table, NULL dulu, baru set nilai derived.
--
-- IRREVERSIBLE: nilai po_number lama (counter-based) tidak di-backup.
-- Kalau ada kegagalan di tengah, COMMIT tidak terjadi -> ROLLBACK otomatis.
-- ============================================================
-- 1. Capture target IDs (snapshot rencana update — sebelum perubahan apapun)
CREATE TEMP TABLE _purchases_po_normalize_ids ON COMMIT DROP AS
SELECT id
FROM purchases
WHERE po_number IS NOT NULL
AND pr_number LIKE 'PR-LTI-%'
AND po_number <> REPLACE(pr_number, 'PR-LTI-', 'PO-LTI-');
-- 2. Update expenses DULU — join via current po_number masih valid sebelum step 3-4
UPDATE expenses e
SET po_number = REPLACE(p.pr_number, 'PR-LTI-', 'PO-LTI-')
FROM purchases p
JOIN _purchases_po_normalize_ids n ON n.id = p.id
WHERE e.po_number = p.po_number
AND e.po_number IS NOT NULL
AND e.po_number <> '';
-- 3. NULL-kan purchases.po_number untuk target — lepas constraint conflict
UPDATE purchases
SET po_number = NULL
WHERE id IN (SELECT id FROM _purchases_po_normalize_ids);
-- 4. Set nilai derived dari pr_number (sekarang aman karena slot lama sudah NULL)
UPDATE purchases p
SET po_number = REPLACE(p.pr_number, 'PR-LTI-', 'PO-LTI-')
FROM _purchases_po_normalize_ids n
WHERE p.id = n.id;
-- 5. Sanity check — fail (auto-rollback) kalau masih ada mismatch
DO $$
DECLARE
v_mismatch_purchases INT;
v_mismatch_expenses INT;
v_target_count INT;
BEGIN
SELECT COUNT(*) INTO v_target_count FROM _purchases_po_normalize_ids;
SELECT COUNT(*) INTO v_mismatch_purchases
FROM purchases
WHERE po_number IS NOT NULL
AND pr_number LIKE 'PR-LTI-%'
AND po_number <> REPLACE(pr_number, 'PR-LTI-', 'PO-LTI-');
IF v_mismatch_purchases > 0 THEN
RAISE EXCEPTION 'Normalize failed: % purchases rows still have mismatched po_number', v_mismatch_purchases;
END IF;
SELECT COUNT(*) INTO v_mismatch_expenses
FROM expenses e
JOIN purchases p ON e.po_number = p.po_number
WHERE p.pr_number LIKE 'PR-LTI-%'
AND e.po_number IS NOT NULL
AND e.po_number <> ''
AND e.po_number <> REPLACE(p.pr_number, 'PR-LTI-', 'PO-LTI-');
IF v_mismatch_expenses > 0 THEN
RAISE EXCEPTION 'Normalize failed: % expenses rows still have mismatched po_number', v_mismatch_expenses;
END IF;
RAISE NOTICE 'Normalize complete: % purchases rows updated', v_target_count;
END $$;
COMMIT;
@@ -1,6 +1,6 @@
-- Hapus open_house dan close_house rows dengan effective_date baru -- Hapus open_house dan close_house rows dengan effective_date baru
DELETE FROM house_depreciation_standards DELETE FROM house_depreciation_standards
WHERE house_type IN ('open_house', 'close_house') AND effective_date = '2026-05-20'; WHERE house_type IN ('open_house', 'close_house') AND effective_date = '2026-05-29';
-- Hapus kolom multiplication_percentage -- Hapus kolom multiplication_percentage
ALTER TABLE house_depreciation_standards DROP COLUMN multiplication_percentage; ALTER TABLE house_depreciation_standards DROP COLUMN multiplication_percentage;
@@ -134,7 +134,7 @@ INSERT INTO house_depreciation_standards
SELECT SELECT
'open_house'::house_type_enum, 'open_house'::house_type_enum,
day, day,
'2026-05-20'::date, '2026-05-29'::date,
depreciation_percent, depreciation_percent,
25, 25,
'Standard Open House Week 25', 'Standard Open House Week 25',
@@ -147,133 +147,26 @@ FROM (
ORDER BY day, effective_date DESC NULLS LAST ORDER BY day, effective_date DESC NULLS LAST
) effective_open_house; ) effective_open_house;
-- Insert close_house baru: depreciation_percent dari open_house, multiplication_percentage dari Excel row 8 (close_house)
-- Insert close_house baru dengan effective_date 2026-05-29
-- multiplication_percentage diambil dari row existing (sudah di-UPDATE di step sebelumnya)
INSERT INTO house_depreciation_standards INSERT INTO house_depreciation_standards
(house_type, day, effective_date, depreciation_percent, standard_week, name, multiplication_percentage) (house_type, day, effective_date, depreciation_percent, standard_week, name, multiplication_percentage)
SELECT SELECT
'close_house'::house_type_enum, 'close_house'::house_type_enum,
oh.day, day,
'2026-05-20'::date, '2026-05-29'::date,
oh.depreciation_percent, depreciation_percent,
25, 25,
'Standard Close House Week 25', 'Standard Close House Week 25',
ch.val multiplication_percentage
FROM ( FROM (
SELECT DISTINCT ON (day) SELECT DISTINCT ON (day)
day, depreciation_percent day, depreciation_percent, multiplication_percentage
FROM house_depreciation_standards FROM house_depreciation_standards
WHERE house_type = 'open_house' WHERE house_type = 'open_house'
ORDER BY day, effective_date DESC NULLS LAST ORDER BY day, effective_date DESC NULLS LAST
) oh ) effective_close_house;
JOIN (VALUES
(1,0.9981),(2,0.9981),(3,0.9981),(4,0.9981),(5,0.9981),
(6,0.9981),(7,0.9981),(8,0.9978),(9,0.9978),(10,0.9978),
(11,0.9978),(12,0.9978),(13,0.9978),(14,0.9978),(15,0.9978),
(16,0.9978),(17,0.9978),(18,0.9978),(19,0.9978),(20,0.9978),
(21,0.9978),(22,0.9981),(23,0.9981),(24,0.9981),(25,0.9981),
(26,0.9981),(27,0.9981),(28,0.9981),(29,0.9978),(30,0.9978),
(31,0.9978),(32,0.9978),(33,0.9978),(34,0.9978),(35,0.9978),
(36,0.9978),(37,0.9978),(38,0.9978),(39,0.9978),(40,0.9978),
(41,0.9978),(42,0.9978),(43,0.9978),(44,0.9978),(45,0.9978),
(46,0.9978),(47,0.9978),(48,0.9978),(49,0.9978),(50,0.9981),
(51,0.9981),(52,0.9981),(53,0.9981),(54,0.9981),(55,0.9981),
(56,0.9981),(57,0.9978),(58,0.9978),(59,0.9978),(60,0.9978),
(61,0.9978),(62,0.9978),(63,0.9978),(64,0.9978),(65,0.9978),
(66,0.9977),(67,0.9977),(68,0.9977),(69,0.9977),(70,0.9977),
(71,0.9973),(72,0.9973),(73,0.9973),(74,0.9973),(75,0.9973),
(76,0.9973),(77,0.9973),(78,0.9977),(79,0.9977),(80,0.9977),
(81,0.9977),(82,0.9977),(83,0.9976),(84,0.9976),(85,0.9972),
(86,0.9972),(87,0.9972),(88,0.9972),(89,0.9972),(90,0.9972),
(91,0.9972),(92,0.9972),(93,0.9972),(94,0.9972),(95,0.9972),
(96,0.9972),(97,0.9972),(98,0.9971),(99,0.9975),(100,0.9975),
(101,0.9975),(102,0.9975),(103,0.9975),(104,0.9975),(105,0.9975),
(106,0.9971),(107,0.9971),(108,0.9971),(109,0.9971),(110,0.9971),
(111,0.997),(112,0.997),(113,0.9974),(114,0.9974),(115,0.9974),
(116,0.9974),(117,0.9974),(118,0.9974),(119,0.9974),(120,0.997),
(121,0.997),(122,0.997),(123,0.9969),(124,0.9969),(125,0.9969),
(126,0.9969),(127,0.9973),(128,0.9973),(129,0.9973),(130,0.9973),
(131,0.9973),(132,0.9973),(133,0.9973),(134,0.9968),(135,0.9968),
(136,0.9968),(137,0.9968),(138,0.9968),(139,0.9968),(140,0.9968),
(141,0.9972),(142,0.9972),(143,0.9972),(144,0.9972),(145,0.9972),
(146,0.9972),(147,0.9972),(148,0.9967),(149,0.9967),(150,0.9967),
(151,0.9967),(152,0.9967),(153,0.9967),(154,0.9966),(155,0.9971),
(156,0.9971),(157,0.9971),(158,0.9971),(159,0.9971),(160,0.9971),
(161,0.9971),(162,0.9971),(163,0.997),(164,0.997),(165,0.997),
(166,0.997),(167,0.997),(168,0.997),(169,0.9965),(170,0.9965),
(171,0.9965),(172,0.9965),(173,0.9964),(174,0.9964),(175,0.9964),
(176,0.9969),(177,0.9969),(178,0.9969),(179,0.9969),(180,0.9969),
(181,0.9969),(182,0.9969),(183,0.9968),(184,0.9968),(185,0.9968),
(186,0.9968),(187,0.9968),(188,0.9968),(189,0.9968),(190,0.9962),
(191,0.9962),(192,0.9962),(193,0.9962),(194,0.9962),(195,0.9962),
(196,0.9962),(197,0.9967),(198,0.9967),(199,0.9967),(200,0.9967),
(201,0.9966),(202,0.9966),(203,0.9966),(204,0.9966),(205,0.9966),
(206,0.9966),(207,0.9966),(208,0.9966),(209,0.9966),(210,0.9965),
(211,0.9965),(212,0.9965),(213,0.9965),(214,0.9965),(215,0.9965),
(216,0.9965),(217,0.9965),(218,0.9964),(219,0.9964),(220,0.9964),
(221,0.9964),(222,0.9964),(223,0.9964),(224,0.9964),(225,0.9957),
(226,0.9957),(227,0.9957),(228,0.9957),(229,0.9957),(230,0.9957),
(231,0.9956),(232,0.9962),(233,0.9962),(234,0.9962),(235,0.9962),
(236,0.9962),(237,0.9962),(238,0.9962),(239,0.9961),(240,0.9961),
(241,0.9961),(242,0.9961),(243,0.9961),(244,0.9961),(245,0.996),
(246,0.996),(247,0.996),(248,0.996),(249,0.996),(250,0.996),
(251,0.996),(252,0.9959),(253,0.9959),(254,0.9959),(255,0.9959),
(256,0.9959),(257,0.9959),(258,0.9958),(259,0.9958),(260,0.9958),
(261,0.9958),(262,0.9958),(263,0.9957),(264,0.9957),(265,0.9957),
(266,0.9957),(267,0.9957),(268,0.9957),(269,0.9956),(270,0.9956),
(271,0.9956),(272,0.9956),(273,0.9956),(274,0.9955),(275,0.9955),
(276,0.9955),(277,0.9955),(278,0.9955),(279,0.9954),(280,0.9954),
(281,0.9954),(282,0.9954),(283,0.9953),(284,0.9953),(285,0.9953),
(286,0.9953),(287,0.9953),(288,0.996),(289,0.996),(290,0.996),
(291,0.996),(292,0.996),(293,0.996),(294,0.9959),(295,0.9951),
(296,0.9951),(297,0.9951),(298,0.995),(299,0.995),(300,0.995),
(301,0.995),(302,0.9949),(303,0.9949),(304,0.9949),(305,0.9948),
(306,0.9948),(307,0.9948),(308,0.9948),(309,0.9947),(310,0.9947),
(311,0.9947),(312,0.9947),(313,0.9946),(314,0.9946),(315,0.9946),
(316,0.9945),(317,0.9945),(318,0.9945),(319,0.9944),(320,0.9944),
(321,0.9944),(322,0.9944),(323,0.9953),(324,0.9952),(325,0.9952),
(326,0.9952),(327,0.9952),(328,0.9952),(329,0.9951),(330,0.9941),
(331,0.9941),(332,0.9941),(333,0.994),(334,0.994),(335,0.994),
(336,0.9939),(337,0.9949),(338,0.9949),(339,0.9948),(340,0.9948),
(341,0.9948),(342,0.9948),(343,0.9947),(344,0.9937),(345,0.9936),
(346,0.9936),(347,0.9935),(348,0.9935),(349,0.9934),(350,0.9934),
(351,0.9934),(352,0.9933),(353,0.9933),(354,0.9932),(355,0.9932),
(356,0.9931),(357,0.9931),(358,0.9942),(359,0.9942),(360,0.9941),
(361,0.9941),(362,0.9941),(363,0.994),(364,0.994),(365,0.9927),
(366,0.9927),(367,0.9926),(368,0.9926),(369,0.9925),(370,0.9925),
(371,0.9924),(372,0.9936),(373,0.9936),(374,0.9935),(375,0.9935),
(376,0.9935),(377,0.9934),(378,0.9934),(379,0.9933),(380,0.9933),
(381,0.9932),(382,0.9932),(383,0.9931),(384,0.9931),(385,0.993),
(386,0.9916),(387,0.9915),(388,0.9915),(389,0.9914),(390,0.9913),
(391,0.9912),(392,0.9912),(393,0.9926),(394,0.9925),(395,0.9924),
(396,0.9924),(397,0.9923),(398,0.9923),(399,0.9922),(400,0.9922),
(401,0.9921),(402,0.992),(403,0.992),(404,0.9919),(405,0.9918),
(406,0.9918),(407,0.9917),(408,0.9916),(409,0.9916),(410,0.9915),
(411,0.9914),(412,0.9913),(413,0.9913),(414,0.9894),(415,0.9893),
(416,0.9892),(417,0.9891),(418,0.989),(419,0.9888),(420,0.9887),
(421,0.9905),(422,0.9904),(423,0.9903),(424,0.9902),(425,0.9901),
(426,0.99),(427,0.9899),(428,0.9898),(429,0.9897),(430,0.9896),
(431,0.9895),(432,0.9894),(433,0.9892),(434,0.9891),(435,0.989),
(436,0.9889),(437,0.9888),(438,0.9886),(439,0.9885),(440,0.9884),
(441,0.9882),(442,0.9881),(443,0.988),(444,0.9878),(445,0.9877),
(446,0.9875),(447,0.9873),(448,0.9872),(449,0.987),(450,0.9868),
(451,0.9867),(452,0.9865),(453,0.9863),(454,0.9861),(455,0.9859),
(456,0.9857),(457,0.9855),(458,0.9853),(459,0.9851),(460,0.9848),
(461,0.9846),(462,0.9844),(463,0.9873),(464,0.9871),(465,0.987),
(466,0.9868),(467,0.9866),(468,0.9864),(469,0.9863),(470,0.9826),
(471,0.9823),(472,0.9819),(473,0.9816),(474,0.9813),(475,0.9809),
(476,0.9805),(477,0.9802),(478,0.9798),(479,0.9793),(480,0.9789),
(481,0.9784),(482,0.978),(483,0.9775),(484,0.977),(485,0.9764),
(486,0.9758),(487,0.9752),(488,0.9746),(489,0.974),(490,0.9733),
(491,0.978),(492,0.9775),(493,0.977),(494,0.9765),(495,0.9759),
(496,0.9753),(497,0.9747),(498,0.9675),(499,0.9664),(500,0.9653),
(501,0.964),(502,0.9627),(503,0.9612),(504,0.9597),(505,0.9664),
(506,0.9652),(507,0.964),(508,0.9626),(509,0.9612),(510,0.9596),
(511,0.9579),(512,0.9451),(513,0.9419),(514,0.9383),(515,0.9342),
(516,0.9296),(517,0.9242),(518,0.918),(519,0.9286),(520,0.9231),
(521,0.9167),(522,0.9091),(523,0.9),(524,0.8889),(525,0.875),
(526,0.8571),(527,0.8333),(528,0.8),(529,0.75),(530,0.6667),
(531,0.5),(532,0)
) AS ch(day, val) ON oh.day = ch.day;
-- Invalidate snapshot cache depreciation agar recompute dengan standard baru -- Invalidate snapshot cache depreciation agar recompute dengan standard baru
DELETE FROM farm_depreciation_snapshots; DELETE FROM farm_depreciation_snapshots;
@@ -0,0 +1,14 @@
-- Rollback total_cost ke nilai sebelum migration
UPDATE farm_depreciation_manual_inputs
SET total_cost = 562618200.000,
updated_at = NOW()
WHERE project_flock_id = 10;
UPDATE farm_depreciation_manual_inputs
SET total_cost = 598552406.000,
updated_at = NOW()
WHERE project_flock_id = 11;
-- Snapshot lama tidak bisa di-restore — biarkan kosong, recompute otomatis
-- saat user request endpoint depresiasi
TRUNCATE TABLE farm_depreciation_snapshots;
@@ -0,0 +1,21 @@
-- Update total_cost farm_depreciation_manual_inputs untuk PFK 10 & 11
-- per permintaan user (cutover 28 Feb 2026)
--
-- PFK 10 (Flock Jamali 003) : 562.618.200,000 -> 1.900.157.533,55
-- PFK 11 (Flock Tamansari 001) : 598.552.406,000 -> 2.521.797.832,14
UPDATE farm_depreciation_manual_inputs
SET total_cost = 1900157533.55,
cutover_date = DATE '2026-02-28',
updated_at = NOW()
WHERE project_flock_id = 10;
UPDATE farm_depreciation_manual_inputs
SET total_cost = 2521797832.14,
cutover_date = DATE '2026-02-28',
updated_at = NOW()
WHERE project_flock_id = 11;
-- Pengaman: pastikan snapshot di-recompute dengan total_cost baru
-- saat user request /api/reports/expense/depreciation
TRUNCATE TABLE farm_depreciation_snapshots;
+1
View File
@@ -18,6 +18,7 @@ type Expense struct {
TransactionDate time.Time `gorm:"type:date;not null"` TransactionDate time.Time `gorm:"type:date;not null"`
Notes string `gorm:"type:text;column:notes"` Notes string `gorm:"type:text;column:notes"`
IsPaid bool `gorm:"column:is_paid;not null;default:false"` IsPaid bool `gorm:"column:is_paid;not null;default:false"`
GrandTotal float64 `gorm:"column:grand_total;type:numeric(15,3);not null;default:0"`
CreatedBy uint64 `gorm:""` CreatedBy uint64 `gorm:""`
CreatedAt time.Time `gorm:"autoCreateTime"` CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"` UpdatedAt time.Time `gorm:"autoUpdateTime"`
+1
View File
@@ -15,6 +15,7 @@ type Marketing struct {
SalesPersonId uint `gorm:"not null"` SalesPersonId uint `gorm:"not null"`
Notes string `gorm:"type:text"` Notes string `gorm:"type:text"`
MarketingType string `gorm:"type:varchar(50)"` MarketingType string `gorm:"type:varchar(50)"`
GrandTotal float64 `gorm:"column:grand_total;type:numeric(15,3);not null;default:0"`
CreatedBy uint `gorm:"not null"` CreatedBy uint `gorm:"not null"`
CreatedAt time.Time `gorm:"autoCreateTime"` CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"` UpdatedAt time.Time `gorm:"autoUpdateTime"`
+23
View File
@@ -0,0 +1,23 @@
package entities
import (
"time"
)
// PaymentAllocation merepresentasikan hasil FIFO matching dari 1 payment ke
// tepat 1 sub-row anak (purchase_item / marketing_delivery_product /
// expense_realization). DB constraint memastikan hanya satu FK yang non-null.
type PaymentAllocation struct {
Id uint64 `gorm:"primaryKey;autoIncrement"`
PaymentId uint `gorm:"not null;index"`
PurchaseItemId *uint `gorm:"column:purchase_item_id"`
MarketingDeliveryProductId *uint `gorm:"column:marketing_delivery_product_id"`
ExpenseRealizationId *uint64 `gorm:"column:expense_realization_id"`
Amount float64 `gorm:"type:numeric(15,3);not null"`
AllocatedAt time.Time `gorm:"type:timestamptz;not null;default:NOW()"`
Payment *Payment `gorm:"foreignKey:PaymentId;references:Id"`
PurchaseItem *PurchaseItem `gorm:"foreignKey:PurchaseItemId;references:Id"`
MarketingDeliveryProduct *MarketingDeliveryProduct `gorm:"foreignKey:MarketingDeliveryProductId;references:Id"`
ExpenseRealization *ExpenseRealization `gorm:"foreignKey:ExpenseRealizationId;references:Id"`
}
+1
View File
@@ -12,6 +12,7 @@ type Purchase struct {
SupplierId uint `gorm:"not null"` SupplierId uint `gorm:"not null"`
CreditTerm int `gorm:"column:credit_term;not null;default:0"` CreditTerm int `gorm:"column:credit_term;not null;default:0"`
DueDate *time.Time DueDate *time.Time
GrandTotal float64 `gorm:"column:grand_total;type:numeric(15,3);not null;default:0"`
Notes *string Notes *string
CreatedAt time.Time `gorm:"autoCreateTime"` CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"` UpdatedAt time.Time `gorm:"autoUpdateTime"`
+2 -1
View File
@@ -45,7 +45,8 @@ func (ExpenseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *
panic(fmt.Sprintf("failed to register expense approval workflow: %v", err)) panic(fmt.Sprintf("failed to register expense approval workflow: %v", err))
} }
expenseService := sExpense.NewExpenseService(expenseRepo, supplierRepo, nonstockRepo, approvalSvc, realizationRepo, projectFlockKandangRepo, documentSvc, validate) fifoPaymentSvc := commonSvc.NewFifoPaymentService(db, utils.Log)
expenseService := sExpense.NewExpenseService(expenseRepo, supplierRepo, nonstockRepo, approvalSvc, realizationRepo, projectFlockKandangRepo, documentSvc, fifoPaymentSvc, validate)
userService := sUser.NewUserService(userRepo, validate) userService := sUser.NewUserService(userRepo, validate)
ExpenseRoutes(router, userService, expenseService) ExpenseRoutes(router, userService, expenseService)
@@ -54,9 +54,10 @@ type expenseService struct {
RealizationRepository repository.ExpenseRealizationRepository RealizationRepository repository.ExpenseRealizationRepository
ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository
DocumentSvc commonSvc.DocumentService DocumentSvc commonSvc.DocumentService
FifoPaymentSvc commonSvc.FifoPaymentService
} }
func NewExpenseService(repo repository.ExpenseRepository, supplierRepo supplierRepo.SupplierRepository, nonstockRepo nonstockRepo.NonstockRepository, approvalSvc commonSvc.ApprovalService, realizationRepo repository.ExpenseRealizationRepository, projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository, documentSvc commonSvc.DocumentService, validate *validator.Validate) ExpenseService { func NewExpenseService(repo repository.ExpenseRepository, supplierRepo supplierRepo.SupplierRepository, nonstockRepo nonstockRepo.NonstockRepository, approvalSvc commonSvc.ApprovalService, realizationRepo repository.ExpenseRealizationRepository, projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository, documentSvc commonSvc.DocumentService, fifoPaymentSvc commonSvc.FifoPaymentService, validate *validator.Validate) ExpenseService {
return &expenseService{ return &expenseService{
Log: utils.Log, Log: utils.Log,
Validate: validate, Validate: validate,
@@ -67,6 +68,23 @@ func NewExpenseService(repo repository.ExpenseRepository, supplierRepo supplierR
RealizationRepository: realizationRepo, RealizationRepository: realizationRepo,
ProjectFlockKandangRepo: projectFlockKandangRepo, ProjectFlockKandangRepo: projectFlockKandangRepo,
DocumentSvc: documentSvc, DocumentSvc: documentSvc,
FifoPaymentSvc: fifoPaymentSvc,
}
}
// reallocateAfterRealization called after expense realization changes that may
// affect supplier debt: recompute grand_total + reallocate FIFO.
func (s *expenseService) reallocateAfterRealization(ctx context.Context, expenseID uint, supplierID uint64) {
if s.FifoPaymentSvc == nil {
return
}
if err := s.FifoPaymentSvc.RecomputeGrandTotal(ctx, nil, commonSvc.ParentKindExpense, expenseID); err != nil {
s.Log.Warnf("Failed to recompute grand_total for expense %d: %+v", expenseID, err)
}
if supplierID > 0 {
if err := s.FifoPaymentSvc.ReallocateForParty(ctx, nil, string(utils.PaymentPartySupplier), uint(supplierID)); err != nil {
s.Log.Warnf("Failed to reallocate payments for supplier %d: %+v", supplierID, err)
}
} }
} }
@@ -1078,6 +1096,9 @@ func (s *expenseService) CreateRealization(c *fiber.Ctx, expenseID uint, req *va
} }
invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(expense.TransactionDate, realizationDate, expense.RealizationDate) invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(expense.TransactionDate, realizationDate, expense.RealizationDate)
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil) s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil)
s.reallocateAfterRealization(c.Context(), expenseID, expense.SupplierId)
return responseDTO, nil return responseDTO, nil
} }
@@ -1522,6 +1543,9 @@ func (s *expenseService) UpdateRealization(c *fiber.Ctx, expenseID uint, req *va
return nil, err return nil, err
} }
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil) s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil)
s.reallocateAfterRealization(c.Context(), expenseID, expense.SupplierId)
return responseDTO, nil return responseDTO, nil
} }
+3 -1
View File
@@ -29,7 +29,9 @@ func (PaymentModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *
panic(fmt.Sprintf("failed to register payment approval workflow: %v", err)) panic(fmt.Sprintf("failed to register payment approval workflow: %v", err))
} }
paymentService := sPayment.NewPaymentService(paymentRepo, approvalService, validate) fifoPaymentService := commonSvc.NewFifoPaymentService(db, nil)
paymentService := sPayment.NewPaymentService(paymentRepo, approvalService, fifoPaymentService, validate)
userService := sUser.NewUserService(userRepo, validate) userService := sUser.NewUserService(userRepo, validate)
PaymentRoutes(router, userService, paymentService) PaymentRoutes(router, userService, paymentService)
@@ -32,12 +32,14 @@ type paymentService struct {
Validate *validator.Validate Validate *validator.Validate
Repository repository.PaymentRepository Repository repository.PaymentRepository
ApprovalSvc commonSvc.ApprovalService ApprovalSvc commonSvc.ApprovalService
FifoPaymentSvc commonSvc.FifoPaymentService
approvalWorkflow approvalutils.ApprovalWorkflowKey approvalWorkflow approvalutils.ApprovalWorkflowKey
} }
func NewPaymentService( func NewPaymentService(
repo repository.PaymentRepository, repo repository.PaymentRepository,
approvalSvc commonSvc.ApprovalService, approvalSvc commonSvc.ApprovalService,
fifoPaymentSvc commonSvc.FifoPaymentService,
validate *validator.Validate, validate *validator.Validate,
) PaymentService { ) PaymentService {
return &paymentService{ return &paymentService{
@@ -45,6 +47,7 @@ func NewPaymentService(
Validate: validate, Validate: validate,
Repository: repo, Repository: repo,
ApprovalSvc: approvalSvc, ApprovalSvc: approvalSvc,
FifoPaymentSvc: fifoPaymentSvc,
approvalWorkflow: utils.ApprovalWorkflowPayment, approvalWorkflow: utils.ApprovalWorkflowPayment,
} }
} }
@@ -159,6 +162,12 @@ func (s *paymentService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
} }
} }
if s.FifoPaymentSvc != nil {
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), dbTransaction, createBody.PartyType, createBody.PartyId); err != nil {
return err
}
}
return nil return nil
}) })
if err != nil { if err != nil {
@@ -251,7 +260,46 @@ func (s paymentService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
return s.GetOne(c, id) return s.GetOne(c, id)
} }
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil { // Snapshot party lama untuk reallocate kalau party baru berbeda.
existing, err := s.Repository.GetByID(c.Context(), id, nil)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Payment not found")
}
if err != nil {
s.Log.Errorf("Failed get payment for update: %+v", err)
return nil, err
}
oldPartyType := existing.PartyType
oldPartyID := existing.PartyId
newPartyType := oldPartyType
newPartyID := oldPartyID
if v, ok := updateBody["party_type"].(string); ok {
newPartyType = v
}
if v, ok := updateBody["party_id"].(uint); ok {
newPartyID = v
}
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
paymentRepoTx := repository.NewPaymentRepository(tx)
if err := paymentRepoTx.PatchOne(c.Context(), id, updateBody, nil); err != nil {
return err
}
if s.FifoPaymentSvc != nil {
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), tx, newPartyType, newPartyID); err != nil {
return err
}
if oldPartyType != newPartyType || oldPartyID != newPartyID {
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), tx, oldPartyType, oldPartyID); err != nil {
return err
}
}
}
return nil
})
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Payment not found") return nil, fiber.NewError(fiber.StatusNotFound, "Payment not found")
} }
@@ -35,7 +35,8 @@ func (TransactionModule) RegisterRoutes(router fiber.Router, db *gorm.DB, valida
panic(fmt.Sprintf("failed to register injection approval workflow: %v", err)) panic(fmt.Sprintf("failed to register injection approval workflow: %v", err))
} }
transactionService := sTransaction.NewTransactionService(transactionRepo, approvalService, validate) fifoPaymentService := commonSvc.NewFifoPaymentService(db, utils.Log)
transactionService := sTransaction.NewTransactionService(transactionRepo, approvalService, fifoPaymentService, validate)
userService := sUser.NewUserService(userRepo, validate) userService := sUser.NewUserService(userRepo, validate)
TransactionRoutes(router, userService, transactionService) TransactionRoutes(router, userService, transactionService)
@@ -30,19 +30,22 @@ type transactionService struct {
Validate *validator.Validate Validate *validator.Validate
Repository repository.TransactionRepository Repository repository.TransactionRepository
ApprovalSvc commonSvc.ApprovalService ApprovalSvc commonSvc.ApprovalService
FifoPaymentSvc commonSvc.FifoPaymentService
approvalWorkflows map[string]approvalutils.ApprovalWorkflowKey approvalWorkflows map[string]approvalutils.ApprovalWorkflowKey
} }
func NewTransactionService( func NewTransactionService(
repo repository.TransactionRepository, repo repository.TransactionRepository,
approvalSvc commonSvc.ApprovalService, approvalSvc commonSvc.ApprovalService,
fifoPaymentSvc commonSvc.FifoPaymentService,
validate *validator.Validate, validate *validator.Validate,
) TransactionService { ) TransactionService {
return &transactionService{ return &transactionService{
Log: utils.Log, Log: utils.Log,
Validate: validate, Validate: validate,
Repository: repo, Repository: repo,
ApprovalSvc: approvalSvc, ApprovalSvc: approvalSvc,
FifoPaymentSvc: fifoPaymentSvc,
approvalWorkflows: map[string]approvalutils.ApprovalWorkflowKey{ approvalWorkflows: map[string]approvalutils.ApprovalWorkflowKey{
string(utils.TransactionTypeSaldoAwal): utils.ApprovalWorkflowInitial, string(utils.TransactionTypeSaldoAwal): utils.ApprovalWorkflowInitial,
string(utils.TransactionTypeInjection): utils.ApprovalWorkflowInjection, string(utils.TransactionTypeInjection): utils.ApprovalWorkflowInjection,
@@ -182,6 +185,19 @@ func (s transactionService) GetOne(c *fiber.Ctx, id uint) (*entity.Payment, erro
} }
func (s transactionService) DeleteOne(c *fiber.Ctx, id uint) error { func (s transactionService) DeleteOne(c *fiber.Ctx, id uint) error {
// Snapshot party SEBELUM delete supaya bisa re-FIFO setelah trigger DB
// (`trg_soft_delete_fk_payments`) CASCADE hard-DELETE allocations.
existing, err := s.Repository.GetByID(c.Context(), id, nil)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Transaction not found")
}
s.Log.Errorf("Failed to load transaction before delete: %+v", err)
return err
}
partyType := existing.PartyType
partyID := existing.PartyId
if err := s.Repository.DeleteOne(c.Context(), id); err != nil { if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Transaction not found") return fiber.NewError(fiber.StatusNotFound, "Transaction not found")
@@ -189,6 +205,14 @@ func (s transactionService) DeleteOne(c *fiber.Ctx, id uint) error {
s.Log.Errorf("Failed to delete transaction: %+v", err) s.Log.Errorf("Failed to delete transaction: %+v", err)
return err return err
} }
// Re-FIFO setelah delete agar payment lain yang masih punya unallocated nominal
// otomatis reflow ke MDP/purchase_item/expense_realization yang kekurangan paid.
if s.FifoPaymentSvc != nil && partyID > 0 {
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), nil, partyType, partyID); err != nil {
s.Log.Warnf("Failed to reallocate payments after delete (party=%s id=%d): %+v", partyType, partyID, err)
}
}
return nil return nil
} }
@@ -65,6 +65,7 @@ func (TransferModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
expenseRealizationRepo, expenseRealizationRepo,
projectFlockKandangRepo, projectFlockKandangRepo,
documentSvc, documentSvc,
commonSvc.NewFifoPaymentService(db, utils.Log),
validate, validate,
) )
+2 -1
View File
@@ -35,6 +35,7 @@ func (MarketingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
stockLogRepo := rShared.NewStockLogRepository(db) stockLogRepo := rShared.NewStockLogRepository(db)
fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log) fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)
fifoPaymentService := commonSvc.NewFifoPaymentService(db, utils.Log)
approvalRepo := commonRepo.NewApprovalRepository(db) approvalRepo := commonRepo.NewApprovalRepository(db)
approvalSvc := commonSvc.NewApprovalService(approvalRepo) approvalSvc := commonSvc.NewApprovalService(approvalRepo)
@@ -47,7 +48,7 @@ func (MarketingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db) projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
salesOrdersService := service.NewSalesOrdersService(marketingRepo, customerRepo, productWarehouseRepo, userRepo, approvalSvc, fifoStockV2Service, warehouseRepo, projectFlockKandangRepo, validate) salesOrdersService := service.NewSalesOrdersService(marketingRepo, customerRepo, productWarehouseRepo, userRepo, approvalSvc, fifoStockV2Service, warehouseRepo, projectFlockKandangRepo, validate)
deliveryOrdersService := service.NewDeliveryOrdersService(marketingRepo, marketingProductRepo, marketingDeliveryProductRepo, stockLogRepo, productWarehouseRepo, projectFlockPopulationRepo, approvalSvc, fifoStockV2Service, validate) deliveryOrdersService := service.NewDeliveryOrdersService(marketingRepo, marketingProductRepo, marketingDeliveryProductRepo, stockLogRepo, productWarehouseRepo, projectFlockPopulationRepo, approvalSvc, fifoStockV2Service, fifoPaymentService, validate)
userService := sUser.NewUserService(userRepo, validate) userService := sUser.NewUserService(userRepo, validate)
RegisterRoutes(router, userService, salesOrdersService, deliveryOrdersService) RegisterRoutes(router, userService, salesOrdersService, deliveryOrdersService)
@@ -48,6 +48,7 @@ type deliveryOrdersService struct {
ProjectFlockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository ProjectFlockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository
ApprovalSvc commonSvc.ApprovalService ApprovalSvc commonSvc.ApprovalService
FifoStockV2Svc commonSvc.FifoStockV2Service FifoStockV2Svc commonSvc.FifoStockV2Service
FifoPaymentSvc commonSvc.FifoPaymentService
} }
func NewDeliveryOrdersService( func NewDeliveryOrdersService(
@@ -59,6 +60,7 @@ func NewDeliveryOrdersService(
projectFlockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository, projectFlockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository,
approvalSvc commonSvc.ApprovalService, approvalSvc commonSvc.ApprovalService,
fifoStockV2Svc commonSvc.FifoStockV2Service, fifoStockV2Svc commonSvc.FifoStockV2Service,
fifoPaymentSvc commonSvc.FifoPaymentService,
validate *validator.Validate, validate *validator.Validate,
) DeliveryOrdersService { ) DeliveryOrdersService {
return &deliveryOrdersService{ return &deliveryOrdersService{
@@ -71,6 +73,22 @@ func NewDeliveryOrdersService(
ProjectFlockPopulationRepo: projectFlockPopulationRepo, ProjectFlockPopulationRepo: projectFlockPopulationRepo,
ApprovalSvc: approvalSvc, ApprovalSvc: approvalSvc,
FifoStockV2Svc: fifoStockV2Svc, FifoStockV2Svc: fifoStockV2Svc,
FifoPaymentSvc: fifoPaymentSvc,
}
}
// reallocateAfterDelivery refresh marketing.grand_total + reallocate FIFO untuk customer.
func (s *deliveryOrdersService) reallocateAfterDelivery(ctx context.Context, marketingID uint, customerID uint) {
if s.FifoPaymentSvc == nil {
return
}
if err := s.FifoPaymentSvc.RecomputeGrandTotal(ctx, nil, commonSvc.ParentKindMarketing, marketingID); err != nil {
utils.Log.Warnf("Failed to recompute grand_total for marketing %d: %+v", marketingID, err)
}
if customerID > 0 {
if err := s.FifoPaymentSvc.ReallocateForParty(ctx, nil, string(utils.PaymentPartyCustomer), customerID); err != nil {
utils.Log.Warnf("Failed to reallocate payments for customer %d: %+v", customerID, err)
}
} }
} }
@@ -418,6 +436,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
return nil, fiber.NewError(fiber.StatusBadRequest, "Delivery order already exists for this marketing") return nil, fiber.NewError(fiber.StatusBadRequest, "Delivery order already exists for this marketing")
} }
var capturedCustomerID uint
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error { err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction) marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction) marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
@@ -428,6 +447,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing") return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
} }
capturedCustomerID = marketing.CustomerId
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), req.MarketingId) allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), req.MarketingId)
if err != nil { if err != nil {
@@ -519,6 +539,8 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order") return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order")
} }
s.reallocateAfterDelivery(c.Context(), req.MarketingId, capturedCustomerID)
return s.getMarketingWithDeliveries(c, req.MarketingId) return s.getMarketingWithDeliveries(c, req.MarketingId)
} }
@@ -547,6 +569,7 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status") return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
} }
var capturedCustomerID uint
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error { err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction) marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction) marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
@@ -557,6 +580,7 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing") return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
} }
capturedCustomerID = marketing.CustomerId
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), id) allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), id)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
@@ -662,6 +686,8 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery order") return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery order")
} }
s.reallocateAfterDelivery(c.Context(), id, capturedCustomerID)
return s.getMarketingWithDeliveries(c, id) return s.getMarketingWithDeliveries(c, id)
} }
@@ -387,16 +387,12 @@ func (s productionStandardService) EnsureWeekAvailable(ctx context.Context, stan
return nil return nil
} }
upperCategory := strings.ToUpper(category) week := ((day - 1) / 7) + 1
weekBase := 1
if upperCategory == string(utils.ProjectFlockCategoryLaying) {
weekBase = config.LayingWeekStart()
}
week := ((day - 1) / 7) + weekBase
if week <= 0 { if week <= 0 {
return nil return nil
} }
upperCategory := strings.ToUpper(category)
if upperCategory == string(utils.ProjectFlockCategoryLaying) { if upperCategory == string(utils.ProjectFlockCategoryLaying) {
detail, err := s.ProductionStandardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week) detail, err := s.ProductionStandardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week)
if err != nil { if err != nil {
+3
View File
@@ -61,6 +61,7 @@ func (PurchaseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
expenseRealizationRepo, expenseRealizationRepo,
projectFlockKandangRepository, projectFlockKandangRepository,
documentSvc, documentSvc,
commonSvc.NewFifoPaymentService(db, utils.Log),
validate, validate,
) )
expenseBridge := service.NewExpenseBridge( expenseBridge := service.NewExpenseBridge(
@@ -72,6 +73,7 @@ func (PurchaseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
) )
fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log) fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)
fifoPaymentService := commonSvc.NewFifoPaymentService(db, utils.Log)
purchaseService := service.NewPurchaseService( purchaseService := service.NewPurchaseService(
validate, validate,
@@ -84,6 +86,7 @@ func (PurchaseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
approvalService, approvalService,
expenseBridge, expenseBridge,
fifoStockV2Service, fifoStockV2Service,
fifoPaymentService,
documentSvc, documentSvc,
) )
@@ -24,7 +24,6 @@ type PurchaseRepository interface {
UpdateReceivingDetails(ctx context.Context, purchaseID uint, updates []PurchaseReceivingUpdate) error UpdateReceivingDetails(ctx context.Context, purchaseID uint, updates []PurchaseReceivingUpdate) error
DeleteItems(ctx context.Context, purchaseID uint, itemIDs []uint) error DeleteItems(ctx context.Context, purchaseID uint, itemIDs []uint) error
NextPrNumber(ctx context.Context, tx *gorm.DB) (string, error) NextPrNumber(ctx context.Context, tx *gorm.DB) (string, error)
NextPoNumber(ctx context.Context, tx *gorm.DB) (string, error)
BackfillProjectFlockKandang(ctx context.Context, purchaseID uint) error BackfillProjectFlockKandang(ctx context.Context, purchaseID uint) error
SoftDeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) error SoftDeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) error
GetItemsByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.PurchaseItem, error) GetItemsByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.PurchaseItem, error)
@@ -369,9 +368,8 @@ func (r *PurchaseRepositoryImpl) NextPrNumber(ctx context.Context, tx *gorm.DB)
return r.generateSequentialNumber(ctx, tx, "pr_number", utils.PurchasePRNumberPrefix, utils.PurchaseNumberPadding) return r.generateSequentialNumber(ctx, tx, "pr_number", utils.PurchasePRNumberPrefix, utils.PurchaseNumberPadding)
} }
func (r *PurchaseRepositoryImpl) NextPoNumber(ctx context.Context, tx *gorm.DB) (string, error) { // NOTE: NextPoNumber dihapus per migration 20260529143940 — po_number sekarang
return r.generateSequentialNumber(ctx, tx, "po_number", utils.PurchasePONumberPrefix, utils.PurchaseNumberPadding) // di-derive dari pr_number (swap prefix) via derivePoFromPr di purchase.service.go.
}
func (r *PurchaseRepositoryImpl) generateSequentialNumber(ctx context.Context, tx *gorm.DB, column, prefix string, padding int) (string, error) { func (r *PurchaseRepositoryImpl) generateSequentialNumber(ctx context.Context, tx *gorm.DB, column, prefix string, padding int) (string, error) {
db := tx db := tx
@@ -64,6 +64,7 @@ type purchaseService struct {
ApprovalSvc commonSvc.ApprovalService ApprovalSvc commonSvc.ApprovalService
ExpenseBridge PurchaseExpenseBridge ExpenseBridge PurchaseExpenseBridge
FifoStockV2Svc commonSvc.FifoStockV2Service FifoStockV2Svc commonSvc.FifoStockV2Service
FifoPaymentSvc commonSvc.FifoPaymentService
DocumentSvc commonSvc.DocumentService DocumentSvc commonSvc.DocumentService
approvalWorkflow approvalutils.ApprovalWorkflowKey approvalWorkflow approvalutils.ApprovalWorkflowKey
} }
@@ -91,6 +92,7 @@ func NewPurchaseService(
approvalSvc commonSvc.ApprovalService, approvalSvc commonSvc.ApprovalService,
expenseBridge PurchaseExpenseBridge, expenseBridge PurchaseExpenseBridge,
fifoStockV2Svc commonSvc.FifoStockV2Service, fifoStockV2Svc commonSvc.FifoStockV2Service,
fifoPaymentSvc commonSvc.FifoPaymentService,
documentSvc commonSvc.DocumentService, documentSvc commonSvc.DocumentService,
) PurchaseService { ) PurchaseService {
return &purchaseService{ return &purchaseService{
@@ -105,6 +107,7 @@ func NewPurchaseService(
ApprovalSvc: approvalSvc, ApprovalSvc: approvalSvc,
ExpenseBridge: expenseBridge, ExpenseBridge: expenseBridge,
FifoStockV2Svc: fifoStockV2Svc, FifoStockV2Svc: fifoStockV2Svc,
FifoPaymentSvc: fifoPaymentSvc,
DocumentSvc: documentSvc, DocumentSvc: documentSvc,
approvalWorkflow: utils.ApprovalWorkflowPurchase, approvalWorkflow: utils.ApprovalWorkflowPurchase,
} }
@@ -776,8 +779,7 @@ func (s *purchaseService) ApproveManagerPurchase(c *fiber.Ctx, id uint, req *val
transactionErr := s.PurchaseRepo.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error { transactionErr := s.PurchaseRepo.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
updateData := map[string]any{} updateData := map[string]any{}
if !hasExistingPO { if !hasExistingPO {
repoTx := rPurchase.NewPurchaseRepository(tx) code, err := derivePoFromPr(purchase.PrNumber)
code, err := repoTx.NextPoNumber(c.Context(), tx)
if err != nil { if err != nil {
return err return err
} }
@@ -1406,6 +1408,16 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
return nil, err return nil, err
} }
// Refresh purchase.grand_total + reallocate payment FIFO untuk supplier (new debt baru emerges).
if s.FifoPaymentSvc != nil && receivingAction == entity.ApprovalActionApproved {
if err := s.FifoPaymentSvc.RecomputeGrandTotal(c.Context(), nil, commonSvc.ParentKindPurchase, purchase.Id); err != nil {
s.Log.Warnf("Failed to recompute grand_total for purchase %d: %+v", purchase.Id, err)
}
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), nil, string(utils.PaymentPartySupplier), uint(purchase.SupplierId)); err != nil {
s.Log.Warnf("Failed to reallocate payments for supplier %d: %+v", purchase.SupplierId, err)
}
}
return updated, nil return updated, nil
} }
@@ -2500,6 +2512,18 @@ func parseApprovalActionInput(raw string) (entity.ApprovalAction, error) {
} }
} }
// derivePoFromPr menghasilkan po_number dari pr_number dengan swap prefix.
// Contoh: "PR-LTI-0050" -> "PO-LTI-0050". Mengembalikan error kalau pr_number
// tidak diawali prefix standar — caller harus memastikan PR sudah valid.
func derivePoFromPr(prNumber string) (string, error) {
trimmed := strings.TrimSpace(prNumber)
if !strings.HasPrefix(trimmed, utils.PurchasePRNumberPrefix) {
return "", fmt.Errorf("invalid pr_number %q: missing prefix %q", trimmed, utils.PurchasePRNumberPrefix)
}
suffix := strings.TrimPrefix(trimmed, utils.PurchasePRNumberPrefix)
return utils.PurchasePONumberPrefix + suffix, nil
}
func (s *purchaseService) rejectAndReload( func (s *purchaseService) rejectAndReload(
c *fiber.Ctx, c *fiber.Ctx,
step approvalutils.ApprovalStep, step approvalutils.ApprovalStep,
@@ -26,6 +26,8 @@ type ExpenseDepreciationRowDTO struct {
DayN int `json:"day_n"` DayN int `json:"day_n"`
ChickinDate string `json:"chickin_date"` ChickinDate string `json:"chickin_date"`
TotalValuePulletAfterDepreciation float64 `json:"total_value_pullet_after_depreciation"` TotalValuePulletAfterDepreciation float64 `json:"total_value_pullet_after_depreciation"`
StandardEffectiveDate string `json:"standard_effective_date,omitempty"`
TotalPopulation float64 `json:"total_population"`
Components any `json:"components"` Components any `json:"components"`
} }
@@ -41,6 +41,7 @@ type houseMultiplicationPercentageRow struct {
HouseType string HouseType string
Day int Day int
MultiplicationPercentage float64 MultiplicationPercentage float64
EffectiveDate *time.Time
} }
type ExpenseDepreciationRepository interface { type ExpenseDepreciationRepository interface {
@@ -50,7 +51,7 @@ type ExpenseDepreciationRepository interface {
DeleteSnapshotsFromDate(ctx context.Context, fromDate time.Time, farmIDs []uint) error DeleteSnapshotsFromDate(ctx context.Context, fromDate time.Time, farmIDs []uint) error
DeleteSnapshotsByFarmIDs(ctx context.Context, farmIDs []uint) error DeleteSnapshotsByFarmIDs(ctx context.Context, farmIDs []uint) error
GetLatestTransferInputsByFarms(ctx context.Context, period time.Time, farmIDs []uint) ([]FarmDepreciationLatestTransferRow, error) GetLatestTransferInputsByFarms(ctx context.Context, period time.Time, farmIDs []uint) ([]FarmDepreciationLatestTransferRow, error)
GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, error) GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, map[string]*time.Time, error)
GetLatestManualInputsByFarms(ctx context.Context, areaIDs, locationIDs, projectFlockIDs []int64) ([]FarmDepreciationManualInputRow, error) GetLatestManualInputsByFarms(ctx context.Context, areaIDs, locationIDs, projectFlockIDs []int64) ([]FarmDepreciationManualInputRow, error)
UpsertManualInput(ctx context.Context, row *entity.FarmDepreciationManualInput) error UpsertManualInput(ctx context.Context, row *entity.FarmDepreciationManualInput) error
DB() *gorm.DB DB() *gorm.DB
@@ -244,21 +245,22 @@ func (r *expenseDepreciationRepository) GetMultiplicationPercentages(
ctx context.Context, ctx context.Context,
houseTypes []string, houseTypes []string,
maxDay int, maxDay int,
) (map[string]map[int]float64, error) { ) (map[string]map[int]float64, map[string]*time.Time, error) {
result := make(map[string]map[int]float64) result := make(map[string]map[int]float64)
effectiveDates := make(map[string]*time.Time)
if len(houseTypes) == 0 || maxDay <= 0 { if len(houseTypes) == 0 || maxDay <= 0 {
return result, nil return result, effectiveDates, nil
} }
rows := make([]houseMultiplicationPercentageRow, 0) rows := make([]houseMultiplicationPercentageRow, 0)
if err := r.db.WithContext(ctx).Raw(` if err := r.db.WithContext(ctx).Raw(`
SELECT DISTINCT ON (house_type::text, day) SELECT DISTINCT ON (house_type::text, day)
house_type::text AS house_type, day, multiplication_percentage house_type::text AS house_type, day, multiplication_percentage, effective_date
FROM house_depreciation_standards FROM house_depreciation_standards
WHERE house_type::text IN ? AND day <= ? WHERE house_type::text IN ? AND day <= ?
ORDER BY house_type, day, effective_date DESC NULLS LAST ORDER BY house_type, day, effective_date DESC NULLS LAST
`, houseTypes, maxDay).Scan(&rows).Error; err != nil { `, houseTypes, maxDay).Scan(&rows).Error; err != nil {
return nil, err return nil, nil, err
} }
for _, row := range rows { for _, row := range rows {
@@ -266,9 +268,12 @@ func (r *expenseDepreciationRepository) GetMultiplicationPercentages(
result[row.HouseType] = make(map[int]float64) result[row.HouseType] = make(map[int]float64)
} }
result[row.HouseType][row.Day] = row.MultiplicationPercentage result[row.HouseType][row.Day] = row.MultiplicationPercentage
if _, tracked := effectiveDates[row.HouseType]; !tracked {
effectiveDates[row.HouseType] = row.EffectiveDate
}
} }
return result, nil return result, effectiveDates, nil
} }
func (r *expenseDepreciationRepository) GetLatestManualInputsByFarms( func (r *expenseDepreciationRepository) GetLatestManualInputsByFarms(
@@ -304,7 +304,8 @@ func (s *repportService) GetExpenseDepreciation(ctx *fiber.Ctx) ([]dto.ExpenseDe
continue continue
} }
components := parseSnapshotComponents(snapshot.Components) components := parseSnapshotComponents(snapshot.Components)
multiplicationPercentage, dayN, chickinDate := depreciationSnapshotInfo(components) multiplicationPercentage, dayN, chickinDate, standardEffectiveDate := depreciationSnapshotInfo(components)
totalPopulation := depreciationTotalPopulation(components)
rows = append(rows, dto.ExpenseDepreciationRowDTO{ rows = append(rows, dto.ExpenseDepreciationRowDTO{
ProjectFlockID: int64(snapshot.ProjectFlockId), ProjectFlockID: int64(snapshot.ProjectFlockId),
FarmName: candidate.FarmName, FarmName: candidate.FarmName,
@@ -316,6 +317,8 @@ func (s *repportService) GetExpenseDepreciation(ctx *fiber.Ctx) ([]dto.ExpenseDe
DayN: dayN, DayN: dayN,
ChickinDate: chickinDate, ChickinDate: chickinDate,
TotalValuePulletAfterDepreciation: snapshot.PulletCostDayNTotal - snapshot.DepreciationValue, TotalValuePulletAfterDepreciation: snapshot.PulletCostDayNTotal - snapshot.DepreciationValue,
StandardEffectiveDate: standardEffectiveDate,
TotalPopulation: totalPopulation,
Components: components, Components: components,
}) })
} }
@@ -502,11 +505,14 @@ type depreciationKandangComponent struct {
OriginDate string `json:"origin_date,omitempty"` OriginDate string `json:"origin_date,omitempty"`
ChickinDate string `json:"chickin_date,omitempty"` ChickinDate string `json:"chickin_date,omitempty"`
StartScheduleDay *int `json:"start_schedule_day,omitempty"` StartScheduleDay *int `json:"start_schedule_day,omitempty"`
StandardEffectiveDate string `json:"standard_effective_date,omitempty"`
Population float64 `json:"population"`
} }
type depreciationFarmComponents struct { type depreciationFarmComponents struct {
KandangCount int `json:"kandang_count"` KandangCount int `json:"kandang_count"`
Kandang []depreciationKandangComponent `json:"kandang"` TotalPopulation float64 `json:"total_population"`
Kandang []depreciationKandangComponent `json:"kandang"`
} }
func (s *repportService) computeExpenseDepreciationSnapshots( func (s *repportService) computeExpenseDepreciationSnapshots(
@@ -540,6 +546,7 @@ func (s *repportService) computeExpenseDepreciationSnapshots(
totalDepreciationValue := 0.0 totalDepreciationValue := 0.0
totalPulletCostDayN := 0.0 totalPulletCostDayN := 0.0
totalPopulation := 0.0
for _, kandangID := range kandangIDs { for _, kandangID := range kandangIDs {
breakdown, err := s.HppV2Svc.CalculateHppBreakdown(kandangID, &periodDate) breakdown, err := s.HppV2Svc.CalculateHppBreakdown(kandangID, &periodDate)
if err != nil { if err != nil {
@@ -575,6 +582,8 @@ func (s *repportService) computeExpenseDepreciationSnapshots(
DepreciationSource: part.Code, DepreciationSource: part.Code,
OriginDate: hppV2DetailString(part.Details, "origin_date"), OriginDate: hppV2DetailString(part.Details, "origin_date"),
ChickinDate: hppV2DetailString(part.Details, "origin_date"), ChickinDate: hppV2DetailString(part.Details, "origin_date"),
StandardEffectiveDate: hppV2DetailString(part.Details, "standard_effective_date"),
Population: hppV2DetailFloat(part.Details, "kandang_population"),
} }
if component.HouseType == "" { if component.HouseType == "" {
@@ -605,11 +614,13 @@ func (s *repportService) computeExpenseDepreciationSnapshots(
totalPulletCostDayN += component.PulletCostDayN totalPulletCostDayN += component.PulletCostDayN
totalDepreciationValue += component.DepreciationValue totalDepreciationValue += component.DepreciationValue
totalPopulation += component.Population
components.Kandang = append(components.Kandang, component) components.Kandang = append(components.Kandang, component)
} }
} }
components.KandangCount = len(components.Kandang) components.KandangCount = len(components.Kandang)
components.TotalPopulation = totalPopulation
effectivePercent := approvalService.CalculateEffectiveDepreciationPercent(totalDepreciationValue, totalPulletCostDayN) effectivePercent := approvalService.CalculateEffectiveDepreciationPercent(totalDepreciationValue, totalPulletCostDayN)
componentsJSON, marshalErr := json.Marshal(components) componentsJSON, marshalErr := json.Marshal(components)
@@ -744,14 +755,14 @@ func parseSnapshotComponents(raw []byte) any {
return out return out
} }
func depreciationSnapshotInfo(components any) (float64, int, string) { func depreciationSnapshotInfo(components any) (float64, int, string, string) {
root, ok := components.(map[string]any) root, ok := components.(map[string]any)
if !ok { if !ok {
return 0, 0, "" return 0, 0, "", ""
} }
kandang, ok := root["kandang"].([]any) kandang, ok := root["kandang"].([]any)
if !ok { if !ok {
return 0, 0, "" return 0, 0, "", ""
} }
for _, raw := range kandang { for _, raw := range kandang {
component, ok := raw.(map[string]any) component, ok := raw.(map[string]any)
@@ -765,10 +776,19 @@ func depreciationSnapshotInfo(components any) (float64, int, string) {
chickinDate = anyString(component["origin_date"]) chickinDate = anyString(component["origin_date"])
} }
if dayN > 0 || multiplicationPercentage > 0 || chickinDate != "" { if dayN > 0 || multiplicationPercentage > 0 || chickinDate != "" {
return multiplicationPercentage, dayN, chickinDate standardEffectiveDate := anyString(component["standard_effective_date"])
return multiplicationPercentage, dayN, chickinDate, standardEffectiveDate
} }
} }
return 0, 0, "" return 0, 0, "", ""
}
func depreciationTotalPopulation(components any) float64 {
root, ok := components.(map[string]any)
if !ok {
return 0
}
return anyFloat(root["total_population"])
} }
func anyFloat(raw any) float64 { func anyFloat(raw any) float64 {
@@ -831,37 +851,28 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing
customerGroups[customerID] = append(customerGroups[customerID], dp) customerGroups[customerID] = append(customerGroups[customerID], dp)
} }
// Aging untuk setiap MDP berdasarkan payment_allocations: LUNAS pakai last_payment_date,
// else pakai today.
agingMap := make(map[int]int) agingMap := make(map[int]int)
for customerID := range customerGroups { allMdpIDsForAging := make([]uint, 0)
transactions, err := s.CustomerPaymentRepo.GetCustomerPaymentTransactions(c.Context(), &customerID) for _, dp := range deliveryProducts {
if err != nil { allMdpIDsForAging = append(allMdpIDsForAging, dp.Id)
continue }
} mdpAllocSummaryForMarketing, err := s.fetchMdpAllocationSummary(c.Context(), allMdpIDsForAging)
if err != nil {
initialBalance, err := s.CustomerPaymentRepo.GetInitialBalanceByCustomer(c.Context(), customerID) return nil, 0, err
if err != nil { }
initialBalance = 0 for _, dp := range deliveryProducts {
} summary := mdpAllocSummaryForMarketing[dp.Id]
soDate := dp.MarketingProduct.Marketing.SoDate
runningBalance := initialBalance if customerPaymentStatusFromAllocation(dp.TotalPrice, summary.PaidAmount) == "LUNAS" && !summary.LastPaymentDate.IsZero() {
for i, tx := range transactions { days := int(summary.LastPaymentDate.Sub(soDate).Hours() / 24)
if tx.TransactionType == "SALES" { if days < 0 {
previousBalance := runningBalance days = 0
runningBalance -= tx.TotalPrice
currentBalance := runningBalance
_, paymentDate := s.determineSalesStatusAndPaymentDate(transactions, i, previousBalance, currentBalance)
if paymentDate != nil {
agingDays := int(paymentDate.Sub(tx.TransDate).Hours() / 24)
agingMap[int(tx.TransactionID)] = agingDays
} else {
agingDays := int(time.Since(tx.TransDate).Hours() / 24)
agingMap[int(tx.TransactionID)] = agingDays
}
} else if tx.TransactionType == "PAYMENT" {
runningBalance += tx.PaymentAmount
} }
agingMap[int(dp.Id)] = days
} else {
agingMap[int(dp.Id)] = int(time.Since(soDate).Hours() / 24)
} }
} }
@@ -1250,28 +1261,39 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
return dto.CustomerPaymentReportItem{}, err return dto.CustomerPaymentReportItem{}, err
} }
// Batch fetch payment allocation summaries untuk semua SALES rows (per MDP).
mdpIDs := make([]uint, 0)
for _, tx := range transactions {
if tx.TransactionType == "SALES" && tx.TransactionID > 0 {
mdpIDs = append(mdpIDs, uint(tx.TransactionID))
}
}
mdpAllocSummary, err := s.fetchMdpAllocationSummary(ctx, mdpIDs)
if err != nil {
return dto.CustomerPaymentReportItem{}, err
}
rows := make([]dto.CustomerPaymentReportRow, 0, len(transactions)) rows := make([]dto.CustomerPaymentReportRow, 0, len(transactions))
runningBalance := initialBalance runningBalance := initialBalance
for i, tx := range transactions { for _, tx := range transactions {
previousBalance := runningBalance
row := dto.ToCustomerPaymentReportRow(tx) row := dto.ToCustomerPaymentReportRow(tx)
if tx.TransactionType == "SALES" { if tx.TransactionType == "SALES" {
runningBalance -= tx.TotalPrice runningBalance -= tx.TotalPrice
status, paymentDate := s.determineSalesStatusAndPaymentDate(transactions, i, previousBalance, runningBalance) summary := mdpAllocSummary[uint(tx.TransactionID)]
row.Status = status row.Status = customerPaymentStatusFromAllocation(tx.TotalPrice, summary.PaidAmount)
if status == "LUNAS" { if row.Status == "LUNAS" && !summary.LastPaymentDate.IsZero() {
if paymentDate != nil { days := int(summary.LastPaymentDate.Sub(tx.TransDate).Hours() / 24)
days := int(paymentDate.Sub(tx.TransDate).Hours() / 24) if days < 0 {
row.AgingDay = &days days = 0
} else {
days := 0
row.AgingDay = &days
} }
row.AgingDay = &days
} else if row.Status == "LUNAS" {
zero := 0
row.AgingDay = &zero
} else { } else {
days := int(time.Since(tx.TransDate).Hours() / 24) days := int(time.Since(tx.TransDate).Hours() / 24)
row.AgingDay = &days row.AgingDay = &days
@@ -1343,91 +1365,19 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
return dto.ToCustomerPaymentReportItem(*customer, initialBalance, rows, summary), nil return dto.ToCustomerPaymentReportItem(*customer, initialBalance, rows, summary), nil
} }
func (s *repportService) determineSalesStatusAndPaymentDate(transactions []repportRepo.CustomerPaymentTransaction, currentIndex int, previousBalance, currentBalance float64) (string, *time.Time) { // customerPaymentStatusFromAllocation menentukan status per-MDP berdasarkan
currentSales := transactions[currentIndex] // SUM(payment_allocations.amount) vs MDP total_price.
func customerPaymentStatusFromAllocation(totalPrice, paidAmount float64) string {
if previousBalance >= currentSales.TotalPrice { if totalPrice <= fifoAllocationEpsilon {
type paymentAllocation struct { return "LUNAS"
date time.Time
amount float64
consumed float64
}
allocations := []paymentAllocation{}
runningBalance := 0.0
for i := 0; i < currentIndex; i++ {
if transactions[i].TransactionType == "PAYMENT" {
allocations = append(allocations, paymentAllocation{
date: transactions[i].TransDate,
amount: transactions[i].PaymentAmount,
consumed: 0,
})
runningBalance += transactions[i].PaymentAmount
} else if transactions[i].TransactionType == "SALES" {
salesAmount := transactions[i].TotalPrice
remainingToConsume := salesAmount
for j := range allocations {
if remainingToConsume <= 0 {
break
}
available := allocations[j].amount - allocations[j].consumed
if available > 0 {
consume := available
if consume > remainingToConsume {
consume = remainingToConsume
}
allocations[j].consumed += consume
remainingToConsume -= consume
}
}
runningBalance -= salesAmount
}
}
amountNeeded := currentSales.TotalPrice
for _, alloc := range allocations {
available := alloc.amount - alloc.consumed
if available > 0 {
if amountNeeded <= available {
return "LUNAS", &alloc.date
} else {
amountNeeded -= available
}
}
}
if len(allocations) > 0 {
return "LUNAS", &allocations[0].date
}
return "LUNAS", nil
} }
if paidAmount+fifoAllocationEpsilon >= totalPrice {
hasPartialPaymentFromBalance := previousBalance > 0 && previousBalance < currentSales.TotalPrice return "LUNAS"
futureBalance := currentBalance
hasPayment := false
var paymentDateThatMadeItLunas *time.Time
for i := currentIndex + 1; i < len(transactions); i++ {
if transactions[i].TransactionType == "PAYMENT" {
futureBalance += transactions[i].PaymentAmount
hasPayment = true
if futureBalance >= 0 {
paymentDateThatMadeItLunas = &transactions[i].TransDate
return "LUNAS", paymentDateThatMadeItLunas
}
} else if transactions[i].TransactionType == "SALES" {
futureBalance -= transactions[i].TotalPrice
}
} }
if paidAmount > fifoAllocationEpsilon {
if hasPayment || hasPartialPaymentFromBalance { return "DIBAYAR SEBAGIAN"
return "DIBAYAR SEBAGIAN", nil
} }
return "BELUM LUNAS"
return "BELUM LUNAS", nil
} }
func mapRecordingToProductionResultDTO(record entity.Recording) dto.ProductionResultDTO { func mapRecordingToProductionResultDTO(record entity.Recording) dto.ProductionResultDTO {
@@ -1893,15 +1843,6 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
return nil, 0, err return nil, 0, err
} }
expenseIDs := make([]uint64, 0, len(expenses))
for _, exp := range expenses {
expenseIDs = append(expenseIDs, exp.Id)
}
expenseWarehousesMap, err := s.DebtSupplierRepo.GetWarehousesByExpenseIDs(c.Context(), expenseIDs)
if err != nil {
return nil, 0, err
}
purchasesBySupplier := make(map[uint][]entity.Purchase, len(supplierIDs)) purchasesBySupplier := make(map[uint][]entity.Purchase, len(supplierIDs))
for _, purchase := range purchases { for _, purchase := range purchases {
purchasesBySupplier[purchase.SupplierId] = append(purchasesBySupplier[purchase.SupplierId], purchase) purchasesBySupplier[purchase.SupplierId] = append(purchasesBySupplier[purchase.SupplierId], purchase)
@@ -1951,15 +1892,34 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
DeltaBalance float64 DeltaBalance float64
CountTotals bool CountTotals bool
} }
type debtSupplierAllocation struct {
RowIndex int // Batch fetch payment allocation summaries (per purchase + per expense) untuk semua supplier.
SortTime time.Time // FIFO matching dilakukan saat payment di-create/update; report tinggal baca dari DB.
Amount float64 allPurchaseIDs := make([]uint, 0)
CalcAging func(endDate time.Time) int allExpenseIDs := make([]uint64, 0)
for _, sid := range supplierIDs {
for _, p := range purchasesBySupplier[sid] {
allPurchaseIDs = append(allPurchaseIDs, p.Id)
}
for _, e := range expensesBySupplier[sid] {
allExpenseIDs = append(allExpenseIDs, e.Id)
}
} }
type paymentAllocation struct { purchaseAllocSummary, err := s.fetchPurchaseAllocationSummary(c.Context(), allPurchaseIDs)
Date time.Time if err != nil {
Amount float64 return nil, 0, err
}
expenseAllocSummary, err := s.fetchExpenseAllocationSummary(c.Context(), allExpenseIDs)
if err != nil {
return nil, 0, err
}
// rowRef tracks which combinedRows index belongs to which purchase/expense untuk update status di-akhir.
type rowRef struct {
Index int
Kind string // "PURCHASE" / "EXPENSE"
Purchase entity.Purchase
Expense entity.Expense
} }
for _, supplierID := range supplierIDs { for _, supplierID := range supplierIDs {
@@ -1974,7 +1934,7 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
total := dto.DebtSupplierTotalDTO{} total := dto.DebtSupplierTotalDTO{}
combinedRows := make([]debtSupplierRowItem, 0, len(items)+len(paymentItems)) combinedRows := make([]debtSupplierRowItem, 0, len(items)+len(paymentItems))
purchaseAllocations := make([]debtSupplierAllocation, 0, len(items)) rowRefs := make([]rowRef, 0, len(items)+len(expensesBySupplier[supplierID]))
for _, purchase := range items { for _, purchase := range items {
row := buildDebtSupplierRow(purchase, now, location) row := buildDebtSupplierRow(purchase, now, location)
sortTime := resolveDebtSupplierSortTime(purchase, params.FilterBy, location) sortTime := resolveDebtSupplierSortTime(purchase, params.FilterBy, location)
@@ -1986,17 +1946,11 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
DeltaBalance: -row.TotalPrice, DeltaBalance: -row.TotalPrice,
CountTotals: true, CountTotals: true,
}) })
capturedPurchase := purchase rowRefs = append(rowRefs, rowRef{Index: rowIndex, Kind: "PURCHASE", Purchase: purchase})
purchaseAllocations = append(purchaseAllocations, debtSupplierAllocation{
RowIndex: rowIndex,
SortTime: sortTime,
Amount: row.TotalPrice,
CalcAging: func(endDate time.Time) int { return calculateDebtSupplierAging(capturedPurchase, endDate, location) },
})
} }
for _, exp := range expensesBySupplier[supplierID] { for _, exp := range expensesBySupplier[supplierID] {
row := buildDebtSupplierExpenseRow(exp, expenseWarehousesMap[exp.Id], now, location) row := buildDebtSupplierExpenseRow(exp, now, location)
sortTime := exp.TransactionDate.In(location) sortTime := exp.TransactionDate.In(location)
rowIndex := len(combinedRows) rowIndex := len(combinedRows)
combinedRows = append(combinedRows, debtSupplierRowItem{ combinedRows = append(combinedRows, debtSupplierRowItem{
@@ -2006,25 +1960,7 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
DeltaBalance: -row.TotalPrice, DeltaBalance: -row.TotalPrice,
CountTotals: true, CountTotals: true,
}) })
capturedExp := exp rowRefs = append(rowRefs, rowRef{Index: rowIndex, Kind: "EXPENSE", Expense: exp})
purchaseAllocations = append(purchaseAllocations, debtSupplierAllocation{
RowIndex: rowIndex,
SortTime: sortTime,
Amount: row.TotalPrice,
CalcAging: func(endDate time.Time) int { return calculateExpenseAging(capturedExp, endDate, location) },
})
}
paymentAllocations := make([]paymentAllocation, 0, len(paymentItems)+1)
initialAllocation := initialBalanceTotals[supplierID] + initialPaymentTotals[supplierID] - initialPurchaseTotals[supplierID]
paymentCarry := 0.0
if initialAllocation > 0 && len(purchaseAllocations) > 0 {
paymentAllocations = append(paymentAllocations, paymentAllocation{
Date: purchaseAllocations[0].SortTime,
Amount: initialAllocation,
})
} else if initialAllocation < 0 {
paymentCarry = -initialAllocation
} }
for _, payment := range paymentItems { for _, payment := range paymentItems {
@@ -2037,51 +1973,29 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
DeltaBalance: payment.Nominal, DeltaBalance: payment.Nominal,
CountTotals: false, CountTotals: false,
}) })
paymentAllocations = append(paymentAllocations, paymentAllocation{
Date: sortTime,
Amount: payment.Nominal,
})
} }
if len(purchaseAllocations) > 0 && len(paymentAllocations) > 0 { // Determine Status & Aging dari payment_allocations DB.
sort.SliceStable(purchaseAllocations, func(i, j int) bool { for _, ref := range rowRefs {
return purchaseAllocations[i].SortTime.Before(purchaseAllocations[j].SortTime) rowTotal := combinedRows[ref.Index].Row.TotalPrice
}) if rowTotal <= fifoAllocationEpsilon {
sort.SliceStable(paymentAllocations, func(i, j int) bool { continue
return paymentAllocations[i].Date.Before(paymentAllocations[j].Date)
})
remaining := make([]float64, len(purchaseAllocations))
for i := range purchaseAllocations {
remaining[i] = purchaseAllocations[i].Amount
} }
purchaseIndex := 0 var summary paymentAllocationSummary
for _, pay := range paymentAllocations { if ref.Kind == "PURCHASE" {
amount := pay.Amount summary = purchaseAllocSummary[ref.Purchase.Id]
if amount <= 0 { } else {
continue summary = expenseAllocSummary[ref.Expense.Id]
} }
if paymentCarry > 0 { if summary.PaidAmount+fifoAllocationEpsilon < rowTotal {
used := math.Min(amount, paymentCarry) continue
paymentCarry -= used }
amount -= used combinedRows[ref.Index].Row.Status = "Lunas"
} if !summary.LastPaymentDate.IsZero() {
for amount > 0 && purchaseIndex < len(remaining) { if ref.Kind == "PURCHASE" {
if remaining[purchaseIndex] <= 0 { combinedRows[ref.Index].Row.Aging = calculateDebtSupplierAging(ref.Purchase, summary.LastPaymentDate.In(location), location)
purchaseIndex++ } else {
continue combinedRows[ref.Index].Row.Aging = calculateExpenseAging(ref.Expense, summary.LastPaymentDate.In(location), location)
}
used := math.Min(amount, remaining[purchaseIndex])
remaining[purchaseIndex] -= used
amount -= used
if remaining[purchaseIndex] <= 0.000001 {
allocation := purchaseAllocations[purchaseIndex]
combinedRows[allocation.RowIndex].Row.Status = "Lunas"
combinedRows[allocation.RowIndex].Row.Aging = allocation.CalcAging(pay.Date)
purchaseIndex++
}
}
if purchaseIndex >= len(remaining) {
break
} }
} }
} }
@@ -2208,6 +2122,12 @@ func buildDebtSupplierRow(purchase entity.Purchase, now time.Time, loc *time.Loc
poDate = purchase.PoDate.In(loc).Format("2006-01-02") poDate = purchase.PoDate.In(loc).Format("2006-01-02")
} }
var firstWarehouse *warehouseDTO.WarehouseRelationDTO
if len(warehouses) > 0 {
w := warehouses[0]
firstWarehouse = &w
}
return dto.DebtSupplierRowDTO{ return dto.DebtSupplierRowDTO{
PrNumber: prNumber, PrNumber: prNumber,
PoNumber: poNumber, PoNumber: poNumber,
@@ -2215,7 +2135,7 @@ func buildDebtSupplierRow(purchase entity.Purchase, now time.Time, loc *time.Loc
ReceivedDate: receivedDate, ReceivedDate: receivedDate,
Aging: aging, Aging: aging,
Area: area, Area: area,
Warehouses: warehouses, Warehouse: firstWarehouse,
DueDate: dueDate, DueDate: dueDate,
DueStatus: dueStatus, DueStatus: dueStatus,
TotalPrice: totalPrice, TotalPrice: totalPrice,
@@ -2245,7 +2165,7 @@ func buildDebtSupplierPaymentRow(payment entity.Payment, loc *time.Location) dto
ReceivedDate: payment.PaymentDate.In(loc).Format("2006-01-02"), ReceivedDate: payment.PaymentDate.In(loc).Format("2006-01-02"),
Aging: 0, Aging: 0,
Area: nil, Area: nil,
Warehouses: []warehouseDTO.WarehouseRelationDTO{}, Warehouse: nil,
DueDate: "-", DueDate: "-",
DueStatus: "-", DueStatus: "-",
TotalPrice: 0, TotalPrice: 0,
@@ -2257,6 +2177,115 @@ func buildDebtSupplierPaymentRow(payment entity.Payment, loc *time.Location) dto
} }
} }
// fifoAllocationEpsilon untuk float comparison saat membandingkan paid vs total.
const fifoAllocationEpsilon = 0.001
// paymentAllocationSummary aggregates per-document paid amount + latest payment date
// from payment_allocations table, sebagai pengganti FIFO greedy in-memory.
type paymentAllocationSummary struct {
PaidAmount float64
LastPaymentDate time.Time
}
// fetchPurchaseAllocationSummary returns map[purchase_id]{paid_amount, last_payment_date}.
// paid_amount = SUM(payment_allocations.amount) untuk semua items dalam purchase.
// last_payment_date = MAX(payments.payment_date) untuk allocation tersebut.
func (s *repportService) fetchPurchaseAllocationSummary(ctx context.Context, purchaseIDs []uint) (map[uint]paymentAllocationSummary, error) {
out := make(map[uint]paymentAllocationSummary)
if len(purchaseIDs) == 0 {
return out, nil
}
type row struct {
PurchaseID uint
Total float64
LastPayment *time.Time
}
var rows []row
if err := s.db.WithContext(ctx).
Table("payment_allocations pa").
Joins("JOIN purchase_items pi ON pi.id = pa.purchase_item_id").
Joins("JOIN payments p ON p.id = pa.payment_id").
Select("pi.purchase_id AS purchase_id, SUM(pa.amount) AS total, MAX(p.payment_date) AS last_payment").
Where("pi.purchase_id IN ?", purchaseIDs).
Group("pi.purchase_id").
Scan(&rows).Error; err != nil {
return nil, err
}
for _, r := range rows {
summary := paymentAllocationSummary{PaidAmount: r.Total}
if r.LastPayment != nil {
summary.LastPaymentDate = *r.LastPayment
}
out[r.PurchaseID] = summary
}
return out, nil
}
// fetchExpenseAllocationSummary returns map[expense_id]{paid_amount, last_payment_date}.
// Allocation di expense_realization_id → JOIN expense_nonstocks → expenses.id.
func (s *repportService) fetchExpenseAllocationSummary(ctx context.Context, expenseIDs []uint64) (map[uint64]paymentAllocationSummary, error) {
out := make(map[uint64]paymentAllocationSummary)
if len(expenseIDs) == 0 {
return out, nil
}
type row struct {
ExpenseID uint64
Total float64
LastPayment *time.Time
}
var rows []row
if err := s.db.WithContext(ctx).
Table("payment_allocations pa").
Joins("JOIN expense_realizations er ON er.id = pa.expense_realization_id").
Joins("JOIN expense_nonstocks en ON en.id = er.expense_nonstock_id").
Joins("JOIN payments p ON p.id = pa.payment_id").
Select("en.expense_id AS expense_id, SUM(pa.amount) AS total, MAX(p.payment_date) AS last_payment").
Where("en.expense_id IN ?", expenseIDs).
Group("en.expense_id").
Scan(&rows).Error; err != nil {
return nil, err
}
for _, r := range rows {
summary := paymentAllocationSummary{PaidAmount: r.Total}
if r.LastPayment != nil {
summary.LastPaymentDate = *r.LastPayment
}
out[r.ExpenseID] = summary
}
return out, nil
}
// fetchMdpAllocationSummary returns map[mdp_id]{paid_amount, last_payment_date}.
func (s *repportService) fetchMdpAllocationSummary(ctx context.Context, mdpIDs []uint) (map[uint]paymentAllocationSummary, error) {
out := make(map[uint]paymentAllocationSummary)
if len(mdpIDs) == 0 {
return out, nil
}
type row struct {
MdpID uint
Total float64
LastPayment *time.Time
}
var rows []row
if err := s.db.WithContext(ctx).
Table("payment_allocations pa").
Joins("JOIN payments p ON p.id = pa.payment_id").
Select("pa.marketing_delivery_product_id AS mdp_id, SUM(pa.amount) AS total, MAX(p.payment_date) AS last_payment").
Where("pa.marketing_delivery_product_id IN ?", mdpIDs).
Group("pa.marketing_delivery_product_id").
Scan(&rows).Error; err != nil {
return nil, err
}
for _, r := range rows {
summary := paymentAllocationSummary{PaidAmount: r.Total}
if r.LastPayment != nil {
summary.LastPaymentDate = *r.LastPayment
}
out[r.MdpID] = summary
}
return out, nil
}
func resolveDebtSupplierSortTime(purchase entity.Purchase, filterBy string, loc *time.Location) time.Time { func resolveDebtSupplierSortTime(purchase entity.Purchase, filterBy string, loc *time.Location) time.Time {
if strings.EqualFold(strings.TrimSpace(filterBy), "po_date") { if strings.EqualFold(strings.TrimSpace(filterBy), "po_date") {
if purchase.PoDate != nil && !purchase.PoDate.IsZero() { if purchase.PoDate != nil && !purchase.PoDate.IsZero() {
@@ -2349,7 +2378,7 @@ func resolveDebtSupplierReceivedDate(purchase entity.Purchase, loc *time.Locatio
return time.Date(earliest.Year(), earliest.Month(), earliest.Day(), 0, 0, 0, 0, loc) return time.Date(earliest.Year(), earliest.Month(), earliest.Day(), 0, 0, 0, 0, loc)
} }
func buildDebtSupplierExpenseRow(exp entity.Expense, warehouses []entity.Warehouse, now time.Time, loc *time.Location) dto.DebtSupplierRowDTO { func buildDebtSupplierExpenseRow(exp entity.Expense, now time.Time, loc *time.Location) dto.DebtSupplierRowDTO {
txDate := exp.TransactionDate.In(loc) txDate := exp.TransactionDate.In(loc)
dateStr := txDate.Format("2006-01-02") dateStr := txDate.Format("2006-01-02")
@@ -2360,10 +2389,10 @@ func buildDebtSupplierExpenseRow(exp entity.Expense, warehouses []entity.Warehou
aging = int(endDay.Sub(startDay).Hours() / 24) aging = int(endDay.Sub(startDay).Hours() / 24)
} }
totalPrice := 0.0 // TotalPrice pakai expense.GrandTotal (= SUM realisasi) supaya konsisten dengan
for _, ns := range exp.Nonstocks { // FIFO allocation yang juga pakai realisasi. Hindari pakai SUM nonstock pengajuan
totalPrice += ns.Qty * ns.Price // karena bisa beda nilai dari realisasi → mismatch dengan paid_amount → status salah.
} totalPrice := exp.GrandTotal
var area *areaDTO.AreaRelationDTO var area *areaDTO.AreaRelationDTO
if exp.Location != nil && exp.Location.Area.Id != 0 { if exp.Location != nil && exp.Location.Area.Id != 0 {
@@ -2371,15 +2400,6 @@ func buildDebtSupplierExpenseRow(exp entity.Expense, warehouses []entity.Warehou
area = &mapped area = &mapped
} }
warehouseDTOs := make([]warehouseDTO.WarehouseRelationDTO, 0, len(warehouses))
seenWarehouseIDs := map[uint]bool{}
for _, w := range warehouses {
if w.Id != 0 && !seenWarehouseIDs[w.Id] {
seenWarehouseIDs[w.Id] = true
warehouseDTOs = append(warehouseDTOs, warehouseDTO.ToWarehouseRelationDTO(w))
}
}
poNumber := "" poNumber := ""
if strings.TrimSpace(exp.PoNumber) != "" { if strings.TrimSpace(exp.PoNumber) != "" {
poNumber = exp.PoNumber poNumber = exp.PoNumber
@@ -2392,7 +2412,7 @@ func buildDebtSupplierExpenseRow(exp entity.Expense, warehouses []entity.Warehou
ReceivedDate: dateStr, ReceivedDate: dateStr,
Aging: aging, Aging: aging,
Area: area, Area: area,
Warehouses: warehouseDTOs, Warehouse: nil,
DueDate: "-", DueDate: "-",
DueStatus: "-", DueStatus: "-",
TotalPrice: totalPrice, TotalPrice: totalPrice,