mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 23:35:43 +00:00
fix: adjusment module depletion, chickin, recording refactor
This commit is contained in:
@@ -605,59 +605,11 @@ func (s *chickinService) ConsumeChickinStocks(ctx context.Context, tx *gorm.DB,
|
||||
if tx == nil {
|
||||
return errors.New("transaction is required")
|
||||
}
|
||||
if s.FifoStockV2Svc == nil {
|
||||
return errors.New("fifo v2 service is not available")
|
||||
}
|
||||
if desiredQty < 0 {
|
||||
return errors.New("desired quantity must be zero or greater")
|
||||
}
|
||||
|
||||
if err := s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, desiredQty, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
asOf := chickin.ChickInDate
|
||||
if asOf.IsZero() {
|
||||
asOf = chickin.CreatedAt
|
||||
}
|
||||
if err := reflowChickinScope(ctx, s.FifoStockV2Svc, tx, chickin.ProductWarehouseId, &asOf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var refreshed entity.ProjectChickin
|
||||
if err := tx.WithContext(ctx).
|
||||
Where("id = ?", chickin.Id).
|
||||
Take(&refreshed).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if refreshed.UsageQty > 0 {
|
||||
decreaseLog := &entity.StockLog{
|
||||
Decrease: refreshed.UsageQty,
|
||||
LoggableType: string(utils.StockLogTypeChikin),
|
||||
LoggableId: refreshed.Id,
|
||||
ProductWarehouseId: refreshed.ProductWarehouseId,
|
||||
CreatedBy: actorID,
|
||||
Notes: fmt.Sprintf("Chickin #%d", refreshed.Id),
|
||||
}
|
||||
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, refreshed.ProductWarehouseId, 1)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get stock logs")
|
||||
}
|
||||
if len(stockLogs) > 0 {
|
||||
latestStockLog := stockLogs[0]
|
||||
decreaseLog.Stock = latestStockLog.Stock
|
||||
decreaseLog.Stock -= decreaseLog.Decrease
|
||||
} else {
|
||||
decreaseLog.Stock -= decreaseLog.Decrease
|
||||
}
|
||||
|
||||
if err := s.StockLogRepo.WithTx(tx).CreateOne(ctx, decreaseLog, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, desiredQty, 0)
|
||||
}
|
||||
|
||||
func (s *chickinService) ReplenishChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, targetPW *entity.ProductWarehouse, population *entity.ProjectFlockPopulation, actorID uint) error {
|
||||
@@ -667,9 +619,6 @@ func (s *chickinService) ReplenishChickinStocks(ctx context.Context, tx *gorm.DB
|
||||
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{}).
|
||||
@@ -678,11 +627,7 @@ func (s *chickinService) ReplenishChickinStocks(ctx context.Context, tx *gorm.DB
|
||||
return err
|
||||
}
|
||||
|
||||
asOf := chickin.ChickInDate
|
||||
if asOf.IsZero() {
|
||||
asOf = chickin.CreatedAt
|
||||
}
|
||||
return reflowChickinScope(ctx, s.FifoStockV2Svc, tx, targetPW.Id, &asOf)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *chickinService) ReleaseChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, actorID uint) error {
|
||||
@@ -692,53 +637,11 @@ func (s *chickinService) ReleaseChickinStocks(ctx context.Context, tx *gorm.DB,
|
||||
if tx == nil {
|
||||
return errors.New("transaction is required")
|
||||
}
|
||||
if s.FifoStockV2Svc == nil {
|
||||
return errors.New("fifo v2 service is not available")
|
||||
}
|
||||
|
||||
var currentUsage float64
|
||||
if err := tx.Model(&entity.ProjectChickin{}).Where("id = ?", chickin.Id).Select("usage_qty").Scan(¤tUsage).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, 0, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
asOf := chickin.ChickInDate
|
||||
if asOf.IsZero() {
|
||||
asOf = chickin.CreatedAt
|
||||
}
|
||||
if err := reflowChickinScope(ctx, s.FifoStockV2Svc, tx, chickin.ProductWarehouseId, &asOf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if currentUsage > 0 {
|
||||
increaseLog := &entity.StockLog{
|
||||
Increase: currentUsage,
|
||||
LoggableType: string(utils.StockLogTypeChikin),
|
||||
LoggableId: chickin.Id,
|
||||
ProductWarehouseId: chickin.ProductWarehouseId,
|
||||
CreatedBy: actorID,
|
||||
Notes: fmt.Sprintf("Chickin #%d - Stock released", chickin.Id),
|
||||
}
|
||||
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, chickin.ProductWarehouseId, 1)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get stock logs")
|
||||
}
|
||||
if len(stockLogs) > 0 {
|
||||
latestStockLog := stockLogs[0]
|
||||
increaseLog.Stock = latestStockLog.Stock
|
||||
increaseLog.Stock += increaseLog.Increase
|
||||
} else {
|
||||
increaseLog.Stock += increaseLog.Increase
|
||||
}
|
||||
|
||||
if err := s.StockLogRepo.WithTx(tx).CreateOne(ctx, increaseLog, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -25,20 +25,27 @@ type RecordingRepository interface {
|
||||
GetLatestByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (*entity.Recording, error)
|
||||
ListByProjectFlockKandangID(ctx context.Context, tx *gorm.DB, projectFlockKandangId uint, from *time.Time) ([]entity.Recording, error)
|
||||
GenerateNextDay(tx *gorm.DB, projectFlockKandangId uint) (int, error)
|
||||
CreateRecording(tx *gorm.DB, recording *entity.Recording) error
|
||||
|
||||
CreateStocks(tx *gorm.DB, stocks []entity.RecordingStock) error
|
||||
CreateStock(tx *gorm.DB, stock *entity.RecordingStock) error
|
||||
DeleteStocks(tx *gorm.DB, recordingID uint) error
|
||||
DeleteStocksByIDs(tx *gorm.DB, ids []uint) error
|
||||
ListStocks(tx *gorm.DB, recordingID uint) ([]entity.RecordingStock, error)
|
||||
GetStockByID(tx *gorm.DB, stockID uint) (*entity.RecordingStock, error)
|
||||
UpdateStockUsage(tx *gorm.DB, stockID uint, usageQty, pendingQty float64) error
|
||||
UpdateDepletionPending(tx *gorm.DB, depletionID uint, pendingQty float64) error
|
||||
UpdateDepletionQuantities(tx *gorm.DB, depletionID uint, qty, usageQty, pendingQty float64) error
|
||||
|
||||
CreateDepletions(tx *gorm.DB, depletions []entity.RecordingDepletion) error
|
||||
DeleteDepletions(tx *gorm.DB, recordingID uint) error
|
||||
ListDepletions(tx *gorm.DB, recordingID uint) ([]entity.RecordingDepletion, error)
|
||||
GetDepletionByID(tx *gorm.DB, depletionID uint) (*entity.RecordingDepletion, error)
|
||||
|
||||
CreateEggs(tx *gorm.DB, eggs []entity.RecordingEgg) error
|
||||
DeleteEggs(tx *gorm.DB, recordingID uint) error
|
||||
ListEggs(tx *gorm.DB, recordingID uint) ([]entity.RecordingEgg, error)
|
||||
UpdateEggTotalQty(tx *gorm.DB, eggID uint, totalQty float64) error
|
||||
GetRecordingEggByID(ctx context.Context, id uint, modifier func(*gorm.DB) *gorm.DB) (*entity.RecordingEgg, error)
|
||||
|
||||
ExistsOnDate(ctx context.Context, projectFlockKandangId uint, recordTime time.Time) (bool, error)
|
||||
@@ -272,6 +279,18 @@ func (r *RecordingRepositoryImpl) GenerateNextDay(tx *gorm.DB, projectFlockKanda
|
||||
return nextRecordingDay(days), nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) CreateRecording(tx *gorm.DB, recording *entity.Recording) error {
|
||||
if recording == nil {
|
||||
return nil
|
||||
}
|
||||
return tx.Select(
|
||||
"ProjectFlockKandangId",
|
||||
"RecordDatetime",
|
||||
"Day",
|
||||
"CreatedBy",
|
||||
).Create(recording).Error
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) CreateStocks(tx *gorm.DB, stocks []entity.RecordingStock) error {
|
||||
if len(stocks) == 0 {
|
||||
return nil
|
||||
@@ -279,10 +298,24 @@ func (r *RecordingRepositoryImpl) CreateStocks(tx *gorm.DB, stocks []entity.Reco
|
||||
return tx.Create(&stocks).Error
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) CreateStock(tx *gorm.DB, stock *entity.RecordingStock) error {
|
||||
if stock == nil {
|
||||
return nil
|
||||
}
|
||||
return tx.Create(stock).Error
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) DeleteStocks(tx *gorm.DB, recordingID uint) error {
|
||||
return tx.Where("recording_id = ?", recordingID).Delete(&entity.RecordingStock{}).Error
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) DeleteStocksByIDs(tx *gorm.DB, ids []uint) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tx.Where("id IN ?", ids).Delete(&entity.RecordingStock{}).Error
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) ListStocks(tx *gorm.DB, recordingID uint) ([]entity.RecordingStock, error) {
|
||||
var items []entity.RecordingStock
|
||||
if err := tx.Where("recording_id = ?", recordingID).Find(&items).Error; err != nil {
|
||||
@@ -291,6 +324,18 @@ func (r *RecordingRepositoryImpl) ListStocks(tx *gorm.DB, recordingID uint) ([]e
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) GetStockByID(tx *gorm.DB, stockID uint) (*entity.RecordingStock, error) {
|
||||
if stockID == 0 {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
var stock entity.RecordingStock
|
||||
if err := tx.Where("id = ?", stockID).Take(&stock).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &stock, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) UpdateStockUsage(tx *gorm.DB, stockID uint, usageQty, pendingQty float64) error {
|
||||
return tx.Model(&entity.RecordingStock{}).
|
||||
Where("id = ?", stockID).
|
||||
@@ -306,6 +351,16 @@ func (r *RecordingRepositoryImpl) UpdateDepletionPending(tx *gorm.DB, depletionI
|
||||
Update("pending_qty", pendingQty).Error
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) UpdateDepletionQuantities(tx *gorm.DB, depletionID uint, qty, usageQty, pendingQty float64) error {
|
||||
return tx.Model(&entity.RecordingDepletion{}).
|
||||
Where("id = ?", depletionID).
|
||||
Updates(map[string]any{
|
||||
"qty": qty,
|
||||
"usage_qty": usageQty,
|
||||
"pending_qty": pendingQty,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) CreateDepletions(tx *gorm.DB, depletions []entity.RecordingDepletion) error {
|
||||
if len(depletions) == 0 {
|
||||
return nil
|
||||
@@ -325,6 +380,18 @@ func (r *RecordingRepositoryImpl) ListDepletions(tx *gorm.DB, recordingID uint)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) GetDepletionByID(tx *gorm.DB, depletionID uint) (*entity.RecordingDepletion, error) {
|
||||
if depletionID == 0 {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
var depletion entity.RecordingDepletion
|
||||
if err := tx.Where("id = ?", depletionID).Take(&depletion).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &depletion, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) CreateEggs(tx *gorm.DB, eggs []entity.RecordingEgg) error {
|
||||
if len(eggs) == 0 {
|
||||
return nil
|
||||
@@ -344,6 +411,12 @@ func (r *RecordingRepositoryImpl) ListEggs(tx *gorm.DB, recordingID uint) ([]ent
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) UpdateEggTotalQty(tx *gorm.DB, eggID uint, totalQty float64) error {
|
||||
return tx.Model(&entity.RecordingEgg{}).
|
||||
Where("id = ?", eggID).
|
||||
Update("total_qty", totalQty).Error
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) GetRecordingEggByID(
|
||||
ctx context.Context,
|
||||
id uint,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user