Compare commits

..

128 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 16ac54ff39 add export excel and pdf report penjualan 2026-04-29 11:52:03 +07:00
Giovanni Gabriel Septriadi 196bbf4277 Merge branch 'fix/consolidate-pw' into 'development'
[FEAT][BE]: adjust query consolidate duplicate product warehouses

See merge request mbugroup/lti-api!491
2026-04-28 11:26:03 +00:00
giovanni d3053d6417 adjust query consolidate duplicate product warehouses 2026-04-28 18:23:17 +07:00
Giovanni Gabriel Septriadi 27302ea6b5 Merge branch 'fix/create-flock' into 'development'
[FEAT][BE]: adjust periode at create project flock

See merge request mbugroup/lti-api!490
2026-04-28 09:01:46 +00:00
giovanni 76ac1d1671 adjust periode at create project flock 2026-04-28 16:00:35 +07:00
Adnan Zahir 44cbc1b304 Merge branch 'feat/toggle-negative-usgae' into 'development'
fix: flock label on farm-level products

See merge request mbugroup/lti-api!489
2026-04-28 13:59:06 +07:00
Adnan Zahir e4d4bd9483 fix: flock label on farm-level products 2026-04-28 13:47:16 +07:00
Adnan Zahir cea79bc569 Merge branch 'feat/toggle-negative-usgae' into 'development'
fix: onToggle return undefined

See merge request mbugroup/lti-api!488
2026-04-28 12:34:52 +07:00
Adnan Zahir 38cfc6b103 fix: onToggle return undefined 2026-04-28 12:30:52 +07:00
Adnan Zahir 80e4a04875 Merge branch 'feat/toggle-negative-usgae' into 'development'
fix: missing useAuth

See merge request mbugroup/lti-api!487
2026-04-28 12:07:54 +07:00
Adnan Zahir 9513da2a7c fix: missing useAuth 2026-04-28 12:07:36 +07:00
Adnan Zahir 874cefda45 Merge branch 'feat/toggle-negative-usgae' into 'development'
fix: getAll and update response

See merge request mbugroup/lti-api!486
2026-04-28 11:53:14 +07:00
Adnan Zahir 795f201a0b fix: getAll and update response 2026-04-28 11:52:32 +07:00
Adnan Zahir 49d684ebbe Merge branch 'feat/toggle-negative-usgae' into 'development'
feat: konfigurasi sistem toggle pemakaian pakan ovk negatif

See merge request mbugroup/lti-api!485
2026-04-28 11:23:37 +07:00
Adnan Zahir 6f6541d4c1 feat: konfigurasi sistem toggle pemakaian pakan ovk negatif 2026-04-28 10:51:54 +07:00
Giovanni Gabriel Septriadi 1d6e1fa5be Merge branch 'feat/limit' into 'development'
[FEAT][BE]: get field type to response detail recording

See merge request mbugroup/lti-api!484
2026-04-27 06:39:02 +00:00
giovanni aadd87852b get field type to response detail recording 2026-04-27 13:38:13 +07:00
Giovanni Gabriel Septriadi ac69ae054a Merge branch 'feat/limit' into 'development'
[FEAT][BE]: adjust validation limit max 100

See merge request mbugroup/lti-api!483
2026-04-27 04:55:21 +00:00
giovanni 7cc5c39092 adjust validation limit max 100 2026-04-27 11:54:29 +07:00
Giovanni Gabriel Septriadi 706303980b Merge branch 'feat/periode-flock' into 'development'
[FEAT][BE]: adjust suffix flock name with max periode

See merge request mbugroup/lti-api!482
2026-04-27 04:25:02 +00:00
giovanni d7a2a5a2ed adjust suffix flock name with max periode 2026-04-27 11:24:21 +07:00
Giovanni Gabriel Septriadi ae47493109 Merge branch 'feat/periode-flock' into 'development'
[FEAT][BE]: add feature edit periode project flock

See merge request mbugroup/lti-api!481
2026-04-27 03:55:02 +00:00
giovanni 9726303eeb add feature edit periode project flock 2026-04-27 10:54:02 +07:00
Adnan Zahir b764d2c3c0 Merge branch 'codex/filter-improvement' into 'development'
Codex/po date

See merge request mbugroup/lti-api!479
2026-04-25 22:50:20 +07:00
Adnan Zahir eefc9850e1 feat: editable po_date 2026-04-25 22:47:52 +07:00
Adnan Zahir 732ebd423d feat: input po_date manual 2026-04-25 22:36:13 +07:00
Adnan Zahir 27d076b817 feat: expose received_date in laporan pembelian 2026-04-25 22:24:28 +07:00
Adnan Zahir 9a9cc114fe Merge branch 'codex/filter-improvement' into 'development'
feat: update numeric tolerance for fcr

See merge request mbugroup/lti-api!477
2026-04-25 15:03:17 +07:00
Adnan Zahir af5f3dc7d4 feat: update numeric tolerance for fcr 2026-04-25 14:59:56 +07:00
Adnan Zahir e1f8d357c5 Merge branch 'codex/filter-improvement' into 'development'
feat: add flag ayam for chickin

See merge request mbugroup/lti-api!475
2026-04-25 14:04:49 +07:00
Adnan Zahir f6b37926e9 feat: add flag ayam for chickin 2026-04-25 14:02:22 +07:00
Adnan Zahir 4e7c0e64ad Merge branch 'codex/filter-improvement' into 'development'
feat: add more filters

See merge request mbugroup/lti-api!474
2026-04-25 12:27:46 +07:00
Adnan Zahir e79fde2408 feat: add more filters 2026-04-25 12:15:55 +07:00
Adnan Zahir 28394ee727 Merge branch 'cmd/auto-transfer-products-to-farm' into 'development'
cmd: two steps auto transfer

See merge request mbugroup/lti-api!472
2026-04-24 21:19:33 +07:00
Adnan Zahir 42c088e772 cmd: two steps auto transfer 2026-04-24 21:17:41 +07:00
Adnan Zahir df45803f8d Merge branch 'cmd/auto-transfer-products-to-farm' into 'development'
cmd: skip ambiguous farm then resolve double farm warehouses

See merge request mbugroup/lti-api!471
2026-04-24 20:18:59 +07:00
Adnan Zahir 6033535894 cmd: skip ambiguous farm then resolve double farm warehouses 2026-04-24 17:37:04 +07:00
Adnan Zahir 62723e5ab2 Merge branch 'cmd/auto-transfer-products-to-farm' into 'development'
cmd: add disticnt farm warehouse validation

See merge request mbugroup/lti-api!470
2026-04-24 15:57:42 +07:00
Adnan Zahir f1d7966e2f cmd: add disticnt farm warehouse validation 2026-04-24 15:55:59 +07:00
Adnan Zahir b94b16ab73 Merge branch 'cmd/auto-transfer-products-to-farm' into 'development'
cmd: new command to auto transfer leftover stocks from kandang to farm

See merge request mbugroup/lti-api!468
2026-04-24 13:59:44 +07:00
Adnan Zahir 9fd9c441d3 cmd: new command to auto transfer leftover stocks from kandang to farm 2026-04-24 13:58:11 +07:00
Adnan Zahir f5d986bf27 Merge branch 'fix/hppv2-egg-production-double-count' into 'development'
fix: change pakan cutover flag to PAKAN

See merge request mbugroup/lti-api!466
2026-04-24 13:09:41 +07:00
Adnan Zahir 1e9a51b8cd fix: change pakan cutover flag to PAKAN 2026-04-24 13:06:57 +07:00
Adnan Zahir 512d616867 Merge branch 'fix/hppv2-egg-production-double-count' into 'development'
fix: remove date filter when getting adjustment eggs

See merge request mbugroup/lti-api!465
2026-04-24 11:52:20 +07:00
Adnan Zahir 50ce780c2c fix: remove date filter when getting adjustment eggs 2026-04-24 11:51:23 +07:00
Adnan Zahir bacc7217b3 Merge branch 'fix/hppv2-egg-production-double-count' into 'development'
fix: add debug_values

See merge request mbugroup/lti-api!464
2026-04-24 11:40:29 +07:00
Adnan Zahir 878b8ccce9 fix: add debug_values 2026-04-24 11:39:12 +07:00
Adnan Zahir 5c39237ac0 Merge branch 'fix/hppv2-egg-production-double-count' into 'development'
Fix/hppv2 egg production double count

See merge request mbugroup/lti-api!463
2026-04-24 03:22:56 +07:00
Adnan Zahir 0fc6096637 fix: remove join query between adjustment_stocks and recording (egg) 2026-04-24 03:22:21 +07:00
Adnan Zahir 8389bd579b fix: revert removal of adjustment egg weight 2026-04-24 03:10:24 +07:00
Adnan Zahir cf9e60c64d Merge branch 'fix/hppv2-egg-production-double-count' into 'development'
fix: attempt on fixing double counted egg in hpp v2 calculation

See merge request mbugroup/lti-api!462
2026-04-24 02:37:59 +07:00
Adnan Zahir 2dcfd9efde fix: attemnt on fixing double counted egg in hpp v2 calculation 2026-04-24 02:33:48 +07:00
Adnan Zahir adcd4dc8fb Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: check validate and fix bug risks in commands

See merge request mbugroup/lti-api!460
2026-04-24 01:49:27 +07:00
Adnan Zahir 9a19bff8d3 cmd: check validate and fix bug risks in commands 2026-04-24 01:48:30 +07:00
Adnan Zahir 596b5ed2fb Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: broaden product_warehouse_id relation normalization

See merge request mbugroup/lti-api!459
2026-04-24 01:38:22 +07:00
Adnan Zahir 20479c2ca2 cmd: broaden product_warehouse_id relation normalization 2026-04-24 01:36:58 +07:00
Adnan Zahir a79213882d Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: fix unclosed

See merge request mbugroup/lti-api!458
2026-04-24 01:31:50 +07:00
Adnan Zahir befb351e49 cmd: fix unclosed 2026-04-24 01:30:43 +07:00
Adnan Zahir 5d38dfac69 Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: fix aggregation is not allowed in FILTER

See merge request mbugroup/lti-api!457
2026-04-24 01:24:30 +07:00
Adnan Zahir 26a04d3bdd cmd: fix aggregation is not allowed in FILTER 2026-04-24 01:23:27 +07:00
Adnan Zahir 044649abc9 Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: post-consolidation command for duplicate product_warehouses

See merge request mbugroup/lti-api!456
2026-04-24 01:20:39 +07:00
Adnan Zahir 83eab9de17 cmd: post-consolidation command for duplicate product_warehouses 2026-04-24 01:18:10 +07:00
Adnan Zahir 940899dad8 Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: still invalid field

See merge request mbugroup/lti-api!455
2026-04-24 01:06:54 +07:00
Adnan Zahir ffccd6daee cmd: still invalid field 2026-04-24 01:05:38 +07:00
Adnan Zahir 7f9604bcb4 Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: invalid field error

See merge request mbugroup/lti-api!454
2026-04-24 01:01:34 +07:00
Adnan Zahir e5e090aefe cmd: invalid field error 2026-04-24 01:00:53 +07:00
Adnan Zahir 552de15c1a Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: fix id is ambigous

See merge request mbugroup/lti-api!453
2026-04-24 00:57:35 +07:00
Adnan Zahir 8e396d758f cmd: fix id is ambigous 2026-04-24 00:56:56 +07:00
Adnan Zahir 84b76a4b2d Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: fix wrong parameter parsing

See merge request mbugroup/lti-api!452
2026-04-24 00:55:05 +07:00
Adnan Zahir 7c4664844c cmd: fix wrong parameter parsing 2026-04-24 00:54:20 +07:00
Adnan Zahir 47d2371b7f Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: only check blocked references if delete kandang warehouse flag enabled

See merge request mbugroup/lti-api!451
2026-04-24 00:46:56 +07:00
Adnan Zahir 5bfd93e300 cmd: only check blocked references if delete kandang warehouse flag enabled 2026-04-24 00:45:34 +07:00
Adnan Zahir 3fa75e0a31 Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: skip consolidation for incomplete locations (locations without farm-level warehouse)

See merge request mbugroup/lti-api!450
2026-04-24 00:27:36 +07:00
Adnan Zahir 101a83cd9e cmd: skip consolidation for incomplete locations (locations without farm-level warehouse) 2026-04-24 00:25:38 +07:00
Adnan Zahir 13fc537171 Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: resolve active allocation repointment

See merge request mbugroup/lti-api!449
2026-04-23 23:33:12 +07:00
Adnan Zahir 46737e4a96 cmd: resolve active allocation repointment 2026-04-23 23:31:39 +07:00
Adnan Zahir 06a23ce1b5 Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: add flag to bypass strict repointment

See merge request mbugroup/lti-api!448
2026-04-23 22:53:27 +07:00
Adnan Zahir fb9915759b cmd: add flag to bypass strict repointment 2026-04-23 22:52:44 +07:00
Adnan Zahir f0e364f884 Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'
cmd: p.type doesn't exist

See merge request mbugroup/lti-api!447
2026-04-23 22:43:01 +07:00
Adnan Zahir 65299d7913 cmd: p.type doesn't exist 2026-04-23 22:41:32 +07: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
119 changed files with 5630 additions and 525 deletions
+18 -4
View File
@@ -27,10 +27,24 @@ 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"
if [ -n "${AWS_SESSION_TOKEN:-}" ]; then HAS_SECRET_KEY="false"
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN" 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
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
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 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 \
BIN
View File
Binary file not shown.
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,601 @@
package main
import (
"context"
"errors"
"strings"
"testing"
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
transferSvc "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/transfers/services"
)
// ── Fake executor ─────────────────────────────────────────────────────────────
type fakeSystemTransferExecutor struct {
createRequests []*transferSvc.SystemTransferRequest
createResponses []*entity.StockTransfer
createErrors []error
deletedTransferIDs []uint
deleteErrors map[uint]error
}
func (f *fakeSystemTransferExecutor) CreateSystemTransfer(_ context.Context, req *transferSvc.SystemTransferRequest) (*entity.StockTransfer, error) {
f.createRequests = append(f.createRequests, req)
idx := len(f.createRequests) - 1
if idx < len(f.createErrors) && f.createErrors[idx] != nil {
return nil, f.createErrors[idx]
}
if idx < len(f.createResponses) && f.createResponses[idx] != nil {
return f.createResponses[idx], nil
}
return &entity.StockTransfer{Id: uint64(1000 + idx), MovementNumber: "PND-LTI-DEFAULT"}, nil
}
func (f *fakeSystemTransferExecutor) DeleteSystemTransfer(_ context.Context, id uint, _ uint) error {
f.deletedTransferIDs = append(f.deletedTransferIDs, id)
if f.deleteErrors != nil {
return f.deleteErrors[id]
}
return nil
}
// ── applyFarmWarehouseOverride ────────────────────────────────────────────────
func TestApplyOverrideResolvesMultiFarmLocation(t *testing.T) {
farmMap := map[uint]farmWarehouseInfo{
10: {
LocationID: 10,
LocationName: "Cijangkar",
AllFarm: []farmWarehouseEntry{
{ID: 50, Name: "Farm A"},
{ID: 51, Name: "Farm B"},
},
},
}
if err := applyFarmWarehouseOverride(farmMap, 51); err != nil {
t.Fatalf("unexpected error: %v", err)
}
info := farmMap[10]
if info.ChosenID != 51 {
t.Errorf("expected ChosenID=51, got %d", info.ChosenID)
}
if info.ChosenName != "Farm B" {
t.Errorf("expected ChosenName=Farm B, got %s", info.ChosenName)
}
if len(info.OtherFarm) != 1 || info.OtherFarm[0].ID != 50 {
t.Errorf("expected OtherFarm=[Farm A], got %+v", info.OtherFarm)
}
}
func TestApplyOverrideErrorsWhenIDNotInAllFarm(t *testing.T) {
farmMap := map[uint]farmWarehouseInfo{
10: {
LocationID: 10,
LocationName: "Cijangkar",
AllFarm: []farmWarehouseEntry{
{ID: 50, Name: "Farm A"},
{ID: 51, Name: "Farm B"},
},
},
}
err := applyFarmWarehouseOverride(farmMap, 99)
if err == nil {
t.Fatal("expected error for unknown warehouse id, got nil")
}
if !strings.Contains(err.Error(), "99") {
t.Errorf("error should mention the invalid id, got: %v", err)
}
if !strings.Contains(err.Error(), "Farm A") || !strings.Contains(err.Error(), "Farm B") {
t.Errorf("error should list available warehouses, got: %v", err)
}
}
func TestApplyOverrideIgnoresSingleFarmLocations(t *testing.T) {
farmMap := map[uint]farmWarehouseInfo{
10: {
LocationID: 10,
LocationName: "Jamali",
AllFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}},
ChosenID: 50,
ChosenName: "Farm A",
},
}
// Override ID 50 is present, but there is only 1 farm; the function should
// not touch this location (no OtherFarm to populate).
if err := applyFarmWarehouseOverride(farmMap, 50); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(farmMap[10].OtherFarm) != 0 {
t.Errorf("expected no OtherFarm for single-farm location, got %+v", farmMap[10].OtherFarm)
}
}
func TestApplyOverrideNoopWhenZero(t *testing.T) {
farmMap := map[uint]farmWarehouseInfo{
10: {
LocationID: 10,
LocationName: "Cijangkar",
AllFarm: []farmWarehouseEntry{
{ID: 50, Name: "Farm A"},
{ID: 51, Name: "Farm B"},
},
},
}
if err := applyFarmWarehouseOverride(farmMap, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if farmMap[10].ChosenID != 0 {
t.Errorf("expected ChosenID unchanged (0), got %d", farmMap[10].ChosenID)
}
}
// ── listUnresolvedLocations ───────────────────────────────────────────────────
func TestListUnresolvedLocationsReturnsOnlyAmbiguous(t *testing.T) {
farmMap := map[uint]farmWarehouseInfo{
1: {LocationID: 1, LocationName: "Jamali", AllFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}}, ChosenID: 50},
2: {
LocationID: 2,
LocationName: "Cijangkar",
AllFarm: []farmWarehouseEntry{{ID: 60, Name: "Farm X"}, {ID: 61, Name: "Farm Y"}},
// ChosenID = 0: unresolved
},
3: {LocationID: 3, LocationName: "Tamansari", AllFarm: nil}, // no farm at all, not an error here
}
msgs := listUnresolvedLocations(farmMap)
if len(msgs) != 1 {
t.Fatalf("expected 1 unresolved message, got %d: %v", len(msgs), msgs)
}
if !strings.Contains(msgs[0], "Cijangkar") {
t.Errorf("message should name the ambiguous location, got: %s", msgs[0])
}
if !strings.Contains(msgs[0], "Farm X") || !strings.Contains(msgs[0], "Farm Y") {
t.Errorf("message should list available warehouses, got: %s", msgs[0])
}
}
// ── buildTransferPlan — kandang source ───────────────────────────────────────
func TestBuildPlanKandangEligibleGroupedByWarehousePair(t *testing.T) {
opts := &commandOptions{RunID: "test-run"}
farmMap := map[uint]farmWarehouseInfo{
10: {LocationID: 10, LocationName: "Jamali", AllFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}}, ChosenID: 50, ChosenName: "Farm A"},
}
stocks := []kandangStockRow{
{LocationID: 10, LocationName: "Jamali", SourceWarehouseID: 20, SourceWarehouseName: "K1", ProductID: 1, ProductName: "Pakan A", OnHandQty: 100, LeftoverQty: 100, SourceType: sourceTypeKandang},
{LocationID: 10, LocationName: "Jamali", SourceWarehouseID: 20, SourceWarehouseName: "K1", ProductID: 2, ProductName: "OVK B", OnHandQty: 50, AllocatedQty: 10, LeftoverQty: 40, SourceType: sourceTypeKandang},
}
reportRows, groups := buildTransferPlan(opts, farmMap, stocks)
if len(groups) != 1 || len(groups[0].Rows) != 2 {
t.Fatalf("expected 1 group with 2 rows, got %d groups", len(groups))
}
if groups[0].SourceType != sourceTypeKandang {
t.Errorf("expected group source type kandang_to_farm, got %s", groups[0].SourceType)
}
for _, row := range reportRows {
if row.Status != "eligible" {
t.Errorf("expected eligible, got %s for %s", row.Status, row.ProductName)
}
}
if reportRows[1].Qty != 40 {
t.Errorf("expected leftover qty 40 for OVK B, got %.3f", reportRows[1].Qty)
}
}
func TestBuildPlanSkipsMissingFarmWarehouse(t *testing.T) {
opts := &commandOptions{RunID: "test-run"}
farmMap := map[uint]farmWarehouseInfo{
10: {LocationID: 10, LocationName: "Jamali", AllFarm: nil},
}
stocks := []kandangStockRow{
{LocationID: 10, SourceWarehouseID: 20, ProductID: 1, ProductName: "Pakan A", OnHandQty: 100, LeftoverQty: 100, SourceType: sourceTypeKandang},
}
reportRows, groups := buildTransferPlan(opts, farmMap, stocks)
if len(groups) != 0 {
t.Fatalf("expected no groups, got %d", len(groups))
}
if reportRows[0].Status != "skipped" || reportRows[0].Reason != "missing_farm_warehouse" {
t.Errorf("unexpected: %s / %s", reportRows[0].Status, reportRows[0].Reason)
}
}
func TestBuildPlanErrorForUnresolvedMultiFarmWarehouse(t *testing.T) {
opts := &commandOptions{RunID: "test-run"}
farmMap := map[uint]farmWarehouseInfo{
10: {
LocationID: 10,
LocationName: "Cijangkar",
AllFarm: []farmWarehouseEntry{{ID: 60, Name: "Farm X"}, {ID: 61, Name: "Farm Y"}},
// ChosenID = 0: unresolved
},
}
stocks := []kandangStockRow{
{LocationID: 10, LocationName: "Cijangkar", SourceWarehouseID: 21, ProductID: 3, ProductName: "Pakan C", OnHandQty: 200, LeftoverQty: 200, SourceType: sourceTypeKandang},
}
reportRows, groups := buildTransferPlan(opts, farmMap, stocks)
if len(groups) != 0 {
t.Fatalf("expected no groups, got %d", len(groups))
}
if reportRows[0].Status != "error" {
t.Errorf("expected error status, got %s", reportRows[0].Status)
}
if !strings.Contains(reportRows[0].Reason, "multiple_farm_warehouses") {
t.Errorf("reason should mention multiple_farm_warehouses, got: %s", reportRows[0].Reason)
}
// The error message must list the available warehouses so the operator knows
// which --farm-warehouse-id to use.
if !strings.Contains(reportRows[0].Reason, "Farm X") || !strings.Contains(reportRows[0].Reason, "Farm Y") {
t.Errorf("reason should list available warehouses, got: %s", reportRows[0].Reason)
}
}
func TestBuildPlanSkipsFullyAllocatedKandangStock(t *testing.T) {
opts := &commandOptions{RunID: "test-run"}
farmMap := map[uint]farmWarehouseInfo{
10: {LocationID: 10, AllFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}}, ChosenID: 50, ChosenName: "Farm A"},
}
stocks := []kandangStockRow{
{LocationID: 10, SourceWarehouseID: 20, ProductID: 1, ProductName: "Pakan A", OnHandQty: 100, AllocatedQty: 100, LeftoverQty: 0, SourceType: sourceTypeKandang},
{LocationID: 10, SourceWarehouseID: 20, ProductID: 2, ProductName: "OVK B", OnHandQty: 80, AllocatedQty: 30, LeftoverQty: 50, SourceType: sourceTypeKandang},
}
_, groups := buildTransferPlan(opts, farmMap, stocks)
if len(groups) != 1 || len(groups[0].Rows) != 1 {
t.Fatalf("expected 1 group with 1 eligible row, got groups=%d", len(groups))
}
if groups[0].Rows[0].ProductName != "OVK B" {
t.Errorf("expected only OVK B to be eligible, got %s", groups[0].Rows[0].ProductName)
}
}
func TestBuildPlanSkipAmbiguousDowngradesErrorToSkipped(t *testing.T) {
opts := &commandOptions{RunID: "test-run", SkipAmbiguous: true}
farmMap := map[uint]farmWarehouseInfo{
10: {
LocationID: 10,
LocationName: "Cijangkar",
AllFarm: []farmWarehouseEntry{{ID: 60, Name: "Farm X"}, {ID: 61, Name: "Farm Y"}},
// ChosenID = 0: unresolved
},
11: {
LocationID: 11,
LocationName: "Jamali",
AllFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}},
ChosenID: 50,
ChosenName: "Farm A",
},
}
stocks := []kandangStockRow{
{LocationID: 10, LocationName: "Cijangkar", SourceWarehouseID: 21, ProductID: 3, ProductName: "Pakan C", OnHandQty: 200, LeftoverQty: 200, SourceType: sourceTypeKandang},
{LocationID: 11, LocationName: "Jamali", SourceWarehouseID: 20, ProductID: 1, ProductName: "Pakan A", OnHandQty: 100, LeftoverQty: 100, SourceType: sourceTypeKandang},
}
reportRows, groups := buildTransferPlan(opts, farmMap, stocks)
// Ambiguous location must be skipped, not error.
ambiguous := reportRows[0]
if ambiguous.LocationName != "Cijangkar" {
t.Fatalf("expected first row to be Cijangkar, got %s", ambiguous.LocationName)
}
if ambiguous.Status != "skipped" {
t.Errorf("expected skipped with --skip-ambiguous, got %s", ambiguous.Status)
}
if !strings.Contains(ambiguous.Reason, "multiple_farm_warehouses") {
t.Errorf("reason should still explain the cause, got: %s", ambiguous.Reason)
}
// Unambiguous location must still be eligible and grouped.
if len(groups) != 1 || groups[0].LocationName != "Jamali" {
t.Errorf("expected 1 group for Jamali, got %d groups", len(groups))
}
}
// ── applyFlagFilter (unit-level, via buildTransferPlan) ───────────────────────
// applyFlagFilter is a DB-level filter so we test its effect indirectly: the
// flag filter is applied before rows reach buildTransferPlan, so we simulate
// by only passing stock rows that the query would have returned.
// The real guard is that loadKandangLeftoverStocks receives the filtered set.
// Here we verify that buildTransferPlan itself is agnostic to the filter and
// simply processes whatever rows it is given.
func TestBuildPlanOnlyTransfersRowsPassedToIt(t *testing.T) {
opts := &commandOptions{RunID: "test-run", FlagFilter: []string{"PAKAN"}}
farmMap := map[uint]farmWarehouseInfo{
10: {LocationID: 10, AllFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}}, ChosenID: 50, ChosenName: "Farm A"},
}
// Simulate: only PAKAN products survived the DB filter; OVK was excluded.
stocks := []kandangStockRow{
{LocationID: 10, SourceWarehouseID: 20, ProductID: 1, ProductName: "Pakan Broiler", OnHandQty: 100, LeftoverQty: 100, SourceType: sourceTypeKandang},
}
_, groups := buildTransferPlan(opts, farmMap, stocks)
if len(groups) != 1 || len(groups[0].Rows) != 1 {
t.Fatalf("expected 1 group with 1 row, got %d groups", len(groups))
}
if groups[0].Rows[0].ProductName != "Pakan Broiler" {
t.Errorf("unexpected product: %s", groups[0].Rows[0].ProductName)
}
}
// ── buildTransferPlan — farm_consolidation source ────────────────────────────
func TestBuildPlanFarmConsolidationCreatesOwnGroup(t *testing.T) {
opts := &commandOptions{RunID: "test-run"}
// Location 10 has 2 farm warehouses; Farm B (id=51) was chosen, Farm A
// (id=50) is OtherFarm whose stocks need consolidating.
farmMap := map[uint]farmWarehouseInfo{
10: {
LocationID: 10,
LocationName: "Cijangkar",
AllFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}, {ID: 51, Name: "Farm B"}},
ChosenID: 51,
ChosenName: "Farm B",
OtherFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}},
},
}
// Extra farm stock from Farm A + normal kandang stock.
stocks := []kandangStockRow{
{LocationID: 10, LocationName: "Cijangkar", SourceWarehouseID: 20, SourceWarehouseName: "Kandang K1", ProductID: 1, ProductName: "Pakan A", OnHandQty: 100, LeftoverQty: 100, SourceType: sourceTypeKandang},
{LocationID: 10, LocationName: "Cijangkar", SourceWarehouseID: 50, SourceWarehouseName: "Farm A", ProductID: 2, ProductName: "OVK B", OnHandQty: 60, LeftoverQty: 60, SourceType: sourceTypeFarmConsol},
}
reportRows, groups := buildTransferPlan(opts, farmMap, stocks)
if len(groups) != 2 {
t.Fatalf("expected 2 groups (one per source warehouse), got %d", len(groups))
}
if len(reportRows) != 2 {
t.Fatalf("expected 2 report rows, got %d", len(reportRows))
}
groupsBySource := make(map[uint]*transferGroup, 2)
for i := range groups {
groupsBySource[groups[i].SourceWarehouseID] = &groups[i]
}
kandangGroup := groupsBySource[20]
if kandangGroup == nil {
t.Fatal("expected a group with SourceWarehouseID=20")
}
if kandangGroup.SourceType != sourceTypeKandang {
t.Errorf("expected kandang_to_farm group, got %s", kandangGroup.SourceType)
}
if kandangGroup.FarmWarehouseID != 51 {
t.Errorf("expected kandang group to target Farm B (51), got %d", kandangGroup.FarmWarehouseID)
}
consolGroup := groupsBySource[50]
if consolGroup == nil {
t.Fatal("expected a consolidation group with SourceWarehouseID=50")
}
if consolGroup.SourceType != sourceTypeFarmConsol {
t.Errorf("expected farm_consolidation group, got %s", consolGroup.SourceType)
}
if consolGroup.FarmWarehouseID != 51 {
t.Errorf("expected consolidation group to target Farm B (51), got %d", consolGroup.FarmWarehouseID)
}
}
func TestBuildPlanFarmConsolidationSkipsZeroLeftover(t *testing.T) {
opts := &commandOptions{RunID: "test-run"}
farmMap := map[uint]farmWarehouseInfo{
10: {LocationID: 10, AllFarm: []farmWarehouseEntry{{ID: 50}, {ID: 51}}, ChosenID: 51, ChosenName: "Farm B", OtherFarm: []farmWarehouseEntry{{ID: 50, Name: "Farm A"}}},
}
stocks := []kandangStockRow{
{LocationID: 10, SourceWarehouseID: 50, ProductID: 1, ProductName: "Pakan A", OnHandQty: 100, AllocatedQty: 100, LeftoverQty: 0, SourceType: sourceTypeFarmConsol},
}
reportRows, groups := buildTransferPlan(opts, farmMap, stocks)
if len(groups) != 0 {
t.Fatalf("expected no groups, got %d", len(groups))
}
if reportRows[0].Status != "skipped" {
t.Errorf("expected skipped, got %s", reportRows[0].Status)
}
}
// ── executeApply ──────────────────────────────────────────────────────────────
func TestExecuteApplyTagsReasonAndNotesWithSourceType(t *testing.T) {
date := time.Date(2026, 4, 24, 0, 0, 0, 0, time.UTC)
opts := &commandOptions{RunID: "run-apply", TransferDate: date, ActorID: 99}
groups := []transferGroup{
{
SourceType: sourceTypeKandang,
LocationName: "Jamali",
SourceWarehouseID: 20,
SourceWarehouseName: "K1",
FarmWarehouseID: 50,
FarmWarehouseName: "Farm A",
Rows: []*transferReportRow{{ProductID: 1, ProductName: "Pakan A", Qty: 100}},
},
{
SourceType: sourceTypeFarmConsol,
LocationName: "Cijangkar",
SourceWarehouseID: 60,
SourceWarehouseName: "Farm X",
FarmWarehouseID: 61,
FarmWarehouseName: "Farm Y",
Rows: []*transferReportRow{{ProductID: 2, ProductName: "OVK B", Qty: 40}},
},
}
executor := &fakeSystemTransferExecutor{}
summary, err := executeApply(context.Background(), executor, opts, groups)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if summary.GroupsApplied != 2 {
t.Errorf("expected 2 groups applied, got %d", summary.GroupsApplied)
}
if len(executor.createRequests) != 2 {
t.Fatalf("expected 2 create requests, got %d", len(executor.createRequests))
}
// Both requests must carry the run_id in the reason for rollback to work.
for i, req := range executor.createRequests {
if !strings.Contains(req.TransferReason, "run_id=run-apply") {
t.Errorf("request %d reason missing run_id: %s", i, req.TransferReason)
}
}
// Notes for farm_consolidation should be distinct from kandang_to_farm.
if !strings.Contains(executor.createRequests[0].StockLogNotes, "kandang to farm") {
t.Errorf("kandang group notes should say 'kandang to farm', got: %s", executor.createRequests[0].StockLogNotes)
}
if !strings.Contains(executor.createRequests[1].StockLogNotes, "consolidation") {
t.Errorf("consolidation group notes should say 'consolidation', got: %s", executor.createRequests[1].StockLogNotes)
}
}
func TestExecuteApplyCreatesTransferWithCorrectProductsAndRecordsTransferID(t *testing.T) {
date := time.Date(2026, 4, 24, 0, 0, 0, 0, time.UTC)
opts := &commandOptions{RunID: "run-1", TransferDate: date, ActorID: 1}
row1 := &transferReportRow{ProductID: 1, ProductName: "Pakan A", Qty: 100}
row2 := &transferReportRow{ProductID: 2, ProductName: "OVK B", Qty: 40}
groups := []transferGroup{
{
SourceType: sourceTypeKandang, SourceWarehouseID: 20, FarmWarehouseID: 50,
Rows: []*transferReportRow{row1, row2},
},
}
executor := &fakeSystemTransferExecutor{
createResponses: []*entity.StockTransfer{{Id: 1001, MovementNumber: "PND-LTI-1001"}},
}
_, err := executeApply(context.Background(), executor, opts, groups)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if row1.Status != "applied" || row2.Status != "applied" {
t.Errorf("both rows should be applied: %s, %s", row1.Status, row2.Status)
}
if row1.TransferID == nil || *row1.TransferID != 1001 {
t.Errorf("expected transfer id 1001, got %+v", row1.TransferID)
}
if row1.MovementNumber == nil || *row1.MovementNumber != "PND-LTI-1001" {
t.Errorf("expected movement number PND-LTI-1001, got %+v", row1.MovementNumber)
}
// Verify both products were included in the create request.
if len(executor.createRequests[0].Products) != 2 {
t.Errorf("expected 2 products in request, got %d", len(executor.createRequests[0].Products))
}
}
// ── executeRollback ───────────────────────────────────────────────────────────
func TestExecuteRollbackDeletesDescendingAndMarksStatuses(t *testing.T) {
executor := &fakeSystemTransferExecutor{
deleteErrors: map[uint]error{200: errors.New("already consumed")},
}
rows := []rollbackDetailRow{
{TransferID: 100, ProductName: "Pakan A"},
{TransferID: 200, ProductName: "OVK B"},
{TransferID: 100, ProductName: "Pakan C"},
}
err := executeRollback(context.Background(), executor, rows, 99)
if err == nil || !strings.Contains(err.Error(), "already consumed") {
t.Fatalf("expected error for transfer 200, got: %v", err)
}
if executor.deletedTransferIDs[0] != 200 || executor.deletedTransferIDs[1] != 100 {
t.Fatalf("expected delete order [200 100], got %v", executor.deletedTransferIDs)
}
if rows[0].Status != "rolled_back" || rows[2].Status != "rolled_back" {
t.Errorf("transfer 100 rows must be rolled_back: %+v", rows)
}
if rows[1].Status != "failed" {
t.Errorf("transfer 200 row must be failed: %+v", rows[1])
}
}
func TestExecuteRollbackRequiresActorID(t *testing.T) {
err := executeRollback(context.Background(), &fakeSystemTransferExecutor{}, []rollbackDetailRow{{TransferID: 1}}, 0)
if err == nil || !strings.Contains(err.Error(), "actor-id") {
t.Fatalf("expected actor-id error, got: %v", err)
}
}
// ── buildTransferReason / buildRunReasonMatcher ───────────────────────────────
func TestBuildTransferReasonMatchesRunReasonMatcher(t *testing.T) {
runID := "product-farm-transfer-20260424T120000.000000000Z"
date := time.Date(2026, 4, 24, 0, 0, 0, 0, time.UTC)
reason := buildTransferReason(runID, "Jamali", "Gudang K1", "Gudang Farm Jamali", date)
needle := strings.TrimSuffix(buildRunReasonMatcher(runID), "%")
if !strings.HasPrefix(reason, needle) {
t.Errorf("reason %q does not match matcher prefix %q", reason, needle)
}
}
func TestBuildTransferReasonSanitizesPipes(t *testing.T) {
reason := buildTransferReason("run-1", "Lok|asi", "Gudang|K1", "Farm|WH", time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC))
parts := strings.Split(reason, "|")
// prefix + 5 key=value segments = 6 parts
if len(parts) != 6 {
t.Errorf("expected 6 pipe-separated segments, got %d: %v", len(parts), parts)
}
}
func TestBuildStockLogNotesContainsSourceTypeHint(t *testing.T) {
date := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
kandangNote := buildStockLogNotes("r", "Loc", "Src", "Dst", date, sourceTypeKandang)
consolNote := buildStockLogNotes("r", "Loc", "Src", "Dst", date, sourceTypeFarmConsol)
if !strings.Contains(kandangNote, "kandang to farm") {
t.Errorf("kandang note should mention 'kandang to farm': %s", kandangNote)
}
if !strings.Contains(consolNote, "consolidation") {
t.Errorf("consolidation note should mention 'consolidation': %s", consolNote)
}
}
// ── summarizeReport ───────────────────────────────────────────────────────────
func TestSummarizeReportCountsAllStatuses(t *testing.T) {
rows := []transferReportRow{
{Status: "eligible"},
{Status: "applied"},
{Status: "applied"},
{Status: "skipped"},
{Status: "error"},
{Status: "failed"},
}
groups := []transferGroup{{}, {}}
s := summarizeReport(rows, groups, 1)
if s.RowsPlanned != 4 { // eligible + 2 applied + 1 failed
t.Errorf("expected RowsPlanned=4, got %d", s.RowsPlanned)
}
if s.RowsApplied != 2 {
t.Errorf("expected RowsApplied=2, got %d", s.RowsApplied)
}
if s.RowsSkipped != 1 {
t.Errorf("expected RowsSkipped=1, got %d", s.RowsSkipped)
}
if s.RowsError != 1 {
t.Errorf("expected RowsError=1, got %d", s.RowsError)
}
if s.RowsFailed != 1 {
t.Errorf("expected RowsFailed=1, got %d", s.RowsFailed)
}
if s.GroupsPlanned != 2 || s.GroupsApplied != 1 {
t.Errorf("unexpected group counts: %+v", s)
}
}
@@ -0,0 +1,436 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
"gitlab.com/mbugroup/lti-api.git/internal/config"
"gitlab.com/mbugroup/lti-api.git/internal/database"
"gorm.io/gorm"
)
const (
outputTable = "table"
outputJSON = "json"
)
type options struct {
Apply bool
Output string
DBSSLMode string
AreaName string
}
type duplicateGroup struct {
WarehouseID uint `json:"warehouse_id"`
WarehouseName string `json:"warehouse_name"`
ProductID uint `json:"product_id"`
ProductName string `json:"product_name"`
AreaName string `json:"area_name"`
LocationName string `json:"location_name"`
ProjectFlockKandangID *uint `json:"project_flock_kandang_id,omitempty"`
SurvivorID uint `json:"survivor_id"`
SurvivorQty float64 `json:"survivor_qty"`
AbsorbedCount int `json:"absorbed_count"`
TotalMergedQty float64 `json:"total_merged_qty"`
AbsorbedIDs string `json:"absorbed_ids"`
}
type consolidateSummary struct {
TotalDuplicateGroups int `json:"total_duplicate_groups"`
TotalProductWarehouses int64 `json:"total_product_warehouses"`
UpdatedReferences map[string]int64 `json:"updated_references,omitempty"`
DeletedProductWarehouses int64 `json:"deleted_product_warehouses,omitempty"`
OverallStatus string `json:"overall_status"`
}
func main() {
opts, err := parseFlags()
if err != nil {
log.Fatalf("invalid flags: %v", err)
}
if opts.DBSSLMode != "" {
config.DBSSLMode = opts.DBSSLMode
}
ctx := context.Background()
db := database.Connect(config.DBHost, config.DBName)
// Find duplicate groups
groups, err := findDuplicateProductWarehouses(ctx, db, opts)
if err != nil {
log.Fatalf("failed to find duplicates: %v", err)
}
if len(groups) == 0 {
fmt.Println("No duplicate product_warehouses found")
return
}
summary := summarizeGroups(groups)
if !opts.Apply {
renderConsolidation(opts.Output, groups, summary)
return
}
applied, err := applyConsolidation(ctx, db, groups)
if err != nil {
log.Fatalf("apply failed: %v", err)
}
renderConsolidation(opts.Output, groups, applied)
}
func parseFlags() (*options, error) {
var opts options
flag.BoolVar(&opts.Apply, "apply", false, "Apply consolidation (omit for dry-run)")
flag.StringVar(&opts.Output, "output", outputTable, "Output format: table or json")
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Database sslmode override")
flag.StringVar(&opts.AreaName, "area-name", "", "Optional area filter")
flag.Parse()
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
opts.AreaName = strings.TrimSpace(opts.AreaName)
opts.DBSSLMode = strings.TrimSpace(opts.DBSSLMode)
if opts.Output == "" {
opts.Output = outputTable
}
if opts.Output != outputTable && opts.Output != outputJSON {
return nil, fmt.Errorf("unsupported --output=%s", opts.Output)
}
return &opts, nil
}
func findDuplicateProductWarehouses(ctx context.Context, db *gorm.DB, opts *options) ([]duplicateGroup, error) {
filters := ""
args := []any{}
if opts.AreaName != "" {
filters = "WHERE a.name = ?"
args = append(args, opts.AreaName)
}
query := fmt.Sprintf(`
WITH duplicates AS (
SELECT
pw.warehouse_id,
w.name AS warehouse_name,
pw.product_id,
p.name AS product_name,
COALESCE(a.name, 'N/A') AS area_name,
COALESCE(l.name, 'N/A') AS location_name,
pw.project_flock_kandang_id,
pw.id,
pw.qty,
MIN(pw.id) OVER (PARTITION BY pw.warehouse_id, pw.product_id) AS survivor_id,
COUNT(*) OVER (PARTITION BY pw.warehouse_id, pw.product_id) AS duplicate_count,
SUM(pw.qty) OVER (PARTITION BY pw.warehouse_id, pw.product_id) AS total_qty
FROM product_warehouses pw
JOIN warehouses w ON w.id = pw.warehouse_id
JOIN products p ON p.id = pw.product_id
LEFT JOIN locations l ON l.id = w.location_id
LEFT JOIN areas a ON a.id = l.area_id
%s
)
SELECT
warehouse_id,
warehouse_name,
product_id,
product_name,
area_name,
location_name,
(SELECT project_flock_kandang_id FROM duplicates d2 WHERE d2.id = survivor_id LIMIT 1) AS project_flock_kandang_id,
survivor_id,
(SELECT qty FROM duplicates d2 WHERE d2.id = survivor_id LIMIT 1) AS survivor_qty,
duplicate_count - 1 AS absorbed_count,
total_qty AS total_merged_qty,
STRING_AGG(id::text, ', ' ORDER BY id::text) FILTER (WHERE id <> survivor_id) AS absorbed_ids
FROM duplicates
WHERE duplicate_count > 1
GROUP BY warehouse_id, warehouse_name, product_id, product_name, area_name, location_name, survivor_id, total_qty, duplicate_count
ORDER BY area_name, location_name, warehouse_name, product_name
`, filters)
rows := make([]duplicateGroup, 0)
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
return nil, err
}
return rows, nil
}
func applyConsolidation(ctx context.Context, db *gorm.DB, groups []duplicateGroup) (consolidateSummary, error) {
summary := consolidateSummary{
TotalDuplicateGroups: len(groups),
UpdatedReferences: make(map[string]int64),
OverallStatus: "PASS",
}
fifoSvc := commonSvc.NewFifoStockV2Service(db, nil)
err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
for _, group := range groups {
absorbedIDs := []uint{}
if group.AbsorbedIDs != "" {
parts := strings.Split(group.AbsorbedIDs, ", ")
for _, p := range parts {
var id uint
fmt.Sscanf(p, "%d", &id)
absorbedIDs = append(absorbedIDs, id)
}
}
if len(absorbedIDs) == 0 {
continue
}
// Update all references to point to survivor
refTables := []struct {
table string
column string
}{
{"stock_allocations", "product_warehouse_id"},
{"stock_logs", "product_warehouse_id"},
{"purchase_items", "product_warehouse_id"},
{"recording_stocks", "product_warehouse_id"},
{"recording_eggs", "product_warehouse_id"},
{"recording_depletions", "product_warehouse_id"},
{"recording_depletions", "source_product_warehouse_id"},
{"marketing_delivery_products", "product_warehouse_id"},
{"marketing_products", "product_warehouse_id"},
{"stock_transfer_details", "source_product_warehouse_id"},
{"stock_transfer_details", "dest_product_warehouse_id"},
{"adjustment_stocks", "product_warehouse_id"},
{"laying_transfer_sources", "product_warehouse_id"},
{"laying_transfer_targets", "product_warehouse_id"},
{"laying_transfers", "source_product_warehouse_id"},
{"project_chickin_details", "product_warehouse_id"},
{"project_chickins", "product_warehouse_id"},
{"project_flock_populations", "product_warehouse_id"},
{"fifo_stock_v2_operation_log", "product_warehouse_id"},
{"fifo_stock_v2_reflow_checkpoints", "product_warehouse_id"},
{"fifo_stock_v2_shadow_allocations", "product_warehouse_id"},
}
for _, ref := range refTables {
res := tx.WithContext(ctx).
Table(ref.table).
Where(fmt.Sprintf("%s IN ?", ref.column), absorbedIDs).
Update(ref.column, group.SurvivorID)
if res.Error != nil {
return fmt.Errorf("update %s.%s: %w", ref.table, ref.column, res.Error)
}
if res.RowsAffected > 0 {
summary.UpdatedReferences[ref.table+"."+ref.column] += res.RowsAffected
}
}
// Update survivor qty to merged total
res := tx.WithContext(ctx).
Table("product_warehouses").
Where("id = ?", group.SurvivorID).
Update("qty", group.TotalMergedQty)
if res.Error != nil {
return fmt.Errorf("update survivor qty: %w", res.Error)
}
// Clear project_flock_kandang_id for LOKASI warehouse survivors
if err := tx.WithContext(ctx).Exec(`
UPDATE product_warehouses pw
SET project_flock_kandang_id = NULL
FROM warehouses w
WHERE pw.warehouse_id = w.id
AND pw.id = ?
AND UPPER(w.type) = 'LOKASI'
AND pw.project_flock_kandang_id IS NOT NULL
`, group.SurvivorID).Error; err != nil {
return fmt.Errorf("clear project_flock_kandang_id survivor %d: %w", group.SurvivorID, err)
}
// Delete absorbed product_warehouses
res = tx.WithContext(ctx).
Table("product_warehouses").
Where("id IN ?", absorbedIDs).
Delete(nil)
if res.Error != nil {
return fmt.Errorf("delete absorbed: %w", res.Error)
}
summary.DeletedProductWarehouses += res.RowsAffected
// Recalculate stock_logs for survivor
if err := recalculateStockLogs(ctx, tx, []uint{group.SurvivorID}); err != nil {
return fmt.Errorf("recalculate stock_logs: %w", err)
}
// Reflow and recalculate FIFO
if err := reflowProductWarehouse(ctx, fifoSvc, tx, group.SurvivorID); err != nil {
return fmt.Errorf("reflow product_warehouse %d: %w", group.SurvivorID, err)
}
}
return nil
})
if err != nil {
summary.OverallStatus = "FAIL"
return summary, err
}
return summary, nil
}
func recalculateStockLogs(ctx context.Context, tx *gorm.DB, productWarehouseIDs []uint) error {
if len(productWarehouseIDs) == 0 {
return nil
}
query := `
WITH recalculated AS (
SELECT
id,
SUM(COALESCE(increase, 0) - COALESCE(decrease, 0))
OVER (PARTITION BY product_warehouse_id ORDER BY created_at ASC, id ASC) AS running_stock
FROM stock_logs
WHERE product_warehouse_id IN ?
)
UPDATE stock_logs sl
SET stock = recalculated.running_stock
FROM recalculated
WHERE sl.id = recalculated.id
`
return tx.WithContext(ctx).Exec(query, productWarehouseIDs).Error
}
func reflowProductWarehouse(ctx context.Context, fifoSvc commonSvc.FifoStockV2Service, tx *gorm.DB, productWarehouseID uint) error {
type row struct {
FlagGroupCode string `gorm:"column:flag_group_code"`
}
var selected row
err := tx.WithContext(ctx).
Table("fifo_stock_v2_route_rules rr").
Select("rr.flag_group_code").
Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE").
Where("rr.is_active = TRUE").
Where("rr.lane = ?", "STOCKABLE").
Where("rr.function_code = ?", "PURCHASE_IN").
Where("rr.source_table = ?", "purchase_items").
Where(`EXISTS (
SELECT 1
FROM product_warehouses pw
JOIN flags f ON f.flagable_id = pw.product_id
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
WHERE pw.id = ?
AND f.flagable_type = 'products'
AND fm.flag_group_code = rr.flag_group_code
)`, productWarehouseID).
Order("rr.id ASC").
Limit(1).
Take(&selected).Error
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
if err == gorm.ErrRecordNotFound {
return nil
}
flagGroupCode := strings.TrimSpace(selected.FlagGroupCode)
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
FlagGroupCode: flagGroupCode,
ProductWarehouseID: productWarehouseID,
Tx: tx,
}); err != nil {
return err
}
if _, err := fifoSvc.Recalculate(ctx, commonSvc.FifoStockV2RecalculateRequest{
ProductWarehouseIDs: []uint{productWarehouseID},
FlagGroupCodes: []string{flagGroupCode},
FixDrift: true,
Tx: tx,
}); err != nil {
return err
}
return nil
}
func summarizeGroups(groups []duplicateGroup) consolidateSummary {
var totalQty int64
for _, g := range groups {
totalQty += int64(g.AbsorbedCount)
}
return consolidateSummary{
TotalDuplicateGroups: len(groups),
TotalProductWarehouses: totalQty,
OverallStatus: "PASS",
}
}
func renderConsolidation(mode string, groups []duplicateGroup, summary consolidateSummary) {
if mode == outputJSON {
payload := map[string]any{
"groups": groups,
"summary": summary,
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
_ = enc.Encode(payload)
return
}
w := tabwriter.NewWriter(os.Stdout, 2, 8, 2, ' ', 0)
fmt.Fprintln(w, "AREA\tLOCATION\tWAREHOUSE\tPRODUCT\tPFK_ID\tSURVIVOR_ID\tSURVIVOR_QTY\tABSORBED_COUNT\tTOTAL_MERGED_QTY\tABSORBED_IDS")
for _, g := range groups {
pfkID := "-"
if g.ProjectFlockKandangID != nil {
pfkID = fmt.Sprintf("%d", *g.ProjectFlockKandangID)
}
fmt.Fprintf(
w,
"%s\t%s\t%s\t%s\t%s\t%d\t%.3f\t%d\t%.3f\t%s\n",
g.AreaName,
g.LocationName,
g.WarehouseName,
g.ProductName,
pfkID,
g.SurvivorID,
g.SurvivorQty,
g.AbsorbedCount,
g.TotalMergedQty,
g.AbsorbedIDs,
)
}
_ = w.Flush()
fmt.Printf("\n=== SUMMARY ===\n")
fmt.Printf("Duplicate groups found: %d\n", summary.TotalDuplicateGroups)
fmt.Printf("Product warehouses to delete: %d\n", summary.TotalProductWarehouses)
fmt.Printf("Overall status: %s\n", summary.OverallStatus)
if len(summary.UpdatedReferences) > 0 {
fmt.Println("\nUpdated references:")
keys := make([]string, 0, len(summary.UpdatedReferences))
for k := range summary.UpdatedReferences {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Printf(" %s=%d\n", k, summary.UpdatedReferences[k])
}
}
}
+37 -12
View File
@@ -26,12 +26,14 @@ const (
) )
type options struct { type options struct {
Apply bool Apply bool
Output string Output string
AreaName string AreaName string
KandangLocationName string KandangLocationName string
DBSSLMode string DBSSLMode string
DeleteKandangWarehouses bool DeleteKandangWarehouses bool
SkipBlockedRefsCheck bool
SkipIncompleteLocations bool
} }
type consolidateRow struct { type consolidateRow struct {
@@ -132,7 +134,7 @@ func main() {
log.Fatalf("failed to inspect warehouse references: %v", err) log.Fatalf("failed to inspect warehouse references: %v", err)
} }
if err := runPrechecks(ctx, db, rows, refs); err != nil { if err := runPrechecks(ctx, db, rows, refs, opts); err != nil {
log.Fatalf("precheck failed: %v", err) log.Fatalf("precheck failed: %v", err)
} }
@@ -157,6 +159,8 @@ func parseFlags() (*options, error) {
flag.StringVar(&opts.KandangLocationName, "kandang-location-name", "", "Optional exact canonical kandang location filter") flag.StringVar(&opts.KandangLocationName, "kandang-location-name", "", "Optional exact canonical kandang location filter")
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require") flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require")
flag.BoolVar(&opts.DeleteKandangWarehouses, "delete-kandang-warehouses", true, "Soft delete kandang warehouse rows after all stocks have been moved") flag.BoolVar(&opts.DeleteKandangWarehouses, "delete-kandang-warehouses", true, "Soft delete kandang warehouse rows after all stocks have been moved")
flag.BoolVar(&opts.SkipBlockedRefsCheck, "skip-blocked-refs-check", false, "Skip blocked references check (use with caution - only if you understand the stock_transfers references)")
flag.BoolVar(&opts.SkipIncompleteLocations, "skip-incomplete-locations", false, "Skip locations that don't have farm-level warehouses and process the rest")
flag.Parse() flag.Parse()
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output)) opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
@@ -186,6 +190,12 @@ func loadConsolidateRows(ctx context.Context, db *gorm.DB, opts *options) ([]con
args = append(args, opts.KandangLocationName) args = append(args, opts.KandangLocationName)
} }
// If skipping incomplete locations, filter out NULL farm warehouses
whereClause := ""
if opts.SkipIncompleteLocations {
whereClause = "AND fw.id IS NOT NULL"
}
query := fmt.Sprintf(` query := fmt.Sprintf(`
SELECT SELECT
a.name AS area_name, a.name AS area_name,
@@ -224,7 +234,10 @@ JOIN product_warehouses wp
ON wp.warehouse_id = w.id ON wp.warehouse_id = w.id
JOIN products p JOIN products p
ON p.id = wp.product_id ON p.id = wp.product_id
AND UPPER(COALESCE(p.type, '')) IN ('PAKAN', 'OVK') JOIN flags f
ON f.flagable_id = p.id
AND f.flagable_type = 'products'
AND UPPER(f.name) IN ('PAKAN', 'OVK')
LEFT JOIN product_warehouses fpw LEFT JOIN product_warehouses fpw
ON fpw.product_id = wp.product_id ON fpw.product_id = wp.product_id
AND fpw.warehouse_id = fw.id AND fpw.warehouse_id = fw.id
@@ -246,8 +259,11 @@ WHERE w.deleted_at IS NULL
AND sa.deleted_at IS NULL AND sa.deleted_at IS NULL
) )
%s %s
%s
ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC
`, andClause(filters)) `,
andClause(filters),
whereClause)
rows := make([]consolidateRow, 0) rows := make([]consolidateRow, 0)
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil { if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
@@ -297,9 +313,12 @@ func buildReferencePlan(ctx context.Context, db *gorm.DB) (*referencePlan, error
}, nil }, nil
} }
func runPrechecks(ctx context.Context, db *gorm.DB, rows []consolidateRow, refs *referencePlan) error { func runPrechecks(ctx context.Context, db *gorm.DB, rows []consolidateRow, refs *referencePlan, opts *options) error {
if err := ensureNoBlockedWarehouseRefsConsolidate(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil { // Only check blocked references if we're actually deleting the warehouses
return err if opts.DeleteKandangWarehouses && !opts.SkipBlockedRefsCheck {
if err := ensureNoBlockedWarehouseRefsConsolidate(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
return err
}
} }
if err := ensureNoPurchaseItemWarehouseConflictsConsolidate(ctx, db, rows); err != nil { if err := ensureNoPurchaseItemWarehouseConflictsConsolidate(ctx, db, rows); err != nil {
return err return err
@@ -710,6 +729,9 @@ func reflowAndRecalculateProductWarehouse(
if err != nil { if err != nil {
return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err) return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err)
} }
if flagGroupCode == "" {
return nil
}
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{ if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
FlagGroupCode: flagGroupCode, FlagGroupCode: flagGroupCode,
@@ -759,6 +781,9 @@ func resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, produc
Order("rr.id ASC"). Order("rr.id ASC").
Limit(1). Limit(1).
Take(&selected).Error Take(&selected).Error
if err == gorm.ErrRecordNotFound {
return "", nil
}
if err != nil { if err != nil {
return "", err return "", err
} }
+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
}
+116 -9
View File
@@ -26,12 +26,13 @@ const (
) )
type options struct { type options struct {
Apply bool Apply bool
Output string Output string
AreaName string AreaName string
KandangLocationName string KandangLocationName string
DBSSLMode string DBSSLMode string
DeleteWrongWarehouses bool DeleteWrongWarehouses bool
AllowMovingAllocatedStocks bool
} }
type planRow struct { type planRow struct {
@@ -123,6 +124,16 @@ func main() {
log.Fatalf("failed to load plan rows: %v", err) log.Fatalf("failed to load plan rows: %v", err)
} }
if opts.AllowMovingAllocatedStocks {
allocatedRows, err := loadPlanRowsWithAllocations(ctx, db, opts)
if err != nil {
log.Fatalf("failed to load allocated plan rows: %v", err)
}
rows = append(rows, allocatedRows...)
// Remove duplicates
rows = deduplicatePlanRows(rows)
}
if len(rows) == 0 { if len(rows) == 0 {
fmt.Println("No misplaced PAKAN/OVK stocks found in wrong-location warehouses") fmt.Println("No misplaced PAKAN/OVK stocks found in wrong-location warehouses")
return return
@@ -158,6 +169,7 @@ func parseFlags() (*options, error) {
flag.StringVar(&opts.KandangLocationName, "kandang-location-name", "", "Optional exact canonical kandang location filter") flag.StringVar(&opts.KandangLocationName, "kandang-location-name", "", "Optional exact canonical kandang location filter")
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require") flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require")
flag.BoolVar(&opts.DeleteWrongWarehouses, "delete-wrong-warehouses", true, "Soft delete wrong warehouse rows after all references have been moved") flag.BoolVar(&opts.DeleteWrongWarehouses, "delete-wrong-warehouses", true, "Soft delete wrong warehouse rows after all references have been moved")
flag.BoolVar(&opts.AllowMovingAllocatedStocks, "allow-moving-allocated-stocks", false, "Allow moving stocks that have active allocations (use with caution - for old recordings with completed allocations)")
flag.Parse() flag.Parse()
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output)) opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
@@ -175,6 +187,90 @@ func parseFlags() (*options, error) {
return &opts, nil return &opts, nil
} }
func deduplicatePlanRows(rows []planRow) []planRow {
seen := make(map[uint]struct{})
result := make([]planRow, 0, len(rows))
for _, row := range rows {
if _, ok := seen[row.SurvivorPWID]; !ok {
seen[row.SurvivorPWID] = struct{}{}
result = append(result, row)
}
}
return result
}
func loadPlanRowsWithAllocations(ctx context.Context, db *gorm.DB, opts *options) ([]planRow, error) {
filters := make([]string, 0, 2)
args := make([]any, 0, 2)
if opts.AreaName != "" {
filters = append(filters, "a.name = ?")
args = append(args, opts.AreaName)
}
if opts.KandangLocationName != "" {
filters = append(filters, "kl.name = ?")
args = append(args, opts.KandangLocationName)
}
query := fmt.Sprintf(`
SELECT
a.name AS area_name,
kl.name AS kandang_location_name,
k.id AS kandang_id,
k.name AS kandang_name,
w.id AS wrong_warehouse_id,
w.name AS wrong_warehouse_name,
correct_w.id AS correct_warehouse_id,
correct_w.name AS correct_warehouse_name,
p.id AS product_id,
p.name AS product_name,
wp.project_flock_kandang_id,
wp.id AS survivor_pw_id,
COALESCE(wp.qty, 0) AS survivor_current_qty,
cpw.id AS absorbed_pw_id,
cpw.qty AS absorbed_current_qty
FROM warehouses w
JOIN kandangs k
ON k.id = w.kandang_id
AND k.deleted_at IS NULL
JOIN locations kl
ON kl.id = k.location_id
JOIN areas a
ON a.id = kl.area_id
JOIN LATERAL (
SELECT w2.id, w2.name
FROM warehouses w2
WHERE w2.location_id = k.location_id
AND UPPER(COALESCE(w2.type, '')) = 'LOKASI'
AND w2.deleted_at IS NULL
ORDER BY w2.id ASC
LIMIT 1
) AS correct_w ON TRUE
JOIN product_warehouses wp
ON wp.warehouse_id = w.id
JOIN products p
ON p.id = wp.product_id
JOIN flags f
ON f.flagable_id = p.id
AND f.flagable_type = 'products'
AND UPPER(f.name) IN ('PAKAN', 'OVK')
LEFT JOIN product_warehouses cpw
ON cpw.product_id = wp.product_id
AND cpw.warehouse_id = correct_w.id
AND cpw.project_flock_kandang_id IS NOT DISTINCT FROM wp.project_flock_kandang_id
WHERE w.deleted_at IS NULL
AND w.kandang_id IS NOT NULL
AND w.location_id IS DISTINCT FROM k.location_id
%s
ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC
`, andClause(filters))
rows := make([]planRow, 0)
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
return nil, err
}
return rows, nil
}
func loadPlanRows(ctx context.Context, db *gorm.DB, opts *options) ([]planRow, error) { func loadPlanRows(ctx context.Context, db *gorm.DB, opts *options) ([]planRow, error) {
filters := make([]string, 0, 2) filters := make([]string, 0, 2)
args := make([]any, 0, 2) args := make([]any, 0, 2)
@@ -225,7 +321,10 @@ JOIN product_warehouses wp
ON wp.warehouse_id = w.id ON wp.warehouse_id = w.id
JOIN products p JOIN products p
ON p.id = wp.product_id ON p.id = wp.product_id
AND UPPER(COALESCE(p.type, '')) IN ('PAKAN', 'OVK') JOIN flags f
ON f.flagable_id = p.id
AND f.flagable_type = 'products'
AND UPPER(f.name) IN ('PAKAN', 'OVK')
LEFT JOIN product_warehouses cpw LEFT JOIN product_warehouses cpw
ON cpw.product_id = wp.product_id ON cpw.product_id = wp.product_id
AND cpw.warehouse_id = correct_w.id AND cpw.warehouse_id = correct_w.id
@@ -301,8 +400,10 @@ func buildReferencePlan(ctx context.Context, db *gorm.DB) (*referencePlan, error
} }
func runPrechecks(ctx context.Context, db *gorm.DB, rows []planRow, refs *referencePlan, opts *options) error { func runPrechecks(ctx context.Context, db *gorm.DB, rows []planRow, refs *referencePlan, opts *options) error {
if err := ensureNoBlockedWarehouseRefs(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil { if opts.DeleteWrongWarehouses {
return err if err := ensureNoBlockedWarehouseRefs(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
return err
}
} }
if err := ensureNoPurchaseItemWarehouseConflicts(ctx, db, rows); err != nil { if err := ensureNoPurchaseItemWarehouseConflicts(ctx, db, rows); err != nil {
return err return err
@@ -714,6 +815,9 @@ func reflowAndRecalculateProductWarehouse(
if err != nil { if err != nil {
return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err) return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err)
} }
if flagGroupCode == "" {
return nil
}
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{ if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
FlagGroupCode: flagGroupCode, FlagGroupCode: flagGroupCode,
@@ -763,6 +867,9 @@ func resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, produc
Order("rr.id ASC"). Order("rr.id ASC").
Limit(1). Limit(1).
Take(&selected).Error Take(&selected).Error
if err == gorm.ErrRecordNotFound {
return "", nil
}
if err != nil { if err != nil {
return "", err return "", err
} }
+48 -29
View File
@@ -19,9 +19,9 @@ const (
outputTable = "table" outputTable = "table"
outputJSON = "json" outputJSON = "json"
caseA = "A" caseA = "A"
caseB = "B" caseB = "B"
caseAll = "all" caseAll = "ALL"
) )
type options struct { type options struct {
@@ -39,7 +39,7 @@ type sourceWarehouseCheck struct {
KandangName string `json:"kandang_name"` KandangName string `json:"kandang_name"`
SourceWarehouseID uint `json:"source_warehouse_id"` SourceWarehouseID uint `json:"source_warehouse_id"`
SourceWarehouseName string `json:"source_warehouse_name"` SourceWarehouseName string `json:"source_warehouse_name"`
Case string `json:"case"` Case string `json:"case" gorm:"column:case_type"`
DeletedAt *string `json:"deleted_at"` DeletedAt *string `json:"deleted_at"`
StockInProductWH float64 `json:"stock_in_product_wh"` StockInProductWH float64 `json:"stock_in_product_wh"`
ActivePurchaseItems int64 `json:"active_purchase_items"` ActivePurchaseItems int64 `json:"active_purchase_items"`
@@ -145,7 +145,7 @@ func parseFlags() (*options, error) {
} }
func verifySourceWarehouses(ctx context.Context, db *gorm.DB, opts *options) ([]sourceWarehouseCheck, error) { func verifySourceWarehouses(ctx context.Context, db *gorm.DB, opts *options) ([]sourceWarehouseCheck, error) {
filters := buildFilters(opts) filters, args := buildFilters(opts)
query := fmt.Sprintf(` query := fmt.Sprintf(`
WITH case_a_warehouses AS ( WITH case_a_warehouses AS (
-- Case A: Kandang-level warehouses (type != 'LOKASI' or NULL) -- Case A: Kandang-level warehouses (type != 'LOKASI' or NULL)
@@ -182,9 +182,27 @@ case_b_warehouses AS (
AND w.location_id IS DISTINCT FROM k.location_id AND w.location_id IS DISTINCT FROM k.location_id
), ),
all_source_warehouses AS ( all_source_warehouses AS (
SELECT id, area_name, kandang_location_name, id AS kandang_id, name, case_type FROM case_a_warehouses SELECT w_id, area_name, kandang_location_name, k_id AS kandang_id, name, case_type FROM (
SELECT w.id as w_id, a.name AS area_name, kl.name AS kandang_location_name, k.id as k_id, k.name, 'A'::text AS case_type
FROM warehouses w
JOIN kandangs k ON k.id = w.kandang_id AND k.deleted_at IS NULL
JOIN locations kl ON kl.id = k.location_id
JOIN areas a ON a.id = kl.area_id
WHERE w.deleted_at IS NOT NULL
AND w.kandang_id IS NOT NULL
AND UPPER(COALESCE(w.type, '')) <> 'LOKASI'
) case_a_warehouses
UNION ALL UNION ALL
SELECT id, area_name, kandang_location_name, id AS kandang_id, name, case_type FROM case_b_warehouses SELECT w_id, area_name, kandang_location_name, k_id AS kandang_id, name, case_type FROM (
SELECT w.id as w_id, a.name AS area_name, kl.name AS kandang_location_name, k.id as k_id, k.name, 'B'::text AS case_type
FROM warehouses w
JOIN kandangs k ON k.id = w.kandang_id AND k.deleted_at IS NULL
JOIN locations kl ON kl.id = k.location_id
JOIN areas a ON a.id = kl.area_id
WHERE w.deleted_at IS NOT NULL
AND w.kandang_id IS NOT NULL
AND w.location_id IS DISTINCT FROM k.location_id
) case_b_warehouses
) )
SELECT SELECT
asw.area_name, asw.area_name,
@@ -198,7 +216,7 @@ SELECT
COALESCE(SUM(pw.qty), 0) AS stock_in_product_wh, COALESCE(SUM(pw.qty), 0) AS stock_in_product_wh,
COUNT(DISTINCT pi.id) AS active_purchase_items COUNT(DISTINCT pi.id) AS active_purchase_items
FROM all_source_warehouses asw FROM all_source_warehouses asw
JOIN warehouses w ON w.id = asw.id JOIN warehouses w ON w.id = asw.w_id
LEFT JOIN product_warehouses pw ON pw.warehouse_id = w.id LEFT JOIN product_warehouses pw ON pw.warehouse_id = w.id
LEFT JOIN purchase_items pi ON pi.warehouse_id = w.id LEFT JOIN purchase_items pi ON pi.warehouse_id = w.id
WHERE true WHERE true
@@ -216,7 +234,7 @@ ORDER BY asw.area_name ASC, asw.kandang_location_name ASC, w.name ASC
`, andClause(filters)) `, andClause(filters))
rows := make([]sourceWarehouseCheck, 0) rows := make([]sourceWarehouseCheck, 0)
if err := db.WithContext(ctx).Raw(query).Scan(&rows).Error; err != nil { if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
return nil, err return nil, err
} }
@@ -239,7 +257,7 @@ ORDER BY asw.area_name ASC, asw.kandang_location_name ASC, w.name ASC
} }
func verifyDestinationWarehouses(ctx context.Context, db *gorm.DB, opts *options, sourceChecks []sourceWarehouseCheck) ([]destinationWarehouseCheck, error) { func verifyDestinationWarehouses(ctx context.Context, db *gorm.DB, opts *options, sourceChecks []sourceWarehouseCheck) ([]destinationWarehouseCheck, error) {
filters := buildFilters(opts) filters, args := buildFilters(opts)
query := fmt.Sprintf(` query := fmt.Sprintf(`
SELECT SELECT
a.name AS area_name, a.name AS area_name,
@@ -252,12 +270,11 @@ SELECT
COALESCE(SUM(sl.stock), 0) AS stock_logs_total, COALESCE(SUM(sl.stock), 0) AS stock_logs_total,
COUNT(DISTINCT sl.id) AS stock_logs_count COUNT(DISTINCT sl.id) AS stock_logs_count
FROM warehouses fw FROM warehouses fw
JOIN locations loc ON loc.id = fw.location_id JOIN locations kl ON kl.id = fw.location_id
JOIN areas a ON a.id = loc.area_id JOIN areas a ON a.id = kl.area_id
JOIN kandangs k ON k.location_id = fw.location_id AND k.deleted_at IS NULL JOIN product_warehouses pw ON pw.warehouse_id = fw.id
JOIN locations kl ON kl.id = k.location_id JOIN products p ON p.id = pw.product_id
JOIN products p ON UPPER(COALESCE(p.type, '')) IN ('PAKAN', 'OVK') JOIN flags f ON f.flagable_id = p.id AND f.flagable_type = 'products' AND UPPER(f.name) IN ('PAKAN', 'OVK')
LEFT JOIN product_warehouses pw ON pw.warehouse_id = fw.id AND pw.product_id = p.id
LEFT JOIN stock_logs sl ON sl.product_warehouse_id = pw.id LEFT JOIN stock_logs sl ON sl.product_warehouse_id = pw.id
WHERE fw.deleted_at IS NULL WHERE fw.deleted_at IS NULL
AND UPPER(COALESCE(fw.type, '')) = 'LOKASI' AND UPPER(COALESCE(fw.type, '')) = 'LOKASI'
@@ -274,7 +291,7 @@ ORDER BY a.name ASC, kl.name ASC, fw.name ASC, p.name ASC
`, andClause(filters)) `, andClause(filters))
rows := make([]destinationWarehouseCheck, 0) rows := make([]destinationWarehouseCheck, 0)
if err := db.WithContext(ctx).Raw(query).Scan(&rows).Error; err != nil { if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
return nil, err return nil, err
} }
@@ -316,7 +333,6 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
{"purchase_items", "warehouse_id"}, {"purchase_items", "warehouse_id"},
{"stock_transfers", "from_warehouse_id"}, {"stock_transfers", "from_warehouse_id"},
{"stock_transfers", "to_warehouse_id"}, {"stock_transfers", "to_warehouse_id"},
{"fifo_stock_v2_operation_log", "warehouse_id"},
} }
for _, ref := range refChecks { for _, ref := range refChecks {
@@ -328,17 +344,17 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
} }
if count > 0 { if count > 0 {
// Get the specific warehouse IDs // Get the specific warehouse IDs using raw SQL
var ids []uint var ids []uint
if err := db.Table(ref.table). query := fmt.Sprintf("SELECT DISTINCT %s FROM %s WHERE %s IN ?",
Where(fmt.Sprintf("%s IN ?", ref.column), warehouseIDs). ref.column, ref.table, ref.column)
Pluck(ref.column, &ids).Error; err != nil { if err := db.Raw(query, warehouseIDs).Scan(&ids).Error; err != nil {
return nil, err return nil, err
} }
idStrs := make([]string, len(ids)) idStrs := make([]string, 0, len(ids))
for i, id := range ids { for _, id := range ids {
idStrs[i] = fmt.Sprintf("%d", id) idStrs = append(idStrs, fmt.Sprintf("%d", id))
} }
results = append(results, orphanedReferenceCheck{ results = append(results, orphanedReferenceCheck{
@@ -353,15 +369,18 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
return results, nil return results, nil
} }
func buildFilters(opts *options) []string { func buildFilters(opts *options) ([]string, []any) {
filters := make([]string, 0, 2) filters := make([]string, 0, 2)
args := make([]any, 0, 2)
if opts.AreaName != "" { if opts.AreaName != "" {
filters = append(filters, fmt.Sprintf("a.name = '%s'", opts.AreaName)) filters = append(filters, "a.name = ?")
args = append(args, opts.AreaName)
} }
if opts.KandangLocationName != "" { if opts.KandangLocationName != "" {
filters = append(filters, fmt.Sprintf("kl.name = '%s'", opts.KandangLocationName)) filters = append(filters, "kl.name = ?")
args = append(args, opts.KandangLocationName)
} }
return filters return filters, args
} }
func andClause(filters []string) string { func andClause(filters []string) string {
Binary file not shown.
BIN
View File
Binary file not shown.
+1
View File
@@ -52,6 +52,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/go-pdf/fpdf v0.9.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect github.com/google/go-cmp v0.6.0 // indirect
+2
View File
@@ -77,6 +77,8 @@ github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9g
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ= github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
github.com/go-pdf/fpdf v0.9.0 h1:PPvSaUuo1iMi9KkaAn90NuKi+P4gwMedWPHhj8YlJQw=
github.com/go-pdf/fpdf v0.9.0/go.mod h1:oO8N111TkmKb9D7VvWGLvLJlaZUQVPM+6V42pp3iV4Y=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
@@ -115,6 +115,7 @@ type HppV2CostRepository interface {
GetFeedUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error) GetFeedUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error)
GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error) GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error)
GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error)
GetEggProduksiBreakdownByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (recordingQty, recordingWeight, adjustmentQty, adjustmentWeight float64, err error)
GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, startDate *time.Time, endDate *time.Time) (float64, float64, error) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, startDate *time.Time, endDate *time.Time) (float64, float64, error)
GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, error) GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, error)
} }
@@ -858,58 +859,50 @@ func (r *HppV2RepositoryImpl) GetTotalPopulation(ctx context.Context, projectFlo
} }
func (r *HppV2RepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) { func (r *HppV2RepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) {
rQty, rWeight, aQty, aWeight, err := r.GetEggProduksiBreakdownByProjectFlockKandangIds(ctx, projectFlockKandangIDs, date)
if err != nil {
return 0, 0, err
}
return rQty + aQty, rWeight + aWeight, nil
}
func (r *HppV2RepositoryImpl) GetEggProduksiBreakdownByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (recordingQty, recordingWeight, adjustmentQty, adjustmentWeight float64, err error) {
if date == nil { if date == nil {
now := time.Now() now := time.Now()
date = &now date = &now
} }
var totals struct { var recordingTotals struct {
TotalPieces float64 TotalPieces float64
TotalWeightKg float64 TotalWeightKg float64
} }
err := r.db.WithContext(ctx). err = r.db.WithContext(ctx).
Table("recordings AS r"). Table("recordings AS r").
Select("COALESCE(SUM(re.qty), 0) AS total_pieces, COALESCE(SUM(re.weight), 0)AS total_weight_kg"). Select("COALESCE(SUM(re.qty), 0) AS total_pieces, COALESCE(SUM(re.weight), 0) AS total_weight_kg").
Joins("JOIN recording_eggs AS re ON re.recording_id = r.id"). Joins("JOIN recording_eggs AS re ON re.recording_id = r.id").
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs). Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
Where("r.record_datetime <= ?", *date). Where("r.record_datetime <= ?", *date).
Scan(&totals).Error Scan(&recordingTotals).Error
if err != nil { if err != nil {
return 0, 0, err return 0, 0, 0, 0, err
} }
var adjustmentTotals struct { var adjustmentTotals struct {
TotalQty float64 TotalQty float64
TotalWeight float64 TotalWeight float64
} }
adjustmentSubQuery := r.db.WithContext(ctx).
Table("recordings AS r").
Select("DISTINCT ast.id AS adjustment_id, ast.total_qty AS total_qty, ast.price AS price").
Joins("JOIN recording_eggs AS re ON re.recording_id = r.id").
Joins("JOIN stock_transfer_details AS std ON std.dest_product_warehouse_id = re.product_warehouse_id").
Joins(
"JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = std.id AND sa.stockable_type = ? AND sa.status = ? AND sa.allocation_purpose = ?",
fifo.UsableKeyStockTransferOut.String(),
fifo.StockableKeyAdjustmentIn.String(),
entity.StockAllocationStatusActive,
entity.StockAllocationPurposeConsume,
).
Joins("JOIN adjustment_stocks AS ast ON ast.id = sa.stockable_id AND ast.product_warehouse_id = std.source_product_warehouse_id").
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
Where("r.record_datetime <= ?", *date)
err = r.db.WithContext(ctx). err = r.db.WithContext(ctx).
Table("(?) AS adjustment_sources", adjustmentSubQuery). Table("adjustment_stocks AS ast").
Select("COALESCE(SUM(adjustment_sources.total_qty), 0) AS total_qty, COALESCE(SUM(adjustment_sources.price), 0) AS total_weight"). Select("COALESCE(SUM(ast.total_qty), 0) AS total_qty, COALESCE(SUM(ast.price), 0) AS total_weight").
Joins("JOIN product_warehouses AS pw ON pw.id = ast.product_warehouse_id").
Where("pw.project_flock_kandang_id IN (?)", projectFlockKandangIDs).
Where("ast.function_code = ?", string(utils.AdjustmentTransactionSubtypeRecordingEggIn)).
Scan(&adjustmentTotals).Error Scan(&adjustmentTotals).Error
if err != nil { if err != nil {
return 0, 0, err return 0, 0, 0, 0, err
} }
totals.TotalPieces += adjustmentTotals.TotalQty return recordingTotals.TotalPieces, recordingTotals.TotalWeightKg, adjustmentTotals.TotalQty, adjustmentTotals.TotalWeight, nil
totals.TotalWeightKg += adjustmentTotals.TotalWeight
return totals.TotalPieces, totals.TotalWeightKg, nil
} }
func (r *HppV2RepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds( func (r *HppV2RepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(
@@ -1160,7 +1153,6 @@ CROSS JOIN lokasi_rec_totals lrt
string(utils.AdjustmentTransactionTypeRecording), string(utils.AdjustmentTransactionTypeRecording),
). ).
Scan(&totals).Error Scan(&totals).Error
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
@@ -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 {
@@ -18,8 +18,9 @@ type HppService interface {
} }
type HppCostResponse struct { type HppCostResponse struct {
Estimation HppCostDetail `json:"estimation"` Estimation HppCostDetail `json:"estimation"`
Real HppCostDetail `json:"real"` Real HppCostDetail `json:"real"`
DebugValues *HppCostDebugValues `json:"debug_values,omitempty"`
} }
type HppCostDetail struct { type HppCostDetail struct {
@@ -44,6 +44,15 @@ type HppV2Component struct {
Parts []HppV2ComponentPart `json:"parts"` Parts []HppV2ComponentPart `json:"parts"`
} }
type HppCostDebugValues struct {
RecordingEggQty float64 `json:"recording_egg_qty"`
RecordingEggWeight float64 `json:"recording_egg_weight"`
AdjustmentEggQty float64 `json:"adjustment_egg_qty"`
AdjustmentEggWeight float64 `json:"adjustment_egg_weight"`
SoldEggQty float64 `json:"sold_egg_qty"`
SoldEggWeight float64 `json:"sold_egg_weight"`
}
type HppV2Breakdown struct { type HppV2Breakdown struct {
ProjectFlockKandangID uint `json:"project_flock_kandang_id"` ProjectFlockKandangID uint `json:"project_flock_kandang_id"`
ProjectFlockID uint `json:"project_flock_id"` ProjectFlockID uint `json:"project_flock_id"`
@@ -36,7 +36,7 @@ const (
hppV2ProrationEggPiece = "laying_egg_piece_share" hppV2ProrationEggPiece = "laying_egg_piece_share"
hppV2ScopePulletCost = "pullet_cost" hppV2ScopePulletCost = "pullet_cost"
hppV2ScopeProductionCost = "production_cost" hppV2ScopeProductionCost = "production_cost"
hppV2CutoverFlagPakan = "PAKAN-CUTOVER" hppV2CutoverFlagPakan = string(utils.FlagPakan)
hppV2CutoverFlagOvk = "OVK" hppV2CutoverFlagOvk = "OVK"
) )
@@ -1489,7 +1489,7 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64,
return &HppCostResponse{}, nil return &HppCostResponse{}, nil
} }
estimPieces, estimWeightKg, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate) recordingQty, recordingWeight, adjustmentQty, adjustmentWeight, err := s.hppRepo.GetEggProduksiBreakdownByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate)
if err != nil { if err != nil {
utils.Log.WithError(err).Errorf( utils.Log.WithError(err).Errorf(
"GetHppEstimationDanRealisasi failed to get estimation egg production: project_flock_kandang_id=%d end_date=%s", "GetHppEstimationDanRealisasi failed to get estimation egg production: project_flock_kandang_id=%d end_date=%s",
@@ -1498,6 +1498,8 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64,
) )
return nil, err return nil, err
} }
estimPieces := recordingQty + adjustmentQty
estimWeightKg := recordingWeight + adjustmentWeight
realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate) realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate)
if err != nil { if err != nil {
@@ -1551,6 +1553,14 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64,
return &HppCostResponse{ return &HppCostResponse{
Estimation: estimation, Estimation: estimation,
Real: real, Real: real,
DebugValues: &HppCostDebugValues{
RecordingEggQty: recordingQty,
RecordingEggWeight: recordingWeight,
AdjustmentEggQty: adjustmentQty,
AdjustmentEggWeight: adjustmentWeight,
SoldEggQty: realPieces,
SoldEggWeight: realWeightKg,
},
}, nil }, nil
} }
@@ -132,6 +132,17 @@ func (s *hppV2RepoStub) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(
return totalPieces, totalKg, nil return totalPieces, totalKg, nil
} }
func (s *hppV2RepoStub) GetEggProduksiBreakdownByProjectFlockKandangIds(_ context.Context, projectFlockKandangIDs []uint, _ *time.Time) (float64, float64, float64, float64, error) {
totalPieces := 0.0
totalKg := 0.0
for _, projectFlockKandangID := range projectFlockKandangIDs {
row := s.eggProductionByPFK[projectFlockKandangID]
totalPieces += row.pieces
totalKg += row.kg
}
return totalPieces, totalKg, 0, 0, nil
}
func (s *hppV2RepoStub) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(_ context.Context, projectFlockKandangIDs []uint, _ *time.Time, _ *time.Time) (float64, float64, error) { func (s *hppV2RepoStub) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(_ context.Context, projectFlockKandangIDs []uint, _ *time.Time, _ *time.Time) (float64, float64, error) {
if len(projectFlockKandangIDs) != 1 { if len(projectFlockKandangIDs) != 1 {
return 0, 0, nil return 0, 0, nil
@@ -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,11 @@
BEGIN;
-- Revert fcr_value and cum_depletion_rate back to NUMERIC(7,3).
-- WARNING: any value with an integer part > 9999 (e.g. high-FCR early-laying recordings)
-- will fail the cast and must be cleared first, or this rollback will error.
ALTER TABLE recordings
ALTER COLUMN fcr_value TYPE NUMERIC(7,3) USING fcr_value::NUMERIC(7,3),
ALTER COLUMN cum_depletion_rate TYPE NUMERIC(7,3) USING cum_depletion_rate::NUMERIC(7,3);
COMMIT;
@@ -0,0 +1,13 @@
BEGIN;
-- fcr_value and cum_depletion_rate were created as NUMERIC(7,3) (max integer part: 9999).
-- Early-laying flocks produce very few eggs relative to total feed consumed, so
-- FCR = usageInGrams / totalEggWeightGrams can legitimately exceed 9999 (e.g. ~31 740).
-- Widening to NUMERIC(15,3) keeps the same 3-decimal-place scale and is
-- fully backward-compatible: no existing value will be truncated or altered.
ALTER TABLE recordings
ALTER COLUMN fcr_value TYPE NUMERIC(15,3) USING fcr_value::NUMERIC(15,3),
ALTER COLUMN cum_depletion_rate TYPE NUMERIC(15,3) USING cum_depletion_rate::NUMERIC(15,3);
COMMIT;
@@ -0,0 +1 @@
DROP TABLE IF EXISTS system_settings;
@@ -0,0 +1,11 @@
CREATE TABLE system_settings (
key VARCHAR(100) PRIMARY KEY,
value TEXT NOT NULL DEFAULT '',
description TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
INSERT INTO system_settings (key, value, description) VALUES
('allow_negative_pakan_ovk', 'false',
'Izinkan pencatatan penggunaan PAKAN & OVK negatif (mode migrasi): membuka semua produk PAKAN & OVK meskipun belum ada pembelian di sistem');
@@ -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);
+17
View File
@@ -0,0 +1,17 @@
package entities
import "time"
const SystemSettingKeyAllowNegativePakanOVK = "allow_negative_pakan_ovk"
type SystemSetting struct {
Key string `gorm:"column:key;primaryKey" json:"key"`
Value string `gorm:"column:value;not null;default:''" json:"value"`
Description string `gorm:"column:description" json:"description"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime" json:"updated_at"`
}
func (SystemSetting) TableName() string {
return "system_settings"
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
type Warehouse struct { type Warehouse struct {
Id uint `gorm:"primaryKey"` Id uint `gorm:"primaryKey"`
Name string `gorm:"type:varchar(50);not null"` Name string `gorm:"type:varchar(50);not null"`
Type string `gorm:"not null"` Type string `gorm:"column:type;not null"`
AreaId uint `gorm:"not null"` AreaId uint `gorm:"not null"`
LocationId *uint LocationId *uint
KandangId *uint KandangId *uint
+5
View File
@@ -4,6 +4,10 @@ const (
P_DashboardGetAll = "lti.dashboard.list" P_DashboardGetAll = "lti.dashboard.list"
) )
const (
P_SystemSettingUpdate = "lti.system_settings.update"
)
// project-flock // project-flock
const ( const (
P_ProjectFlockKandangsClosing = "lti.production.project_flock_kandangs.closing" P_ProjectFlockKandangsClosing = "lti.production.project_flock_kandangs.closing"
@@ -203,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"
@@ -10,7 +10,7 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
ProjectStatus *int `query:"project_status" validate:"omitempty,oneof=1 2"` ProjectStatus *int `query:"project_status" validate:"omitempty,oneof=1 2"`
LocationID *uint `query:"location_id" validate:"omitempty,gt=0"` LocationID *uint `query:"location_id" validate:"omitempty,gt=0"`
@@ -24,7 +24,7 @@ const (
type ClosingSapronakQuery struct { type ClosingSapronakQuery struct {
Type string `query:"type" validate:"required,oneof=incoming outgoing"` Type string `query:"type" validate:"required,oneof=incoming outgoing"`
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
KandangID *uint `query:"kandang_id" validate:"omitempty,gt=0"` KandangID *uint `query:"kandang_id" validate:"omitempty,gt=0"`
Search string `query:"search" validate:"omitempty,max=100"` Search string `query:"search" validate:"omitempty,max=100"`
} }
@@ -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
} }
@@ -123,12 +123,13 @@ type DailyChecklistReportCategory struct {
} }
const ( const (
dailyChecklistDateLayout = "2006-01-02" dailyChecklistDateLayout = "2006-01-02"
dailyChecklistCategoryEmptyKandang = "empty_kandang" dailyChecklistCategoryEmptyKandang = "empty_kandang"
dailyChecklistStatusRejected = "REJECTED" dailyChecklistStatusRejected = "REJECTED"
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,15 +536,17 @@ 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
}
if err := s.validateNoDeletedNonEmptyKandangForDate(tx, req.KandangId, date); err != nil {
return err
}
} else {
if err := s.validateNoEmptyKandangConflict(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.validateNoEmptyKandangConflict(tx, req.KandangId, date, endDate); 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
} }
@@ -9,8 +9,7 @@ type Create struct {
KandangId uint `json:"kandang_id" validate:"required"` KandangId uint `json:"kandang_id" validate:"required"`
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,
) )
} }
@@ -33,6 +33,7 @@ type ProjectFlockRelationDTO struct {
type WarehouseRelationDTO struct { type WarehouseRelationDTO struct {
Id uint `json:"id"` Id uint `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Type string `json:"type"`
Location *LocationRelationDTO `json:"location,omitempty"` Location *LocationRelationDTO `json:"location,omitempty"`
} }
@@ -113,6 +114,7 @@ func ToWarehouseRelationDTO(e *entity.Warehouse) *WarehouseRelationDTO {
return &WarehouseRelationDTO{ return &WarehouseRelationDTO{
Id: e.Id, Id: e.Id,
Name: e.Name, Name: e.Name,
Type: e.Type,
Location: ToLocationRelationDTO(e.Location), Location: ToLocationRelationDTO(e.Location),
} }
} }
@@ -16,7 +16,7 @@ type Create struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,min=1"` Page int `query:"page" validate:"omitempty,min=1"`
Limit int `query:"limit" validate:"omitempty,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,min=1"`
ProductID uint `query:"product_id" validate:"omitempty,min=0"` ProductID uint `query:"product_id" validate:"omitempty,min=0"`
WarehouseID uint `query:"warehouse_id" validate:"omitempty,min=0"` WarehouseID uint `query:"warehouse_id" validate:"omitempty,min=0"`
TransactionType string `query:"transaction_type" validate:"omitempty,max=100"` TransactionType string `query:"transaction_type" validate:"omitempty,max=100"`
@@ -25,9 +25,10 @@ func NewProductStockController(productStockService service.ProductStockService)
func (u *ProductStockController) GetAll(c *fiber.Ctx) error { func (u *ProductStockController) 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),
Search: c.Query("search", ""), Search: c.Query("search", ""),
ProductCategoryID: uint(c.QueryInt("product_category_id", 0)),
} }
if query.Page < 1 || query.Limit < 1 { if query.Page < 1 || query.Limit < 1 {
@@ -129,6 +129,9 @@ func (s productStockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
if params.Search != "" { if params.Search != "" {
db = db.Where("products.name ILIKE ?", "%"+params.Search+"%") db = db.Where("products.name ILIKE ?", "%"+params.Search+"%")
} }
if params.ProductCategoryID > 0 {
db = db.Where("products.product_category_id = ?", params.ProductCategoryID)
}
return db.Order("products.created_at DESC").Order("products.updated_at DESC") return db.Order("products.created_at DESC").Order("products.updated_at DESC")
}) })
@@ -9,7 +9,8 @@ type Update struct {
} }
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
ProductCategoryID uint `query:"product_category_id" validate:"omitempty"`
} }
@@ -79,8 +79,12 @@ func ToProductWarehouseListDTO(e entity.ProductWarehouse) ProductWarehouseListDT
if e.Product.Id != 0 { if e.Product.Id != 0 {
product := productDTO.ToProductRelationDTO(e.Product) product := productDTO.ToProductRelationDTO(e.Product)
// Create a copy with flock name appended if exists // Append flock name only for KANDANG-type warehouses.
if e.ProjectFlockKandang != nil && e.ProjectFlockKandang.ProjectFlock.Id != 0 { // Farm-level (LOKASI) warehouses are shared across flocks — attaching a flock
// label there creates duplicates and is misleading.
if e.ProjectFlockKandang != nil &&
e.ProjectFlockKandang.ProjectFlock.Id != 0 &&
e.Warehouse.Type == "KANDANG" {
productCopy := product productCopy := product
productCopy.Name = product.Name + " (" + e.ProjectFlockKandang.ProjectFlock.FlockName + ")" productCopy.Name = product.Name + " (" + e.ProjectFlockKandang.ProjectFlock.FlockName + ")"
dto.Product = &productCopy dto.Product = &productCopy
@@ -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"
) )
@@ -23,6 +22,7 @@ type ProductWarehouseRepository interface {
GetByCategoryCodeAndWarehouseID(ctx context.Context, categoryCode string, warehouseId uint) ([]entity.ProductWarehouse, error) GetByCategoryCodeAndWarehouseID(ctx context.Context, categoryCode string, warehouseId uint) ([]entity.ProductWarehouse, error)
GetLatestByCategoryCodeAndWarehouseID(ctx context.Context, categoryCode string, warehouseId uint, db *gorm.DB) (*entity.ProductWarehouse, error) GetLatestByCategoryCodeAndWarehouseID(ctx context.Context, categoryCode string, warehouseId uint, db *gorm.DB) (*entity.ProductWarehouse, error)
GetByFlagAndWarehouseID(ctx context.Context, flagName string, warehouseId uint) ([]entity.ProductWarehouse, error) GetByFlagAndWarehouseID(ctx context.Context, flagName string, warehouseId uint) ([]entity.ProductWarehouse, error)
GetByFlagsAndWarehouseID(ctx context.Context, flagNames []string, excludeFlagNames []string, warehouseId uint) ([]entity.ProductWarehouse, error)
GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error) GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error)
ListProductIDsByFlagPrefixes(ctx context.Context, prefixes []string, visibleStatus *bool) ([]uint, error) ListProductIDsByFlagPrefixes(ctx context.Context, prefixes []string, visibleStatus *bool) ([]uint, error)
ApplyFlagsFilter(db *gorm.DB, flags []string) *gorm.DB ApplyFlagsFilter(db *gorm.DB, flags []string) *gorm.DB
@@ -165,42 +165,16 @@ func (r *ProductWarehouseRepositoryImpl) ApplyFlagsFilter(db *gorm.DB, flags []s
return db return db
} }
fallbackCategoryCodes := utils.LegacyProductCategoryCodesForFlags(flags)
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 (
SELECT 1
FROM flags f_flag
WHERE f_flag.flagable_id = p_flag.id
AND f_flag.flagable_type = ?
AND f_flag.name IN ?
)
`
if len(fallbackCategoryCodes) == 0 {
return db.Where(actualFlagFilter, entity.FlagableTypeProduct, flags).Distinct()
}
return db. return db.
Where( Where(`
`(`+actualFlagFilter+`) OR ( EXISTS (
NOT EXISTS ( SELECT 1
SELECT 1 FROM flags f_flag
FROM flags f_any WHERE f_flag.flagable_id = product_warehouses.product_id
WHERE f_any.flagable_id = p_flag.id AND f_flag.flagable_type = ?
AND f_any.flagable_type = ? AND f_flag.name IN ?
) )
AND pc_flag.code IN ? `, entity.FlagableTypeProduct, flags).
)`,
entity.FlagableTypeProduct,
flags,
entity.FlagableTypeProduct,
fallbackCategoryCodes,
).
Distinct() Distinct()
} }
@@ -430,6 +404,27 @@ func (r *ProductWarehouseRepositoryImpl) GetByFlagAndWarehouseID(ctx context.Con
return productWarehouses, nil return productWarehouses, nil
} }
func (r *ProductWarehouseRepositoryImpl) GetByFlagsAndWarehouseID(ctx context.Context, flagNames []string, excludeFlagNames []string, warehouseId uint) ([]entity.ProductWarehouse, error) {
var productWarehouses []entity.ProductWarehouse
q := r.DB().WithContext(ctx).Model(&entity.ProductWarehouse{}).
Joins("JOIN products ON products.id = product_warehouses.product_id").
Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = 'products'").
Where("flags.name IN ? AND product_warehouses.warehouse_id = ?", flagNames, warehouseId)
if len(excludeFlagNames) > 0 {
q = q.Where("NOT EXISTS (SELECT 1 FROM flags ef WHERE ef.flagable_id = products.id AND ef.flagable_type = 'products' AND ef.name IN ?)", excludeFlagNames)
}
err := q.Order("product_warehouses.id DESC").
Preload("Product").
Preload("Product.ProductCategory").
Preload("Product.Uom").
Preload("Warehouse").
Find(&productWarehouses).Error
if err != nil {
return nil, err
}
return productWarehouses, nil
}
func (r *ProductWarehouseRepositoryImpl) GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error) { func (r *ProductWarehouseRepositoryImpl) GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error) {
var product entity.Product var product entity.Product
err := r.DB().WithContext(ctx). err := r.DB().WithContext(ctx).
@@ -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)
} }
} }
@@ -14,7 +14,7 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty"` Search string `query:"search" validate:"omitempty"`
ProductId uint `query:"product_id" validate:"omitempty,number,min=1"` ProductId uint `query:"product_id" validate:"omitempty,number,min=1"`
WarehouseId uint `query:"warehouse_id" validate:"omitempty,number,min=1"` WarehouseId uint `query:"warehouse_id" validate:"omitempty,number,min=1"`
@@ -25,9 +25,11 @@ func NewTransferController(transferService service.TransferService) *TransferCon
func (u *TransferController) GetAll(c *fiber.Ctx) error { func (u *TransferController) 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),
Search: c.Query("search", ""), Search: c.Query("search", ""),
ProductID: uint(c.QueryInt("product_id", 0)),
WarehouseID: uint(c.QueryInt("warehouse_id", 0)),
} }
result, totalResults, err := u.TransferService.GetAll(c, query) result, totalResults, err := u.TransferService.GetAll(c, query)
@@ -157,6 +157,12 @@ func (s transferService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
Where("movement_number ILIKE ? OR from_warehouses.name ILIKE ? OR to_warehouses.name ILIKE ?", Where("movement_number ILIKE ? OR from_warehouses.name ILIKE ? OR to_warehouses.name ILIKE ?",
searchTerm, searchTerm, searchTerm) searchTerm, searchTerm, searchTerm)
} }
if params.ProductID > 0 {
db = db.Joins("JOIN stock_transfer_details AS filter_std ON filter_std.stock_transfer_id = stock_transfers.id AND filter_std.deleted_at IS NULL AND filter_std.product_id = ?", params.ProductID)
}
if params.WarehouseID > 0 {
db = db.Where("stock_transfers.from_warehouse_id = ? OR stock_transfers.to_warehouse_id = ?", params.WarehouseID, params.WarehouseID)
}
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}) })
@@ -5,9 +5,11 @@ type Create struct {
} }
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
ProductID uint `query:"product_id" validate:"omitempty"`
WarehouseID uint `query:"warehouse_id" validate:"omitempty"`
} }
type TransferProduct struct { type TransferProduct struct {
@@ -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)
} }
return db.Order("created_at DESC").Order("updated_at DESC")
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")
}
}) })
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
@@ -23,7 +23,7 @@ type DeliveryOrderUpdate struct {
type DeliveryOrderQuery struct { type DeliveryOrderQuery struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
Search string `query:"search" validate:"omitempty,max=100"` Search string `query:"search" validate:"omitempty,max=100"`
ProductIDs []uint `query:"product_ids" validate:"omitempty,dive,gt=0"` ProductIDs []uint `query:"product_ids" validate:"omitempty,dive,gt=0"`
Status string `query:"status" validate:"omitempty,max=50"` Status string `query:"status" validate:"omitempty,max=50"`
@@ -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 {
@@ -10,6 +10,6 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
} }
@@ -16,6 +16,6 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
} }
@@ -14,6 +14,6 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
} }
@@ -22,7 +22,7 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
HasMarketing *bool `query:"has_marketing" validate:"omitempty"` HasMarketing *bool `query:"has_marketing" validate:"omitempty"`
} }
@@ -24,9 +24,11 @@ func NewEmployeesController(employeesService service.EmployeesService) *Employee
func (u *EmployeesController) GetAll(c *fiber.Ctx) error { func (u *EmployeesController) 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),
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"`
} }
@@ -18,6 +18,6 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
} }
@@ -10,6 +10,6 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
} }
@@ -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"`
} }
@@ -16,7 +16,7 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
SupplierID uint `query:"supplier_id" validate:"omitempty,gt=0"` SupplierID uint `query:"supplier_id" validate:"omitempty,gt=0"`
} }
@@ -15,7 +15,7 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
PhaseIDs string `query:"phase_ids" validate:"omitempty"` PhaseIDs string `query:"phase_ids" validate:"omitempty"`
} }
@@ -11,7 +11,7 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
Category *string `query:"category" validate:"omitempty"` Category *string `query:"category" validate:"omitempty"`
} }
@@ -12,6 +12,6 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
} }
@@ -36,7 +36,7 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
ProjectCategory string `query:"project_category" validate:"omitempty,oneof=GROWING LAYING"` ProjectCategory string `query:"project_category" validate:"omitempty,oneof=GROWING LAYING"`
} }
@@ -264,6 +264,18 @@ func (s productService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
db = db.Where("NOT "+existsQuery, entity.FlagableTypeProduct, depletionProductFlags) db = db.Where("NOT "+existsQuery, entity.FlagableTypeProduct, depletionProductFlags)
} }
} }
if strings.TrimSpace(params.Flags) != "" {
cleanFlags := utils.ParseFlags(params.Flags)
if len(cleanFlags) > 0 {
db = db.Where(`
EXISTS (
SELECT 1 FROM flags f
WHERE f.flagable_type = ?
AND f.flagable_id = products.id
AND UPPER(f.name) IN ?
)`, entity.FlagableTypeProduct, cleanFlags)
}
}
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}) })
@@ -46,4 +46,5 @@ type Query struct {
ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"` ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"`
IsDepletion *bool `query:"is_depletion" validate:"omitempty"` IsDepletion *bool `query:"is_depletion" validate:"omitempty"`
IncludeAll *bool `query:"include_all" validate:"omitempty"` IncludeAll *bool `query:"include_all" validate:"omitempty"`
Flags string `query:"flags" validate:"omitempty,max=200"`
} }
@@ -10,6 +10,6 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
} }
@@ -18,7 +18,7 @@ type Update struct {
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"` AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"` LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
@@ -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,8 +15,8 @@ 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)
route.Delete("/:id", ctrl.DeleteOne) route.Delete("/:id", ctrl.DeleteOne)
@@ -302,16 +302,22 @@ func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, project
return nil, nil return nil, nil
} }
var productCategoryCode string if projectFlockKandang.ProjectFlock.Category != string(utils.ProjectFlockCategoryGrowing) &&
if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryGrowing) { projectFlockKandang.ProjectFlock.Category != string(utils.ProjectFlockCategoryLaying) {
productCategoryCode = "DOC"
} else if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryLaying) {
productCategoryCode = "PULLET"
} else {
return nil, nil return nil, nil
} }
products, err := s.ProductWarehouseRepo.GetByFlagAndWarehouseID(c.Context(), productCategoryCode, warehouse.Id) ayamFlags := []string{
string(utils.FlagAyam),
string(utils.FlagDOC),
string(utils.FlagPullet),
}
ayamSubFlags := []string{
string(utils.FlagAyamAfkir),
string(utils.FlagAyamCulling),
string(utils.FlagAyamMati),
}
products, err := s.ProductWarehouseRepo.GetByFlagsAndWarehouseID(c.Context(), ayamFlags, ayamSubFlags, warehouse.Id)
if err != nil || len(products) == 0 { if err != nil || len(products) == 0 {
return nil, nil return nil, nil
} }
@@ -359,40 +359,6 @@ func applyCutOverLayingLookupOverride(result *dto.ProjectFlockKandangDTO) {
result.IsLaying = true result.IsLaying = true
} }
func (u *ProjectflockController) UpdateOne(c *fiber.Ctx) error {
param := c.Params("id")
req := new(validation.Update)
id, err := strconv.Atoi(param)
if err != nil || id <= 0 {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
result, err := u.ProjectflockService.UpdateOne(c, req, uint(id))
if err != nil {
return err
}
var period int
if periods, err := u.ProjectflockService.GetProjectPeriods(c, []uint{uint(id)}); err == nil {
if p, ok := periods[uint(id)]; ok {
period = p
}
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Update projectflock successfully",
Data: dto.ToProjectFlockListDTOWithPeriod(*result, period),
})
}
func (u *ProjectflockController) Resubmit(c *fiber.Ctx) error { func (u *ProjectflockController) Resubmit(c *fiber.Ctx) error {
param := c.Params("id") param := c.Params("id")
req := new(validation.Resubmit) req := new(validation.Resubmit)
@@ -120,6 +120,11 @@ func ToProjectFlockListDTOWithPeriod(e entity.ProjectFlock, period int) ProjectF
locationSummary = &mapped locationSummary = &mapped
} }
// Jika period tidak di-pass secara eksplisit (0), derive dari KandangHistory
if period == 0 && len(e.KandangHistory) > 0 {
period = e.KandangHistory[0].Period
}
latestApproval := defaultProjectFlockLatestApproval(e) latestApproval := defaultProjectFlockLatestApproval(e)
if e.LatestApproval != nil { if e.LatestApproval != nil {
snapshot := approvalDTO.ToApprovalDTO(*e.LatestApproval) snapshot := approvalDTO.ToApprovalDTO(*e.LatestApproval)
@@ -17,7 +17,6 @@ type ProjectFlockKandangRepository interface {
GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error) GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error)
GetActiveByKandangID(ctx context.Context, kandangID uint) (*entity.ProjectFlockKandang, error) GetActiveByKandangID(ctx context.Context, kandangID uint) (*entity.ProjectFlockKandang, error)
UpdateClosedAt(ctx context.Context, id uint, t *time.Time) error UpdateClosedAt(ctx context.Context, id uint, t *time.Time) error
UpdatePeriodByProjectFlockID(ctx context.Context, projectFlockID uint, period int) (int64, error)
CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error
DeleteMany(ctx context.Context, projectFlockID uint, kandangIDs []uint) error DeleteMany(ctx context.Context, projectFlockID uint, kandangIDs []uint) error
GetAll(ctx context.Context, offset int, limit int, modifier func(*gorm.DB) *gorm.DB) ([]entity.ProjectFlockKandang, int64, error) GetAll(ctx context.Context, offset int, limit int, modifier func(*gorm.DB) *gorm.DB) ([]entity.ProjectFlockKandang, int64, error)
@@ -526,19 +525,6 @@ func (r *projectFlockKandangRepositoryImpl) UpdateClosedAt(ctx context.Context,
Update("closed_at", t).Error Update("closed_at", t).Error
} }
// UpdatePeriodByProjectFlockID updates the period column on every pivot row that
// belongs to the given project flock. Returns the number of rows affected.
func (r *projectFlockKandangRepositoryImpl) UpdatePeriodByProjectFlockID(ctx context.Context, projectFlockID uint, period int) (int64, error) {
result := r.db.WithContext(ctx).
Model(&entity.ProjectFlockKandang{}).
Where("project_flock_id = ?", projectFlockID).
Update("period", period)
if result.Error != nil {
return 0, result.Error
}
return result.RowsAffected, nil
}
func (r *projectFlockKandangRepositoryImpl) HasOpenNewerPeriod(ctx context.Context, kandangID uint, currentPeriod int, excludeID *uint) (bool, error) { func (r *projectFlockKandangRepositoryImpl) HasOpenNewerPeriod(ctx context.Context, kandangID uint, currentPeriod int, excludeID *uint) (bool, error) {
if kandangID == 0 { if kandangID == 0 {
return false, nil return false, nil
@@ -15,14 +15,13 @@ func ProjectflockRoutes(v1 fiber.Router, u user.UserService, s projectflock.Proj
route := v1.Group("/project-flocks") route := v1.Group("/project-flocks")
route.Use(m.Auth(u)) route.Use(m.Auth(u))
route.Get("/", m.RequirePermissions(m.P_ProjectFlockGetAll), ctrl.GetAll) route.Get("/",m.RequirePermissions(m.P_ProjectFlockGetAll),ctrl.GetAll)
route.Post("/", m.RequirePermissions(m.P_ProjectFlockCreate), ctrl.CreateOne) route.Post("/",m.RequirePermissions(m.P_ProjectFlockCreate), ctrl.CreateOne)
route.Get("/:id", m.RequirePermissions(m.P_ProjectFlockGetOne), ctrl.GetOne) route.Get("/:id",m.RequirePermissions(m.P_ProjectFlockGetOne), ctrl.GetOne)
route.Patch("/:id", m.RequirePermissions(m.P_ProjectFlockCreate), ctrl.UpdateOne) route.Delete("/:id",m.RequirePermissions(m.P_ProjectFlockGetAll), ctrl.DeleteOne)
route.Delete("/:id", m.RequirePermissions(m.P_ProjectFlockGetAll), ctrl.DeleteOne) route.Get("/kandangs/lookup",m.RequirePermissions(m.P_ProjectFlockLookup), ctrl.LookupProjectFlockKandang)
route.Get("/kandangs/lookup", m.RequirePermissions(m.P_ProjectFlockLookup), ctrl.LookupProjectFlockKandang) route.Post("/approvals",m.RequirePermissions(m.P_ProjectFlockApprove), ctrl.Approval)
route.Post("/approvals", m.RequirePermissions(m.P_ProjectFlockApprove), ctrl.Approval) route.Get("/locations/:location_id/periods",m.RequirePermissions(m.P_ProjectFlockNextPeriod), ctrl.GetPeriodSummary)
route.Get("/locations/:location_id/periods", m.RequirePermissions(m.P_ProjectFlockNextPeriod), ctrl.GetPeriodSummary) route.Put("/:id/resubmit",m.RequirePermissions(m.P_ProjectFlockResubmit), ctrl.Resubmit)
route.Put("/:id/resubmit", m.RequirePermissions(m.P_ProjectFlockResubmit), ctrl.Resubmit)
} }
@@ -51,7 +51,6 @@ type ProjectflockService interface {
GetProjectPeriods(ctx *fiber.Ctx, projectIDs []uint) (map[uint]int, error) GetProjectPeriods(ctx *fiber.Ctx, projectIDs []uint) (map[uint]int, error)
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlock, error) Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlock, error)
Resubmit(ctx *fiber.Ctx, req *validation.Resubmit, id uint) (*entity.ProjectFlock, error) Resubmit(ctx *fiber.Ctx, req *validation.Resubmit, id uint) (*entity.ProjectFlock, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectFlock, error)
EnsureProjectFlockApproved(ctx context.Context, projectFlockID uint) error EnsureProjectFlockApproved(ctx context.Context, projectFlockID uint) error
} }
@@ -375,7 +374,32 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error { err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
projectRepo := repository.NewProjectflockRepository(dbTransaction) projectRepo := repository.NewProjectflockRepository(dbTransaction)
generatedName, _, err := s.generateSequentialFlockName(c.Context(), projectRepo, canonicalBase, 1, nil) var periods map[uint]int
if req.Periode != nil {
// Pakai periode yang diminta untuk semua kandang
periods = make(map[uint]int, len(kandangIDs))
for _, kandangID := range kandangIDs {
periods[kandangID] = *req.Periode
}
} else {
periods, err = projectRepo.GetNextPeriodsForKandangs(c.Context(), kandangIDs)
if err != nil {
return err
}
}
startPeriod := 1
if req.Periode != nil {
startPeriod = *req.Periode
} else {
for _, p := range periods {
if p > startPeriod {
startPeriod = p
}
}
}
generatedName, _, err := s.generateSequentialFlockName(c.Context(), projectRepo, canonicalBase, startPeriod, nil)
if err != nil { if err != nil {
return err return err
} }
@@ -385,10 +409,6 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
return err return err
} }
periods, err := projectRepo.GetNextPeriodsForKandangs(c.Context(), kandangIDs)
if err != nil {
return err
}
if err := s.attachKandangs(c.Context(), dbTransaction, createBody.Id, kandangIDs, periods); err != nil { if err := s.attachKandangs(c.Context(), dbTransaction, createBody.Id, kandangIDs, periods); err != nil {
return err return err
} }
@@ -1226,12 +1246,34 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, id
} }
} }
// Hitung newFlockName sebelum membuka transaksi (fast-path conflict check)
var newFlockName string
if req.Periode != nil {
lastSpace := strings.LastIndex(existing.FlockName, " ")
if lastSpace < 0 {
return nil, fiber.NewError(fiber.StatusInternalServerError, "Format flock name tidak valid")
}
baseName := strings.TrimSpace(existing.FlockName[:lastSpace])
newFlockName = fmt.Sprintf("%s %03d", baseName, *req.Periode)
taken, err := s.Repository.ExistsByFlockName(c.Context(), newFlockName, &id)
if err != nil {
return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa nama flock")
}
if taken {
return nil, fiber.NewError(fiber.StatusConflict,
fmt.Sprintf("Nama flock '%s' sudah digunakan oleh project flock lain", newFlockName))
}
}
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error { err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction)) approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
var period int = 1 var period int = 1
if len(existing.KandangHistory) > 0 { if req.Periode != nil {
period = *req.Periode
} else if len(existing.KandangHistory) > 0 {
period = existing.KandangHistory[0].Period period = existing.KandangHistory[0].Period
} }
@@ -1243,6 +1285,40 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, id
if err := s.attachKandangs(c.Context(), dbTransaction, existing.Id, kandangIDs, periods); err != nil { if err := s.attachKandangs(c.Context(), dbTransaction, existing.Id, kandangIDs, periods); err != nil {
return err return err
} }
// Update period pada SEMUA row project_flock_kandangs milik flock ini.
// attachKandangs hanya INSERT baris baru dan melewati yang sudah ada,
// sehingga period pada baris lama tidak terupdate tanpa langkah ini.
if req.Periode != nil {
if err := dbTransaction.WithContext(c.Context()).
Model(&entity.ProjectFlockKandang{}).
Where("project_flock_id = ?", existing.Id).
Update("period", *req.Periode).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memperbarui periode kandang")
}
}
// Update flock_name sesuai periode baru.
if req.Periode != nil {
projectRepoTx := repository.NewProjectflockRepository(dbTransaction)
// Re-check di dalam transaksi untuk cegah race condition.
taken, err := projectRepoTx.ExistsByFlockName(c.Context(), newFlockName, &existing.Id)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa nama flock")
}
if taken {
return fiber.NewError(fiber.StatusConflict,
fmt.Sprintf("Nama flock '%s' sudah digunakan oleh project flock lain", newFlockName))
}
if err := projectRepoTx.PatchOne(c.Context(), existing.Id, map[string]any{
"flock_name": newFlockName,
}, nil); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memperbarui nama flock")
}
}
if err := s.UpsertProjectBudget(c.Context(), dbTransaction, existing.Id, req.ProjectBudgets); err != nil { if err := s.UpsertProjectBudget(c.Context(), dbTransaction, existing.Id, req.ProjectBudgets); err != nil {
return err return err
} }
@@ -1274,52 +1350,6 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, id
return s.getOneEntityOnly(c, id) return s.getOneEntityOnly(c, id)
} }
// UpdateOne updates mutable fields of a project flock.
// Currently only the `period` is updatable; the value is applied to every
// project_flock_kandang pivot row belonging to the project flock so it stays
// consistent with how periods are provisioned in CreateOne/Resubmit.
func (s projectflockService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectFlock, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
if req.Period == nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "period is required")
}
existing, err := s.Repository.GetByID(c.Context(), id, nil)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Projectflock not found")
}
if err != nil {
s.Log.Errorf("Failed to fetch projectflock %d before update: %+v", id, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flock")
}
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
affected, err := s.pivotRepoWithTx(dbTransaction).UpdatePeriodByProjectFlockID(c.Context(), existing.Id, *req.Period)
if err != nil {
return err
}
if affected == 0 {
return fiber.NewError(fiber.StatusBadRequest, "Project flock tidak memiliki kandang yang dapat diperbarui periodenya")
}
return nil
})
if err != nil {
if fiberErr, ok := err.(*fiber.Error); ok {
return nil, fiberErr
}
if errors.Is(err, gorm.ErrDuplicatedKey) {
return nil, fiber.NewError(fiber.StatusConflict, "Project flock period already exists")
}
s.Log.Errorf("Failed to update projectflock %d period: %+v", id, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update project flock")
}
return s.getOneEntityOnly(c, id)
}
func (s projectflockService) UpsertProjectBudget(ctx context.Context, dbTransaction *gorm.DB, projectFlockID uint, budgets []validation.ProjectBudget) error { func (s projectflockService) UpsertProjectBudget(ctx context.Context, dbTransaction *gorm.DB, projectFlockID uint, budgets []validation.ProjectBudget) error {
if len(budgets) == 0 { if len(budgets) == 0 {
@@ -6,6 +6,7 @@ type Create struct {
Category string `json:"category" validate:"required_strict"` Category string `json:"category" validate:"required_strict"`
ProductionStandardId uint `json:"production_standard_id" validate:"required_strict,number,gt=0"` ProductionStandardId uint `json:"production_standard_id" validate:"required_strict,number,gt=0"`
LocationId uint `json:"location_id" validate:"required_strict,number,gt=0"` LocationId uint `json:"location_id" validate:"required_strict,number,gt=0"`
Periode *int `json:"periode" validate:"omitempty,gt=0"`
KandangIds []uint `json:"kandang_ids" validate:"required,min=1,dive,gt=0"` KandangIds []uint `json:"kandang_ids" validate:"required,min=1,dive,gt=0"`
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required,min=1,dive"` ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required,min=1,dive"`
} }
@@ -40,8 +41,5 @@ type ProjectBudget struct {
type Resubmit struct { type Resubmit struct {
KandangIds []uint `json:"kandang_ids" validate:"required_strict,min=1,dive,gt=0"` KandangIds []uint `json:"kandang_ids" validate:"required_strict,min=1,dive,gt=0"`
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"` ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"`
} Periode *int `json:"periode" validate:"omitempty,gt=0"`
type Update struct {
Period *int `json:"period" validate:"required,number,gt=0"`
} }
@@ -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
@@ -104,19 +108,23 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
} }
headerValues := map[string]string{ headerValues := map[string]string{
"A1": "No", "A1": "No",
"B1": "Lokasi", "B1": "Lokasi",
"C1": "Flock", "C1": "Flock",
"D1": "Kandang", "D1": "Kandang",
"E1": "Periode", "E1": "Periode",
"F1": "Kategori", "F1": "Kategori",
"G1": "Umur (hari)", "G1": "Umur (hari)",
"H1": "Waktu Recording", "H1": "Waktu Recording",
"I1": "Populasi Akhir", "I1": "Populasi Akhir",
"Y1": "Status Approval", "Y1": "Status Approval",
"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,13 +92,20 @@ 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"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
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),
} }
} }
@@ -27,6 +27,7 @@ import (
rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories" rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services" sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services"
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories" rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
rSystemSettings "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/repositories"
"gitlab.com/mbugroup/lti-api.git/internal/utils" "gitlab.com/mbugroup/lti-api.git/internal/utils"
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo" "gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
@@ -159,6 +160,8 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
validate, validate,
) )
systemSettingRepo := rSystemSettings.NewSystemSettingRepository(db)
recordingService := sRecording.NewRecordingService( recordingService := sRecording.NewRecordingService(
recordingRepo, recordingRepo,
projectFlockKandangRepo, projectFlockKandangRepo,
@@ -174,6 +177,7 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
transferLayingRepo, transferLayingRepo,
transferLayingService, transferLayingService,
validate, validate,
systemSettingRepo,
) )
userService := sUser.NewUserService(userRepo, validate) userService := sUser.NewUserService(userRepo, validate)
@@ -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,
@@ -24,6 +24,7 @@ import (
rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories" rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services" sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services"
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories" rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
rSystemSettings "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/repositories"
"gitlab.com/mbugroup/lti-api.git/internal/utils" "gitlab.com/mbugroup/lti-api.git/internal/utils"
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals" approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
fifo "gitlab.com/mbugroup/lti-api.git/internal/utils/fifo" fifo "gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
@@ -63,6 +64,7 @@ type recordingService struct {
TransferLayingSvc sTransferLaying.TransferLayingService TransferLayingSvc sTransferLaying.TransferLayingService
FifoStockV2Svc commonSvc.FifoStockV2Service FifoStockV2Svc commonSvc.FifoStockV2Service
StockLogRepo rStockLogs.StockLogRepository StockLogRepo rStockLogs.StockLogRepository
SystemSettingRepo rSystemSettings.SystemSettingRepository
} }
func NewRecordingService( func NewRecordingService(
@@ -80,6 +82,7 @@ func NewRecordingService(
transferLayingRepo rTransferLaying.TransferLayingRepository, transferLayingRepo rTransferLaying.TransferLayingRepository,
transferLayingSvc sTransferLaying.TransferLayingService, transferLayingSvc sTransferLaying.TransferLayingService,
validate *validator.Validate, validate *validator.Validate,
systemSettingRepo rSystemSettings.SystemSettingRepository,
) RecordingService { ) RecordingService {
return &recordingService{ return &recordingService{
Log: utils.Log, Log: utils.Log,
@@ -97,6 +100,7 @@ func NewRecordingService(
TransferLayingSvc: transferLayingSvc, TransferLayingSvc: transferLayingSvc,
FifoStockV2Svc: fifoStockV2Svc, FifoStockV2Svc: fifoStockV2Svc,
StockLogRepo: stockLogRepo, StockLogRepo: stockLogRepo,
SystemSettingRepo: systemSettingRepo,
} }
} }
@@ -169,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() {
@@ -188,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
} }
@@ -390,6 +425,11 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
return nil, err return nil, err
} }
req.Stocks, err = s.resolveStocksForMigrationMode(ctx, req.Stocks, pfk, actorID)
if err != nil {
return nil, err
}
if err := s.ensureProductWarehousesExist(c, req.Stocks, req.Depletions, req.Eggs); err != nil { if err := s.ensureProductWarehousesExist(c, req.Stocks, req.Depletions, req.Eggs); err != nil {
return nil, err return nil, err
} }
@@ -635,6 +675,10 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
if match { if match {
hasStockChanges = false hasStockChanges = false
} else { } else {
req.Stocks, err = s.resolveStocksForMigrationMode(ctx, req.Stocks, pfkForRoute, actorID)
if err != nil {
return err
}
if err := s.ensureProductWarehousesExist(c, req.Stocks, nil, nil); err != nil { if err := s.ensureProductWarehousesExist(c, req.Stocks, nil, nil); err != nil {
return err return err
} }
@@ -755,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 {
@@ -772,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 {
@@ -1295,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
@@ -2205,6 +2331,104 @@ func (s *recordingService) validateWarehouseIDs(
return nil return nil
} }
// resolveMigrationWarehouseID returns the warehouse ID to use when auto-creating product_warehouse
// entries in migration mode. Prefers the farm-level (LOKASI) warehouse of the kandang's location;
// falls back to the kandang-level (KANDANG) warehouse if no LOKASI warehouse exists.
func (s *recordingService) resolveMigrationWarehouseID(ctx context.Context, kandangID uint) (uint, error) {
type row struct {
ID uint `gorm:"column:id"`
LocationID *uint `gorm:"column:location_id"`
}
db := s.ProductWarehouseRepo.DB().WithContext(ctx)
// Step 1: get the kandang's location_id
var kandang row
if err := db.Table("kandangs").Select("id, location_id").
Where("id = ? AND deleted_at IS NULL", kandangID).
Limit(1).Take(&kandang).Error; err != nil {
return 0, fmt.Errorf("kandang %d tidak ditemukan: %w", kandangID, err)
}
// Step 2: prefer a LOKASI-type warehouse at the kandang's location (farm-level)
if kandang.LocationID != nil && *kandang.LocationID != 0 {
var lokasi row
err := db.Table("warehouses").Select("id").
Where("type = 'LOKASI' AND location_id = ? AND deleted_at IS NULL", *kandang.LocationID).
Limit(1).Take(&lokasi).Error
if err == nil {
return lokasi.ID, nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return 0, fmt.Errorf("gagal mencari warehouse LOKASI untuk location %d: %w", *kandang.LocationID, err)
}
}
// Step 3: fall back to the KANDANG-type warehouse
var kandangWH row
if err := db.Table("warehouses").Select("id").
Where("type = 'KANDANG' AND kandang_id = ? AND deleted_at IS NULL", kandangID).
Limit(1).Take(&kandangWH).Error; err != nil {
return 0, fmt.Errorf("warehouse tidak ditemukan untuk kandang %d: %w", kandangID, err)
}
return kandangWH.ID, nil
}
// resolveStocksForMigrationMode handles stocks that use product_id (instead of product_warehouse_id)
// when migration mode (allow_negative_pakan_ovk) is enabled. It finds or creates product_warehouse
// entries in the farm-level (LOKASI) warehouse, falling back to the kandang warehouse, then sets
// the resolved product_warehouse_id on each stock item.
func (s *recordingService) resolveStocksForMigrationMode(
ctx context.Context,
stocks []validation.Stock,
pfk *entity.ProjectFlockKandang,
actorID uint,
) ([]validation.Stock, error) {
if s.SystemSettingRepo == nil {
return stocks, nil
}
allowed, err := s.SystemSettingRepo.GetAllowNegativePakanOVK(ctx)
if err != nil {
s.Log.Errorf("Failed to read allow_negative_pakan_ovk setting: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal membaca konfigurasi sistem")
}
if !allowed {
return stocks, nil
}
var warehouseID uint
warehouseResolved := false
result := make([]validation.Stock, len(stocks))
copy(result, stocks)
for i := range result {
stock := &result[i]
if stock.ProductId == nil || stock.ProductWarehouseId != 0 {
continue
}
// Resolve target warehouse lazily on first need (same for all stocks in one request)
if !warehouseResolved {
if pfk == nil || pfk.KandangId == 0 {
return nil, fiber.NewError(fiber.StatusBadRequest, "Kandang tidak ditemukan untuk mode migrasi")
}
warehouseID, err = s.resolveMigrationWarehouseID(ctx, pfk.KandangId)
if err != nil {
s.Log.Errorf("Failed to resolve migration warehouse for kandang %d: %+v", pfk.KandangId, err)
return nil, fiber.NewError(fiber.StatusBadRequest, "Warehouse tidak ditemukan untuk mode migrasi")
}
warehouseResolved = true
}
pwID, err := s.ProductWarehouseRepo.EnsureProductWarehouse(ctx, *stock.ProductId, warehouseID, nil, actorID)
if err != nil {
s.Log.Errorf("Failed to ensure product warehouse for product %d in warehouse %d: %+v", *stock.ProductId, warehouseID, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Gagal menyiapkan product warehouse untuk produk %d", *stock.ProductId))
}
stock.ProductWarehouseId = pwID
}
return result, nil
}
func (s *recordingService) computeRecordingDay(ctx context.Context, projectFlockKandangID uint, recordTime time.Time) (int, error) { func (s *recordingService) computeRecordingDay(ctx context.Context, projectFlockKandangID uint, recordTime time.Time) (int, error) {
if projectFlockKandangID == 0 { if projectFlockKandangID == 0 {
return 0, fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid") return 0, fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
@@ -2236,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 {
@@ -2397,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)
@@ -2897,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]
@@ -2904,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{
@@ -3561,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
@@ -4,7 +4,8 @@ import "time"
type ( type (
Stock struct { Stock struct {
ProductWarehouseId uint `json:"product_warehouse_id" validate:"required,number,min=1"` ProductWarehouseId uint `json:"product_warehouse_id" validate:"omitempty,number,min=1"`
ProductId *uint `json:"product_id,omitempty" validate:"omitempty,number,min=1"`
Qty float64 `json:"qty" validate:"required,gte=0"` Qty float64 `json:"qty" validate:"required,gte=0"`
} }
@@ -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
}
@@ -283,6 +283,32 @@ func validatePurchaseDocumentSizes(files []*multipart.FileHeader) error {
return nil return nil
} }
func (ctrl *PurchaseController) UpdatePoDate(c *fiber.Ctx) error {
param := c.Params("id")
id, err := strconv.Atoi(param)
if err != nil || id == 0 {
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
}
req := new(validation.UpdatePoDateRequest)
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
result, err := ctrl.service.UpdatePoDate(c, uint(id), req)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Purchase PO date updated successfully",
Data: dto.ToPurchaseDetailDTO(*result),
})
}
func (ctrl *PurchaseController) DeleteItems(c *fiber.Ctx) error { func (ctrl *PurchaseController) DeleteItems(c *fiber.Ctx) error {
param := c.Params("id") param := c.Params("id")
id, err := strconv.Atoi(param) id, err := strconv.Atoi(param)
@@ -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{
@@ -78,12 +75,14 @@ func setPurchaseExportColumns(file *excelize.File, sheet string) error {
"A": 16, "A": 16,
"B": 16, "B": 16,
"C": 14, "C": 14,
"D": 22, "D": 14,
"E": 22, "E": 22,
"F": 18, "F": 22,
"G": 18, "G": 22,
"H": 52, "H": 32,
"I": 24, "I": 18,
"J": 18,
"K": 24,
} }
for col, width := range columnWidths { for col, width := range columnWidths {
@@ -103,11 +102,13 @@ func setPurchaseExportHeaders(file *excelize.File, sheet string) error {
"PR Number", "PR Number",
"PO Number", "PO Number",
"Tanggal PO", "Tanggal PO",
"Tanggal Terima",
"Supplier", "Supplier",
"Lokasi", "Lokasi",
"Gudang",
"Product",
"Status", "Status",
"Grand Total", "Grand Total",
"Products",
"Notes", "Notes",
} }
@@ -136,46 +137,34 @@ func setPurchaseExportHeaders(file *excelize.File, sheet string) error {
return err return err
} }
return file.SetCellStyle(sheet, "A1", "I1", 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]
return err total := grandTotals[purchase.Id]
if len(purchase.Items) == 0 {
if err := writePurchaseExportRow(file, sheet, rowIdx, purchase, nil, total); err != nil {
return err
}
rowIdx++
continue
} }
if err := file.SetCellValue(sheet, "B"+row, safePurchaseExportPointerText(item.PoNumber)); err != nil { for it := range purchase.Items {
return err if err := writePurchaseExportRow(file, sheet, rowIdx, purchase, &purchase.Items[it], total); err != nil {
} return err
if err := file.SetCellValue(sheet, "C"+row, formatPurchaseExportDate(item.PoDate)); err != nil { }
return err rowIdx++
}
if err := file.SetCellValue(sheet, "D"+row, safePurchaseSupplierName(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "E"+row, safePurchaseLocationName(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "F"+row, formatPurchaseExportStatus(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "G"+row, formatPurchaseRupiah(grandTotals[item.Id])); err != nil {
return err
}
if err := file.SetCellValue(sheet, "H"+row, formatPurchaseProducts(item)); err != nil {
return err
}
if err := file.SetCellValue(sheet, "I"+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",
@@ -192,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", "I"+strconv.Itoa(lastRow), dataStyle); err != nil { if err := file.SetCellStyle(sheet, "A2", "K"+strconv.Itoa(lastRow), dataStyle); err != nil {
return err return err
} }
@@ -212,7 +201,59 @@ func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.Purcha
return err return err
} }
return file.SetCellStyle(sheet, "G2", "G"+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 {
@@ -227,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 {
@@ -268,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 "-"

Some files were not shown because too many files have changed in this diff Show More