mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 23:35:43 +00:00
Feat[BE-222]: Completed SO and DO API
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -9,8 +10,8 @@ import (
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
marketingDeliveryProductRepo "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"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/dto"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/validations"
|
||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -22,18 +23,15 @@ import (
|
||||
)
|
||||
|
||||
type DeliveryOrdersService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.DeliveryOrdersListDTO, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*dto.DeliveryOrdersListDTO, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*dto.DeliveryOrdersListDTO, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*dto.DeliveryOrdersListDTO, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.DeliveryOrders, error)
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.MarketingListDTO, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*dto.MarketingDetailDTO, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*dto.MarketingDetailDTO, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*dto.MarketingDetailDTO, error)
|
||||
}
|
||||
|
||||
type deliveryOrdersService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.DeliveryOrdersRepository
|
||||
MarketingRepo marketingRepo.MarketingRepository
|
||||
MarketingProductRepo marketingRepo.MarketingProductRepository
|
||||
MarketingDeliveryProductRepo marketingDeliveryProductRepo.MarketingDeliveryProductRepository
|
||||
@@ -41,7 +39,6 @@ type deliveryOrdersService struct {
|
||||
}
|
||||
|
||||
func NewDeliveryOrdersService(
|
||||
repo repository.DeliveryOrdersRepository,
|
||||
marketingRepo marketingRepo.MarketingRepository,
|
||||
marketingProductRepo marketingRepo.MarketingProductRepository,
|
||||
marketingDeliveryProductRepo marketingDeliveryProductRepo.MarketingDeliveryProductRepository,
|
||||
@@ -51,7 +48,6 @@ func NewDeliveryOrdersService(
|
||||
return &deliveryOrdersService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
MarketingRepo: marketingRepo,
|
||||
MarketingProductRepo: marketingProductRepo,
|
||||
MarketingDeliveryProductRepo: marketingDeliveryProductRepo,
|
||||
@@ -60,20 +56,45 @@ func NewDeliveryOrdersService(
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").
|
||||
Preload("Marketing")
|
||||
return db.
|
||||
Preload("CreatedUser").
|
||||
Preload("Customer").
|
||||
Preload("SalesPerson").
|
||||
Preload("Products.ProductWarehouse.Product").
|
||||
Preload("Products.ProductWarehouse.Warehouse").
|
||||
Preload("Products.DeliveryProduct")
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.DeliveryOrdersListDTO, int64, error) {
|
||||
func (s deliveryOrdersService) getMarketingWithDeliveries(c *fiber.Ctx, marketingId uint) (*dto.MarketingDetailDTO, error) {
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), marketingId, s.withRelations)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
|
||||
}
|
||||
|
||||
latestApproval, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, marketingId, nil)
|
||||
if err != nil {
|
||||
}
|
||||
marketing.LatestApproval = latestApproval
|
||||
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), marketingId)
|
||||
if err != nil {
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{}
|
||||
}
|
||||
|
||||
responseDTO := dto.ToMarketingDetailDTO(marketing, allDeliveryProducts)
|
||||
return &responseDTO, nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.MarketingListDTO, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
// Fetch dari Marketing, bukan DeliveryOrders
|
||||
marketings, total, err := s.MarketingRepo.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = db.Preload("CreatedUser").
|
||||
db = db.
|
||||
Preload("CreatedUser").
|
||||
Preload("Customer").
|
||||
Preload("SalesPerson").
|
||||
Preload("Products.ProductWarehouse")
|
||||
@@ -87,101 +108,58 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
||||
s.Log.Errorf("Failed to get marketings: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Load delivery products untuk setiap marketing
|
||||
result := make([]dto.DeliveryOrdersListDTO, len(marketings))
|
||||
for i, marketing := range marketings {
|
||||
// Get marketing delivery products menggunakan repository method
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), marketing.Id)
|
||||
for i := range marketings {
|
||||
latestApproval, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, marketings[i].Id, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products for marketing %d: %+v", marketing.Id, err)
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{} // Set empty slice jika gagal
|
||||
s.Log.Warnf("Failed to load approval for marketing %d: %+v", marketings[i].Id, err)
|
||||
}
|
||||
marketings[i].LatestApproval = latestApproval
|
||||
}
|
||||
|
||||
// Build response DTO
|
||||
deliveryOrderResponse := &entity.DeliveryOrders{
|
||||
MarketingId: marketing.Id,
|
||||
CreatedUser: &marketing.CreatedUser,
|
||||
Marketing: &marketing,
|
||||
DeliveryProducts: allDeliveryProducts,
|
||||
}
|
||||
|
||||
result[i] = dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
||||
result := make([]dto.MarketingListDTO, len(marketings))
|
||||
for i, marketing := range marketings {
|
||||
result[i] = dto.ToMarketingListDTO(&marketing, []entity.MarketingDeliveryProduct{})
|
||||
}
|
||||
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.DeliveryOrdersListDTO, error) {
|
||||
// Fetch Marketing by ID, bukan DeliveryOrders
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").
|
||||
Preload("Customer").
|
||||
Preload("SalesPerson").
|
||||
Preload("Products.ProductWarehouse.Product").
|
||||
Preload("Products.ProductWarehouse.Warehouse")
|
||||
})
|
||||
func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.MarketingDetailDTO, error) {
|
||||
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Marketing not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get marketing by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get marketing delivery products menggunakan repository method
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), id)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products for marketing %d: %+v", id, err)
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{} // Set empty slice jika gagal
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{}
|
||||
}
|
||||
|
||||
// Debug: Log jumlah delivery products
|
||||
s.Log.Infof("Found %d delivery products for marketing %d", len(allDeliveryProducts), id)
|
||||
if s.ApprovalSvc != nil {
|
||||
approvals, err := s.ApprovalSvc.ListByTarget(c.Context(), utils.ApprovalWorkflowMarketing, marketing.Id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
// Jika tidak ada delivery products, buat dummy data untuk testing
|
||||
if len(allDeliveryProducts) == 0 && len(marketing.Products) > 0 {
|
||||
s.Log.Infof("Creating dummy delivery products for testing")
|
||||
for i, product := range marketing.Products {
|
||||
deliveryDate := marketing.SoDate.AddDate(0, 0, i+7) // 7 hari setelah SO
|
||||
dummyDeliveryProduct := entity.MarketingDeliveryProduct{
|
||||
Id: uint(i + 1),
|
||||
MarketingProductId: product.Id,
|
||||
Qty: product.Qty / 2, // Setengah dari qty asli
|
||||
UnitPrice: product.UnitPrice,
|
||||
TotalWeight: product.TotalWeight / 2,
|
||||
AvgWeight: product.AvgWeight,
|
||||
TotalPrice: (product.Qty / 2) * product.UnitPrice,
|
||||
DeliveryDate: &deliveryDate,
|
||||
VehicleNumber: fmt.Sprintf("B%04d%s", (i+1)*1000, "ABC"),
|
||||
} else if len(approvals) > 0 {
|
||||
if marketing.LatestApproval == nil {
|
||||
latest := approvals[len(approvals)-1]
|
||||
marketing.LatestApproval = &latest
|
||||
}
|
||||
allDeliveryProducts = append(allDeliveryProducts, dummyDeliveryProduct)
|
||||
} else {
|
||||
marketing.LatestApproval = nil
|
||||
}
|
||||
s.Log.Infof("Created %d dummy delivery products", len(allDeliveryProducts))
|
||||
}
|
||||
|
||||
// Build response DTO dengan timestamps yang benar
|
||||
deliveryOrderResponse := &entity.DeliveryOrders{
|
||||
MarketingId: marketing.Id,
|
||||
CreatedUser: &marketing.CreatedUser,
|
||||
Marketing: marketing,
|
||||
DeliveryProducts: allDeliveryProducts,
|
||||
CreatedAt: marketing.CreatedAt,
|
||||
UpdatedAt: marketing.UpdatedAt,
|
||||
}
|
||||
|
||||
// Set delivery_date dari delivery products atau fallback ke marketing date
|
||||
if len(allDeliveryProducts) > 0 && allDeliveryProducts[0].DeliveryDate != nil {
|
||||
deliveryOrderResponse.DeliveryDate = *allDeliveryProducts[0].DeliveryDate
|
||||
} else {
|
||||
deliveryOrderResponse.DeliveryDate = marketing.SoDate
|
||||
}
|
||||
|
||||
responseDTO := dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
||||
responseDTO := dto.ToMarketingDetailDTO(marketing, allDeliveryProducts)
|
||||
return &responseDTO, nil
|
||||
}
|
||||
|
||||
func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*dto.DeliveryOrdersListDTO, error) {
|
||||
func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*dto.MarketingDetailDTO, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -192,14 +170,8 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var relationChecks []commonSvc.RelationCheck
|
||||
for _, requestedProduct := range req.DeliveryProducts {
|
||||
relationChecks = append(relationChecks, commonSvc.RelationCheck{
|
||||
Name: "MarketingProduct", ID: &requestedProduct.MarketingProductId, Exists: s.MarketingProductRepo.IdExists,
|
||||
})
|
||||
}
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(s.MarketingRepo.DB()))
|
||||
|
||||
latestApproval, err := approvalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, req.MarketingId, nil)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
|
||||
@@ -210,25 +182,28 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
if latestApproval.StepNumber < uint16(utils.MarketingStepSalesOrder) {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Marketing must be approved to Sales Order step before creating delivery order")
|
||||
}
|
||||
if latestApproval.StepNumber >= uint16(utils.MarketingDeliveryOrder) {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Delivery order already exists for this marketing")
|
||||
}
|
||||
if latestApproval.Action == nil || *latestApproval.Action != entity.ApprovalActionApproved {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Marketing is not approved for delivery")
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Marketing is not approved - current status: %v", *latestApproval.Action))
|
||||
}
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingDeliveryProductRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), req.MarketingId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("No marketing products found for marketing %d", req.MarketingId))
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing products")
|
||||
}
|
||||
|
||||
for _, requestedProduct := range req.DeliveryProducts {
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), req.MarketingId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("No marketing products found for marketing %d", req.MarketingId))
|
||||
}
|
||||
s.Log.Errorf("Failed to fetch marketing products: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing products")
|
||||
}
|
||||
|
||||
var foundMarketingProduct *entity.MarketingProduct
|
||||
for i := range allMarketingProducts {
|
||||
if allMarketingProducts[i].Id == requestedProduct.MarketingProductId {
|
||||
@@ -248,15 +223,13 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch delivery product")
|
||||
}
|
||||
|
||||
var itemDeliveryDate time.Time
|
||||
var itemDeliveryDate *time.Time
|
||||
if requestedProduct.DeliveryDate != "" {
|
||||
parsedDate, err := utils.ParseDateString(requestedProduct.DeliveryDate)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid delivery date format for product %d: %v", requestedProduct.MarketingProductId, err))
|
||||
}
|
||||
itemDeliveryDate = parsedDate
|
||||
} else {
|
||||
itemDeliveryDate = time.Now()
|
||||
itemDeliveryDate = &parsedDate
|
||||
}
|
||||
|
||||
deliveryProduct.Qty = requestedProduct.Qty
|
||||
@@ -264,24 +237,22 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
||||
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
||||
deliveryProduct.TotalPrice = requestedProduct.TotalPrice
|
||||
deliveryProduct.DeliveryDate = &itemDeliveryDate
|
||||
deliveryProduct.DeliveryDate = itemDeliveryDate
|
||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||
|
||||
if requestedProduct.Qty > 0 {
|
||||
if err := s.validateAndReduceProductWarehouse(c.Context(), dbTransaction, foundMarketingProduct, requestedProduct.Qty); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := marketingDeliveryProductRepositoryTx.UpdateOne(c.Context(), deliveryProduct.Id, deliveryProduct, nil); err != nil {
|
||||
s.Log.Errorf("Failed to update marketing delivery product: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||
}
|
||||
|
||||
s.Log.Infof("Updated delivery product %d: qty=%v, unitPrice=%v, totalPrice=%v", deliveryProduct.Id, requestedProduct.Qty, requestedProduct.UnitPrice, requestedProduct.TotalPrice)
|
||||
}
|
||||
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
actorID := uint(1) // TODO: ambil dari auth context
|
||||
approvalAction := entity.ApprovalActionCreated
|
||||
var notes *string
|
||||
if req.Notes != "" {
|
||||
notes = &req.Notes
|
||||
}
|
||||
approvalAction := entity.ApprovalActionApproved
|
||||
if _, err := approvalSvcTx.CreateApproval(
|
||||
c.Context(),
|
||||
utils.ApprovalWorkflowMarketing,
|
||||
@@ -289,9 +260,8 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
utils.MarketingDeliveryOrder,
|
||||
&approvalAction,
|
||||
actorID,
|
||||
notes); err != nil {
|
||||
nil); err != nil {
|
||||
if !errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
s.Log.Errorf("Failed to create delivery order approval: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order approval")
|
||||
}
|
||||
}
|
||||
@@ -300,76 +270,38 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order")
|
||||
}
|
||||
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), req.MarketingId, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").Preload("Products.DeliveryProduct")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch marketing after update: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch updated marketing")
|
||||
}
|
||||
|
||||
// Get marketing delivery products menggunakan repository method
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), req.MarketingId)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products: %+v", err)
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{} // Set empty slice jika gagal
|
||||
}
|
||||
|
||||
// Build response DTO
|
||||
deliveryOrderResponse := &entity.DeliveryOrders{
|
||||
MarketingId: req.MarketingId,
|
||||
Notes: req.Notes,
|
||||
CreatedUser: &marketing.CreatedUser,
|
||||
Marketing: marketing,
|
||||
DeliveryProducts: allDeliveryProducts,
|
||||
}
|
||||
|
||||
responseDTO := dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
||||
return &responseDTO, nil
|
||||
return s.getMarketingWithDeliveries(c, req.MarketingId)
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*dto.DeliveryOrdersListDTO, error) {
|
||||
func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*dto.MarketingDetailDTO, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate bahwa marketing ID yang di-update ada (id parameter adalah marketing_id untuk delivery orders)
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Marketing", ID: &id, Exists: s.MarketingRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate delivery products jika ada
|
||||
if len(req.DeliveryProducts) > 0 {
|
||||
for _, requestedProduct := range req.DeliveryProducts {
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "MarketingProduct", ID: &requestedProduct.MarketingProductId, Exists: s.MarketingProductRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
err := s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
err := s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingDeliveryProductRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
|
||||
// Update delivery products jika ada dalam request
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing products")
|
||||
}
|
||||
|
||||
if len(req.DeliveryProducts) > 0 {
|
||||
for _, requestedProduct := range req.DeliveryProducts {
|
||||
// Validate bahwa marketing product ada untuk marketing ini
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("No marketing products found for marketing %d", id))
|
||||
}
|
||||
s.Log.Errorf("Failed to fetch marketing products: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing products")
|
||||
}
|
||||
|
||||
var foundMarketingProduct *entity.MarketingProduct
|
||||
for i := range allMarketingProducts {
|
||||
@@ -382,7 +314,6 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, i
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Marketing product %d not found for this marketing", requestedProduct.MarketingProductId))
|
||||
}
|
||||
|
||||
// Get existing delivery product
|
||||
deliveryProduct, err := marketingDeliveryProductRepositoryTx.GetByMarketingProductID(c.Context(), foundMarketingProduct.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -391,157 +322,101 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, i
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch delivery product")
|
||||
}
|
||||
|
||||
// Parse delivery date
|
||||
var itemDeliveryDate time.Time
|
||||
var itemDeliveryDate *time.Time
|
||||
if requestedProduct.DeliveryDate != "" {
|
||||
parsedDate, err := utils.ParseDateString(requestedProduct.DeliveryDate)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid delivery date format for product %d: %v", requestedProduct.MarketingProductId, err))
|
||||
}
|
||||
itemDeliveryDate = parsedDate
|
||||
itemDeliveryDate = &parsedDate
|
||||
} else if deliveryProduct.DeliveryDate != nil {
|
||||
itemDeliveryDate = *deliveryProduct.DeliveryDate
|
||||
} else {
|
||||
itemDeliveryDate = time.Now()
|
||||
itemDeliveryDate = deliveryProduct.DeliveryDate
|
||||
}
|
||||
|
||||
// Update delivery product
|
||||
oldQty := deliveryProduct.Qty
|
||||
deliveryProduct.Qty = requestedProduct.Qty
|
||||
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
||||
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
||||
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
||||
deliveryProduct.TotalPrice = requestedProduct.TotalPrice
|
||||
deliveryProduct.DeliveryDate = &itemDeliveryDate
|
||||
deliveryProduct.DeliveryDate = itemDeliveryDate
|
||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||
|
||||
if err := marketingDeliveryProductRepositoryTx.UpdateOne(c.Context(), deliveryProduct.Id, deliveryProduct, nil); err != nil {
|
||||
s.Log.Errorf("Failed to update marketing delivery product: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||
qtyChange := requestedProduct.Qty - oldQty
|
||||
if qtyChange > 0 {
|
||||
if err := s.validateAndReduceProductWarehouse(c.Context(), dbTransaction, foundMarketingProduct, qtyChange); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if qtyChange < 0 {
|
||||
if err := s.restoreProductWarehouseStock(c.Context(), dbTransaction, foundMarketingProduct, -qtyChange); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
s.Log.Infof("Updated delivery product %d: qty=%v, unitPrice=%v, totalPrice=%v", deliveryProduct.Id, requestedProduct.Qty, requestedProduct.UnitPrice, requestedProduct.TotalPrice)
|
||||
if err := marketingDeliveryProductRepositoryTx.UpdateOne(c.Context(), deliveryProduct.Id, deliveryProduct, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Fetch updated marketing with delivery products
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").Preload("Products.DeliveryProduct")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch marketing after update: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch updated marketing")
|
||||
}
|
||||
|
||||
// Get marketing delivery products menggunakan repository method
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), id)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products: %+v", err)
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{} // Set empty slice jika gagal
|
||||
}
|
||||
|
||||
// Build response DTO
|
||||
deliveryOrderResponse := &entity.DeliveryOrders{
|
||||
MarketingId: id,
|
||||
Notes: req.Notes,
|
||||
CreatedUser: &marketing.CreatedUser,
|
||||
Marketing: marketing,
|
||||
DeliveryProducts: allDeliveryProducts,
|
||||
}
|
||||
|
||||
responseDTO := dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
||||
return &responseDTO, nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.DeliveryOrders, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var action entity.ApprovalAction
|
||||
switch req.Action {
|
||||
case "APPROVED":
|
||||
action = entity.ApprovalActionApproved
|
||||
case "REJECTED":
|
||||
action = entity.ApprovalActionRejected
|
||||
default:
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "action must be APPROVED or REJECTED")
|
||||
}
|
||||
|
||||
approvableIDs := utils.UniqueUintSlice(req.ApprovableIds)
|
||||
if len(approvableIDs) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "approvable_ids must contain at least one id")
|
||||
}
|
||||
|
||||
// Validate semua delivery order ada
|
||||
for _, id := range approvableIDs {
|
||||
_, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Delivery order %d not found", id))
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get delivery order %d: %+v", id, err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get delivery order %d", id))
|
||||
}
|
||||
}
|
||||
|
||||
err := s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTx *gorm.DB) error {
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTx))
|
||||
|
||||
for _, approvableID := range approvableIDs {
|
||||
actorID := uint(1) // TODO: get from auth context
|
||||
if _, err := approvalSvc.CreateApproval(
|
||||
c.Context(),
|
||||
utils.ApprovalWorkflowMarketing,
|
||||
approvableID,
|
||||
utils.MarketingDeliveryOrder,
|
||||
&action,
|
||||
actorID,
|
||||
req.Notes,
|
||||
); err != nil {
|
||||
s.Log.Errorf("Failed to create approval for %d: %+v", approvableID, err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to record approval")
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery order")
|
||||
}
|
||||
|
||||
updated := make([]entity.DeliveryOrders, 0, len(approvableIDs))
|
||||
for _, id := range approvableIDs {
|
||||
deliveryOrder, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Delivery order %d not found", id))
|
||||
}
|
||||
s.Log.Errorf("Failed to get delivery order %d: %+v", id, err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get delivery order %d", id))
|
||||
}
|
||||
updated = append(updated, *deliveryOrder)
|
||||
}
|
||||
|
||||
return updated, nil
|
||||
return s.getMarketingWithDeliveries(c, id)
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
func (s deliveryOrdersService) validateAndReduceProductWarehouse(ctx context.Context, tx *gorm.DB, marketingProduct *entity.MarketingProduct, qtyDeliver float64) error {
|
||||
if marketingProduct == nil || marketingProduct.ProductWarehouseId == 0 {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Product warehouse not found")
|
||||
}
|
||||
|
||||
pwRepo := productWarehouseRepo.NewProductWarehouseRepository(tx)
|
||||
|
||||
pw, err := pwRepo.GetByID(ctx, marketingProduct.ProductWarehouseId, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "DeliveryOrders not found")
|
||||
return fiber.NewError(fiber.StatusNotFound, "Product warehouse not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to delete deliveryOrders: %+v", err)
|
||||
return err
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check stock")
|
||||
}
|
||||
|
||||
if pw.Quantity < qtyDeliver {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock for warehouse - available: %.2f, requested: %.2f", pw.Quantity, qtyDeliver))
|
||||
}
|
||||
|
||||
pw.Quantity = pw.Quantity - qtyDeliver
|
||||
if err := pwRepo.UpdateOne(ctx, pw.Id, pw, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update stock")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) restoreProductWarehouseStock(ctx context.Context, tx *gorm.DB, marketingProduct *entity.MarketingProduct, qtyRestore float64) error {
|
||||
if marketingProduct == nil || marketingProduct.ProductWarehouseId == 0 {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Product warehouse not found")
|
||||
}
|
||||
|
||||
pwRepo := productWarehouseRepo.NewProductWarehouseRepository(tx)
|
||||
pw, err := pwRepo.GetByID(ctx, marketingProduct.ProductWarehouseId, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Product warehouse not found")
|
||||
}
|
||||
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check stock")
|
||||
}
|
||||
|
||||
pw.Quantity = pw.Quantity + qtyRestore
|
||||
if err := pwRepo.UpdateOne(ctx, pw.Id, pw, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update stock")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user