mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 07:15:43 +00:00
fix: reimplement transfer to laying logics separating effective financial date and physical transfer date
This commit is contained in:
@@ -293,7 +293,8 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
||||
category := strings.ToUpper(pfk.ProjectFlock.Category)
|
||||
isLaying := category == strings.ToUpper(string(utils.ProjectFlockCategoryLaying))
|
||||
|
||||
if err := s.enforceTransferRecordingRoute(ctx, pfk, recordTime); err != nil {
|
||||
routePayload := buildRecordingRoutePayloadFromCreate(req)
|
||||
if err := s.enforceTransferRecordingRoute(ctx, pfk, recordTime, routePayload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -494,6 +495,23 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
}
|
||||
|
||||
recordingEntity = recording
|
||||
pfkForRoute := recordingEntity.ProjectFlockKandang
|
||||
if pfkForRoute == nil || pfkForRoute.Id == 0 {
|
||||
fetchedPfk, fetchErr := s.ProjectFlockKandangRepo.GetByIDLight(ctx, recordingEntity.ProjectFlockKandangId)
|
||||
if fetchErr != nil {
|
||||
if errors.Is(fetchErr, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Project flock kandang not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to fetch project flock kandang for route validation: %+v", fetchErr)
|
||||
return fetchErr
|
||||
}
|
||||
pfkForRoute = fetchedPfk
|
||||
}
|
||||
routePayload := buildRecordingRoutePayloadFromUpdate(req)
|
||||
if err := s.enforceTransferRecordingRoute(ctx, pfkForRoute, recordingEntity.RecordDatetime, routePayload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hasStockChanges := req.Stocks != nil
|
||||
hasDepletionChanges := req.Depletions != nil
|
||||
hasEggChanges := req.Eggs != nil
|
||||
@@ -909,6 +927,7 @@ func (s *recordingService) enforceTransferRecordingRoute(
|
||||
ctx context.Context,
|
||||
pfk *entity.ProjectFlockKandang,
|
||||
recordTime time.Time,
|
||||
payload recordingRoutePayload,
|
||||
) error {
|
||||
if pfk == nil || pfk.Id == 0 || s.TransferLayingRepo == nil {
|
||||
return nil
|
||||
@@ -928,22 +947,35 @@ func (s *recordingService) enforceTransferRecordingRoute(
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi transfer laying")
|
||||
}
|
||||
|
||||
effectiveDate := effectiveTransferDate(transfer)
|
||||
if effectiveDate.IsZero() {
|
||||
physicalMoveDate, economicCutoffDate := transferRecordingWindow(transfer)
|
||||
if physicalMoveDate.IsZero() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if recordDate.Before(effectiveDate) {
|
||||
if recordDate.Before(physicalMoveDate) {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
fmt.Sprintf("Recording kandang laying hanya bisa dimulai pada %s. Sebelumnya gunakan kandang growing", effectiveDate.Format("2006-01-02")),
|
||||
fmt.Sprintf("Recording kandang laying hanya bisa dimulai pada %s (tanggal pindah fisik). Sebelumnya gunakan kandang growing", physicalMoveDate.Format("2006-01-02")),
|
||||
)
|
||||
}
|
||||
|
||||
if transfer.ExecutedAt == nil || transfer.ExecutedAt.IsZero() {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
fmt.Sprintf("Transfer laying %s sudah efektif pada %s tetapi belum dieksekusi. Eksekusi transfer terlebih dahulu", transfer.TransferNumber, effectiveDate.Format("2006-01-02")),
|
||||
fmt.Sprintf("Transfer laying %s dengan tanggal pindah fisik %s belum dieksekusi. Eksekusi transfer terlebih dahulu", transfer.TransferNumber, physicalMoveDate.Format("2006-01-02")),
|
||||
)
|
||||
}
|
||||
|
||||
if recordDate.Before(economicCutoffDate) && payload.StockCount > 0 {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
fmt.Sprintf(
|
||||
"Periode transisi transfer laying %s (%s s.d. %s): input PAKAN/OVK harus dicatat di kandang growing hingga %s. Recording kandang laying pada periode ini hanya untuk deplesi (dan telur bila ada).",
|
||||
transfer.TransferNumber,
|
||||
physicalMoveDate.Format("2006-01-02"),
|
||||
economicCutoffDate.AddDate(0, 0, -1).Format("2006-01-02"),
|
||||
economicCutoffDate.AddDate(0, 0, -1).Format("2006-01-02"),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -957,22 +989,38 @@ func (s *recordingService) enforceTransferRecordingRoute(
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi transfer laying")
|
||||
}
|
||||
|
||||
if transfer != nil && transfer.ExecutedAt != nil && !transfer.ExecutedAt.IsZero() {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
"Project flock kandang sudah dipindahkan ke laying",
|
||||
)
|
||||
}
|
||||
|
||||
effectiveDate := effectiveTransferDate(transfer)
|
||||
if effectiveDate.IsZero() {
|
||||
physicalMoveDate, economicCutoffDate := transferRecordingWindow(transfer)
|
||||
if physicalMoveDate.IsZero() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !recordDate.Before(effectiveDate) {
|
||||
if recordDate.Before(physicalMoveDate) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if transfer.ExecutedAt == nil || transfer.ExecutedAt.IsZero() {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
fmt.Sprintf("Recording kandang growing hanya diperbolehkan sampai %s. Gunakan kandang laying mulai %s", effectiveDate.AddDate(0, 0, -1).Format("2006-01-02"), effectiveDate.Format("2006-01-02")),
|
||||
fmt.Sprintf("Transfer laying %s sudah memasuki tanggal pindah fisik %s namun belum dieksekusi. Eksekusi transfer lalu lakukan recording transisi sesuai aturan", transfer.TransferNumber, physicalMoveDate.Format("2006-01-02")),
|
||||
)
|
||||
}
|
||||
|
||||
if !recordDate.Before(economicCutoffDate) {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
fmt.Sprintf("Recording kandang growing hanya diperbolehkan sampai %s. Gunakan kandang laying mulai %s", economicCutoffDate.AddDate(0, 0, -1).Format("2006-01-02"), economicCutoffDate.Format("2006-01-02")),
|
||||
)
|
||||
}
|
||||
|
||||
if payload.DepletionCount > 0 {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
fmt.Sprintf(
|
||||
"Periode transisi transfer laying %s (%s s.d. %s): deplesi harus dicatat di kandang laying tujuan agar mapping tidak ambigu. Kandang growing pada periode ini hanya untuk PAKAN/OVK.",
|
||||
transfer.TransferNumber,
|
||||
physicalMoveDate.Format("2006-01-02"),
|
||||
economicCutoffDate.AddDate(0, 0, -1).Format("2006-01-02"),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -980,19 +1028,93 @@ func (s *recordingService) enforceTransferRecordingRoute(
|
||||
return nil
|
||||
}
|
||||
|
||||
func effectiveTransferDate(transfer *entity.LayingTransfer) time.Time {
|
||||
type recordingRoutePayload struct {
|
||||
StockCount int
|
||||
DepletionCount int
|
||||
EggCount int
|
||||
}
|
||||
|
||||
func buildRecordingRoutePayloadFromCreate(req *validation.Create) recordingRoutePayload {
|
||||
payload := recordingRoutePayload{}
|
||||
if req == nil {
|
||||
return payload
|
||||
}
|
||||
for _, stock := range req.Stocks {
|
||||
if stock.Qty > 0 {
|
||||
payload.StockCount++
|
||||
}
|
||||
}
|
||||
for _, depletion := range req.Depletions {
|
||||
if depletion.Qty > 0 {
|
||||
payload.DepletionCount++
|
||||
}
|
||||
}
|
||||
for _, egg := range req.Eggs {
|
||||
if egg.Qty > 0 {
|
||||
payload.EggCount++
|
||||
}
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func buildRecordingRoutePayloadFromUpdate(req *validation.Update) recordingRoutePayload {
|
||||
payload := recordingRoutePayload{}
|
||||
if req == nil {
|
||||
return payload
|
||||
}
|
||||
for _, stock := range req.Stocks {
|
||||
if stock.Qty > 0 {
|
||||
payload.StockCount++
|
||||
}
|
||||
}
|
||||
for _, depletion := range req.Depletions {
|
||||
if depletion.Qty > 0 {
|
||||
payload.DepletionCount++
|
||||
}
|
||||
}
|
||||
for _, egg := range req.Eggs {
|
||||
if egg.Qty > 0 {
|
||||
payload.EggCount++
|
||||
}
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func transferPhysicalMoveDate(transfer *entity.LayingTransfer) time.Time {
|
||||
if transfer == nil {
|
||||
return time.Time{}
|
||||
}
|
||||
if transfer.EffectiveMoveDate != nil && !transfer.EffectiveMoveDate.IsZero() {
|
||||
return normalizeDateOnlyUTC(*transfer.EffectiveMoveDate)
|
||||
}
|
||||
if !transfer.TransferDate.IsZero() {
|
||||
return normalizeDateOnlyUTC(transfer.TransferDate)
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func transferEconomicCutoffDate(transfer *entity.LayingTransfer) time.Time {
|
||||
if transfer == nil {
|
||||
return time.Time{}
|
||||
}
|
||||
if transfer.EconomicCutoffDate != nil && !transfer.EconomicCutoffDate.IsZero() {
|
||||
return normalizeDateOnlyUTC(*transfer.EconomicCutoffDate)
|
||||
}
|
||||
if transfer.EffectiveMoveDate != nil && !transfer.EffectiveMoveDate.IsZero() {
|
||||
return normalizeDateOnlyUTC(*transfer.EffectiveMoveDate)
|
||||
}
|
||||
return transferPhysicalMoveDate(transfer)
|
||||
}
|
||||
|
||||
func transferRecordingWindow(transfer *entity.LayingTransfer) (time.Time, time.Time) {
|
||||
physicalMoveDate := transferPhysicalMoveDate(transfer)
|
||||
economicCutoffDate := transferEconomicCutoffDate(transfer)
|
||||
if economicCutoffDate.IsZero() {
|
||||
economicCutoffDate = physicalMoveDate
|
||||
}
|
||||
if !physicalMoveDate.IsZero() && economicCutoffDate.Before(physicalMoveDate) {
|
||||
economicCutoffDate = physicalMoveDate
|
||||
}
|
||||
return physicalMoveDate, economicCutoffDate
|
||||
}
|
||||
|
||||
func normalizeDateOnlyUTC(value time.Time) time.Time {
|
||||
return time.Date(value.UTC().Year(), value.UTC().Month(), value.UTC().Day(), 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
)
|
||||
|
||||
func mustDate(t *testing.T, value string) time.Time {
|
||||
t.Helper()
|
||||
parsed, err := time.Parse("2006-01-02", value)
|
||||
if err != nil {
|
||||
t.Fatalf("failed parsing date %s: %v", value, err)
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func TestTransferRecordingWindow(t *testing.T) {
|
||||
t.Run("early transfer keeps transition until economic cutoff", func(t *testing.T) {
|
||||
physical := mustDate(t, "2026-04-08")
|
||||
cutoff := mustDate(t, "2026-05-13")
|
||||
transfer := &entity.LayingTransfer{
|
||||
TransferDate: physical,
|
||||
EconomicCutoffDate: &cutoff,
|
||||
}
|
||||
|
||||
gotPhysical, gotCutoff := transferRecordingWindow(transfer)
|
||||
if gotPhysical.Format("2006-01-02") != "2026-04-08" {
|
||||
t.Fatalf("unexpected physical date: %s", gotPhysical.Format("2006-01-02"))
|
||||
}
|
||||
if gotCutoff.Format("2006-01-02") != "2026-05-13" {
|
||||
t.Fatalf("unexpected cutoff date: %s", gotCutoff.Format("2006-01-02"))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("standard transfer has no transition window", func(t *testing.T) {
|
||||
physical := mustDate(t, "2026-05-13")
|
||||
cutoff := mustDate(t, "2026-05-13")
|
||||
transfer := &entity.LayingTransfer{
|
||||
TransferDate: physical,
|
||||
EconomicCutoffDate: &cutoff,
|
||||
}
|
||||
|
||||
gotPhysical, gotCutoff := transferRecordingWindow(transfer)
|
||||
if gotPhysical.Format("2006-01-02") != "2026-05-13" {
|
||||
t.Fatalf("unexpected physical date: %s", gotPhysical.Format("2006-01-02"))
|
||||
}
|
||||
if gotCutoff.Format("2006-01-02") != "2026-05-13" {
|
||||
t.Fatalf("unexpected cutoff date: %s", gotCutoff.Format("2006-01-02"))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("late transfer clamps economic cutoff to physical move", func(t *testing.T) {
|
||||
physical := mustDate(t, "2026-06-03")
|
||||
cutoff := mustDate(t, "2026-05-13")
|
||||
transfer := &entity.LayingTransfer{
|
||||
TransferDate: physical,
|
||||
EconomicCutoffDate: &cutoff,
|
||||
}
|
||||
|
||||
gotPhysical, gotCutoff := transferRecordingWindow(transfer)
|
||||
if gotPhysical.Format("2006-01-02") != "2026-06-03" {
|
||||
t.Fatalf("unexpected physical date: %s", gotPhysical.Format("2006-01-02"))
|
||||
}
|
||||
if gotCutoff.Format("2006-01-02") != "2026-06-03" {
|
||||
t.Fatalf("unexpected cutoff date: %s", gotCutoff.Format("2006-01-02"))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("legacy data falls back to effective move date", func(t *testing.T) {
|
||||
physical := mustDate(t, "2026-04-08")
|
||||
legacyEffective := mustDate(t, "2026-05-13")
|
||||
transfer := &entity.LayingTransfer{
|
||||
TransferDate: physical,
|
||||
EffectiveMoveDate: &legacyEffective,
|
||||
}
|
||||
|
||||
gotPhysical, gotCutoff := transferRecordingWindow(transfer)
|
||||
if gotPhysical.Format("2006-01-02") != "2026-04-08" {
|
||||
t.Fatalf("unexpected physical date: %s", gotPhysical.Format("2006-01-02"))
|
||||
}
|
||||
if gotCutoff.Format("2006-01-02") != "2026-05-13" {
|
||||
t.Fatalf("unexpected cutoff date: %s", gotCutoff.Format("2006-01-02"))
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user