add restrict for expense,purchase,adjustment transfer: unfinished

This commit is contained in:
ragilap
2025-12-09 15:16:01 +07:00
parent 7094d90034
commit 0fbf04fc1d
18 changed files with 681 additions and 395 deletions
@@ -222,6 +222,14 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Marketing product %d not found for this marketing", requestedProduct.MarketingProductId))
}
if err := commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(
c.Context(),
dbTransaction,
[]uint{foundMarketingProduct.ProductWarehouseId},
); err != nil {
return err
}
deliveryProduct, err := marketingDeliveryProductRepositoryTx.GetByMarketingProductID(c.Context(), foundMarketingProduct.Id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -319,6 +327,13 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, i
if foundMarketingProduct == nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Marketing product %d not found for this marketing", requestedProduct.MarketingProductId))
}
if err := commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(
c.Context(),
dbTransaction,
[]uint{foundMarketingProduct.ProductWarehouseId},
); err != nil {
return err
}
deliveryProduct, err := marketingDeliveryProductRepositoryTx.GetByMarketingProductID(c.Context(), foundMarketingProduct.Id)
if err != nil {
@@ -13,6 +13,8 @@ import (
rSalesOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/repositories"
sSalesOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/services"
rCustomer "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
rProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
@@ -27,12 +29,13 @@ func (SalesOrdersModule) RegisterRoutes(router fiber.Router, db *gorm.DB, valida
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(db))
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
if err := approvalSvc.RegisterWorkflowSteps(utils.ApprovalWorkflowMarketing, utils.MarketingApprovalSteps); err != nil {
panic(fmt.Sprintf("failed to register marketing approval workflow: %v", err))
}
salesOrdersService := sSalesOrders.NewSalesOrdersService(marketingRepo, customerRepo, productWarehouseRepo, userRepo, approvalSvc, validate)
salesOrdersService := sSalesOrders.NewSalesOrdersService(marketingRepo, customerRepo, productWarehouseRepo, userRepo, approvalSvc, warehouseRepo, projectFlockKandangRepo,validate)
userService := sUser.NewUserService(userRepo, validate)
SalesOrdersRoutes(router, userService, salesOrdersService)
@@ -14,6 +14,8 @@ import (
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/validations"
customerRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
warehouseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
projectFlockKandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
userRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
@@ -32,24 +34,29 @@ type SalesOrdersService interface {
}
type salesOrdersService struct {
Log *logrus.Logger
Validate *validator.Validate
MarketingRepo repository.MarketingRepository
CustomerRepo customerRepo.CustomerRepository
ProductWarehouseRepo productWarehouseRepo.ProductWarehouseRepository
UserRepo userRepo.UserRepository
ApprovalSvc commonSvc.ApprovalService
Log *logrus.Logger
Validate *validator.Validate
MarketingRepo repository.MarketingRepository
CustomerRepo customerRepo.CustomerRepository
ProductWarehouseRepo productWarehouseRepo.ProductWarehouseRepository
UserRepo userRepo.UserRepository
ApprovalSvc commonSvc.ApprovalService
WarehouseRepo warehouseRepo.WarehouseRepository
ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository
}
func NewSalesOrdersService(marketingRepo repository.MarketingRepository, customerRepo customerRepo.CustomerRepository, productWarehouseRepo productWarehouseRepo.ProductWarehouseRepository, userRepo userRepo.UserRepository, approvalSvc commonSvc.ApprovalService, validate *validator.Validate) SalesOrdersService {
func NewSalesOrdersService(marketingRepo repository.MarketingRepository, customerRepo customerRepo.CustomerRepository, productWarehouseRepo productWarehouseRepo.ProductWarehouseRepository, userRepo userRepo.UserRepository, approvalSvc commonSvc.ApprovalService, warehouseRepo warehouseRepo.WarehouseRepository,
projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository, validate *validator.Validate) SalesOrdersService {
return &salesOrdersService{
Log: utils.Log,
Validate: validate,
MarketingRepo: marketingRepo,
CustomerRepo: customerRepo,
ProductWarehouseRepo: productWarehouseRepo,
UserRepo: userRepo,
ApprovalSvc: approvalSvc,
Log: utils.Log,
Validate: validate,
MarketingRepo: marketingRepo,
CustomerRepo: customerRepo,
ProductWarehouseRepo: productWarehouseRepo,
UserRepo: userRepo,
ApprovalSvc: approvalSvc,
WarehouseRepo: warehouseRepo,
ProjectFlockKandangRepo: projectFlockKandangRepo,
}
}
@@ -140,10 +147,18 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
}
if len(req.MarketingProducts) > 0 {
pwIDs := make([]uint, 0, len(req.MarketingProducts))
for _, product := range req.MarketingProducts {
if product.ProductWarehouseId != 0 {
pwIDs = append(pwIDs, product.ProductWarehouseId)
}
if err := s.createMarketingProductWithDelivery(c.Context(), marketing.Id, product, marketingProductRepoTx, invDeliveryRepoTx); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing product")
}
}
if err := commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(c.Context(), s.MarketingRepo.DB(), pwIDs); err != nil {
return err
}
}
@@ -213,6 +228,18 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
}
}
}
if len(req.MarketingProducts) > 0 {
pwIDs := make([]uint, 0, len(req.MarketingProducts))
for _, item := range req.MarketingProducts {
if item.ProductWarehouseId != 0 {
pwIDs = append(pwIDs, item.ProductWarehouseId)
}
}
if err := commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(c.Context(), s.MarketingRepo.DB(), pwIDs); err != nil {
return nil, err
}
}
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
@@ -367,7 +394,18 @@ func (s salesOrdersService) DeleteOne(c *fiber.Ctx, id uint) error {
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales order")
}
if len(marketing.Products) > 0 {
pwIDs := make([]uint, 0, len(marketing.Products))
for _, p := range marketing.Products {
if p.ProductWarehouseId != 0 {
pwIDs = append(pwIDs, p.ProductWarehouseId)
}
}
if err := commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(c.Context(), s.MarketingRepo.DB(), pwIDs); err != nil {
return err
}
}
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
marketingProductRepoTx := repository.NewMarketingProductRepository(dbTransaction)
@@ -458,6 +496,27 @@ func (s salesOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]e
fmt.Sprintf("Marketing %d cannot be approved - current step is %d", id, latestApproval.StepNumber))
}
}
marketing, mErr := s.MarketingRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
return db.Preload("Products")
})
if mErr != nil {
if errors.Is(mErr, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("SalesOrders %d not found", id))
}
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales order for project validation")
}
if len(marketing.Products) > 0 {
pwIDs := make([]uint, 0, len(marketing.Products))
for _, p := range marketing.Products {
if p.ProductWarehouseId != 0 {
pwIDs = append(pwIDs, p.ProductWarehouseId)
}
}
if err := commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(c.Context(), s.MarketingRepo.DB(), pwIDs); err != nil {
return nil, err
}
}
}
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {