mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-21 22:05:44 +00:00
Compare commits
80 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 |
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 {
|
type options struct {
|
||||||
Apply bool
|
Apply bool
|
||||||
Output string
|
Output string
|
||||||
AreaName string
|
AreaName string
|
||||||
KandangLocationName string
|
KandangLocationName string
|
||||||
DBSSLMode string
|
DBSSLMode string
|
||||||
DeleteKandangWarehouses bool
|
DeleteKandangWarehouses bool
|
||||||
|
SkipBlockedRefsCheck bool
|
||||||
|
SkipIncompleteLocations bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type consolidateRow struct {
|
type consolidateRow struct {
|
||||||
@@ -132,7 +134,7 @@ func main() {
|
|||||||
log.Fatalf("failed to inspect warehouse references: %v", err)
|
log.Fatalf("failed to inspect warehouse references: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := runPrechecks(ctx, db, rows, refs); err != nil {
|
if err := runPrechecks(ctx, db, rows, refs, opts); err != nil {
|
||||||
log.Fatalf("precheck failed: %v", err)
|
log.Fatalf("precheck failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,6 +159,8 @@ func parseFlags() (*options, error) {
|
|||||||
flag.StringVar(&opts.KandangLocationName, "kandang-location-name", "", "Optional exact canonical kandang location filter")
|
flag.StringVar(&opts.KandangLocationName, "kandang-location-name", "", "Optional exact canonical kandang location filter")
|
||||||
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require")
|
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require")
|
||||||
flag.BoolVar(&opts.DeleteKandangWarehouses, "delete-kandang-warehouses", true, "Soft delete kandang warehouse rows after all stocks have been moved")
|
flag.BoolVar(&opts.DeleteKandangWarehouses, "delete-kandang-warehouses", true, "Soft delete kandang warehouse rows after all stocks have been moved")
|
||||||
|
flag.BoolVar(&opts.SkipBlockedRefsCheck, "skip-blocked-refs-check", false, "Skip blocked references check (use with caution - only if you understand the stock_transfers references)")
|
||||||
|
flag.BoolVar(&opts.SkipIncompleteLocations, "skip-incomplete-locations", false, "Skip locations that don't have farm-level warehouses and process the rest")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
|
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
|
||||||
@@ -186,6 +190,12 @@ func loadConsolidateRows(ctx context.Context, db *gorm.DB, opts *options) ([]con
|
|||||||
args = append(args, opts.KandangLocationName)
|
args = append(args, opts.KandangLocationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If skipping incomplete locations, filter out NULL farm warehouses
|
||||||
|
whereClause := ""
|
||||||
|
if opts.SkipIncompleteLocations {
|
||||||
|
whereClause = "AND fw.id IS NOT NULL"
|
||||||
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf(`
|
query := fmt.Sprintf(`
|
||||||
SELECT
|
SELECT
|
||||||
a.name AS area_name,
|
a.name AS area_name,
|
||||||
@@ -224,7 +234,10 @@ JOIN product_warehouses wp
|
|||||||
ON wp.warehouse_id = w.id
|
ON wp.warehouse_id = w.id
|
||||||
JOIN products p
|
JOIN products p
|
||||||
ON p.id = wp.product_id
|
ON p.id = wp.product_id
|
||||||
AND UPPER(COALESCE(p.type, '')) IN ('PAKAN', 'OVK')
|
JOIN flags f
|
||||||
|
ON f.flagable_id = p.id
|
||||||
|
AND f.flagable_type = 'products'
|
||||||
|
AND UPPER(f.name) IN ('PAKAN', 'OVK')
|
||||||
LEFT JOIN product_warehouses fpw
|
LEFT JOIN product_warehouses fpw
|
||||||
ON fpw.product_id = wp.product_id
|
ON fpw.product_id = wp.product_id
|
||||||
AND fpw.warehouse_id = fw.id
|
AND fpw.warehouse_id = fw.id
|
||||||
@@ -246,8 +259,11 @@ WHERE w.deleted_at IS NULL
|
|||||||
AND sa.deleted_at IS NULL
|
AND sa.deleted_at IS NULL
|
||||||
)
|
)
|
||||||
%s
|
%s
|
||||||
|
%s
|
||||||
ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC
|
ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC
|
||||||
`, andClause(filters))
|
`,
|
||||||
|
andClause(filters),
|
||||||
|
whereClause)
|
||||||
|
|
||||||
rows := make([]consolidateRow, 0)
|
rows := make([]consolidateRow, 0)
|
||||||
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
|
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
|
||||||
@@ -297,9 +313,12 @@ func buildReferencePlan(ctx context.Context, db *gorm.DB) (*referencePlan, error
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func runPrechecks(ctx context.Context, db *gorm.DB, rows []consolidateRow, refs *referencePlan) error {
|
func runPrechecks(ctx context.Context, db *gorm.DB, rows []consolidateRow, refs *referencePlan, opts *options) error {
|
||||||
if err := ensureNoBlockedWarehouseRefsConsolidate(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
|
// Only check blocked references if we're actually deleting the warehouses
|
||||||
return err
|
if opts.DeleteKandangWarehouses && !opts.SkipBlockedRefsCheck {
|
||||||
|
if err := ensureNoBlockedWarehouseRefsConsolidate(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := ensureNoPurchaseItemWarehouseConflictsConsolidate(ctx, db, rows); err != nil {
|
if err := ensureNoPurchaseItemWarehouseConflictsConsolidate(ctx, db, rows); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -710,6 +729,9 @@ func reflowAndRecalculateProductWarehouse(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err)
|
return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err)
|
||||||
}
|
}
|
||||||
|
if flagGroupCode == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
|
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
|
||||||
FlagGroupCode: flagGroupCode,
|
FlagGroupCode: flagGroupCode,
|
||||||
@@ -759,6 +781,9 @@ func resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, produc
|
|||||||
Order("rr.id ASC").
|
Order("rr.id ASC").
|
||||||
Limit(1).
|
Limit(1).
|
||||||
Take(&selected).Error
|
Take(&selected).Error
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,12 +26,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
Apply bool
|
Apply bool
|
||||||
Output string
|
Output string
|
||||||
AreaName string
|
AreaName string
|
||||||
KandangLocationName string
|
KandangLocationName string
|
||||||
DBSSLMode string
|
DBSSLMode string
|
||||||
DeleteWrongWarehouses bool
|
DeleteWrongWarehouses bool
|
||||||
|
AllowMovingAllocatedStocks bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type planRow struct {
|
type planRow struct {
|
||||||
@@ -123,6 +124,16 @@ func main() {
|
|||||||
log.Fatalf("failed to load plan rows: %v", err)
|
log.Fatalf("failed to load plan rows: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.AllowMovingAllocatedStocks {
|
||||||
|
allocatedRows, err := loadPlanRowsWithAllocations(ctx, db, opts)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to load allocated plan rows: %v", err)
|
||||||
|
}
|
||||||
|
rows = append(rows, allocatedRows...)
|
||||||
|
// Remove duplicates
|
||||||
|
rows = deduplicatePlanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
if len(rows) == 0 {
|
if len(rows) == 0 {
|
||||||
fmt.Println("No misplaced PAKAN/OVK stocks found in wrong-location warehouses")
|
fmt.Println("No misplaced PAKAN/OVK stocks found in wrong-location warehouses")
|
||||||
return
|
return
|
||||||
@@ -158,6 +169,7 @@ func parseFlags() (*options, error) {
|
|||||||
flag.StringVar(&opts.KandangLocationName, "kandang-location-name", "", "Optional exact canonical kandang location filter")
|
flag.StringVar(&opts.KandangLocationName, "kandang-location-name", "", "Optional exact canonical kandang location filter")
|
||||||
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require")
|
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require")
|
||||||
flag.BoolVar(&opts.DeleteWrongWarehouses, "delete-wrong-warehouses", true, "Soft delete wrong warehouse rows after all references have been moved")
|
flag.BoolVar(&opts.DeleteWrongWarehouses, "delete-wrong-warehouses", true, "Soft delete wrong warehouse rows after all references have been moved")
|
||||||
|
flag.BoolVar(&opts.AllowMovingAllocatedStocks, "allow-moving-allocated-stocks", false, "Allow moving stocks that have active allocations (use with caution - for old recordings with completed allocations)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
|
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
|
||||||
@@ -175,6 +187,90 @@ func parseFlags() (*options, error) {
|
|||||||
return &opts, nil
|
return &opts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deduplicatePlanRows(rows []planRow) []planRow {
|
||||||
|
seen := make(map[uint]struct{})
|
||||||
|
result := make([]planRow, 0, len(rows))
|
||||||
|
for _, row := range rows {
|
||||||
|
if _, ok := seen[row.SurvivorPWID]; !ok {
|
||||||
|
seen[row.SurvivorPWID] = struct{}{}
|
||||||
|
result = append(result, row)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadPlanRowsWithAllocations(ctx context.Context, db *gorm.DB, opts *options) ([]planRow, error) {
|
||||||
|
filters := make([]string, 0, 2)
|
||||||
|
args := make([]any, 0, 2)
|
||||||
|
if opts.AreaName != "" {
|
||||||
|
filters = append(filters, "a.name = ?")
|
||||||
|
args = append(args, opts.AreaName)
|
||||||
|
}
|
||||||
|
if opts.KandangLocationName != "" {
|
||||||
|
filters = append(filters, "kl.name = ?")
|
||||||
|
args = append(args, opts.KandangLocationName)
|
||||||
|
}
|
||||||
|
|
||||||
|
query := fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
a.name AS area_name,
|
||||||
|
kl.name AS kandang_location_name,
|
||||||
|
k.id AS kandang_id,
|
||||||
|
k.name AS kandang_name,
|
||||||
|
w.id AS wrong_warehouse_id,
|
||||||
|
w.name AS wrong_warehouse_name,
|
||||||
|
correct_w.id AS correct_warehouse_id,
|
||||||
|
correct_w.name AS correct_warehouse_name,
|
||||||
|
p.id AS product_id,
|
||||||
|
p.name AS product_name,
|
||||||
|
wp.project_flock_kandang_id,
|
||||||
|
wp.id AS survivor_pw_id,
|
||||||
|
COALESCE(wp.qty, 0) AS survivor_current_qty,
|
||||||
|
cpw.id AS absorbed_pw_id,
|
||||||
|
cpw.qty AS absorbed_current_qty
|
||||||
|
FROM warehouses w
|
||||||
|
JOIN kandangs k
|
||||||
|
ON k.id = w.kandang_id
|
||||||
|
AND k.deleted_at IS NULL
|
||||||
|
JOIN locations kl
|
||||||
|
ON kl.id = k.location_id
|
||||||
|
JOIN areas a
|
||||||
|
ON a.id = kl.area_id
|
||||||
|
JOIN LATERAL (
|
||||||
|
SELECT w2.id, w2.name
|
||||||
|
FROM warehouses w2
|
||||||
|
WHERE w2.location_id = k.location_id
|
||||||
|
AND UPPER(COALESCE(w2.type, '')) = 'LOKASI'
|
||||||
|
AND w2.deleted_at IS NULL
|
||||||
|
ORDER BY w2.id ASC
|
||||||
|
LIMIT 1
|
||||||
|
) AS correct_w ON TRUE
|
||||||
|
JOIN product_warehouses wp
|
||||||
|
ON wp.warehouse_id = w.id
|
||||||
|
JOIN products p
|
||||||
|
ON p.id = wp.product_id
|
||||||
|
JOIN flags f
|
||||||
|
ON f.flagable_id = p.id
|
||||||
|
AND f.flagable_type = 'products'
|
||||||
|
AND UPPER(f.name) IN ('PAKAN', 'OVK')
|
||||||
|
LEFT JOIN product_warehouses cpw
|
||||||
|
ON cpw.product_id = wp.product_id
|
||||||
|
AND cpw.warehouse_id = correct_w.id
|
||||||
|
AND cpw.project_flock_kandang_id IS NOT DISTINCT FROM wp.project_flock_kandang_id
|
||||||
|
WHERE w.deleted_at IS NULL
|
||||||
|
AND w.kandang_id IS NOT NULL
|
||||||
|
AND w.location_id IS DISTINCT FROM k.location_id
|
||||||
|
%s
|
||||||
|
ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC
|
||||||
|
`, andClause(filters))
|
||||||
|
|
||||||
|
rows := make([]planRow, 0)
|
||||||
|
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return rows, nil
|
||||||
|
}
|
||||||
|
|
||||||
func loadPlanRows(ctx context.Context, db *gorm.DB, opts *options) ([]planRow, error) {
|
func loadPlanRows(ctx context.Context, db *gorm.DB, opts *options) ([]planRow, error) {
|
||||||
filters := make([]string, 0, 2)
|
filters := make([]string, 0, 2)
|
||||||
args := make([]any, 0, 2)
|
args := make([]any, 0, 2)
|
||||||
@@ -225,7 +321,10 @@ JOIN product_warehouses wp
|
|||||||
ON wp.warehouse_id = w.id
|
ON wp.warehouse_id = w.id
|
||||||
JOIN products p
|
JOIN products p
|
||||||
ON p.id = wp.product_id
|
ON p.id = wp.product_id
|
||||||
AND UPPER(COALESCE(p.type, '')) IN ('PAKAN', 'OVK')
|
JOIN flags f
|
||||||
|
ON f.flagable_id = p.id
|
||||||
|
AND f.flagable_type = 'products'
|
||||||
|
AND UPPER(f.name) IN ('PAKAN', 'OVK')
|
||||||
LEFT JOIN product_warehouses cpw
|
LEFT JOIN product_warehouses cpw
|
||||||
ON cpw.product_id = wp.product_id
|
ON cpw.product_id = wp.product_id
|
||||||
AND cpw.warehouse_id = correct_w.id
|
AND cpw.warehouse_id = correct_w.id
|
||||||
@@ -301,8 +400,10 @@ func buildReferencePlan(ctx context.Context, db *gorm.DB) (*referencePlan, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runPrechecks(ctx context.Context, db *gorm.DB, rows []planRow, refs *referencePlan, opts *options) error {
|
func runPrechecks(ctx context.Context, db *gorm.DB, rows []planRow, refs *referencePlan, opts *options) error {
|
||||||
if err := ensureNoBlockedWarehouseRefs(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
|
if opts.DeleteWrongWarehouses {
|
||||||
return err
|
if err := ensureNoBlockedWarehouseRefs(ctx, db, rows, refs.BlockedWarehouseRefs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := ensureNoPurchaseItemWarehouseConflicts(ctx, db, rows); err != nil {
|
if err := ensureNoPurchaseItemWarehouseConflicts(ctx, db, rows); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -714,6 +815,9 @@ func reflowAndRecalculateProductWarehouse(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err)
|
return fmt.Errorf("resolve flag group for product_warehouse %d: %w", productWarehouseID, err)
|
||||||
}
|
}
|
||||||
|
if flagGroupCode == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
|
if _, err := fifoSvc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{
|
||||||
FlagGroupCode: flagGroupCode,
|
FlagGroupCode: flagGroupCode,
|
||||||
@@ -763,6 +867,9 @@ func resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, produc
|
|||||||
Order("rr.id ASC").
|
Order("rr.id ASC").
|
||||||
Limit(1).
|
Limit(1).
|
||||||
Take(&selected).Error
|
Take(&selected).Error
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,9 +19,9 @@ const (
|
|||||||
outputTable = "table"
|
outputTable = "table"
|
||||||
outputJSON = "json"
|
outputJSON = "json"
|
||||||
|
|
||||||
caseA = "A"
|
caseA = "A"
|
||||||
caseB = "B"
|
caseB = "B"
|
||||||
caseAll = "all"
|
caseAll = "ALL"
|
||||||
)
|
)
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
@@ -39,7 +39,7 @@ type sourceWarehouseCheck struct {
|
|||||||
KandangName string `json:"kandang_name"`
|
KandangName string `json:"kandang_name"`
|
||||||
SourceWarehouseID uint `json:"source_warehouse_id"`
|
SourceWarehouseID uint `json:"source_warehouse_id"`
|
||||||
SourceWarehouseName string `json:"source_warehouse_name"`
|
SourceWarehouseName string `json:"source_warehouse_name"`
|
||||||
Case string `json:"case"`
|
Case string `json:"case" gorm:"column:case_type"`
|
||||||
DeletedAt *string `json:"deleted_at"`
|
DeletedAt *string `json:"deleted_at"`
|
||||||
StockInProductWH float64 `json:"stock_in_product_wh"`
|
StockInProductWH float64 `json:"stock_in_product_wh"`
|
||||||
ActivePurchaseItems int64 `json:"active_purchase_items"`
|
ActivePurchaseItems int64 `json:"active_purchase_items"`
|
||||||
@@ -145,7 +145,7 @@ func parseFlags() (*options, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verifySourceWarehouses(ctx context.Context, db *gorm.DB, opts *options) ([]sourceWarehouseCheck, error) {
|
func verifySourceWarehouses(ctx context.Context, db *gorm.DB, opts *options) ([]sourceWarehouseCheck, error) {
|
||||||
filters := buildFilters(opts)
|
filters, args := buildFilters(opts)
|
||||||
query := fmt.Sprintf(`
|
query := fmt.Sprintf(`
|
||||||
WITH case_a_warehouses AS (
|
WITH case_a_warehouses AS (
|
||||||
-- Case A: Kandang-level warehouses (type != 'LOKASI' or NULL)
|
-- Case A: Kandang-level warehouses (type != 'LOKASI' or NULL)
|
||||||
@@ -182,9 +182,27 @@ case_b_warehouses AS (
|
|||||||
AND w.location_id IS DISTINCT FROM k.location_id
|
AND w.location_id IS DISTINCT FROM k.location_id
|
||||||
),
|
),
|
||||||
all_source_warehouses AS (
|
all_source_warehouses AS (
|
||||||
SELECT id, area_name, kandang_location_name, id AS kandang_id, name, case_type FROM case_a_warehouses
|
SELECT w_id, area_name, kandang_location_name, k_id AS kandang_id, name, case_type FROM (
|
||||||
|
SELECT w.id as w_id, a.name AS area_name, kl.name AS kandang_location_name, k.id as k_id, k.name, 'A'::text AS case_type
|
||||||
|
FROM warehouses w
|
||||||
|
JOIN kandangs k ON k.id = w.kandang_id AND k.deleted_at IS NULL
|
||||||
|
JOIN locations kl ON kl.id = k.location_id
|
||||||
|
JOIN areas a ON a.id = kl.area_id
|
||||||
|
WHERE w.deleted_at IS NOT NULL
|
||||||
|
AND w.kandang_id IS NOT NULL
|
||||||
|
AND UPPER(COALESCE(w.type, '')) <> 'LOKASI'
|
||||||
|
) case_a_warehouses
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT id, area_name, kandang_location_name, id AS kandang_id, name, case_type FROM case_b_warehouses
|
SELECT w_id, area_name, kandang_location_name, k_id AS kandang_id, name, case_type FROM (
|
||||||
|
SELECT w.id as w_id, a.name AS area_name, kl.name AS kandang_location_name, k.id as k_id, k.name, 'B'::text AS case_type
|
||||||
|
FROM warehouses w
|
||||||
|
JOIN kandangs k ON k.id = w.kandang_id AND k.deleted_at IS NULL
|
||||||
|
JOIN locations kl ON kl.id = k.location_id
|
||||||
|
JOIN areas a ON a.id = kl.area_id
|
||||||
|
WHERE w.deleted_at IS NOT NULL
|
||||||
|
AND w.kandang_id IS NOT NULL
|
||||||
|
AND w.location_id IS DISTINCT FROM k.location_id
|
||||||
|
) case_b_warehouses
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
asw.area_name,
|
asw.area_name,
|
||||||
@@ -198,7 +216,7 @@ SELECT
|
|||||||
COALESCE(SUM(pw.qty), 0) AS stock_in_product_wh,
|
COALESCE(SUM(pw.qty), 0) AS stock_in_product_wh,
|
||||||
COUNT(DISTINCT pi.id) AS active_purchase_items
|
COUNT(DISTINCT pi.id) AS active_purchase_items
|
||||||
FROM all_source_warehouses asw
|
FROM all_source_warehouses asw
|
||||||
JOIN warehouses w ON w.id = asw.id
|
JOIN warehouses w ON w.id = asw.w_id
|
||||||
LEFT JOIN product_warehouses pw ON pw.warehouse_id = w.id
|
LEFT JOIN product_warehouses pw ON pw.warehouse_id = w.id
|
||||||
LEFT JOIN purchase_items pi ON pi.warehouse_id = w.id
|
LEFT JOIN purchase_items pi ON pi.warehouse_id = w.id
|
||||||
WHERE true
|
WHERE true
|
||||||
@@ -216,7 +234,7 @@ ORDER BY asw.area_name ASC, asw.kandang_location_name ASC, w.name ASC
|
|||||||
`, andClause(filters))
|
`, andClause(filters))
|
||||||
|
|
||||||
rows := make([]sourceWarehouseCheck, 0)
|
rows := make([]sourceWarehouseCheck, 0)
|
||||||
if err := db.WithContext(ctx).Raw(query).Scan(&rows).Error; err != nil {
|
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,7 +257,7 @@ ORDER BY asw.area_name ASC, asw.kandang_location_name ASC, w.name ASC
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verifyDestinationWarehouses(ctx context.Context, db *gorm.DB, opts *options, sourceChecks []sourceWarehouseCheck) ([]destinationWarehouseCheck, error) {
|
func verifyDestinationWarehouses(ctx context.Context, db *gorm.DB, opts *options, sourceChecks []sourceWarehouseCheck) ([]destinationWarehouseCheck, error) {
|
||||||
filters := buildFilters(opts)
|
filters, args := buildFilters(opts)
|
||||||
query := fmt.Sprintf(`
|
query := fmt.Sprintf(`
|
||||||
SELECT
|
SELECT
|
||||||
a.name AS area_name,
|
a.name AS area_name,
|
||||||
@@ -252,12 +270,11 @@ SELECT
|
|||||||
COALESCE(SUM(sl.stock), 0) AS stock_logs_total,
|
COALESCE(SUM(sl.stock), 0) AS stock_logs_total,
|
||||||
COUNT(DISTINCT sl.id) AS stock_logs_count
|
COUNT(DISTINCT sl.id) AS stock_logs_count
|
||||||
FROM warehouses fw
|
FROM warehouses fw
|
||||||
JOIN locations loc ON loc.id = fw.location_id
|
JOIN locations kl ON kl.id = fw.location_id
|
||||||
JOIN areas a ON a.id = loc.area_id
|
JOIN areas a ON a.id = kl.area_id
|
||||||
JOIN kandangs k ON k.location_id = fw.location_id AND k.deleted_at IS NULL
|
JOIN product_warehouses pw ON pw.warehouse_id = fw.id
|
||||||
JOIN locations kl ON kl.id = k.location_id
|
JOIN products p ON p.id = pw.product_id
|
||||||
JOIN products p ON UPPER(COALESCE(p.type, '')) IN ('PAKAN', 'OVK')
|
JOIN flags f ON f.flagable_id = p.id AND f.flagable_type = 'products' AND UPPER(f.name) IN ('PAKAN', 'OVK')
|
||||||
LEFT JOIN product_warehouses pw ON pw.warehouse_id = fw.id AND pw.product_id = p.id
|
|
||||||
LEFT JOIN stock_logs sl ON sl.product_warehouse_id = pw.id
|
LEFT JOIN stock_logs sl ON sl.product_warehouse_id = pw.id
|
||||||
WHERE fw.deleted_at IS NULL
|
WHERE fw.deleted_at IS NULL
|
||||||
AND UPPER(COALESCE(fw.type, '')) = 'LOKASI'
|
AND UPPER(COALESCE(fw.type, '')) = 'LOKASI'
|
||||||
@@ -274,7 +291,7 @@ ORDER BY a.name ASC, kl.name ASC, fw.name ASC, p.name ASC
|
|||||||
`, andClause(filters))
|
`, andClause(filters))
|
||||||
|
|
||||||
rows := make([]destinationWarehouseCheck, 0)
|
rows := make([]destinationWarehouseCheck, 0)
|
||||||
if err := db.WithContext(ctx).Raw(query).Scan(&rows).Error; err != nil {
|
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,7 +333,6 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
|
|||||||
{"purchase_items", "warehouse_id"},
|
{"purchase_items", "warehouse_id"},
|
||||||
{"stock_transfers", "from_warehouse_id"},
|
{"stock_transfers", "from_warehouse_id"},
|
||||||
{"stock_transfers", "to_warehouse_id"},
|
{"stock_transfers", "to_warehouse_id"},
|
||||||
{"fifo_stock_v2_operation_log", "warehouse_id"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ref := range refChecks {
|
for _, ref := range refChecks {
|
||||||
@@ -328,17 +344,17 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
|
|||||||
}
|
}
|
||||||
|
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
// Get the specific warehouse IDs
|
// Get the specific warehouse IDs using raw SQL
|
||||||
var ids []uint
|
var ids []uint
|
||||||
if err := db.Table(ref.table).
|
query := fmt.Sprintf("SELECT DISTINCT %s FROM %s WHERE %s IN ?",
|
||||||
Where(fmt.Sprintf("%s IN ?", ref.column), warehouseIDs).
|
ref.column, ref.table, ref.column)
|
||||||
Pluck(ref.column, &ids).Error; err != nil {
|
if err := db.Raw(query, warehouseIDs).Scan(&ids).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
idStrs := make([]string, len(ids))
|
idStrs := make([]string, 0, len(ids))
|
||||||
for i, id := range ids {
|
for _, id := range ids {
|
||||||
idStrs[i] = fmt.Sprintf("%d", id)
|
idStrs = append(idStrs, fmt.Sprintf("%d", id))
|
||||||
}
|
}
|
||||||
|
|
||||||
results = append(results, orphanedReferenceCheck{
|
results = append(results, orphanedReferenceCheck{
|
||||||
@@ -353,15 +369,18 @@ func verifyOrphanedReferences(ctx context.Context, db *gorm.DB, sourceChecks []s
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildFilters(opts *options) []string {
|
func buildFilters(opts *options) ([]string, []any) {
|
||||||
filters := make([]string, 0, 2)
|
filters := make([]string, 0, 2)
|
||||||
|
args := make([]any, 0, 2)
|
||||||
if opts.AreaName != "" {
|
if opts.AreaName != "" {
|
||||||
filters = append(filters, fmt.Sprintf("a.name = '%s'", opts.AreaName))
|
filters = append(filters, "a.name = ?")
|
||||||
|
args = append(args, opts.AreaName)
|
||||||
}
|
}
|
||||||
if opts.KandangLocationName != "" {
|
if opts.KandangLocationName != "" {
|
||||||
filters = append(filters, fmt.Sprintf("kl.name = '%s'", opts.KandangLocationName))
|
filters = append(filters, "kl.name = ?")
|
||||||
|
args = append(args, opts.KandangLocationName)
|
||||||
}
|
}
|
||||||
return filters
|
return filters, args
|
||||||
}
|
}
|
||||||
|
|
||||||
func andClause(filters []string) string {
|
func andClause(filters []string) string {
|
||||||
|
|||||||
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/fsnotify/fsnotify v1.7.0 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||||
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
||||||
|
github.com/go-pdf/fpdf v0.9.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/google/go-cmp v0.6.0 // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
|||||||
@@ -77,6 +77,8 @@ github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9g
|
|||||||
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
|
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
|
||||||
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
|
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
|
||||||
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
|
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
|
||||||
|
github.com/go-pdf/fpdf v0.9.0 h1:PPvSaUuo1iMi9KkaAn90NuKi+P4gwMedWPHhj8YlJQw=
|
||||||
|
github.com/go-pdf/fpdf v0.9.0/go.mod h1:oO8N111TkmKb9D7VvWGLvLJlaZUQVPM+6V42pp3iV4Y=
|
||||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||||
|
|||||||
@@ -115,6 +115,7 @@ type HppV2CostRepository interface {
|
|||||||
GetFeedUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error)
|
GetFeedUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error)
|
||||||
GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error)
|
GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error)
|
||||||
GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error)
|
GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error)
|
||||||
|
GetEggProduksiBreakdownByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (recordingQty, recordingWeight, adjustmentQty, adjustmentWeight float64, err error)
|
||||||
GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, startDate *time.Time, endDate *time.Time) (float64, float64, error)
|
GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, startDate *time.Time, endDate *time.Time) (float64, float64, error)
|
||||||
GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, error)
|
GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, error)
|
||||||
}
|
}
|
||||||
@@ -858,58 +859,50 @@ func (r *HppV2RepositoryImpl) GetTotalPopulation(ctx context.Context, projectFlo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *HppV2RepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) {
|
func (r *HppV2RepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) {
|
||||||
|
rQty, rWeight, aQty, aWeight, err := r.GetEggProduksiBreakdownByProjectFlockKandangIds(ctx, projectFlockKandangIDs, date)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
return rQty + aQty, rWeight + aWeight, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppV2RepositoryImpl) GetEggProduksiBreakdownByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (recordingQty, recordingWeight, adjustmentQty, adjustmentWeight float64, err error) {
|
||||||
if date == nil {
|
if date == nil {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
date = &now
|
date = &now
|
||||||
}
|
}
|
||||||
|
|
||||||
var totals struct {
|
var recordingTotals struct {
|
||||||
TotalPieces float64
|
TotalPieces float64
|
||||||
TotalWeightKg float64
|
TotalWeightKg float64
|
||||||
}
|
}
|
||||||
err := r.db.WithContext(ctx).
|
err = r.db.WithContext(ctx).
|
||||||
Table("recordings AS r").
|
Table("recordings AS r").
|
||||||
Select("COALESCE(SUM(re.qty), 0) AS total_pieces, COALESCE(SUM(re.weight), 0)AS total_weight_kg").
|
Select("COALESCE(SUM(re.qty), 0) AS total_pieces, COALESCE(SUM(re.weight), 0) AS total_weight_kg").
|
||||||
Joins("JOIN recording_eggs AS re ON re.recording_id = r.id").
|
Joins("JOIN recording_eggs AS re ON re.recording_id = r.id").
|
||||||
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
||||||
Where("r.record_datetime <= ?", *date).
|
Where("r.record_datetime <= ?", *date).
|
||||||
Scan(&totals).Error
|
Scan(&recordingTotals).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, 0, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var adjustmentTotals struct {
|
var adjustmentTotals struct {
|
||||||
TotalQty float64
|
TotalQty float64
|
||||||
TotalWeight float64
|
TotalWeight float64
|
||||||
}
|
}
|
||||||
adjustmentSubQuery := r.db.WithContext(ctx).
|
|
||||||
Table("recordings AS r").
|
|
||||||
Select("DISTINCT ast.id AS adjustment_id, ast.total_qty AS total_qty, ast.price AS price").
|
|
||||||
Joins("JOIN recording_eggs AS re ON re.recording_id = r.id").
|
|
||||||
Joins("JOIN stock_transfer_details AS std ON std.dest_product_warehouse_id = re.product_warehouse_id").
|
|
||||||
Joins(
|
|
||||||
"JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = std.id AND sa.stockable_type = ? AND sa.status = ? AND sa.allocation_purpose = ?",
|
|
||||||
fifo.UsableKeyStockTransferOut.String(),
|
|
||||||
fifo.StockableKeyAdjustmentIn.String(),
|
|
||||||
entity.StockAllocationStatusActive,
|
|
||||||
entity.StockAllocationPurposeConsume,
|
|
||||||
).
|
|
||||||
Joins("JOIN adjustment_stocks AS ast ON ast.id = sa.stockable_id AND ast.product_warehouse_id = std.source_product_warehouse_id").
|
|
||||||
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
|
||||||
Where("r.record_datetime <= ?", *date)
|
|
||||||
|
|
||||||
err = r.db.WithContext(ctx).
|
err = r.db.WithContext(ctx).
|
||||||
Table("(?) AS adjustment_sources", adjustmentSubQuery).
|
Table("adjustment_stocks AS ast").
|
||||||
Select("COALESCE(SUM(adjustment_sources.total_qty), 0) AS total_qty, COALESCE(SUM(adjustment_sources.price), 0) AS total_weight").
|
Select("COALESCE(SUM(ast.total_qty), 0) AS total_qty, COALESCE(SUM(ast.price), 0) AS total_weight").
|
||||||
|
Joins("JOIN product_warehouses AS pw ON pw.id = ast.product_warehouse_id").
|
||||||
|
Where("pw.project_flock_kandang_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Where("ast.function_code = ?", string(utils.AdjustmentTransactionSubtypeRecordingEggIn)).
|
||||||
Scan(&adjustmentTotals).Error
|
Scan(&adjustmentTotals).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, 0, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
totals.TotalPieces += adjustmentTotals.TotalQty
|
return recordingTotals.TotalPieces, recordingTotals.TotalWeightKg, adjustmentTotals.TotalQty, adjustmentTotals.TotalWeight, nil
|
||||||
totals.TotalWeightKg += adjustmentTotals.TotalWeight
|
|
||||||
|
|
||||||
return totals.TotalPieces, totals.TotalWeightKg, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *HppV2RepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(
|
func (r *HppV2RepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(
|
||||||
@@ -1160,7 +1153,6 @@ CROSS JOIN lokasi_rec_totals lrt
|
|||||||
string(utils.AdjustmentTransactionTypeRecording),
|
string(utils.AdjustmentTransactionTypeRecording),
|
||||||
).
|
).
|
||||||
Scan(&totals).Error
|
Scan(&totals).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ type HppService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type HppCostResponse struct {
|
type HppCostResponse struct {
|
||||||
Estimation HppCostDetail `json:"estimation"`
|
Estimation HppCostDetail `json:"estimation"`
|
||||||
Real HppCostDetail `json:"real"`
|
Real HppCostDetail `json:"real"`
|
||||||
|
DebugValues *HppCostDebugValues `json:"debug_values,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HppCostDetail struct {
|
type HppCostDetail struct {
|
||||||
|
|||||||
@@ -44,6 +44,15 @@ type HppV2Component struct {
|
|||||||
Parts []HppV2ComponentPart `json:"parts"`
|
Parts []HppV2ComponentPart `json:"parts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HppCostDebugValues struct {
|
||||||
|
RecordingEggQty float64 `json:"recording_egg_qty"`
|
||||||
|
RecordingEggWeight float64 `json:"recording_egg_weight"`
|
||||||
|
AdjustmentEggQty float64 `json:"adjustment_egg_qty"`
|
||||||
|
AdjustmentEggWeight float64 `json:"adjustment_egg_weight"`
|
||||||
|
SoldEggQty float64 `json:"sold_egg_qty"`
|
||||||
|
SoldEggWeight float64 `json:"sold_egg_weight"`
|
||||||
|
}
|
||||||
|
|
||||||
type HppV2Breakdown struct {
|
type HppV2Breakdown struct {
|
||||||
ProjectFlockKandangID uint `json:"project_flock_kandang_id"`
|
ProjectFlockKandangID uint `json:"project_flock_kandang_id"`
|
||||||
ProjectFlockID uint `json:"project_flock_id"`
|
ProjectFlockID uint `json:"project_flock_id"`
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ const (
|
|||||||
hppV2ProrationEggPiece = "laying_egg_piece_share"
|
hppV2ProrationEggPiece = "laying_egg_piece_share"
|
||||||
hppV2ScopePulletCost = "pullet_cost"
|
hppV2ScopePulletCost = "pullet_cost"
|
||||||
hppV2ScopeProductionCost = "production_cost"
|
hppV2ScopeProductionCost = "production_cost"
|
||||||
hppV2CutoverFlagPakan = "PAKAN-CUTOVER"
|
hppV2CutoverFlagPakan = string(utils.FlagPakan)
|
||||||
hppV2CutoverFlagOvk = "OVK"
|
hppV2CutoverFlagOvk = "OVK"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1489,7 +1489,7 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64,
|
|||||||
return &HppCostResponse{}, nil
|
return &HppCostResponse{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
estimPieces, estimWeightKg, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate)
|
recordingQty, recordingWeight, adjustmentQty, adjustmentWeight, err := s.hppRepo.GetEggProduksiBreakdownByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Log.WithError(err).Errorf(
|
utils.Log.WithError(err).Errorf(
|
||||||
"GetHppEstimationDanRealisasi failed to get estimation egg production: project_flock_kandang_id=%d end_date=%s",
|
"GetHppEstimationDanRealisasi failed to get estimation egg production: project_flock_kandang_id=%d end_date=%s",
|
||||||
@@ -1498,6 +1498,8 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64,
|
|||||||
)
|
)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
estimPieces := recordingQty + adjustmentQty
|
||||||
|
estimWeightKg := recordingWeight + adjustmentWeight
|
||||||
|
|
||||||
realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate)
|
realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1551,6 +1553,14 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64,
|
|||||||
return &HppCostResponse{
|
return &HppCostResponse{
|
||||||
Estimation: estimation,
|
Estimation: estimation,
|
||||||
Real: real,
|
Real: real,
|
||||||
|
DebugValues: &HppCostDebugValues{
|
||||||
|
RecordingEggQty: recordingQty,
|
||||||
|
RecordingEggWeight: recordingWeight,
|
||||||
|
AdjustmentEggQty: adjustmentQty,
|
||||||
|
AdjustmentEggWeight: adjustmentWeight,
|
||||||
|
SoldEggQty: realPieces,
|
||||||
|
SoldEggWeight: realWeightKg,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -132,6 +132,17 @@ func (s *hppV2RepoStub) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(
|
|||||||
return totalPieces, totalKg, nil
|
return totalPieces, totalKg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *hppV2RepoStub) GetEggProduksiBreakdownByProjectFlockKandangIds(_ context.Context, projectFlockKandangIDs []uint, _ *time.Time) (float64, float64, float64, float64, error) {
|
||||||
|
totalPieces := 0.0
|
||||||
|
totalKg := 0.0
|
||||||
|
for _, projectFlockKandangID := range projectFlockKandangIDs {
|
||||||
|
row := s.eggProductionByPFK[projectFlockKandangID]
|
||||||
|
totalPieces += row.pieces
|
||||||
|
totalKg += row.kg
|
||||||
|
}
|
||||||
|
return totalPieces, totalKg, 0, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *hppV2RepoStub) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(_ context.Context, projectFlockKandangIDs []uint, _ *time.Time, _ *time.Time) (float64, float64, error) {
|
func (s *hppV2RepoStub) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(_ context.Context, projectFlockKandangIDs []uint, _ *time.Time, _ *time.Time) (float64, float64, error) {
|
||||||
if len(projectFlockKandangIDs) != 1 {
|
if len(projectFlockKandangIDs) != 1 {
|
||||||
return 0, 0, nil
|
return 0, 0, nil
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Revert fcr_value and cum_depletion_rate back to NUMERIC(7,3).
|
||||||
|
-- WARNING: any value with an integer part > 9999 (e.g. high-FCR early-laying recordings)
|
||||||
|
-- will fail the cast and must be cleared first, or this rollback will error.
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
ALTER COLUMN fcr_value TYPE NUMERIC(7,3) USING fcr_value::NUMERIC(7,3),
|
||||||
|
ALTER COLUMN cum_depletion_rate TYPE NUMERIC(7,3) USING cum_depletion_rate::NUMERIC(7,3);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- fcr_value and cum_depletion_rate were created as NUMERIC(7,3) (max integer part: 9999).
|
||||||
|
-- Early-laying flocks produce very few eggs relative to total feed consumed, so
|
||||||
|
-- FCR = usageInGrams / totalEggWeightGrams can legitimately exceed 9999 (e.g. ~31 740).
|
||||||
|
-- Widening to NUMERIC(15,3) keeps the same 3-decimal-place scale and is
|
||||||
|
-- fully backward-compatible: no existing value will be truncated or altered.
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
ALTER COLUMN fcr_value TYPE NUMERIC(15,3) USING fcr_value::NUMERIC(15,3),
|
||||||
|
ALTER COLUMN cum_depletion_rate TYPE NUMERIC(15,3) USING cum_depletion_rate::NUMERIC(15,3);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS system_settings;
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
CREATE TABLE system_settings (
|
||||||
|
key VARCHAR(100) PRIMARY KEY,
|
||||||
|
value TEXT NOT NULL DEFAULT '',
|
||||||
|
description TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO system_settings (key, value, description) VALUES
|
||||||
|
('allow_negative_pakan_ovk', 'false',
|
||||||
|
'Izinkan pencatatan penggunaan PAKAN & OVK negatif (mode migrasi): membuka semua produk PAKAN & OVK meskipun belum ada pembelian di sistem');
|
||||||
@@ -0,0 +1,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 {
|
type Warehouse struct {
|
||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
Name string `gorm:"type:varchar(50);not null"`
|
Name string `gorm:"type:varchar(50);not null"`
|
||||||
Type string `gorm:"not null"`
|
Type string `gorm:"column:type;not null"`
|
||||||
AreaId uint `gorm:"not null"`
|
AreaId uint `gorm:"not null"`
|
||||||
LocationId *uint
|
LocationId *uint
|
||||||
KandangId *uint
|
KandangId *uint
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ const (
|
|||||||
P_DashboardGetAll = "lti.dashboard.list"
|
P_DashboardGetAll = "lti.dashboard.list"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_SystemSettingUpdate = "lti.system_settings.update"
|
||||||
|
)
|
||||||
|
|
||||||
// project-flock
|
// project-flock
|
||||||
const (
|
const (
|
||||||
P_ProjectFlockKandangsClosing = "lti.production.project_flock_kandangs.closing"
|
P_ProjectFlockKandangsClosing = "lti.production.project_flock_kandangs.closing"
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
ProjectStatus *int `query:"project_status" validate:"omitempty,oneof=1 2"`
|
ProjectStatus *int `query:"project_status" validate:"omitempty,oneof=1 2"`
|
||||||
LocationID *uint `query:"location_id" validate:"omitempty,gt=0"`
|
LocationID *uint `query:"location_id" validate:"omitempty,gt=0"`
|
||||||
@@ -24,7 +24,7 @@ const (
|
|||||||
type ClosingSapronakQuery struct {
|
type ClosingSapronakQuery struct {
|
||||||
Type string `query:"type" validate:"required,oneof=incoming outgoing"`
|
Type string `query:"type" validate:"required,oneof=incoming outgoing"`
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||||
KandangID *uint `query:"kandang_id" validate:"omitempty,gt=0"`
|
KandangID *uint `query:"kandang_id" validate:"omitempty,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=100"`
|
Search string `query:"search" validate:"omitempty,max=100"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ type ProjectFlockRelationDTO struct {
|
|||||||
type WarehouseRelationDTO struct {
|
type WarehouseRelationDTO struct {
|
||||||
Id uint `json:"id"`
|
Id uint `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
Location *LocationRelationDTO `json:"location,omitempty"`
|
Location *LocationRelationDTO `json:"location,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,6 +114,7 @@ func ToWarehouseRelationDTO(e *entity.Warehouse) *WarehouseRelationDTO {
|
|||||||
return &WarehouseRelationDTO{
|
return &WarehouseRelationDTO{
|
||||||
Id: e.Id,
|
Id: e.Id,
|
||||||
Name: e.Name,
|
Name: e.Name,
|
||||||
|
Type: e.Type,
|
||||||
Location: ToLocationRelationDTO(e.Location),
|
Location: ToLocationRelationDTO(e.Location),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ type Create struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,min=1"`
|
Page int `query:"page" validate:"omitempty,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,min=1"`
|
||||||
ProductID uint `query:"product_id" validate:"omitempty,min=0"`
|
ProductID uint `query:"product_id" validate:"omitempty,min=0"`
|
||||||
WarehouseID uint `query:"warehouse_id" validate:"omitempty,min=0"`
|
WarehouseID uint `query:"warehouse_id" validate:"omitempty,min=0"`
|
||||||
TransactionType string `query:"transaction_type" validate:"omitempty,max=100"`
|
TransactionType string `query:"transaction_type" validate:"omitempty,max=100"`
|
||||||
|
|||||||
@@ -25,9 +25,10 @@ func NewProductStockController(productStockService service.ProductStockService)
|
|||||||
|
|
||||||
func (u *ProductStockController) GetAll(c *fiber.Ctx) error {
|
func (u *ProductStockController) GetAll(c *fiber.Ctx) error {
|
||||||
query := &validation.Query{
|
query := &validation.Query{
|
||||||
Page: c.QueryInt("page", 1),
|
Page: c.QueryInt("page", 1),
|
||||||
Limit: c.QueryInt("limit", 10),
|
Limit: c.QueryInt("limit", 10),
|
||||||
Search: c.Query("search", ""),
|
Search: c.Query("search", ""),
|
||||||
|
ProductCategoryID: uint(c.QueryInt("product_category_id", 0)),
|
||||||
}
|
}
|
||||||
|
|
||||||
if query.Page < 1 || query.Limit < 1 {
|
if query.Page < 1 || query.Limit < 1 {
|
||||||
|
|||||||
@@ -129,6 +129,9 @@ func (s productStockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
|
|||||||
if params.Search != "" {
|
if params.Search != "" {
|
||||||
db = db.Where("products.name ILIKE ?", "%"+params.Search+"%")
|
db = db.Where("products.name ILIKE ?", "%"+params.Search+"%")
|
||||||
}
|
}
|
||||||
|
if params.ProductCategoryID > 0 {
|
||||||
|
db = db.Where("products.product_category_id = ?", params.ProductCategoryID)
|
||||||
|
}
|
||||||
return db.Order("products.created_at DESC").Order("products.updated_at DESC")
|
return db.Order("products.created_at DESC").Order("products.updated_at DESC")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ type Update struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
|
ProductCategoryID uint `query:"product_category_id" validate:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,8 +79,12 @@ func ToProductWarehouseListDTO(e entity.ProductWarehouse) ProductWarehouseListDT
|
|||||||
if e.Product.Id != 0 {
|
if e.Product.Id != 0 {
|
||||||
product := productDTO.ToProductRelationDTO(e.Product)
|
product := productDTO.ToProductRelationDTO(e.Product)
|
||||||
|
|
||||||
// Create a copy with flock name appended if exists
|
// Append flock name only for KANDANG-type warehouses.
|
||||||
if e.ProjectFlockKandang != nil && e.ProjectFlockKandang.ProjectFlock.Id != 0 {
|
// Farm-level (LOKASI) warehouses are shared across flocks — attaching a flock
|
||||||
|
// label there creates duplicates and is misleading.
|
||||||
|
if e.ProjectFlockKandang != nil &&
|
||||||
|
e.ProjectFlockKandang.ProjectFlock.Id != 0 &&
|
||||||
|
e.Warehouse.Type == "KANDANG" {
|
||||||
productCopy := product
|
productCopy := product
|
||||||
productCopy.Name = product.Name + " (" + e.ProjectFlockKandang.ProjectFlock.FlockName + ")"
|
productCopy.Name = product.Name + " (" + e.ProjectFlockKandang.ProjectFlock.FlockName + ")"
|
||||||
dto.Product = &productCopy
|
dto.Product = &productCopy
|
||||||
|
|||||||
+22
@@ -23,6 +23,7 @@ type ProductWarehouseRepository interface {
|
|||||||
GetByCategoryCodeAndWarehouseID(ctx context.Context, categoryCode string, warehouseId uint) ([]entity.ProductWarehouse, error)
|
GetByCategoryCodeAndWarehouseID(ctx context.Context, categoryCode string, warehouseId uint) ([]entity.ProductWarehouse, error)
|
||||||
GetLatestByCategoryCodeAndWarehouseID(ctx context.Context, categoryCode string, warehouseId uint, db *gorm.DB) (*entity.ProductWarehouse, error)
|
GetLatestByCategoryCodeAndWarehouseID(ctx context.Context, categoryCode string, warehouseId uint, db *gorm.DB) (*entity.ProductWarehouse, error)
|
||||||
GetByFlagAndWarehouseID(ctx context.Context, flagName string, warehouseId uint) ([]entity.ProductWarehouse, error)
|
GetByFlagAndWarehouseID(ctx context.Context, flagName string, warehouseId uint) ([]entity.ProductWarehouse, error)
|
||||||
|
GetByFlagsAndWarehouseID(ctx context.Context, flagNames []string, excludeFlagNames []string, warehouseId uint) ([]entity.ProductWarehouse, error)
|
||||||
GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error)
|
GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error)
|
||||||
ListProductIDsByFlagPrefixes(ctx context.Context, prefixes []string, visibleStatus *bool) ([]uint, error)
|
ListProductIDsByFlagPrefixes(ctx context.Context, prefixes []string, visibleStatus *bool) ([]uint, error)
|
||||||
ApplyFlagsFilter(db *gorm.DB, flags []string) *gorm.DB
|
ApplyFlagsFilter(db *gorm.DB, flags []string) *gorm.DB
|
||||||
@@ -430,6 +431,27 @@ func (r *ProductWarehouseRepositoryImpl) GetByFlagAndWarehouseID(ctx context.Con
|
|||||||
return productWarehouses, nil
|
return productWarehouses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *ProductWarehouseRepositoryImpl) GetByFlagsAndWarehouseID(ctx context.Context, flagNames []string, excludeFlagNames []string, warehouseId uint) ([]entity.ProductWarehouse, error) {
|
||||||
|
var productWarehouses []entity.ProductWarehouse
|
||||||
|
q := r.DB().WithContext(ctx).Model(&entity.ProductWarehouse{}).
|
||||||
|
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||||
|
Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = 'products'").
|
||||||
|
Where("flags.name IN ? AND product_warehouses.warehouse_id = ?", flagNames, warehouseId)
|
||||||
|
if len(excludeFlagNames) > 0 {
|
||||||
|
q = q.Where("NOT EXISTS (SELECT 1 FROM flags ef WHERE ef.flagable_id = products.id AND ef.flagable_type = 'products' AND ef.name IN ?)", excludeFlagNames)
|
||||||
|
}
|
||||||
|
err := q.Order("product_warehouses.id DESC").
|
||||||
|
Preload("Product").
|
||||||
|
Preload("Product.ProductCategory").
|
||||||
|
Preload("Product.Uom").
|
||||||
|
Preload("Warehouse").
|
||||||
|
Find(&productWarehouses).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return productWarehouses, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *ProductWarehouseRepositoryImpl) GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error) {
|
func (r *ProductWarehouseRepositoryImpl) GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error) {
|
||||||
var product entity.Product
|
var product entity.Product
|
||||||
err := r.DB().WithContext(ctx).
|
err := r.DB().WithContext(ctx).
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty"`
|
Search string `query:"search" validate:"omitempty"`
|
||||||
ProductId uint `query:"product_id" validate:"omitempty,number,min=1"`
|
ProductId uint `query:"product_id" validate:"omitempty,number,min=1"`
|
||||||
WarehouseId uint `query:"warehouse_id" validate:"omitempty,number,min=1"`
|
WarehouseId uint `query:"warehouse_id" validate:"omitempty,number,min=1"`
|
||||||
|
|||||||
@@ -25,9 +25,11 @@ func NewTransferController(transferService service.TransferService) *TransferCon
|
|||||||
|
|
||||||
func (u *TransferController) GetAll(c *fiber.Ctx) error {
|
func (u *TransferController) GetAll(c *fiber.Ctx) error {
|
||||||
query := &validation.Query{
|
query := &validation.Query{
|
||||||
Page: c.QueryInt("page", 1),
|
Page: c.QueryInt("page", 1),
|
||||||
Limit: c.QueryInt("limit", 10),
|
Limit: c.QueryInt("limit", 10),
|
||||||
Search: c.Query("search", ""),
|
Search: c.Query("search", ""),
|
||||||
|
ProductID: uint(c.QueryInt("product_id", 0)),
|
||||||
|
WarehouseID: uint(c.QueryInt("warehouse_id", 0)),
|
||||||
}
|
}
|
||||||
|
|
||||||
result, totalResults, err := u.TransferService.GetAll(c, query)
|
result, totalResults, err := u.TransferService.GetAll(c, query)
|
||||||
|
|||||||
@@ -157,6 +157,12 @@ func (s transferService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
|
|||||||
Where("movement_number ILIKE ? OR from_warehouses.name ILIKE ? OR to_warehouses.name ILIKE ?",
|
Where("movement_number ILIKE ? OR from_warehouses.name ILIKE ? OR to_warehouses.name ILIKE ?",
|
||||||
searchTerm, searchTerm, searchTerm)
|
searchTerm, searchTerm, searchTerm)
|
||||||
}
|
}
|
||||||
|
if params.ProductID > 0 {
|
||||||
|
db = db.Joins("JOIN stock_transfer_details AS filter_std ON filter_std.stock_transfer_id = stock_transfers.id AND filter_std.deleted_at IS NULL AND filter_std.product_id = ?", params.ProductID)
|
||||||
|
}
|
||||||
|
if params.WarehouseID > 0 {
|
||||||
|
db = db.Where("stock_transfers.from_warehouse_id = ? OR stock_transfers.to_warehouse_id = ?", params.WarehouseID, params.WarehouseID)
|
||||||
|
}
|
||||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,11 @@ type Create struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
|
ProductID uint `query:"product_id" validate:"omitempty"`
|
||||||
|
WarehouseID uint `query:"warehouse_id" validate:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransferProduct struct {
|
type TransferProduct struct {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ type DeliveryOrderUpdate struct {
|
|||||||
|
|
||||||
type DeliveryOrderQuery struct {
|
type DeliveryOrderQuery struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=100"`
|
Search string `query:"search" validate:"omitempty,max=100"`
|
||||||
ProductIDs []uint `query:"product_ids" validate:"omitempty,dive,gt=0"`
|
ProductIDs []uint `query:"product_ids" validate:"omitempty,dive,gt=0"`
|
||||||
Status string `query:"status" validate:"omitempty,max=50"`
|
Status string `query:"status" validate:"omitempty,max=50"`
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -14,6 +14,6 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
HasMarketing *bool `query:"has_marketing" validate:"omitempty"`
|
HasMarketing *bool `query:"has_marketing" validate:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,6 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
SupplierID uint `query:"supplier_id" validate:"omitempty,gt=0"`
|
SupplierID uint `query:"supplier_id" validate:"omitempty,gt=0"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
PhaseIDs string `query:"phase_ids" validate:"omitempty"`
|
PhaseIDs string `query:"phase_ids" validate:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
Category *string `query:"category" validate:"omitempty"`
|
Category *string `query:"category" validate:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -12,6 +12,6 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -36,7 +36,7 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
ProjectCategory string `query:"project_category" validate:"omitempty,oneof=GROWING LAYING"`
|
ProjectCategory string `query:"project_category" validate:"omitempty,oneof=GROWING LAYING"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -264,6 +264,18 @@ func (s productService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
|
|||||||
db = db.Where("NOT "+existsQuery, entity.FlagableTypeProduct, depletionProductFlags)
|
db = db.Where("NOT "+existsQuery, entity.FlagableTypeProduct, depletionProductFlags)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if strings.TrimSpace(params.Flags) != "" {
|
||||||
|
cleanFlags := utils.ParseFlags(params.Flags)
|
||||||
|
if len(cleanFlags) > 0 {
|
||||||
|
db = db.Where(`
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1 FROM flags f
|
||||||
|
WHERE f.flagable_type = ?
|
||||||
|
AND f.flagable_id = products.id
|
||||||
|
AND UPPER(f.name) IN ?
|
||||||
|
)`, entity.FlagableTypeProduct, cleanFlags)
|
||||||
|
}
|
||||||
|
}
|
||||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -46,4 +46,5 @@ type Query struct {
|
|||||||
ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"`
|
ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"`
|
||||||
IsDepletion *bool `query:"is_depletion" validate:"omitempty"`
|
IsDepletion *bool `query:"is_depletion" validate:"omitempty"`
|
||||||
IncludeAll *bool `query:"include_all" validate:"omitempty"`
|
IncludeAll *bool `query:"include_all" validate:"omitempty"`
|
||||||
|
Flags string `query:"flags" validate:"omitempty,max=200"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type Update struct {
|
|||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
||||||
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
|
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
|
||||||
|
|||||||
+13
-7
@@ -302,16 +302,22 @@ func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, project
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var productCategoryCode string
|
if projectFlockKandang.ProjectFlock.Category != string(utils.ProjectFlockCategoryGrowing) &&
|
||||||
if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryGrowing) {
|
projectFlockKandang.ProjectFlock.Category != string(utils.ProjectFlockCategoryLaying) {
|
||||||
productCategoryCode = "DOC"
|
|
||||||
} else if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryLaying) {
|
|
||||||
productCategoryCode = "PULLET"
|
|
||||||
} else {
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
products, err := s.ProductWarehouseRepo.GetByFlagAndWarehouseID(c.Context(), productCategoryCode, warehouse.Id)
|
ayamFlags := []string{
|
||||||
|
string(utils.FlagAyam),
|
||||||
|
string(utils.FlagDOC),
|
||||||
|
string(utils.FlagPullet),
|
||||||
|
}
|
||||||
|
ayamSubFlags := []string{
|
||||||
|
string(utils.FlagAyamAfkir),
|
||||||
|
string(utils.FlagAyamCulling),
|
||||||
|
string(utils.FlagAyamMati),
|
||||||
|
}
|
||||||
|
products, err := s.ProductWarehouseRepo.GetByFlagsAndWarehouseID(c.Context(), ayamFlags, ayamSubFlags, warehouse.Id)
|
||||||
if err != nil || len(products) == 0 {
|
if err != nil || len(products) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,6 +120,11 @@ func ToProjectFlockListDTOWithPeriod(e entity.ProjectFlock, period int) ProjectF
|
|||||||
locationSummary = &mapped
|
locationSummary = &mapped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Jika period tidak di-pass secara eksplisit (0), derive dari KandangHistory
|
||||||
|
if period == 0 && len(e.KandangHistory) > 0 {
|
||||||
|
period = e.KandangHistory[0].Period
|
||||||
|
}
|
||||||
|
|
||||||
latestApproval := defaultProjectFlockLatestApproval(e)
|
latestApproval := defaultProjectFlockLatestApproval(e)
|
||||||
if e.LatestApproval != nil {
|
if e.LatestApproval != nil {
|
||||||
snapshot := approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
snapshot := approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||||
|
|||||||
@@ -374,7 +374,32 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
|
|||||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||||
projectRepo := repository.NewProjectflockRepository(dbTransaction)
|
projectRepo := repository.NewProjectflockRepository(dbTransaction)
|
||||||
|
|
||||||
generatedName, _, err := s.generateSequentialFlockName(c.Context(), projectRepo, canonicalBase, 1, nil)
|
var periods map[uint]int
|
||||||
|
if req.Periode != nil {
|
||||||
|
// Pakai periode yang diminta untuk semua kandang
|
||||||
|
periods = make(map[uint]int, len(kandangIDs))
|
||||||
|
for _, kandangID := range kandangIDs {
|
||||||
|
periods[kandangID] = *req.Periode
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
periods, err = projectRepo.GetNextPeriodsForKandangs(c.Context(), kandangIDs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
startPeriod := 1
|
||||||
|
if req.Periode != nil {
|
||||||
|
startPeriod = *req.Periode
|
||||||
|
} else {
|
||||||
|
for _, p := range periods {
|
||||||
|
if p > startPeriod {
|
||||||
|
startPeriod = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
generatedName, _, err := s.generateSequentialFlockName(c.Context(), projectRepo, canonicalBase, startPeriod, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -384,10 +409,6 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
periods, err := projectRepo.GetNextPeriodsForKandangs(c.Context(), kandangIDs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := s.attachKandangs(c.Context(), dbTransaction, createBody.Id, kandangIDs, periods); err != nil {
|
if err := s.attachKandangs(c.Context(), dbTransaction, createBody.Id, kandangIDs, periods); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1225,12 +1246,34 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, id
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hitung newFlockName sebelum membuka transaksi (fast-path conflict check)
|
||||||
|
var newFlockName string
|
||||||
|
if req.Periode != nil {
|
||||||
|
lastSpace := strings.LastIndex(existing.FlockName, " ")
|
||||||
|
if lastSpace < 0 {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Format flock name tidak valid")
|
||||||
|
}
|
||||||
|
baseName := strings.TrimSpace(existing.FlockName[:lastSpace])
|
||||||
|
newFlockName = fmt.Sprintf("%s %03d", baseName, *req.Periode)
|
||||||
|
|
||||||
|
taken, err := s.Repository.ExistsByFlockName(c.Context(), newFlockName, &id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa nama flock")
|
||||||
|
}
|
||||||
|
if taken {
|
||||||
|
return nil, fiber.NewError(fiber.StatusConflict,
|
||||||
|
fmt.Sprintf("Nama flock '%s' sudah digunakan oleh project flock lain", newFlockName))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||||
|
|
||||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||||
|
|
||||||
var period int = 1
|
var period int = 1
|
||||||
if len(existing.KandangHistory) > 0 {
|
if req.Periode != nil {
|
||||||
|
period = *req.Periode
|
||||||
|
} else if len(existing.KandangHistory) > 0 {
|
||||||
period = existing.KandangHistory[0].Period
|
period = existing.KandangHistory[0].Period
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1242,6 +1285,40 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, id
|
|||||||
if err := s.attachKandangs(c.Context(), dbTransaction, existing.Id, kandangIDs, periods); err != nil {
|
if err := s.attachKandangs(c.Context(), dbTransaction, existing.Id, kandangIDs, periods); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update period pada SEMUA row project_flock_kandangs milik flock ini.
|
||||||
|
// attachKandangs hanya INSERT baris baru dan melewati yang sudah ada,
|
||||||
|
// sehingga period pada baris lama tidak terupdate tanpa langkah ini.
|
||||||
|
if req.Periode != nil {
|
||||||
|
if err := dbTransaction.WithContext(c.Context()).
|
||||||
|
Model(&entity.ProjectFlockKandang{}).
|
||||||
|
Where("project_flock_id = ?", existing.Id).
|
||||||
|
Update("period", *req.Periode).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memperbarui periode kandang")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update flock_name sesuai periode baru.
|
||||||
|
if req.Periode != nil {
|
||||||
|
projectRepoTx := repository.NewProjectflockRepository(dbTransaction)
|
||||||
|
|
||||||
|
// Re-check di dalam transaksi untuk cegah race condition.
|
||||||
|
taken, err := projectRepoTx.ExistsByFlockName(c.Context(), newFlockName, &existing.Id)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa nama flock")
|
||||||
|
}
|
||||||
|
if taken {
|
||||||
|
return fiber.NewError(fiber.StatusConflict,
|
||||||
|
fmt.Sprintf("Nama flock '%s' sudah digunakan oleh project flock lain", newFlockName))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := projectRepoTx.PatchOne(c.Context(), existing.Id, map[string]any{
|
||||||
|
"flock_name": newFlockName,
|
||||||
|
}, nil); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memperbarui nama flock")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := s.UpsertProjectBudget(c.Context(), dbTransaction, existing.Id, req.ProjectBudgets); err != nil {
|
if err := s.UpsertProjectBudget(c.Context(), dbTransaction, existing.Id, req.ProjectBudgets); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ type Create struct {
|
|||||||
Category string `json:"category" validate:"required_strict"`
|
Category string `json:"category" validate:"required_strict"`
|
||||||
ProductionStandardId uint `json:"production_standard_id" validate:"required_strict,number,gt=0"`
|
ProductionStandardId uint `json:"production_standard_id" validate:"required_strict,number,gt=0"`
|
||||||
LocationId uint `json:"location_id" validate:"required_strict,number,gt=0"`
|
LocationId uint `json:"location_id" validate:"required_strict,number,gt=0"`
|
||||||
|
Periode *int `json:"periode" validate:"omitempty,gt=0"`
|
||||||
KandangIds []uint `json:"kandang_ids" validate:"required,min=1,dive,gt=0"`
|
KandangIds []uint `json:"kandang_ids" validate:"required,min=1,dive,gt=0"`
|
||||||
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required,min=1,dive"`
|
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required,min=1,dive"`
|
||||||
}
|
}
|
||||||
@@ -40,4 +41,5 @@ type ProjectBudget struct {
|
|||||||
type Resubmit struct {
|
type Resubmit struct {
|
||||||
KandangIds []uint `json:"kandang_ids" validate:"required_strict,min=1,dive,gt=0"`
|
KandangIds []uint `json:"kandang_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||||
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"`
|
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"`
|
||||||
|
Periode *int `json:"periode" validate:"omitempty,gt=0"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import (
|
|||||||
rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
|
rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
|
||||||
sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services"
|
sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services"
|
||||||
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||||
|
rSystemSettings "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/repositories"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||||
|
|
||||||
@@ -159,6 +160,8 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
validate,
|
validate,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
systemSettingRepo := rSystemSettings.NewSystemSettingRepository(db)
|
||||||
|
|
||||||
recordingService := sRecording.NewRecordingService(
|
recordingService := sRecording.NewRecordingService(
|
||||||
recordingRepo,
|
recordingRepo,
|
||||||
projectFlockKandangRepo,
|
projectFlockKandangRepo,
|
||||||
@@ -174,6 +177,7 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
transferLayingRepo,
|
transferLayingRepo,
|
||||||
transferLayingService,
|
transferLayingService,
|
||||||
validate,
|
validate,
|
||||||
|
systemSettingRepo,
|
||||||
)
|
)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
|
rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
|
||||||
sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services"
|
sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services"
|
||||||
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||||
|
rSystemSettings "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/repositories"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||||
fifo "gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
fifo "gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||||
@@ -63,6 +64,7 @@ type recordingService struct {
|
|||||||
TransferLayingSvc sTransferLaying.TransferLayingService
|
TransferLayingSvc sTransferLaying.TransferLayingService
|
||||||
FifoStockV2Svc commonSvc.FifoStockV2Service
|
FifoStockV2Svc commonSvc.FifoStockV2Service
|
||||||
StockLogRepo rStockLogs.StockLogRepository
|
StockLogRepo rStockLogs.StockLogRepository
|
||||||
|
SystemSettingRepo rSystemSettings.SystemSettingRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRecordingService(
|
func NewRecordingService(
|
||||||
@@ -80,6 +82,7 @@ func NewRecordingService(
|
|||||||
transferLayingRepo rTransferLaying.TransferLayingRepository,
|
transferLayingRepo rTransferLaying.TransferLayingRepository,
|
||||||
transferLayingSvc sTransferLaying.TransferLayingService,
|
transferLayingSvc sTransferLaying.TransferLayingService,
|
||||||
validate *validator.Validate,
|
validate *validator.Validate,
|
||||||
|
systemSettingRepo rSystemSettings.SystemSettingRepository,
|
||||||
) RecordingService {
|
) RecordingService {
|
||||||
return &recordingService{
|
return &recordingService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
@@ -97,6 +100,7 @@ func NewRecordingService(
|
|||||||
TransferLayingSvc: transferLayingSvc,
|
TransferLayingSvc: transferLayingSvc,
|
||||||
FifoStockV2Svc: fifoStockV2Svc,
|
FifoStockV2Svc: fifoStockV2Svc,
|
||||||
StockLogRepo: stockLogRepo,
|
StockLogRepo: stockLogRepo,
|
||||||
|
SystemSettingRepo: systemSettingRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,6 +394,11 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.Stocks, err = s.resolveStocksForMigrationMode(ctx, req.Stocks, pfk, actorID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if err := s.ensureProductWarehousesExist(c, req.Stocks, req.Depletions, req.Eggs); err != nil {
|
if err := s.ensureProductWarehousesExist(c, req.Stocks, req.Depletions, req.Eggs); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -635,6 +644,10 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
|||||||
if match {
|
if match {
|
||||||
hasStockChanges = false
|
hasStockChanges = false
|
||||||
} else {
|
} else {
|
||||||
|
req.Stocks, err = s.resolveStocksForMigrationMode(ctx, req.Stocks, pfkForRoute, actorID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := s.ensureProductWarehousesExist(c, req.Stocks, nil, nil); err != nil {
|
if err := s.ensureProductWarehousesExist(c, req.Stocks, nil, nil); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -2205,6 +2218,104 @@ func (s *recordingService) validateWarehouseIDs(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveMigrationWarehouseID returns the warehouse ID to use when auto-creating product_warehouse
|
||||||
|
// entries in migration mode. Prefers the farm-level (LOKASI) warehouse of the kandang's location;
|
||||||
|
// falls back to the kandang-level (KANDANG) warehouse if no LOKASI warehouse exists.
|
||||||
|
func (s *recordingService) resolveMigrationWarehouseID(ctx context.Context, kandangID uint) (uint, error) {
|
||||||
|
type row struct {
|
||||||
|
ID uint `gorm:"column:id"`
|
||||||
|
LocationID *uint `gorm:"column:location_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
db := s.ProductWarehouseRepo.DB().WithContext(ctx)
|
||||||
|
|
||||||
|
// Step 1: get the kandang's location_id
|
||||||
|
var kandang row
|
||||||
|
if err := db.Table("kandangs").Select("id, location_id").
|
||||||
|
Where("id = ? AND deleted_at IS NULL", kandangID).
|
||||||
|
Limit(1).Take(&kandang).Error; err != nil {
|
||||||
|
return 0, fmt.Errorf("kandang %d tidak ditemukan: %w", kandangID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: prefer a LOKASI-type warehouse at the kandang's location (farm-level)
|
||||||
|
if kandang.LocationID != nil && *kandang.LocationID != 0 {
|
||||||
|
var lokasi row
|
||||||
|
err := db.Table("warehouses").Select("id").
|
||||||
|
Where("type = 'LOKASI' AND location_id = ? AND deleted_at IS NULL", *kandang.LocationID).
|
||||||
|
Limit(1).Take(&lokasi).Error
|
||||||
|
if err == nil {
|
||||||
|
return lokasi.ID, nil
|
||||||
|
}
|
||||||
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return 0, fmt.Errorf("gagal mencari warehouse LOKASI untuk location %d: %w", *kandang.LocationID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 3: fall back to the KANDANG-type warehouse
|
||||||
|
var kandangWH row
|
||||||
|
if err := db.Table("warehouses").Select("id").
|
||||||
|
Where("type = 'KANDANG' AND kandang_id = ? AND deleted_at IS NULL", kandangID).
|
||||||
|
Limit(1).Take(&kandangWH).Error; err != nil {
|
||||||
|
return 0, fmt.Errorf("warehouse tidak ditemukan untuk kandang %d: %w", kandangID, err)
|
||||||
|
}
|
||||||
|
return kandangWH.ID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// resolveStocksForMigrationMode handles stocks that use product_id (instead of product_warehouse_id)
|
||||||
|
// when migration mode (allow_negative_pakan_ovk) is enabled. It finds or creates product_warehouse
|
||||||
|
// entries in the farm-level (LOKASI) warehouse, falling back to the kandang warehouse, then sets
|
||||||
|
// the resolved product_warehouse_id on each stock item.
|
||||||
|
func (s *recordingService) resolveStocksForMigrationMode(
|
||||||
|
ctx context.Context,
|
||||||
|
stocks []validation.Stock,
|
||||||
|
pfk *entity.ProjectFlockKandang,
|
||||||
|
actorID uint,
|
||||||
|
) ([]validation.Stock, error) {
|
||||||
|
if s.SystemSettingRepo == nil {
|
||||||
|
return stocks, nil
|
||||||
|
}
|
||||||
|
allowed, err := s.SystemSettingRepo.GetAllowNegativePakanOVK(ctx)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to read allow_negative_pakan_ovk setting: %+v", err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal membaca konfigurasi sistem")
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
return stocks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var warehouseID uint
|
||||||
|
warehouseResolved := false
|
||||||
|
|
||||||
|
result := make([]validation.Stock, len(stocks))
|
||||||
|
copy(result, stocks)
|
||||||
|
|
||||||
|
for i := range result {
|
||||||
|
stock := &result[i]
|
||||||
|
if stock.ProductId == nil || stock.ProductWarehouseId != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Resolve target warehouse lazily on first need (same for all stocks in one request)
|
||||||
|
if !warehouseResolved {
|
||||||
|
if pfk == nil || pfk.KandangId == 0 {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Kandang tidak ditemukan untuk mode migrasi")
|
||||||
|
}
|
||||||
|
warehouseID, err = s.resolveMigrationWarehouseID(ctx, pfk.KandangId)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to resolve migration warehouse for kandang %d: %+v", pfk.KandangId, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Warehouse tidak ditemukan untuk mode migrasi")
|
||||||
|
}
|
||||||
|
warehouseResolved = true
|
||||||
|
}
|
||||||
|
pwID, err := s.ProductWarehouseRepo.EnsureProductWarehouse(ctx, *stock.ProductId, warehouseID, nil, actorID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to ensure product warehouse for product %d in warehouse %d: %+v", *stock.ProductId, warehouseID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Gagal menyiapkan product warehouse untuk produk %d", *stock.ProductId))
|
||||||
|
}
|
||||||
|
stock.ProductWarehouseId = pwID
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *recordingService) computeRecordingDay(ctx context.Context, projectFlockKandangID uint, recordTime time.Time) (int, error) {
|
func (s *recordingService) computeRecordingDay(ctx context.Context, projectFlockKandangID uint, recordTime time.Time) (int, error) {
|
||||||
if projectFlockKandangID == 0 {
|
if projectFlockKandangID == 0 {
|
||||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
|
return 0, fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import "time"
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Stock struct {
|
Stock struct {
|
||||||
ProductWarehouseId uint `json:"product_warehouse_id" validate:"required,number,min=1"`
|
ProductWarehouseId uint `json:"product_warehouse_id" validate:"omitempty,number,min=1"`
|
||||||
|
ProductId *uint `json:"product_id,omitempty" validate:"omitempty,number,min=1"`
|
||||||
Qty float64 `json:"qty" validate:"required,gte=0"`
|
Qty float64 `json:"qty" validate:"required,gte=0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -283,6 +283,32 @@ func validatePurchaseDocumentSizes(files []*multipart.FileHeader) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctrl *PurchaseController) UpdatePoDate(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("id")
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil || id == 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
||||||
|
}
|
||||||
|
|
||||||
|
req := new(validation.UpdatePoDateRequest)
|
||||||
|
if err := c.BodyParser(req); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := ctrl.service.UpdatePoDate(c, uint(id), req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Purchase PO date updated successfully",
|
||||||
|
Data: dto.ToPurchaseDetailDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (ctrl *PurchaseController) DeleteItems(c *fiber.Ctx) error {
|
func (ctrl *PurchaseController) DeleteItems(c *fiber.Ctx) error {
|
||||||
param := c.Params("id")
|
param := c.Params("id")
|
||||||
id, err := strconv.Atoi(param)
|
id, err := strconv.Atoi(param)
|
||||||
|
|||||||
@@ -78,12 +78,13 @@ func setPurchaseExportColumns(file *excelize.File, sheet string) error {
|
|||||||
"A": 16,
|
"A": 16,
|
||||||
"B": 16,
|
"B": 16,
|
||||||
"C": 14,
|
"C": 14,
|
||||||
"D": 22,
|
"D": 14,
|
||||||
"E": 22,
|
"E": 22,
|
||||||
"F": 18,
|
"F": 22,
|
||||||
"G": 18,
|
"G": 18,
|
||||||
"H": 52,
|
"H": 18,
|
||||||
"I": 24,
|
"I": 52,
|
||||||
|
"J": 24,
|
||||||
}
|
}
|
||||||
|
|
||||||
for col, width := range columnWidths {
|
for col, width := range columnWidths {
|
||||||
@@ -103,6 +104,7 @@ func setPurchaseExportHeaders(file *excelize.File, sheet string) error {
|
|||||||
"PR Number",
|
"PR Number",
|
||||||
"PO Number",
|
"PO Number",
|
||||||
"Tanggal PO",
|
"Tanggal PO",
|
||||||
|
"Tanggal Terima",
|
||||||
"Supplier",
|
"Supplier",
|
||||||
"Lokasi",
|
"Lokasi",
|
||||||
"Status",
|
"Status",
|
||||||
@@ -136,7 +138,7 @@ func setPurchaseExportHeaders(file *excelize.File, sheet string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return file.SetCellStyle(sheet, "A1", "I1", headerStyle)
|
return file.SetCellStyle(sheet, "A1", "J1", headerStyle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.PurchaseListDTO, grandTotals map[uint]float64) error {
|
func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.PurchaseListDTO, grandTotals map[uint]float64) error {
|
||||||
@@ -155,22 +157,25 @@ func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.Purcha
|
|||||||
if err := file.SetCellValue(sheet, "C"+row, formatPurchaseExportDate(item.PoDate)); err != nil {
|
if err := file.SetCellValue(sheet, "C"+row, formatPurchaseExportDate(item.PoDate)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := file.SetCellValue(sheet, "D"+row, safePurchaseSupplierName(item)); err != nil {
|
if err := file.SetCellValue(sheet, "D"+row, formatPurchaseExportDate(item.ReceivedDate)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := file.SetCellValue(sheet, "E"+row, safePurchaseLocationName(item)); err != nil {
|
if err := file.SetCellValue(sheet, "E"+row, safePurchaseSupplierName(item)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := file.SetCellValue(sheet, "F"+row, formatPurchaseExportStatus(item)); err != nil {
|
if err := file.SetCellValue(sheet, "F"+row, safePurchaseLocationName(item)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := file.SetCellValue(sheet, "G"+row, formatPurchaseRupiah(grandTotals[item.Id])); err != nil {
|
if err := file.SetCellValue(sheet, "G"+row, formatPurchaseExportStatus(item)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := file.SetCellValue(sheet, "H"+row, formatPurchaseProducts(item)); err != nil {
|
if err := file.SetCellValue(sheet, "H"+row, formatPurchaseRupiah(grandTotals[item.Id])); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := file.SetCellValue(sheet, "I"+row, safePurchaseExportPointerText(item.Notes)); err != nil {
|
if err := file.SetCellValue(sheet, "I"+row, formatPurchaseProducts(item)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, "J"+row, safePurchaseExportPointerText(item.Notes)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -192,7 +197,7 @@ func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.Purcha
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := file.SetCellStyle(sheet, "A2", "I"+strconv.Itoa(lastRow), dataStyle); err != nil {
|
if err := file.SetCellStyle(sheet, "A2", "J"+strconv.Itoa(lastRow), dataStyle); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +217,7 @@ func setPurchaseExportRows(file *excelize.File, sheet string, items []dto.Purcha
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return file.SetCellStyle(sheet, "G2", "G"+strconv.Itoa(lastRow), moneyStyle)
|
return file.SetCellStyle(sheet, "H2", "H"+strconv.Itoa(lastRow), moneyStyle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildPurchaseGrandTotalMap(items []entity.Purchase) map[uint]float64 {
|
func buildPurchaseGrandTotalMap(items []entity.Purchase) map[uint]float64 {
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ type PurchaseListDTO struct {
|
|||||||
PurchaseRelationDTO
|
PurchaseRelationDTO
|
||||||
Supplier *supplierDTO.SupplierRelationDTO `json:"supplier"`
|
Supplier *supplierDTO.SupplierRelationDTO `json:"supplier"`
|
||||||
DueDate *time.Time `json:"due_date"`
|
DueDate *time.Time `json:"due_date"`
|
||||||
|
ReceivedDate *time.Time `json:"received_date"`
|
||||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
||||||
RequesterName string `json:"requester_name"`
|
RequesterName string `json:"requester_name"`
|
||||||
PoExpedition []PoExpeditionDTO `json:"po_expedition"`
|
PoExpedition []PoExpeditionDTO `json:"po_expedition"`
|
||||||
@@ -174,6 +175,7 @@ func ToPurchaseListDTO(p entity.Purchase) PurchaseListDTO {
|
|||||||
poExpedition = make([]PoExpeditionDTO, 0)
|
poExpedition = make([]PoExpeditionDTO, 0)
|
||||||
location *locationDTO.LocationRelationDTO
|
location *locationDTO.LocationRelationDTO
|
||||||
area *areaDTO.AreaRelationDTO
|
area *areaDTO.AreaRelationDTO
|
||||||
|
receivedDate *time.Time
|
||||||
)
|
)
|
||||||
productMap := make(map[uint]productDTO.ProductRelationDTO)
|
productMap := make(map[uint]productDTO.ProductRelationDTO)
|
||||||
expeditionRefSet := make(map[uint64]struct{})
|
expeditionRefSet := make(map[uint64]struct{})
|
||||||
@@ -205,6 +207,12 @@ func ToPurchaseListDTO(p entity.Purchase) PurchaseListDTO {
|
|||||||
ar := areaDTO.ToAreaRelationDTO(item.Warehouse.Area)
|
ar := areaDTO.ToAreaRelationDTO(item.Warehouse.Area)
|
||||||
area = &ar
|
area = &ar
|
||||||
}
|
}
|
||||||
|
if item.ReceivedDate != nil && !item.ReceivedDate.IsZero() {
|
||||||
|
if receivedDate == nil || item.ReceivedDate.Before(*receivedDate) {
|
||||||
|
t := *item.ReceivedDate
|
||||||
|
receivedDate = &t
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
products := make([]productDTO.ProductRelationDTO, 0, len(productMap))
|
products := make([]productDTO.ProductRelationDTO, 0, len(productMap))
|
||||||
for _, prod := range productMap {
|
for _, prod := range productMap {
|
||||||
@@ -215,6 +223,7 @@ func ToPurchaseListDTO(p entity.Purchase) PurchaseListDTO {
|
|||||||
PurchaseRelationDTO: ToPurchaseRelationDTO(&p),
|
PurchaseRelationDTO: ToPurchaseRelationDTO(&p),
|
||||||
Supplier: supplier,
|
Supplier: supplier,
|
||||||
DueDate: p.DueDate,
|
DueDate: p.DueDate,
|
||||||
|
ReceivedDate: receivedDate,
|
||||||
CreatedUser: createdUser,
|
CreatedUser: createdUser,
|
||||||
RequesterName: requesterName,
|
RequesterName: requesterName,
|
||||||
PoExpedition: poExpedition,
|
PoExpedition: poExpedition,
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ func Routes(router fiber.Router, purchaseService service.PurchaseService, userSe
|
|||||||
route.Post("/:id/approvals/staff", m.RequirePermissions(m.P_PurchaseApprovalStaff), ctrl.ApproveStaffPurchase)
|
route.Post("/:id/approvals/staff", m.RequirePermissions(m.P_PurchaseApprovalStaff), ctrl.ApproveStaffPurchase)
|
||||||
route.Post("/:id/approvals/manager", m.RequirePermissions(m.P_PurchaseApprovalManager), ctrl.ApproveManagerPurchase)
|
route.Post("/:id/approvals/manager", m.RequirePermissions(m.P_PurchaseApprovalManager), ctrl.ApproveManagerPurchase)
|
||||||
route.Post("/:id/receipts", m.RequirePermissions(m.P_PurchaseReceive), ctrl.ReceiveProducts)
|
route.Post("/:id/receipts", m.RequirePermissions(m.P_PurchaseReceive), ctrl.ReceiveProducts)
|
||||||
|
route.Patch("/:id/po-date", m.RequirePermissions(m.P_PurchaseUpdateOne), ctrl.UpdatePoDate)
|
||||||
route.Delete("/:id", m.RequirePermissions(m.P_PurchaseDeleteOne), ctrl.DeletePurchase)
|
route.Delete("/:id", m.RequirePermissions(m.P_PurchaseDeleteOne), ctrl.DeletePurchase)
|
||||||
route.Delete("/:id/items", m.RequirePermissions(m.P_PurchaseItemDeleteOne), ctrl.DeleteItems)
|
route.Delete("/:id/items", m.RequirePermissions(m.P_PurchaseItemDeleteOne), ctrl.DeleteItems)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ type PurchaseService interface {
|
|||||||
DeleteItems(ctx *fiber.Ctx, id uint, req *validation.DeletePurchaseItemsRequest) (*entity.Purchase, error)
|
DeleteItems(ctx *fiber.Ctx, id uint, req *validation.DeletePurchaseItemsRequest) (*entity.Purchase, error)
|
||||||
DeletePurchase(ctx *fiber.Ctx, id uint) error
|
DeletePurchase(ctx *fiber.Ctx, id uint) error
|
||||||
GetProgressRows(ctx *fiber.Ctx, query *exportprogress.Query) ([]exportprogress.Row, error)
|
GetProgressRows(ctx *fiber.Ctx, query *exportprogress.Query) ([]exportprogress.Row, error)
|
||||||
|
UpdatePoDate(ctx *fiber.Ctx, id uint, req *validation.UpdatePoDateRequest) (*entity.Purchase, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -713,6 +714,12 @@ func (s *purchaseService) ApproveManagerPurchase(c *fiber.Ctx, id uint, req *val
|
|||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
|
poDateToSet := now
|
||||||
|
if req.PoDate != nil && strings.TrimSpace(*req.PoDate) != "" {
|
||||||
|
if parsed, parseErr := utils.ParseDateString(strings.TrimSpace(*req.PoDate)); parseErr == nil {
|
||||||
|
poDateToSet = parsed.UTC()
|
||||||
|
}
|
||||||
|
}
|
||||||
hasExistingPO := purchase.PoNumber != nil && strings.TrimSpace(*purchase.PoNumber) != ""
|
hasExistingPO := purchase.PoNumber != nil && strings.TrimSpace(*purchase.PoNumber) != ""
|
||||||
var generatedNumber string
|
var generatedNumber string
|
||||||
|
|
||||||
@@ -725,7 +732,7 @@ func (s *purchaseService) ApproveManagerPurchase(c *fiber.Ctx, id uint, req *val
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
updateData["po_number"] = code
|
updateData["po_number"] = code
|
||||||
updateData["po_date"] = now
|
updateData["po_date"] = poDateToSet
|
||||||
generatedNumber = code
|
generatedNumber = code
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -770,7 +777,7 @@ func (s *purchaseService) ApproveManagerPurchase(c *fiber.Ctx, id uint, req *val
|
|||||||
|
|
||||||
if generatedNumber != "" {
|
if generatedNumber != "" {
|
||||||
purchase.PoNumber = &generatedNumber
|
purchase.PoNumber = &generatedNumber
|
||||||
purchase.PoDate = &now
|
purchase.PoDate = &poDateToSet
|
||||||
}
|
}
|
||||||
|
|
||||||
updated, err := s.PurchaseRepo.GetByID(c.Context(), purchase.Id, s.withRelations)
|
updated, err := s.PurchaseRepo.GetByID(c.Context(), purchase.Id, s.withRelations)
|
||||||
@@ -792,6 +799,38 @@ func (s *purchaseService) ApproveManagerPurchase(c *fiber.Ctx, id uint, req *val
|
|||||||
return updated, nil
|
return updated, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *purchaseService) UpdatePoDate(c *fiber.Ctx, id uint, req *validation.UpdatePoDateRequest) (*entity.Purchase, error) {
|
||||||
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed, err := utils.ParseDateString(strings.TrimSpace(req.PoDate))
|
||||||
|
if err != nil {
|
||||||
|
return nil, utils.BadRequest("po_date must use format YYYY-MM-DD")
|
||||||
|
}
|
||||||
|
poDate := parsed.UTC()
|
||||||
|
|
||||||
|
purchase, err := s.loadPurchase(c.Context(), id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.PurchaseRepo.PatchOne(c.Context(), id, map[string]any{"po_date": poDate}, nil); err != nil {
|
||||||
|
return nil, utils.Internal("Failed to update po_date")
|
||||||
|
}
|
||||||
|
|
||||||
|
purchase.PoDate = &poDate
|
||||||
|
|
||||||
|
updated, err := s.PurchaseRepo.GetByID(c.Context(), purchase.Id, s.withRelations)
|
||||||
|
if err != nil {
|
||||||
|
return nil, utils.Internal("Failed to reload purchase")
|
||||||
|
}
|
||||||
|
if err := s.attachLatestApproval(c.Context(), updated); err != nil {
|
||||||
|
s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", updated.Id, err)
|
||||||
|
}
|
||||||
|
return updated, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation.ReceivePurchaseRequest) (*entity.Purchase, error) {
|
func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation.ReceivePurchaseRequest) (*entity.Purchase, error) {
|
||||||
if err := s.Validate.Struct(req); err != nil {
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ type ApproveStaffPurchaseRequest struct {
|
|||||||
type ApproveManagerPurchaseRequest struct {
|
type ApproveManagerPurchaseRequest struct {
|
||||||
Action string `json:"action" validate:"required,oneof=APPROVED REJECTED"`
|
Action string `json:"action" validate:"required,oneof=APPROVED REJECTED"`
|
||||||
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||||
|
PoDate *string `json:"po_date,omitempty" validate:"omitempty,datetime=2006-01-02"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReceivePurchaseItemRequest struct {
|
type ReceivePurchaseItemRequest struct {
|
||||||
@@ -60,9 +61,13 @@ type DeletePurchaseItemsRequest struct {
|
|||||||
ItemIDs []uint `json:"item_ids" validate:"required,min=1,dive,gt=0"`
|
ItemIDs []uint `json:"item_ids" validate:"required,min=1,dive,gt=0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdatePoDateRequest struct {
|
||||||
|
PoDate string `json:"po_date" validate:"required,datetime=2006-01-02"`
|
||||||
|
}
|
||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1"`
|
||||||
SupplierID uint `query:"supplier_id" validate:"omitempty,gt=0"`
|
SupplierID uint `query:"supplier_id" validate:"omitempty,gt=0"`
|
||||||
AreaID uint `query:"area_id" validate:"omitempty,gt=0"`
|
AreaID uint `query:"area_id" validate:"omitempty,gt=0"`
|
||||||
LocationID uint `query:"location_id" validate:"omitempty,gt=0"`
|
LocationID uint `query:"location_id" validate:"omitempty,gt=0"`
|
||||||
|
|||||||
@@ -246,6 +246,16 @@ func (c *RepportController) GetMarketing(ctx *fiber.Ctx) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isMarketingExcelExportRequest(ctx) {
|
||||||
|
return exportMarketingReportExcel(ctx, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
if isMarketingPdfExportRequest(ctx) {
|
||||||
|
meta := buildMarketingPdfMeta(query.StartDate, query.EndDate)
|
||||||
|
return exportMarketingReportPdf(ctx, result, meta)
|
||||||
|
}
|
||||||
|
|
||||||
total := dto.ToSummaryFromDTOItems(result)
|
total := dto.ToSummaryFromDTOItems(result)
|
||||||
|
|
||||||
return ctx.Status(fiber.StatusOK).
|
return ctx.Status(fiber.StatusOK).
|
||||||
|
|||||||
@@ -0,0 +1,271 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/xuri/excelize/v2"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
|
||||||
|
)
|
||||||
|
|
||||||
|
const marketingReportExportSheetName = "Laporan Marketing Harian"
|
||||||
|
|
||||||
|
func isMarketingExcelExportRequest(c *fiber.Ctx) bool {
|
||||||
|
return strings.EqualFold(strings.TrimSpace(c.Query("export")), "excel")
|
||||||
|
}
|
||||||
|
|
||||||
|
func exportMarketingReportExcel(c *fiber.Ctx, items []dto.RepportMarketingItemDTO) error {
|
||||||
|
content, err := buildMarketingReportWorkbook(items)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "failed to generate excel file")
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := fmt.Sprintf("laporan_marketing_harian_%s.xlsx", time.Now().Format("20060102_150405"))
|
||||||
|
c.Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||||
|
c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||||
|
return c.Status(fiber.StatusOK).Send(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildMarketingReportWorkbook(items []dto.RepportMarketingItemDTO) ([]byte, error) {
|
||||||
|
file := excelize.NewFile()
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
defaultSheet := file.GetSheetName(file.GetActiveSheetIndex())
|
||||||
|
if defaultSheet != marketingReportExportSheetName {
|
||||||
|
if err := file.SetSheetName(defaultSheet, marketingReportExportSheetName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := setMarketingReportColumns(file); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := setMarketingReportHeaders(file); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := setMarketingReportRows(file, items); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer, err := file.WriteToBuffer()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setMarketingReportColumns(file *excelize.File) error {
|
||||||
|
columnWidths := map[string]float64{
|
||||||
|
"A": 10,
|
||||||
|
"B": 15,
|
||||||
|
"C": 18,
|
||||||
|
"D": 10,
|
||||||
|
"E": 25,
|
||||||
|
"F": 25,
|
||||||
|
"G": 15,
|
||||||
|
"H": 20,
|
||||||
|
"I": 15,
|
||||||
|
"J": 15,
|
||||||
|
"K": 20,
|
||||||
|
"L": 12,
|
||||||
|
"M": 20,
|
||||||
|
"N": 18,
|
||||||
|
"O": 18,
|
||||||
|
"P": 15,
|
||||||
|
"Q": 20,
|
||||||
|
"R": 20,
|
||||||
|
}
|
||||||
|
|
||||||
|
sheet := marketingReportExportSheetName
|
||||||
|
for col, width := range columnWidths {
|
||||||
|
if err := file.SetColWidth(sheet, col, col, width); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setMarketingReportHeaders(file *excelize.File) error {
|
||||||
|
sheet := marketingReportExportSheetName
|
||||||
|
headers := []string{
|
||||||
|
"No",
|
||||||
|
"Tanggal Jual",
|
||||||
|
"Tanggal Realisasi",
|
||||||
|
"Aging",
|
||||||
|
"Gudang Fisik",
|
||||||
|
"Pelanggan",
|
||||||
|
"No. DO",
|
||||||
|
"Sales/Marketing",
|
||||||
|
"No. Polisi",
|
||||||
|
"Marketing Type",
|
||||||
|
"Produk",
|
||||||
|
"Kuantitas",
|
||||||
|
"Bobot Rata-Rata (Kg)",
|
||||||
|
"Bobot Total (Kg)",
|
||||||
|
"Harga Jual (Rp)",
|
||||||
|
"HPP (Rp)",
|
||||||
|
"HPP Amount (Rp)",
|
||||||
|
"Total (Rp)",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, header := range headers {
|
||||||
|
colName, err := excelize.ColumnNumberToName(i + 1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, colName+"1", header); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setMarketingReportRows(file *excelize.File, items []dto.RepportMarketingItemDTO) error {
|
||||||
|
sheet := marketingReportExportSheetName
|
||||||
|
summary := dto.ToSummaryFromDTOItems(items)
|
||||||
|
|
||||||
|
for idx, item := range items {
|
||||||
|
row := strconv.Itoa(idx + 2)
|
||||||
|
|
||||||
|
warehouseName := "-"
|
||||||
|
if item.Warehouse != nil {
|
||||||
|
warehouseName = safeMarketingExportText(item.Warehouse.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
customerName := "-"
|
||||||
|
if item.Customer != nil {
|
||||||
|
customerName = safeMarketingExportText(item.Customer.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
salesName := "-"
|
||||||
|
if item.Sales != nil {
|
||||||
|
salesName = safeMarketingExportText(item.Sales.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
productName := "-"
|
||||||
|
if item.Product != nil {
|
||||||
|
productName = safeMarketingExportText(item.Product.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
agingText := fmt.Sprintf("%d hari", item.AgingDays)
|
||||||
|
|
||||||
|
values := []interface{}{
|
||||||
|
idx + 1,
|
||||||
|
formatMarketingDate(item.SoDate),
|
||||||
|
formatMarketingDate(item.RealizationDate),
|
||||||
|
agingText,
|
||||||
|
warehouseName,
|
||||||
|
customerName,
|
||||||
|
safeMarketingExportText(item.DoNumber),
|
||||||
|
salesName,
|
||||||
|
safeMarketingExportText(item.VehicleNumber),
|
||||||
|
safeMarketingExportText(item.MarketingType),
|
||||||
|
productName,
|
||||||
|
item.Qty,
|
||||||
|
item.AverageWeightKg,
|
||||||
|
item.TotalWeightKg,
|
||||||
|
formatMarketingRupiah(item.SalesPricePerKg),
|
||||||
|
formatMarketingRupiah(item.HppPricePerKg),
|
||||||
|
formatMarketingRupiah(item.HppAmount),
|
||||||
|
formatMarketingRupiah(item.SalesAmount),
|
||||||
|
}
|
||||||
|
|
||||||
|
for colIdx, val := range values {
|
||||||
|
colName, err := excelize.ColumnNumberToName(colIdx + 1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, colName+row, val); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Baris TOTAL
|
||||||
|
totalRow := strconv.Itoa(len(items) + 2)
|
||||||
|
if err := file.SetCellValue(sheet, "A"+totalRow, "TOTAL"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if summary != nil {
|
||||||
|
if err := file.SetCellValue(sheet, "L"+totalRow, summary.TotalQty); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, "M"+totalRow, summary.AverageWeightKg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, "N"+totalRow, summary.TotalWeightKg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, "O"+totalRow, formatMarketingRupiah(summary.AverageSalesPrice)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, "P"+totalRow, formatMarketingRupiah(summary.TotalHppPricePerKg)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, "Q"+totalRow, formatMarketingRupiah(float64(summary.TotalHppAmount))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.SetCellValue(sheet, "R"+totalRow, formatMarketingRupiah(float64(summary.TotalSalesAmount))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatMarketingDate(t time.Time) string {
|
||||||
|
if t.IsZero() {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := time.LoadLocation("Asia/Jakarta")
|
||||||
|
if err == nil {
|
||||||
|
t = t.In(location)
|
||||||
|
}
|
||||||
|
|
||||||
|
return t.Format("02 Jan 2006")
|
||||||
|
}
|
||||||
|
|
||||||
|
func safeMarketingExportText(value string) string {
|
||||||
|
trimmed := strings.TrimSpace(value)
|
||||||
|
if trimmed == "" {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatMarketingRupiah formats a float64 as Indonesian Rupiah string.
|
||||||
|
// e.g. 1000000 → "Rp 1.000.000"
|
||||||
|
func formatMarketingRupiah(value float64) string {
|
||||||
|
rounded := int64(math.Round(value))
|
||||||
|
|
||||||
|
negative := rounded < 0
|
||||||
|
abs := rounded
|
||||||
|
if negative {
|
||||||
|
abs = -rounded
|
||||||
|
}
|
||||||
|
|
||||||
|
numStr := strconv.FormatInt(abs, 10)
|
||||||
|
n := len(numStr)
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
for i, c := range numStr {
|
||||||
|
if i > 0 && (n-i)%3 == 0 {
|
||||||
|
b.WriteByte('.')
|
||||||
|
}
|
||||||
|
b.WriteRune(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
if negative {
|
||||||
|
return "Rp -" + b.String()
|
||||||
|
}
|
||||||
|
return "Rp " + b.String()
|
||||||
|
}
|
||||||
@@ -0,0 +1,510 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-pdf/fpdf"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Trigger
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func isMarketingPdfExportRequest(c *fiber.Ctx) bool {
|
||||||
|
return strings.EqualFold(strings.TrimSpace(c.Query("export")), "pdf")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HTTP handler
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func exportMarketingReportPdf(c *fiber.Ctx, items []dto.RepportMarketingItemDTO, query marketingPdfQueryMeta) error {
|
||||||
|
content, err := buildMarketingReportPdf(items, query)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "failed to generate pdf file")
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := fmt.Sprintf("laporan_marketing_harian_%s.pdf", time.Now().Format("20060102_150405"))
|
||||||
|
c.Set("Content-Type", "application/pdf")
|
||||||
|
c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||||
|
return c.Status(fiber.StatusOK).Send(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// marketingPdfQueryMeta holds display metadata for the PDF header.
|
||||||
|
type marketingPdfQueryMeta struct {
|
||||||
|
StartDate string // e.g. "01 April 2026"
|
||||||
|
EndDate string // e.g. "29 April 2026"
|
||||||
|
PrintedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Column definitions
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
type pdfColumn struct {
|
||||||
|
header string
|
||||||
|
width float64 // mm
|
||||||
|
align string // "L", "C", "R"
|
||||||
|
}
|
||||||
|
|
||||||
|
var marketingPdfColumns = []pdfColumn{
|
||||||
|
{"No", 6, "C"},
|
||||||
|
{"Tanggal Sales Order", 16, "C"},
|
||||||
|
{"Tanggal Delivery Order", 16, "C"},
|
||||||
|
{"Aging\n(Hari)", 9, "C"},
|
||||||
|
{"Gudang Fisik", 20, "L"},
|
||||||
|
{"Pelanggan", 20, "L"},
|
||||||
|
{"Sales", 18, "L"},
|
||||||
|
{"Produk", 16, "L"},
|
||||||
|
{"Nomor DO", 14, "C"},
|
||||||
|
{"Nomor Polisi", 14, "C"},
|
||||||
|
{"Tipe\nMarketing", 14, "C"},
|
||||||
|
{"Quantity", 13, "R"},
|
||||||
|
{"Rata-Rata\n(Kg)", 13, "R"},
|
||||||
|
{"Total Berat\n(Kg)", 14, "R"},
|
||||||
|
{"Harga Jual\n(Rp)", 17, "R"},
|
||||||
|
{"HPP\n(Rp)", 17, "R"},
|
||||||
|
{"Total Jual\n(Rp)", 18, "R"},
|
||||||
|
{"Total HPP\n(Rp)", 18, "R"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Colours
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const (
|
||||||
|
headerR, headerG, headerB = 30, 64, 120 // dark blue header bg
|
||||||
|
headerTextR, headerTextG, headerTextB = 255, 255, 255 // white header text
|
||||||
|
rowAltR, rowAltG, rowAltB = 245, 247, 250 // alternating row bg
|
||||||
|
borderR, borderG, borderB = 200, 200, 200 // light border
|
||||||
|
|
||||||
|
badgeTelurR, badgeTelurG, badgeTelurB = 59, 130, 246 // blue
|
||||||
|
badgeAyamR, badgeAyamG, badgeAyamB = 34, 197, 94 // green
|
||||||
|
badgeTradingR, badgeTradingG, badgeTradingB = 249, 115, 22 // orange
|
||||||
|
badgeDefaultR, badgeDefaultG, badgeDefaultB = 107, 114, 128 // gray
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Workbook builder
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func buildMarketingReportPdf(items []dto.RepportMarketingItemDTO, meta marketingPdfQueryMeta) ([]byte, error) {
|
||||||
|
pdf := fpdf.New("L", "mm", "A4", "")
|
||||||
|
pdf.SetMargins(10, 12, 10)
|
||||||
|
pdf.SetAutoPageBreak(true, 12)
|
||||||
|
pdf.AddPage()
|
||||||
|
|
||||||
|
// ---- title ----
|
||||||
|
pdf.SetFont("Helvetica", "B", 14)
|
||||||
|
pdf.SetTextColor(30, 64, 120)
|
||||||
|
pdf.CellFormat(0, 8, "Laporan > Penjualan Harian", "", 1, "L", false, 0, "")
|
||||||
|
|
||||||
|
// ---- subtitle ----
|
||||||
|
pdf.SetFont("Helvetica", "", 8)
|
||||||
|
pdf.SetTextColor(80, 80, 80)
|
||||||
|
dateLabel := buildMarketingPdfDateLabel(meta)
|
||||||
|
printedAt := formatMarketingPdfDateTime(meta.PrintedAt)
|
||||||
|
pdf.CellFormat(0, 5, fmt.Sprintf("%s Dicetak: %s", dateLabel, printedAt), "", 1, "L", false, 0, "")
|
||||||
|
pdf.Ln(3)
|
||||||
|
|
||||||
|
// ---- table ----
|
||||||
|
writeMarketingPdfHeader(pdf)
|
||||||
|
writeMarketingPdfRows(pdf, items)
|
||||||
|
writeMarketingPdfTotal(pdf, items)
|
||||||
|
|
||||||
|
return marshalMarketingPdf(pdf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func marshalMarketingPdf(pdf *fpdf.Fpdf) ([]byte, error) {
|
||||||
|
w := &pdfByteBuffer{}
|
||||||
|
err := pdf.Output(w)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return w.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// pdfByteBuffer implements io.Writer and accumulates bytes.
|
||||||
|
type pdfByteBuffer struct {
|
||||||
|
buf []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *pdfByteBuffer) Write(p []byte) (n int, err error) {
|
||||||
|
b.buf = append(b.buf, p...)
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *pdfByteBuffer) Bytes() []byte { return b.buf }
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Header row
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func writeMarketingPdfHeader(pdf *fpdf.Fpdf) {
|
||||||
|
pdf.SetFont("Helvetica", "B", 6.5)
|
||||||
|
pdf.SetFillColor(headerR, headerG, headerB)
|
||||||
|
pdf.SetTextColor(headerTextR, headerTextG, headerTextB)
|
||||||
|
pdf.SetDrawColor(borderR, borderG, borderB)
|
||||||
|
pdf.SetLineWidth(0.1)
|
||||||
|
|
||||||
|
headerH := 9.0 // height of header row (mm)
|
||||||
|
|
||||||
|
x0 := pdf.GetX()
|
||||||
|
y0 := pdf.GetY()
|
||||||
|
|
||||||
|
for _, col := range marketingPdfColumns {
|
||||||
|
lines := strings.Split(col.header, "\n")
|
||||||
|
lineH := headerH / float64(len(lines))
|
||||||
|
|
||||||
|
x := pdf.GetX()
|
||||||
|
for li, line := range lines {
|
||||||
|
pdf.SetXY(x, y0+float64(li)*lineH)
|
||||||
|
pdf.CellFormat(col.width, lineH, line, "", 0, "C", true, 0, "")
|
||||||
|
}
|
||||||
|
pdf.SetXY(x+col.width, y0)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = x0
|
||||||
|
pdf.SetXY(10, y0+headerH)
|
||||||
|
pdf.Ln(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Data rows
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func writeMarketingPdfRows(pdf *fpdf.Fpdf, items []dto.RepportMarketingItemDTO) {
|
||||||
|
pdf.SetFont("Helvetica", "", 6)
|
||||||
|
pdf.SetDrawColor(borderR, borderG, borderB)
|
||||||
|
pdf.SetLineWidth(0.1)
|
||||||
|
|
||||||
|
rowH := 6.0
|
||||||
|
|
||||||
|
for idx, item := range items {
|
||||||
|
// page break check
|
||||||
|
if pdf.GetY()+rowH > marketingPdfPageHeight(pdf)-12 {
|
||||||
|
pdf.AddPage()
|
||||||
|
writeMarketingPdfHeader(pdf)
|
||||||
|
pdf.SetFont("Helvetica", "", 6)
|
||||||
|
}
|
||||||
|
|
||||||
|
// alternating bg
|
||||||
|
if idx%2 == 1 {
|
||||||
|
pdf.SetFillColor(rowAltR, rowAltG, rowAltB)
|
||||||
|
} else {
|
||||||
|
pdf.SetFillColor(255, 255, 255)
|
||||||
|
}
|
||||||
|
pdf.SetTextColor(40, 40, 40)
|
||||||
|
|
||||||
|
y := pdf.GetY()
|
||||||
|
writeMarketingPdfRow(pdf, idx+1, item, rowH, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeMarketingPdfRow(pdf *fpdf.Fpdf, no int, item dto.RepportMarketingItemDTO, h, y float64) {
|
||||||
|
fill := true // use the fill colour already set
|
||||||
|
|
||||||
|
cols := marketingPdfColumns
|
||||||
|
x := 10.0 // left margin
|
||||||
|
|
||||||
|
values := marketingPdfRowValues(no, item)
|
||||||
|
|
||||||
|
for i, col := range cols {
|
||||||
|
pdf.SetXY(x, y)
|
||||||
|
|
||||||
|
if i == 10 { // Tipe Marketing → badge
|
||||||
|
drawMarketingTypeBadge(pdf, x, y, col.width, h, item.MarketingType)
|
||||||
|
} else {
|
||||||
|
pdf.CellFormat(col.width, h, values[i], "1", 0, col.align, fill, 0, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
x += col.width
|
||||||
|
}
|
||||||
|
|
||||||
|
pdf.SetXY(10, y+h)
|
||||||
|
}
|
||||||
|
|
||||||
|
func marketingPdfRowValues(no int, item dto.RepportMarketingItemDTO) []string {
|
||||||
|
warehouse := "-"
|
||||||
|
if item.Warehouse != nil {
|
||||||
|
warehouse = safeMarketingExportText(item.Warehouse.Name)
|
||||||
|
}
|
||||||
|
customer := "-"
|
||||||
|
if item.Customer != nil {
|
||||||
|
customer = safeMarketingExportText(item.Customer.Name)
|
||||||
|
}
|
||||||
|
sales := "-"
|
||||||
|
if item.Sales != nil {
|
||||||
|
sales = safeMarketingExportText(item.Sales.Name)
|
||||||
|
}
|
||||||
|
product := "-"
|
||||||
|
if item.Product != nil {
|
||||||
|
product = safeMarketingExportText(item.Product.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return []string{
|
||||||
|
strconv.Itoa(no),
|
||||||
|
formatMarketingDate(item.SoDate),
|
||||||
|
formatMarketingDate(item.RealizationDate),
|
||||||
|
strconv.Itoa(item.AgingDays),
|
||||||
|
warehouse,
|
||||||
|
customer,
|
||||||
|
sales,
|
||||||
|
product,
|
||||||
|
safeMarketingExportText(item.DoNumber),
|
||||||
|
safeMarketingExportText(item.VehicleNumber),
|
||||||
|
safeMarketingExportText(item.MarketingType), // index 10, overridden by badge
|
||||||
|
formatMarketingPdfNumber(item.Qty),
|
||||||
|
formatMarketingPdfDecimal(item.AverageWeightKg),
|
||||||
|
formatMarketingPdfDecimal(item.TotalWeightKg),
|
||||||
|
formatMarketingPdfRupiah(item.SalesPricePerKg),
|
||||||
|
formatMarketingPdfRupiah(item.HppPricePerKg),
|
||||||
|
formatMarketingPdfRupiah(item.SalesAmount),
|
||||||
|
formatMarketingPdfRupiah(item.HppAmount),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Total row
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func writeMarketingPdfTotal(pdf *fpdf.Fpdf, items []dto.RepportMarketingItemDTO) {
|
||||||
|
summary := dto.ToSummaryFromDTOItems(items)
|
||||||
|
if summary == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rowH := 6.5
|
||||||
|
|
||||||
|
if pdf.GetY()+rowH > marketingPdfPageHeight(pdf)-12 {
|
||||||
|
pdf.AddPage()
|
||||||
|
writeMarketingPdfHeader(pdf)
|
||||||
|
}
|
||||||
|
|
||||||
|
pdf.SetFont("Helvetica", "B", 6)
|
||||||
|
pdf.SetFillColor(220, 230, 245)
|
||||||
|
pdf.SetTextColor(30, 64, 120)
|
||||||
|
pdf.SetDrawColor(borderR, borderG, borderB)
|
||||||
|
pdf.SetLineWidth(0.1)
|
||||||
|
|
||||||
|
y := pdf.GetY()
|
||||||
|
x := 10.0
|
||||||
|
|
||||||
|
// merge first 11 cols (No … Tipe Marketing) into "TOTAL" label
|
||||||
|
mergedWidth := 0.0
|
||||||
|
for i := 0; i < 11; i++ {
|
||||||
|
mergedWidth += marketingPdfColumns[i].width
|
||||||
|
}
|
||||||
|
pdf.SetXY(x, y)
|
||||||
|
pdf.CellFormat(mergedWidth, rowH, "TOTAL", "1", 0, "R", true, 0, "")
|
||||||
|
x += mergedWidth
|
||||||
|
|
||||||
|
totals := []string{
|
||||||
|
formatMarketingPdfNumber(float64(summary.TotalQty)),
|
||||||
|
formatMarketingPdfDecimal(summary.AverageWeightKg),
|
||||||
|
formatMarketingPdfDecimal(summary.TotalWeightKg),
|
||||||
|
formatMarketingPdfRupiah(summary.AverageSalesPrice),
|
||||||
|
formatMarketingPdfRupiah(summary.TotalHppPricePerKg),
|
||||||
|
formatMarketingPdfRupiah(float64(summary.TotalSalesAmount)),
|
||||||
|
formatMarketingPdfRupiah(float64(summary.TotalHppAmount)),
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, val := range totals {
|
||||||
|
col := marketingPdfColumns[11+i]
|
||||||
|
pdf.SetXY(x, y)
|
||||||
|
pdf.CellFormat(col.width, rowH, val, "1", 0, "R", true, 0, "")
|
||||||
|
x += col.width
|
||||||
|
}
|
||||||
|
|
||||||
|
pdf.SetXY(10, y+rowH)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Badge drawer
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func drawMarketingTypeBadge(pdf *fpdf.Fpdf, x, y, w, h float64, marketingType string) {
|
||||||
|
lower := strings.ToLower(strings.TrimSpace(marketingType))
|
||||||
|
|
||||||
|
var r, g, b int
|
||||||
|
switch lower {
|
||||||
|
case "telur":
|
||||||
|
r, g, b = badgeTelurR, badgeTelurG, badgeTelurB
|
||||||
|
case "ayam":
|
||||||
|
r, g, b = badgeAyamR, badgeAyamG, badgeAyamB
|
||||||
|
case "trading":
|
||||||
|
r, g, b = badgeTradingR, badgeTradingG, badgeTradingB
|
||||||
|
default:
|
||||||
|
r, g, b = badgeDefaultR, badgeDefaultG, badgeDefaultB
|
||||||
|
}
|
||||||
|
|
||||||
|
// badge background (slightly inset from the cell border)
|
||||||
|
padH := 1.2
|
||||||
|
padV := 1.2
|
||||||
|
bx := x + padH
|
||||||
|
by := y + padV
|
||||||
|
bw := w - padH*2
|
||||||
|
bh := h - padV*2
|
||||||
|
|
||||||
|
pdf.SetFillColor(r, g, b)
|
||||||
|
pdf.SetDrawColor(r, g, b)
|
||||||
|
pdf.RoundedRect(bx, by, bw, bh, 1.5, "1234", "FD")
|
||||||
|
|
||||||
|
// border of the cell itself (transparent bg, just border)
|
||||||
|
pdf.SetDrawColor(borderR, borderG, borderB)
|
||||||
|
pdf.SetFillColor(255, 255, 255)
|
||||||
|
pdf.SetXY(x, y)
|
||||||
|
pdf.CellFormat(w, h, "", "1", 0, "C", false, 0, "")
|
||||||
|
|
||||||
|
// badge label
|
||||||
|
pdf.SetFont("Helvetica", "B", 5.5)
|
||||||
|
pdf.SetTextColor(255, 255, 255)
|
||||||
|
label := strings.Title(strings.ToLower(marketingType))
|
||||||
|
if label == "" {
|
||||||
|
label = "-"
|
||||||
|
}
|
||||||
|
textW := pdf.GetStringWidth(label)
|
||||||
|
pdf.SetXY(bx+(bw-textW)/2, by+(bh-3)/2)
|
||||||
|
pdf.CellFormat(textW, 3, label, "", 0, "C", false, 0, "")
|
||||||
|
|
||||||
|
// reset
|
||||||
|
pdf.SetFont("Helvetica", "", 6)
|
||||||
|
pdf.SetTextColor(40, 40, 40)
|
||||||
|
pdf.SetDrawColor(borderR, borderG, borderB)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helpers — date/time
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// buildMarketingPdfMeta converts raw query string dates into display meta.
|
||||||
|
func buildMarketingPdfMeta(startDate, endDate string) marketingPdfQueryMeta {
|
||||||
|
loc, _ := time.LoadLocation("Asia/Jakarta")
|
||||||
|
|
||||||
|
format := func(s string) string {
|
||||||
|
t, err := time.ParseInLocation("2006-01-02", s, loc)
|
||||||
|
if err != nil {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return t.Format("02 January 2006")
|
||||||
|
}
|
||||||
|
|
||||||
|
return marketingPdfQueryMeta{
|
||||||
|
StartDate: format(startDate),
|
||||||
|
EndDate: format(endDate),
|
||||||
|
PrintedAt: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildMarketingPdfDateLabel(meta marketingPdfQueryMeta) string {
|
||||||
|
if meta.StartDate != "" && meta.EndDate != "" {
|
||||||
|
return fmt.Sprintf("Tanggal: %s - %s", meta.StartDate, meta.EndDate)
|
||||||
|
}
|
||||||
|
if meta.StartDate != "" {
|
||||||
|
return fmt.Sprintf("Tanggal: %s", meta.StartDate)
|
||||||
|
}
|
||||||
|
if meta.EndDate != "" {
|
||||||
|
return fmt.Sprintf("Tanggal: %s", meta.EndDate)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("Tanggal: %s", time.Now().Format("02 January 2006"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatMarketingPdfDateTime(t time.Time) string {
|
||||||
|
if t.IsZero() {
|
||||||
|
t = time.Now()
|
||||||
|
}
|
||||||
|
loc, err := time.LoadLocation("Asia/Jakarta")
|
||||||
|
if err == nil {
|
||||||
|
t = t.In(loc)
|
||||||
|
}
|
||||||
|
return t.Format("02 Jan 2006 15:04")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helpers — number formatting
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// formatMarketingPdfNumber formats a float64 as an integer with period-thousands separator.
|
||||||
|
// e.g. 7299 → "7.299"
|
||||||
|
func formatMarketingPdfNumber(v float64) string {
|
||||||
|
return formatMarketingPdfThousands(int64(math.Round(v)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatMarketingPdfDecimal formats a float64 with 2 decimal places (Indonesian locale).
|
||||||
|
// e.g. 452.54 → "452,54"
|
||||||
|
func formatMarketingPdfDecimal(v float64) string {
|
||||||
|
rounded := math.Round(v*100) / 100
|
||||||
|
intPart := int64(rounded)
|
||||||
|
fracPart := int64(math.Round((rounded - float64(intPart)) * 100))
|
||||||
|
if fracPart < 0 {
|
||||||
|
fracPart = -fracPart
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s,%02d", formatMarketingPdfThousands(intPart), fracPart)
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatMarketingPdfRupiah formats a float64 as Rupiah with Indonesian locale.
|
||||||
|
// Drops trailing ",00" decimals for whole numbers.
|
||||||
|
// e.g. 25500 → "Rp 25.500" | 240896.94 → "Rp 240.896,94"
|
||||||
|
func formatMarketingPdfRupiah(v float64) string {
|
||||||
|
rounded := math.Round(v*100) / 100
|
||||||
|
intPart := int64(rounded)
|
||||||
|
fracPart := int64(math.Round((rounded - float64(intPart)) * 100))
|
||||||
|
if fracPart < 0 {
|
||||||
|
fracPart = -fracPart
|
||||||
|
}
|
||||||
|
|
||||||
|
negative := intPart < 0
|
||||||
|
absInt := intPart
|
||||||
|
if negative {
|
||||||
|
absInt = -intPart
|
||||||
|
}
|
||||||
|
|
||||||
|
s := formatMarketingPdfThousands(absInt)
|
||||||
|
var result string
|
||||||
|
if fracPart == 0 {
|
||||||
|
result = "Rp " + s
|
||||||
|
} else {
|
||||||
|
result = fmt.Sprintf("Rp %s,%02d", s, fracPart)
|
||||||
|
}
|
||||||
|
if negative {
|
||||||
|
return "Rp -" + s
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// marketingPdfPageHeight returns the page height in mm.
|
||||||
|
func marketingPdfPageHeight(pdf *fpdf.Fpdf) float64 {
|
||||||
|
_, h := pdf.GetPageSize()
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatMarketingPdfThousands inserts period every 3 digits.
|
||||||
|
func formatMarketingPdfThousands(v int64) string {
|
||||||
|
negative := v < 0
|
||||||
|
abs := v
|
||||||
|
if negative {
|
||||||
|
abs = -v
|
||||||
|
}
|
||||||
|
|
||||||
|
numStr := strconv.FormatInt(abs, 10)
|
||||||
|
n := len(numStr)
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
for i, c := range numStr {
|
||||||
|
if i > 0 && (n-i)%3 == 0 {
|
||||||
|
b.WriteByte('.')
|
||||||
|
}
|
||||||
|
b.WriteRune(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
if negative {
|
||||||
|
return "-" + b.String()
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ package validation
|
|||||||
|
|
||||||
type ExpenseQuery struct {
|
type ExpenseQuery struct {
|
||||||
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,min=1,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=100"`
|
Search string `query:"search" validate:"omitempty,max=100"`
|
||||||
Category string `query:"category" validate:"omitempty,oneof=BOP NON-BOP"`
|
Category string `query:"category" validate:"omitempty,oneof=BOP NON-BOP"`
|
||||||
SupplierId int64 `query:"supplier_id" validate:"omitempty"`
|
SupplierId int64 `query:"supplier_id" validate:"omitempty"`
|
||||||
@@ -65,7 +65,7 @@ type DebtSupplierQuery struct {
|
|||||||
|
|
||||||
type HppPerKandangQuery struct {
|
type HppPerKandangQuery struct {
|
||||||
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,min=1,max=1000,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,min=10,gt=0"`
|
||||||
Period string `query:"period" validate:"required"`
|
Period string `query:"period" validate:"required"`
|
||||||
ShowUnrecorded bool `query:"show_unrecorded"`
|
ShowUnrecorded bool `query:"show_unrecorded"`
|
||||||
AreaIDs []int64 `query:"-"`
|
AreaIDs []int64 `query:"-"`
|
||||||
@@ -82,7 +82,7 @@ type HppV2BreakdownQuery struct {
|
|||||||
|
|
||||||
type ExpenseDepreciationQuery struct {
|
type ExpenseDepreciationQuery struct {
|
||||||
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,min=1,max=1000,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,min=10,gt=0"`
|
||||||
Period string `query:"period" validate:"required,datetime=2006-01-02"`
|
Period string `query:"period" validate:"required,datetime=2006-01-02"`
|
||||||
ForceRecompute bool `query:"force_recompute"`
|
ForceRecompute bool `query:"force_recompute"`
|
||||||
ProjectFlockIDs []int64 `query:"-"`
|
ProjectFlockIDs []int64 `query:"-"`
|
||||||
@@ -105,7 +105,7 @@ type ProductionResultQuery struct {
|
|||||||
|
|
||||||
type CustomerPaymentQuery struct {
|
type CustomerPaymentQuery struct {
|
||||||
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,min=1,gt=0"`
|
||||||
CustomerIDs []uint `query:"customer_ids" validate:"omitempty,dive,gt=0"`
|
CustomerIDs []uint `query:"customer_ids" validate:"omitempty,dive,gt=0"`
|
||||||
FilterBy string `query:"filter_by" validate:"omitempty,oneof=TRANS_DATE REALIZATION_DATE"`
|
FilterBy string `query:"filter_by" validate:"omitempty,oneof=TRANS_DATE REALIZATION_DATE"`
|
||||||
StartDate string `query:"start_date" validate:"omitempty,datetime=2006-01-02"`
|
StartDate string `query:"start_date" validate:"omitempty,datetime=2006-01-02"`
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/services"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SystemSettingController struct {
|
||||||
|
Service service.SystemSettingService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSystemSettingController(svc service.SystemSettingService) *SystemSettingController {
|
||||||
|
return &SystemSettingController{Service: svc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctrl *SystemSettingController) GetAll(c *fiber.Ctx) error {
|
||||||
|
settings, err := ctrl.Service.GetAll(c.Context())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get all system settings successfully",
|
||||||
|
Data: settings,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type setAllowNegativePakanOVKRequest struct {
|
||||||
|
Value bool `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctrl *SystemSettingController) SetAllowNegativePakanOVK(c *fiber.Ctx) error {
|
||||||
|
var req setAllowNegativePakanOVKRequest
|
||||||
|
if err := c.BodyParser(&req); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Request body tidak valid")
|
||||||
|
}
|
||||||
|
if err := ctrl.Service.SetAllowNegativePakanOVK(c.Context(), req.Value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(response.Common{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Setting berhasil diperbarui",
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package systemsettings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/repositories"
|
||||||
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/services"
|
||||||
|
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||||
|
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SystemSettingsModule struct{}
|
||||||
|
|
||||||
|
func (SystemSettingsModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
|
userRepo := rUser.NewUserRepository(db)
|
||||||
|
userSvc := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
|
repo := repository.NewSystemSettingRepository(db)
|
||||||
|
svc := service.NewSystemSettingService(repo)
|
||||||
|
SystemSettingRoutes(router, userSvc, svc)
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SystemSettingRepository interface {
|
||||||
|
Get(ctx context.Context, key string) (*entity.SystemSetting, error)
|
||||||
|
Set(ctx context.Context, key, value string) error
|
||||||
|
List(ctx context.Context) ([]entity.SystemSetting, error)
|
||||||
|
GetAllowNegativePakanOVK(ctx context.Context) (bool, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type systemSettingRepositoryImpl struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSystemSettingRepository(db *gorm.DB) SystemSettingRepository {
|
||||||
|
return &systemSettingRepositoryImpl{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *systemSettingRepositoryImpl) Get(ctx context.Context, key string) (*entity.SystemSetting, error) {
|
||||||
|
var setting entity.SystemSetting
|
||||||
|
if err := r.db.WithContext(ctx).Where("key = ?", key).First(&setting).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &setting, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *systemSettingRepositoryImpl) Set(ctx context.Context, key, value string) error {
|
||||||
|
return r.db.WithContext(ctx).
|
||||||
|
Model(&entity.SystemSetting{}).
|
||||||
|
Where("key = ?", key).
|
||||||
|
Updates(map[string]interface{}{
|
||||||
|
"value": value,
|
||||||
|
"updated_at": time.Now(),
|
||||||
|
}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *systemSettingRepositoryImpl) List(ctx context.Context) ([]entity.SystemSetting, error) {
|
||||||
|
var settings []entity.SystemSetting
|
||||||
|
if err := r.db.WithContext(ctx).Order("key ASC").Find(&settings).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return settings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *systemSettingRepositoryImpl) GetAllowNegativePakanOVK(ctx context.Context) (bool, error) {
|
||||||
|
setting, err := r.Get(ctx, entity.SystemSettingKeyAllowNegativePakanOVK)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return setting.Value == "true", nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package systemsettings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/controllers"
|
||||||
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/services"
|
||||||
|
userService "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SystemSettingRoutes(v1 fiber.Router, u userService.UserService, svc service.SystemSettingService) {
|
||||||
|
ctrl := controller.NewSystemSettingController(svc)
|
||||||
|
|
||||||
|
route := v1.Group("/system-settings")
|
||||||
|
route.Use(m.Auth(u))
|
||||||
|
|
||||||
|
route.Get("/", ctrl.GetAll)
|
||||||
|
route.Patch("/allow-negative-pakan-ovk", m.RequirePermissions(m.P_SystemSettingUpdate), ctrl.SetAllowNegativePakanOVK)
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings/repositories"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SystemSettingService interface {
|
||||||
|
GetAll(ctx context.Context) ([]entity.SystemSetting, error)
|
||||||
|
GetAllowNegativePakanOVK(ctx context.Context) (bool, error)
|
||||||
|
SetAllowNegativePakanOVK(ctx context.Context, allow bool) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type systemSettingService struct {
|
||||||
|
Repository repository.SystemSettingRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSystemSettingService(repo repository.SystemSettingRepository) SystemSettingService {
|
||||||
|
return &systemSettingService{Repository: repo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *systemSettingService) GetAll(ctx context.Context) ([]entity.SystemSetting, error) {
|
||||||
|
settings, err := s.Repository.List(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil system settings")
|
||||||
|
}
|
||||||
|
return settings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *systemSettingService) GetAllowNegativePakanOVK(ctx context.Context) (bool, error) {
|
||||||
|
return s.Repository.GetAllowNegativePakanOVK(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *systemSettingService) SetAllowNegativePakanOVK(ctx context.Context, allow bool) error {
|
||||||
|
value := "false"
|
||||||
|
if allow {
|
||||||
|
value = "true"
|
||||||
|
}
|
||||||
|
if err := s.Repository.Set(ctx, entity.SystemSettingKeyAllowNegativePakanOVK, value); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal mengubah setting allow_negative_pakan_ovk")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
ssoModule "gitlab.com/mbugroup/lti-api.git/internal/modules/sso"
|
ssoModule "gitlab.com/mbugroup/lti-api.git/internal/modules/sso"
|
||||||
users "gitlab.com/mbugroup/lti-api.git/internal/modules/users"
|
users "gitlab.com/mbugroup/lti-api.git/internal/modules/users"
|
||||||
dashboards "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards"
|
dashboards "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards"
|
||||||
|
systemsettings "gitlab.com/mbugroup/lti-api.git/internal/modules/system-settings"
|
||||||
// MODULE IMPORTS
|
// MODULE IMPORTS
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,6 +51,7 @@ func Routes(app *fiber.App, db *gorm.DB) {
|
|||||||
finance.FinanceModule{},
|
finance.FinanceModule{},
|
||||||
dailyChecklists.DailyChecklistModule{},
|
dailyChecklists.DailyChecklistModule{},
|
||||||
dashboards.DashboardModule{},
|
dashboards.DashboardModule{},
|
||||||
|
systemsettings.SystemSettingsModule{},
|
||||||
// MODULE REGISTRY
|
// MODULE REGISTRY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user