mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-21 13:55:43 +00:00
Compare commits
128 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5511dc78dc | |||
| 699e4448d1 | |||
| 6f02387d69 | |||
| fc5d5d8ad4 | |||
| 0d6ab5e718 | |||
| 547fc309f5 | |||
| 094e8f904b | |||
| 0d928d5856 | |||
| 0357531e73 | |||
| 2fa279c073 | |||
| 90ed035abd | |||
| 81b9e88bb6 | |||
| 7e01d8afb9 | |||
| d5a98b95dc | |||
| 8900937e71 | |||
| cad80e2216 | |||
| 34dad85734 | |||
| b1d2d30773 | |||
| f910d165e4 | |||
| 6f6985ef32 | |||
| d07f074fb1 | |||
| 561481679e | |||
| d86577f007 | |||
| 49ea3f0295 | |||
| 7bab8c66c1 | |||
| f9de4d28f9 | |||
| 45028212e1 | |||
| 0f285dc684 | |||
| d0cd82c703 | |||
| 48351661c5 | |||
| 19d7cd33ca | |||
| 03474dc1fa | |||
| 916fa4205b | |||
| b2be67e052 | |||
| 0ac40adb5a | |||
| cee59c2b99 | |||
| da99bf1429 | |||
| 28fd711ece | |||
| 3768892a17 | |||
| c804c59f05 | |||
| b219bf829f | |||
| f97b1a6484 | |||
| 0d2cdef10f | |||
| 128c8e0d08 | |||
| cf4e723f64 | |||
| a3156a156f | |||
| 16ac54ff39 | |||
| 196bbf4277 | |||
| d3053d6417 | |||
| 27302ea6b5 | |||
| 76ac1d1671 | |||
| 44cbc1b304 | |||
| e4d4bd9483 | |||
| cea79bc569 | |||
| 38cfc6b103 | |||
| 80e4a04875 | |||
| 9513da2a7c | |||
| 874cefda45 | |||
| 795f201a0b | |||
| 49d684ebbe | |||
| 6f6541d4c1 | |||
| 1d6e1fa5be | |||
| aadd87852b | |||
| ac69ae054a | |||
| 7cc5c39092 | |||
| 706303980b | |||
| d7a2a5a2ed | |||
| ae47493109 | |||
| 9726303eeb | |||
| b764d2c3c0 | |||
| eefc9850e1 | |||
| 732ebd423d | |||
| 27d076b817 | |||
| 9a9cc114fe | |||
| af5f3dc7d4 | |||
| e1f8d357c5 | |||
| f6b37926e9 | |||
| 4e7c0e64ad | |||
| e79fde2408 | |||
| 28394ee727 | |||
| 42c088e772 | |||
| df45803f8d | |||
| 6033535894 | |||
| 62723e5ab2 | |||
| f1d7966e2f | |||
| b94b16ab73 | |||
| 9fd9c441d3 | |||
| f5d986bf27 | |||
| 1e9a51b8cd | |||
| 512d616867 | |||
| 50ce780c2c | |||
| bacc7217b3 | |||
| 878b8ccce9 | |||
| 5c39237ac0 | |||
| 0fc6096637 | |||
| 8389bd579b | |||
| cf9e60c64d | |||
| 2dcfd9efde | |||
| adcd4dc8fb | |||
| 9a19bff8d3 | |||
| 596b5ed2fb | |||
| 20479c2ca2 | |||
| a79213882d | |||
| befb351e49 | |||
| 5d38dfac69 | |||
| 26a04d3bdd | |||
| 044649abc9 | |||
| 83eab9de17 | |||
| 940899dad8 | |||
| ffccd6daee | |||
| 7f9604bcb4 | |||
| e5e090aefe | |||
| 552de15c1a | |||
| 8e396d758f | |||
| 84b76a4b2d | |||
| 7c4664844c | |||
| 47d2371b7f | |||
| 5bfd93e300 | |||
| 3fa75e0a31 | |||
| 101a83cd9e | |||
| 13fc537171 | |||
| 46737e4a96 | |||
| 06a23ce1b5 | |||
| fb9915759b | |||
| f0e364f884 | |||
| 65299d7913 | |||
| 88b6e2f294 | |||
| 36b0f97897 |
+18
-4
@@ -27,10 +27,24 @@ workflow:
|
||||
.ecr_login: &ecr_login |
|
||||
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_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"
|
||||
|
||||
HAS_ACCESS_KEY="false"
|
||||
HAS_SECRET_KEY="false"
|
||||
if [ -n "${AWS_ACCESS_KEY_ID:-}" ]; then
|
||||
HAS_ACCESS_KEY="true"
|
||||
fi
|
||||
if [ -n "${AWS_SECRET_ACCESS_KEY:-}" ]; then
|
||||
HAS_SECRET_KEY="true"
|
||||
fi
|
||||
|
||||
if [ "$HAS_ACCESS_KEY" = "true" ] && [ "$HAS_SECRET_KEY" = "true" ]; then
|
||||
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID"
|
||||
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY"
|
||||
if [ -n "${AWS_SESSION_TOKEN:-}" ]; then
|
||||
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
|
||||
|
||||
PASS="$(docker run --rm $AWS_CLI_ENV_ARGS public.ecr.aws/aws-cli/aws-cli:latest \
|
||||
|
||||
Executable
BIN
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,12 +26,14 @@ const (
|
||||
)
|
||||
|
||||
type options struct {
|
||||
Apply bool
|
||||
Output string
|
||||
AreaName string
|
||||
KandangLocationName string
|
||||
DBSSLMode string
|
||||
DeleteKandangWarehouses bool
|
||||
Apply bool
|
||||
Output string
|
||||
AreaName string
|
||||
KandangLocationName string
|
||||
DBSSLMode string
|
||||
DeleteKandangWarehouses bool
|
||||
SkipBlockedRefsCheck bool
|
||||
SkipIncompleteLocations bool
|
||||
}
|
||||
|
||||
type consolidateRow struct {
|
||||
@@ -132,7 +134,7 @@ func main() {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -157,6 +159,8 @@ func parseFlags() (*options, error) {
|
||||
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.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()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// If skipping incomplete locations, filter out NULL farm warehouses
|
||||
whereClause := ""
|
||||
if opts.SkipIncompleteLocations {
|
||||
whereClause = "AND fw.id IS NOT NULL"
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
SELECT
|
||||
a.name AS area_name,
|
||||
@@ -224,7 +234,10 @@ JOIN product_warehouses wp
|
||||
ON wp.warehouse_id = w.id
|
||||
JOIN products p
|
||||
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
|
||||
ON fpw.product_id = wp.product_id
|
||||
AND fpw.warehouse_id = fw.id
|
||||
@@ -246,8 +259,11 @@ WHERE w.deleted_at IS NULL
|
||||
AND sa.deleted_at IS NULL
|
||||
)
|
||||
%s
|
||||
%s
|
||||
ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC
|
||||
`, andClause(filters))
|
||||
`,
|
||||
andClause(filters),
|
||||
whereClause)
|
||||
|
||||
rows := make([]consolidateRow, 0)
|
||||
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
|
||||
}
|
||||
|
||||
func runPrechecks(ctx context.Context, db *gorm.DB, rows []consolidateRow, refs *referencePlan) error {
|
||||
if err := ensureNoBlockedWarehouseRefsConsolidate(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
|
||||
return err
|
||||
func runPrechecks(ctx context.Context, db *gorm.DB, rows []consolidateRow, refs *referencePlan, opts *options) error {
|
||||
// Only check blocked references if we're actually deleting the warehouses
|
||||
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 {
|
||||
return err
|
||||
@@ -710,6 +729,9 @@ func reflowAndRecalculateProductWarehouse(
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err)
|
||||
}
|
||||
if flagGroupCode == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
|
||||
FlagGroupCode: flagGroupCode,
|
||||
@@ -759,6 +781,9 @@ func resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, produc
|
||||
Order("rr.id ASC").
|
||||
Limit(1).
|
||||
Take(&selected).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -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(¬es, "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
|
||||
}
|
||||
@@ -26,12 +26,13 @@ const (
|
||||
)
|
||||
|
||||
type options struct {
|
||||
Apply bool
|
||||
Output string
|
||||
AreaName string
|
||||
KandangLocationName string
|
||||
DBSSLMode string
|
||||
DeleteWrongWarehouses bool
|
||||
Apply bool
|
||||
Output string
|
||||
AreaName string
|
||||
KandangLocationName string
|
||||
DBSSLMode string
|
||||
DeleteWrongWarehouses bool
|
||||
AllowMovingAllocatedStocks bool
|
||||
}
|
||||
|
||||
type planRow struct {
|
||||
@@ -123,6 +124,16 @@ func main() {
|
||||
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 {
|
||||
fmt.Println("No misplaced PAKAN/OVK stocks found in wrong-location warehouses")
|
||||
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.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.AllowMovingAllocatedStocks, "allow-moving-allocated-stocks", false, "Allow moving stocks that have active allocations (use with caution - for old recordings with completed allocations)")
|
||||
flag.Parse()
|
||||
|
||||
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
|
||||
@@ -175,6 +187,90 @@ func parseFlags() (*options, error) {
|
||||
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) {
|
||||
filters := make([]string, 0, 2)
|
||||
args := make([]any, 0, 2)
|
||||
@@ -225,7 +321,10 @@ JOIN product_warehouses wp
|
||||
ON wp.warehouse_id = w.id
|
||||
JOIN products p
|
||||
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
|
||||
ON cpw.product_id = wp.product_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 {
|
||||
if err := ensureNoBlockedWarehouseRefs(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
|
||||
return err
|
||||
if opts.DeleteWrongWarehouses {
|
||||
if err := ensureNoBlockedWarehouseRefs(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := ensureNoPurchaseItemWarehouseConflicts(ctx, db, rows); err != nil {
|
||||
return err
|
||||
@@ -714,6 +815,9 @@ func reflowAndRecalculateProductWarehouse(
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err)
|
||||
}
|
||||
if flagGroupCode == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
|
||||
FlagGroupCode: flagGroupCode,
|
||||
@@ -763,6 +867,9 @@ func resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, produc
|
||||
Order("rr.id ASC").
|
||||
Limit(1).
|
||||
Take(&selected).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ const (
|
||||
outputTable = "table"
|
||||
outputJSON = "json"
|
||||
|
||||
caseA = "A"
|
||||
caseB = "B"
|
||||
caseAll = "all"
|
||||
caseA = "A"
|
||||
caseB = "B"
|
||||
caseAll = "ALL"
|
||||
)
|
||||
|
||||
type options struct {
|
||||
@@ -39,7 +39,7 @@ type sourceWarehouseCheck struct {
|
||||
KandangName string `json:"kandang_name"`
|
||||
SourceWarehouseID uint `json:"source_warehouse_id"`
|
||||
SourceWarehouseName string `json:"source_warehouse_name"`
|
||||
Case string `json:"case"`
|
||||
Case string `json:"case" gorm:"column:case_type"`
|
||||
DeletedAt *string `json:"deleted_at"`
|
||||
StockInProductWH float64 `json:"stock_in_product_wh"`
|
||||
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) {
|
||||
filters := buildFilters(opts)
|
||||
filters, args := buildFilters(opts)
|
||||
query := fmt.Sprintf(`
|
||||
WITH case_a_warehouses AS (
|
||||
-- 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
|
||||
),
|
||||
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
|
||||
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
|
||||
asw.area_name,
|
||||
@@ -198,7 +216,7 @@ SELECT
|
||||
COALESCE(SUM(pw.qty), 0) AS stock_in_product_wh,
|
||||
COUNT(DISTINCT pi.id) AS active_purchase_items
|
||||
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 purchase_items pi ON pi.warehouse_id = w.id
|
||||
WHERE true
|
||||
@@ -216,7 +234,7 @@ ORDER BY asw.area_name ASC, asw.kandang_location_name ASC, w.name ASC
|
||||
`, andClause(filters))
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
filters := buildFilters(opts)
|
||||
filters, args := buildFilters(opts)
|
||||
query := fmt.Sprintf(`
|
||||
SELECT
|
||||
a.name AS area_name,
|
||||
@@ -252,12 +270,11 @@ SELECT
|
||||
COALESCE(SUM(sl.stock), 0) AS stock_logs_total,
|
||||
COUNT(DISTINCT sl.id) AS stock_logs_count
|
||||
FROM warehouses fw
|
||||
JOIN locations loc ON loc.id = fw.location_id
|
||||
JOIN areas a ON a.id = loc.area_id
|
||||
JOIN kandangs k ON k.location_id = fw.location_id AND k.deleted_at IS NULL
|
||||
JOIN locations kl ON kl.id = k.location_id
|
||||
JOIN products p ON UPPER(COALESCE(p.type, '')) IN ('PAKAN', 'OVK')
|
||||
LEFT JOIN product_warehouses pw ON pw.warehouse_id = fw.id AND pw.product_id = p.id
|
||||
JOIN locations kl ON kl.id = fw.location_id
|
||||
JOIN areas a ON a.id = kl.area_id
|
||||
JOIN product_warehouses pw ON pw.warehouse_id = fw.id
|
||||
JOIN products p ON p.id = pw.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 stock_logs sl ON sl.product_warehouse_id = pw.id
|
||||
WHERE fw.deleted_at IS NULL
|
||||
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))
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -316,7 +333,6 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
|
||||
{"purchase_items", "warehouse_id"},
|
||||
{"stock_transfers", "from_warehouse_id"},
|
||||
{"stock_transfers", "to_warehouse_id"},
|
||||
{"fifo_stock_v2_operation_log", "warehouse_id"},
|
||||
}
|
||||
|
||||
for _, ref := range refChecks {
|
||||
@@ -328,17 +344,17 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
// Get the specific warehouse IDs
|
||||
// Get the specific warehouse IDs using raw SQL
|
||||
var ids []uint
|
||||
if err := db.Table(ref.table).
|
||||
Where(fmt.Sprintf("%s IN ?", ref.column), warehouseIDs).
|
||||
Pluck(ref.column, &ids).Error; err != nil {
|
||||
query := fmt.Sprintf("SELECT DISTINCT %s FROM %s WHERE %s IN ?",
|
||||
ref.column, ref.table, ref.column)
|
||||
if err := db.Raw(query, warehouseIDs).Scan(&ids).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
idStrs := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
idStrs[i] = fmt.Sprintf("%d", id)
|
||||
idStrs := make([]string, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
idStrs = append(idStrs, fmt.Sprintf("%d", id))
|
||||
}
|
||||
|
||||
results = append(results, orphanedReferenceCheck{
|
||||
@@ -353,15 +369,18 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func buildFilters(opts *options) []string {
|
||||
func buildFilters(opts *options) ([]string, []any) {
|
||||
filters := make([]string, 0, 2)
|
||||
args := make([]any, 0, 2)
|
||||
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 != "" {
|
||||
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 {
|
||||
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -52,6 +52,7 @@ require (
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // 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/universal-translator v0.18.1 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
|
||||
@@ -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/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
|
||||
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/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
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)
|
||||
GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (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)
|
||||
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) {
|
||||
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 {
|
||||
now := time.Now()
|
||||
date = &now
|
||||
}
|
||||
|
||||
var totals struct {
|
||||
var recordingTotals struct {
|
||||
TotalPieces float64
|
||||
TotalWeightKg float64
|
||||
}
|
||||
err := r.db.WithContext(ctx).
|
||||
err = r.db.WithContext(ctx).
|
||||
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").
|
||||
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
||||
Where("r.record_datetime <= ?", *date).
|
||||
Scan(&totals).Error
|
||||
Scan(&recordingTotals).Error
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
return 0, 0, 0, 0, err
|
||||
}
|
||||
|
||||
var adjustmentTotals struct {
|
||||
TotalQty 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).
|
||||
Table("(?) AS adjustment_sources", adjustmentSubQuery).
|
||||
Select("COALESCE(SUM(adjustment_sources.total_qty), 0) AS total_qty, COALESCE(SUM(adjustment_sources.price), 0) AS total_weight").
|
||||
Table("adjustment_stocks AS ast").
|
||||
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
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
return 0, 0, 0, 0, err
|
||||
}
|
||||
|
||||
totals.TotalPieces += adjustmentTotals.TotalQty
|
||||
totals.TotalWeightKg += adjustmentTotals.TotalWeight
|
||||
|
||||
return totals.TotalPieces, totals.TotalWeightKg, nil
|
||||
return recordingTotals.TotalPieces, recordingTotals.TotalWeightKg, adjustmentTotals.TotalQty, adjustmentTotals.TotalWeight, nil
|
||||
}
|
||||
|
||||
func (r *HppV2RepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(
|
||||
@@ -1160,7 +1153,6 @@ CROSS JOIN lokasi_rec_totals lrt
|
||||
string(utils.AdjustmentTransactionTypeRecording),
|
||||
).
|
||||
Scan(&totals).Error
|
||||
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func FlockAgeDay(originDate time.Time, periodDate time.Time) int {
|
||||
if period.Before(origin) {
|
||||
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 {
|
||||
|
||||
@@ -18,8 +18,9 @@ type HppService interface {
|
||||
}
|
||||
|
||||
type HppCostResponse struct {
|
||||
Estimation HppCostDetail `json:"estimation"`
|
||||
Real HppCostDetail `json:"real"`
|
||||
Estimation HppCostDetail `json:"estimation"`
|
||||
Real HppCostDetail `json:"real"`
|
||||
DebugValues *HppCostDebugValues `json:"debug_values,omitempty"`
|
||||
}
|
||||
|
||||
type HppCostDetail struct {
|
||||
|
||||
@@ -44,6 +44,15 @@ type HppV2Component struct {
|
||||
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 {
|
||||
ProjectFlockKandangID uint `json:"project_flock_kandang_id"`
|
||||
ProjectFlockID uint `json:"project_flock_id"`
|
||||
|
||||
@@ -36,7 +36,7 @@ const (
|
||||
hppV2ProrationEggPiece = "laying_egg_piece_share"
|
||||
hppV2ScopePulletCost = "pullet_cost"
|
||||
hppV2ScopeProductionCost = "production_cost"
|
||||
hppV2CutoverFlagPakan = "PAKAN-CUTOVER"
|
||||
hppV2CutoverFlagPakan = string(utils.FlagPakan)
|
||||
hppV2CutoverFlagOvk = "OVK"
|
||||
)
|
||||
|
||||
@@ -1489,7 +1489,7 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64,
|
||||
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 {
|
||||
utils.Log.WithError(err).Errorf(
|
||||
"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
|
||||
}
|
||||
estimPieces := recordingQty + adjustmentQty
|
||||
estimWeightKg := recordingWeight + adjustmentWeight
|
||||
|
||||
realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate)
|
||||
if err != nil {
|
||||
@@ -1551,6 +1553,14 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64,
|
||||
return &HppCostResponse{
|
||||
Estimation: estimation,
|
||||
Real: real,
|
||||
DebugValues: &HppCostDebugValues{
|
||||
RecordingEggQty: recordingQty,
|
||||
RecordingEggWeight: recordingWeight,
|
||||
AdjustmentEggQty: adjustmentQty,
|
||||
AdjustmentEggWeight: adjustmentWeight,
|
||||
SoldEggQty: realPieces,
|
||||
SoldEggWeight: realWeightKg,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -132,6 +132,17 @@ func (s *hppV2RepoStub) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(
|
||||
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) {
|
||||
if len(projectFlockKandangIDs) != 1 {
|
||||
return 0, 0, nil
|
||||
|
||||
+9
@@ -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;
|
||||
+10
@@ -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');
|
||||
+21
@@ -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;
|
||||
+13
@@ -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;
|
||||
+21
@@ -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);
|
||||
+23
@@ -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);
|
||||
@@ -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"
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
type Warehouse struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
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"`
|
||||
LocationId *uint
|
||||
KandangId *uint
|
||||
|
||||
@@ -4,6 +4,10 @@ const (
|
||||
P_DashboardGetAll = "lti.dashboard.list"
|
||||
)
|
||||
|
||||
const (
|
||||
P_SystemSettingUpdate = "lti.system_settings.update"
|
||||
)
|
||||
|
||||
// project-flock
|
||||
const (
|
||||
P_ProjectFlockKandangsClosing = "lti.production.project_flock_kandangs.closing"
|
||||
@@ -203,6 +207,7 @@ const (
|
||||
P_Finances_Transaction_DeleteOne = "lti.finance.transactions.delete"
|
||||
)
|
||||
const (
|
||||
P_ChickinsGetAll = "lti.production.chickins.list"
|
||||
P_ChickinsCreateOne = "lti.production.chickins.create"
|
||||
P_ChickinsGetOne = "lti.production.chickins.detail"
|
||||
P_ChickinsApproval = "lti.production.chickins.approve"
|
||||
|
||||
@@ -10,7 +10,7 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
ProjectStatus *int `query:"project_status" validate:"omitempty,oneof=1 2"`
|
||||
LocationID *uint `query:"location_id" validate:"omitempty,gt=0"`
|
||||
@@ -24,7 +24,7 @@ const (
|
||||
type ClosingSapronakQuery struct {
|
||||
Type string `query:"type" validate:"required,oneof=incoming outgoing"`
|
||||
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"`
|
||||
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)
|
||||
withoutActivities := func(src map[string]int) map[string]int {
|
||||
if src == nil {
|
||||
return map[string]int{}
|
||||
}
|
||||
return src
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
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))
|
||||
for i, item := range result {
|
||||
responseData[i] = dto.DailyChecklistReportDTO{
|
||||
|
||||
@@ -72,13 +72,14 @@ type DailyChecklistPerformanceOverviewDTO struct {
|
||||
ActivityLeft int `json:"activity_left"`
|
||||
}
|
||||
|
||||
|
||||
type DailyChecklistReportDTO struct {
|
||||
Area DailyChecklistReportEntityDTO `json:"area"`
|
||||
Farm DailyChecklistReportEntityDTO `json:"farm"`
|
||||
Kandang DailyChecklistReportEntityDTO `json:"kandang"`
|
||||
ABK DailyChecklistReportEntityDTO `json:"abk"`
|
||||
Phase string `json:"phase"`
|
||||
DailyActivities map[string]int `json:"daily_activities"`
|
||||
DailyActivities map[string]any `json:"daily_activities"`
|
||||
Summary DailyChecklistReportSummaryDTO `json:"summary"`
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ type DailyChecklistReportItem struct {
|
||||
EmployeeID uint
|
||||
EmployeeName string
|
||||
PhaseName string
|
||||
DailyActivities map[string]int
|
||||
DailyActivities map[string]any
|
||||
Summary DailyChecklistReportSummary
|
||||
}
|
||||
|
||||
@@ -123,12 +123,13 @@ type DailyChecklistReportCategory struct {
|
||||
}
|
||||
|
||||
const (
|
||||
dailyChecklistDateLayout = "2006-01-02"
|
||||
dailyChecklistCategoryEmptyKandang = "empty_kandang"
|
||||
dailyChecklistStatusRejected = "REJECTED"
|
||||
dailyChecklistStatusDraft = "DRAFT"
|
||||
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"
|
||||
dailyChecklistDateLayout = "2006-01-02"
|
||||
dailyChecklistCategoryEmptyKandang = "empty_kandang"
|
||||
dailyChecklistStatusRejected = "REJECTED"
|
||||
dailyChecklistStatusDraft = "DRAFT"
|
||||
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"
|
||||
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 {
|
||||
@@ -276,7 +277,11 @@ func (s dailyChecklistService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
||||
normalizedSearch := re.ReplaceAllString(params.Search, "")
|
||||
if 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
|
||||
category := req.Category
|
||||
endDate := date
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -544,15 +536,17 @@ func (s *dailyChecklistService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
return err
|
||||
}
|
||||
|
||||
if req.EmptyKandang {
|
||||
if err := s.validateNoChecklistOverlapForEmptyKandang(tx, req.KandangId, date, endDate); err != nil {
|
||||
if category == dailyChecklistCategoryEmptyKandang {
|
||||
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 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)
|
||||
@@ -615,6 +609,22 @@ func (s *dailyChecklistService) validateNoEmptyKandangConflict(tx *gorm.DB, kand
|
||||
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 {
|
||||
existing := new(entity.DailyChecklist)
|
||||
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")
|
||||
}
|
||||
|
||||
var total int64
|
||||
// --- Count approved rows ---
|
||||
var approvedTotal int64
|
||||
groupedForCount := buildGroupedQuery()
|
||||
if err := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("(?) AS grouped", groupedForCount).
|
||||
Count(&total).Error; err != nil {
|
||||
Count(&approvedTotal).Error; err != nil {
|
||||
s.Log.Errorf("Failed to count report data: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -1473,19 +1484,246 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
|
||||
TotalAssignments int64
|
||||
}
|
||||
|
||||
rows := make([]reportRow, 0)
|
||||
if err := buildGroupedQuery().
|
||||
Order("a.name, loc.name, k.name, e.name").
|
||||
Offset(offset).
|
||||
Limit(params.Limit).
|
||||
Scan(&rows).Error; err != nil {
|
||||
s.Log.Errorf("Failed to fetch report data: %+v", err)
|
||||
type fallbackRowType struct {
|
||||
AreaID uint
|
||||
AreaName string
|
||||
LocationID uint
|
||||
LocationName string
|
||||
KandangID uint
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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 {
|
||||
EmployeeID uint
|
||||
@@ -1507,7 +1745,7 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
|
||||
kandangSet := 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}
|
||||
comboSet[key] = struct{}{}
|
||||
if _, ok := employeeSet[row.EmployeeID]; !ok {
|
||||
@@ -1648,8 +1886,9 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
|
||||
return selected
|
||||
}
|
||||
|
||||
items := make([]DailyChecklistReportItem, len(rows))
|
||||
for i, row := range rows {
|
||||
// --- Build approved items (existing logic) ---
|
||||
approvedItems := make([]DailyChecklistReportItem, len(pageApproved))
|
||||
for i, row := range pageApproved {
|
||||
key := comboKey{EmployeeID: row.EmployeeID, KandangID: row.KandangID, PhaseID: row.PhaseID}
|
||||
|
||||
activities := dailyActivityMap[key]
|
||||
@@ -1659,7 +1898,7 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
|
||||
|
||||
totalChecklist := 0
|
||||
categoryCounts := DailyChecklistReportCategory{}
|
||||
activityOutput := make(map[string]int, len(activities))
|
||||
activityOutput := make(map[string]any, len(activities))
|
||||
|
||||
for day, stat := range activities {
|
||||
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))
|
||||
}
|
||||
|
||||
items[i] = DailyChecklistReportItem{
|
||||
approvedItems[i] = DailyChecklistReportItem{
|
||||
AreaID: row.AreaID,
|
||||
AreaName: row.AreaName,
|
||||
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"`
|
||||
Category string `json:"category" validate:"required"`
|
||||
Status string `json:"status" validate:"required"`
|
||||
EmptyKandang bool `json:"empty_kandang"`
|
||||
EmptyKandangEndDate string `json:"empty_kandang_end_date"`
|
||||
EmptyKandang bool `json:"empty_kandang"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
|
||||
@@ -119,9 +119,9 @@ func (r *DashboardRepositoryImpl) GetRecordingWeeklyMetrics(ctx context.Context,
|
||||
var rows []RecordingWeeklyMetric
|
||||
|
||||
weekExpr := `CASE
|
||||
WHEN r.day IS NULL OR r.day <= 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17
|
||||
ELSE ((r.day - 1) / 7 + 1)
|
||||
WHEN r.day IS NULL OR r.day < 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
|
||||
ELSE (r.day / 7 + 1)
|
||||
END`
|
||||
|
||||
db := r.DB().WithContext(ctx).
|
||||
@@ -503,9 +503,9 @@ func (r *DashboardRepositoryImpl) GetComparisonWeeklyMetrics(ctx context.Context
|
||||
|
||||
var rows []ComparisonWeeklyMetric
|
||||
weekExpr := `CASE
|
||||
WHEN r.day IS NULL OR r.day <= 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17
|
||||
ELSE ((r.day - 1) / 7 + 1)
|
||||
WHEN r.day IS NULL OR r.day < 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
|
||||
ELSE (r.day / 7 + 1)
|
||||
END`
|
||||
|
||||
db := r.DB().WithContext(ctx).
|
||||
@@ -574,9 +574,9 @@ func (r *DashboardRepositoryImpl) GetEggQualityWeeklyMetrics(ctx context.Context
|
||||
var rows []EggQualityWeeklyMetric
|
||||
|
||||
weekExpr := `CASE
|
||||
WHEN r.day IS NULL OR r.day <= 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17
|
||||
ELSE ((r.day - 1) / 7 + 1)
|
||||
WHEN r.day IS NULL OR r.day < 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
|
||||
ELSE (r.day / 7 + 1)
|
||||
END`
|
||||
|
||||
db := r.DB().WithContext(ctx).
|
||||
@@ -616,9 +616,9 @@ func (r *DashboardRepositoryImpl) GetEggWeightWeeklyGrams(ctx context.Context, s
|
||||
var rows []WeeklyEggWeightMetric
|
||||
|
||||
weekExpr := `CASE
|
||||
WHEN r.day IS NULL OR r.day <= 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17
|
||||
ELSE ((r.day - 1) / 7 + 1)
|
||||
WHEN r.day IS NULL OR r.day < 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
|
||||
ELSE (r.day / 7 + 1)
|
||||
END`
|
||||
|
||||
db := r.DB().WithContext(ctx).
|
||||
@@ -647,9 +647,9 @@ func (r *DashboardRepositoryImpl) GetFeedUsageWeeklyByUom(ctx context.Context, s
|
||||
var rows []WeeklyFeedUsageMetric
|
||||
|
||||
weekExpr := `CASE
|
||||
WHEN r.day IS NULL OR r.day <= 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17
|
||||
ELSE ((r.day - 1) / 7 + 1)
|
||||
WHEN r.day IS NULL OR r.day < 0 THEN 1
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17
|
||||
ELSE (r.day / 7 + 1)
|
||||
END`
|
||||
|
||||
db := r.DB().WithContext(ctx).
|
||||
|
||||
@@ -86,6 +86,7 @@ type KandangGroupDTO struct {
|
||||
type DocumentDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// === MAPPERS ===
|
||||
@@ -184,6 +185,7 @@ func ToExpenseDetailDTO(e *entity.Expense) ExpenseDetailDTO {
|
||||
documents = append(documents, DocumentDTO{
|
||||
ID: uint64(doc.Id),
|
||||
Path: doc.Path,
|
||||
Name: doc.Name,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -191,6 +193,7 @@ func ToExpenseDetailDTO(e *entity.Expense) ExpenseDetailDTO {
|
||||
realizationDocs = append(realizationDocs, DocumentDTO{
|
||||
ID: uint64(doc.Id),
|
||||
Path: doc.Path,
|
||||
Name: doc.Name,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -342,6 +342,18 @@ func (s expenseService) GetOne(c *fiber.Ctx, id uint) (*expenseDto.ExpenseDetail
|
||||
expense.LatestApproval = approval
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -91,8 +91,10 @@ func (s transactionService) GetAll(c *fiber.Ctx, params *validation.Query) ([]en
|
||||
LOWER(COALESCE(notes, '')) LIKE ? OR
|
||||
LOWER(COALESCE(customers.name, '')) LIKE ? OR
|
||||
LOWER(COALESCE(suppliers.name, '')) LIKE ? OR
|
||||
LOWER(COALESCE(banks.name, '')) LIKE ?`,
|
||||
like, like, like, like, like, like, like, like,
|
||||
LOWER(COALESCE(banks.name, '')) LIKE ? OR
|
||||
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 {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Location *LocationRelationDTO `json:"location,omitempty"`
|
||||
}
|
||||
|
||||
@@ -113,6 +114,7 @@ func ToWarehouseRelationDTO(e *entity.Warehouse) *WarehouseRelationDTO {
|
||||
return &WarehouseRelationDTO{
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
Type: e.Type,
|
||||
Location: ToLocationRelationDTO(e.Location),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ type Create struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
WarehouseID uint `query:"warehouse_id" validate:"omitempty,min=0"`
|
||||
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 {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
ProductCategoryID: uint(c.QueryInt("product_category_id", 0)),
|
||||
}
|
||||
|
||||
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 != "" {
|
||||
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")
|
||||
})
|
||||
|
||||
|
||||
@@ -9,7 +9,8 @@ type Update struct {
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||
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 {
|
||||
product := productDTO.ToProductRelationDTO(e.Product)
|
||||
|
||||
// Create a copy with flock name appended if exists
|
||||
if e.ProjectFlockKandang != nil && e.ProjectFlockKandang.ProjectFlock.Id != 0 {
|
||||
// Append flock name only for KANDANG-type warehouses.
|
||||
// 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.Name = product.Name + " (" + e.ProjectFlockKandang.ProjectFlock.FlockName + ")"
|
||||
dto.Product = &productCopy
|
||||
|
||||
+31
-36
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -23,6 +22,7 @@ type ProductWarehouseRepository interface {
|
||||
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)
|
||||
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)
|
||||
ListProductIDsByFlagPrefixes(ctx context.Context, prefixes []string, visibleStatus *bool) ([]uint, error)
|
||||
ApplyFlagsFilter(db *gorm.DB, flags []string) *gorm.DB
|
||||
@@ -165,42 +165,16 @@ func (r *ProductWarehouseRepositoryImpl) ApplyFlagsFilter(db *gorm.DB, flags []s
|
||||
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.
|
||||
Where(
|
||||
`(`+actualFlagFilter+`) OR (
|
||||
NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM flags f_any
|
||||
WHERE f_any.flagable_id = p_flag.id
|
||||
AND f_any.flagable_type = ?
|
||||
)
|
||||
AND pc_flag.code IN ?
|
||||
)`,
|
||||
entity.FlagableTypeProduct,
|
||||
flags,
|
||||
entity.FlagableTypeProduct,
|
||||
fallbackCategoryCodes,
|
||||
).
|
||||
Where(`
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM flags f_flag
|
||||
WHERE f_flag.flagable_id = product_warehouses.product_id
|
||||
AND f_flag.flagable_type = ?
|
||||
AND f_flag.name IN ?
|
||||
)
|
||||
`, entity.FlagableTypeProduct, flags).
|
||||
Distinct()
|
||||
}
|
||||
|
||||
@@ -430,6 +404,27 @@ func (r *ProductWarehouseRepositoryImpl) GetByFlagAndWarehouseID(ctx context.Con
|
||||
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) {
|
||||
var product entity.Product
|
||||
err := r.DB().WithContext(ctx).
|
||||
|
||||
+8
-5
@@ -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)
|
||||
repo := NewProductWarehouseRepository(db)
|
||||
ctx := context.Background()
|
||||
@@ -131,12 +131,14 @@ func TestApplyFlagsFilterIncludesLegacyCategoryFallback(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if len(ids) != 2 || ids[0] != 1 || ids[1] != 2 {
|
||||
t.Fatalf("expected flagged and legacy RAW rows to match, got %v", ids)
|
||||
// Only PW 1 (product 10, flagged PAKAN) should match.
|
||||
// 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)
|
||||
repo := NewProductWarehouseRepository(db)
|
||||
ctx := context.Background()
|
||||
@@ -150,8 +152,9 @@ func TestApplyFlagsFilterDoesNotFallbackWhenProductAlreadyHasDifferentFlags(t *t
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
ProductId uint `query:"product_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 {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
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)
|
||||
|
||||
@@ -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 ?",
|
||||
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")
|
||||
})
|
||||
|
||||
|
||||
@@ -5,9 +5,11 @@ type Create struct {
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||
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 {
|
||||
|
||||
@@ -56,6 +56,12 @@ func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
|
||||
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{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
@@ -66,6 +72,8 @@ func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
|
||||
MarketingId: uint(c.QueryInt("marketing_id", 0)),
|
||||
ProjectFlockID: uint(c.QueryInt("project_flock_id", 0)),
|
||||
ProjectFlockKandangID: uint(c.QueryInt("project_flock_kandang_id", 0)),
|
||||
SortBy: sortBy,
|
||||
SortOrder: sortOrder,
|
||||
}
|
||||
|
||||
if isAllExcelExportRequest(c) {
|
||||
|
||||
@@ -292,7 +292,27 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
||||
if params.MarketingId != 0 {
|
||||
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 {
|
||||
return nil, 0, err
|
||||
|
||||
@@ -23,7 +23,7 @@ type DeliveryOrderUpdate struct {
|
||||
|
||||
type DeliveryOrderQuery struct {
|
||||
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"`
|
||||
ProductIDs []uint `query:"product_ids" validate:"omitempty,dive,gt=0"`
|
||||
Status string `query:"status" validate:"omitempty,max=50"`
|
||||
@@ -31,6 +31,8 @@ type DeliveryOrderQuery struct {
|
||||
MarketingId uint `query:"marketing_id" validate:"omitempty,gt=0"`
|
||||
ProjectFlockID uint `query:"project_flock_id" validate:"omitempty,gt=0"`
|
||||
ProjectFlockKandangID uint `query:"project_flock_kandang_id" validate:"omitempty,gt=0"`
|
||||
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 {
|
||||
|
||||
@@ -10,6 +10,6 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -16,6 +16,6 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
+1
-1
@@ -14,6 +14,6 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
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 {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
OrderBy: c.Query("order_by", "desc"),
|
||||
SortBy: c.Query("sort_by", "updated_at"),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
|
||||
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
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 {
|
||||
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").
|
||||
Group("employees.id, employees.name, employees.is_active, employees.created_at, employees.updated_at").
|
||||
Order("employees.created_at DESC").
|
||||
Order("employees.updated_at DESC")
|
||||
Group("employees.id, employees.name, employees.is_active, employees.created_at, employees.updated_at")
|
||||
|
||||
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 {
|
||||
|
||||
@@ -18,4 +18,6 @@ type Query struct {
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
KandangId *uint `query:"kandang_id" 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 {
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -10,6 +10,6 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ func (u *KandangGroupController) GetAll(c *fiber.Ctx) error {
|
||||
Search: c.Query("search", ""),
|
||||
LocationId: c.QueryInt("location_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 {
|
||||
|
||||
@@ -70,7 +70,14 @@ func (s kandangGroupService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
|
||||
if params.PicId != 0 {
|
||||
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 {
|
||||
|
||||
@@ -20,4 +20,6 @@ type Query struct {
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
LocationId int `query:"location_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", ""),
|
||||
LocationId: c.QueryInt("location_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 {
|
||||
|
||||
@@ -66,7 +66,14 @@ func (s kandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
|
||||
if params.PicId != 0 {
|
||||
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 {
|
||||
|
||||
@@ -26,4 +26,6 @@ type Query struct {
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
LocationId int `query:"location_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 {
|
||||
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"`
|
||||
SupplierID uint `query:"supplier_id" validate:"omitempty,gt=0"`
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
PhaseIDs string `query:"phase_ids" validate:"omitempty"`
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
Category *string `query:"category" validate:"omitempty"`
|
||||
}
|
||||
|
||||
+1
-1
@@ -12,6 +12,6 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
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)
|
||||
}
|
||||
}
|
||||
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")
|
||||
})
|
||||
|
||||
|
||||
@@ -46,4 +46,5 @@ type Query struct {
|
||||
ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"`
|
||||
IsDepletion *bool `query:"is_depletion" 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 {
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ type Update struct {
|
||||
|
||||
type Query struct {
|
||||
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"`
|
||||
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
||||
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"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 {
|
||||
// query := &validation.Query{
|
||||
// Page: c.QueryInt("page", 1),
|
||||
// Limit: c.QueryInt("limit", 10),
|
||||
// ProjectFlockKandangId: uint(c.QueryInt("project_flock_kandang_id", 0)),
|
||||
// }
|
||||
func (u *ChickinController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
ProjectFlockKandangId: uint(c.QueryInt("project_flock_kandang_id", 0)),
|
||||
}
|
||||
|
||||
// result, totalResults, err := u.ChickinService.GetAll(c, query)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
result, totalResults, err := u.ChickinService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// return c.Status(fiber.StatusOK).
|
||||
// JSON(response.SuccessWithPaginate[dto.ChickinListDTO]{
|
||||
// Code: fiber.StatusOK,
|
||||
// Status: "success",
|
||||
// Message: "Get all chickins successfully",
|
||||
// Meta: response.Meta{
|
||||
// Page: query.Page,
|
||||
// Limit: query.Limit,
|
||||
// TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
// TotalResults: totalResults,
|
||||
// },
|
||||
// Data: dto.ToChickinListDTOs(result),
|
||||
// })
|
||||
// }
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.ChickinListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all chickins successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.ToChickinListDTOs(result),
|
||||
})
|
||||
}
|
||||
|
||||
// func (u *ChickinController) GetOne(c *fiber.Ctx) error {
|
||||
// param := c.Params("id")
|
||||
|
||||
@@ -15,8 +15,8 @@ func ChickinRoutes(v1 fiber.Router, u user.UserService, s chickin.ChickinService
|
||||
route := v1.Group("/chickins")
|
||||
route.Use(m.Auth(u))
|
||||
|
||||
// route.Get("/", ctrl.GetAll)
|
||||
route.Post("/",m.RequirePermissions(m.P_ChickinsCreateOne), ctrl.CreateOne)
|
||||
route.Get("/", m.RequirePermissions(m.P_ChickinsGetAll), ctrl.GetAll)
|
||||
route.Post("/", m.RequirePermissions(m.P_ChickinsCreateOne), ctrl.CreateOne)
|
||||
route.Get("/:id",m.RequirePermissions(m.P_ChickinsGetOne), ctrl.GetOne)
|
||||
// route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
|
||||
+13
-7
@@ -302,16 +302,22 @@ func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, project
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var productCategoryCode string
|
||||
if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryGrowing) {
|
||||
productCategoryCode = "DOC"
|
||||
} else if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryLaying) {
|
||||
productCategoryCode = "PULLET"
|
||||
} else {
|
||||
if projectFlockKandang.ProjectFlock.Category != string(utils.ProjectFlockCategoryGrowing) &&
|
||||
projectFlockKandang.ProjectFlock.Category != string(utils.ProjectFlockCategoryLaying) {
|
||||
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 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -359,40 +359,6 @@ func applyCutOverLayingLookupOverride(result *dto.ProjectFlockKandangDTO) {
|
||||
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 {
|
||||
param := c.Params("id")
|
||||
req := new(validation.Resubmit)
|
||||
|
||||
@@ -120,6 +120,11 @@ func ToProjectFlockListDTOWithPeriod(e entity.ProjectFlock, period int) ProjectF
|
||||
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)
|
||||
if e.LatestApproval != nil {
|
||||
snapshot := approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||
|
||||
-14
@@ -17,7 +17,6 @@ type ProjectFlockKandangRepository interface {
|
||||
GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error)
|
||||
GetActiveByKandangID(ctx context.Context, kandangID uint) (*entity.ProjectFlockKandang, 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
|
||||
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)
|
||||
@@ -526,19 +525,6 @@ func (r *projectFlockKandangRepositoryImpl) UpdateClosedAt(ctx context.Context,
|
||||
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) {
|
||||
if kandangID == 0 {
|
||||
return false, nil
|
||||
|
||||
@@ -15,14 +15,13 @@ func ProjectflockRoutes(v1 fiber.Router, u user.UserService, s projectflock.Proj
|
||||
route := v1.Group("/project-flocks")
|
||||
route.Use(m.Auth(u))
|
||||
|
||||
route.Get("/", m.RequirePermissions(m.P_ProjectFlockGetAll), ctrl.GetAll)
|
||||
route.Post("/", m.RequirePermissions(m.P_ProjectFlockCreate), ctrl.CreateOne)
|
||||
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.Get("/kandangs/lookup", m.RequirePermissions(m.P_ProjectFlockLookup), ctrl.LookupProjectFlockKandang)
|
||||
route.Post("/approvals", m.RequirePermissions(m.P_ProjectFlockApprove), ctrl.Approval)
|
||||
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.Get("/",m.RequirePermissions(m.P_ProjectFlockGetAll),ctrl.GetAll)
|
||||
route.Post("/",m.RequirePermissions(m.P_ProjectFlockCreate), ctrl.CreateOne)
|
||||
route.Get("/:id",m.RequirePermissions(m.P_ProjectFlockGetOne), ctrl.GetOne)
|
||||
route.Delete("/:id",m.RequirePermissions(m.P_ProjectFlockGetAll), ctrl.DeleteOne)
|
||||
route.Get("/kandangs/lookup",m.RequirePermissions(m.P_ProjectFlockLookup), ctrl.LookupProjectFlockKandang)
|
||||
route.Post("/approvals",m.RequirePermissions(m.P_ProjectFlockApprove), ctrl.Approval)
|
||||
route.Get("/locations/:location_id/periods",m.RequirePermissions(m.P_ProjectFlockNextPeriod), ctrl.GetPeriodSummary)
|
||||
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)
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@@ -385,10 +409,6 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
|
||||
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 {
|
||||
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 {
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@@ -1274,52 +1350,6 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, 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 {
|
||||
|
||||
if len(budgets) == 0 {
|
||||
|
||||
@@ -6,6 +6,7 @@ type Create struct {
|
||||
Category string `json:"category" validate:"required_strict"`
|
||||
ProductionStandardId uint `json:"production_standard_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"`
|
||||
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required,min=1,dive"`
|
||||
}
|
||||
@@ -40,8 +41,5 @@ type ProjectBudget struct {
|
||||
type Resubmit struct {
|
||||
KandangIds []uint `json:"kandang_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
Period *int `json:"period" validate:"required,number,gt=0"`
|
||||
Periode *int `json:"periode" validate:"omitempty,gt=0"`
|
||||
}
|
||||
|
||||
@@ -77,6 +77,10 @@ func setRecordingExportColumns(file *excelize.File, sheet string) error {
|
||||
"Z": 22,
|
||||
"AA": 16,
|
||||
"AB": 18,
|
||||
"AC": 24,
|
||||
"AD": 18,
|
||||
"AE": 18,
|
||||
"AF": 18,
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
if err := file.MergeCell(sheet, col+"1", col+"2"); err != nil {
|
||||
return err
|
||||
@@ -104,19 +108,23 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
|
||||
}
|
||||
|
||||
headerValues := map[string]string{
|
||||
"A1": "No",
|
||||
"B1": "Lokasi",
|
||||
"C1": "Flock",
|
||||
"D1": "Kandang",
|
||||
"E1": "Periode",
|
||||
"F1": "Kategori",
|
||||
"G1": "Umur (hari)",
|
||||
"H1": "Waktu Recording",
|
||||
"I1": "Populasi Akhir",
|
||||
"Y1": "Status Approval",
|
||||
"Z1": "Catatan Approval",
|
||||
"AA1": "Dibuat Oleh",
|
||||
"AB1": "Tanggal Submit",
|
||||
"A1": "No",
|
||||
"B1": "Lokasi",
|
||||
"C1": "Flock",
|
||||
"D1": "Kandang",
|
||||
"E1": "Periode",
|
||||
"F1": "Kategori",
|
||||
"G1": "Umur (hari)",
|
||||
"H1": "Waktu Recording",
|
||||
"I1": "Populasi Akhir",
|
||||
"Y1": "Status Approval",
|
||||
"Z1": "Catatan Approval",
|
||||
"AA1": "Dibuat Oleh",
|
||||
"AB1": "Tanggal Submit",
|
||||
"AC1": "Nama Pakan",
|
||||
"AD1": "Jumlah Input Pakan",
|
||||
"AE1": "Jumlah Penggunaan",
|
||||
"AF1": "Pending Qty",
|
||||
}
|
||||
for cell, value := range headerValues {
|
||||
if err := file.SetCellValue(sheet, cell, value); err != nil {
|
||||
@@ -230,7 +238,7 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
|
||||
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 {
|
||||
@@ -241,6 +249,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
columns := []string{
|
||||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
|
||||
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB",
|
||||
"AC", "AD", "AE", "AF",
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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{}{
|
||||
i + 1,
|
||||
locationName,
|
||||
@@ -312,6 +344,10 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
safeExportText(pointerString(item.Approval.Notes)),
|
||||
createdBy,
|
||||
formatDateIndonesian(item.CreatedAt),
|
||||
feedNameCol, // AC
|
||||
inputCol, // AD - Jumlah Input Pakan
|
||||
usageCol, // AE - Jumlah Penggunaan
|
||||
pendingCol, // AF - Pending Qty
|
||||
}
|
||||
|
||||
for idx, col := range columns {
|
||||
@@ -339,7 +375,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -360,7 +396,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
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 {
|
||||
if err := file.SetCellStyle(sheet, col+"3", fmt.Sprintf("%s%d", col, lastRow), dataLeftStyle); err != nil {
|
||||
return err
|
||||
|
||||
@@ -92,13 +92,20 @@ type RecordingRelationDTO struct {
|
||||
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 {
|
||||
RecordingRelationDTO
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Kandang *RecordingKandangDTO `json:"kandang,omitempty"`
|
||||
Location *RecordingLocationDTO `json:"location,omitempty"`
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Kandang *RecordingKandangDTO `json:"kandang,omitempty"`
|
||||
Location *RecordingLocationDTO `json:"location,omitempty"`
|
||||
FeedUsage []RecordingFeedUsageDTO `json:"feed_usage,omitempty"`
|
||||
}
|
||||
|
||||
type RecordingDetailDTO struct {
|
||||
@@ -192,6 +199,36 @@ func ToRecordingStockDTOs(stocks []entity.RecordingStock) []RecordingStockDTO {
|
||||
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 {
|
||||
result := make([]RecordingEggDTO, len(eggs))
|
||||
for i, egg := range eggs {
|
||||
@@ -222,6 +259,7 @@ func toRecordingListDTO(e entity.Recording) RecordingListDTO {
|
||||
CreatedUser: createdUser,
|
||||
Kandang: recordingKandangDTO(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"
|
||||
sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services"
|
||||
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/fifo"
|
||||
|
||||
@@ -159,6 +160,8 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
validate,
|
||||
)
|
||||
|
||||
systemSettingRepo := rSystemSettings.NewSystemSettingRepository(db)
|
||||
|
||||
recordingService := sRecording.NewRecordingService(
|
||||
recordingRepo,
|
||||
projectFlockKandangRepo,
|
||||
@@ -174,6 +177,7 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
transferLayingRepo,
|
||||
transferLayingService,
|
||||
validate,
|
||||
systemSettingRepo,
|
||||
)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ type RecordingRepository interface {
|
||||
DeleteEggs(tx *gorm.DB, recordingID uint) error
|
||||
ListEggs(tx *gorm.DB, recordingID uint) ([]entity.RecordingEgg, 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)
|
||||
|
||||
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.Location").
|
||||
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 {
|
||||
@@ -543,6 +547,12 @@ func (r *RecordingRepositoryImpl) UpdateEggTotalQty(tx *gorm.DB, eggID uint, tot
|
||||
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(
|
||||
ctx context.Context,
|
||||
id uint,
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
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"
|
||||
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"
|
||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||
fifo "gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||
@@ -63,6 +64,7 @@ type recordingService struct {
|
||||
TransferLayingSvc sTransferLaying.TransferLayingService
|
||||
FifoStockV2Svc commonSvc.FifoStockV2Service
|
||||
StockLogRepo rStockLogs.StockLogRepository
|
||||
SystemSettingRepo rSystemSettings.SystemSettingRepository
|
||||
}
|
||||
|
||||
func NewRecordingService(
|
||||
@@ -80,6 +82,7 @@ func NewRecordingService(
|
||||
transferLayingRepo rTransferLaying.TransferLayingRepository,
|
||||
transferLayingSvc sTransferLaying.TransferLayingService,
|
||||
validate *validator.Validate,
|
||||
systemSettingRepo rSystemSettings.SystemSettingRepository,
|
||||
) RecordingService {
|
||||
return &recordingService{
|
||||
Log: utils.Log,
|
||||
@@ -97,6 +100,7 @@ func NewRecordingService(
|
||||
TransferLayingSvc: transferLayingSvc,
|
||||
FifoStockV2Svc: fifoStockV2Svc,
|
||||
StockLogRepo: stockLogRepo,
|
||||
SystemSettingRepo: systemSettingRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,6 +173,37 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
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)
|
||||
for i := range recordings {
|
||||
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)
|
||||
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 {
|
||||
return nil, 0, stateErr
|
||||
}
|
||||
@@ -390,6 +425,11 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -635,6 +675,10 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
if match {
|
||||
hasStockChanges = false
|
||||
} 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 {
|
||||
return err
|
||||
}
|
||||
@@ -755,6 +799,12 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
match := recordingutil.EggTotalsEqual(existingTotals, incomingTotals)
|
||||
if match {
|
||||
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 {
|
||||
category := ""
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
if err := ensureRecordingEggsUnused(existingEggs); err != nil {
|
||||
if err := ensureRecordingEggQtyChangeSafe(existingEggs, req.Eggs); err != nil {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if transfer == nil || transfer.Id == 0 {
|
||||
return false, nil
|
||||
@@ -2205,6 +2331,104 @@ func (s *recordingService) validateWarehouseIDs(
|
||||
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) {
|
||||
if projectFlockKandangID == 0 {
|
||||
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 diff + 1, nil
|
||||
return diff, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
week := 0
|
||||
if recording.Day != nil && *recording.Day > 0 {
|
||||
week = (*recording.Day-1)/7 + 1
|
||||
if recording.Day != nil && *recording.Day >= 0 {
|
||||
week = *recording.Day/7 + 1
|
||||
}
|
||||
if week > 0 && s.Repository != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
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))
|
||||
for _, item := range incoming {
|
||||
list := existingByWarehouse[item.ProductWarehouseId]
|
||||
@@ -2904,6 +3134,25 @@ func (s *recordingService) reflowSyncRecordingStocks(
|
||||
if len(list) > 0 {
|
||||
stock = list[0]
|
||||
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 {
|
||||
zero := 0.0
|
||||
stock = entity.RecordingStock{
|
||||
@@ -3561,6 +3810,44 @@ func ensureRecordingEggsUnused(eggs []entity.RecordingEgg) error {
|
||||
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 {
|
||||
if tx == nil || projectFlockKandangId == 0 || from.IsZero() {
|
||||
return nil
|
||||
|
||||
@@ -4,7 +4,8 @@ import "time"
|
||||
|
||||
type (
|
||||
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"`
|
||||
}
|
||||
|
||||
|
||||
+120
@@ -17,6 +17,8 @@ type TransferLayingRepository interface {
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
GetLatestApprovedBySourceKandang(ctx context.Context, sourceProjectFlockKandangID 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
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
param := c.Params("id")
|
||||
id, err := strconv.Atoi(param)
|
||||
|
||||
@@ -3,13 +3,11 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
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/xuri/excelize/v2"
|
||||
@@ -45,7 +43,6 @@ func buildPurchaseExportWorkbook(purchases []entity.Purchase) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
listItems := dto.ToPurchaseListDTOs(purchases)
|
||||
grandTotals := buildPurchaseGrandTotalMap(purchases)
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
if err := setPurchaseExportRows(file, purchaseExportSheetName, listItems, grandTotals); err != nil {
|
||||
if err := setPurchaseExportRows(file, purchaseExportSheetName, purchases, grandTotals); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := file.SetPanes(purchaseExportSheetName, &excelize.Panes{
|
||||
@@ -78,12 +75,14 @@ func setPurchaseExportColumns(file *excelize.File, sheet string) error {
|
||||
"A": 16,
|
||||
"B": 16,
|
||||
"C": 14,
|
||||
"D": 22,
|
||||
"D": 14,
|
||||
"E": 22,
|
||||
"F": 18,
|
||||
"G": 18,
|
||||
"H": 52,
|
||||
"I": 24,
|
||||
"F": 22,
|
||||
"G": 22,
|
||||
"H": 32,
|
||||
"I": 18,
|
||||
"J": 18,
|
||||
"K": 24,
|
||||
}
|
||||
|
||||
for col, width := range columnWidths {
|
||||
@@ -103,11 +102,13 @@ func setPurchaseExportHeaders(file *excelize.File, sheet string) error {
|
||||
"PR Number",
|
||||
"PO Number",
|
||||
"Tanggal PO",
|
||||
"Tanggal Terima",
|
||||
"Supplier",
|
||||
"Lokasi",
|
||||
"Gudang",
|
||||
"Product",
|
||||
"Status",
|
||||
"Grand Total",
|
||||
"Products",
|
||||
"Notes",
|
||||
}
|
||||
|
||||
@@ -136,46 +137,34 @@ func setPurchaseExportHeaders(file *excelize.File, sheet string) error {
|
||||
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 {
|
||||
if len(items) == 0 {
|
||||
func setPurchaseExportRows(file *excelize.File, sheet string, purchases []entity.Purchase, grandTotals map[uint]float64) error {
|
||||
if len(purchases) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i, item := range items {
|
||||
row := strconv.Itoa(i + 2)
|
||||
if err := file.SetCellValue(sheet, "A"+row, safePurchaseExportText(item.PrNumber)); err != nil {
|
||||
return err
|
||||
rowIdx := 2
|
||||
for p := range purchases {
|
||||
purchase := &purchases[p]
|
||||
total := grandTotals[purchase.Id]
|
||||
if len(purchase.Items) == 0 {
|
||||
if err := writePurchaseExportRow(file, sheet, rowIdx, purchase, nil, total); err != nil {
|
||||
return err
|
||||
}
|
||||
rowIdx++
|
||||
continue
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "B"+row, safePurchaseExportPointerText(item.PoNumber)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, "C"+row, formatPurchaseExportDate(item.PoDate)); err != nil {
|
||||
return err
|
||||
}
|
||||
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
|
||||
for it := range purchase.Items {
|
||||
if err := writePurchaseExportRow(file, sheet, rowIdx, purchase, &purchase.Items[it], total); err != nil {
|
||||
return err
|
||||
}
|
||||
rowIdx++
|
||||
}
|
||||
}
|
||||
|
||||
lastRow := len(items) + 1
|
||||
lastRow := rowIdx - 1
|
||||
dataStyle, err := file.NewStyle(&excelize.Style{
|
||||
Alignment: &excelize.Alignment{
|
||||
Horizontal: "left",
|
||||
@@ -192,7 +181,7 @@ func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.Purcha
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -212,7 +201,59 @@ func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.Purcha
|
||||
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 {
|
||||
@@ -227,31 +268,45 @@ func buildPurchaseGrandTotalMap(items []entity.Purchase) map[uint]float64 {
|
||||
return result
|
||||
}
|
||||
|
||||
func safePurchaseSupplierName(item dto.PurchaseListDTO) string {
|
||||
if item.Supplier == nil {
|
||||
func safePurchaseExportEntitySupplierName(purchase *entity.Purchase) string {
|
||||
if purchase.Supplier.Id == 0 {
|
||||
return "-"
|
||||
}
|
||||
return safePurchaseExportText(item.Supplier.Name)
|
||||
return safePurchaseExportText(purchase.Supplier.Name)
|
||||
}
|
||||
|
||||
func safePurchaseLocationName(item dto.PurchaseListDTO) string {
|
||||
if item.Location == nil {
|
||||
func safePurchaseWarehouseName(item *entity.PurchaseItem) string {
|
||||
if item.Warehouse == nil {
|
||||
return "-"
|
||||
}
|
||||
return safePurchaseExportText(item.Location.Name)
|
||||
return safePurchaseExportText(item.Warehouse.Name)
|
||||
}
|
||||
|
||||
func formatPurchaseExportStatus(item dto.PurchaseListDTO) string {
|
||||
if item.LatestApproval == nil {
|
||||
func safePurchaseItemLocationName(item *entity.PurchaseItem) string {
|
||||
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 "-"
|
||||
}
|
||||
|
||||
if item.LatestApproval.Action != nil &&
|
||||
strings.EqualFold(strings.TrimSpace(*item.LatestApproval.Action), string(entity.ApprovalActionRejected)) {
|
||||
if purchase.LatestApproval.Action != nil &&
|
||||
strings.EqualFold(strings.TrimSpace(string(*purchase.LatestApproval.Action)), string(entity.ApprovalActionRejected)) {
|
||||
return "Ditolak"
|
||||
}
|
||||
|
||||
return safePurchaseExportText(item.LatestApproval.StepName)
|
||||
return safePurchaseExportText(purchase.LatestApproval.StepName)
|
||||
}
|
||||
|
||||
func formatPurchaseExportDate(value *time.Time) string {
|
||||
@@ -268,33 +323,6 @@ func formatPurchaseExportDate(value *time.Time) string {
|
||||
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 {
|
||||
if value == nil {
|
||||
return "-"
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user