Compare commits

...

48 Commits

Author SHA1 Message Date
giovanni 5511dc78dc add migration for update day recording pullet cikaum 1 dan 2 2026-05-07 17:24:46 +07:00
Giovanni Gabriel Septriadi 699e4448d1 Merge branch 'fix/marketing' into 'development'
[FIX][BE]: add field name to document

See merge request mbugroup/lti-api!515
2026-05-07 09:05:05 +00:00
giovanni 6f02387d69 add field name to document 2026-05-07 16:03:47 +07:00
Giovanni Gabriel Septriadi fc5d5d8ad4 Merge branch 'fix/marketing' into 'development'
[FIX][BE]: adjust path document for detail biaya

See merge request mbugroup/lti-api!514
2026-05-07 08:46:29 +00:00
giovanni 0d6ab5e718 adjust path document for detail biaya 2026-05-07 15:45:39 +07:00
Giovanni Gabriel Septriadi 547fc309f5 Merge branch 'fix/marketing' into 'development'
add query sort by grand total

See merge request mbugroup/lti-api!513
2026-05-07 07:12:11 +00:00
giovanni 094e8f904b add query sort by grand total 2026-05-07 14:11:39 +07:00
Giovanni Gabriel Septriadi 0d928d5856 Merge branch 'fix/marketing' into 'development'
[FIX][BE]: add sorting at marketing

See merge request mbugroup/lti-api!512
2026-05-07 07:04:22 +00:00
giovanni 0357531e73 add sorting at marketing 2026-05-07 14:03:17 +07:00
Giovanni Gabriel Septriadi 2fa279c073 Merge branch 'feat/export-recording-a' into 'development'
[FEAT][BE]: add feed use at export excel recording

See merge request mbugroup/lti-api!511
2026-05-07 03:58:19 +00:00
giovanni 90ed035abd add feed use at export excel recording 2026-05-07 10:56:30 +07:00
Rivaldi A N S 81b9e88bb6 Merge branch 'fix/marketing-report-pdf' into 'development'
[FIX][BE] Marketing Report PDF

See merge request mbugroup/lti-api!510
2026-05-06 06:14:45 +00:00
ValdiANS 7e01d8afb9 fix: adjust marketing report pdf column and copywriting 2026-05-06 13:13:02 +07:00
Giovanni Gabriel Septriadi d5a98b95dc Merge branch 'fix/result' into 'development'
[FIX][BE]: fix woa at hasil produksi

See merge request mbugroup/lti-api!508
2026-05-05 09:39:35 +00:00
giovanni 8900937e71 fix woa at hasil produksi 2026-05-05 16:38:37 +07:00
Giovanni Gabriel Septriadi cad80e2216 Merge branch 'fix/chickin' into 'development'
[FIX][BE]: add migration for edit chickin_date pullet cikaum 1 dan pullet cikaum 2

See merge request mbugroup/lti-api!507
2026-05-05 08:45:52 +00:00
Giovanni Gabriel Septriadi 34dad85734 Merge branch 'fix/kosong' into 'development'
[FIX][BE]: fix laporan daily checklist kandang kosong

See merge request mbugroup/lti-api!506
2026-05-05 08:44:51 +00:00
giovanni b1d2d30773 add migration for edit chickin_date pullet cikaum 1 dan pullet cikaum 2 2026-05-05 15:44:07 +07:00
giovanni f910d165e4 fix laporan daily checklist kandang kosong 2026-05-05 14:06:41 +07:00
M1 AIR 6f6985ef32 ci: ignore partial aws env during ecr login 2026-05-05 13:32:16 +07:00
M1 AIR d07f074fb1 fix(migrate): align recording day constraint with zero-based migration 2026-05-05 12:09:02 +07:00
Giovanni Gabriel Septriadi 561481679e Merge branch 'fix/stock-log' into 'development'
[FIX][BE]: fixing stock log when editing recording

See merge request mbugroup/lti-api!503
2026-05-05 03:08:41 +00:00
Giovanni Gabriel Septriadi d86577f007 Merge branch 'feat/export-po' into 'development'
[FEAT][BE]: add kolom gudang tujuan to export PURCHASE

See merge request mbugroup/lti-api!504
2026-05-05 03:08:01 +00:00
giovanni 49ea3f0295 add command for fix stock log missmatch 2026-05-05 10:06:35 +07:00
giovanni 7bab8c66c1 add gudang tujuan to po 2026-05-05 06:41:10 +07:00
giovanni f9de4d28f9 fixing stock log when editing recording 2026-05-04 23:06:13 +07:00
Rivaldi A N S 45028212e1 Merge branch 'fix/daily-checklist' into 'development'
[FIX][BE] Daily Checklist

See merge request mbugroup/lti-api!502
2026-05-04 09:39:23 +00:00
ValdiANS 0f285dc684 Merge branch 'fix/daily-checklist' of https://gitlab.com/mbugroup/lti-api into fix/daily-checklist 2026-05-04 16:36:05 +07:00
ValdiANS d0cd82c703 Merge branch 'development' into fix/daily-checklist 2026-05-04 16:29:07 +07:00
ValdiANS 48351661c5 fix: add order_by and sort_by query to master data employee 2026-05-04 16:28:03 +07:00
ValdiANS 19d7cd33ca fix: add search for Kandang Kosong 2026-05-04 16:27:50 +07:00
Giovanni Gabriel Septriadi 03474dc1fa Merge branch 'feat/umur' into 'development'
[FEAT][BE]: adjust calculate umur ayam at recording

See merge request mbugroup/lti-api!500
2026-05-04 06:38:19 +00:00
Rivaldi A N S 916fa4205b Merge branch 'fix/master-data-kandang' into 'development'
[FIX][BE] Master Data Kandang

See merge request mbugroup/lti-api!501
2026-05-04 04:59:12 +00:00
ValdiANS b2be67e052 fix: add sort_by and order_by query in master data kandang and kandang groups API 2026-05-04 11:54:19 +07:00
giovanni 0ac40adb5a adjust calculate umur ayam at recording 2026-05-04 11:30:53 +07:00
Adnan Zahir cee59c2b99 Merge branch 'fix/recording-validation' into 'development'
fix: allow editing sold egg weight on recording

See merge request mbugroup/lti-api!499
2026-05-02 17:05:00 +07:00
Adnan Zahir da99bf1429 fix: allow editing sold egg weight on recording 2026-05-02 17:03:57 +07:00
Adnan Zahir 28fd711ece Merge branch 'feat/fix-openapi-dashboard-integration' into 'development'
fix: resolve dashboard OpenAPI integration issues

See merge request mbugroup/lti-api!497
2026-05-02 10:59:28 +07:00
Adnan Zahir 3768892a17 fix: resolve dashboard OpenAPI integration issues
- FCRs & Transfer to Laying: add ExampleResponse field to routeMeta and
  inject example payloads into OpenAPI 200 responses for list and detail
  endpoints so dashboard consumers have concrete response shapes to work with

- Chick In: enable GET /api/production/chickins/ list endpoint (was
  commented out); add P_ChickinsGetAll permission constant and wire it
  into the route; add OpenAPI spec entry with query params and example

- Recording GET all: fix N+1 query bottleneck (2-3s response time) by
  pre-fetching approved transfer maps per PFK ID in two batch queries
  before the per-recording loop; add evaluatePopulationMutationStateFromCaches
  that uses the pre-fetched maps and caches hasAnyRecordingOnTransferTargets
  results by transfer ID — reducing per-page query count from ~20-40 to ~10-12

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-02 10:57:45 +07:00
Giovanni Gabriel Septriadi c804c59f05 Merge branch 'feat/empty' into 'development'
[FEAT][BE]: add search nominal keuangan

See merge request mbugroup/lti-api!496
2026-04-30 08:56:19 +00:00
giovanni b219bf829f add search nominal keuangan 2026-04-30 15:54:56 +07:00
Giovanni Gabriel Septriadi f97b1a6484 Merge branch 'feat/empty' into 'development'
[FIX][BE]: adjust empty kandang daily checklist

See merge request mbugroup/lti-api!495
2026-04-30 04:16:19 +00:00
giovanni 0d2cdef10f adjust empty kandang daily checklist 2026-04-30 11:15:11 +07:00
Adnan Zahir 128c8e0d08 Merge branch 'feat/toggle-negative-usgae' into 'development'
fix: hide legace unflagged products to be consistent with the validation

See merge request mbugroup/lti-api!493
2026-04-29 12:18:56 +07:00
Adnan Zahir cf4e723f64 fix: hide legace unflagged products to be consistent with the validation 2026-04-29 11:55:35 +07:00
Giovanni Gabriel Septriadi a3156a156f Merge branch 'feat/export-penjualan' into 'development'
[FEAT][BE]: add export excel and pdf report penjualan

See merge request mbugroup/lti-api!492
2026-04-29 04:53:03 +00:00
giovanni 88b6e2f294 adjust sql migration 2026-04-02 11:40:38 +07:00
giovanni 36b0f97897 fix upser daily checklist status rejected; fix search list daily checklist 2026-04-02 11:24:53 +07:00
47 changed files with 1742 additions and 317 deletions
+16 -2
View File
@@ -27,11 +27,25 @@ workflow:
.ecr_login: &ecr_login | .ecr_login: &ecr_login |
AWS_CLI_ENV_ARGS="" AWS_CLI_ENV_ARGS=""
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_REGION=$AWS_REGION" AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_REGION=$AWS_REGION"
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-}"
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:-}" HAS_ACCESS_KEY="false"
HAS_SECRET_KEY="false"
if [ -n "${AWS_ACCESS_KEY_ID:-}" ]; then
HAS_ACCESS_KEY="true"
fi
if [ -n "${AWS_SECRET_ACCESS_KEY:-}" ]; then
HAS_SECRET_KEY="true"
fi
if [ "$HAS_ACCESS_KEY" = "true" ] && [ "$HAS_SECRET_KEY" = "true" ]; then
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID"
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY"
if [ -n "${AWS_SESSION_TOKEN:-}" ]; then if [ -n "${AWS_SESSION_TOKEN:-}" ]; then
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN" AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
fi fi
elif [ "$HAS_ACCESS_KEY" = "true" ] || [ "$HAS_SECRET_KEY" = "true" ] || [ -n "${AWS_SESSION_TOKEN:-}" ]; then
echo "WARN: Incomplete AWS_* env vars detected; ignoring injected AWS credentials for ECR login."
fi
PASS="$(docker run --rm $AWS_CLI_ENV_ARGS public.ecr.aws/aws-cli/aws-cli:latest \ PASS="$(docker run --rm $AWS_CLI_ENV_ARGS public.ecr.aws/aws-cli/aws-cli:latest \
ecr get-login-password --region "$AWS_REGION" || true)" ecr get-login-password --region "$AWS_REGION" || true)"
+387
View File
@@ -0,0 +1,387 @@
// Command: fix-stock-log-drift
//
// Tujuan:
// Sinkronkan `stock_logs.stock` (running ledger) dengan `product_warehouses.qty`
// (FIFO truth) ketika keduanya drift.
//
// Drift biasanya terjadi karena bug di Recording-Edit (sebelum fix) yang
// hanya menulis -decrease tanpa +increase saat in-place update. Akibatnya
// running ledger di stock_logs tertinggal dari qty riil di product_warehouses.
//
// Cara kerja:
// 1. Ambil product_warehouses.qty (sebagai truth)
// 2. Ambil last_stock_log.stock
// 3. Cari recording yang berkontribusi pada drift (untuk notes)
// 4. Hitung drift = qty - last_stock_log.stock
// 5. Jika drift != 0, insert 1 stock_log corrective:
// - drift > 0 → increase = drift
// - drift < 0 → decrease = |drift|
// stock akhir akan sama dengan qty (truth).
// Notes otomatis berisi daftar recording IDs yang berkontribusi pada drift.
//
// Mode:
// --apply=false (default) → dry-run, hanya tampilkan rencana
// --apply=true → eksekusi insert
//
// Contoh:
// go run cmd/fix-stock-log-drift/main.go --product-warehouse-id=1261
// go run cmd/fix-stock-log-drift/main.go --product-warehouse-id=1261 --apply
// go run cmd/fix-stock-log-drift/main.go --product-warehouse-id=1261 --apply \
// --actor-id=1 --notes="Koreksi manual drift"
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"math"
"os"
"strings"
"time"
"gitlab.com/mbugroup/lti-api.git/internal/config"
"gitlab.com/mbugroup/lti-api.git/internal/database"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"gorm.io/gorm"
)
const (
qtyEpsilon = 1e-6
defaultActorID uint = 1
maxSuspectInNotes = 30
)
type driftRow struct {
ProductWarehouseID uint `gorm:"column:product_warehouse_id"`
ProductID uint `gorm:"column:product_id"`
ProductName string `gorm:"column:product_name"`
WarehouseName string `gorm:"column:warehouse_name"`
CurrentQty float64 `gorm:"column:current_qty"`
LastLogStock float64 `gorm:"column:last_log_stock"`
LastLogID uint `gorm:"column:last_log_id"`
FifoExpected float64 `gorm:"column:fifo_expected"`
}
type suspectRecording struct {
RecordingID uint `gorm:"column:recording_id"`
FifoUsage float64 `gorm:"column:fifo_usage"`
NetLogConsumed float64 `gorm:"column:net_log_consumed"`
Phantom float64 `gorm:"column:phantom"`
}
func main() {
var (
productWarehouseID uint
apply bool
actorID uint
notes string
)
flag.UintVar(&productWarehouseID, "product-warehouse-id", 0, "Target product_warehouse_id (required)")
flag.BoolVar(&apply, "apply", false, "Apply changes. If false, run as dry-run")
flag.UintVar(&actorID, "actor-id", defaultActorID, "User id yang akan dicatat sebagai created_by stock_log corrective")
flag.StringVar(&notes, "notes", "", "Custom notes untuk stock_log corrective (opsional, default=auto-generate dari data recording)")
flag.Parse()
notes = strings.TrimSpace(notes)
if err := validateFlags(productWarehouseID, actorID); err != nil {
log.Fatalf("invalid flags: %v", err)
}
ctx := context.Background()
db := database.Connect(config.DBHost, config.DBName)
row, err := loadDriftRow(ctx, db, productWarehouseID)
if err != nil {
log.Fatalf("failed to load product warehouse: %v", err)
}
suspects, err := loadSuspectRecordings(ctx, db, productWarehouseID)
if err != nil {
log.Fatalf("failed to load suspect recordings: %v", err)
}
drift := row.CurrentQty - row.LastLogStock
// Print info header
fmt.Printf("Mode: %s\n", modeLabel(apply))
fmt.Printf("Target product_warehouse_id: %d\n", productWarehouseID)
fmt.Printf("Product: %q\n", row.ProductName)
fmt.Printf("Warehouse: %q\n", row.WarehouseName)
fmt.Printf("Current qty (product_warehouses): %.3f\n", row.CurrentQty)
fmt.Printf("FIFO expected (sum total_qty - total_used): %.3f\n", row.FifoExpected)
fmt.Printf("Last stock_log: id=%d stock=%.3f\n", row.LastLogID, row.LastLogStock)
fmt.Printf("Drift (qty - last_log_stock): %+.3f\n", drift)
if !nearlyEqual(row.CurrentQty, row.FifoExpected) {
fmt.Println()
fmt.Println("⚠️ WARNING: product_warehouses.qty TIDAK match dengan FIFO expected.")
fmt.Println(" Disarankan jalankan dulu cmd/reflow-quantity-product-warehouse-from-stock-allocation")
fmt.Println(" sebelum fix stock_log drift, agar truth source-nya sudah benar.")
}
// Print suspect recordings
fmt.Println()
if len(suspects) > 0 {
totalPhantom := 0.0
for _, s := range suspects {
totalPhantom += s.Phantom
}
fmt.Printf("Suspect recordings (drift contributors): %d\n", len(suspects))
for _, s := range suspects {
fmt.Printf(
" #%-6d fifo=%-10.3f net_log=%-10.3f phantom=%+.3f\n",
s.RecordingID, s.FifoUsage, s.NetLogConsumed, s.Phantom,
)
}
fmt.Printf("Total suspect phantom: %+.3f\n", totalPhantom)
} else {
fmt.Println("Suspect recordings: none found (drift origin unknown)")
}
fmt.Println()
if nearlyEqual(drift, 0) {
fmt.Println("✓ Tidak ada drift. Stock_log sudah sinkron dengan product_warehouses.qty.")
fmt.Println("Summary: planned=0 inserted=0 skipped=1 failed=0")
return
}
// Build notes if not provided
if notes == "" {
notes = buildDefaultNotes(row, drift, suspects)
}
plan := buildCorrectiveLog(row, drift, actorID, notes)
fmt.Printf(
"PLAN insert stock_log:\n pw=%d increase=%.3f decrease=%.3f stock=%.3f\n notes=%q\n",
plan.ProductWarehouseId,
plan.Increase,
plan.Decrease,
plan.Stock,
plan.Notes,
)
if !apply {
fmt.Println()
fmt.Println("Summary: planned=1 inserted=0 skipped=0 failed=0 (dry-run)")
return
}
if err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// Re-check di dalam transaction agar aman dari race condition
current, err := loadDriftRow(ctx, tx, productWarehouseID)
if err != nil {
return fmt.Errorf("re-read product_warehouse_id=%d: %w", productWarehouseID, err)
}
currentDrift := current.CurrentQty - current.LastLogStock
if nearlyEqual(currentDrift, 0) {
fmt.Println("Drift hilang sebelum insert (kemungkinan ada operasi paralel). Skip.")
return nil
}
fresh := buildCorrectiveLog(current, currentDrift, actorID, notes)
if err := tx.Create(&fresh).Error; err != nil {
return fmt.Errorf("insert corrective stock_log for pw=%d: %w", productWarehouseID, err)
}
fmt.Printf(
"DONE inserted stock_log id=%d pw=%d increase=%.3f decrease=%.3f stock=%.3f\n",
fresh.Id,
fresh.ProductWarehouseId,
fresh.Increase,
fresh.Decrease,
fresh.Stock,
)
return nil
}); err != nil {
fmt.Println()
fmt.Println("Summary: planned=1 inserted=0 skipped=0 failed=1")
log.Printf("error: %v", err)
os.Exit(1)
}
fmt.Println()
fmt.Println("Summary: planned=1 inserted=1 skipped=0 failed=0")
}
func validateFlags(productWarehouseID uint, actorID uint) error {
if productWarehouseID == 0 {
return errors.New("--product-warehouse-id is required (must be > 0)")
}
if actorID == 0 {
return errors.New("--actor-id must be > 0")
}
return nil
}
func loadDriftRow(ctx context.Context, db *gorm.DB, productWarehouseID uint) (driftRow, error) {
row := driftRow{}
lastLogSub := db.WithContext(ctx).
Table("stock_logs").
Select("id, product_warehouse_id, stock").
Where("product_warehouse_id = ?", productWarehouseID).
Order("id DESC").
Limit(1)
fifoSub := db.WithContext(ctx).
Table("purchase_items").
Select(`
product_warehouse_id,
COALESCE(SUM(COALESCE(total_qty, 0) - COALESCE(total_used, 0)), 0) AS fifo_expected
`).
Where("product_warehouse_id = ?", productWarehouseID).
Group("product_warehouse_id")
if err := db.WithContext(ctx).
Table("product_warehouses pw").
Select(`
pw.id AS product_warehouse_id,
pw.product_id AS product_id,
COALESCE(p.name, '') AS product_name,
COALESCE(w.name, '') AS warehouse_name,
COALESCE(pw.qty, 0) AS current_qty,
COALESCE(last_log.stock, 0) AS last_log_stock,
COALESCE(last_log.id, 0) AS last_log_id,
COALESCE(fifo.fifo_expected, 0) AS fifo_expected
`).
Joins("LEFT JOIN products p ON p.id = pw.product_id").
Joins("LEFT JOIN warehouses w ON w.id = pw.warehouse_id").
Joins("LEFT JOIN (?) last_log ON last_log.product_warehouse_id = pw.id", lastLogSub).
Joins("LEFT JOIN (?) fifo ON fifo.product_warehouse_id = pw.id", fifoSub).
Where("pw.id = ?", productWarehouseID).
Scan(&row).Error; err != nil {
return row, err
}
if row.ProductWarehouseID == 0 {
return row, fmt.Errorf("product_warehouse_id=%d not found", productWarehouseID)
}
return row, nil
}
// loadSuspectRecordings mencari recording yang net stock_log consumed-nya
// melebihi FIFO usage_qty — ini adalah recording yang berkontribusi pada drift
// akibat bug Recording-Edit yang hanya menulis -decrease tanpa +increase.
func loadSuspectRecordings(ctx context.Context, db *gorm.DB, productWarehouseID uint) ([]suspectRecording, error) {
rows := make([]suspectRecording, 0)
if err := db.WithContext(ctx).
Table("recording_stocks rs").
Select(`
rs.recording_id,
COALESCE(rs.usage_qty, 0) AS fifo_usage,
(
COALESCE(SUM(CASE WHEN sl.decrease > 0 THEN sl.decrease ELSE 0 END), 0) -
COALESCE(SUM(CASE WHEN sl.increase > 0 THEN sl.increase ELSE 0 END), 0)
) AS net_log_consumed,
(
COALESCE(SUM(CASE WHEN sl.decrease > 0 THEN sl.decrease ELSE 0 END), 0) -
COALESCE(SUM(CASE WHEN sl.increase > 0 THEN sl.increase ELSE 0 END), 0) -
COALESCE(rs.usage_qty, 0)
) AS phantom
`).
Joins(`
JOIN stock_logs sl ON sl.loggable_type = ?
AND sl.loggable_id = rs.recording_id
AND sl.product_warehouse_id = rs.product_warehouse_id
`, string(utils.StockLogTypeRecording)).
Where("rs.product_warehouse_id = ?", productWarehouseID).
Group("rs.recording_id, rs.usage_qty").
Having(`
ABS(
COALESCE(SUM(CASE WHEN sl.decrease > 0 THEN sl.decrease ELSE 0 END), 0) -
COALESCE(SUM(CASE WHEN sl.increase > 0 THEN sl.increase ELSE 0 END), 0) -
COALESCE(rs.usage_qty, 0)
) > ?
`, qtyEpsilon).
Order("rs.recording_id ASC").
Scan(&rows).Error; err != nil {
return nil, err
}
return rows, nil
}
// buildDefaultNotes membuat notes otomatis yang berisi penjelasan drift
// beserta daftar recording_id yang berkontribusi + phantom amount masing-masing.
func buildDefaultNotes(row driftRow, drift float64, suspects []suspectRecording) string {
sign := "+"
if drift < 0 {
sign = ""
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf(
"Koreksi drift stock_log akibat bug Recording-Edit (in-place update menulis -decrease tanpa +increase). PW=%d (%s) drift=%s%.3f.",
row.ProductWarehouseID,
row.WarehouseName,
sign,
drift,
))
if len(suspects) == 0 {
return sb.String()
}
sb.WriteString(" Recordings affected:")
limit := len(suspects)
truncated := 0
if limit > maxSuspectInNotes {
truncated = limit - maxSuspectInNotes
limit = maxSuspectInNotes
}
for i := 0; i < limit; i++ {
s := suspects[i]
phantomSign := "+"
if s.Phantom < 0 {
phantomSign = ""
}
sb.WriteString(fmt.Sprintf(" #%d(%s%.0f)", s.RecordingID, phantomSign, s.Phantom))
if i < limit-1 || truncated > 0 {
sb.WriteString(",")
}
}
if truncated > 0 {
sb.WriteString(fmt.Sprintf(" ... (+%d more)", truncated))
}
sb.WriteString(".")
return sb.String()
}
func buildCorrectiveLog(row driftRow, drift float64, actorID uint, notes string) entity.StockLog {
corrective := entity.StockLog{
ProductWarehouseId: row.ProductWarehouseID,
CreatedBy: actorID,
LoggableType: string(utils.StockLogTypeAdjustment),
LoggableId: 0,
Stock: row.CurrentQty,
Notes: notes,
CreatedAt: time.Now(),
}
if drift > 0 {
corrective.Increase = drift
corrective.Decrease = 0
} else {
corrective.Increase = 0
corrective.Decrease = -drift
}
return corrective
}
func modeLabel(apply bool) string {
if apply {
return "APPLY"
}
return "DRY-RUN"
}
func nearlyEqual(a, b float64) bool {
return math.Abs(a-b) <= qtyEpsilon
}
@@ -31,7 +31,7 @@ func FlockAgeDay(originDate time.Time, periodDate time.Time) int {
if period.Before(origin) { if period.Before(origin) {
return 0 return 0
} }
return int(period.Sub(origin).Hours()/24) + 1 return int(period.Sub(origin).Hours() / 24)
} }
func DepreciationScheduleDay(originDate time.Time, periodDate time.Time, houseType string) int { func DepreciationScheduleDay(originDate time.Time, periodDate time.Time, houseType string) int {
@@ -0,0 +1,9 @@
BEGIN;
DROP INDEX IF EXISTS idx_daily_checklists_unique_non_rejected;
ALTER TABLE daily_checklists
ADD CONSTRAINT daily_checklists_date_kandang_category_key
UNIQUE (date, kandang_id, category);
COMMIT;
@@ -0,0 +1,10 @@
BEGIN;
ALTER TABLE daily_checklists
DROP CONSTRAINT IF EXISTS daily_checklists_date_kandang_category_key;
CREATE UNIQUE INDEX IF NOT EXISTS idx_daily_checklists_unique_non_rejected
ON daily_checklists (date, kandang_id, category)
WHERE (status IS NULL OR status <> 'REJECTED');
COMMIT;
@@ -0,0 +1,21 @@
UPDATE recordings r
SET day = (
SELECT (r.record_datetime::date - MIN(pc.chick_in_date)::date)::int + 1
FROM project_chickins pc
WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id
AND pc.deleted_at IS NULL
)
WHERE r.deleted_at IS NULL
AND (
SELECT MIN(pc.chick_in_date)
FROM project_chickins pc
WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id
AND pc.deleted_at IS NULL
) IS NOT NULL;
ALTER TABLE recordings
DROP CONSTRAINT IF EXISTS chk_recordings_day;
ALTER TABLE recordings
ADD CONSTRAINT chk_recordings_day
CHECK (day IS NULL OR day >= 1);
@@ -0,0 +1,21 @@
ALTER TABLE recordings
DROP CONSTRAINT IF EXISTS chk_recordings_day;
ALTER TABLE recordings
ADD CONSTRAINT chk_recordings_day
CHECK (day IS NULL OR day >= 0);
UPDATE recordings r
SET day = (
SELECT (r.record_datetime::date - MIN(pc.chick_in_date)::date)::int
FROM project_chickins pc
WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id
AND pc.deleted_at IS NULL
)
WHERE r.deleted_at IS NULL
AND (
SELECT MIN(pc.chick_in_date)
FROM project_chickins pc
WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id
AND pc.deleted_at IS NULL
) IS NOT NULL;
@@ -0,0 +1,13 @@
-- Revert chick_in_date Pullet Cikaum 1 (project_flock_kandang_id = 70) -> 23 Maret 2026
UPDATE public.project_chickins
SET chick_in_date = DATE '2026-03-23',
updated_at = NOW()
WHERE project_flock_kandang_id = 70
AND deleted_at IS NULL;
-- Revert chick_in_date Pullet Cikaum 2 (project_flock_kandang_id = 71) -> 15 Desember 2025
UPDATE public.project_chickins
SET chick_in_date = DATE '2025-12-15',
updated_at = NOW()
WHERE project_flock_kandang_id = 71
AND deleted_at IS NULL;
@@ -0,0 +1,13 @@
-- Update chick_in_date Pullet Cikaum 1 (project_flock_kandang_id = 70) -> 24 Maret 2026
UPDATE public.project_chickins
SET chick_in_date = DATE '2026-03-24',
updated_at = NOW()
WHERE project_flock_kandang_id = 70
AND deleted_at IS NULL;
-- Update chick_in_date Pullet Cikaum 2 (project_flock_kandang_id = 71) -> 6 April 2026
UPDATE public.project_chickins
SET chick_in_date = DATE '2026-04-06',
updated_at = NOW()
WHERE project_flock_kandang_id = 71
AND deleted_at IS NULL;
@@ -0,0 +1,21 @@
-- Revert: hitung ulang recording.day menggunakan chick_in_date sebelum perubahan
-- PFK 70: old chick_in_date = 2026-03-23
-- PFK 71: old chick_in_date = 2025-12-15
-- Kembalikan constraint chk_recordings_day ke >= 1
UPDATE recordings r
SET day = GREATEST(1, (r.record_datetime::date -
CASE r.project_flock_kandangs_id
WHEN 70 THEN DATE '2026-03-23'
WHEN 71 THEN DATE '2025-12-15'
END)::int + 1),
updated_at = NOW()
WHERE r.project_flock_kandangs_id IN (70, 71)
AND r.deleted_at IS NULL;
ALTER TABLE recordings
DROP CONSTRAINT IF EXISTS chk_recordings_day;
ALTER TABLE recordings
ADD CONSTRAINT chk_recordings_day
CHECK (day IS NULL OR day >= 1);
@@ -0,0 +1,23 @@
-- Normalize recording.day untuk Pullet Cikaum 1 & 2
-- Setelah migrasi 20260505083754_update_pullet_cikaum_chick_in_date mengubah chick_in_date:
-- PFK 70: 2026-03-23 → 2026-03-24 (shift +1 hari)
-- PFK 71: 2025-12-15 → 2026-04-06 (shift +112 hari)
-- Recording.day perlu dihitung ulang: day = record_datetime::date - chick_in_date::date
-- Edge case: PFK 70 punya 1 recording (2026-03-23) sebelum chick_in_date baru → di-clamp ke 0
-- Note: constraint chk_recordings_day diubah ke >= 0 karena zero-indexed day
ALTER TABLE recordings
DROP CONSTRAINT IF EXISTS chk_recordings_day;
ALTER TABLE recordings
ADD CONSTRAINT chk_recordings_day
CHECK (day IS NULL OR day >= 0);
UPDATE recordings r
SET day = GREATEST(0, (r.record_datetime::date - pc.chick_in_date::date)::int),
updated_at = NOW()
FROM project_chickins pc
WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id
AND pc.deleted_at IS NULL
AND r.deleted_at IS NULL
AND r.project_flock_kandangs_id IN (70, 71);
+1
View File
@@ -207,6 +207,7 @@ const (
P_Finances_Transaction_DeleteOne = "lti.finance.transactions.delete" P_Finances_Transaction_DeleteOne = "lti.finance.transactions.delete"
) )
const ( const (
P_ChickinsGetAll = "lti.production.chickins.list"
P_ChickinsCreateOne = "lti.production.chickins.create" P_ChickinsCreateOne = "lti.production.chickins.create"
P_ChickinsGetOne = "lti.production.chickins.detail" P_ChickinsGetOne = "lti.production.chickins.detail"
P_ChickinsApproval = "lti.production.chickins.approve" P_ChickinsApproval = "lti.production.chickins.approve"
@@ -238,17 +238,17 @@ func (u *DailyChecklistController) GetReport(c *fiber.Ctx) error {
} }
result, totalResults, err := u.DailyChecklistService.GetReport(c, query) result, totalResults, err := u.DailyChecklistService.GetReport(c, query)
withoutActivities := func(src map[string]int) map[string]int {
if src == nil {
return map[string]int{}
}
return src
}
if err != nil { if err != nil {
return err return err
} }
withoutActivities := func(src map[string]any) map[string]any {
if src == nil {
return map[string]any{}
}
return src
}
responseData := make([]dto.DailyChecklistReportDTO, len(result)) responseData := make([]dto.DailyChecklistReportDTO, len(result))
for i, item := range result { for i, item := range result {
responseData[i] = dto.DailyChecklistReportDTO{ responseData[i] = dto.DailyChecklistReportDTO{
@@ -72,13 +72,14 @@ type DailyChecklistPerformanceOverviewDTO struct {
ActivityLeft int `json:"activity_left"` ActivityLeft int `json:"activity_left"`
} }
type DailyChecklistReportDTO struct { type DailyChecklistReportDTO struct {
Area DailyChecklistReportEntityDTO `json:"area"` Area DailyChecklistReportEntityDTO `json:"area"`
Farm DailyChecklistReportEntityDTO `json:"farm"` Farm DailyChecklistReportEntityDTO `json:"farm"`
Kandang DailyChecklistReportEntityDTO `json:"kandang"` Kandang DailyChecklistReportEntityDTO `json:"kandang"`
ABK DailyChecklistReportEntityDTO `json:"abk"` ABK DailyChecklistReportEntityDTO `json:"abk"`
Phase string `json:"phase"` Phase string `json:"phase"`
DailyActivities map[string]int `json:"daily_activities"` DailyActivities map[string]any `json:"daily_activities"`
Summary DailyChecklistReportSummaryDTO `json:"summary"` Summary DailyChecklistReportSummaryDTO `json:"summary"`
} }
@@ -104,7 +104,7 @@ type DailyChecklistReportItem struct {
EmployeeID uint EmployeeID uint
EmployeeName string EmployeeName string
PhaseName string PhaseName string
DailyActivities map[string]int DailyActivities map[string]any
Summary DailyChecklistReportSummary Summary DailyChecklistReportSummary
} }
@@ -129,6 +129,7 @@ const (
dailyChecklistStatusDraft = "DRAFT" dailyChecklistStatusDraft = "DRAFT"
dailyChecklistErrEmptyKandangExist = "DailyChecklist cannot be created because empty_kandang already exists for at least one date in range" dailyChecklistErrEmptyKandangExist = "DailyChecklist cannot be created because empty_kandang already exists for at least one date in range"
dailyChecklistErrDateOverlapExist = "DailyChecklist cannot be created because at least one date in range already has a checklist" dailyChecklistErrDateOverlapExist = "DailyChecklist cannot be created because at least one date in range already has a checklist"
dailyChecklistErrDeletedNonEmptyKandangExists = "DailyChecklist cannot be created as empty_kandang because a deleted non-empty_kandang checklist exists for this date"
) )
func NewDailyChecklistService(repo repository.DailyChecklistRepository, phaseRepo phaseRepo.PhasesRepository, validate *validator.Validate, documentSvc commonSvc.DocumentService) DailyChecklistService { func NewDailyChecklistService(repo repository.DailyChecklistRepository, phaseRepo phaseRepo.PhasesRepository, validate *validator.Validate, documentSvc commonSvc.DocumentService) DailyChecklistService {
@@ -276,7 +277,11 @@ func (s dailyChecklistService) GetAll(c *fiber.Ctx, params *validation.Query) ([
normalizedSearch := re.ReplaceAllString(params.Search, "") normalizedSearch := re.ReplaceAllString(params.Search, "")
if normalizedSearch != "" { if normalizedSearch != "" {
like := "%" + normalizedSearch + "%" like := "%" + normalizedSearch + "%"
db = db.Where("(regexp_replace(k.name, '[^a-zA-Z0-9]', '', 'g') ILIKE ? OR regexp_replace(dc.category::text, '[^a-zA-Z0-9]', '', 'g') ILIKE ?)", like, like) db = db.Where(`(
regexp_replace(k.name, '[^a-zA-Z0-9]', '', 'g') ILIKE ? OR
regexp_replace(dc.category::text, '[^a-zA-Z0-9]', '', 'g') ILIKE ? OR
(dc.category = 'empty_kandang' AND regexp_replace('Kandang Kosong', '[^a-zA-Z0-9]', '', 'g') ILIKE ?)
)`, like, like, like)
} }
} }
@@ -519,21 +524,8 @@ func (s *dailyChecklistService) CreateOne(c *fiber.Ctx, req *validation.Create)
status := req.Status status := req.Status
category := req.Category category := req.Category
endDate := date
if req.EmptyKandang { if req.EmptyKandang {
if strings.TrimSpace(req.EmptyKandangEndDate) == "" {
return nil, fiber.NewError(fiber.StatusBadRequest, "empty_kandang_end_date is required when empty_kandang is true")
}
endDate, err = time.Parse(dailyChecklistDateLayout, strings.TrimSpace(req.EmptyKandangEndDate))
if err != nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "invalid empty_kandang_end_date format, use YYYY-MM-DD")
}
if endDate.Before(date) {
return nil, fiber.NewError(fiber.StatusBadRequest, "empty_kandang_end_date must be greater than or equal to date")
}
category = dailyChecklistCategoryEmptyKandang category = dailyChecklistCategoryEmptyKandang
} }
@@ -544,16 +536,18 @@ func (s *dailyChecklistService) CreateOne(c *fiber.Ctx, req *validation.Create)
return err return err
} }
if req.EmptyKandang { if category == dailyChecklistCategoryEmptyKandang {
if err := s.validateNoChecklistOverlapForEmptyKandang(tx, req.KandangId, date, endDate); err != nil { if err := s.validateNoChecklistOverlapForEmptyKandang(tx, req.KandangId, date, date); err != nil {
return err return err
} }
return s.createBulkDailyChecklists(tx, req.KandangId, date, endDate, category, status, &targetID) if err := s.validateNoDeletedNonEmptyKandangForDate(tx, req.KandangId, date); err != nil {
}
if err := s.validateNoEmptyKandangConflict(tx, req.KandangId, date, endDate); err != nil {
return err return err
} }
} else {
if err := s.validateNoEmptyKandangConflict(tx, req.KandangId, date, date); err != nil {
return err
}
}
return s.createOrReuseSingleDailyChecklist(tx, req.KandangId, date, category, status, &targetID) return s.createOrReuseSingleDailyChecklist(tx, req.KandangId, date, category, status, &targetID)
}) })
@@ -615,6 +609,22 @@ func (s *dailyChecklistService) validateNoEmptyKandangConflict(tx *gorm.DB, kand
return nil return nil
} }
func (s *dailyChecklistService) validateNoDeletedNonEmptyKandangForDate(tx *gorm.DB, kandangID uint, date time.Time) error {
var conflictCount int64
if err := tx.Model(&entity.DailyChecklist{}).
Unscoped().
Where("kandang_id = ? AND date = ? AND deleted_at IS NULL AND category != ?", kandangID, date, dailyChecklistCategoryEmptyKandang).
Count(&conflictCount).Error; err != nil {
return err
}
if conflictCount > 0 {
return fiber.NewError(fiber.StatusBadRequest, dailyChecklistErrDeletedNonEmptyKandangExists)
}
return nil
}
func (s *dailyChecklistService) createOrReuseSingleDailyChecklist(tx *gorm.DB, kandangID uint, date time.Time, category, status string, targetID *uint) error { func (s *dailyChecklistService) createOrReuseSingleDailyChecklist(tx *gorm.DB, kandangID uint, date time.Time, category, status string, targetID *uint) error {
existing := new(entity.DailyChecklist) existing := new(entity.DailyChecklist)
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}). err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
@@ -1449,11 +1459,12 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
Group("a.id, a.name, loc.id, loc.name, k.id, k.name, e.id, e.name, p.id, p.name") Group("a.id, a.name, loc.id, loc.name, k.id, k.name, e.id, e.name, p.id, p.name")
} }
var total int64 // --- Count approved rows ---
var approvedTotal int64
groupedForCount := buildGroupedQuery() groupedForCount := buildGroupedQuery()
if err := s.Repository.DB().WithContext(c.Context()). if err := s.Repository.DB().WithContext(c.Context()).
Table("(?) AS grouped", groupedForCount). Table("(?) AS grouped", groupedForCount).
Count(&total).Error; err != nil { Count(&approvedTotal).Error; err != nil {
s.Log.Errorf("Failed to count report data: %+v", err) s.Log.Errorf("Failed to count report data: %+v", err)
return nil, 0, err return nil, 0, err
} }
@@ -1473,19 +1484,246 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
TotalAssignments int64 TotalAssignments int64
} }
rows := make([]reportRow, 0) type fallbackRowType struct {
if err := buildGroupedQuery(). AreaID uint
Order("a.name, loc.name, k.name, e.name"). AreaName string
Offset(offset). LocationID uint
Limit(params.Limit). LocationName string
Scan(&rows).Error; err != nil { KandangID uint
s.Log.Errorf("Failed to fetch report data: %+v", err) KandangName string
EmployeeID uint
EmployeeName string
}
// buildFallbackQ returns employees in kandangs that have NO approved checklist data
// for the filtered period. Applies the same scope/area/location/kandang/employee filters.
buildFallbackQ := func() *gorm.DB {
approvedKandangSubQ := buildBase().Select("DISTINCT dc.kandang_id")
q := s.Repository.DB().WithContext(c.Context()).
Table("employee_kandangs ek").
Joins("JOIN employees e ON e.id = ek.employee_id AND e.deleted_at IS NULL").
Joins("JOIN kandang_groups k ON k.id = ek.kandang_id AND k.deleted_at IS NULL").
Joins("JOIN locations loc ON loc.id = k.location_id AND loc.deleted_at IS NULL").
Joins("JOIN areas a ON a.id = loc.area_id AND a.deleted_at IS NULL").
Where("ek.kandang_id NOT IN (?)", approvedKandangSubQ).
Select("e.id AS employee_id, e.name AS employee_name, k.id AS kandang_id, k.name AS kandang_name, loc.id AS location_id, loc.name AS location_name, a.id AS area_id, a.name AS area_name")
q = m.ApplyScopeFilter(q, locationScope, "loc.id")
q = m.ApplyScopeFilter(q, areaScope, "a.id")
if params.AreaID != nil {
q = q.Where("a.id = ?", *params.AreaID)
}
if params.LocationID != nil {
q = q.Where("loc.id = ?", *params.LocationID)
}
if params.KandangID != nil {
q = q.Where("ek.kandang_id = ?", *params.KandangID)
}
if params.EmployeeID != nil {
q = q.Where("ek.employee_id = ?", *params.EmployeeID)
}
// PhaseID not applied: fallback rows have no phase data
return q
}
// --- Count fallback rows ---
var fallbackTotal int64
if err := s.Repository.DB().WithContext(c.Context()).
Table("(?) AS fb", buildFallbackQ()).
Count(&fallbackTotal).Error; err != nil {
s.Log.Errorf("Failed to count fallback report data: %+v", err)
return nil, 0, err return nil, 0, err
} }
if len(rows) == 0 { total := approvedTotal + fallbackTotal
// --- Fetch ALL approved rows (pagination done in Go after merging with fallback) ---
allApprovedRows := make([]reportRow, 0)
if approvedTotal > 0 {
if err := buildGroupedQuery().
Order("a.name, loc.name, k.name, e.name").
Scan(&allApprovedRows).Error; err != nil {
s.Log.Errorf("Failed to fetch report data: %+v", err)
return nil, 0, err
}
}
// --- Fetch ALL fallback rows ---
allFallbackRows := make([]fallbackRowType, 0)
if fallbackTotal > 0 {
if err := buildFallbackQ().
Order("a.name, loc.name, k.name, e.name").
Scan(&allFallbackRows).Error; err != nil {
s.Log.Errorf("Failed to fetch fallback report data: %+v", err)
return nil, 0, err
}
}
// --- Merge approved + fallback and sort consistently ---
type mergedEntry struct {
AreaName string
LocationName string
KandangName string
EmployeeName string
IsApproved bool
Idx int
}
merged := make([]mergedEntry, 0, len(allApprovedRows)+len(allFallbackRows))
for i, r := range allApprovedRows {
merged = append(merged, mergedEntry{
AreaName: r.AreaName, LocationName: r.LocationName,
KandangName: r.KandangName, EmployeeName: r.EmployeeName,
IsApproved: true, Idx: i,
})
}
for i, r := range allFallbackRows {
merged = append(merged, mergedEntry{
AreaName: r.AreaName, LocationName: r.LocationName,
KandangName: r.KandangName, EmployeeName: r.EmployeeName,
IsApproved: false, Idx: i,
})
}
sort.Slice(merged, func(i, j int) bool {
a, b := merged[i], merged[j]
if a.AreaName != b.AreaName {
return a.AreaName < b.AreaName
}
if a.LocationName != b.LocationName {
return a.LocationName < b.LocationName
}
if a.KandangName != b.KandangName {
return a.KandangName < b.KandangName
}
return a.EmployeeName < b.EmployeeName
})
// --- Apply Go-level pagination ---
end := offset + params.Limit
if end > len(merged) {
end = len(merged)
}
if offset >= len(merged) {
return []DailyChecklistReportItem{}, total, nil return []DailyChecklistReportItem{}, total, nil
} }
pageData := merged[offset:end]
// --- Split page into approved vs fallback rows ---
pageApproved := make([]reportRow, 0)
pageFallback := make([]fallbackRowType, 0)
for _, entry := range pageData {
if entry.IsApproved {
pageApproved = append(pageApproved, allApprovedRows[entry.Idx])
} else {
pageFallback = append(pageFallback, allFallbackRows[entry.Idx])
}
}
applyEmptyKandangFlags := func(items []DailyChecklistReportItem, kandangIDs []uint) error {
if len(kandangIDs) == 0 {
return nil
}
firstDay := time.Date(params.Year, time.Month(params.Month), 1, 0, 0, 0, 0, time.UTC)
lastDay := firstDay.AddDate(0, 1, 0).AddDate(0, 0, -1)
today := time.Now().UTC().Truncate(24 * time.Hour)
type emptyKandangRec struct {
KandangID uint
Date time.Time
}
var emptyRecs []emptyKandangRec
if err := s.Repository.DB().WithContext(c.Context()).
Model(&entity.DailyChecklist{}).
Where("kandang_id IN ? AND category = ? AND date <= ? AND deleted_at IS NULL",
kandangIDs, dailyChecklistCategoryEmptyKandang, lastDay).
Select("kandang_id, date").
Scan(&emptyRecs).Error; err != nil {
s.Log.Errorf("Failed to get empty kandang records for report: %+v", err)
return err
}
emptyDaysByKandang := make(map[uint]map[int]struct{})
if len(emptyRecs) > 0 {
minEmptyDate := emptyRecs[0].Date
for _, rec := range emptyRecs[1:] {
if rec.Date.Before(minEmptyDate) {
minEmptyDate = rec.Date
}
}
type checklistDateRec struct {
KandangID uint
Date time.Time
}
var nextDates []checklistDateRec
if err := s.Repository.DB().WithContext(c.Context()).
Model(&entity.DailyChecklist{}).
Where("kandang_id IN ? AND category != ? AND date > ? AND (status IS NULL OR status != ?) AND deleted_at IS NULL",
kandangIDs, dailyChecklistCategoryEmptyKandang, minEmptyDate, dailyChecklistStatusRejected).
Select("kandang_id, date").
Order("kandang_id ASC, date ASC").
Scan(&nextDates).Error; err != nil {
s.Log.Errorf("Failed to get next checklist dates for empty kandang: %+v", err)
return err
}
nextDatesByKandang := make(map[uint][]time.Time)
for _, row := range nextDates {
nextDatesByKandang[row.KandangID] = append(nextDatesByKandang[row.KandangID], row.Date)
}
for _, rec := range emptyRecs {
var nextDate time.Time
for _, d := range nextDatesByKandang[rec.KandangID] {
if d.After(rec.Date) {
nextDate = d
break
}
}
// If no next checklist, cap empty period at today (not end of month)
ceiling := lastDay
if today.Before(lastDay) {
ceiling = today
}
periodEnd := ceiling
if !nextDate.IsZero() {
periodEnd = nextDate.AddDate(0, 0, -1)
}
effectiveStart := rec.Date
if effectiveStart.Before(firstDay) {
effectiveStart = firstDay
}
effectiveEnd := periodEnd
if effectiveEnd.After(lastDay) {
effectiveEnd = lastDay
}
if effectiveStart.After(effectiveEnd) {
continue
}
if _, ok := emptyDaysByKandang[rec.KandangID]; !ok {
emptyDaysByKandang[rec.KandangID] = make(map[int]struct{})
}
for d := effectiveStart; !d.After(effectiveEnd); d = d.AddDate(0, 0, 1) {
emptyDaysByKandang[rec.KandangID][d.Day()] = struct{}{}
}
}
}
for i, item := range items {
daySet := emptyDaysByKandang[item.KandangID]
for day := range daySet {
key := strconv.Itoa(day)
if _, exists := items[i].DailyActivities[key]; !exists {
items[i].DailyActivities[key] = "Kandang kosong"
}
}
}
return nil
}
type comboKey struct { type comboKey struct {
EmployeeID uint EmployeeID uint
@@ -1507,7 +1745,7 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
kandangSet := make(map[uint]struct{}) kandangSet := make(map[uint]struct{})
phaseSet := make(map[uint]struct{}) phaseSet := make(map[uint]struct{})
for _, row := range rows { for _, row := range pageApproved {
key := comboKey{EmployeeID: row.EmployeeID, KandangID: row.KandangID, PhaseID: row.PhaseID} key := comboKey{EmployeeID: row.EmployeeID, KandangID: row.KandangID, PhaseID: row.PhaseID}
comboSet[key] = struct{}{} comboSet[key] = struct{}{}
if _, ok := employeeSet[row.EmployeeID]; !ok { if _, ok := employeeSet[row.EmployeeID]; !ok {
@@ -1648,8 +1886,9 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
return selected return selected
} }
items := make([]DailyChecklistReportItem, len(rows)) // --- Build approved items (existing logic) ---
for i, row := range rows { approvedItems := make([]DailyChecklistReportItem, len(pageApproved))
for i, row := range pageApproved {
key := comboKey{EmployeeID: row.EmployeeID, KandangID: row.KandangID, PhaseID: row.PhaseID} key := comboKey{EmployeeID: row.EmployeeID, KandangID: row.KandangID, PhaseID: row.PhaseID}
activities := dailyActivityMap[key] activities := dailyActivityMap[key]
@@ -1659,7 +1898,7 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
totalChecklist := 0 totalChecklist := 0
categoryCounts := DailyChecklistReportCategory{} categoryCounts := DailyChecklistReportCategory{}
activityOutput := make(map[string]int, len(activities)) activityOutput := make(map[string]any, len(activities))
for day, stat := range activities { for day, stat := range activities {
activityOutput[day] = stat.Completed activityOutput[day] = stat.Completed
@@ -1696,7 +1935,7 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
kandangPercentage = int(math.Round(float64(kandangStat.Completed) / float64(kandangStat.Total) * 100)) kandangPercentage = int(math.Round(float64(kandangStat.Completed) / float64(kandangStat.Total) * 100))
} }
items[i] = DailyChecklistReportItem{ approvedItems[i] = DailyChecklistReportItem{
AreaID: row.AreaID, AreaID: row.AreaID,
AreaName: row.AreaName, AreaName: row.AreaName,
LocationID: row.LocationID, LocationID: row.LocationID,
@@ -1717,5 +1956,55 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
} }
} }
return items, total, nil // --- Build fallback items (kandangs with no approved data) ---
fallbackItems := make([]DailyChecklistReportItem, len(pageFallback))
for i, fb := range pageFallback {
fallbackItems[i] = DailyChecklistReportItem{
AreaID: fb.AreaID,
AreaName: fb.AreaName,
LocationID: fb.LocationID,
LocationName: fb.LocationName,
KandangID: fb.KandangID,
KandangName: fb.KandangName,
EmployeeID: fb.EmployeeID,
EmployeeName: fb.EmployeeName,
PhaseName: "",
DailyActivities: map[string]any{},
Summary: DailyChecklistReportSummary{},
}
}
// --- Reconstruct allItems in the sorted pageData order ---
allItems := make([]DailyChecklistReportItem, len(pageData))
approvedIdx := 0
fallbackIdx := 0
for i, entry := range pageData {
if entry.IsApproved {
allItems[i] = approvedItems[approvedIdx]
approvedIdx++
} else {
allItems[i] = fallbackItems[fallbackIdx]
fallbackIdx++
}
}
// --- Collect all kandangIDs on this page (approved + fallback) for empty_kandang flags ---
allKandangSet := make(map[uint]struct{})
for _, id := range kandangIDs {
allKandangSet[id] = struct{}{}
}
for _, fb := range pageFallback {
allKandangSet[fb.KandangID] = struct{}{}
}
allKandangIDs := make([]uint, 0, len(allKandangSet))
for id := range allKandangSet {
allKandangIDs = append(allKandangIDs, id)
}
// --- Flag empty kandang days within this report month ---
if err := applyEmptyKandangFlags(allItems, allKandangIDs); err != nil {
return nil, 0, err
}
return allItems, total, nil
} }
@@ -10,7 +10,6 @@ type Create struct {
Category string `json:"category" validate:"required"` Category string `json:"category" validate:"required"`
Status string `json:"status" validate:"required"` Status string `json:"status" validate:"required"`
EmptyKandang bool `json:"empty_kandang"` EmptyKandang bool `json:"empty_kandang"`
EmptyKandangEndDate string `json:"empty_kandang_end_date"`
} }
type Update struct { type Update struct {
@@ -119,9 +119,9 @@ func (r *DashboardRepositoryImpl) GetRecordingWeeklyMetrics(ctx context.Context,
var rows []RecordingWeeklyMetric var rows []RecordingWeeklyMetric
weekExpr := `CASE weekExpr := `CASE
WHEN r.day IS NULL OR r.day <= 0 THEN 1 WHEN r.day IS NULL OR r.day < 0 THEN 1
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
ELSE ((r.day - 1) / 7 + 1) ELSE (r.day / 7 + 1)
END` END`
db := r.DB().WithContext(ctx). db := r.DB().WithContext(ctx).
@@ -503,9 +503,9 @@ func (r *DashboardRepositoryImpl) GetComparisonWeeklyMetrics(ctx context.Context
var rows []ComparisonWeeklyMetric var rows []ComparisonWeeklyMetric
weekExpr := `CASE weekExpr := `CASE
WHEN r.day IS NULL OR r.day <= 0 THEN 1 WHEN r.day IS NULL OR r.day < 0 THEN 1
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
ELSE ((r.day - 1) / 7 + 1) ELSE (r.day / 7 + 1)
END` END`
db := r.DB().WithContext(ctx). db := r.DB().WithContext(ctx).
@@ -574,9 +574,9 @@ func (r *DashboardRepositoryImpl) GetEggQualityWeeklyMetrics(ctx context.Context
var rows []EggQualityWeeklyMetric var rows []EggQualityWeeklyMetric
weekExpr := `CASE weekExpr := `CASE
WHEN r.day IS NULL OR r.day <= 0 THEN 1 WHEN r.day IS NULL OR r.day < 0 THEN 1
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
ELSE ((r.day - 1) / 7 + 1) ELSE (r.day / 7 + 1)
END` END`
db := r.DB().WithContext(ctx). db := r.DB().WithContext(ctx).
@@ -616,9 +616,9 @@ func (r *DashboardRepositoryImpl) GetEggWeightWeeklyGrams(ctx context.Context, s
var rows []WeeklyEggWeightMetric var rows []WeeklyEggWeightMetric
weekExpr := `CASE weekExpr := `CASE
WHEN r.day IS NULL OR r.day <= 0 THEN 1 WHEN r.day IS NULL OR r.day < 0 THEN 1
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
ELSE ((r.day - 1) / 7 + 1) ELSE (r.day / 7 + 1)
END` END`
db := r.DB().WithContext(ctx). db := r.DB().WithContext(ctx).
@@ -647,9 +647,9 @@ func (r *DashboardRepositoryImpl) GetFeedUsageWeeklyByUom(ctx context.Context, s
var rows []WeeklyFeedUsageMetric var rows []WeeklyFeedUsageMetric
weekExpr := `CASE weekExpr := `CASE
WHEN r.day IS NULL OR r.day <= 0 THEN 1 WHEN r.day IS NULL OR r.day < 0 THEN 1
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
ELSE ((r.day - 1) / 7 + 1) ELSE (r.day / 7 + 1)
END` END`
db := r.DB().WithContext(ctx). db := r.DB().WithContext(ctx).
@@ -86,6 +86,7 @@ type KandangGroupDTO struct {
type DocumentDTO struct { type DocumentDTO struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
Path string `json:"path"` Path string `json:"path"`
Name string `json:"name"`
} }
// === MAPPERS === // === MAPPERS ===
@@ -184,6 +185,7 @@ func ToExpenseDetailDTO(e *entity.Expense) ExpenseDetailDTO {
documents = append(documents, DocumentDTO{ documents = append(documents, DocumentDTO{
ID: uint64(doc.Id), ID: uint64(doc.Id),
Path: doc.Path, Path: doc.Path,
Name: doc.Name,
}) })
} }
@@ -191,6 +193,7 @@ func ToExpenseDetailDTO(e *entity.Expense) ExpenseDetailDTO {
realizationDocs = append(realizationDocs, DocumentDTO{ realizationDocs = append(realizationDocs, DocumentDTO{
ID: uint64(doc.Id), ID: uint64(doc.Id),
Path: doc.Path, Path: doc.Path,
Name: doc.Name,
}) })
} }
@@ -342,6 +342,18 @@ func (s expenseService) GetOne(c *fiber.Ctx, id uint) (*expenseDto.ExpenseDetail
expense.LatestApproval = approval expense.LatestApproval = approval
responseDTO := expenseDto.ToExpenseDetailDTO(expense) responseDTO := expenseDto.ToExpenseDetailDTO(expense)
for i := range responseDTO.Documents {
if url, err := commonSvc.ResolveDocumentURL(c.Context(), s.DocumentSvc, responseDTO.Documents[i].Path, 15*time.Minute); err == nil && url != "" {
responseDTO.Documents[i].Path = url
}
}
for i := range responseDTO.RealizationDocs {
if url, err := commonSvc.ResolveDocumentURL(c.Context(), s.DocumentSvc, responseDTO.RealizationDocs[i].Path, 15*time.Minute); err == nil && url != "" {
responseDTO.RealizationDocs[i].Path = url
}
}
return &responseDTO, nil return &responseDTO, nil
} }
@@ -91,8 +91,10 @@ func (s transactionService) GetAll(c *fiber.Ctx, params *validation.Query) ([]en
LOWER(COALESCE(notes, '')) LIKE ? OR LOWER(COALESCE(notes, '')) LIKE ? OR
LOWER(COALESCE(customers.name, '')) LIKE ? OR LOWER(COALESCE(customers.name, '')) LIKE ? OR
LOWER(COALESCE(suppliers.name, '')) LIKE ? OR LOWER(COALESCE(suppliers.name, '')) LIKE ? OR
LOWER(COALESCE(banks.name, '')) LIKE ?`, LOWER(COALESCE(banks.name, '')) LIKE ? OR
like, like, like, like, like, like, like, like, CAST(payments.nominal AS TEXT) LIKE ? OR
TO_CHAR(payments.payment_date, 'YYYY-MM-DD') LIKE ?`,
like, like, like, like, like, like, like, like, like, like,
) )
} }
@@ -7,7 +7,6 @@ import (
"gitlab.com/mbugroup/lti-api.git/internal/common/repository" "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities" entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -166,42 +165,16 @@ func (r *ProductWarehouseRepositoryImpl) ApplyFlagsFilter(db *gorm.DB, flags []s
return db return db
} }
fallbackCategoryCodes := utils.LegacyProductCategoryCodesForFlags(flags) return db.
Where(`
db = db.
Joins("JOIN products p_flag ON p_flag.id = product_warehouses.product_id").
Joins("LEFT JOIN product_categories pc_flag ON pc_flag.id = p_flag.product_category_id")
actualFlagFilter := `
EXISTS ( EXISTS (
SELECT 1 SELECT 1
FROM flags f_flag FROM flags f_flag
WHERE f_flag.flagable_id = p_flag.id WHERE f_flag.flagable_id = product_warehouses.product_id
AND f_flag.flagable_type = ? AND f_flag.flagable_type = ?
AND f_flag.name IN ? AND f_flag.name IN ?
) )
` `, entity.FlagableTypeProduct, flags).
if len(fallbackCategoryCodes) == 0 {
return db.Where(actualFlagFilter, entity.FlagableTypeProduct, flags).Distinct()
}
return db.
Where(
`(`+actualFlagFilter+`) OR (
NOT EXISTS (
SELECT 1
FROM flags f_any
WHERE f_any.flagable_id = p_flag.id
AND f_any.flagable_type = ?
)
AND pc_flag.code IN ?
)`,
entity.FlagableTypeProduct,
flags,
entity.FlagableTypeProduct,
fallbackCategoryCodes,
).
Distinct() Distinct()
} }
@@ -117,7 +117,7 @@ func insertProductWarehouseTestFixtures(t *testing.T, db *gorm.DB) {
} }
} }
func TestApplyFlagsFilterIncludesLegacyCategoryFallback(t *testing.T) { func TestApplyFlagsFilterOnlyIncludesFlaggedProducts(t *testing.T) {
db := setupProductWarehouseFlagFilterTestDB(t) db := setupProductWarehouseFlagFilterTestDB(t)
repo := NewProductWarehouseRepository(db) repo := NewProductWarehouseRepository(db)
ctx := context.Background() ctx := context.Background()
@@ -131,12 +131,14 @@ func TestApplyFlagsFilterIncludesLegacyCategoryFallback(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if len(ids) != 2 || ids[0] != 1 || ids[1] != 2 { // Only PW 1 (product 10, flagged PAKAN) should match.
t.Fatalf("expected flagged and legacy RAW rows to match, got %v", ids) // PW 2 (product 20, no flags, RAW category) must not appear — legacy fallback removed.
if len(ids) != 1 || ids[0] != 1 {
t.Fatalf("expected only flagged row to match, got %v", ids)
} }
} }
func TestApplyFlagsFilterDoesNotFallbackWhenProductAlreadyHasDifferentFlags(t *testing.T) { func TestApplyFlagsFilterExcludesWrongFlaggedProducts(t *testing.T) {
db := setupProductWarehouseFlagFilterTestDB(t) db := setupProductWarehouseFlagFilterTestDB(t)
repo := NewProductWarehouseRepository(db) repo := NewProductWarehouseRepository(db)
ctx := context.Background() ctx := context.Background()
@@ -150,8 +152,9 @@ func TestApplyFlagsFilterDoesNotFallbackWhenProductAlreadyHasDifferentFlags(t *t
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
// PW 3 belongs to an OVK-flagged product — must not appear when filtering for PAKAN.
if len(ids) != 0 { if len(ids) != 0 {
t.Fatalf("expected OVK-flagged product not to match PAKAN fallback, got %v", ids) t.Fatalf("expected OVK-flagged product not to match PAKAN filter, got %v", ids)
} }
} }
@@ -56,6 +56,12 @@ func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, "Invalid product_ids") return fiber.NewError(fiber.StatusBadRequest, "Invalid product_ids")
} }
sortBy := strings.TrimSpace(c.Query("sort_by", ""))
sortOrder := strings.TrimSpace(c.Query("sort_order", ""))
if sortOrder == "" {
sortOrder = "asc"
}
query := &validation.DeliveryOrderQuery{ query := &validation.DeliveryOrderQuery{
Page: c.QueryInt("page", 1), Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10), Limit: c.QueryInt("limit", 10),
@@ -66,6 +72,8 @@ func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
MarketingId: uint(c.QueryInt("marketing_id", 0)), MarketingId: uint(c.QueryInt("marketing_id", 0)),
ProjectFlockID: uint(c.QueryInt("project_flock_id", 0)), ProjectFlockID: uint(c.QueryInt("project_flock_id", 0)),
ProjectFlockKandangID: uint(c.QueryInt("project_flock_kandang_id", 0)), ProjectFlockKandangID: uint(c.QueryInt("project_flock_kandang_id", 0)),
SortBy: sortBy,
SortOrder: sortOrder,
} }
if isAllExcelExportRequest(c) { if isAllExcelExportRequest(c) {
@@ -292,7 +292,27 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
if params.MarketingId != 0 { if params.MarketingId != 0 {
return db.Where("id = ?", params.MarketingId) return db.Where("id = ?", params.MarketingId)
} }
orderDir := "DESC"
if params.SortOrder != "" {
orderDir = strings.ToUpper(params.SortOrder)
}
switch strings.TrimSpace(params.SortBy) {
case "so_number":
return db.Order("marketings.so_number " + orderDir)
case "so_date":
return db.Order("marketings.so_date " + orderDir)
case "status":
statusSQL := "(SELECT step_name FROM approvals WHERE approvable_type = '" + utils.ApprovalWorkflowMarketing.String() + "' AND approvable_id = marketings.id ORDER BY action_at DESC, id DESC LIMIT 1) " + orderDir
return db.Order(statusSQL)
case "customer":
return db.Joins("LEFT JOIN customers ON customers.id = marketings.customer_id").Order("COALESCE(customers.name, '') " + orderDir)
case "grand_total":
return db.Order("(SELECT COALESCE(SUM(mp.total_price), 0) FROM marketing_products mp WHERE mp.marketing_id = marketings.id) " + orderDir)
default:
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}
}) })
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
@@ -31,6 +31,8 @@ type DeliveryOrderQuery struct {
MarketingId uint `query:"marketing_id" validate:"omitempty,gt=0"` MarketingId uint `query:"marketing_id" validate:"omitempty,gt=0"`
ProjectFlockID uint `query:"project_flock_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"` ProjectFlockKandangID uint `query:"project_flock_kandang_id" validate:"omitempty,gt=0"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=so_number so_date status customer grand_total"`
SortOrder string `query:"sort_order" validate:"omitempty,oneof=asc desc"`
} }
type DeliveryOrderApprove struct { type DeliveryOrderApprove struct {
@@ -27,6 +27,8 @@ func (u *EmployeesController) GetAll(c *fiber.Ctx) error {
Page: c.QueryInt("page", 1), Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10), Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""), Search: c.Query("search", ""),
OrderBy: c.Query("order_by", "desc"),
SortBy: c.Query("sort_by", "updated_at"),
} }
if query.Page < 1 || query.Limit < 1 { if query.Page < 1 || query.Limit < 1 {
@@ -2,6 +2,7 @@ package service
import ( import (
"errors" "errors"
"fmt"
"strings" "strings"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities" entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
@@ -126,11 +127,18 @@ func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
if params.IsActive != nil { if params.IsActive != nil {
db = db.Where("employees.is_active = ?", *params.IsActive) db = db.Where("employees.is_active = ?", *params.IsActive)
} }
return db.
db = db.
Select("employees.id, employees.name, employees.is_active, employees.created_at, employees.updated_at"). Select("employees.id, employees.name, employees.is_active, employees.created_at, employees.updated_at").
Group("employees.id, employees.name, employees.is_active, employees.created_at, employees.updated_at"). Group("employees.id, employees.name, employees.is_active, employees.created_at, employees.updated_at")
Order("employees.created_at DESC").
Order("employees.updated_at DESC") if params.OrderBy == "desc" || params.OrderBy == "" {
db = db.Order(fmt.Sprintf("employees.%s DESC", params.SortBy))
} else {
db = db.Order(fmt.Sprintf("employees.%s ASC", params.SortBy))
}
return db
}) })
if err != nil { if err != nil {
@@ -18,4 +18,6 @@ type Query struct {
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
KandangId *uint `query:"kandang_id" validate:"omitempty"` KandangId *uint `query:"kandang_id" validate:"omitempty"`
IsActive *bool `query:"is_active" validate:"omitempty"` IsActive *bool `query:"is_active" validate:"omitempty"`
SortBy string `query:"sort_by" validate:"omitempty,max=50,oneof=name created_at updated_at" default:"updated_at"`
OrderBy string `query:"order_by" validate:"omitempty,oneof=asc desc" default:"desc"`
} }
@@ -29,6 +29,8 @@ func (u *KandangGroupController) GetAll(c *fiber.Ctx) error {
Search: c.Query("search", ""), Search: c.Query("search", ""),
LocationId: c.QueryInt("location_id", 0), LocationId: c.QueryInt("location_id", 0),
PicId: c.QueryInt("pic_id", 0), PicId: c.QueryInt("pic_id", 0),
OrderBy: c.Query("order_by", "desc"),
SortBy: c.Query("sort_by", "updated_at"),
} }
if query.Page < 1 || query.Limit < 1 { if query.Page < 1 || query.Limit < 1 {
@@ -70,7 +70,14 @@ func (s kandangGroupService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
if params.PicId != 0 { if params.PicId != 0 {
db = db.Where("kandang_groups.pic_id = ?", params.PicId) db = db.Where("kandang_groups.pic_id = ?", params.PicId)
} }
return db.Order("kandang_groups.created_at DESC").Order("kandang_groups.updated_at DESC")
if params.OrderBy == "desc" || params.OrderBy == "" {
db = db.Order(fmt.Sprintf("kandang_groups.%s DESC", params.SortBy))
} else {
db = db.Order(fmt.Sprintf("kandang_groups.%s ASC", params.SortBy))
}
return db
}) })
if scopeErr != nil { if scopeErr != nil {
@@ -20,4 +20,6 @@ type Query struct {
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"` LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
PicId int `query:"pic_id" validate:"omitempty,number,gt=0"` PicId int `query:"pic_id" validate:"omitempty,number,gt=0"`
SortBy string `query:"sort_by" validate:"omitempty,max=50,oneof=name created_at updated_at" default:"updated_at"`
OrderBy string `query:"order_by" validate:"omitempty,oneof=asc desc" default:"desc"`
} }
@@ -29,6 +29,8 @@ func (u *KandangController) GetAll(c *fiber.Ctx) error {
Search: c.Query("search", ""), Search: c.Query("search", ""),
LocationId: c.QueryInt("location_id", 0), LocationId: c.QueryInt("location_id", 0),
PicId: c.QueryInt("pic_id", 0), PicId: c.QueryInt("pic_id", 0),
OrderBy: c.Query("order_by", "desc"),
SortBy: c.Query("sort_by", "created_at"),
} }
if query.Page < 1 || query.Limit < 1 { if query.Page < 1 || query.Limit < 1 {
@@ -66,7 +66,14 @@ func (s kandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
if params.PicId != 0 { if params.PicId != 0 {
db = db.Where("pic_id = ?", params.PicId) db = db.Where("pic_id = ?", params.PicId)
} }
return db.Order("created_at DESC").Order("updated_at DESC")
if params.OrderBy == "desc" || params.OrderBy == "" {
db = db.Order(fmt.Sprintf("%s DESC", params.SortBy))
} else {
db = db.Order(fmt.Sprintf("%s ASC", params.SortBy))
}
return db
}) })
if scopeErr != nil { if scopeErr != nil {
@@ -26,4 +26,6 @@ type Query struct {
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"` LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
PicId int `query:"pic_id" validate:"omitempty,number,gt=0"` PicId int `query:"pic_id" validate:"omitempty,number,gt=0"`
SortBy string `query:"sort_by" validate:"omitempty,max=50,oneof=name created_at updated_at" default:"updated_at"`
OrderBy string `query:"order_by" validate:"omitempty,oneof=asc desc" default:"desc"`
} }
@@ -1,6 +1,7 @@
package controller package controller
import ( import (
"math"
"strconv" "strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/dto" "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/dto"
@@ -21,32 +22,32 @@ func NewChickinController(chickinService service.ChickinService) *ChickinControl
} }
} }
// func (u *ChickinController) GetAll(c *fiber.Ctx) error { func (u *ChickinController) GetAll(c *fiber.Ctx) error {
// query := &validation.Query{ query := &validation.Query{
// Page: c.QueryInt("page", 1), Page: c.QueryInt("page", 1),
// Limit: c.QueryInt("limit", 10), Limit: c.QueryInt("limit", 10),
// ProjectFlockKandangId: uint(c.QueryInt("project_flock_kandang_id", 0)), ProjectFlockKandangId: uint(c.QueryInt("project_flock_kandang_id", 0)),
// } }
// result, totalResults, err := u.ChickinService.GetAll(c, query) result, totalResults, err := u.ChickinService.GetAll(c, query)
// if err != nil { if err != nil {
// return err return err
// } }
// return c.Status(fiber.StatusOK). return c.Status(fiber.StatusOK).
// JSON(response.SuccessWithPaginate[dto.ChickinListDTO]{ JSON(response.SuccessWithPaginate[dto.ChickinListDTO]{
// Code: fiber.StatusOK, Code: fiber.StatusOK,
// Status: "success", Status: "success",
// Message: "Get all chickins successfully", Message: "Get all chickins successfully",
// Meta: response.Meta{ Meta: response.Meta{
// Page: query.Page, Page: query.Page,
// Limit: query.Limit, Limit: query.Limit,
// TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))), TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
// TotalResults: totalResults, TotalResults: totalResults,
// }, },
// Data: dto.ToChickinListDTOs(result), Data: dto.ToChickinListDTOs(result),
// }) })
// } }
// func (u *ChickinController) GetOne(c *fiber.Ctx) error { // func (u *ChickinController) GetOne(c *fiber.Ctx) error {
// param := c.Params("id") // param := c.Params("id")
@@ -15,7 +15,7 @@ func ChickinRoutes(v1 fiber.Router, u user.UserService, s chickin.ChickinService
route := v1.Group("/chickins") route := v1.Group("/chickins")
route.Use(m.Auth(u)) route.Use(m.Auth(u))
// route.Get("/", ctrl.GetAll) route.Get("/", m.RequirePermissions(m.P_ChickinsGetAll), ctrl.GetAll)
route.Post("/", m.RequirePermissions(m.P_ChickinsCreateOne), ctrl.CreateOne) route.Post("/", m.RequirePermissions(m.P_ChickinsCreateOne), ctrl.CreateOne)
route.Get("/:id",m.RequirePermissions(m.P_ChickinsGetOne), ctrl.GetOne) route.Get("/:id",m.RequirePermissions(m.P_ChickinsGetOne), ctrl.GetOne)
// route.Patch("/:id", ctrl.UpdateOne) // route.Patch("/:id", ctrl.UpdateOne)
@@ -77,6 +77,10 @@ func setRecordingExportColumns(file *excelize.File, sheet string) error {
"Z": 22, "Z": 22,
"AA": 16, "AA": 16,
"AB": 18, "AB": 18,
"AC": 24,
"AD": 18,
"AE": 18,
"AF": 18,
} }
for col, width := range columnWidths { for col, width := range columnWidths {
@@ -96,7 +100,7 @@ func setRecordingExportColumns(file *excelize.File, sheet string) error {
} }
func setRecordingExportHeaders(file *excelize.File, sheet string) error { func setRecordingExportHeaders(file *excelize.File, sheet string) error {
verticalHeaderCols := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "Y", "Z", "AA", "AB"} verticalHeaderCols := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF"}
for _, col := range verticalHeaderCols { for _, col := range verticalHeaderCols {
if err := file.MergeCell(sheet, col+"1", col+"2"); err != nil { if err := file.MergeCell(sheet, col+"1", col+"2"); err != nil {
return err return err
@@ -117,6 +121,10 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
"Z1": "Catatan Approval", "Z1": "Catatan Approval",
"AA1": "Dibuat Oleh", "AA1": "Dibuat Oleh",
"AB1": "Tanggal Submit", "AB1": "Tanggal Submit",
"AC1": "Nama Pakan",
"AD1": "Jumlah Input Pakan",
"AE1": "Jumlah Penggunaan",
"AF1": "Pending Qty",
} }
for cell, value := range headerValues { for cell, value := range headerValues {
if err := file.SetCellValue(sheet, cell, value); err != nil { if err := file.SetCellValue(sheet, cell, value); err != nil {
@@ -230,7 +238,7 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
return err return err
} }
return file.SetCellStyle(sheet, "A1", "AB2", headerStyle) return file.SetCellStyle(sheet, "A1", "AF2", headerStyle)
} }
func setRecordingExportRows(file *excelize.File, sheet string, items []dto.RecordingListDTO) error { func setRecordingExportRows(file *excelize.File, sheet string, items []dto.RecordingListDTO) error {
@@ -241,6 +249,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
columns := []string{ columns := []string{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "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", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB",
"AC", "AD", "AE", "AF",
} }
for i, item := range items { for i, item := range items {
@@ -283,6 +292,29 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
createdBy = safeExportText(item.Approval.ActionBy.Name) createdBy = safeExportText(item.Approval.ActionBy.Name)
} }
// Build feed usage columns — concatenate multiple feeds with newline
feedNames := make([]string, 0, len(item.FeedUsage))
usageAmounts := make([]string, 0, len(item.FeedUsage))
pendingQtys := make([]string, 0, len(item.FeedUsage))
inputQtys := make([]string, 0, len(item.FeedUsage))
for _, fu := range item.FeedUsage {
feedNames = append(feedNames, safeExportText(fu.ProductName))
usageAmounts = append(usageAmounts, formatNumberID(fu.UsageAmount, 2, true))
pendingQtys = append(pendingQtys, formatNumberID(fu.PendingQty, 2, true))
inputQtys = append(inputQtys, formatNumberID(fu.UsageAmount+fu.PendingQty, 2, true))
}
feedNameCol := "-"
usageCol := "-"
pendingCol := "-"
inputCol := "-"
if len(feedNames) > 0 {
feedNameCol = strings.Join(feedNames, "\n")
usageCol = strings.Join(usageAmounts, "\n")
pendingCol = strings.Join(pendingQtys, "\n")
inputCol = strings.Join(inputQtys, "\n")
}
rowValues := []interface{}{ rowValues := []interface{}{
i + 1, i + 1,
locationName, locationName,
@@ -312,6 +344,10 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
safeExportText(pointerString(item.Approval.Notes)), safeExportText(pointerString(item.Approval.Notes)),
createdBy, createdBy,
formatDateIndonesian(item.CreatedAt), formatDateIndonesian(item.CreatedAt),
feedNameCol, // AC
inputCol, // AD - Jumlah Input Pakan
usageCol, // AE - Jumlah Penggunaan
pendingCol, // AF - Pending Qty
} }
for idx, col := range columns { for idx, col := range columns {
@@ -339,7 +375,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
if err != nil { if err != nil {
return err return err
} }
if err := file.SetCellStyle(sheet, "A3", fmt.Sprintf("AB%d", lastRow), dataCenterStyle); err != nil { if err := file.SetCellStyle(sheet, "A3", fmt.Sprintf("AF%d", lastRow), dataCenterStyle); err != nil {
return err return err
} }
@@ -360,7 +396,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
return err return err
} }
leftColumns := []string{"B", "C", "D", "F", "G", "H", "Y", "Z", "AA", "AB"} leftColumns := []string{"B", "C", "D", "F", "G", "H", "Y", "Z", "AA", "AB", "AC"}
for _, col := range leftColumns { for _, col := range leftColumns {
if err := file.SetCellStyle(sheet, col+"3", fmt.Sprintf("%s%d", col, lastRow), dataLeftStyle); err != nil { if err := file.SetCellStyle(sheet, col+"3", fmt.Sprintf("%s%d", col, lastRow), dataLeftStyle); err != nil {
return err return err
@@ -92,6 +92,12 @@ type RecordingRelationDTO struct {
Approval approvalDTO.ApprovalRelationDTO `json:"approval"` Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
} }
type RecordingFeedUsageDTO struct {
ProductName string `json:"product_name"`
UsageAmount float64 `json:"usage_amount"`
PendingQty float64 `json:"pending_qty"`
}
type RecordingListDTO struct { type RecordingListDTO struct {
RecordingRelationDTO RecordingRelationDTO
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"` CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
@@ -99,6 +105,7 @@ type RecordingListDTO struct {
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
Kandang *RecordingKandangDTO `json:"kandang,omitempty"` Kandang *RecordingKandangDTO `json:"kandang,omitempty"`
Location *RecordingLocationDTO `json:"location,omitempty"` Location *RecordingLocationDTO `json:"location,omitempty"`
FeedUsage []RecordingFeedUsageDTO `json:"feed_usage,omitempty"`
} }
type RecordingDetailDTO struct { type RecordingDetailDTO struct {
@@ -192,6 +199,36 @@ func ToRecordingStockDTOs(stocks []entity.RecordingStock) []RecordingStockDTO {
return result return result
} }
func ToRecordingFeedUsageDTOs(stocks []entity.RecordingStock) []RecordingFeedUsageDTO {
return toRecordingFeedUsageDTOs(stocks)
}
func toRecordingFeedUsageDTOs(stocks []entity.RecordingStock) []RecordingFeedUsageDTO {
result := make([]RecordingFeedUsageDTO, 0, len(stocks))
for _, s := range stocks {
productName := ""
if s.ProductWarehouse.Product.Id != 0 {
productName = s.ProductWarehouse.Product.Name
}
var usageAmount float64
if s.UsageQty != nil {
usageAmount = *s.UsageQty
}
var pendingQty float64
if s.PendingQty != nil {
pendingQty = *s.PendingQty
}
result = append(result, RecordingFeedUsageDTO{
ProductName: productName,
UsageAmount: usageAmount,
PendingQty: pendingQty,
})
}
return result
}
func ToRecordingEggDTOs(eggs []entity.RecordingEgg) []RecordingEggDTO { func ToRecordingEggDTOs(eggs []entity.RecordingEgg) []RecordingEggDTO {
result := make([]RecordingEggDTO, len(eggs)) result := make([]RecordingEggDTO, len(eggs))
for i, egg := range eggs { for i, egg := range eggs {
@@ -222,6 +259,7 @@ func toRecordingListDTO(e entity.Recording) RecordingListDTO {
CreatedUser: createdUser, CreatedUser: createdUser,
Kandang: recordingKandangDTO(e), Kandang: recordingKandangDTO(e),
Location: recordingKandangLocationDTO(e), Location: recordingKandangLocationDTO(e),
FeedUsage: toRecordingFeedUsageDTOs(e.Stocks),
} }
} }
@@ -49,6 +49,7 @@ type RecordingRepository interface {
DeleteEggs(tx *gorm.DB, recordingID uint) error DeleteEggs(tx *gorm.DB, recordingID uint) error
ListEggs(tx *gorm.DB, recordingID uint) ([]entity.RecordingEgg, error) ListEggs(tx *gorm.DB, recordingID uint) ([]entity.RecordingEgg, error)
UpdateEggTotalQty(tx *gorm.DB, eggID uint, totalQty float64) error 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) GetRecordingEggByID(ctx context.Context, id uint, modifier func(*gorm.DB) *gorm.DB) (*entity.RecordingEgg, error)
ExistsOnDate(ctx context.Context, projectFlockKandangId uint, recordTime time.Time) (bool, error) ExistsOnDate(ctx context.Context, projectFlockKandangId uint, recordTime time.Time) (bool, error)
@@ -146,7 +147,10 @@ func (r *RecordingRepositoryImpl) WithRelationsList(db *gorm.DB) *gorm.DB {
Preload("ProjectFlockKandang.Kandang"). Preload("ProjectFlockKandang.Kandang").
Preload("ProjectFlockKandang.Kandang.Location"). Preload("ProjectFlockKandang.Kandang.Location").
Preload("ProjectFlockKandang.ProjectFlock"). Preload("ProjectFlockKandang.ProjectFlock").
Preload("ProjectFlockKandang.ProjectFlock.ProductionStandard") Preload("ProjectFlockKandang.ProjectFlock.ProductionStandard").
Preload("Stocks").
Preload("Stocks.ProductWarehouse").
Preload("Stocks.ProductWarehouse.Product")
} }
func (r *RecordingRepositoryImpl) latestApprovalSubQuery(db *gorm.DB) *gorm.DB { func (r *RecordingRepositoryImpl) latestApprovalSubQuery(db *gorm.DB) *gorm.DB {
@@ -543,6 +547,12 @@ func (r *RecordingRepositoryImpl) UpdateEggTotalQty(tx *gorm.DB, eggID uint, tot
Update("total_qty", totalQty).Error Update("total_qty", totalQty).Error
} }
func (r *RecordingRepositoryImpl) UpdateEggWeight(tx *gorm.DB, eggID uint, weight *float64) error {
return tx.Model(&entity.RecordingEgg{}).
Where("id = ?", eggID).
Update("weight", weight).Error
}
func (r *RecordingRepositoryImpl) GetRecordingEggByID( func (r *RecordingRepositoryImpl) GetRecordingEggByID(
ctx context.Context, ctx context.Context,
id uint, id uint,
@@ -173,6 +173,37 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
return nil, 0, err return nil, 0, err
} }
// Pre-fetch transfer maps by category to avoid N+1 per-recording queries.
growingPFKIDs := make([]uint, 0, len(pfkIDs))
layingPFKIDs := make([]uint, 0, len(pfkIDs))
seenCat := make(map[uint]bool, len(pfkIDs))
for i := range recordings {
pfkID := recordings[i].ProjectFlockKandangId
if pfkID == 0 || seenCat[pfkID] {
continue
}
seenCat[pfkID] = true
cat := ""
if recordings[i].ProjectFlockKandang != nil && recordings[i].ProjectFlockKandang.ProjectFlock.Id != 0 {
cat = strings.ToUpper(strings.TrimSpace(recordings[i].ProjectFlockKandang.ProjectFlock.Category))
}
switch cat {
case string(utils.ProjectFlockCategoryGrowing):
growingPFKIDs = append(growingPFKIDs, pfkID)
case string(utils.ProjectFlockCategoryLaying):
layingPFKIDs = append(layingPFKIDs, pfkID)
}
}
sourceTransferByPFK, err := s.TransferLayingRepo.GetLatestApprovedBySourceKandangs(c.Context(), growingPFKIDs)
if err != nil {
return nil, 0, err
}
targetTransferByPFK, err := s.TransferLayingRepo.GetLatestApprovedByTargetKandangs(c.Context(), layingPFKIDs)
if err != nil {
return nil, 0, err
}
hasTargetRecordingCache := make(map[uint]bool)
cutOverChickinAvailability := make(map[uint]bool) cutOverChickinAvailability := make(map[uint]bool)
for i := range recordings { for i := range recordings {
if recordings[i].ProjectFlockKandangId != 0 && !recordings[i].RecordDatetime.IsZero() { if recordings[i].ProjectFlockKandangId != 0 && !recordings[i].RecordDatetime.IsZero() {
@@ -192,7 +223,7 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
rate := recordingutil.ComputeDepletionRate(prev, current, totalChick) rate := recordingutil.ComputeDepletionRate(prev, current, totalChick)
recordings[i].DepletionRate = &rate recordings[i].DepletionRate = &rate
populationCanChange, transferExecuted, isTransition, isLaying, _, _, stateErr := s.evaluatePopulationMutationState(c.Context(), &recordings[i]) populationCanChange, transferExecuted, isTransition, isLaying, _, _, stateErr := s.evaluatePopulationMutationStateFromCaches(c.Context(), &recordings[i], sourceTransferByPFK, targetTransferByPFK, hasTargetRecordingCache)
if stateErr != nil { if stateErr != nil {
return nil, 0, stateErr return nil, 0, stateErr
} }
@@ -768,6 +799,12 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
match := recordingutil.EggTotalsEqual(existingTotals, incomingTotals) match := recordingutil.EggTotalsEqual(existingTotals, incomingTotals)
if match { if match {
hasEggChanges = false hasEggChanges = false
} else if recordingutil.EggQtyByWarehouseEqual(existingTotals, incomingTotals) {
// Weight-only change: update weight fields directly without touching FIFO
if err := s.updateEggWeightsOnly(tx, existingEggs, req.Eggs); err != nil {
return err
}
// hasEggChanges stays true so metrics are recomputed
} else { } else {
category := "" category := ""
if recordingEntity.ProjectFlockKandang != nil && recordingEntity.ProjectFlockKandang.ProjectFlock.Id != 0 { if recordingEntity.ProjectFlockKandang != nil && recordingEntity.ProjectFlockKandang.ProjectFlock.Id != 0 {
@@ -785,7 +822,7 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
if err := s.ensureProductWarehousesByFlags(ctx, eggIDs, []string{"TELUR-UTUH", "TELUR-PECAH", "TELUR-PUTIH", "TELUR-RETAK", "TELUR"}, "egg"); err != nil { if err := s.ensureProductWarehousesByFlags(ctx, eggIDs, []string{"TELUR-UTUH", "TELUR-PECAH", "TELUR-PUTIH", "TELUR-RETAK", "TELUR"}, "egg"); err != nil {
return err return err
} }
if err := ensureRecordingEggsUnused(existingEggs); err != nil { if err := ensureRecordingEggQtyChangeSafe(existingEggs, req.Eggs); err != nil {
return err return err
} }
if err := s.logRecordingEggUsage(ctx, tx, existingEggs, note, actorID); err != nil { if err := s.logRecordingEggUsage(ctx, tx, existingEggs, note, actorID); err != nil {
@@ -1308,6 +1345,82 @@ func (s *recordingService) evaluatePopulationMutationState(ctx context.Context,
return populationCanChange, transferExecuted, isTransition, isLaying, transfer, transferDate, nil return populationCanChange, transferExecuted, isTransition, isLaying, transfer, transferDate, nil
} }
// evaluatePopulationMutationStateFromCaches is identical to evaluatePopulationMutationState
// but uses pre-fetched transfer maps to avoid N+1 queries in list endpoints.
func (s *recordingService) evaluatePopulationMutationStateFromCaches(
ctx context.Context,
recording *entity.Recording,
sourceTransferByPFK map[uint]*entity.LayingTransfer,
targetTransferByPFK map[uint]*entity.LayingTransfer,
hasTargetRecordingCache map[uint]bool,
) (bool, bool, bool, bool, *entity.LayingTransfer, time.Time, error) {
if recording == nil || recording.ProjectFlockKandangId == 0 || s.TransferLayingRepo == nil {
return true, false, false, false, nil, time.Time{}, nil
}
category, err := s.resolveRecordingCategory(ctx, recording)
if err != nil {
s.Log.Errorf("Failed to resolve recording category for population mutation check (recording=%d): %+v", recording.Id, err)
return true, false, false, false, nil, time.Time{}, fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi perubahan populasi recording")
}
var transfer *entity.LayingTransfer
switch category {
case strings.ToUpper(string(utils.ProjectFlockCategoryGrowing)):
transfer = sourceTransferByPFK[recording.ProjectFlockKandangId]
case strings.ToUpper(string(utils.ProjectFlockCategoryLaying)):
transfer = targetTransferByPFK[recording.ProjectFlockKandangId]
default:
return true, false, false, false, nil, time.Time{}, nil
}
if transfer == nil {
return true, false, false, false, nil, time.Time{}, nil
}
transferDate := transferPhysicalMoveDate(transfer)
if transferDate.IsZero() {
return true, false, false, false, transfer, transferDate, nil
}
transferExecuted := transfer.ExecutedAt != nil && !transfer.ExecutedAt.IsZero()
recordDate := normalizeDateOnlyUTC(recording.RecordDatetime)
_, economicCutoffDate := transferRecordingWindow(transfer)
isTransition := !recordDate.Before(transferDate) && recordDate.Before(economicCutoffDate)
isLaying := !recordDate.Before(economicCutoffDate)
populationCanChange := true
if category == strings.ToUpper(string(utils.ProjectFlockCategoryGrowing)) {
populationCanChange = !(transferExecuted && !recordDate.Before(transferDate))
if transferExecuted && !recordDate.Before(transferDate) {
var hasTargetLayingRecording bool
if cached, ok := hasTargetRecordingCache[transfer.Id]; ok {
hasTargetLayingRecording = cached
} else {
hasTargetLayingRecording, err = s.hasAnyRecordingOnTransferTargets(ctx, transfer)
if err != nil {
s.Log.Errorf("Failed to resolve target laying recording state for transfer %d: %+v", transfer.Id, err)
return true, false, false, false, nil, time.Time{}, fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi status transisi recording")
}
hasTargetRecordingCache[transfer.Id] = hasTargetLayingRecording
}
if hasTargetLayingRecording {
isTransition = false
isLaying = true
} else {
today := normalizeDateOnlyUTC(time.Now().UTC())
if !today.Before(economicCutoffDate) {
isTransition = true
isLaying = false
}
}
}
}
return populationCanChange, transferExecuted, isTransition, isLaying, transfer, transferDate, nil
}
func (s *recordingService) hasAnyRecordingOnTransferTargets(ctx context.Context, transfer *entity.LayingTransfer) (bool, error) { func (s *recordingService) hasAnyRecordingOnTransferTargets(ctx context.Context, transfer *entity.LayingTransfer) (bool, error) {
if transfer == nil || transfer.Id == 0 { if transfer == nil || transfer.Id == 0 {
return false, nil return false, nil
@@ -2347,7 +2460,7 @@ func (s *recordingService) computeRecordingDay(ctx context.Context, projectFlock
return 0, fiber.NewError(fiber.StatusBadRequest, "Record date tidak boleh sebelum tanggal chick in") return 0, fiber.NewError(fiber.StatusBadRequest, "Record date tidak boleh sebelum tanggal chick in")
} }
return diff + 1, nil return diff, nil
} }
func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm.DB, recording *entity.Recording) error { func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm.DB, recording *entity.Recording) error {
@@ -2508,8 +2621,8 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm
if isGrowing { if isGrowing {
week := 0 week := 0
if recording.Day != nil && *recording.Day > 0 { if recording.Day != nil && *recording.Day >= 0 {
week = (*recording.Day-1)/7 + 1 week = *recording.Day/7 + 1
} }
if week > 0 && s.Repository != nil { if week > 0 && s.Repository != nil {
meanBw, ok, err := s.Repository.GetUniformityMeanBwByWeek(tx, recording.ProjectFlockKandangId, week) meanBw, ok, err := s.Repository.GetUniformityMeanBwByWeek(tx, recording.ProjectFlockKandangId, week)
@@ -3008,6 +3121,12 @@ func (s *recordingService) reflowSyncRecordingStocks(
existingByWarehouse[stock.ProductWarehouseId] = append(existingByWarehouse[stock.ProductWarehouseId], stock) existingByWarehouse[stock.ProductWarehouseId] = append(existingByWarehouse[stock.ProductWarehouseId], stock)
} }
shouldWriteLog := shouldWriteRecordingStockLog(note, actorID)
if shouldWriteLog && s.StockLogRepo == nil {
return errors.New("stock log repository is not available")
}
resetLogState := newRecordingStockLogState()
stocksToApply := make([]entity.RecordingStock, 0, len(incoming)) stocksToApply := make([]entity.RecordingStock, 0, len(incoming))
for _, item := range incoming { for _, item := range incoming {
list := existingByWarehouse[item.ProductWarehouseId] list := existingByWarehouse[item.ProductWarehouseId]
@@ -3015,6 +3134,25 @@ func (s *recordingService) reflowSyncRecordingStocks(
if len(list) > 0 { if len(list) > 0 {
stock = list[0] stock = list[0]
existingByWarehouse[item.ProductWarehouseId] = list[1:] existingByWarehouse[item.ProductWarehouseId] = list[1:]
// Write reset (increase) stock_log for the OLD consumption BEFORE overwriting UsageQty.
// FIFO internally does Rollback+Reallocate inside reflowApplyRecordingStocks, but the
// corresponding +increase stock_log for the rollback step was previously missing, causing
// stock_log.stock to drift below the true FIFO qty on every in-place edit.
rollbackQty := recordingStockRollbackQty(stock)
if rollbackQty > 1e-6 && shouldWriteLog {
resetLog := &entity.StockLog{
ProductWarehouseId: stock.ProductWarehouseId,
CreatedBy: actorID,
Increase: rollbackQty,
LoggableType: string(utils.StockLogTypeRecording),
LoggableId: stock.RecordingId,
Notes: note,
}
if err := s.appendRecordingStockLog(ctx, tx, resetLogState, resetLog); err != nil {
return err
}
}
} else { } else {
zero := 0.0 zero := 0.0
stock = entity.RecordingStock{ stock = entity.RecordingStock{
@@ -3672,6 +3810,44 @@ func ensureRecordingEggsUnused(eggs []entity.RecordingEgg) error {
return nil return nil
} }
func ensureRecordingEggQtyChangeSafe(existingEggs []entity.RecordingEgg, reqEggs []validation.Egg) error {
usedByWarehouse := make(map[uint]float64)
for _, egg := range existingEggs {
usedByWarehouse[egg.ProductWarehouseId] += egg.TotalUsed
}
newQtyByWarehouse := make(map[uint]int)
for _, egg := range reqEggs {
newQtyByWarehouse[egg.ProductWarehouseId] += egg.Qty
}
for warehouseID, used := range usedByWarehouse {
if used <= 0 {
continue
}
if float64(newQtyByWarehouse[warehouseID]) < used {
return fiber.NewError(fiber.StatusBadRequest,
fmt.Sprintf("Jumlah telur tidak dapat dikurangi di bawah jumlah yang sudah terjual (%.0f butir)", used))
}
}
return nil
}
func (s *recordingService) updateEggWeightsOnly(tx *gorm.DB, existingEggs []entity.RecordingEgg, reqEggs []validation.Egg) error {
weightByWarehouse := make(map[uint]*float64)
for i := range reqEggs {
weightByWarehouse[reqEggs[i].ProductWarehouseId] = reqEggs[i].Weight
}
for _, egg := range existingEggs {
newWeight, ok := weightByWarehouse[egg.ProductWarehouseId]
if !ok {
continue
}
if err := s.Repository.UpdateEggWeight(tx, egg.Id, newWeight); err != nil {
return err
}
}
return nil
}
func (s *recordingService) recalculateFrom(ctx context.Context, tx *gorm.DB, projectFlockKandangId uint, from time.Time) error { func (s *recordingService) recalculateFrom(ctx context.Context, tx *gorm.DB, projectFlockKandangId uint, from time.Time) error {
if tx == nil || projectFlockKandangId == 0 || from.IsZero() { if tx == nil || projectFlockKandangId == 0 || from.IsZero() {
return nil return nil
@@ -17,6 +17,8 @@ type TransferLayingRepository interface {
IdExists(ctx context.Context, id uint) (bool, error) IdExists(ctx context.Context, id uint) (bool, error)
GetLatestApprovedBySourceKandang(ctx context.Context, sourceProjectFlockKandangID uint) (*entity.LayingTransfer, error) GetLatestApprovedBySourceKandang(ctx context.Context, sourceProjectFlockKandangID uint) (*entity.LayingTransfer, error)
GetLatestApprovedByTargetKandang(ctx context.Context, targetProjectFlockKandangID uint) (*entity.LayingTransfer, error) GetLatestApprovedByTargetKandang(ctx context.Context, targetProjectFlockKandangID uint) (*entity.LayingTransfer, error)
GetLatestApprovedBySourceKandangs(ctx context.Context, pfkIDs []uint) (map[uint]*entity.LayingTransfer, error)
GetLatestApprovedByTargetKandangs(ctx context.Context, pfkIDs []uint) (map[uint]*entity.LayingTransfer, error)
// Tambah method baru untuk query dengan filter lengkap // Tambah method baru untuk query dengan filter lengkap
GetAllWithFilters(ctx context.Context, offset int, limit int, params *GetAllFilterParams) ([]entity.LayingTransfer, int64, error) GetAllWithFilters(ctx context.Context, offset int, limit int, params *GetAllFilterParams) ([]entity.LayingTransfer, int64, error)
@@ -242,3 +244,121 @@ func (r *TransferLayingRepositoryImpl) GetLatestApprovedByTargetKandang(ctx cont
} }
return &transfer, nil return &transfer, nil
} }
type pfkTransferIDRow struct {
SourcePFKID uint `gorm:"column:source_pfk_id"`
TransferID uint `gorm:"column:transfer_id"`
}
func (r *TransferLayingRepositoryImpl) GetLatestApprovedBySourceKandangs(ctx context.Context, pfkIDs []uint) (map[uint]*entity.LayingTransfer, error) {
result := make(map[uint]*entity.LayingTransfer)
if len(pfkIDs) == 0 {
return result, nil
}
var rows []pfkTransferIDRow
err := r.db.WithContext(ctx).Raw(`
SELECT DISTINCT ON (source_pfk_id) source_pfk_id, transfer_id
FROM (
SELECT id AS transfer_id, source_project_flock_kandang_id AS source_pfk_id
FROM laying_transfers
WHERE source_project_flock_kandang_id IN ?
AND deleted_at IS NULL
AND (
SELECT a.action FROM approvals a
WHERE a.approvable_type = ? AND a.approvable_id = id
ORDER BY a.id DESC LIMIT 1
) = ?
UNION ALL
SELECT lts.laying_transfer_id AS transfer_id, lts.source_project_flock_kandang_id AS source_pfk_id
FROM laying_transfer_sources lts
JOIN laying_transfers t ON t.id = lts.laying_transfer_id AND t.deleted_at IS NULL
WHERE lts.source_project_flock_kandang_id IN ?
AND lts.deleted_at IS NULL
AND (
SELECT a.action FROM approvals a
WHERE a.approvable_type = ? AND a.approvable_id = t.id
ORDER BY a.id DESC LIMIT 1
) = ?
) combined
ORDER BY source_pfk_id, transfer_id DESC
`,
pfkIDs, string(utils.ApprovalWorkflowTransferToLaying), string(entity.ApprovalActionApproved),
pfkIDs, string(utils.ApprovalWorkflowTransferToLaying), string(entity.ApprovalActionApproved),
).Scan(&rows).Error
if err != nil {
return nil, err
}
if len(rows) == 0 {
return result, nil
}
transferIDs := make([]uint, 0, len(rows))
pfkByTransfer := make(map[uint]uint, len(rows))
for _, row := range rows {
transferIDs = append(transferIDs, row.TransferID)
pfkByTransfer[row.TransferID] = row.SourcePFKID
}
var transfers []entity.LayingTransfer
if err := r.db.WithContext(ctx).Where("id IN ? AND deleted_at IS NULL", transferIDs).Find(&transfers).Error; err != nil {
return nil, err
}
for i := range transfers {
if pfkID := pfkByTransfer[transfers[i].Id]; pfkID != 0 {
result[pfkID] = &transfers[i]
}
}
return result, nil
}
func (r *TransferLayingRepositoryImpl) GetLatestApprovedByTargetKandangs(ctx context.Context, pfkIDs []uint) (map[uint]*entity.LayingTransfer, error) {
result := make(map[uint]*entity.LayingTransfer)
if len(pfkIDs) == 0 {
return result, nil
}
var rows []pfkTransferIDRow
err := r.db.WithContext(ctx).Raw(`
SELECT DISTINCT ON (source_pfk_id) source_pfk_id, transfer_id
FROM (
SELECT ltt.laying_transfer_id AS transfer_id, ltt.target_project_flock_kandang_id AS source_pfk_id
FROM laying_transfer_targets ltt
JOIN laying_transfers t ON t.id = ltt.laying_transfer_id AND t.deleted_at IS NULL
WHERE ltt.target_project_flock_kandang_id IN ?
AND ltt.deleted_at IS NULL
AND (
SELECT a.action FROM approvals a
WHERE a.approvable_type = ? AND a.approvable_id = t.id
ORDER BY a.id DESC LIMIT 1
) = ?
) combined
ORDER BY source_pfk_id, transfer_id DESC
`,
pfkIDs, string(utils.ApprovalWorkflowTransferToLaying), string(entity.ApprovalActionApproved),
).Scan(&rows).Error
if err != nil {
return nil, err
}
if len(rows) == 0 {
return result, nil
}
transferIDs := make([]uint, 0, len(rows))
pfkByTransfer := make(map[uint]uint, len(rows))
for _, row := range rows {
transferIDs = append(transferIDs, row.TransferID)
pfkByTransfer[row.TransferID] = row.SourcePFKID
}
var transfers []entity.LayingTransfer
if err := r.db.WithContext(ctx).Where("id IN ? AND deleted_at IS NULL", transferIDs).Find(&transfers).Error; err != nil {
return nil, err
}
for i := range transfers {
if pfkID := pfkByTransfer[transfers[i].Id]; pfkID != 0 {
result[pfkID] = &transfers[i]
}
}
return result, nil
}
@@ -3,13 +3,11 @@ package controller
import ( import (
"fmt" "fmt"
"math" "math"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities" entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/dto"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/xuri/excelize/v2" "github.com/xuri/excelize/v2"
@@ -45,7 +43,6 @@ func buildPurchaseExportWorkbook(purchases []entity.Purchase) ([]byte, error) {
} }
} }
listItems := dto.ToPurchaseListDTOs(purchases)
grandTotals := buildPurchaseGrandTotalMap(purchases) grandTotals := buildPurchaseGrandTotalMap(purchases)
if err := setPurchaseExportColumns(file, purchaseExportSheetName); err != nil { if err := setPurchaseExportColumns(file, purchaseExportSheetName); err != nil {
@@ -54,7 +51,7 @@ func buildPurchaseExportWorkbook(purchases []entity.Purchase) ([]byte, error) {
if err := setPurchaseExportHeaders(file, purchaseExportSheetName); err != nil { if err := setPurchaseExportHeaders(file, purchaseExportSheetName); err != nil {
return nil, err return nil, err
} }
if err := setPurchaseExportRows(file, purchaseExportSheetName, listItems, grandTotals); err != nil { if err := setPurchaseExportRows(file, purchaseExportSheetName, purchases, grandTotals); err != nil {
return nil, err return nil, err
} }
if err := file.SetPanes(purchaseExportSheetName, &excelize.Panes{ if err := file.SetPanes(purchaseExportSheetName, &excelize.Panes{
@@ -81,10 +78,11 @@ func setPurchaseExportColumns(file *excelize.File, sheet string) error {
"D": 14, "D": 14,
"E": 22, "E": 22,
"F": 22, "F": 22,
"G": 18, "G": 22,
"H": 18, "H": 32,
"I": 52, "I": 18,
"J": 24, "J": 18,
"K": 24,
} }
for col, width := range columnWidths { for col, width := range columnWidths {
@@ -107,9 +105,10 @@ func setPurchaseExportHeaders(file *excelize.File, sheet string) error {
"Tanggal Terima", "Tanggal Terima",
"Supplier", "Supplier",
"Lokasi", "Lokasi",
"Gudang",
"Product",
"Status", "Status",
"Grand Total", "Grand Total",
"Products",
"Notes", "Notes",
} }
@@ -138,49 +137,34 @@ func setPurchaseExportHeaders(file *excelize.File, sheet string) error {
return err return err
} }
return file.SetCellStyle(sheet, "A1", "J1", headerStyle) return file.SetCellStyle(sheet, "A1", "K1", headerStyle)
} }
func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.PurchaseListDTO, grandTotals map[uint]float64) error { func setPurchaseExportRows(file *excelize.File, sheet string, purchases []entity.Purchase, grandTotals map[uint]float64) error {
if len(items) == 0 { if len(purchases) == 0 {
return nil return nil
} }
for i, item := range items { rowIdx := 2
row := strconv.Itoa(i + 2) for p := range purchases {
if err := file.SetCellValue(sheet, "A"+row, safePurchaseExportText(item.PrNumber)); err != nil { purchase := &purchases[p]
total := grandTotals[purchase.Id]
if len(purchase.Items) == 0 {
if err := writePurchaseExportRow(file, sheet, rowIdx, purchase, nil, total); err != nil {
return err return err
} }
if err := file.SetCellValue(sheet, "B"+row, safePurchaseExportPointerText(item.PoNumber)); err != nil { rowIdx++
continue
}
for it := range purchase.Items {
if err := writePurchaseExportRow(file, sheet, rowIdx, purchase, &purchase.Items[it], total); err != nil {
return err return err
} }
if err := file.SetCellValue(sheet, "C"+row, formatPurchaseExportDate(item.PoDate)); err != nil { rowIdx++
return err
}
if err := file.SetCellValue(sheet, "D"+row, formatPurchaseExportDate(item.ReceivedDate)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "E"+row, safePurchaseSupplierName(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "F"+row, safePurchaseLocationName(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "G"+row, formatPurchaseExportStatus(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "H"+row, formatPurchaseRupiah(grandTotals[item.Id])); err != nil {
return err
}
if err := file.SetCellValue(sheet, "I"+row, formatPurchaseProducts(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "J"+row, safePurchaseExportPointerText(item.Notes)); err != nil {
return err
} }
} }
lastRow := len(items) + 1 lastRow := rowIdx - 1
dataStyle, err := file.NewStyle(&excelize.Style{ dataStyle, err := file.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{ Alignment: &excelize.Alignment{
Horizontal: "left", Horizontal: "left",
@@ -197,7 +181,7 @@ func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.Purcha
if err != nil { if err != nil {
return err return err
} }
if err := file.SetCellStyle(sheet, "A2", "J"+strconv.Itoa(lastRow), dataStyle); err != nil { if err := file.SetCellStyle(sheet, "A2", "K"+strconv.Itoa(lastRow), dataStyle); err != nil {
return err return err
} }
@@ -217,7 +201,59 @@ func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.Purcha
return err return err
} }
return file.SetCellStyle(sheet, "H2", "H"+strconv.Itoa(lastRow), moneyStyle) return file.SetCellStyle(sheet, "J2", "J"+strconv.Itoa(lastRow), moneyStyle)
}
func writePurchaseExportRow(file *excelize.File, sheet string, rowIdx int, purchase *entity.Purchase, item *entity.PurchaseItem, grandTotal float64) error {
row := strconv.Itoa(rowIdx)
// Purchase-level columns (repeat across rows of the same purchase)
if err := file.SetCellValue(sheet, "A"+row, safePurchaseExportText(purchase.PrNumber)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "B"+row, safePurchaseExportPointerText(purchase.PoNumber)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "C"+row, formatPurchaseExportDate(purchase.PoDate)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "E"+row, safePurchaseExportEntitySupplierName(purchase)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "I"+row, formatPurchaseExportEntityStatus(purchase)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "J"+row, formatPurchaseRupiah(grandTotal)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "K"+row, safePurchaseExportPointerText(purchase.Notes)); err != nil {
return err
}
// Item-level columns
if item == nil {
for _, col := range []string{"D", "F", "G", "H"} {
if err := file.SetCellValue(sheet, col+row, "-"); err != nil {
return err
}
}
return nil
}
if err := file.SetCellValue(sheet, "D"+row, formatPurchaseExportDate(item.ReceivedDate)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "F"+row, safePurchaseItemLocationName(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "G"+row, safePurchaseWarehouseName(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "H"+row, safePurchaseItemProductName(item)); err != nil {
return err
}
return nil
} }
func buildPurchaseGrandTotalMap(items []entity.Purchase) map[uint]float64 { func buildPurchaseGrandTotalMap(items []entity.Purchase) map[uint]float64 {
@@ -232,31 +268,45 @@ func buildPurchaseGrandTotalMap(items []entity.Purchase) map[uint]float64 {
return result return result
} }
func safePurchaseSupplierName(item dto.PurchaseListDTO) string { func safePurchaseExportEntitySupplierName(purchase *entity.Purchase) string {
if item.Supplier == nil { if purchase.Supplier.Id == 0 {
return "-" return "-"
} }
return safePurchaseExportText(item.Supplier.Name) return safePurchaseExportText(purchase.Supplier.Name)
} }
func safePurchaseLocationName(item dto.PurchaseListDTO) string { func safePurchaseWarehouseName(item *entity.PurchaseItem) string {
if item.Location == nil { if item.Warehouse == nil {
return "-" return "-"
} }
return safePurchaseExportText(item.Location.Name) return safePurchaseExportText(item.Warehouse.Name)
} }
func formatPurchaseExportStatus(item dto.PurchaseListDTO) string { func safePurchaseItemLocationName(item *entity.PurchaseItem) string {
if item.LatestApproval == nil { if item.Warehouse == nil || item.Warehouse.Location == nil {
return "-"
}
return safePurchaseExportText(item.Warehouse.Location.Name)
}
func safePurchaseItemProductName(item *entity.PurchaseItem) string {
if item.Product == nil {
return "-"
}
return safePurchaseExportText(item.Product.Name)
}
func formatPurchaseExportEntityStatus(purchase *entity.Purchase) string {
if purchase.LatestApproval == nil {
return "-" return "-"
} }
if item.LatestApproval.Action != nil && if purchase.LatestApproval.Action != nil &&
strings.EqualFold(strings.TrimSpace(*item.LatestApproval.Action), string(entity.ApprovalActionRejected)) { strings.EqualFold(strings.TrimSpace(string(*purchase.LatestApproval.Action)), string(entity.ApprovalActionRejected)) {
return "Ditolak" return "Ditolak"
} }
return safePurchaseExportText(item.LatestApproval.StepName) return safePurchaseExportText(purchase.LatestApproval.StepName)
} }
func formatPurchaseExportDate(value *time.Time) string { func formatPurchaseExportDate(value *time.Time) string {
@@ -273,33 +323,6 @@ func formatPurchaseExportDate(value *time.Time) string {
return t.Format("02-01-2006") return t.Format("02-01-2006")
} }
func formatPurchaseProducts(item dto.PurchaseListDTO) string {
if len(item.Products) == 0 {
return "-"
}
seen := make(map[string]struct{})
names := make([]string, 0, len(item.Products))
for i := range item.Products {
name := strings.TrimSpace(item.Products[i].Name)
if name == "" {
continue
}
if _, exists := seen[name]; exists {
continue
}
seen[name] = struct{}{}
names = append(names, name)
}
if len(names) == 0 {
return "-"
}
sort.Strings(names)
return strings.Join(names, ", ")
}
func safePurchaseExportPointerText(value *string) string { func safePurchaseExportPointerText(value *string) string {
if value == nil { if value == nil {
return "-" return "-"
@@ -31,6 +31,7 @@ type PurchaseListDTO struct {
CreatedUser *userDTO.UserRelationDTO `json:"created_user"` CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
RequesterName string `json:"requester_name"` RequesterName string `json:"requester_name"`
PoExpedition []PoExpeditionDTO `json:"po_expedition"` PoExpedition []PoExpeditionDTO `json:"po_expedition"`
Items []PurchaseItemDTO `json:"items"`
Products []productDTO.ProductRelationDTO `json:"products"` Products []productDTO.ProductRelationDTO `json:"products"`
Location *locationDTO.LocationRelationDTO `json:"location"` Location *locationDTO.LocationRelationDTO `json:"location"`
Area *areaDTO.AreaRelationDTO `json:"area"` Area *areaDTO.AreaRelationDTO `json:"area"`
@@ -227,6 +228,7 @@ func ToPurchaseListDTO(p entity.Purchase) PurchaseListDTO {
CreatedUser: createdUser, CreatedUser: createdUser,
RequesterName: requesterName, RequesterName: requesterName,
PoExpedition: poExpedition, PoExpedition: poExpedition,
Items: ToPurchaseItemDTOs(p.Items),
Products: products, Products: products,
Location: location, Location: location,
Area: area, Area: area,
@@ -55,19 +55,19 @@ type pdfColumn struct {
var marketingPdfColumns = []pdfColumn{ var marketingPdfColumns = []pdfColumn{
{"No", 6, "C"}, {"No", 6, "C"},
{"Tanggal Sales Order", 16, "C"}, {"Tanggal\nJual", 16, "C"},
{"Tanggal Delivery Order", 16, "C"}, {"Tanggal\nRealisasi", 16, "C"},
{"Aging\n(Hari)", 9, "C"}, {"Aging\n(Hari)", 9, "C"},
{"Gudang Fisik", 20, "L"}, {"Gudang\nFisik", 20, "L"},
{"Pelanggan", 20, "L"}, {"Pelanggan", 20, "L"},
{"No. DO", 18, "L"},
{"Sales", 18, "L"}, {"Sales", 18, "L"},
{"Produk", 16, "L"}, {"No. Polisi", 18, "L"},
{"Nomor DO", 14, "C"},
{"Nomor Polisi", 14, "C"},
{"Tipe\nMarketing", 14, "C"}, {"Tipe\nMarketing", 14, "C"},
{"Quantity", 13, "R"}, {"Produk", 16, "L"},
{"Rata-Rata\n(Kg)", 13, "R"}, {"Kuantitas", 13, "R"},
{"Total Berat\n(Kg)", 14, "R"}, {"Bobot Rata-Rata\n(Kg)", 13, "R"},
{"Bobot Total Berat\n(Kg)", 14, "R"},
{"Harga Jual\n(Rp)", 17, "R"}, {"Harga Jual\n(Rp)", 17, "R"},
{"HPP\n(Rp)", 17, "R"}, {"HPP\n(Rp)", 17, "R"},
{"Total Jual\n(Rp)", 18, "R"}, {"Total Jual\n(Rp)", 18, "R"},
@@ -184,50 +184,76 @@ func writeMarketingPdfRows(pdf *fpdf.Fpdf, items []dto.RepportMarketingItemDTO)
pdf.SetDrawColor(borderR, borderG, borderB) pdf.SetDrawColor(borderR, borderG, borderB)
pdf.SetLineWidth(0.1) pdf.SetLineWidth(0.1)
rowH := 6.0 lineH := 5.0
for idx, item := range items { for idx, item := range items {
// page break check values := marketingPdfRowValues(idx+1, item)
rowH := calcMarketingRowHeight(pdf, values, lineH)
if pdf.GetY()+rowH > marketingPdfPageHeight(pdf)-12 { if pdf.GetY()+rowH > marketingPdfPageHeight(pdf)-12 {
pdf.AddPage() pdf.AddPage()
writeMarketingPdfHeader(pdf) writeMarketingPdfHeader(pdf)
pdf.SetFont("Helvetica", "", 6) pdf.SetFont("Helvetica", "", 6)
} }
// alternating bg var fillR, fillG, fillB int
if idx%2 == 1 { if idx%2 == 1 {
pdf.SetFillColor(rowAltR, rowAltG, rowAltB) fillR, fillG, fillB = rowAltR, rowAltG, rowAltB
} else { } else {
pdf.SetFillColor(255, 255, 255) fillR, fillG, fillB = 255, 255, 255
} }
pdf.SetTextColor(40, 40, 40) pdf.SetTextColor(40, 40, 40)
y := pdf.GetY() y := pdf.GetY()
writeMarketingPdfRow(pdf, idx+1, item, rowH, y) writeMarketingPdfRow(pdf, item, values, lineH, rowH, y, fillR, fillG, fillB)
} }
} }
func writeMarketingPdfRow(pdf *fpdf.Fpdf, no int, item dto.RepportMarketingItemDTO, h, y float64) { func calcMarketingRowHeight(pdf *fpdf.Fpdf, values []string, lineH float64) float64 {
fill := true // use the fill colour already set margin := pdf.GetCellMargin()
cols := marketingPdfColumns cols := marketingPdfColumns
x := 10.0 // left margin maxLines := 1
for i, col := range cols {
if i >= len(values) || i == 10 {
continue
}
usableW := col.width - 2*margin
if usableW <= 0 {
continue
}
lines := pdf.SplitLines([]byte(values[i]), usableW)
n := len(lines)
if n == 0 {
n = 1
}
if n > maxLines {
maxLines = n
}
}
return float64(maxLines) * lineH
}
values := marketingPdfRowValues(no, item) func writeMarketingPdfRow(pdf *fpdf.Fpdf, item dto.RepportMarketingItemDTO, values []string, lineH, rowH, y float64, fillR, fillG, fillB int) {
cols := marketingPdfColumns
x := 10.0
for i, col := range cols { for i, col := range cols {
pdf.SetXY(x, y) if i == 10 {
drawMarketingTypeBadge(pdf, x, y, col.width, rowH, item.MarketingType)
if i == 10 { // Tipe Marketing → badge pdf.SetDrawColor(borderR, borderG, borderB)
drawMarketingTypeBadge(pdf, x, y, col.width, h, item.MarketingType) pdf.SetTextColor(40, 40, 40)
} else { } else {
pdf.CellFormat(col.width, h, values[i], "1", 0, col.align, fill, 0, "") pdf.SetFillColor(fillR, fillG, fillB)
pdf.SetDrawColor(borderR, borderG, borderB)
pdf.Rect(x, y, col.width, rowH, "FD")
pdf.SetTextColor(40, 40, 40)
pdf.SetXY(x, y)
pdf.MultiCell(col.width, lineH, values[i], "", col.align, false)
} }
x += col.width x += col.width
} }
pdf.SetXY(10, y+h) pdf.SetXY(10, y+rowH)
} }
func marketingPdfRowValues(no int, item dto.RepportMarketingItemDTO) []string { func marketingPdfRowValues(no int, item dto.RepportMarketingItemDTO) []string {
@@ -255,11 +281,11 @@ func marketingPdfRowValues(no int, item dto.RepportMarketingItemDTO) []string {
strconv.Itoa(item.AgingDays), strconv.Itoa(item.AgingDays),
warehouse, warehouse,
customer, customer,
sales,
product,
safeMarketingExportText(item.DoNumber), safeMarketingExportText(item.DoNumber),
sales,
safeMarketingExportText(item.VehicleNumber), safeMarketingExportText(item.VehicleNumber),
safeMarketingExportText(item.MarketingType), // index 10, overridden by badge safeMarketingExportText(item.MarketingType), // index 10, overridden by badge
product,
formatMarketingPdfNumber(item.Qty), formatMarketingPdfNumber(item.Qty),
formatMarketingPdfDecimal(item.AverageWeightKg), formatMarketingPdfDecimal(item.AverageWeightKg),
formatMarketingPdfDecimal(item.TotalWeightKg), formatMarketingPdfDecimal(item.TotalWeightKg),
@@ -882,8 +882,6 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
defaultBw = 0 defaultBw = 0
defaultUniformText = "90% up" defaultUniformText = "90% up"
) )
defaultStartWoa := config.LayingWeekStart()
if params.Limit <= 0 { if params.Limit <= 0 {
params.Limit = 10 params.Limit = 10
} }
@@ -933,7 +931,7 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
weeks := make([]int, len(weeklyResults)) weeks := make([]int, len(weeklyResults))
for i := range weeklyResults { for i := range weeklyResults {
weeks[i] = defaultStartWoa + i weeks[i] = int(weeklyResults[i].Woa)
} }
uniformityMap, err := s.getUniformityByWeek(ctx.Context(), params.ProjectFlockKandangID, weeks) uniformityMap, err := s.getUniformityByWeek(ctx.Context(), params.ProjectFlockKandangID, weeks)
if err != nil { if err != nil {
@@ -943,13 +941,12 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
var cumulativeButir int64 var cumulativeButir int64
var cumulativeKg float64 var cumulativeKg float64
for i := range weeklyResults { for i := range weeklyResults {
weeklyResults[i].Woa = float64(defaultStartWoa + i)
weeklyResults[i].StdBw = defaultStdBw weeklyResults[i].StdBw = defaultStdBw
weeklyResults[i].Bw = defaultBw weeklyResults[i].Bw = defaultBw
if weeklyResults[i].StdUniformity == "" { if weeklyResults[i].StdUniformity == "" {
weeklyResults[i].StdUniformity = defaultUniformText weeklyResults[i].StdUniformity = defaultUniformText
} }
if uniformity, ok := uniformityMap[defaultStartWoa+i]; ok { if uniformity, ok := uniformityMap[int(weeklyResults[i].Woa)]; ok {
weeklyResults[i].Uniformity = uniformity.Uniformity weeklyResults[i].Uniformity = uniformity.Uniformity
if uniformity.AvgWeight != nil { if uniformity.AvgWeight != nil {
weeklyResults[i].Bw = *uniformity.AvgWeight weeklyResults[i].Bw = *uniformity.AvgWeight
@@ -1356,8 +1353,8 @@ func mapRecordingToProductionResultDTO(record entity.Recording) dto.ProductionRe
Ew: valueOrZero(record.EggWeight), Ew: valueOrZero(record.EggWeight),
} }
if record.Day != nil { if record.Day != nil && *record.Day > 0 {
result.Woa = float64(*record.Day) result.Woa = float64((*record.Day + 6) / 7) // ceil(day/7)
} }
avgWeight := 1.0 avgWeight := 1.0
if avgWeight > 0 { if avgWeight > 0 {
@@ -1588,6 +1585,7 @@ func aggregateProductionResultGroup(group []dto.ProductionResultDTO, groupSize i
CreatedAt: group[0].CreatedAt, CreatedAt: group[0].CreatedAt,
UpdatedAt: group[0].UpdatedAt, UpdatedAt: group[0].UpdatedAt,
StdUniformity: group[0].StdUniformity, StdUniformity: group[0].StdUniformity,
Woa: group[0].Woa,
} }
var sumBw, sumStdBw, sumUniformity float64 var sumBw, sumStdBw, sumUniformity float64
+106 -3
View File
@@ -51,6 +51,7 @@ type routeMeta struct {
Security securityMode Security securityMode
ListStyle bool ListStyle bool
QueryParams []parameterMeta QueryParams []parameterMeta
ExampleResponse any
Exclude bool Exclude bool
} }
@@ -187,6 +188,13 @@ func buildOpenAPIDocument(routes []normalizedRoute) map[string]any {
} }
openAPIPath := toOpenAPIPath(route.Path) openAPIPath := toOpenAPIPath(route.Path)
responseContent := map[string]any{
"schema": successSchema(meta),
}
if meta.ExampleResponse != nil {
responseContent["example"] = meta.ExampleResponse
}
operation := map[string]any{ operation := map[string]any{
"summary": meta.Summary, "summary": meta.Summary,
"description": meta.Description, "description": meta.Description,
@@ -195,9 +203,7 @@ func buildOpenAPIDocument(routes []normalizedRoute) map[string]any {
"200": map[string]any{ "200": map[string]any{
"description": "Successful response", "description": "Successful response",
"content": map[string]any{ "content": map[string]any{
"application/json": map[string]any{ "application/json": responseContent,
"schema": successSchema(meta),
},
}, },
}, },
"401": map[string]any{ "401": map[string]any{
@@ -777,6 +783,31 @@ func describeRoute(route normalizedRoute) routeMeta {
{Name: "limit", In: "query", Description: "Page size.", Example: 10}, {Name: "limit", In: "query", Description: "Page size.", Example: 10},
{Name: "search", In: "query", Description: "Search keyword.", Example: "fcr"}, {Name: "search", In: "query", Description: "Search keyword.", Example: "fcr"},
} }
meta.ExampleResponse = map[string]any{
"code": 200, "status": "success", "message": "Get all fcrs successfully",
"meta": map[string]any{"page": 1, "limit": 10, "total_pages": 1, "total_results": 1},
"data": []map[string]any{
{
"id": 1, "name": "FCR Broiler Standard",
"created_user": map[string]any{"id": 1, "name": "Admin"},
"created_at": "2026-01-01T00:00:00Z", "updated_at": "2026-01-01T00:00:00Z",
},
},
}
case "/api/master-data/fcrs/:id":
meta.ExampleResponse = map[string]any{
"code": 200, "status": "success", "message": "Get fcr successfully",
"data": map[string]any{
"id": 1, "name": "FCR Broiler Standard",
"created_user": map[string]any{"id": 1, "name": "Admin"},
"created_at": "2026-01-01T00:00:00Z", "updated_at": "2026-01-01T00:00:00Z",
"fcr_standards": []map[string]any{
{"id": 1, "weight": 0.5, "fcr_number": 1.2, "mortality": 0.5},
{"id": 2, "weight": 1.0, "fcr_number": 1.35, "mortality": 0.3},
{"id": 3, "weight": 1.5, "fcr_number": 1.5, "mortality": 0.25},
},
},
}
case "/api/master-data/flocks": case "/api/master-data/flocks":
meta.QueryParams = []parameterMeta{ meta.QueryParams = []parameterMeta{
{Name: "page", In: "query", Description: "Page number.", Example: 1}, {Name: "page", In: "query", Description: "Page number.", Example: 1},
@@ -926,6 +957,31 @@ func describeRoute(route normalizedRoute) routeMeta {
{Name: "project_flock_kandang_id", In: "query", Description: "Project flock kandang id.", Required: true, Example: 1, PostmanValue: "{{project_flock_kandang_id}}"}, {Name: "project_flock_kandang_id", In: "query", Description: "Project flock kandang id.", Required: true, Example: 1, PostmanValue: "{{project_flock_kandang_id}}"},
{Name: "record_date", In: "query", Description: "Recording date (YYYY-MM-DD).", Required: true, Example: "2026-01-01"}, {Name: "record_date", In: "query", Description: "Recording date (YYYY-MM-DD).", Required: true, Example: "2026-01-01"},
} }
case "/api/production/chickins":
meta.QueryParams = []parameterMeta{
{Name: "page", In: "query", Description: "Page number.", Example: 1},
{Name: "limit", In: "query", Description: "Page size.", Example: 10},
{Name: "project_flock_kandang_id", In: "query", Description: "Project flock kandang id filter.", Example: 1, PostmanValue: "{{project_flock_kandang_id}}"},
}
meta.ExampleResponse = map[string]any{
"code": 200, "status": "success", "message": "Get all chickins successfully",
"meta": map[string]any{"page": 1, "limit": 10, "total_pages": 1, "total_results": 1},
"data": []map[string]any{
{
"id": 1, "project_flock_kandang_id": 1,
"chick_in_date": "2026-01-01T00:00:00Z",
"product_warehouse_id": 1,
"product_warehouse": map[string]any{
"id": 1,
"product": map[string]any{"id": 1, "name": "DOC Broiler"},
"warehouse": map[string]any{"id": 1, "name": "Gudang DOC"},
},
"usage_qty": 10000.0, "pending_usage_qty": 0.0, "notes": "",
"created_user": map[string]any{"id": 1, "name": "Admin"},
"created_at": "2026-01-01T00:00:00Z", "updated_at": "2026-01-01T00:00:00Z",
},
},
}
case "/api/production/transfer_layings": case "/api/production/transfer_layings":
meta.QueryParams = []parameterMeta{ meta.QueryParams = []parameterMeta{
{Name: "page", In: "query", Description: "Page number.", Example: 1}, {Name: "page", In: "query", Description: "Page number.", Example: 1},
@@ -937,6 +993,53 @@ func describeRoute(route normalizedRoute) routeMeta {
{Name: "flock_destination", In: "query", Description: "Comma separated destination flock ids.", Example: "3,4"}, {Name: "flock_destination", In: "query", Description: "Comma separated destination flock ids.", Example: "3,4"},
{Name: "status", In: "query", Description: "Comma separated status values.", Example: "DRAFT,APPROVED"}, {Name: "status", In: "query", Description: "Comma separated status values.", Example: "DRAFT,APPROVED"},
} }
meta.ExampleResponse = map[string]any{
"code": 200, "status": "success", "message": "Get all transferLayings successfully",
"meta": map[string]any{"page": 1, "limit": 10, "total_pages": 1, "total_results": 1},
"data": []map[string]any{
{
"id": 1, "transfer_number": "TL-00001",
"transfer_date": "2026-01-15T00:00:00Z",
"economic_cutoff_date": "2026-01-20T00:00:00Z",
"effective_move_date": "2026-01-18T00:00:00Z",
"executed_at": nil, "notes": "",
"from_project_flock": map[string]any{"id": 1, "flock_name": "Flock A Period 1"},
"to_project_flock": map[string]any{"id": 2, "flock_name": "Flock B Period 1"},
"created_by": 1,
"created_user": map[string]any{"id": 1, "name": "Admin"},
"created_at": "2026-01-15T00:00:00Z",
"approval": map[string]any{"step_number": 1, "step_name": "Pengajuan", "action": nil},
},
},
}
case "/api/production/transfer_layings/:id":
meta.ExampleResponse = map[string]any{
"code": 200, "status": "success", "message": "Get transferLaying successfully",
"data": map[string]any{
"id": 1, "transfer_number": "TL-00001",
"transfer_date": "2026-01-15T00:00:00Z",
"economic_cutoff_date": "2026-01-20T00:00:00Z",
"effective_move_date": "2026-01-18T00:00:00Z",
"executed_at": nil, "notes": "",
"from_project_flock": map[string]any{"id": 1, "flock_name": "Flock A Period 1"},
"to_project_flock": map[string]any{"id": 2, "flock_name": "Flock B Period 1"},
"created_by": 1, "created_user": map[string]any{"id": 1, "name": "Admin"},
"created_at": "2026-01-15T00:00:00Z",
"approval": map[string]any{"step_number": 1, "step_name": "Pengajuan", "action": nil},
"sources": []map[string]any{
{
"source_project_flock_kandang": map[string]any{"id": 1, "kandang_id": 1, "project_flock_id": 1, "kandang": map[string]any{"id": 1, "name": "Kandang A"}},
"qty": 5000.0, "note": "",
},
},
"targets": []map[string]any{
{
"target_project_flock_kandang": map[string]any{"id": 2, "kandang_id": 2, "project_flock_id": 2, "kandang": map[string]any{"id": 2, "name": "Kandang B"}},
"qty": 5000.0, "note": "",
},
},
},
}
case "/api/production/uniformities": case "/api/production/uniformities":
meta.QueryParams = []parameterMeta{ meta.QueryParams = []parameterMeta{
{Name: "page", In: "query", Description: "Page number.", Example: 1}, {Name: "page", In: "query", Description: "Page number.", Example: 1},
@@ -148,6 +148,19 @@ func EggTotalsEqual(a, b map[uint]EggTotals) bool {
return true return true
} }
func EggQtyByWarehouseEqual(a, b map[uint]EggTotals) bool {
if len(a) != len(b) {
return false
}
for key, value := range a {
other, ok := b[key]
if !ok || value.Qty != other.Qty {
return false
}
}
return true
}
func DepletionRouteMapsEqual(a, b map[DepletionRoute]float64) bool { func DepletionRouteMapsEqual(a, b map[DepletionRoute]float64) bool {
if len(a) != len(b) { if len(a) != len(b) {
return false return false