mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
feat(BE-115,116,117): implement chickin CRUD, approve logic, and stock availabilit
This commit is contained in:
@@ -4,8 +4,14 @@ 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"
|
||||
AuditLogRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -20,24 +26,39 @@ type ChickinService interface {
|
||||
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.ChickinRepository
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProjectChickinRepository
|
||||
KandangRepo KandangRepo.KandangRepository
|
||||
WarehouseRepo rWarehouse.WarehouseRepository
|
||||
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
||||
ProjectFlockRepo rProjectFlock.ProjectflockRepository
|
||||
AuditLogRepo AuditLogRepo.AuditLogRepository
|
||||
}
|
||||
|
||||
func NewChickinService(repo repository.ChickinRepository, validate *validator.Validate) ChickinService {
|
||||
func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, auditLogRepo AuditLogRepo.AuditLogRepository, validate *validator.Validate) ChickinService {
|
||||
return &chickinService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
KandangRepo: kandangRepo,
|
||||
WarehouseRepo: warehouseRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
ProjectFlockRepo: projectFlockRepo,
|
||||
AuditLogRepo: auditLogRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s chickinService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser")
|
||||
return db.
|
||||
Preload("CreatedUser").
|
||||
Preload("ProjectFlock").
|
||||
Preload("ProjectFlock.ProductCategory")
|
||||
|
||||
}
|
||||
|
||||
func (s chickinService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectChickin, int64, error) {
|
||||
@@ -79,16 +100,125 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createBody := &entity.ProjectChickin{
|
||||
ProjectFlocId: 1,
|
||||
// ambil salah satu kandang dari project_floc_id dari kandang repository
|
||||
kandang, err := s.KandangRepo.GetFirstByProjectFlockID(c.Context(), 1)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get kandang: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
// ambil warehouse dari kandangid
|
||||
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), kandang.Id)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get warehouse: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
// getprojectflock id with relation
|
||||
projectFlock, err := s.ProjectFlockRepo.GetByID(
|
||||
c.Context(),
|
||||
req.ProjectFlockId,
|
||||
func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ProductCategory")
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get project flock: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock not found")
|
||||
}
|
||||
// ambil quantity
|
||||
var productWarehouse entity.ProductWarehouse
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
|
||||
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||
Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
|
||||
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", projectFlock.ProductCategory.Code, warehouse.Id).
|
||||
Order("created_at DESC").
|
||||
First(&productWarehouse).Error
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
|
||||
}
|
||||
s.Log.Errorf("Failed to get product warehouse: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||
if productWarehouse.Quantity < 1 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Insufficient product quantity in warehouse")
|
||||
}
|
||||
|
||||
// masukan ke chic in
|
||||
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{
|
||||
ProjectFlocId: req.ProjectFlockId,
|
||||
ChickInDate: chickinDate,
|
||||
Quantity: productWarehouse.Quantity,
|
||||
Note: "",
|
||||
CreatedBy: 1, //todo: ganti dengan
|
||||
}
|
||||
|
||||
err = s.Repository.CreateOne(c.Context(), newChickin, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to create chickin: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, createBody.Id)
|
||||
// Kurangi quantity di product warehouse
|
||||
updatedQuantity := productWarehouse.Quantity - newChickin.Quantity
|
||||
if updatedQuantity < 0 {
|
||||
updatedQuantity = 0
|
||||
}
|
||||
err = s.ProductWarehouseRepo.PatchOne(c.Context(), productWarehouse.Id, map[string]any{
|
||||
"quantity": updatedQuantity,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// masukan check apakah stock availability ada, jika ada update, jika tidak buat baru
|
||||
stockAvailability := &entity.StockAvailability{
|
||||
EntityType: entity.EntityTypeProjectFlockKandang,
|
||||
ReservedQuantity: productWarehouse.Quantity,
|
||||
EntityId: req.ProjectFlockId, //todo: nanti pakek projct flock kandang id
|
||||
ProductId: productWarehouse.ProductId,
|
||||
}
|
||||
|
||||
var existingStockAvailability entity.StockAvailability
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
|
||||
Where("entity_type = ? AND entity_id = ? AND product_id = ?", stockAvailability.EntityType, stockAvailability.EntityId, stockAvailability.ProductId).
|
||||
First(&existingStockAvailability).Error
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// buat baru
|
||||
stockAvailability.ReservedQuantity = newChickin.Quantity
|
||||
stockAvailability.Quantity = 0
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).Create(stockAvailability).Error
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to create stock availability: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
s.Log.Errorf("Failed to get stock availability: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// update existing
|
||||
newQuantity := existingStockAvailability.ReservedQuantity + newChickin.Quantity
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
|
||||
Model(&existingStockAvailability).
|
||||
Update("reserved_quantity", newQuantity).Error
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update stock availability: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error) {
|
||||
@@ -98,10 +228,9 @@ func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
|
||||
if req.Name != nil {
|
||||
updateBody["name"] = *req.Name
|
||||
if req.ChickInDate != "" {
|
||||
updateBody["chick_in_date"] = req.ChickInDate
|
||||
}
|
||||
|
||||
if len(updateBody) == 0 {
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
@@ -125,5 +254,41 @@ func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
s.Log.Errorf("Failed to delete chickin: %+v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *chickinService) Approve(c *fiber.Ctx, id uint) error {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
//pindahkan stock dari reserved ke actual stock
|
||||
// get stock avaibility untuk di update
|
||||
var stockAvailability entity.StockAvailability
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
|
||||
Where("entity_type = ? AND entity_id = ? ", entity.EntityTypeProjectFlockKandang, chickin.ProjectFlocId).
|
||||
First(&stockAvailability).Error
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get stock availability: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
newReservedQuantity := stockAvailability.ReservedQuantity - chickin.Quantity
|
||||
if newReservedQuantity < 0 {
|
||||
newReservedQuantity = 0
|
||||
}
|
||||
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user