Compare commits

...

10 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 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 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
8 changed files with 910 additions and 13 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)
}
@@ -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(
@@ -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;
@@ -387,16 +387,12 @@ func (s productionStandardService) EnsureWeekAvailable(ctx context.Context, stan
return nil
}
upperCategory := strings.ToUpper(category)
weekBase := 1
if upperCategory == string(utils.ProjectFlockCategoryLaying) {
weekBase = config.LayingWeekStart()
}
week := ((day - 1) / 7) + weekBase
week := ((day - 1) / 7) + 1
if week <= 0 {
return nil
}
upperCategory := strings.ToUpper(category)
if upperCategory == string(utils.ProjectFlockCategoryLaying) {
detail, err := s.ProductionStandardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week)
if err != nil {
@@ -24,7 +24,6 @@ type PurchaseRepository interface {
UpdateReceivingDetails(ctx context.Context, purchaseID uint, updates []PurchaseReceivingUpdate) error
DeleteItems(ctx context.Context, purchaseID uint, itemIDs []uint) 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
SoftDeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) 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)
}
func (r *PurchaseRepositoryImpl) NextPoNumber(ctx context.Context, tx *gorm.DB) (string, error) {
return r.generateSequentialNumber(ctx, tx, "po_number", utils.PurchasePONumberPrefix, utils.PurchaseNumberPadding)
}
// NOTE: NextPoNumber dihapus per migration 20260529143940 — po_number sekarang
// 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) {
db := tx
@@ -779,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 {
updateData := map[string]any{}
if !hasExistingPO {
repoTx := rPurchase.NewPurchaseRepository(tx)
code, err := repoTx.NextPoNumber(c.Context(), tx)
code, err := derivePoFromPr(purchase.PrNumber)
if err != nil {
return err
}
@@ -2513,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(
c *fiber.Ctx,
step approvalutils.ApprovalStep,