mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-06-09 15:07:49 +00:00
Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f64839dfe1 | |||
| 0ff720453f | |||
| ef2f9568ad | |||
| badbe4086a | |||
| 6528739bfd | |||
| 68bddd5c78 | |||
| 90efd0ba5a | |||
| bfef144668 | |||
| 09b1f19d19 | |||
| 672f80a3ba | |||
| 0f12c706b0 | |||
| d26c4e9e1a | |||
| be7f3ac82a | |||
| a46edc4498 | |||
| 254ce509fb | |||
| 8624030b39 | |||
| b4fbef702a | |||
| 0410169746 | |||
| bbc7f0f6e9 | |||
| 6264b0f08d | |||
| e6fe4d77eb | |||
| 8ee87a73b7 | |||
| 8fc41ee8e9 | |||
| 8da2b7a3ab | |||
| 7846487254 | |||
| 0ed67955a6 | |||
| 679d835fbb |
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
GetEarliestChickInDateByProjectFlockID(ctx context.Context, projectFlockID uint) (*time.Time, 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)
|
||||
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)
|
||||
@@ -466,28 +466,30 @@ func (r *HppV2RepositoryImpl) GetMultiplicationPercentages(
|
||||
ctx context.Context,
|
||||
houseTypes []string,
|
||||
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)
|
||||
effectiveDates := make(map[string]*time.Time)
|
||||
if len(houseTypes) == 0 || maxDay <= 0 {
|
||||
return result, nil
|
||||
return result, effectiveDates, nil
|
||||
}
|
||||
|
||||
type row struct {
|
||||
HouseType string
|
||||
Day int
|
||||
MultiplicationPercentage float64
|
||||
EffectiveDate *time.Time
|
||||
}
|
||||
|
||||
rows := make([]row, 0)
|
||||
err := r.db.WithContext(ctx).Raw(`
|
||||
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
|
||||
WHERE house_type::text IN ? AND day <= ?
|
||||
ORDER BY house_type, day, effective_date DESC NULLS LAST
|
||||
`, houseTypes, maxDay).Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for _, item := range rows {
|
||||
@@ -495,9 +497,12 @@ func (r *HppV2RepositoryImpl) GetMultiplicationPercentages(
|
||||
result[item.HouseType] = make(map[int]float64)
|
||||
}
|
||||
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(
|
||||
|
||||
@@ -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)
|
||||
multiplicationByHouseType, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, scheduleDay)
|
||||
multiplicationByHouseType, effectiveDates, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, scheduleDay)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1407,6 +1407,11 @@ func (s *hppV2Service) buildNormalTransferDepreciationPart(
|
||||
totalValueAfter := pulletCostDayN * multiplicationPercentage
|
||||
depreciationPercent := (1.0 - multiplicationPercentage) * 100.0
|
||||
|
||||
var standardEffectiveDate string
|
||||
if ed, ok := effectiveDates[houseType]; ok && ed != nil {
|
||||
standardEffectiveDate = formatDateOnly(*ed)
|
||||
}
|
||||
|
||||
return &HppV2ComponentPart{
|
||||
Code: hppV2PartDepreciationNormal,
|
||||
Title: "Normal Transfer",
|
||||
@@ -1422,6 +1427,8 @@ func (s *hppV2Service) buildNormalTransferDepreciationPart(
|
||||
"origin_date": formatDateOnly(*originDate),
|
||||
"transfer_date": formatDateOnly(transferInput.TransferDate),
|
||||
"source_project_flock_id": transferInput.SourceProjectFlockID,
|
||||
"standard_effective_date": standardEffectiveDate,
|
||||
"kandang_population": transferInput.TransferQty,
|
||||
},
|
||||
References: []HppV2Reference{
|
||||
{
|
||||
@@ -1492,7 +1499,7 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1511,6 +1518,11 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
|
||||
depreciationPercent := (1.0 - multiplicationPercentage) * 100.0
|
||||
_ = totalPulletCost
|
||||
|
||||
var standardEffectiveDate string
|
||||
if ed, ok := effectiveDates[houseType]; ok && ed != nil {
|
||||
standardEffectiveDate = formatDateOnly(*ed)
|
||||
}
|
||||
|
||||
return &HppV2ComponentPart{
|
||||
Code: hppV2PartDepreciationCutover,
|
||||
Title: "Manual Cut-over",
|
||||
@@ -1530,6 +1542,8 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
|
||||
"cutover_date": formatDateOnly(manualInput.CutoverDate),
|
||||
"manual_input_id": manualInput.ID,
|
||||
"project_flock_kandang": projectFlockKandangId,
|
||||
"standard_effective_date": standardEffectiveDate,
|
||||
"kandang_population": kandangPopulation,
|
||||
},
|
||||
References: []HppV2Reference{
|
||||
{
|
||||
|
||||
@@ -103,8 +103,9 @@ func (s *hppV2RepoStub) GetDepreciationPercents(_ context.Context, houseTypes []
|
||||
|
||||
// GetMultiplicationPercentages — alias yang sama dengan GetDepreciationPercents untuk match
|
||||
// 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) {
|
||||
return s.GetDepreciationPercents(ctx, houseTypes, maxDay)
|
||||
func (s *hppV2RepoStub) GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, map[string]*time.Time, error) {
|
||||
vals, err := s.GetDepreciationPercents(ctx, houseTypes, maxDay)
|
||||
return vals, make(map[string]*time.Time), err
|
||||
}
|
||||
|
||||
// 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(
|
||||
|
||||
-3
@@ -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;
|
||||
-10
@@ -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;
|
||||
+99
@@ -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;
|
||||
+241
@@ -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;
|
||||
+3
@@ -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;
|
||||
+42
@@ -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
-1
@@ -1,6 +1,6 @@
|
||||
-- Hapus open_house dan close_house rows dengan effective_date baru
|
||||
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
|
||||
ALTER TABLE house_depreciation_standards DROP COLUMN multiplication_percentage;
|
||||
+10
-117
@@ -134,7 +134,7 @@ INSERT INTO house_depreciation_standards
|
||||
SELECT
|
||||
'open_house'::house_type_enum,
|
||||
day,
|
||||
'2026-05-20'::date,
|
||||
'2026-05-29'::date,
|
||||
depreciation_percent,
|
||||
25,
|
||||
'Standard Open House Week 25',
|
||||
@@ -147,133 +147,26 @@ FROM (
|
||||
ORDER BY day, effective_date DESC NULLS LAST
|
||||
) 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
|
||||
(house_type, day, effective_date, depreciation_percent, standard_week, name, multiplication_percentage)
|
||||
SELECT
|
||||
'close_house'::house_type_enum,
|
||||
oh.day,
|
||||
'2026-05-20'::date,
|
||||
oh.depreciation_percent,
|
||||
day,
|
||||
'2026-05-29'::date,
|
||||
depreciation_percent,
|
||||
25,
|
||||
'Standard Close House Week 25',
|
||||
ch.val
|
||||
multiplication_percentage
|
||||
FROM (
|
||||
SELECT DISTINCT ON (day)
|
||||
day, depreciation_percent
|
||||
day, depreciation_percent, multiplication_percentage
|
||||
FROM house_depreciation_standards
|
||||
WHERE house_type = 'open_house'
|
||||
ORDER BY day, effective_date DESC NULLS LAST
|
||||
) oh
|
||||
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;
|
||||
) effective_close_house;
|
||||
|
||||
-- Invalidate snapshot cache depreciation agar recompute dengan standard baru
|
||||
DELETE FROM farm_depreciation_snapshots;
|
||||
+14
@@ -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;
|
||||
+21
@@ -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;
|
||||
@@ -18,6 +18,7 @@ type Expense struct {
|
||||
TransactionDate time.Time `gorm:"type:date;not null"`
|
||||
Notes string `gorm:"type:text;column:notes"`
|
||||
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:""`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
|
||||
@@ -15,6 +15,7 @@ type Marketing struct {
|
||||
SalesPersonId uint `gorm:"not null"`
|
||||
Notes string `gorm:"type:text"`
|
||||
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"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -12,6 +12,7 @@ type Purchase struct {
|
||||
SupplierId uint `gorm:"not null"`
|
||||
CreditTerm int `gorm:"column:credit_term;not null;default:0"`
|
||||
DueDate *time.Time
|
||||
GrandTotal float64 `gorm:"column:grand_total;type:numeric(15,3);not null;default:0"`
|
||||
Notes *string
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
ExpenseRoutes(router, userService, expenseService)
|
||||
|
||||
@@ -54,9 +54,10 @@ type expenseService struct {
|
||||
RealizationRepository repository.ExpenseRealizationRepository
|
||||
ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository
|
||||
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{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
@@ -67,6 +68,23 @@ func NewExpenseService(repo repository.ExpenseRepository, supplierRepo supplierR
|
||||
RealizationRepository: realizationRepo,
|
||||
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
||||
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)
|
||||
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil)
|
||||
|
||||
s.reallocateAfterRealization(c.Context(), expenseID, expense.SupplierId)
|
||||
|
||||
return responseDTO, nil
|
||||
}
|
||||
|
||||
@@ -1522,6 +1543,9 @@ func (s *expenseService) UpdateRealization(c *fiber.Ctx, expenseID uint, req *va
|
||||
return nil, err
|
||||
}
|
||||
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil)
|
||||
|
||||
s.reallocateAfterRealization(c.Context(), expenseID, expense.SupplierId)
|
||||
|
||||
return responseDTO, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
paymentService := sPayment.NewPaymentService(paymentRepo, approvalService, validate)
|
||||
fifoPaymentService := commonSvc.NewFifoPaymentService(db, nil)
|
||||
|
||||
paymentService := sPayment.NewPaymentService(paymentRepo, approvalService, fifoPaymentService, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
PaymentRoutes(router, userService, paymentService)
|
||||
|
||||
@@ -32,12 +32,14 @@ type paymentService struct {
|
||||
Validate *validator.Validate
|
||||
Repository repository.PaymentRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
FifoPaymentSvc commonSvc.FifoPaymentService
|
||||
approvalWorkflow approvalutils.ApprovalWorkflowKey
|
||||
}
|
||||
|
||||
func NewPaymentService(
|
||||
repo repository.PaymentRepository,
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
fifoPaymentSvc commonSvc.FifoPaymentService,
|
||||
validate *validator.Validate,
|
||||
) PaymentService {
|
||||
return &paymentService{
|
||||
@@ -45,6 +47,7 @@ func NewPaymentService(
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
FifoPaymentSvc: fifoPaymentSvc,
|
||||
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
|
||||
})
|
||||
if err != nil {
|
||||
@@ -251,7 +260,46 @@ func (s paymentService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
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) {
|
||||
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))
|
||||
}
|
||||
|
||||
transactionService := sTransaction.NewTransactionService(transactionRepo, approvalService, validate)
|
||||
fifoPaymentService := commonSvc.NewFifoPaymentService(db, utils.Log)
|
||||
transactionService := sTransaction.NewTransactionService(transactionRepo, approvalService, fifoPaymentService, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
TransactionRoutes(router, userService, transactionService)
|
||||
|
||||
@@ -30,19 +30,22 @@ type transactionService struct {
|
||||
Validate *validator.Validate
|
||||
Repository repository.TransactionRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
FifoPaymentSvc commonSvc.FifoPaymentService
|
||||
approvalWorkflows map[string]approvalutils.ApprovalWorkflowKey
|
||||
}
|
||||
|
||||
func NewTransactionService(
|
||||
repo repository.TransactionRepository,
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
fifoPaymentSvc commonSvc.FifoPaymentService,
|
||||
validate *validator.Validate,
|
||||
) TransactionService {
|
||||
return &transactionService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
FifoPaymentSvc: fifoPaymentSvc,
|
||||
approvalWorkflows: map[string]approvalutils.ApprovalWorkflowKey{
|
||||
string(utils.TransactionTypeSaldoAwal): utils.ApprovalWorkflowInitial,
|
||||
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 {
|
||||
// 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 errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
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)
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@ func (TransferModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
expenseRealizationRepo,
|
||||
projectFlockKandangRepo,
|
||||
documentSvc,
|
||||
commonSvc.NewFifoPaymentService(db, utils.Log),
|
||||
validate,
|
||||
)
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
|
||||
MarketingId: uint(c.QueryInt("marketing_id", 0)),
|
||||
ProjectFlockID: uint(c.QueryInt("project_flock_id", 0)),
|
||||
ProjectFlockKandangID: uint(c.QueryInt("project_flock_kandang_id", 0)),
|
||||
WarehouseID: uint(c.QueryInt("warehouse_id", 0)),
|
||||
SortBy: sortBy,
|
||||
SortOrder: sortOrder,
|
||||
}
|
||||
|
||||
@@ -70,23 +70,26 @@ func buildMarketingExportWorkbook(items []dto.MarketingListDTO) ([]byte, error)
|
||||
}
|
||||
|
||||
func setMarketingExportColumns(file *excelize.File, sheet string) error {
|
||||
// A–Q = 17 columns
|
||||
// E = Sales (new), H = Gudang (new), Satuan (old I) removed
|
||||
columnWidths := map[string]float64{
|
||||
"A": 16,
|
||||
"B": 14,
|
||||
"C": 18,
|
||||
"D": 20,
|
||||
"E": 14,
|
||||
"F": 40,
|
||||
"G": 10,
|
||||
"H": 12,
|
||||
"I": 12,
|
||||
"J": 12,
|
||||
"K": 16,
|
||||
"L": 16,
|
||||
"M": 18,
|
||||
"N": 18,
|
||||
"O": 18,
|
||||
"P": 24,
|
||||
"A": 16, // No. Order
|
||||
"B": 14, // Tanggal
|
||||
"C": 18, // Status
|
||||
"D": 20, // Customer
|
||||
"E": 20, // Sales (new)
|
||||
"F": 14, // Tipe
|
||||
"G": 40, // Nama Produk
|
||||
"H": 20, // Gudang (new)
|
||||
"I": 10, // Week
|
||||
"J": 12, // Jumlah
|
||||
"K": 12, // Qty Peti
|
||||
"L": 16, // Berat Rata-rata (kg)
|
||||
"M": 16, // Total Berat (kg)
|
||||
"N": 18, // Harga Satuan
|
||||
"O": 18, // Total Harga
|
||||
"P": 18, // Grand Total
|
||||
"Q": 24, // Catatan
|
||||
}
|
||||
|
||||
for col, width := range columnWidths {
|
||||
@@ -104,22 +107,23 @@ func setMarketingExportColumns(file *excelize.File, sheet string) error {
|
||||
|
||||
func setMarketingExportHeaders(file *excelize.File, sheet string) error {
|
||||
headers := []string{
|
||||
"No. Order", // A
|
||||
"Tanggal", // B
|
||||
"Status", // C
|
||||
"Customer", // D
|
||||
"Tipe", // E
|
||||
"Nama Produk", // F
|
||||
"Week", // G
|
||||
"Jumlah", // H
|
||||
"Satuan", // I
|
||||
"Qty Peti", // J
|
||||
"Berat Rata-rata (kg)", // K
|
||||
"Total Berat (kg)", // L
|
||||
"Harga Satuan", // M
|
||||
"Total Harga", // N
|
||||
"Grand Total", // O
|
||||
"Catatan", // P
|
||||
"No. Order", // A
|
||||
"Tanggal", // B
|
||||
"Status", // C
|
||||
"Customer", // D
|
||||
"Sales", // E (new)
|
||||
"Tipe", // F
|
||||
"Nama Produk", // G
|
||||
"Gudang", // H (new)
|
||||
"Week", // I
|
||||
"Jumlah Butir", // J
|
||||
"Qty Peti", // K
|
||||
"Berat Rata-rata (kg)", // L
|
||||
"Total Berat (kg)", // M
|
||||
"Harga Satuan", // N
|
||||
"Total Harga", // O
|
||||
"Grand Total", // P
|
||||
"Catatan", // Q
|
||||
}
|
||||
|
||||
for i, header := range headers {
|
||||
@@ -148,7 +152,7 @@ func setMarketingExportHeaders(file *excelize.File, sheet string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return file.SetCellStyle(sheet, "A1", "P1", headerStyle)
|
||||
return file.SetCellStyle(sheet, "A1", "Q1", headerStyle)
|
||||
}
|
||||
|
||||
func setMarketingExportRows(file *excelize.File, sheet string, items []dto.MarketingListDTO) error {
|
||||
@@ -162,17 +166,161 @@ func setMarketingExportRows(file *excelize.File, sheet string, items []dto.Marke
|
||||
soDate := formatMarketingExportDate(item.SoDate)
|
||||
status := formatMarketingExportStatus(item)
|
||||
customer := safeMarketingExportText(item.Customer.Name)
|
||||
grandTotal := sumMarketingGrandTotal(item.SalesOrder)
|
||||
notes := safeMarketingExportText(item.Notes)
|
||||
salesPerson := safeMarketingExportText(item.SalesPerson.Name)
|
||||
|
||||
isDeliveryOrder := strings.EqualFold(strings.TrimSpace(status), "delivery order")
|
||||
|
||||
// ── Delivery Order branch ──────────────────────────────────────────────
|
||||
if isDeliveryOrder {
|
||||
grandTotal := sumDeliveryGrandTotal(item.DeliveryOrder)
|
||||
|
||||
if len(item.DeliveryOrder) == 0 {
|
||||
row++
|
||||
r := strconv.Itoa(row)
|
||||
vals := map[string]interface{}{
|
||||
"A": soNumber, "B": soDate, "C": status, "D": customer, "E": salesPerson,
|
||||
"F": "-", "G": "-", "H": "-", "I": "-", "J": "-", "K": "-",
|
||||
"L": "-", "M": "-", "N": "-", "O": "-",
|
||||
"P": grandTotal, "Q": notes,
|
||||
}
|
||||
for col, val := range vals {
|
||||
if err := file.SetCellValue(sheet, col+r, val); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Build lookup map: MarketingProductId → SO product (for Week & MarketingType)
|
||||
soProductMap := make(map[uint]*dto.DeliveryMarketingProductDTO, len(item.SalesOrder))
|
||||
for i := range item.SalesOrder {
|
||||
soProductMap[item.SalesOrder[i].Id] = &item.SalesOrder[i]
|
||||
}
|
||||
|
||||
for _, group := range item.DeliveryOrder {
|
||||
doNumber := safeMarketingExportText(group.DoNumber)
|
||||
|
||||
doDate := "-"
|
||||
if group.DeliveryDate != nil {
|
||||
doDate = formatMarketingExportDate(*group.DeliveryDate)
|
||||
}
|
||||
|
||||
gudang := "-"
|
||||
if group.Warehouse != nil {
|
||||
gudang = safeMarketingExportText(group.Warehouse.Name)
|
||||
}
|
||||
|
||||
if len(group.Deliveries) == 0 {
|
||||
row++
|
||||
r := strconv.Itoa(row)
|
||||
vals := map[string]interface{}{
|
||||
"A": doNumber, "B": doDate, "C": status, "D": customer, "E": salesPerson,
|
||||
"F": "-", "G": "-", "H": gudang, "I": "-", "J": "-", "K": "-",
|
||||
"L": "-", "M": "-", "N": "-", "O": "-",
|
||||
"P": grandTotal, "Q": notes,
|
||||
}
|
||||
for col, val := range vals {
|
||||
if err := file.SetCellValue(sheet, col+r, val); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
for _, delivery := range group.Deliveries {
|
||||
row++
|
||||
r := strconv.Itoa(row)
|
||||
|
||||
productName := "-"
|
||||
if delivery.ProductWarehouse != nil && delivery.ProductWarehouse.Product != nil {
|
||||
if n := strings.TrimSpace(delivery.ProductWarehouse.Product.Name); n != "" {
|
||||
productName = n
|
||||
}
|
||||
}
|
||||
|
||||
week := "-"
|
||||
marketingType := "-"
|
||||
if soProduct, ok := soProductMap[delivery.MarketingProductId]; ok {
|
||||
if soProduct.Week != nil {
|
||||
week = strconv.Itoa(*soProduct.Week)
|
||||
}
|
||||
marketingType = safeMarketingExportText(soProduct.MarketingType)
|
||||
}
|
||||
|
||||
if err := file.SetCellValue(sheet, "A"+r, doNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "B"+r, doDate); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "C"+r, status); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "D"+r, customer); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "E"+r, salesPerson); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "F"+r, marketingType); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "G"+r, productName); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "H"+r, gudang); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "I"+r, week); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "J"+r, delivery.Qty); err != nil {
|
||||
return err
|
||||
}
|
||||
if delivery.TotalPeti != nil {
|
||||
if err := file.SetCellValue(sheet, "K"+r, *delivery.TotalPeti); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := file.SetCellValue(sheet, "K"+r, "-"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "L"+r, delivery.AvgWeight); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "M"+r, delivery.TotalWeight); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "N"+r, delivery.UnitPrice); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "O"+r, delivery.TotalPrice); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "P"+r, grandTotal); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "Q"+r, notes); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// ── Sales Order branch (all other statuses) ───────────────────────────
|
||||
grandTotal := sumMarketingGrandTotal(item.SalesOrder)
|
||||
|
||||
if len(item.SalesOrder) == 0 {
|
||||
row++
|
||||
r := strconv.Itoa(row)
|
||||
vals := map[string]interface{}{
|
||||
"A": soNumber, "B": soDate, "C": status, "D": customer,
|
||||
"E": "-", "F": "-", "G": "-", "H": "-", "I": "-", "J": "-",
|
||||
"K": "-", "L": "-", "M": "-", "N": "-",
|
||||
"O": grandTotal, "P": notes,
|
||||
"A": soNumber, "B": soDate, "C": status, "D": customer, "E": salesPerson,
|
||||
"F": "-", "G": "-", "H": "-", "I": "-", "J": "-", "K": "-",
|
||||
"L": "-", "M": "-", "N": "-", "O": "-",
|
||||
"P": grandTotal, "Q": notes,
|
||||
}
|
||||
for col, val := range vals {
|
||||
if err := file.SetCellValue(sheet, col+r, val); err != nil {
|
||||
@@ -198,9 +346,9 @@ func setMarketingExportRows(file *excelize.File, sheet string, items []dto.Marke
|
||||
week = strconv.Itoa(*prod.Week)
|
||||
}
|
||||
|
||||
satuan := "-"
|
||||
if prod.ConvertionUnit != nil && strings.TrimSpace(*prod.ConvertionUnit) != "" {
|
||||
satuan = *prod.ConvertionUnit
|
||||
gudang := "-"
|
||||
if prod.ProductWarehouse != nil {
|
||||
gudang = safeMarketingExportText(prod.ProductWarehouse.Warehouse.Name)
|
||||
}
|
||||
|
||||
if err := file.SetCellValue(sheet, "A"+r, soNumber); err != nil {
|
||||
@@ -215,46 +363,49 @@ func setMarketingExportRows(file *excelize.File, sheet string, items []dto.Marke
|
||||
if err := file.SetCellValue(sheet, "D"+r, customer); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "E"+r, safeMarketingExportText(prod.MarketingType)); err != nil {
|
||||
if err := file.SetCellValue(sheet, "E"+r, salesPerson); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "F"+r, productName); err != nil {
|
||||
if err := file.SetCellValue(sheet, "F"+r, safeMarketingExportText(prod.MarketingType)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "G"+r, week); err != nil {
|
||||
if err := file.SetCellValue(sheet, "G"+r, productName); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "H"+r, prod.Qty); err != nil {
|
||||
if err := file.SetCellValue(sheet, "H"+r, gudang); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "I"+r, satuan); err != nil {
|
||||
if err := file.SetCellValue(sheet, "I"+r, week); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "J"+r, prod.Qty); err != nil {
|
||||
return err
|
||||
}
|
||||
if prod.TotalPeti != nil {
|
||||
if err := file.SetCellValue(sheet, "J"+r, *prod.TotalPeti); err != nil {
|
||||
if err := file.SetCellValue(sheet, "K"+r, *prod.TotalPeti); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := file.SetCellValue(sheet, "J"+r, "-"); err != nil {
|
||||
if err := file.SetCellValue(sheet, "K"+r, "-"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "K"+r, prod.AvgWeight); err != nil {
|
||||
if err := file.SetCellValue(sheet, "L"+r, prod.AvgWeight); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "L"+r, prod.TotalWeight); err != nil {
|
||||
if err := file.SetCellValue(sheet, "M"+r, prod.TotalWeight); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "M"+r, prod.UnitPrice); err != nil {
|
||||
if err := file.SetCellValue(sheet, "N"+r, prod.UnitPrice); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "N"+r, prod.TotalPrice); err != nil {
|
||||
if err := file.SetCellValue(sheet, "O"+r, prod.TotalPrice); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "O"+r, grandTotal); err != nil {
|
||||
if err := file.SetCellValue(sheet, "P"+r, grandTotal); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "P"+r, notes); err != nil {
|
||||
if err := file.SetCellValue(sheet, "Q"+r, notes); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -276,7 +427,7 @@ func setMarketingExportRows(file *excelize.File, sheet string, items []dto.Marke
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellStyle(sheet, "A2", "P"+lastRowStr, dataStyle); err != nil {
|
||||
if err := file.SetCellStyle(sheet, "A2", "Q"+lastRowStr, dataStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -287,7 +438,7 @@ func setMarketingExportRows(file *excelize.File, sheet string, items []dto.Marke
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellStyle(sheet, "K2", "O"+lastRowStr, numberStyle); err != nil {
|
||||
if err := file.SetCellStyle(sheet, "L2", "P"+lastRowStr, numberStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -298,7 +449,7 @@ func setMarketingExportRows(file *excelize.File, sheet string, items []dto.Marke
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, col := range []string{"G", "H", "J"} {
|
||||
for _, col := range []string{"I", "J", "K"} {
|
||||
if err := file.SetCellStyle(sheet, col+"2", col+lastRowStr, centerStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -327,16 +478,23 @@ func formatMarketingExportStatus(item dto.MarketingListDTO) string {
|
||||
return safeMarketingExportText(item.LatestApproval.StepName)
|
||||
}
|
||||
|
||||
|
||||
func sumMarketingGrandTotal(items []dto.DeliveryMarketingProductDTO) float64 {
|
||||
total := 0.0
|
||||
for _, item := range items {
|
||||
total += item.TotalPrice
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
func sumDeliveryGrandTotal(groups []dto.DeliveryGroupDTO) float64 {
|
||||
total := 0.0
|
||||
for _, g := range groups {
|
||||
for _, d := range g.Deliveries {
|
||||
total += d.TotalPrice
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func safeMarketingExportText(value string) string {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
|
||||
@@ -28,6 +28,8 @@ type MarketingListDTO struct {
|
||||
Customer customerDTO.CustomerRelationDTO `json:"customer"`
|
||||
SalesPerson userDTO.UserRelationDTO `json:"sales_person"`
|
||||
SoDocs string `json:"so_docs"`
|
||||
GrandTotalSO float64 `json:"grand_total_so"`
|
||||
GrandTotalDO float64 `json:"grand_total_do"`
|
||||
SalesOrder []DeliveryMarketingProductDTO `json:"sales_order"`
|
||||
DeliveryOrder []DeliveryGroupDTO `json:"delivery_order"`
|
||||
CreatedUser userDTO.UserRelationDTO `json:"created_user"`
|
||||
@@ -198,11 +200,18 @@ func ToMarketingListDTO(marketing *entity.Marketing, deliveryProducts []entity.M
|
||||
salesOrderProducts[i] = ToDeliveryMarketingProductDTO(product, marketing.MarketingType)
|
||||
}
|
||||
}
|
||||
var grandTotalSO float64
|
||||
for _, p := range marketing.Products {
|
||||
grandTotalSO += p.TotalPrice
|
||||
}
|
||||
|
||||
return MarketingListDTO{
|
||||
MarketingRelationDTO: ToMarketingRelationDTO(marketing),
|
||||
Customer: customer,
|
||||
SalesPerson: salesPerson,
|
||||
SoDocs: marketing.SoDocs,
|
||||
GrandTotalSO: grandTotalSO,
|
||||
GrandTotalDO: marketing.GrandTotal,
|
||||
SalesOrder: salesOrderProducts,
|
||||
DeliveryOrder: extractDeliveryGroupsFromProducts(marketing),
|
||||
CreatedUser: createdUser,
|
||||
|
||||
@@ -35,6 +35,7 @@ func (MarketingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
stockLogRepo := rShared.NewStockLogRepository(db)
|
||||
|
||||
fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)
|
||||
fifoPaymentService := commonSvc.NewFifoPaymentService(db, utils.Log)
|
||||
|
||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||
approvalSvc := commonSvc.NewApprovalService(approvalRepo)
|
||||
@@ -47,7 +48,7 @@ func (MarketingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
|
||||
|
||||
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)
|
||||
|
||||
RegisterRoutes(router, userService, salesOrdersService, deliveryOrdersService)
|
||||
|
||||
@@ -48,6 +48,7 @@ type deliveryOrdersService struct {
|
||||
ProjectFlockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
FifoStockV2Svc commonSvc.FifoStockV2Service
|
||||
FifoPaymentSvc commonSvc.FifoPaymentService
|
||||
}
|
||||
|
||||
func NewDeliveryOrdersService(
|
||||
@@ -59,6 +60,7 @@ func NewDeliveryOrdersService(
|
||||
projectFlockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository,
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
fifoStockV2Svc commonSvc.FifoStockV2Service,
|
||||
fifoPaymentSvc commonSvc.FifoPaymentService,
|
||||
validate *validator.Validate,
|
||||
) DeliveryOrdersService {
|
||||
return &deliveryOrdersService{
|
||||
@@ -71,6 +73,22 @@ func NewDeliveryOrdersService(
|
||||
ProjectFlockPopulationRepo: projectFlockPopulationRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,6 +287,16 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
||||
db = db.Where("marketings.customer_id = ?", params.CustomerId)
|
||||
}
|
||||
|
||||
if params.WarehouseID != 0 {
|
||||
db = db.Where(`EXISTS (
|
||||
SELECT 1
|
||||
FROM marketing_products mp
|
||||
JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id
|
||||
WHERE mp.marketing_id = marketings.id
|
||||
AND pw.warehouse_id = ?
|
||||
)`, params.WarehouseID)
|
||||
}
|
||||
|
||||
db = s.applyMarketingProjectFlockFilter(c.Context(), db, params.ProjectFlockID, params.ProjectFlockKandangID)
|
||||
db = s.applyMarketingSearchFilter(c.Context(), db, params.Search)
|
||||
|
||||
@@ -418,6 +446,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
||||
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 {
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
@@ -428,6 +457,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
|
||||
}
|
||||
capturedCustomerID = marketing.CustomerId
|
||||
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), req.MarketingId)
|
||||
if err != nil {
|
||||
@@ -519,6 +549,8 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order")
|
||||
}
|
||||
|
||||
s.reallocateAfterDelivery(c.Context(), req.MarketingId, capturedCustomerID)
|
||||
|
||||
return s.getMarketingWithDeliveries(c, req.MarketingId)
|
||||
}
|
||||
|
||||
@@ -547,6 +579,7 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
||||
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 {
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
@@ -557,6 +590,7 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
|
||||
}
|
||||
capturedCustomerID = marketing.CustomerId
|
||||
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -662,6 +696,8 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery order")
|
||||
}
|
||||
|
||||
s.reallocateAfterDelivery(c.Context(), id, capturedCustomerID)
|
||||
|
||||
return s.getMarketingWithDeliveries(c, id)
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ type DeliveryOrderQuery struct {
|
||||
MarketingId uint `query:"marketing_id" validate:"omitempty,gt=0"`
|
||||
ProjectFlockID uint `query:"project_flock_id" validate:"omitempty,gt=0"`
|
||||
ProjectFlockKandangID uint `query:"project_flock_kandang_id" validate:"omitempty,gt=0"`
|
||||
WarehouseID uint `query:"warehouse_id" validate:"omitempty,gt=0"`
|
||||
SortBy string `query:"sort_by" validate:"omitempty,oneof=so_number so_date status customer grand_total created_at"`
|
||||
SortOrder string `query:"sort_order" validate:"omitempty,oneof=asc desc"`
|
||||
}
|
||||
|
||||
+2
-6
@@ -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 {
|
||||
|
||||
@@ -172,6 +172,23 @@ func (u *ChickinController) DeleteOne(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ChickinController) UpdateChickInDate(c *fiber.Ctx) error {
|
||||
req := new(validation.UpdateChickInDate)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
if err := u.ChickinService.UpdateChickInDate(c, req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Chick in date berhasil diperbarui",
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ChickinController) Approval(c *fiber.Ctx) error {
|
||||
req := new(validation.Approve)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
@@ -18,6 +19,7 @@ type ProjectChickinRepository interface {
|
||||
GetTotalChickinQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error)
|
||||
GetByProjectFlockKandangIDForUpdate(ctx context.Context, projectFlockKandangID uint) ([]entity.ProjectChickin, error)
|
||||
UpdateUsageFields(ctx context.Context, tx *gorm.DB, chickinID uint, usageQty, pendingUsageQty float64) error
|
||||
UpdateChickInDateByProjectFlockKandangID(ctx context.Context, tx *gorm.DB, pfkID uint, newDate time.Time) error
|
||||
}
|
||||
|
||||
type ChickinRepositoryImpl struct {
|
||||
@@ -134,3 +136,10 @@ func (r *ChickinRepositoryImpl) UpdateUsageFields(ctx context.Context, tx *gorm.
|
||||
"pending_usage_qty": pendingUsageQty,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (r *ChickinRepositoryImpl) UpdateChickInDateByProjectFlockKandangID(ctx context.Context, tx *gorm.DB, pfkID uint, newDate time.Time) error {
|
||||
return tx.WithContext(ctx).
|
||||
Model(&entity.ProjectChickin{}).
|
||||
Where("project_flock_kandang_id = ? AND deleted_at IS NULL", pfkID).
|
||||
Update("chick_in_date", newDate).Error
|
||||
}
|
||||
|
||||
@@ -17,8 +17,9 @@ func ChickinRoutes(v1 fiber.Router, u user.UserService, s chickin.ChickinService
|
||||
|
||||
route.Get("/", m.RequirePermissions(m.P_ChickinsGetAll), ctrl.GetAll)
|
||||
route.Post("/", m.RequirePermissions(m.P_ChickinsCreateOne), ctrl.CreateOne)
|
||||
route.Get("/:id",m.RequirePermissions(m.P_ChickinsGetOne), ctrl.GetOne)
|
||||
route.Patch("/chick-in-date", m.RequirePermissions(m.P_ChickinsCreateOne), ctrl.UpdateChickInDate)
|
||||
route.Get("/:id", m.RequirePermissions(m.P_ChickinsGetOne), ctrl.GetOne)
|
||||
// route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
route.Post("/approvals",m.RequirePermissions(m.P_ChickinsApproval), ctrl.Approval)
|
||||
route.Post("/approvals", m.RequirePermissions(m.P_ChickinsApproval), ctrl.Approval)
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ type ChickinService interface {
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectChickin, error)
|
||||
EnsureChickInExists(ctx context.Context, projectFlockKandangID uint) error
|
||||
UpdateChickInDate(ctx *fiber.Ctx, req *validation.UpdateChickInDate) error
|
||||
}
|
||||
|
||||
type chickinService struct {
|
||||
@@ -2110,3 +2111,38 @@ func (s chickinService) EnsureChickInExists(ctx context.Context, projectFlockKan
|
||||
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Chick in project flock belum disetujui sehingga belum dapat membuat recording")
|
||||
}
|
||||
|
||||
func (s chickinService) UpdateChickInDate(ctx *fiber.Ctx, req *validation.UpdateChickInDate) error {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newDate, err := time.Parse("2006-01-02", req.ChickInDate)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Format tanggal tidak valid, gunakan YYYY-MM-DD")
|
||||
}
|
||||
|
||||
_, err = s.ProjectflockKandangRepo.GetByID(ctx.Context(), req.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Project flock kandang tidak ditemukan")
|
||||
}
|
||||
|
||||
if err := s.Repository.DB().WithContext(ctx.Context()).Transaction(func(tx *gorm.DB) error {
|
||||
if err := s.Repository.UpdateChickInDateByProjectFlockKandangID(ctx.Context(), tx, req.ProjectFlockKandangId, newDate); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Exec(`
|
||||
UPDATE recordings
|
||||
SET day = GREATEST(0, (record_datetime::date - ?::date)::int),
|
||||
updated_at = NOW()
|
||||
WHERE project_flock_kandangs_id = ?
|
||||
AND deleted_at IS NULL
|
||||
`, req.ChickInDate, req.ProjectFlockKandangId).Error
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.invalidateDepreciationSnapshots(ctx.Context(), nil, []uint{req.ProjectFlockKandangId}, newDate)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -27,3 +27,8 @@ type Approve struct {
|
||||
ApprovableIds []uint `json:"approvable_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||
}
|
||||
|
||||
type UpdateChickInDate struct {
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,gt=0"`
|
||||
ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"`
|
||||
}
|
||||
|
||||
@@ -8,10 +8,12 @@ import (
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/exportprogress"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@@ -75,6 +77,43 @@ func (u *RecordingController) GetAll(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
listDTO := dto.ToRecordingListDTOs(result)
|
||||
|
||||
recordingIDs := make([]uint, 0, len(result))
|
||||
for i := range result {
|
||||
if result[i].Id != 0 {
|
||||
recordingIDs = append(recordingIDs, result[i].Id)
|
||||
}
|
||||
}
|
||||
if len(recordingIDs) > 0 {
|
||||
eggs, err := u.RecordingService.GetEggsWithFlagsByRecordingIDs(c.Context(), recordingIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
eggByRecording := make(map[uint][]entity.RecordingEgg, len(recordingIDs))
|
||||
for _, egg := range eggs {
|
||||
eggByRecording[egg.RecordingId] = append(eggByRecording[egg.RecordingId], egg)
|
||||
}
|
||||
for i := range listDTO {
|
||||
id := listDTO[i].Id
|
||||
if eggList, ok := eggByRecording[id]; ok {
|
||||
breakdown := make(map[string]dto.EggExportBreakdownDTO)
|
||||
for _, egg := range eggList {
|
||||
flagName := eggTypeFromProductName(egg.ProductWarehouse.Product.Name)
|
||||
if flagName == "" {
|
||||
continue
|
||||
}
|
||||
entry := breakdown[flagName]
|
||||
entry.Qty += egg.Qty
|
||||
if egg.Weight != nil {
|
||||
entry.Kg += *egg.Weight
|
||||
}
|
||||
breakdown[flagName] = entry
|
||||
}
|
||||
listDTO[i].EggExportBreakdown = breakdown
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if strings.EqualFold(exportType, "excel") {
|
||||
return exportRecordingListExcel(c, listDTO)
|
||||
}
|
||||
@@ -94,6 +133,33 @@ func (u *RecordingController) GetAll(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// eggTypeFromProductName maps product name to egg type flag name by keyword matching.
|
||||
// Falls back to empty string if no keyword matches.
|
||||
func eggTypeFromProductName(name string) string {
|
||||
normalized := strings.ToLower(strings.TrimSpace(name))
|
||||
if normalized == "" {
|
||||
return ""
|
||||
}
|
||||
// Ordered longest-first to prefer "papacal" over partial match of "pacal", etc.
|
||||
keywords := []struct {
|
||||
keyword string
|
||||
flag string
|
||||
}{
|
||||
{"papacal", string(utils.FlagTelurPapacal)},
|
||||
{"jumbo", string(utils.FlagTelurJumbo)},
|
||||
{"retak", string(utils.FlagTelurRetak)},
|
||||
{"putih", string(utils.FlagTelurPutih)},
|
||||
{"pecah", string(utils.FlagTelurPecah)},
|
||||
{"utuh", string(utils.FlagTelurUtuh)},
|
||||
}
|
||||
for _, k := range keywords {
|
||||
if strings.Contains(normalized, k.keyword) {
|
||||
return k.flag
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (u *RecordingController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/dto"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/xuri/excelize/v2"
|
||||
@@ -79,6 +80,18 @@ func setRecordingExportColumns(file *excelize.File, sheet string) error {
|
||||
"AB": 18,
|
||||
"AC": 24,
|
||||
"AD": 18,
|
||||
"AE": 12,
|
||||
"AF": 10,
|
||||
"AG": 12,
|
||||
"AH": 10,
|
||||
"AI": 12,
|
||||
"AJ": 10,
|
||||
"AK": 12,
|
||||
"AL": 10,
|
||||
"AM": 12,
|
||||
"AN": 10,
|
||||
"AO": 12,
|
||||
"AP": 10,
|
||||
}
|
||||
|
||||
for col, width := range columnWidths {
|
||||
@@ -208,6 +221,31 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
eggTypes := []struct {
|
||||
col1, col2, label string
|
||||
}{
|
||||
{"AE", "AF", "Telur Utuh"},
|
||||
{"AG", "AH", "Telur Pecah"},
|
||||
{"AI", "AJ", "Telur Putih"},
|
||||
{"AK", "AL", "Telur Retak"},
|
||||
{"AM", "AN", "Telur Papacal"},
|
||||
{"AO", "AP", "Telur Jumbo"},
|
||||
}
|
||||
for _, et := range eggTypes {
|
||||
if err := file.MergeCell(sheet, et.col1+"1", et.col2+"1"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, et.col1+"1", et.label); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, et.col1+"2", "Butir"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, et.col2+"2", "Kg"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
headerStyle, err := file.NewStyle(&excelize.Style{
|
||||
Font: &excelize.Font{
|
||||
Bold: true,
|
||||
@@ -234,7 +272,7 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return file.SetCellStyle(sheet, "A1", "AD2", headerStyle)
|
||||
return file.SetCellStyle(sheet, "A1", "AP2", headerStyle)
|
||||
}
|
||||
|
||||
func setRecordingExportRows(file *excelize.File, sheet string, items []dto.RecordingListDTO) error {
|
||||
@@ -245,7 +283,8 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
columns := []string{
|
||||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
|
||||
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB",
|
||||
"AC", "AD",
|
||||
"AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN",
|
||||
"AO", "AP",
|
||||
}
|
||||
|
||||
currentRow := 3
|
||||
@@ -293,14 +332,14 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
// Expand recordings into one row per sapronak
|
||||
type sapronakRow struct {
|
||||
name string
|
||||
input string
|
||||
input interface{} // float64 for numeric, string "-" for placeholder
|
||||
}
|
||||
sapronaks := make([]sapronakRow, 0)
|
||||
if len(item.FeedUsage) > 0 {
|
||||
for _, fu := range item.FeedUsage {
|
||||
sapronaks = append(sapronaks, sapronakRow{
|
||||
name: safeExportText(fu.ProductName),
|
||||
input: formatNumberID(fu.UsageAmount+fu.PendingQty, 2, true),
|
||||
input: fu.UsageAmount + fu.PendingQty,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
@@ -311,37 +350,66 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
|
||||
for sIdx, s := range sapronaks {
|
||||
if sIdx == 0 {
|
||||
eggQty := func(flagName string) int {
|
||||
if item.EggExportBreakdown != nil {
|
||||
if bd, ok := item.EggExportBreakdown[flagName]; ok {
|
||||
return bd.Qty
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
eggKg := func(flagName string) float64 {
|
||||
if item.EggExportBreakdown != nil {
|
||||
if bd, ok := item.EggExportBreakdown[flagName]; ok {
|
||||
return bd.Kg
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
rowValues := []interface{}{
|
||||
i + 1, // A
|
||||
locationName, // B
|
||||
safeExportText(item.ProjectFlock.FlockName), // C
|
||||
kandangName, // D
|
||||
item.ProjectFlock.Period, // E
|
||||
i + 1, // A
|
||||
locationName, // B
|
||||
safeExportText(item.ProjectFlock.FlockName), // C
|
||||
kandangName, // D
|
||||
item.ProjectFlock.Period, // E
|
||||
formatCategoryLabel(item.ProjectFlock.ProjectFlockCategory), // F
|
||||
formatAgeLabel(item), // G
|
||||
formatDateIndonesian(item.RecordDatetime), // H
|
||||
formatNumberID(item.ProjectFlock.TotalChickQty, 0, false), // I
|
||||
formatNumberID(item.FcrValue, 2, true), // J
|
||||
formatNumberID(fcrStd, 2, true), // K
|
||||
formatNumberID(item.FeedIntake, 2, true), // L
|
||||
formatNumberID(feedIntakeStd, 2, true), // M
|
||||
formatPercentID(item.CumDepletionRate, 2), // N
|
||||
formatPercentID(maxDepletionStd, 2), // O
|
||||
formatNumberID(item.TotalDepletionQty, 2, true), // P
|
||||
formatNumberID(item.EggMass, 2, true), // Q
|
||||
formatNumberID(eggMassStd, 2, true), // R
|
||||
formatNumberID(item.EggWeight, 2, true), // S
|
||||
formatNumberID(eggWeightStd, 2, true), // T
|
||||
formatPercentID(item.HenDay, 2), // U
|
||||
formatPercentID(henDayStd, 2), // V
|
||||
formatPercentID(item.HenHouse, 2), // W
|
||||
formatPercentID(henHouseStd, 2), // X
|
||||
formatApprovalStatus(item), // Y
|
||||
formatAgeLabel(item), // G
|
||||
formatDateIndonesian(item.RecordDatetime), // H
|
||||
item.ProjectFlock.TotalChickQty, // I
|
||||
item.FcrValue, // J
|
||||
fcrStd, // K
|
||||
item.FeedIntake, // L
|
||||
feedIntakeStd, // M
|
||||
item.CumDepletionRate, // N
|
||||
maxDepletionStd, // O
|
||||
item.TotalDepletionQty, // P
|
||||
item.EggMass, // Q
|
||||
eggMassStd, // R
|
||||
item.EggWeight, // S
|
||||
eggWeightStd, // T
|
||||
item.HenDay, // U
|
||||
henDayStd, // V
|
||||
item.HenHouse, // W
|
||||
henHouseStd, // X
|
||||
formatApprovalStatus(item), // Y
|
||||
safeExportText(pointerString(item.Approval.Notes)), // Z
|
||||
createdBy, // AA
|
||||
formatDateIndonesian(item.CreatedAt), // AB
|
||||
s.name, // AC
|
||||
s.input, // AD
|
||||
createdBy, // AA
|
||||
formatDateIndonesian(item.CreatedAt), // AB
|
||||
s.name, // AC
|
||||
s.input, // AD
|
||||
eggQty(string(utils.FlagTelurUtuh)), // AE
|
||||
eggKg(string(utils.FlagTelurUtuh)), // AF
|
||||
eggQty(string(utils.FlagTelurPecah)), // AG
|
||||
eggKg(string(utils.FlagTelurPecah)), // AH
|
||||
eggQty(string(utils.FlagTelurPutih)), // AI
|
||||
eggKg(string(utils.FlagTelurPutih)), // AJ
|
||||
eggQty(string(utils.FlagTelurRetak)), // AK
|
||||
eggKg(string(utils.FlagTelurRetak)), // AL
|
||||
eggQty(string(utils.FlagTelurPapacal)), // AM
|
||||
eggKg(string(utils.FlagTelurPapacal)), // AN
|
||||
eggQty(string(utils.FlagTelurJumbo)), // AO
|
||||
eggKg(string(utils.FlagTelurJumbo)), // AP
|
||||
}
|
||||
|
||||
for idx, col := range columns {
|
||||
@@ -379,7 +447,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellStyle(sheet, "A3", fmt.Sprintf("AD%d", lastRow), dataCenterStyle); err != nil {
|
||||
if err := file.SetCellStyle(sheet, "A3", fmt.Sprintf("AP%d", lastRow), dataCenterStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -445,6 +513,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
|
||||
mergeCols := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
|
||||
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB",
|
||||
"AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP",
|
||||
}
|
||||
for _, rng := range itemRanges {
|
||||
if rng.end > rng.start {
|
||||
@@ -454,6 +523,53 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
}
|
||||
file.SetCellStyle(sheet, fmt.Sprintf("AC%d", rng.end), fmt.Sprintf("AC%d", rng.end), borderBottomLeftStyle)
|
||||
file.SetCellStyle(sheet, fmt.Sprintf("AD%d", rng.end), fmt.Sprintf("AD%d", rng.end), borderBottomCenterStyle)
|
||||
// Egg columns use center + thick bottom border
|
||||
for _, col := range []string{"AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP"} {
|
||||
file.SetCellStyle(sheet, fmt.Sprintf("%s%d", col, rng.end), fmt.Sprintf("%s%d", col, rng.end), borderBottomCenterStyle)
|
||||
}
|
||||
}
|
||||
|
||||
numFmtInt := "0"
|
||||
numberIntStyle, err := file.NewStyle(&excelize.Style{
|
||||
CustomNumFmt: &numFmtInt,
|
||||
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "E6E6E6", Style: 1},
|
||||
{Type: "top", Color: "E6E6E6", Style: 1},
|
||||
{Type: "bottom", Color: "E6E6E6", Style: 1},
|
||||
{Type: "right", Color: "E6E6E6", Style: 1},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
numFmtFloat := "0.00"
|
||||
numberFloatStyle, err := file.NewStyle(&excelize.Style{
|
||||
CustomNumFmt: &numFmtFloat,
|
||||
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "E6E6E6", Style: 1},
|
||||
{Type: "top", Color: "E6E6E6", Style: 1},
|
||||
{Type: "bottom", Color: "E6E6E6", Style: 1},
|
||||
{Type: "right", Color: "E6E6E6", Style: 1},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
intCols := []string{"E", "I", "AE", "AG", "AI", "AK", "AM", "AO"}
|
||||
for _, col := range intCols {
|
||||
if err := file.SetCellStyle(sheet, col+"3", fmt.Sprintf("%s%d", col, lastRow), numberIntStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
floatCols := []string{"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "AD", "AF", "AH", "AJ", "AL", "AN", "AP"}
|
||||
for _, col := range floatCols {
|
||||
if err := file.SetCellStyle(sheet, col+"3", fmt.Sprintf("%s%d", col, lastRow), numberFloatStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -100,14 +100,20 @@ type RecordingFeedUsageDTO struct {
|
||||
PendingQty float64 `json:"pending_qty"`
|
||||
}
|
||||
|
||||
type EggExportBreakdownDTO struct {
|
||||
Qty int `json:"qty"`
|
||||
Kg float64 `json:"kg"`
|
||||
}
|
||||
|
||||
type RecordingListDTO struct {
|
||||
RecordingRelationDTO
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Kandang *RecordingKandangDTO `json:"kandang,omitempty"`
|
||||
Location *RecordingLocationDTO `json:"location,omitempty"`
|
||||
FeedUsage []RecordingFeedUsageDTO `json:"feed_usage,omitempty"`
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Kandang *RecordingKandangDTO `json:"kandang,omitempty"`
|
||||
Location *RecordingLocationDTO `json:"location,omitempty"`
|
||||
FeedUsage []RecordingFeedUsageDTO `json:"feed_usage,omitempty"`
|
||||
EggExportBreakdown map[string]EggExportBreakdownDTO `json:"egg_breakdown,omitempty"`
|
||||
}
|
||||
|
||||
type RecordingDetailDTO struct {
|
||||
|
||||
@@ -51,6 +51,7 @@ type RecordingRepository interface {
|
||||
UpdateEggTotalQty(tx *gorm.DB, eggID uint, totalQty float64) error
|
||||
UpdateEggWeight(tx *gorm.DB, eggID uint, weight *float64) error
|
||||
GetRecordingEggByID(ctx context.Context, id uint, modifier func(*gorm.DB) *gorm.DB) (*entity.RecordingEgg, error)
|
||||
GetEggsWithFlagsByRecordingIDs(ctx context.Context, recordingIDs []uint) ([]entity.RecordingEgg, error)
|
||||
|
||||
ExistsOnDate(ctx context.Context, projectFlockKandangId uint, recordTime time.Time) (bool, error)
|
||||
|
||||
@@ -581,6 +582,22 @@ func (r *RecordingRepositoryImpl) GetRecordingEggByID(
|
||||
return &egg, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) GetEggsWithFlagsByRecordingIDs(ctx context.Context, recordingIDs []uint) ([]entity.RecordingEgg, error) {
|
||||
if len(recordingIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var eggs []entity.RecordingEgg
|
||||
err := r.DB().WithContext(ctx).
|
||||
Preload("ProductWarehouse.Product").
|
||||
Where("recording_eggs.recording_id IN ?", recordingIDs).
|
||||
Find(&eggs).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eggs, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) ExistsOnDate(ctx context.Context, projectFlockKandangId uint, recordTime time.Time) (bool, error) {
|
||||
if projectFlockKandangId == 0 {
|
||||
return false, nil
|
||||
|
||||
@@ -46,6 +46,7 @@ type RecordingService interface {
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.Recording, error)
|
||||
GetProgressRows(ctx *fiber.Ctx, query *exportprogress.Query) ([]exportprogress.Row, error)
|
||||
GetEggsWithFlagsByRecordingIDs(ctx context.Context, recordingIDs []uint) ([]entity.RecordingEgg, error)
|
||||
}
|
||||
|
||||
type recordingService struct {
|
||||
@@ -259,6 +260,10 @@ func (s recordingService) GetProgressRows(c *fiber.Ctx, query *exportprogress.Qu
|
||||
return s.Repository.GetProgressRows(c.Context(), query.StartDate, query.EndDate, scope.IDs, scope.Restrict)
|
||||
}
|
||||
|
||||
func (s recordingService) GetEggsWithFlagsByRecordingIDs(ctx context.Context, recordingIDs []uint) ([]entity.RecordingEgg, error) {
|
||||
return s.Repository.GetEggsWithFlagsByRecordingIDs(ctx, recordingIDs)
|
||||
}
|
||||
|
||||
func (s recordingService) GetOne(c *fiber.Ctx, id uint) (*entity.Recording, error) {
|
||||
if err := m.EnsureRecordingAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -61,6 +61,7 @@ func (PurchaseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
expenseRealizationRepo,
|
||||
projectFlockKandangRepository,
|
||||
documentSvc,
|
||||
commonSvc.NewFifoPaymentService(db, utils.Log),
|
||||
validate,
|
||||
)
|
||||
expenseBridge := service.NewExpenseBridge(
|
||||
@@ -72,6 +73,7 @@ func (PurchaseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
)
|
||||
|
||||
fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)
|
||||
fifoPaymentService := commonSvc.NewFifoPaymentService(db, utils.Log)
|
||||
|
||||
purchaseService := service.NewPurchaseService(
|
||||
validate,
|
||||
@@ -84,6 +86,7 @@ func (PurchaseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
approvalService,
|
||||
expenseBridge,
|
||||
fifoStockV2Service,
|
||||
fifoPaymentService,
|
||||
documentSvc,
|
||||
)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -64,6 +64,7 @@ type purchaseService struct {
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
ExpenseBridge PurchaseExpenseBridge
|
||||
FifoStockV2Svc commonSvc.FifoStockV2Service
|
||||
FifoPaymentSvc commonSvc.FifoPaymentService
|
||||
DocumentSvc commonSvc.DocumentService
|
||||
approvalWorkflow approvalutils.ApprovalWorkflowKey
|
||||
}
|
||||
@@ -91,6 +92,7 @@ func NewPurchaseService(
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
expenseBridge PurchaseExpenseBridge,
|
||||
fifoStockV2Svc commonSvc.FifoStockV2Service,
|
||||
fifoPaymentSvc commonSvc.FifoPaymentService,
|
||||
documentSvc commonSvc.DocumentService,
|
||||
) PurchaseService {
|
||||
return &purchaseService{
|
||||
@@ -105,6 +107,7 @@ func NewPurchaseService(
|
||||
ApprovalSvc: approvalSvc,
|
||||
ExpenseBridge: expenseBridge,
|
||||
FifoStockV2Svc: fifoStockV2Svc,
|
||||
FifoPaymentSvc: fifoPaymentSvc,
|
||||
DocumentSvc: documentSvc,
|
||||
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 {
|
||||
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
|
||||
}
|
||||
@@ -1406,6 +1408,16 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
c *fiber.Ctx,
|
||||
step approvalutils.ApprovalStep,
|
||||
|
||||
@@ -26,6 +26,8 @@ type ExpenseDepreciationRowDTO struct {
|
||||
DayN int `json:"day_n"`
|
||||
ChickinDate string `json:"chickin_date"`
|
||||
TotalValuePulletAfterDepreciation float64 `json:"total_value_pullet_after_depreciation"`
|
||||
StandardEffectiveDate string `json:"standard_effective_date,omitempty"`
|
||||
TotalPopulation float64 `json:"total_population"`
|
||||
Components any `json:"components"`
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ type houseMultiplicationPercentageRow struct {
|
||||
HouseType string
|
||||
Day int
|
||||
MultiplicationPercentage float64
|
||||
EffectiveDate *time.Time
|
||||
}
|
||||
|
||||
type ExpenseDepreciationRepository interface {
|
||||
@@ -50,7 +51,7 @@ type ExpenseDepreciationRepository interface {
|
||||
DeleteSnapshotsFromDate(ctx context.Context, fromDate time.Time, farmIDs []uint) error
|
||||
DeleteSnapshotsByFarmIDs(ctx context.Context, farmIDs []uint) 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)
|
||||
UpsertManualInput(ctx context.Context, row *entity.FarmDepreciationManualInput) error
|
||||
DB() *gorm.DB
|
||||
@@ -244,21 +245,22 @@ func (r *expenseDepreciationRepository) GetMultiplicationPercentages(
|
||||
ctx context.Context,
|
||||
houseTypes []string,
|
||||
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)
|
||||
effectiveDates := make(map[string]*time.Time)
|
||||
if len(houseTypes) == 0 || maxDay <= 0 {
|
||||
return result, nil
|
||||
return result, effectiveDates, nil
|
||||
}
|
||||
|
||||
rows := make([]houseMultiplicationPercentageRow, 0)
|
||||
if err := r.db.WithContext(ctx).Raw(`
|
||||
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
|
||||
WHERE house_type::text IN ? AND day <= ?
|
||||
ORDER BY house_type, day, effective_date DESC NULLS LAST
|
||||
`, houseTypes, maxDay).Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
@@ -266,9 +268,12 @@ func (r *expenseDepreciationRepository) GetMultiplicationPercentages(
|
||||
result[row.HouseType] = make(map[int]float64)
|
||||
}
|
||||
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(
|
||||
|
||||
@@ -304,7 +304,8 @@ func (s *repportService) GetExpenseDepreciation(ctx *fiber.Ctx) ([]dto.ExpenseDe
|
||||
continue
|
||||
}
|
||||
components := parseSnapshotComponents(snapshot.Components)
|
||||
multiplicationPercentage, dayN, chickinDate := depreciationSnapshotInfo(components)
|
||||
multiplicationPercentage, dayN, chickinDate, standardEffectiveDate := depreciationSnapshotInfo(components)
|
||||
totalPopulation := depreciationTotalPopulation(components)
|
||||
rows = append(rows, dto.ExpenseDepreciationRowDTO{
|
||||
ProjectFlockID: int64(snapshot.ProjectFlockId),
|
||||
FarmName: candidate.FarmName,
|
||||
@@ -316,6 +317,8 @@ func (s *repportService) GetExpenseDepreciation(ctx *fiber.Ctx) ([]dto.ExpenseDe
|
||||
DayN: dayN,
|
||||
ChickinDate: chickinDate,
|
||||
TotalValuePulletAfterDepreciation: snapshot.PulletCostDayNTotal - snapshot.DepreciationValue,
|
||||
StandardEffectiveDate: standardEffectiveDate,
|
||||
TotalPopulation: totalPopulation,
|
||||
Components: components,
|
||||
})
|
||||
}
|
||||
@@ -502,11 +505,14 @@ type depreciationKandangComponent struct {
|
||||
OriginDate string `json:"origin_date,omitempty"`
|
||||
ChickinDate string `json:"chickin_date,omitempty"`
|
||||
StartScheduleDay *int `json:"start_schedule_day,omitempty"`
|
||||
StandardEffectiveDate string `json:"standard_effective_date,omitempty"`
|
||||
Population float64 `json:"population"`
|
||||
}
|
||||
|
||||
type depreciationFarmComponents struct {
|
||||
KandangCount int `json:"kandang_count"`
|
||||
Kandang []depreciationKandangComponent `json:"kandang"`
|
||||
KandangCount int `json:"kandang_count"`
|
||||
TotalPopulation float64 `json:"total_population"`
|
||||
Kandang []depreciationKandangComponent `json:"kandang"`
|
||||
}
|
||||
|
||||
func (s *repportService) computeExpenseDepreciationSnapshots(
|
||||
@@ -540,6 +546,7 @@ func (s *repportService) computeExpenseDepreciationSnapshots(
|
||||
|
||||
totalDepreciationValue := 0.0
|
||||
totalPulletCostDayN := 0.0
|
||||
totalPopulation := 0.0
|
||||
for _, kandangID := range kandangIDs {
|
||||
breakdown, err := s.HppV2Svc.CalculateHppBreakdown(kandangID, &periodDate)
|
||||
if err != nil {
|
||||
@@ -575,6 +582,8 @@ func (s *repportService) computeExpenseDepreciationSnapshots(
|
||||
DepreciationSource: part.Code,
|
||||
OriginDate: 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 == "" {
|
||||
@@ -605,11 +614,13 @@ func (s *repportService) computeExpenseDepreciationSnapshots(
|
||||
|
||||
totalPulletCostDayN += component.PulletCostDayN
|
||||
totalDepreciationValue += component.DepreciationValue
|
||||
totalPopulation += component.Population
|
||||
components.Kandang = append(components.Kandang, component)
|
||||
}
|
||||
}
|
||||
|
||||
components.KandangCount = len(components.Kandang)
|
||||
components.TotalPopulation = totalPopulation
|
||||
effectivePercent := approvalService.CalculateEffectiveDepreciationPercent(totalDepreciationValue, totalPulletCostDayN)
|
||||
|
||||
componentsJSON, marshalErr := json.Marshal(components)
|
||||
@@ -744,14 +755,14 @@ func parseSnapshotComponents(raw []byte) any {
|
||||
return out
|
||||
}
|
||||
|
||||
func depreciationSnapshotInfo(components any) (float64, int, string) {
|
||||
func depreciationSnapshotInfo(components any) (float64, int, string, string) {
|
||||
root, ok := components.(map[string]any)
|
||||
if !ok {
|
||||
return 0, 0, ""
|
||||
return 0, 0, "", ""
|
||||
}
|
||||
kandang, ok := root["kandang"].([]any)
|
||||
if !ok {
|
||||
return 0, 0, ""
|
||||
return 0, 0, "", ""
|
||||
}
|
||||
for _, raw := range kandang {
|
||||
component, ok := raw.(map[string]any)
|
||||
@@ -765,10 +776,19 @@ func depreciationSnapshotInfo(components any) (float64, int, string) {
|
||||
chickinDate = anyString(component["origin_date"])
|
||||
}
|
||||
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 {
|
||||
@@ -831,37 +851,28 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing
|
||||
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)
|
||||
for customerID := range customerGroups {
|
||||
transactions, err := s.CustomerPaymentRepo.GetCustomerPaymentTransactions(c.Context(), &customerID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
initialBalance, err := s.CustomerPaymentRepo.GetInitialBalanceByCustomer(c.Context(), customerID)
|
||||
if err != nil {
|
||||
initialBalance = 0
|
||||
}
|
||||
|
||||
runningBalance := initialBalance
|
||||
for i, tx := range transactions {
|
||||
if tx.TransactionType == "SALES" {
|
||||
previousBalance := runningBalance
|
||||
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
|
||||
allMdpIDsForAging := make([]uint, 0)
|
||||
for _, dp := range deliveryProducts {
|
||||
allMdpIDsForAging = append(allMdpIDsForAging, dp.Id)
|
||||
}
|
||||
mdpAllocSummaryForMarketing, err := s.fetchMdpAllocationSummary(c.Context(), allMdpIDsForAging)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
for _, dp := range deliveryProducts {
|
||||
summary := mdpAllocSummaryForMarketing[dp.Id]
|
||||
soDate := dp.MarketingProduct.Marketing.SoDate
|
||||
if customerPaymentStatusFromAllocation(dp.TotalPrice, summary.PaidAmount) == "LUNAS" && !summary.LastPaymentDate.IsZero() {
|
||||
days := int(summary.LastPaymentDate.Sub(soDate).Hours() / 24)
|
||||
if days < 0 {
|
||||
days = 0
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 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))
|
||||
runningBalance := initialBalance
|
||||
|
||||
for i, tx := range transactions {
|
||||
|
||||
previousBalance := runningBalance
|
||||
for _, tx := range transactions {
|
||||
|
||||
row := dto.ToCustomerPaymentReportRow(tx)
|
||||
|
||||
if tx.TransactionType == "SALES" {
|
||||
runningBalance -= tx.TotalPrice
|
||||
status, paymentDate := s.determineSalesStatusAndPaymentDate(transactions, i, previousBalance, runningBalance)
|
||||
row.Status = status
|
||||
summary := mdpAllocSummary[uint(tx.TransactionID)]
|
||||
row.Status = customerPaymentStatusFromAllocation(tx.TotalPrice, summary.PaidAmount)
|
||||
|
||||
if status == "LUNAS" {
|
||||
if paymentDate != nil {
|
||||
days := int(paymentDate.Sub(tx.TransDate).Hours() / 24)
|
||||
row.AgingDay = &days
|
||||
} else {
|
||||
days := 0
|
||||
row.AgingDay = &days
|
||||
if row.Status == "LUNAS" && !summary.LastPaymentDate.IsZero() {
|
||||
days := int(summary.LastPaymentDate.Sub(tx.TransDate).Hours() / 24)
|
||||
if days < 0 {
|
||||
days = 0
|
||||
}
|
||||
row.AgingDay = &days
|
||||
} else if row.Status == "LUNAS" {
|
||||
zero := 0
|
||||
row.AgingDay = &zero
|
||||
} else {
|
||||
days := int(time.Since(tx.TransDate).Hours() / 24)
|
||||
row.AgingDay = &days
|
||||
@@ -1343,91 +1365,19 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
|
||||
return dto.ToCustomerPaymentReportItem(*customer, initialBalance, rows, summary), nil
|
||||
}
|
||||
|
||||
func (s *repportService) determineSalesStatusAndPaymentDate(transactions []repportRepo.CustomerPaymentTransaction, currentIndex int, previousBalance, currentBalance float64) (string, *time.Time) {
|
||||
currentSales := transactions[currentIndex]
|
||||
|
||||
if previousBalance >= currentSales.TotalPrice {
|
||||
type paymentAllocation struct {
|
||||
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
|
||||
// customerPaymentStatusFromAllocation menentukan status per-MDP berdasarkan
|
||||
// SUM(payment_allocations.amount) vs MDP total_price.
|
||||
func customerPaymentStatusFromAllocation(totalPrice, paidAmount float64) string {
|
||||
if totalPrice <= fifoAllocationEpsilon {
|
||||
return "LUNAS"
|
||||
}
|
||||
|
||||
hasPartialPaymentFromBalance := previousBalance > 0 && previousBalance < currentSales.TotalPrice
|
||||
|
||||
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 >= totalPrice {
|
||||
return "LUNAS"
|
||||
}
|
||||
|
||||
if hasPayment || hasPartialPaymentFromBalance {
|
||||
return "DIBAYAR SEBAGIAN", nil
|
||||
if paidAmount > fifoAllocationEpsilon {
|
||||
return "DIBAYAR SEBAGIAN"
|
||||
}
|
||||
|
||||
return "BELUM LUNAS", nil
|
||||
return "BELUM LUNAS"
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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))
|
||||
for _, purchase := range purchases {
|
||||
purchasesBySupplier[purchase.SupplierId] = append(purchasesBySupplier[purchase.SupplierId], purchase)
|
||||
@@ -1951,15 +1892,34 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
DeltaBalance float64
|
||||
CountTotals bool
|
||||
}
|
||||
type debtSupplierAllocation struct {
|
||||
RowIndex int
|
||||
SortTime time.Time
|
||||
Amount float64
|
||||
CalcAging func(endDate time.Time) int
|
||||
|
||||
// Batch fetch payment allocation summaries (per purchase + per expense) untuk semua supplier.
|
||||
// FIFO matching dilakukan saat payment di-create/update; report tinggal baca dari DB.
|
||||
allPurchaseIDs := make([]uint, 0)
|
||||
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 {
|
||||
Date time.Time
|
||||
Amount float64
|
||||
purchaseAllocSummary, err := s.fetchPurchaseAllocationSummary(c.Context(), allPurchaseIDs)
|
||||
if err != nil {
|
||||
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 {
|
||||
@@ -1974,7 +1934,7 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
total := dto.DebtSupplierTotalDTO{}
|
||||
|
||||
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 {
|
||||
row := buildDebtSupplierRow(purchase, now, location)
|
||||
sortTime := resolveDebtSupplierSortTime(purchase, params.FilterBy, location)
|
||||
@@ -1986,17 +1946,11 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
DeltaBalance: -row.TotalPrice,
|
||||
CountTotals: true,
|
||||
})
|
||||
capturedPurchase := purchase
|
||||
purchaseAllocations = append(purchaseAllocations, debtSupplierAllocation{
|
||||
RowIndex: rowIndex,
|
||||
SortTime: sortTime,
|
||||
Amount: row.TotalPrice,
|
||||
CalcAging: func(endDate time.Time) int { return calculateDebtSupplierAging(capturedPurchase, endDate, location) },
|
||||
})
|
||||
rowRefs = append(rowRefs, rowRef{Index: rowIndex, Kind: "PURCHASE", Purchase: purchase})
|
||||
}
|
||||
|
||||
for _, exp := range expensesBySupplier[supplierID] {
|
||||
row := buildDebtSupplierExpenseRow(exp, expenseWarehousesMap[exp.Id], now, location)
|
||||
row := buildDebtSupplierExpenseRow(exp, now, location)
|
||||
sortTime := exp.TransactionDate.In(location)
|
||||
rowIndex := len(combinedRows)
|
||||
combinedRows = append(combinedRows, debtSupplierRowItem{
|
||||
@@ -2006,25 +1960,7 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
DeltaBalance: -row.TotalPrice,
|
||||
CountTotals: true,
|
||||
})
|
||||
capturedExp := 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
|
||||
rowRefs = append(rowRefs, rowRef{Index: rowIndex, Kind: "EXPENSE", Expense: exp})
|
||||
}
|
||||
|
||||
for _, payment := range paymentItems {
|
||||
@@ -2037,51 +1973,29 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
DeltaBalance: payment.Nominal,
|
||||
CountTotals: false,
|
||||
})
|
||||
paymentAllocations = append(paymentAllocations, paymentAllocation{
|
||||
Date: sortTime,
|
||||
Amount: payment.Nominal,
|
||||
})
|
||||
}
|
||||
|
||||
if len(purchaseAllocations) > 0 && len(paymentAllocations) > 0 {
|
||||
sort.SliceStable(purchaseAllocations, func(i, j int) bool {
|
||||
return purchaseAllocations[i].SortTime.Before(purchaseAllocations[j].SortTime)
|
||||
})
|
||||
sort.SliceStable(paymentAllocations, func(i, j int) bool {
|
||||
return paymentAllocations[i].Date.Before(paymentAllocations[j].Date)
|
||||
})
|
||||
remaining := make([]float64, len(purchaseAllocations))
|
||||
for i := range purchaseAllocations {
|
||||
remaining[i] = purchaseAllocations[i].Amount
|
||||
// Determine Status & Aging dari payment_allocations DB.
|
||||
for _, ref := range rowRefs {
|
||||
rowTotal := combinedRows[ref.Index].Row.TotalPrice
|
||||
if rowTotal <= fifoAllocationEpsilon {
|
||||
continue
|
||||
}
|
||||
purchaseIndex := 0
|
||||
for _, pay := range paymentAllocations {
|
||||
amount := pay.Amount
|
||||
if amount <= 0 {
|
||||
continue
|
||||
}
|
||||
if paymentCarry > 0 {
|
||||
used := math.Min(amount, paymentCarry)
|
||||
paymentCarry -= used
|
||||
amount -= used
|
||||
}
|
||||
for amount > 0 && purchaseIndex < len(remaining) {
|
||||
if remaining[purchaseIndex] <= 0 {
|
||||
purchaseIndex++
|
||||
continue
|
||||
}
|
||||
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
|
||||
var summary paymentAllocationSummary
|
||||
if ref.Kind == "PURCHASE" {
|
||||
summary = purchaseAllocSummary[ref.Purchase.Id]
|
||||
} else {
|
||||
summary = expenseAllocSummary[ref.Expense.Id]
|
||||
}
|
||||
if summary.PaidAmount+fifoAllocationEpsilon < rowTotal {
|
||||
continue
|
||||
}
|
||||
combinedRows[ref.Index].Row.Status = "Lunas"
|
||||
if !summary.LastPaymentDate.IsZero() {
|
||||
if ref.Kind == "PURCHASE" {
|
||||
combinedRows[ref.Index].Row.Aging = calculateDebtSupplierAging(ref.Purchase, summary.LastPaymentDate.In(location), location)
|
||||
} else {
|
||||
combinedRows[ref.Index].Row.Aging = calculateExpenseAging(ref.Expense, summary.LastPaymentDate.In(location), location)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2208,6 +2122,12 @@ func buildDebtSupplierRow(purchase entity.Purchase, now time.Time, loc *time.Loc
|
||||
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{
|
||||
PrNumber: prNumber,
|
||||
PoNumber: poNumber,
|
||||
@@ -2215,7 +2135,7 @@ func buildDebtSupplierRow(purchase entity.Purchase, now time.Time, loc *time.Loc
|
||||
ReceivedDate: receivedDate,
|
||||
Aging: aging,
|
||||
Area: area,
|
||||
Warehouses: warehouses,
|
||||
Warehouse: firstWarehouse,
|
||||
DueDate: dueDate,
|
||||
DueStatus: dueStatus,
|
||||
TotalPrice: totalPrice,
|
||||
@@ -2245,7 +2165,7 @@ func buildDebtSupplierPaymentRow(payment entity.Payment, loc *time.Location) dto
|
||||
ReceivedDate: payment.PaymentDate.In(loc).Format("2006-01-02"),
|
||||
Aging: 0,
|
||||
Area: nil,
|
||||
Warehouses: []warehouseDTO.WarehouseRelationDTO{},
|
||||
Warehouse: nil,
|
||||
DueDate: "-",
|
||||
DueStatus: "-",
|
||||
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 {
|
||||
if strings.EqualFold(strings.TrimSpace(filterBy), "po_date") {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
}
|
||||
|
||||
totalPrice := 0.0
|
||||
for _, ns := range exp.Nonstocks {
|
||||
totalPrice += ns.Qty * ns.Price
|
||||
}
|
||||
// TotalPrice pakai expense.GrandTotal (= SUM realisasi) supaya konsisten dengan
|
||||
// FIFO allocation yang juga pakai realisasi. Hindari pakai SUM nonstock pengajuan
|
||||
// karena bisa beda nilai dari realisasi → mismatch dengan paid_amount → status salah.
|
||||
totalPrice := exp.GrandTotal
|
||||
|
||||
var area *areaDTO.AreaRelationDTO
|
||||
if exp.Location != nil && exp.Location.Area.Id != 0 {
|
||||
@@ -2371,15 +2400,6 @@ func buildDebtSupplierExpenseRow(exp entity.Expense, warehouses []entity.Warehou
|
||||
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 := ""
|
||||
if strings.TrimSpace(exp.PoNumber) != "" {
|
||||
poNumber = exp.PoNumber
|
||||
@@ -2392,7 +2412,7 @@ func buildDebtSupplierExpenseRow(exp entity.Expense, warehouses []entity.Warehou
|
||||
ReceivedDate: dateStr,
|
||||
Aging: aging,
|
||||
Area: area,
|
||||
Warehouses: warehouseDTOs,
|
||||
Warehouse: nil,
|
||||
DueDate: "-",
|
||||
DueStatus: "-",
|
||||
TotalPrice: totalPrice,
|
||||
|
||||
Reference in New Issue
Block a user