mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
403 lines
14 KiB
Go
403 lines
14 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
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"
|
|
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"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
|
|
|
"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
|
|
Approve(ctx *fiber.Ctx, id uint) error
|
|
}
|
|
|
|
type chickinService struct {
|
|
Log *logrus.Logger
|
|
Validate *validator.Validate
|
|
Repository repository.ProjectChickinRepository
|
|
KandangRepo KandangRepo.KandangRepository
|
|
WarehouseRepo rWarehouse.WarehouseRepository
|
|
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
|
ProjectFlockRepo rProjectFlock.ProjectflockRepository
|
|
ProjectflockKandangRepo rProjectFlock.ProjectFlockKandangRepository
|
|
ProjectflockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository
|
|
ProjectChickinDetailRepo repository.ProjectChickinDetailRepository
|
|
}
|
|
|
|
func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, projectflockkandangRepo rProjectFlock.ProjectFlockKandangRepository, projectflockpopulationRepo rProjectFlock.ProjectFlockPopulationRepository, projectChickinDetailRepo repository.ProjectChickinDetailRepository, validate *validator.Validate) ChickinService {
|
|
return &chickinService{
|
|
Log: utils.Log,
|
|
Validate: validate,
|
|
Repository: repo,
|
|
KandangRepo: kandangRepo,
|
|
WarehouseRepo: warehouseRepo,
|
|
ProductWarehouseRepo: productWarehouseRepo,
|
|
ProjectFlockRepo: projectFlockRepo,
|
|
ProjectflockKandangRepo: projectflockkandangRepo,
|
|
ProjectflockPopulationRepo: projectflockpopulationRepo,
|
|
ProjectChickinDetailRepo: projectChickinDetailRepo,
|
|
}
|
|
}
|
|
|
|
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.Flock").
|
|
Preload("ProjectFlockKandang.ProjectFlock.Area").
|
|
Preload("ProjectFlockKandang.ProjectFlock.Fcr").
|
|
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 {
|
|
s.Log.Errorf("Failed to get chickins: %+v", err)
|
|
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 {
|
|
s.Log.Errorf("Failed get chickin by id: %+v", err)
|
|
return nil, err
|
|
}
|
|
return chickin, 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
|
|
}
|
|
|
|
projectflockkandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), req.ProjectFlockKandangId)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to get projectflock kandang: %+v", err)
|
|
return nil, err
|
|
}
|
|
|
|
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectflockkandang.KandangId)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to get warehouse: %+v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// move complex DB query into repository for cleaner service
|
|
productWarehouses, err := s.ProductWarehouseRepo.GetByCategoryCodeAndWarehouseID(c.Context(), "DOC", warehouse.Id)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to get product warehouses: %+v", err)
|
|
return nil, err
|
|
}
|
|
if len(productWarehouses) == 0 {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
|
|
}
|
|
totalQuantity := 0.0
|
|
for _, pw := range productWarehouses {
|
|
totalQuantity += pw.Quantity
|
|
}
|
|
|
|
if totalQuantity < 1 {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Insufficient quantity in Product Warehouses")
|
|
}
|
|
|
|
chickinDate, err := utils.ParseDateString(req.ChickInDate)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to parse chickin date: %+v", err)
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid ChickInDate format")
|
|
}
|
|
newChickin := &entity.ProjectChickin{
|
|
ProjectFlockKandangId: projectflockkandang.Id,
|
|
ChickInDate: chickinDate,
|
|
Quantity: totalQuantity,
|
|
Note: req.Note,
|
|
CreatedBy: 1, //todo: ganti dengan user login
|
|
}
|
|
err = s.Repository.CreateOne(c.Context(), newChickin, nil)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to create chickin: %+v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Update semua product warehouse: set quantity jadi 0
|
|
for _, pw := range productWarehouses {
|
|
err = s.ProductWarehouseRepo.PatchOne(c.Context(), pw.Id, map[string]any{
|
|
"quantity": 0,
|
|
}, nil)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
|
|
return nil, err
|
|
}
|
|
|
|
newChickinDetail := &entity.ProjectChickinDetail{
|
|
ProjectChickinId: newChickin.Id,
|
|
ProductWarehouseId: pw.Id,
|
|
Quantity: pw.Quantity,
|
|
CreatedBy: 1, // todo: ganti dengan user login
|
|
}
|
|
err = s.ProjectChickinDetailRepo.CreateOne(c.Context(), newChickinDetail, nil)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to create chickin detail: %+v", err)
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
existingPopulation, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), req.ProjectFlockKandangId)
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
s.Log.Errorf("Failed to get project flock population: %+v", err)
|
|
return nil, err
|
|
}
|
|
if existingPopulation != nil {
|
|
|
|
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), existingPopulation.Id, map[string]any{
|
|
"reserved_quantity": newChickin.Quantity + existingPopulation.ReservedQuantity,
|
|
}, nil)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to update project flock population: %+v", err)
|
|
return nil, err
|
|
}
|
|
} else {
|
|
newPopulation := &entity.ProjectFlockPopulation{
|
|
ProjectFlockKandangId: req.ProjectFlockKandangId,
|
|
InitialQuantity: 0,
|
|
CurrentQuantity: 0,
|
|
ReservedQuantity: newChickin.Quantity,
|
|
CreatedBy: 1, // todo: ganti dengan user login
|
|
}
|
|
err = s.ProjectflockPopulationRepo.CreateOne(c.Context(), newPopulation, nil)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to create project flock population: %+v", err)
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return s.GetOne(c, newChickin.Id)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
updateBody := make(map[string]any)
|
|
|
|
if req.ChickInDate != "" {
|
|
updateBody["chick_in_date"] = req.ChickInDate
|
|
}
|
|
if req.Note != "" {
|
|
updateBody["note"] = 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")
|
|
}
|
|
s.Log.Errorf("Failed to update chickin: %+v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return s.GetOne(c, id)
|
|
}
|
|
|
|
func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error {
|
|
|
|
err := s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
|
|
|
chickinRepo := repository.NewChickinRepository(dbTransaction)
|
|
projectFlockKandangRepo := s.ProjectflockKandangRepo.WithTx(dbTransaction)
|
|
productWarehouseRepo := s.ProductWarehouseRepo.WithTxRepo(dbTransaction)
|
|
projectFlockPopulationRepo := s.ProjectflockPopulationRepo.WithTx(dbTransaction)
|
|
projectChickinDetailRepo := s.ProjectChickinDetailRepo.WithTxRepo(dbTransaction)
|
|
warehouseRepoTx := rWarehouse.NewWarehouseRepository(dbTransaction)
|
|
|
|
chickin, err := chickinRepo.GetByID(c.Context(), id, nil)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
|
}
|
|
return err
|
|
}
|
|
|
|
population, err := projectFlockPopulationRepo.GetByProjectFlockKandangID(c.Context(), chickin.ProjectFlockKandangId)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Project flock population not found")
|
|
}
|
|
return err
|
|
}
|
|
|
|
newReserved := population.ReservedQuantity - chickin.Quantity
|
|
if newReserved < 0 {
|
|
newReserved = 0
|
|
}
|
|
|
|
err = projectFlockPopulationRepo.PatchOne(c.Context(), population.Id, map[string]any{
|
|
"reserved_quantity": newReserved,
|
|
}, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
restoreFromDetails := func() (bool, error) {
|
|
|
|
details, err := projectChickinDetailRepo.GetByProjectChickinID(c.Context(), chickin.Id)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
for _, d := range details {
|
|
productWarehouse, err := productWarehouseRepo.GetByID(c.Context(), d.ProductWarehouseId, nil)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
continue
|
|
}
|
|
return false, err
|
|
}
|
|
|
|
updatedQuantity := productWarehouse.Quantity + d.Quantity
|
|
if err := productWarehouseRepo.PatchOne(c.Context(), productWarehouse.Id, map[string]any{"quantity": updatedQuantity}, nil); err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
|
|
if err := projectChickinDetailRepo.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB {
|
|
return db.Where("project_chickin_id = ?", chickin.Id)
|
|
}); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
restored, err := restoreFromDetails()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !restored {
|
|
projectflockkandang, err := projectFlockKandangRepo.GetByID(c.Context(), population.ProjectFlockKandangId)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Project flock kandang not found")
|
|
}
|
|
return err
|
|
}
|
|
|
|
warehouse, err := warehouseRepoTx.GetByKandangID(c.Context(), projectflockkandang.KandangId)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Warehouse not found for kandang")
|
|
}
|
|
return err
|
|
}
|
|
|
|
productWarehouse, err := productWarehouseRepo.GetLatestByCategoryCodeAndWarehouseID(c.Context(), "DOC", warehouse.Id)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
|
|
}
|
|
return err
|
|
}
|
|
|
|
updatedQuantity := productWarehouse.Quantity + chickin.Quantity
|
|
|
|
if err := productWarehouseRepo.PatchOne(c.Context(), productWarehouse.Id, map[string]any{"quantity": updatedQuantity}, nil); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := chickinRepo.DeleteOne(c.Context(), id); err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
|
}
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
if ferr, ok := err.(*fiber.Error); ok {
|
|
return ferr
|
|
}
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *chickinService) Approve(c *fiber.Ctx, id uint) error {
|
|
|
|
// todo: ini contoh akhir jika sudah approved
|
|
|
|
chickin, err := s.Repository.GetByID(c.Context(), id, nil)
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
|
}
|
|
if err != nil {
|
|
s.Log.Errorf("Failed get chickin by id: %+v", err)
|
|
return err
|
|
}
|
|
|
|
population, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), chickin.ProjectFlockKandangId)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to get project flock population: %+v", err)
|
|
return err
|
|
}
|
|
|
|
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), population.Id, map[string]any{
|
|
"reserved_quantity": population.ReservedQuantity - chickin.Quantity,
|
|
"initial_quantity": population.InitialQuantity + chickin.Quantity,
|
|
"current_quantity": population.CurrentQuantity + chickin.Quantity,
|
|
}, nil)
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to update project flock population: %+v", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|