mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
969 lines
33 KiB
Go
969 lines
33 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
|
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
|
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
|
KandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
|
rProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/repositories"
|
|
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/validations"
|
|
rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
|
rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
|
|
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ChickinService interface {
|
|
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectChickin, int64, error)
|
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectChickin, error)
|
|
CreateOne(ctx *fiber.Ctx, req *validation.Create) ([]entity.ProjectChickin, error)
|
|
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error)
|
|
DeleteOne(ctx *fiber.Ctx, id uint) error
|
|
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectChickin, error)
|
|
EnsureChickInExists(ctx context.Context, projectFlockKandangID uint) error
|
|
}
|
|
|
|
type chickinService struct {
|
|
Log *logrus.Logger
|
|
Validate *validator.Validate
|
|
Repository repository.ProjectChickinRepository
|
|
KandangRepo KandangRepo.KandangRepository
|
|
WarehouseRepo rWarehouse.WarehouseRepository
|
|
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
|
ProductRepo rProduct.ProductRepository
|
|
ProjectFlockRepo rProjectFlock.ProjectflockRepository
|
|
ProjectflockKandangRepo rProjectFlock.ProjectFlockKandangRepository
|
|
ProjectflockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository
|
|
ProjectChickinDetailRepo repository.ProjectChickinDetailRepository
|
|
TransferLayingRepo rTransferLaying.TransferLayingRepository
|
|
FifoStockV2Svc commonSvc.FifoStockV2Service
|
|
StockLogRepo rStockLogs.StockLogRepository
|
|
}
|
|
|
|
func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, productRepo rProduct.ProductRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, projectflockkandangRepo rProjectFlock.ProjectFlockKandangRepository, projectflockpopulationRepo rProjectFlock.ProjectFlockPopulationRepository, projectChickinDetailRepo repository.ProjectChickinDetailRepository, transferLayingRepo rTransferLaying.TransferLayingRepository, validate *validator.Validate, fifoStockV2Svc commonSvc.FifoStockV2Service) ChickinService {
|
|
return &chickinService{
|
|
Log: utils.Log,
|
|
Validate: validate,
|
|
Repository: repo,
|
|
KandangRepo: kandangRepo,
|
|
WarehouseRepo: warehouseRepo,
|
|
ProductWarehouseRepo: productWarehouseRepo,
|
|
ProductRepo: productRepo,
|
|
ProjectFlockRepo: projectFlockRepo,
|
|
ProjectflockKandangRepo: projectflockkandangRepo,
|
|
ProjectflockPopulationRepo: projectflockpopulationRepo,
|
|
ProjectChickinDetailRepo: projectChickinDetailRepo,
|
|
TransferLayingRepo: transferLayingRepo,
|
|
FifoStockV2Svc: fifoStockV2Svc,
|
|
StockLogRepo: rStockLogs.NewStockLogRepository(repo.DB()),
|
|
}
|
|
}
|
|
|
|
func (s chickinService) withRelations(db *gorm.DB) *gorm.DB {
|
|
return db.
|
|
Preload("CreatedUser").
|
|
Preload("ProjectFlockKandang.Kandang").
|
|
Preload("ProjectFlockKandang.Kandang.Location").
|
|
Preload("ProjectFlockKandang.Kandang.Location.Area").
|
|
Preload("ProjectFlockKandang.Kandang.Pic").
|
|
Preload("ProjectFlockKandang.ProjectFlock").
|
|
Preload("ProjectFlockKandang.ProjectFlock.Area").
|
|
Preload("ProjectFlockKandang.ProjectFlock.ProductionStandard.ProductionStandardDetails").
|
|
Preload("ProjectFlockKandang.ProjectFlock.Location").
|
|
Preload("ProjectFlockKandang.ProjectFlock.Location.Area")
|
|
|
|
}
|
|
|
|
func (s chickinService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectChickin, int64, error) {
|
|
if err := s.Validate.Struct(params); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (params.Page - 1) * params.Limit
|
|
|
|
chickins, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
|
db = s.withRelations(db)
|
|
if params.ProjectFlockKandangId != 0 {
|
|
return db.Where("project_flock_kandang_id = ?", params.ProjectFlockKandangId)
|
|
}
|
|
return db.Order("created_at DESC").Order("updated_at DESC")
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return chickins, total, nil
|
|
}
|
|
|
|
func (s chickinService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectChickin, error) {
|
|
|
|
chickin, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return chickin, nil
|
|
}
|
|
|
|
func (s chickinService) ensureNotTransferred(ctx context.Context, projectFlockKandangID uint) error {
|
|
if projectFlockKandangID == 0 || s.TransferLayingRepo == nil {
|
|
return nil
|
|
}
|
|
|
|
transfer, err := s.TransferLayingRepo.GetLatestApprovedBySourceKandang(ctx, projectFlockKandangID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil
|
|
}
|
|
s.Log.Errorf("Failed to resolve transfer laying by source kandang %d: %+v", projectFlockKandangID, err)
|
|
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")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]entity.ProjectChickin, error) {
|
|
if err := s.Validate.Struct(req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := s.ensureNotTransferred(c.Context(), req.ProjectFlockKandangId); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
projectFlockKandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), req.ProjectFlockKandangId)
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock Kandang not found")
|
|
}
|
|
|
|
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectFlockKandang.KandangId)
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse for Kandang not found")
|
|
}
|
|
|
|
actorID, err := m.ActorIDFromContext(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
newChikins := make([]*entity.ProjectChickin, 0)
|
|
chickinQtyMap := make(map[uint]float64)
|
|
|
|
for idx, chickinReq := range req.ChickinRequests {
|
|
|
|
productWarehouse, err := s.ProductWarehouseRepo.GetByID(c.Context(), chickinReq.ProductWarehouseId, func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("Product.Flags")
|
|
})
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Product warehouse %d not found", chickinReq.ProductWarehouseId))
|
|
}
|
|
|
|
if productWarehouse.WarehouseId != warehouse.Id {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Product warehouse %d is not bound to kandang's warehouse", chickinReq.ProductWarehouseId))
|
|
}
|
|
|
|
if productWarehouse.ProjectFlockKandangId != nil && *productWarehouse.ProjectFlockKandangId != req.ProjectFlockKandangId {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Product warehouse %d belongs to different flock. Only product warehouses with project_flock_kandang_id = NULL or = %d can be used", chickinReq.ProductWarehouseId, req.ProjectFlockKandangId))
|
|
}
|
|
|
|
if productWarehouse.Product.Id != 0 {
|
|
|
|
var requiredFlag utils.FlagType
|
|
if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryGrowing) {
|
|
requiredFlag = utils.FlagDOC
|
|
} else if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryLaying) {
|
|
requiredFlag = utils.FlagPullet
|
|
} else {
|
|
return nil, fmt.Errorf("invalid flock category for chickin")
|
|
}
|
|
|
|
hasRequiredFlag := false
|
|
for _, flag := range productWarehouse.Product.Flags {
|
|
if utils.FlagType(flag.Name) == requiredFlag {
|
|
hasRequiredFlag = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !hasRequiredFlag {
|
|
return nil, fmt.Errorf("product warehouse %d cannot be used for %s chickin. Product must have %s flag (product ID: %d, warehouse ID: %d)", chickinReq.ProductWarehouseId, projectFlockKandang.ProjectFlock.Category, requiredFlag, productWarehouse.Product.Id, productWarehouse.Id)
|
|
}
|
|
}
|
|
|
|
chickinDate, err := utils.ParseDateString(chickinReq.ChickInDate)
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid ChickInDate format for product warehouse %d", chickinReq.ProductWarehouseId))
|
|
}
|
|
|
|
newChickin := &entity.ProjectChickin{
|
|
ProjectFlockKandangId: req.ProjectFlockKandangId,
|
|
ChickInDate: chickinDate,
|
|
UsageQty: 0,
|
|
PendingUsageQty: 0,
|
|
ProductWarehouseId: chickinReq.ProductWarehouseId,
|
|
Notes: chickinReq.Note,
|
|
CreatedBy: actorID,
|
|
}
|
|
|
|
newChikins = append(newChikins, newChickin)
|
|
|
|
totalPopulationQty, err := s.ProjectflockPopulationRepo.GetTotalQtyByProductWarehouseID(c.Context(), chickinReq.ProductWarehouseId)
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get total population quantity for product warehouse %d", chickinReq.ProductWarehouseId))
|
|
}
|
|
|
|
availableQty := productWarehouse.Quantity - totalPopulationQty
|
|
if availableQty < 0 {
|
|
availableQty = 0
|
|
}
|
|
|
|
chickinQtyMap[uint(idx)] = availableQty
|
|
}
|
|
|
|
if len(newChikins) == 0 {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, "No chickins to create")
|
|
}
|
|
|
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
|
|
|
repositoryTx := repository.NewChickinRepository(dbTransaction)
|
|
existingChikins, err := repositoryTx.GetByProjectFlockKandangIDForUpdate(c.Context(), req.ProjectFlockKandangId)
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check existing chickins")
|
|
}
|
|
|
|
isFirstTime := len(existingChikins) == 0
|
|
|
|
pendingQtyMap := make(map[uint]float64)
|
|
for _, existingChickin := range existingChikins {
|
|
if existingChickin.PendingUsageQty > 0 {
|
|
pendingQtyMap[existingChickin.ProductWarehouseId] += existingChickin.PendingUsageQty
|
|
}
|
|
}
|
|
for idx, chickin := range newChikins {
|
|
pendingQty := pendingQtyMap[chickin.ProductWarehouseId]
|
|
desiredQty := chickinQtyMap[uint(idx)]
|
|
|
|
availableQty := desiredQty - pendingQty
|
|
if availableQty < 0 {
|
|
availableQty = 0
|
|
}
|
|
|
|
if availableQty <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No available stock in product warehouse %d for chickin. Warehouse: %.0f, Pending: %.0f, Available: %.0f", chickin.ProductWarehouseId, desiredQty, pendingQty, availableQty))
|
|
}
|
|
|
|
chickinQtyMap[uint(idx)] = availableQty
|
|
|
|
pendingQtyMap[chickin.ProductWarehouseId] += availableQty
|
|
}
|
|
|
|
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
|
|
|
if err := s.Repository.WithTx(dbTransaction).CreateMany(c.Context(), newChikins, nil); err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create chickins")
|
|
}
|
|
|
|
for idx, chickin := range newChikins {
|
|
desiredQty := chickinQtyMap[uint(idx)]
|
|
if err := s.StageChickinStocks(c.Context(), dbTransaction, chickin, desiredQty, actorID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
latest, err := approvalSvcTx.LatestByTarget(c.Context(), utils.ApprovalWorkflowChickin, projectFlockKandang.Id, nil)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get latest approval")
|
|
}
|
|
|
|
var approvalAction entity.ApprovalAction
|
|
if isFirstTime {
|
|
approvalAction = entity.ApprovalActionCreated
|
|
} else {
|
|
approvalAction = entity.ApprovalActionUpdated
|
|
}
|
|
|
|
if latest == nil {
|
|
if _, err := approvalSvcTx.CreateApproval(
|
|
c.Context(),
|
|
utils.ApprovalWorkflowChickin,
|
|
projectFlockKandang.Id,
|
|
utils.ChickinStepPengajuan,
|
|
&approvalAction,
|
|
actorID,
|
|
nil); err != nil {
|
|
if !errors.Is(err, gorm.ErrDuplicatedKey) {
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval")
|
|
}
|
|
}
|
|
} else if latest.StepNumber != uint16(utils.ChickinStepPengajuan) {
|
|
if _, err := approvalSvcTx.CreateApproval(
|
|
c.Context(),
|
|
utils.ApprovalWorkflowChickin,
|
|
projectFlockKandang.Id,
|
|
utils.ChickinStepPengajuan,
|
|
&approvalAction,
|
|
actorID,
|
|
nil); err != nil {
|
|
if !errors.Is(err, gorm.ErrDuplicatedKey) {
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval")
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
if fiberErr, ok := err.(*fiber.Error); ok {
|
|
return nil, fiberErr
|
|
}
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create chickins")
|
|
}
|
|
|
|
result := make([]entity.ProjectChickin, 0, len(newChikins))
|
|
for _, chickin := range newChikins {
|
|
loaded, err := s.GetOne(c, chickin.Id)
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to reload chickin %d with relations: %v", chickin.Id, err))
|
|
}
|
|
result = append(result, *loaded)
|
|
}
|
|
if len(result) == 0 {
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to load created chickins")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error) {
|
|
if err := s.Validate.Struct(req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
chickin, err := s.Repository.GetByID(c.Context(), id, nil)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
|
}
|
|
return nil, err
|
|
}
|
|
if err := s.ensureNotTransferred(c.Context(), chickin.ProjectFlockKandangId); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
updateBody := make(map[string]any)
|
|
|
|
if req.ChickInDate != "" {
|
|
updateBody["chick_in_date"] = req.ChickInDate
|
|
}
|
|
if req.Note != "" {
|
|
updateBody["notes"] = req.Note
|
|
}
|
|
if len(updateBody) == 0 {
|
|
return s.GetOne(c, id)
|
|
}
|
|
|
|
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
updated, err := s.GetOne(c, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if updated.UsageQty > 0 {
|
|
if err := s.syncChickinTraceForProductWarehouse(c.Context(), nil, updated.ProductWarehouseId); err != nil {
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to sync chickin stock trace")
|
|
}
|
|
}
|
|
|
|
return updated, nil
|
|
}
|
|
|
|
func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error {
|
|
|
|
chickin, err := s.Repository.GetByID(c.Context(), id, nil)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
|
}
|
|
return err
|
|
}
|
|
|
|
if err := s.ensureNotTransferred(c.Context(), chickin.ProjectFlockKandangId); err != nil {
|
|
return err
|
|
}
|
|
|
|
actorID, err := m.ActorIDFromContext(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
|
chickinRepoTx := repository.NewChickinRepository(tx)
|
|
|
|
if chickin.UsageQty > 0 || chickin.PendingUsageQty > 0 {
|
|
if err := s.ReleaseChickinStocks(c.Context(), tx, chickin, actorID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := chickinRepoTx.DeleteOne(c.Context(), id); err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
|
}
|
|
return err
|
|
}
|
|
|
|
if err := s.syncChickinTraceForProductWarehouse(c.Context(), tx, chickin.ProductWarehouseId); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
if fiberErr, ok := err.(*fiber.Error); ok {
|
|
return fiberErr
|
|
}
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.ProjectChickin, error) {
|
|
if err := s.Validate.Struct(req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
actorID, err := m.ActorIDFromContext(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(s.Repository.DB()))
|
|
|
|
var action entity.ApprovalAction
|
|
switch strings.ToUpper(strings.TrimSpace(req.Action)) {
|
|
case string(entity.ApprovalActionRejected):
|
|
action = entity.ApprovalActionRejected
|
|
case string(entity.ApprovalActionApproved):
|
|
action = entity.ApprovalActionApproved
|
|
default:
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, "action must be APPROVED or REJECTED")
|
|
}
|
|
|
|
approvableIDs := utils.UniqueUintSlice(req.ApprovableIds)
|
|
if len(approvableIDs) == 0 {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, "approvable_ids must contain at least one id")
|
|
}
|
|
|
|
for _, id := range approvableIDs {
|
|
|
|
if err := commonSvc.EnsureRelations(c.Context(), commonSvc.RelationCheck{Name: "ProjectFlockKandang", ID: &id, Exists: s.ProjectflockKandangRepo.IdExists}); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := s.ensureNotTransferred(c.Context(), id); err != nil {
|
|
return nil, err
|
|
}
|
|
latestApproval, err := approvalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowChickin, id, nil)
|
|
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
|
|
}
|
|
if latestApproval == nil {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No approval found for ProjectFlockKandang %d - chickins must be created first", id))
|
|
}
|
|
if latestApproval.StepNumber != uint16(utils.ChickinStepPengajuan) {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("ProjectFlockKandang %d cannot be approved - current status is not in PENGAJUAN stage", id))
|
|
}
|
|
}
|
|
|
|
step := utils.ChickinStepPengajuan
|
|
if action == entity.ApprovalActionApproved {
|
|
step = utils.ChickinStepDisetujui
|
|
}
|
|
|
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
|
|
|
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
|
chickinRepoTx := repository.NewChickinRepository(dbTransaction)
|
|
ProjectFlockPopulationRepotx := s.ProjectflockPopulationRepo.WithTx(dbTransaction)
|
|
touchedProductWarehouseIDs := make(map[uint]struct{})
|
|
|
|
for _, approvableID := range approvableIDs {
|
|
if _, err := approvalSvc.CreateApproval(
|
|
c.Context(),
|
|
utils.ApprovalWorkflowChickin,
|
|
approvableID,
|
|
step,
|
|
&action,
|
|
actorID,
|
|
req.Notes,
|
|
); err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval")
|
|
}
|
|
|
|
if action == entity.ApprovalActionApproved {
|
|
chickins, err := chickinRepoTx.GetByProjectFlockKandangID(c.Context(), approvableID)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get chickins for approval %d", approvableID))
|
|
}
|
|
|
|
kandangForApproval, err := s.ProjectflockKandangRepo.GetByID(c.Context(), approvableID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("ProjectFlockKandang %d not found", approvableID))
|
|
}
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get ProjectFlockKandang")
|
|
}
|
|
|
|
category := strings.ToUpper(strings.TrimSpace(kandangForApproval.ProjectFlock.Category))
|
|
|
|
var targetFlag utils.FlagType
|
|
if category == string(utils.ProjectFlockCategoryGrowing) {
|
|
targetFlag = utils.FlagPullet
|
|
} else if category == string(utils.ProjectFlockCategoryLaying) {
|
|
targetFlag = utils.FlagLayer
|
|
} else {
|
|
continue
|
|
}
|
|
|
|
for _, chickin := range chickins {
|
|
approvedQty := chickin.UsageQty
|
|
if approvedQty <= 0 {
|
|
approvedQty = chickin.PendingUsageQty
|
|
}
|
|
if approvedQty < 0 {
|
|
approvedQty = 0
|
|
}
|
|
|
|
if err := s.ConsumeChickinStocks(c.Context(), dbTransaction, &chickin, approvedQty, actorID); err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to finalize usage qty for chickin %d", chickin.Id))
|
|
}
|
|
chickin.UsageQty = approvedQty
|
|
chickin.PendingUsageQty = 0
|
|
touchedProductWarehouseIDs[chickin.ProductWarehouseId] = struct{}{}
|
|
|
|
populationExists, err := ProjectFlockPopulationRepotx.ExistsByProjectChickinID(c.Context(), chickin.Id)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to check population for chickin %d", chickin.Id))
|
|
}
|
|
if populationExists {
|
|
continue
|
|
}
|
|
|
|
sourcePW, err := s.ProductWarehouseRepo.GetByID(c.Context(), chickin.ProductWarehouseId, func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("Product.Flags")
|
|
})
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get product warehouse for chickin %d", chickin.Id))
|
|
}
|
|
|
|
if err := s.autoAddFlagToProduct(c.Context(), dbTransaction, sourcePW.Product.Id, targetFlag); err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to auto-add flag to product %d", sourcePW.Product.Id))
|
|
}
|
|
|
|
population := &entity.ProjectFlockPopulation{
|
|
ProjectChickinId: chickin.Id,
|
|
ProductWarehouseId: sourcePW.Id,
|
|
TotalQty: 0,
|
|
TotalUsedQty: 0,
|
|
Notes: chickin.Notes,
|
|
CreatedBy: actorID,
|
|
}
|
|
if err := ProjectFlockPopulationRepotx.CreateOne(c.Context(), population, nil); err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to create population for chickin %d", chickin.Id))
|
|
}
|
|
|
|
if err := s.ReplenishChickinStocks(c.Context(), dbTransaction, &chickin, sourcePW, population, actorID); err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to replenish stock for chickin %d", chickin.Id))
|
|
}
|
|
}
|
|
}
|
|
if action == entity.ApprovalActionRejected {
|
|
chickins, err := chickinRepoTx.GetByProjectFlockKandangID(c.Context(), approvableID)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get pending chickins for rejection %d", approvableID))
|
|
}
|
|
|
|
if len(chickins) == 0 {
|
|
continue
|
|
}
|
|
|
|
for _, chickin := range chickins {
|
|
populationExists, err := ProjectFlockPopulationRepotx.ExistsByProjectChickinID(c.Context(), chickin.Id)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to check population for chickin %d", chickin.Id))
|
|
}
|
|
if populationExists {
|
|
continue
|
|
}
|
|
|
|
if chickin.UsageQty <= 0 && chickin.PendingUsageQty <= 0 {
|
|
continue
|
|
}
|
|
|
|
if err := s.ReleaseChickinStocks(c.Context(), dbTransaction, &chickin, actorID); err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to release stock for rejected chickin %d: %v", chickin.Id, err))
|
|
}
|
|
touchedProductWarehouseIDs[chickin.ProductWarehouseId] = struct{}{}
|
|
|
|
if err := chickinRepoTx.DeleteOne(c.Context(), chickin.Id); err != nil {
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to delete rejected chickin %d", chickin.Id))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for productWarehouseID := range touchedProductWarehouseIDs {
|
|
if err := s.syncChickinTraceForProductWarehouse(c.Context(), dbTransaction, productWarehouseID); err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to sync chickin trace for product warehouse %d", productWarehouseID))
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
if fiberErr, ok := err.(*fiber.Error); ok {
|
|
return nil, fiberErr
|
|
}
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to record approval")
|
|
}
|
|
|
|
updated := make([]entity.ProjectChickin, 0)
|
|
for _, kandangID := range approvableIDs {
|
|
var chickins []entity.ProjectChickin
|
|
if err := s.Repository.DB().WithContext(c.Context()).Where("project_flock_kandang_id = ?", kandangID).Find(&chickins).Error; err != nil {
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to load approved chickins")
|
|
}
|
|
updated = append(updated, chickins...)
|
|
}
|
|
|
|
return updated, nil
|
|
}
|
|
|
|
func (s *chickinService) autoAddFlagToProduct(ctx context.Context, tx *gorm.DB, productID uint, targetFlag utils.FlagType) error {
|
|
if s.ProductRepo == nil {
|
|
return nil
|
|
}
|
|
|
|
currentFlags, err := s.ProductRepo.GetFlags(ctx, productID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get product flags: %w", err)
|
|
}
|
|
|
|
hasTargetFlag := false
|
|
currentFlagNames := make([]string, 0, len(currentFlags))
|
|
for _, flag := range currentFlags {
|
|
currentFlagNames = append(currentFlagNames, flag.Name)
|
|
if flag.Name == string(targetFlag) {
|
|
hasTargetFlag = true
|
|
}
|
|
}
|
|
|
|
if hasTargetFlag {
|
|
return nil
|
|
}
|
|
|
|
newFlags := append(currentFlagNames, string(targetFlag))
|
|
if err := s.ProductRepo.SyncFlags(ctx, tx, productID, newFlags); err != nil {
|
|
return fmt.Errorf("failed to sync flags: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *chickinService) ConsumeChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, desiredQty float64, actorID uint) error {
|
|
if chickin == nil {
|
|
return nil
|
|
}
|
|
if tx == nil {
|
|
return errors.New("transaction is required")
|
|
}
|
|
if desiredQty < 0 {
|
|
return errors.New("desired quantity must be zero or greater")
|
|
}
|
|
|
|
return s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, desiredQty, 0)
|
|
}
|
|
|
|
func (s *chickinService) StageChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, desiredQty float64, actorID uint) error {
|
|
if chickin == nil {
|
|
return nil
|
|
}
|
|
if tx == nil {
|
|
return errors.New("transaction is required")
|
|
}
|
|
if desiredQty < 0 {
|
|
return errors.New("desired quantity must be zero or greater")
|
|
}
|
|
|
|
return s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, 0, desiredQty)
|
|
}
|
|
|
|
func (s *chickinService) ReplenishChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, targetPW *entity.ProductWarehouse, population *entity.ProjectFlockPopulation, actorID uint) error {
|
|
if chickin == nil || targetPW == nil || population == nil {
|
|
return nil
|
|
}
|
|
if tx == nil {
|
|
return errors.New("transaction is required")
|
|
}
|
|
if s.FifoStockV2Svc == nil {
|
|
return errors.New("fifo v2 service is not available")
|
|
}
|
|
|
|
if err := tx.WithContext(ctx).
|
|
Model(&entity.ProjectFlockPopulation{}).
|
|
Where("id = ?", population.Id).
|
|
Update("total_qty", chickin.UsageQty).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
asOf := chickin.ChickInDate
|
|
if asOf.IsZero() {
|
|
asOf = chickin.CreatedAt
|
|
}
|
|
return reflowChickinScope(ctx, s.FifoStockV2Svc, tx, targetPW.Id, &asOf)
|
|
}
|
|
|
|
func (s *chickinService) ReleaseChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, actorID uint) error {
|
|
if chickin == nil {
|
|
return nil
|
|
}
|
|
if tx == nil {
|
|
return errors.New("transaction is required")
|
|
}
|
|
|
|
if err := s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, 0, 0); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *chickinService) syncChickinTraceForProductWarehouse(ctx context.Context, tx *gorm.DB, productWarehouseID uint) error {
|
|
if productWarehouseID == 0 {
|
|
return nil
|
|
}
|
|
if s.FifoStockV2Svc == nil {
|
|
return nil
|
|
}
|
|
|
|
if tx == nil {
|
|
return s.Repository.DB().WithContext(ctx).Transaction(func(innerTx *gorm.DB) error {
|
|
return s.syncChickinTraceForProductWarehouse(ctx, innerTx, productWarehouseID)
|
|
})
|
|
}
|
|
|
|
flagGroupCode, err := s.resolveFlagGroupByProductWarehouse(ctx, tx, productWarehouseID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if strings.TrimSpace(flagGroupCode) == "" {
|
|
return nil
|
|
}
|
|
|
|
now := time.Now()
|
|
if err := tx.WithContext(ctx).
|
|
Table("stock_allocations").
|
|
Where("product_warehouse_id = ?", productWarehouseID).
|
|
Where("usable_type = ?", fifo.UsableKeyProjectChickin.String()).
|
|
Where("allocation_purpose = ?", entity.StockAllocationPurposeTraceChickin).
|
|
Where("status = ?", entity.StockAllocationStatusActive).
|
|
Updates(map[string]any{
|
|
"status": entity.StockAllocationStatusReleased,
|
|
"released_at": now,
|
|
"updated_at": now,
|
|
"note": "chickin_trace_reflow_reset",
|
|
}).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
type chickinTraceRow struct {
|
|
ID uint `gorm:"column:id"`
|
|
UsageQty float64 `gorm:"column:usage_qty"`
|
|
ChickIn time.Time `gorm:"column:chick_in_date"`
|
|
}
|
|
chickins := make([]chickinTraceRow, 0)
|
|
if err := tx.WithContext(ctx).
|
|
Table("project_chickins").
|
|
Select("id, usage_qty, chick_in_date").
|
|
Where("product_warehouse_id = ?", productWarehouseID).
|
|
Where("deleted_at IS NULL").
|
|
Where("usage_qty > 0").
|
|
Order("chick_in_date ASC, id ASC").
|
|
Scan(&chickins).Error; err != nil {
|
|
return err
|
|
}
|
|
if len(chickins) == 0 {
|
|
return nil
|
|
}
|
|
|
|
gatherRows, err := s.FifoStockV2Svc.Gather(ctx, commonSvc.FifoStockV2GatherRequest{
|
|
FlagGroupCode: flagGroupCode,
|
|
Lane: "STOCKABLE",
|
|
ProductWarehouseID: productWarehouseID,
|
|
Limit: 50000,
|
|
Tx: tx,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(gatherRows) == 0 {
|
|
return nil
|
|
}
|
|
|
|
type lotKey struct {
|
|
StockableType string
|
|
StockableID uint
|
|
}
|
|
remainingByLot := make(map[lotKey]float64, len(gatherRows))
|
|
for _, row := range gatherRows {
|
|
key := lotKey{StockableType: row.Ref.LegacyTypeKey, StockableID: row.Ref.ID}
|
|
remainingByLot[key] = row.AvailableQuantity
|
|
}
|
|
|
|
lotIndex := 0
|
|
traceNow := time.Now()
|
|
for _, chickin := range chickins {
|
|
remaining := chickin.UsageQty
|
|
for remaining > 1e-6 && lotIndex < len(gatherRows) {
|
|
lot := gatherRows[lotIndex]
|
|
key := lotKey{StockableType: lot.Ref.LegacyTypeKey, StockableID: lot.Ref.ID}
|
|
available := remainingByLot[key]
|
|
if available <= 1e-6 {
|
|
lotIndex++
|
|
continue
|
|
}
|
|
|
|
portion := math.Min(remaining, available)
|
|
if portion <= 1e-6 {
|
|
lotIndex++
|
|
continue
|
|
}
|
|
|
|
insert := map[string]any{
|
|
"product_warehouse_id": productWarehouseID,
|
|
"stockable_type": lot.Ref.LegacyTypeKey,
|
|
"stockable_id": lot.Ref.ID,
|
|
"usable_type": fifo.UsableKeyProjectChickin.String(),
|
|
"usable_id": chickin.ID,
|
|
"qty": portion,
|
|
"status": entity.StockAllocationStatusActive,
|
|
"allocation_purpose": entity.StockAllocationPurposeTraceChickin,
|
|
"engine_version": "v2",
|
|
"flag_group_code": flagGroupCode,
|
|
"function_code": "CHICKIN_TRACE",
|
|
"created_at": traceNow,
|
|
"updated_at": traceNow,
|
|
}
|
|
if err := tx.WithContext(ctx).Table("stock_allocations").Create(insert).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
remaining -= portion
|
|
remainingByLot[key] = available - portion
|
|
}
|
|
|
|
if remaining > 1e-6 {
|
|
s.Log.Warnf(
|
|
"chickin trace partial allocation for product_warehouse_id=%d chickin_id=%d: remaining=%.3f",
|
|
productWarehouseID,
|
|
chickin.ID,
|
|
remaining,
|
|
)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *chickinService) resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, productWarehouseID uint) (string, error) {
|
|
type row struct {
|
|
FlagGroupCode string `gorm:"column:flag_group_code"`
|
|
}
|
|
|
|
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(`
|
|
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 = ?
|
|
AND fm.flag_group_code = rr.flag_group_code
|
|
)
|
|
`, productWarehouseID, entity.FlagableTypeProduct).
|
|
Order("fg.priority ASC, rr.id ASC").
|
|
Limit(1).
|
|
Take(&selected).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return "", nil
|
|
}
|
|
return "", err
|
|
}
|
|
return selected.FlagGroupCode, nil
|
|
}
|
|
|
|
func (s chickinService) EnsureChickInExists(ctx context.Context, projectFlockKandangID uint) error {
|
|
if projectFlockKandangID == 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
|
|
}
|
|
|
|
populations, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(ctx, projectFlockKandangID)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to check project flock population for project_flock_kandang_id=%d: %+v", projectFlockKandangID, err)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa data chick in")
|
|
}
|
|
|
|
if len(populations) == 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Project flock belum memiliki chick in yang disetujui sehingga belum dapat membuat recording")
|
|
}
|
|
|
|
for _, population := range populations {
|
|
if population.TotalQty > 0 {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "Chick in project flock belum disetujui sehingga belum dapat membuat recording")
|
|
}
|