mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 07:15:43 +00:00
Feat[BE-222]: Completed SO and DO API
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/dto"
|
||||
@@ -22,65 +21,6 @@ func NewSalesOrdersController(salesOrdersService service.SalesOrdersService) *Sa
|
||||
}
|
||||
}
|
||||
|
||||
func (u *SalesOrdersController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
result, totalResults, err := u.SalesOrdersService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Convert marketing data to sales orders DTOs with products
|
||||
salesOrdersDTOs := make([]dto.SalesOrdersListDTO, len(result))
|
||||
for i, marketing := range result {
|
||||
salesOrdersDTOs[i] = dto.ToSalesOrdersListDTOFromMarketing(marketing)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.SalesOrdersListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all salesOrderss successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: salesOrdersDTOs,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *SalesOrdersController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, err := u.SalesOrdersService.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get salesOrders successfully",
|
||||
Data: dto.ToSalesOrdersListDTOFromMarketing(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *SalesOrdersController) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
@@ -115,13 +55,7 @@ func (u *SalesOrdersController) UpdateOne(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
_, err = u.SalesOrdersService.UpdateOne(c, req, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Fetch full updated data for response
|
||||
result, err := u.SalesOrdersService.GetOne(c, uint(id))
|
||||
result, err := u.SalesOrdersService.UpdateOne(c, req, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,111 +4,37 @@ import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||
customerDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/dto"
|
||||
productDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/dto"
|
||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||
productWarehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
|
||||
type SalesOrdersBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type MarketingProductDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
ProductWarehouse *struct {
|
||||
Id uint `json:"id"`
|
||||
Product *productDTO.ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *warehouseDTO.WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
} `json:"product_warehouse,omitempty"`
|
||||
DeliveryProduct *MarketingDeliveryProductDTO `json:"delivery_product,omitempty"`
|
||||
}
|
||||
|
||||
type MarketingDeliveryProductDTO struct {
|
||||
Id uint `json:"id"`
|
||||
MarketingProductId uint `json:"marketing_product_id"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
DeliveryDate *time.Time `json:"delivery_date"`
|
||||
VehicleNumber string `json:"vehicle_number"`
|
||||
Id uint `json:"id"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
ProductWarehouse *productWarehouseDTO.ProductWarehousNestedDTO `json:"product_warehouse,omitempty"`
|
||||
}
|
||||
|
||||
type SalesOrdersListDTO struct {
|
||||
SalesOrdersBaseDTO
|
||||
CustomerId uint `json:"customer_id,omitempty"`
|
||||
Customer *customerDTO.CustomerBaseDTO `json:"customer,omitempty"`
|
||||
SoDate *time.Time `json:"so_date,omitempty"`
|
||||
SalesPersonId uint `json:"sales_person_id,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
MarketingProducts []MarketingProductDTO `json:"marketing_products,omitempty"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Approval approvalDTO.ApprovalBaseDTO `json:"approval"`
|
||||
}
|
||||
|
||||
type SalesOrdersDetailDTO struct {
|
||||
SalesOrdersListDTO
|
||||
Id uint `json:"id"`
|
||||
SoNumber string `json:"so_number"`
|
||||
SoDate time.Time `json:"so_date"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
SalesOrder []MarketingProductDTO `json:"sales_order,omitempty"`
|
||||
}
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func ToSalesOrdersBaseDTO(e entity.SalesOrders) SalesOrdersBaseDTO {
|
||||
return SalesOrdersBaseDTO{
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func ToMarketingProductDTO(e entity.MarketingProduct) MarketingProductDTO {
|
||||
var productWarehouse *struct {
|
||||
Id uint `json:"id"`
|
||||
Product *productDTO.ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *warehouseDTO.WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
}
|
||||
var productWarehouse *productWarehouseDTO.ProductWarehousNestedDTO
|
||||
|
||||
if e.ProductWarehouse.Id != 0 {
|
||||
product := (*productDTO.ProductBaseDTO)(nil)
|
||||
if e.ProductWarehouse.Product.Id != 0 {
|
||||
mapped := productDTO.ToProductBaseDTO(e.ProductWarehouse.Product)
|
||||
product = &mapped
|
||||
}
|
||||
|
||||
warehouse := (*warehouseDTO.WarehouseBaseDTO)(nil)
|
||||
if e.ProductWarehouse.Warehouse.Id != 0 {
|
||||
mapped := warehouseDTO.ToWarehouseBaseDTO(e.ProductWarehouse.Warehouse)
|
||||
warehouse = &mapped
|
||||
}
|
||||
|
||||
productWarehouse = &struct {
|
||||
Id uint `json:"id"`
|
||||
Product *productDTO.ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *warehouseDTO.WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
}{
|
||||
Id: e.ProductWarehouse.Id,
|
||||
Product: product,
|
||||
Warehouse: warehouse,
|
||||
}
|
||||
}
|
||||
|
||||
var deliveryProduct *MarketingDeliveryProductDTO
|
||||
if e.DeliveryProduct != nil && e.DeliveryProduct.Id != 0 {
|
||||
mapped := ToMarketingDeliveryProductDTO(*e.DeliveryProduct)
|
||||
deliveryProduct = &mapped
|
||||
mapped := productWarehouseDTO.ToProductWarehouseNestedDTO(e.ProductWarehouse)
|
||||
productWarehouse = &mapped
|
||||
}
|
||||
|
||||
return MarketingProductDTO{
|
||||
@@ -119,139 +45,38 @@ func ToMarketingProductDTO(e entity.MarketingProduct) MarketingProductDTO {
|
||||
TotalWeight: e.TotalWeight,
|
||||
TotalPrice: e.TotalPrice,
|
||||
ProductWarehouse: productWarehouse,
|
||||
DeliveryProduct: deliveryProduct,
|
||||
}
|
||||
}
|
||||
|
||||
func ToMarketingDeliveryProductDTO(e entity.MarketingDeliveryProduct) MarketingDeliveryProductDTO {
|
||||
return MarketingDeliveryProductDTO{
|
||||
Id: e.Id,
|
||||
MarketingProductId: e.MarketingProductId,
|
||||
Qty: e.Qty,
|
||||
UnitPrice: e.UnitPrice,
|
||||
TotalWeight: e.TotalWeight,
|
||||
AvgWeight: e.AvgWeight,
|
||||
TotalPrice: e.TotalPrice,
|
||||
DeliveryDate: e.DeliveryDate,
|
||||
VehicleNumber: e.VehicleNumber,
|
||||
}
|
||||
}
|
||||
|
||||
func defaultSalesOrdersLatestApproval(e entity.SalesOrders) approvalDTO.ApprovalBaseDTO {
|
||||
result := approvalDTO.ApprovalBaseDTO{}
|
||||
|
||||
step := utils.MarketingStepPengajuan
|
||||
if step > 0 {
|
||||
result.StepNumber = uint16(step)
|
||||
if label, ok := approvalutils.ApprovalStepName(utils.ApprovalWorkflowMarketing, step); ok {
|
||||
result.StepName = label
|
||||
} else if label, ok := utils.MarketingApprovalSteps[step]; ok {
|
||||
result.StepName = label
|
||||
}
|
||||
}
|
||||
|
||||
if e.CreatedUser.Id != 0 {
|
||||
result.ActionBy = userDTO.ToUserBaseDTO(e.CreatedUser)
|
||||
} else if e.CreatedBy != 0 {
|
||||
result.ActionBy = userDTO.UserBaseDTO{
|
||||
Id: e.CreatedBy,
|
||||
IdUser: int64(e.CreatedBy),
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func ToSalesOrdersListDTO(e entity.SalesOrders) SalesOrdersListDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
approval := defaultSalesOrdersLatestApproval(e)
|
||||
if e.LatestApproval != nil {
|
||||
approval = approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||
}
|
||||
|
||||
return SalesOrdersListDTO{
|
||||
SalesOrdersBaseDTO: ToSalesOrdersBaseDTO(e),
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
CreatedUser: createdUser,
|
||||
Approval: approval,
|
||||
Id: e.Id,
|
||||
SoNumber: e.Name,
|
||||
SoDate: time.Time{},
|
||||
Notes: "",
|
||||
SalesOrder: []MarketingProductDTO{},
|
||||
}
|
||||
}
|
||||
|
||||
func ToSalesOrdersListDTOFromMarketing(e entity.Marketing) SalesOrdersListDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
var marketingProducts []MarketingProductDTO
|
||||
var salesOrder []MarketingProductDTO
|
||||
if len(e.Products) > 0 {
|
||||
marketingProducts = make([]MarketingProductDTO, len(e.Products))
|
||||
salesOrder = make([]MarketingProductDTO, len(e.Products))
|
||||
for i, product := range e.Products {
|
||||
marketingProducts[i] = ToMarketingProductDTO(product)
|
||||
salesOrder[i] = ToMarketingProductDTO(product)
|
||||
}
|
||||
}
|
||||
|
||||
var customerSummary *customerDTO.CustomerBaseDTO
|
||||
if e.Customer.Id != 0 {
|
||||
mapped := customerDTO.ToCustomerBaseDTO(e.Customer)
|
||||
customerSummary = &mapped
|
||||
}
|
||||
|
||||
approval := defaultSalesOrdersLatestApprovalFromMarketing(e)
|
||||
if e.LatestApproval != nil {
|
||||
approval = approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||
}
|
||||
|
||||
return SalesOrdersListDTO{
|
||||
SalesOrdersBaseDTO: SalesOrdersBaseDTO{
|
||||
Id: e.Id,
|
||||
Name: e.SoNumber,
|
||||
},
|
||||
CustomerId: e.Customer.Id,
|
||||
Customer: customerSummary,
|
||||
SoDate: &e.SoDate,
|
||||
SalesPersonId: e.SalesPersonId,
|
||||
Notes: e.Notes,
|
||||
MarketingProducts: marketingProducts,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
CreatedUser: createdUser,
|
||||
Approval: approval,
|
||||
Id: e.Id,
|
||||
SoNumber: e.SoNumber,
|
||||
SoDate: e.SoDate,
|
||||
Notes: e.Notes,
|
||||
SalesOrder: salesOrder,
|
||||
}
|
||||
}
|
||||
|
||||
func defaultSalesOrdersLatestApprovalFromMarketing(e entity.Marketing) approvalDTO.ApprovalBaseDTO {
|
||||
result := approvalDTO.ApprovalBaseDTO{}
|
||||
|
||||
step := utils.MarketingStepPengajuan
|
||||
if step > 0 {
|
||||
result.StepNumber = uint16(step)
|
||||
if label, ok := approvalutils.ApprovalStepName(utils.ApprovalWorkflowMarketing, step); ok {
|
||||
result.StepName = label
|
||||
} else if label, ok := utils.MarketingApprovalSteps[step]; ok {
|
||||
result.StepName = label
|
||||
}
|
||||
}
|
||||
|
||||
if e.CreatedUser.Id != 0 {
|
||||
result.ActionBy = userDTO.ToUserBaseDTO(e.CreatedUser)
|
||||
} else if e.CreatedBy != 0 {
|
||||
result.ActionBy = userDTO.UserBaseDTO{
|
||||
Id: e.CreatedBy,
|
||||
IdUser: int64(e.CreatedBy),
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func ToSalesOrdersListDTOsFromMarketing(e []entity.Marketing) []SalesOrdersListDTO {
|
||||
result := make([]SalesOrdersListDTO, len(e))
|
||||
for i, r := range e {
|
||||
@@ -259,17 +84,3 @@ func ToSalesOrdersListDTOsFromMarketing(e []entity.Marketing) []SalesOrdersListD
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ToSalesOrdersListDTOs(e []entity.SalesOrders) []SalesOrdersListDTO {
|
||||
result := make([]SalesOrdersListDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = ToSalesOrdersListDTO(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ToSalesOrdersDetailDTO(e entity.SalesOrders) SalesOrdersDetailDTO {
|
||||
return SalesOrdersDetailDTO{
|
||||
SalesOrdersListDTO: ToSalesOrdersListDTO(e),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package sales_orders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
@@ -11,10 +13,9 @@ 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"
|
||||
rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/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"
|
||||
)
|
||||
|
||||
type SalesOrdersModule struct{}
|
||||
@@ -23,12 +24,16 @@ func (SalesOrdersModule) RegisterRoutes(router fiber.Router, db *gorm.DB, valida
|
||||
salesOrdersRepo := rSalesOrders.NewSalesOrdersRepository(db)
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
customerRepo := rCustomer.NewCustomerRepository(db)
|
||||
kandangRepo := rKandang.NewKandangRepository(db)
|
||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||
marketingRepo := rSalesOrders.NewMarketingRepository(db)
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(db))
|
||||
salesOrdersService := sSalesOrders.NewSalesOrdersService(salesOrdersRepo, customerRepo, kandangRepo, productWarehouseRepo, marketingRepo, userRepo, approvalSvc, validate)
|
||||
|
||||
if err := approvalSvc.RegisterWorkflowSteps(utils.ApprovalWorkflowMarketing, utils.MarketingApprovalSteps); err != nil {
|
||||
panic(fmt.Sprintf("failed to register marketing approval workflow: %v", err))
|
||||
}
|
||||
|
||||
salesOrdersService := sSalesOrders.NewSalesOrdersService(salesOrdersRepo, customerRepo, productWarehouseRepo, marketingRepo, userRepo, approvalSvc, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
SalesOrdersRoutes(router, userService, salesOrdersService)
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
type MarketingRepository interface {
|
||||
repository.BaseRepository[entity.Marketing]
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
GetNextSequence(ctx context.Context) (uint, error)
|
||||
}
|
||||
|
||||
type MarketingRepositoryImpl struct {
|
||||
@@ -26,3 +27,11 @@ func NewMarketingRepository(db *gorm.DB) MarketingRepository {
|
||||
func (r *MarketingRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
|
||||
return repository.Exists[entity.Marketing](ctx, r.DB(), id)
|
||||
}
|
||||
|
||||
func (r *MarketingRepositoryImpl) GetNextSequence(ctx context.Context) (uint, error) {
|
||||
var maxID uint
|
||||
if err := r.DB().WithContext(ctx).Model(&entity.Marketing{}).Select("COALESCE(MAX(id), 0)").Scan(&maxID).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return maxID + 1, nil
|
||||
}
|
||||
|
||||
@@ -12,18 +12,15 @@ import (
|
||||
func SalesOrdersRoutes(v1 fiber.Router, u user.UserService, s salesOrders.SalesOrdersService) {
|
||||
ctrl := controller.NewSalesOrdersController(s)
|
||||
|
||||
v1.Delete("/:id", ctrl.DeleteOne)
|
||||
route := v1.Group("/sales-orders")
|
||||
|
||||
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
|
||||
route.Get("/", ctrl.GetAll)
|
||||
route.Post("/", ctrl.CreateOne)
|
||||
route.Get("/:id", ctrl.GetOne)
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
|
||||
route.Post("/approvals", ctrl.Approval)
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"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"
|
||||
rInvMarketingDeliveryProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/marketing-delivery-products/repositories"
|
||||
productWarehouseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
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"
|
||||
kandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/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"
|
||||
@@ -25,8 +25,6 @@ import (
|
||||
)
|
||||
|
||||
type SalesOrdersService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Marketing, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.Marketing, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Marketing, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Marketing, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
@@ -38,20 +36,18 @@ type salesOrdersService struct {
|
||||
Validate *validator.Validate
|
||||
Repository repository.SalesOrdersRepository
|
||||
CustomerRepo customerRepo.CustomerRepository
|
||||
KandangRepo kandangRepo.KandangRepository
|
||||
ProductWarehouseRepo productWarehouseRepo.ProductWarehouseRepository
|
||||
MarketingRepo repository.MarketingRepository
|
||||
UserRepo userRepo.UserRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
}
|
||||
|
||||
func NewSalesOrdersService(repo repository.SalesOrdersRepository, customerRepo customerRepo.CustomerRepository, kandangRepo kandangRepo.KandangRepository, productWarehouseRepo productWarehouseRepo.ProductWarehouseRepository, marketingRepo repository.MarketingRepository, userRepo userRepo.UserRepository, approvalSvc commonSvc.ApprovalService, validate *validator.Validate) SalesOrdersService {
|
||||
func NewSalesOrdersService(repo repository.SalesOrdersRepository, customerRepo customerRepo.CustomerRepository, productWarehouseRepo productWarehouseRepo.ProductWarehouseRepository, marketingRepo repository.MarketingRepository, userRepo userRepo.UserRepository, approvalSvc commonSvc.ApprovalService, validate *validator.Validate) SalesOrdersService {
|
||||
return &salesOrdersService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
CustomerRepo: customerRepo,
|
||||
KandangRepo: kandangRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
MarketingRepo: marketingRepo,
|
||||
UserRepo: userRepo,
|
||||
@@ -60,58 +56,15 @@ func NewSalesOrdersService(repo repository.SalesOrdersRepository, customerRepo c
|
||||
}
|
||||
|
||||
func (s salesOrdersService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").
|
||||
return db.
|
||||
Preload("CreatedUser").
|
||||
Preload("Customer").
|
||||
Preload("SalesPerson").
|
||||
Preload("Products.ProductWarehouse.Product").
|
||||
Preload("Products.ProductWarehouse.Warehouse")
|
||||
}
|
||||
|
||||
func (s salesOrdersService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Marketing, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
marketings, total, err := s.MarketingRepo.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
if params.Search != "" {
|
||||
return db.Where("so_number LIKE ?", "%"+params.Search+"%")
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get marketings: %+v", err)
|
||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales orders")
|
||||
}
|
||||
|
||||
if s.ApprovalSvc != nil && len(marketings) > 0 {
|
||||
ids := make([]uint, len(marketings))
|
||||
for i, item := range marketings {
|
||||
ids[i] = item.Id
|
||||
}
|
||||
|
||||
latestMap, err := s.ApprovalSvc.LatestByTargets(c.Context(), utils.ApprovalWorkflowMarketing, ids, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Warnf("Unable to load latest approvals for marketings: %+v", err)
|
||||
} else if len(latestMap) > 0 {
|
||||
for i := range marketings {
|
||||
if approval, ok := latestMap[marketings[i].Id]; ok {
|
||||
marketings[i].LatestApproval = approval
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return marketings, total, nil
|
||||
}
|
||||
|
||||
func (s salesOrdersService) GetOne(c *fiber.Ctx, id uint) (*entity.Marketing, error) {
|
||||
|
||||
func (s salesOrdersService) getOne(c *fiber.Ctx, id uint) (*entity.Marketing, error) {
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "SalesOrders not found")
|
||||
@@ -125,15 +78,9 @@ func (s salesOrdersService) GetOne(c *fiber.Ctx, id uint) (*entity.Marketing, er
|
||||
approvals, err := s.ApprovalSvc.ListByTarget(c.Context(), utils.ApprovalWorkflowMarketing, id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Warnf("Unable to load approvals for marketing %d: %+v", id, err)
|
||||
} else if len(approvals) > 0 {
|
||||
if marketing.LatestApproval == nil {
|
||||
latest := approvals[len(approvals)-1]
|
||||
marketing.LatestApproval = &latest
|
||||
}
|
||||
} else {
|
||||
marketing.LatestApproval = nil
|
||||
if err == nil && len(approvals) > 0 {
|
||||
latest := approvals[len(approvals)-1]
|
||||
marketing.LatestApproval = &latest
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +100,6 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
|
||||
|
||||
for _, item := range req.MarketingProducts {
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Kandang", ID: &item.KandangId, Exists: s.KandangRepo.IdExists},
|
||||
commonSvc.RelationCheck{Name: "ProductWarehouse", ID: &item.ProductWarehouseId, Exists: s.ProductWarehouseRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
@@ -165,14 +111,18 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid date format")
|
||||
}
|
||||
|
||||
soNumber := "SO-" + time.Now().Format("20060102150405")
|
||||
nextSeq, err := s.MarketingRepo.GetNextSequence(c.Context())
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to generate SO number")
|
||||
}
|
||||
soNumber := fmt.Sprintf("SO-%05d", nextSeq)
|
||||
|
||||
var marketing *entity.Marketing
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingRepoTx := repository.NewMarketingRepository(dbTransaction)
|
||||
marketingProductRepoTx := repository.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepoTx := repository.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
invDeliveryRepoTx := rInvMarketingDeliveryProduct.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
|
||||
marketing = &entity.Marketing{
|
||||
@@ -184,52 +134,18 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
|
||||
CreatedBy: 1,
|
||||
}
|
||||
if err := marketingRepoTx.CreateOne(c.Context(), marketing, nil); err != nil {
|
||||
s.Log.Errorf("Failed to create marketing: %+v", err)
|
||||
return err
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create salesOrders")
|
||||
}
|
||||
|
||||
if len(req.MarketingProducts) > 0 {
|
||||
for _, product := range req.MarketingProducts {
|
||||
marketingProduct := &entity.MarketingProduct{
|
||||
MarketingId: marketing.Id,
|
||||
ProductWarehouseId: product.ProductWarehouseId,
|
||||
Qty: product.Qty,
|
||||
UnitPrice: product.UnitPrice,
|
||||
AvgWeight: product.AvgWeight,
|
||||
TotalWeight: product.TotalWeight,
|
||||
TotalPrice: product.TotalPrice,
|
||||
}
|
||||
if err := marketingProductRepoTx.CreateOne(c.Context(), marketingProduct, nil); err != nil {
|
||||
s.Log.Errorf("Failed to create marketing product: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
marketingDeliveryProduct := &entity.MarketingDeliveryProduct{
|
||||
MarketingProductId: marketingProduct.Id,
|
||||
Qty: 0,
|
||||
UnitPrice: 0,
|
||||
TotalWeight: 0,
|
||||
AvgWeight: 0,
|
||||
TotalPrice: 0,
|
||||
DeliveryDate: nil,
|
||||
VehicleNumber: product.VehicleNumber,
|
||||
}
|
||||
if err := marketingDeliveryProductRepoTx.CreateOne(c.Context(), marketingDeliveryProduct, nil); err != nil {
|
||||
s.Log.Errorf("Failed to create marketing delivery product: %+v", err)
|
||||
return err
|
||||
if err := s.createMarketingProductWithDelivery(c.Context(), marketing.Id, product, marketingProductRepoTx, invDeliveryRepoTx); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing product")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
actorID := uint(1) // TODO: ambil dari auth context
|
||||
if err := approvalSvcTx.RegisterWorkflowSteps(
|
||||
utils.ApprovalWorkflowMarketing,
|
||||
utils.MarketingApprovalSteps,
|
||||
); err != nil {
|
||||
s.Log.Errorf("Failed to register workflow steps: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
approvalAction := entity.ApprovalActionCreated
|
||||
if _, err := approvalSvcTx.CreateApproval(
|
||||
c.Context(),
|
||||
@@ -240,8 +156,7 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
|
||||
actorID,
|
||||
nil); err != nil {
|
||||
if !errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
s.Log.Errorf("Failed to create approval: %+v", err)
|
||||
return err
|
||||
fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,7 +171,6 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
|
||||
|
||||
marketing, err = s.MarketingRepo.GetByID(c.Context(), marketing.Id, s.withRelations)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch created marketing: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch created sales order")
|
||||
}
|
||||
|
||||
@@ -278,7 +192,6 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
||||
|
||||
latestApproval, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, id, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to check approval status: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
|
||||
}
|
||||
if latestApproval != nil && latestApproval.StepNumber >= 3 {
|
||||
@@ -299,8 +212,8 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
||||
|
||||
marketingRepoTx := repository.NewMarketingRepository(dbTransaction)
|
||||
marketingProductRepoTx := repository.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepoTx := repository.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
invDeliveryRepoTx := rInvMarketingDeliveryProduct.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
if req.CustomerId != 0 {
|
||||
@@ -330,53 +243,85 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
||||
|
||||
oldProducts, err := marketingProductRepoTx.GetByMarketingID(c.Context(), id)
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
s.Log.Errorf("Failed to fetch old marketing products: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update products")
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch existing products")
|
||||
}
|
||||
|
||||
for _, oldProduct := range oldProducts {
|
||||
if err := marketingDeliveryProductRepoTx.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB {
|
||||
return db.Where("marketing_product_id = ?", oldProduct.Id)
|
||||
}); err != nil && err != gorm.ErrRecordNotFound {
|
||||
s.Log.Errorf("Failed to delete delivery products: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update products")
|
||||
oldByPW := make(map[uint]*entity.MarketingProduct)
|
||||
for i := range oldProducts {
|
||||
p := oldProducts[i]
|
||||
oldByPW[p.ProductWarehouseId] = &p
|
||||
}
|
||||
|
||||
reqByPW := make(map[uint]validation.CreateMarketingProduct)
|
||||
for _, rp := range req.MarketingProducts {
|
||||
reqByPW[rp.ProductWarehouseId] = rp
|
||||
}
|
||||
|
||||
for _, rp := range req.MarketingProducts {
|
||||
if old, ok := oldByPW[rp.ProductWarehouseId]; ok {
|
||||
|
||||
updateBody := map[string]any{
|
||||
"product_warehouse_id": rp.ProductWarehouseId,
|
||||
"qty": rp.Qty,
|
||||
"unit_price": rp.UnitPrice,
|
||||
"avg_weight": rp.AvgWeight,
|
||||
"total_weight": rp.TotalWeight,
|
||||
"total_price": rp.TotalPrice,
|
||||
}
|
||||
if err := marketingProductRepoTx.PatchOne(c.Context(), old.Id, updateBody, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update marketing product")
|
||||
}
|
||||
|
||||
// Ensure delivery product exists; if not, create default
|
||||
if _, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
mdp := &entity.MarketingDeliveryProduct{
|
||||
MarketingProductId: old.Id,
|
||||
Qty: 0,
|
||||
UnitPrice: 0,
|
||||
TotalWeight: 0,
|
||||
AvgWeight: 0,
|
||||
TotalPrice: 0,
|
||||
DeliveryDate: nil,
|
||||
VehicleNumber: rp.VehicleNumber,
|
||||
}
|
||||
if err := invDeliveryRepoTx.CreateOne(c.Context(), mdp, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing delivery product")
|
||||
}
|
||||
} else {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check delivery product")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Create new marketing product (use helper)
|
||||
if err := s.createMarketingProductWithDelivery(c.Context(), id, rp, marketingProductRepoTx, invDeliveryRepoTx); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing product")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := marketingProductRepoTx.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB {
|
||||
return db.Where("marketing_id = ?", id)
|
||||
}); err != nil && err != gorm.ErrRecordNotFound {
|
||||
s.Log.Errorf("Failed to delete marketing products: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update products")
|
||||
}
|
||||
|
||||
for _, product := range req.MarketingProducts {
|
||||
marketingProduct := &entity.MarketingProduct{
|
||||
MarketingId: id,
|
||||
ProductWarehouseId: product.ProductWarehouseId,
|
||||
Qty: product.Qty,
|
||||
UnitPrice: product.UnitPrice,
|
||||
AvgWeight: product.AvgWeight,
|
||||
TotalWeight: product.TotalWeight,
|
||||
TotalPrice: product.TotalPrice,
|
||||
}
|
||||
if err := marketingProductRepoTx.CreateOne(c.Context(), marketingProduct, nil); err != nil {
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
marketingDeliveryProduct := &entity.MarketingDeliveryProduct{
|
||||
MarketingProductId: marketingProduct.Id,
|
||||
Qty: 0,
|
||||
UnitPrice: 0,
|
||||
TotalWeight: 0,
|
||||
AvgWeight: 0,
|
||||
TotalPrice: 0,
|
||||
DeliveryDate: nil,
|
||||
VehicleNumber: product.VehicleNumber,
|
||||
}
|
||||
if err := marketingDeliveryProductRepoTx.CreateOne(c.Context(), marketingDeliveryProduct, nil); err != nil {
|
||||
return err
|
||||
// 2) Delete missing old products (prevent deletion if deliveries exist)
|
||||
for _, old := range oldProducts {
|
||||
if _, ok := reqByPW[old.ProductWarehouseId]; !ok {
|
||||
// Check delivery product for this marketing product
|
||||
deliveryProduct, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check existing delivery product")
|
||||
}
|
||||
if err == nil {
|
||||
// If delivery exists (delivery_date not nil or qty > 0), prevent deletion
|
||||
if deliveryProduct.DeliveryDate != nil || deliveryProduct.Qty > 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Cannot delete marketing product %d because it has delivery records", old.Id))
|
||||
}
|
||||
// safe to delete delivery product record
|
||||
if err := invDeliveryRepoTx.DeleteOne(c.Context(), deliveryProduct.Id); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete marketing delivery product")
|
||||
}
|
||||
}
|
||||
// Delete marketing product
|
||||
if err := marketingProductRepoTx.DeleteOne(c.Context(), old.Id); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete marketing product")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -394,7 +339,6 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
||||
actorID,
|
||||
&resetNote)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to create reset approval: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to reset approval status")
|
||||
}
|
||||
}
|
||||
@@ -409,16 +353,16 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update sales order")
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
return s.getOne(c, id)
|
||||
}
|
||||
|
||||
func (s salesOrdersService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, s.withRelations)
|
||||
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "SalesOrders not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch marketing %d before delete: %+v", id, err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales order")
|
||||
}
|
||||
|
||||
@@ -494,7 +438,6 @@ func (s salesOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]e
|
||||
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 Marketing %d - sales orders must be created first", id))
|
||||
}
|
||||
@@ -511,13 +454,12 @@ func (s salesOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]e
|
||||
}
|
||||
|
||||
err := s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
|
||||
for _, approvableID := range approvableIDs {
|
||||
|
||||
latestApproval, err := approvalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, approvableID, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get latest approval for %d: %+v", approvableID, err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check current approval step")
|
||||
}
|
||||
|
||||
@@ -568,7 +510,7 @@ func (s salesOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]e
|
||||
|
||||
updated := make([]entity.Marketing, 0, len(approvableIDs))
|
||||
for _, id := range approvableIDs {
|
||||
marketing, err := s.GetOne(c, id)
|
||||
marketing, err := s.getOne(c, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -577,3 +519,35 @@ func (s salesOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]e
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func (s *salesOrdersService) createMarketingProductWithDelivery(ctx context.Context, marketingId uint, rp validation.CreateMarketingProduct, marketingProductRepo repository.MarketingProductRepository, invDeliveryRepo rInvMarketingDeliveryProduct.MarketingDeliveryProductRepository) error {
|
||||
|
||||
marketingProduct := &entity.MarketingProduct{
|
||||
MarketingId: marketingId,
|
||||
ProductWarehouseId: rp.ProductWarehouseId,
|
||||
Qty: rp.Qty,
|
||||
UnitPrice: rp.UnitPrice,
|
||||
AvgWeight: rp.AvgWeight,
|
||||
TotalWeight: rp.TotalWeight,
|
||||
TotalPrice: rp.TotalPrice,
|
||||
}
|
||||
if err := marketingProductRepo.CreateOne(ctx, marketingProduct, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
marketingDeliveryProduct := &entity.MarketingDeliveryProduct{
|
||||
MarketingProductId: marketingProduct.Id,
|
||||
Qty: 0,
|
||||
UnitPrice: 0,
|
||||
TotalWeight: 0,
|
||||
AvgWeight: 0,
|
||||
TotalPrice: 0,
|
||||
DeliveryDate: nil,
|
||||
VehicleNumber: rp.VehicleNumber,
|
||||
}
|
||||
if err := invDeliveryRepo.CreateOne(ctx, marketingDeliveryProduct, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ type Create struct {
|
||||
|
||||
type CreateMarketingProduct struct {
|
||||
VehicleNumber string `json:"vehicle_number" validate:"required,min=1,max=50"`
|
||||
KandangId uint `json:"kandang_id" validate:"required,gt=0"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id" validate:"required,gt=0"`
|
||||
UnitPrice float64 `json:"unit_price" validate:"required,gt=0"`
|
||||
TotalWeight float64 `json:"total_weight" validate:"required,gt=0"`
|
||||
@@ -27,12 +26,6 @@ type Update struct {
|
||||
MarketingProducts []CreateMarketingProduct `json:"marketing_products" validate:"omitempty,min=1,dive"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
}
|
||||
|
||||
type Approve struct {
|
||||
Action string `json:"action" validate:"required_strict"`
|
||||
ApprovableIds []uint `json:"approvable_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||
|
||||
Reference in New Issue
Block a user