|
|
|
@@ -35,6 +35,7 @@ type deliveryOrdersService struct {
|
|
|
|
|
Validate *validator.Validate
|
|
|
|
|
Repository repository.DeliveryOrdersRepository
|
|
|
|
|
MarketingRepo marketingRepo.MarketingRepository
|
|
|
|
|
MarketingProductRepo marketingRepo.MarketingProductRepository
|
|
|
|
|
MarketingDeliveryProductRepo marketingDeliveryProductRepo.MarketingDeliveryProductRepository
|
|
|
|
|
ApprovalSvc commonSvc.ApprovalService
|
|
|
|
|
}
|
|
|
|
@@ -42,6 +43,7 @@ type deliveryOrdersService struct {
|
|
|
|
|
func NewDeliveryOrdersService(
|
|
|
|
|
repo repository.DeliveryOrdersRepository,
|
|
|
|
|
marketingRepo marketingRepo.MarketingRepository,
|
|
|
|
|
marketingProductRepo marketingRepo.MarketingProductRepository,
|
|
|
|
|
marketingDeliveryProductRepo marketingDeliveryProductRepo.MarketingDeliveryProductRepository,
|
|
|
|
|
approvalSvc commonSvc.ApprovalService,
|
|
|
|
|
validate *validator.Validate,
|
|
|
|
@@ -51,6 +53,7 @@ func NewDeliveryOrdersService(
|
|
|
|
|
Validate: validate,
|
|
|
|
|
Repository: repo,
|
|
|
|
|
MarketingRepo: marketingRepo,
|
|
|
|
|
MarketingProductRepo: marketingProductRepo,
|
|
|
|
|
MarketingDeliveryProductRepo: marketingDeliveryProductRepo,
|
|
|
|
|
ApprovalSvc: approvalSvc,
|
|
|
|
|
}
|
|
|
|
@@ -89,7 +92,7 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
|
|
|
|
result := make([]dto.DeliveryOrdersListDTO, len(marketings))
|
|
|
|
|
for i, marketing := range marketings {
|
|
|
|
|
// Get marketing delivery products
|
|
|
|
|
var deliveryProducts []entity.MarketingDeliveryProduct
|
|
|
|
|
var allDeliveryProducts []entity.MarketingDeliveryProduct
|
|
|
|
|
if err := s.Repository.DB().WithContext(c.Context()).
|
|
|
|
|
Preload("MarketingProduct").
|
|
|
|
|
Where("marketing_product_id IN (?)",
|
|
|
|
@@ -97,20 +100,20 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
|
|
|
|
Model(&entity.MarketingProduct{}).
|
|
|
|
|
Select("id").
|
|
|
|
|
Where("marketing_id = ?", marketing.Id)).
|
|
|
|
|
Find(&deliveryProducts).Error; err != nil {
|
|
|
|
|
Find(&allDeliveryProducts).Error; err != nil {
|
|
|
|
|
s.Log.Errorf("Failed to load delivery products for marketing %d: %+v", marketing.Id, err)
|
|
|
|
|
// Continue without products
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create dummy DeliveryOrders untuk dto mapping
|
|
|
|
|
dummyDO := &entity.DeliveryOrders{
|
|
|
|
|
// Build response DTO
|
|
|
|
|
deliveryOrderResponse := &entity.DeliveryOrders{
|
|
|
|
|
MarketingId: marketing.Id,
|
|
|
|
|
CreatedUser: &marketing.CreatedUser,
|
|
|
|
|
Marketing: &marketing,
|
|
|
|
|
DeliveryProducts: deliveryProducts,
|
|
|
|
|
DeliveryProducts: allDeliveryProducts,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result[i] = dto.ToDeliveryOrdersListDTOWithProducts(*dummyDO, deliveryProducts)
|
|
|
|
|
result[i] = dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, total, nil
|
|
|
|
@@ -133,7 +136,7 @@ func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.DeliveryOrder
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get marketing delivery products
|
|
|
|
|
var deliveryProducts []entity.MarketingDeliveryProduct
|
|
|
|
|
var allDeliveryProducts []entity.MarketingDeliveryProduct
|
|
|
|
|
if err := s.Repository.DB().WithContext(c.Context()).
|
|
|
|
|
Preload("MarketingProduct").
|
|
|
|
|
Where("marketing_product_id IN (?)",
|
|
|
|
@@ -141,21 +144,21 @@ func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.DeliveryOrder
|
|
|
|
|
Model(&entity.MarketingProduct{}).
|
|
|
|
|
Select("id").
|
|
|
|
|
Where("marketing_id = ?", marketing.Id)).
|
|
|
|
|
Find(&deliveryProducts).Error; err != nil {
|
|
|
|
|
Find(&allDeliveryProducts).Error; err != nil {
|
|
|
|
|
s.Log.Errorf("Failed to load delivery products for marketing %d: %+v", marketing.Id, err)
|
|
|
|
|
// Continue without products
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create dummy DeliveryOrders untuk dto mapping
|
|
|
|
|
dummyDO := &entity.DeliveryOrders{
|
|
|
|
|
// Build response DTO
|
|
|
|
|
deliveryOrderResponse := &entity.DeliveryOrders{
|
|
|
|
|
MarketingId: marketing.Id,
|
|
|
|
|
CreatedUser: &marketing.CreatedUser,
|
|
|
|
|
Marketing: marketing,
|
|
|
|
|
DeliveryProducts: deliveryProducts,
|
|
|
|
|
DeliveryProducts: allDeliveryProducts,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := dto.ToDeliveryOrdersListDTOWithProducts(*dummyDO, deliveryProducts)
|
|
|
|
|
return &result, nil
|
|
|
|
|
responseDTO := dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
|
|
|
|
return &responseDTO, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*dto.DeliveryOrdersListDTO, error) {
|
|
|
|
@@ -163,88 +166,93 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate marketing exists
|
|
|
|
|
_, err := s.MarketingRepo.GetByID(c.Context(), req.MarketingId, nil)
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Marketing not found")
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.Log.Errorf("Failed to fetch marketing %d: %+v", req.MarketingId, err)
|
|
|
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
|
|
|
|
|
if err := commonSvc.EnsureRelations(c.Context(),
|
|
|
|
|
commonSvc.RelationCheck{Name: "Marketing", ID: &req.MarketingId, Exists: s.MarketingRepo.IdExists},
|
|
|
|
|
); err != nil {
|
|
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate marketing approval status - harus sudah di approve ke step Sales Order
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if latestApproval == nil {
|
|
|
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Marketing has not been submitted for approval")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cek apakah status approval sudah Sales Order (step 2) atau lebih
|
|
|
|
|
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.Action == nil || *latestApproval.Action != entity.ApprovalActionApproved {
|
|
|
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Marketing is not approved for delivery")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate semua delivery products ada dan update mereka
|
|
|
|
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTx *gorm.DB) error {
|
|
|
|
|
for _, product := range req.DeliveryProducts {
|
|
|
|
|
// Fetch marketing_product terlebih dahulu untuk pastikan punya marketing_id yang sama
|
|
|
|
|
var marketingProduct entity.MarketingProduct
|
|
|
|
|
if err := dbTx.Where("id = ? AND marketing_id = ?", product.MarketingProductId, req.MarketingId).
|
|
|
|
|
First(&marketingProduct).Error; err != nil {
|
|
|
|
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
|
|
|
|
|
|
|
|
|
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
|
|
|
|
marketingDeliveryProductRepositoryTx := marketingDeliveryProductRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
|
|
|
|
|
|
|
|
|
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("Marketing product %d not found for this marketing", product.MarketingProductId))
|
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("No marketing products found for marketing %d", req.MarketingId))
|
|
|
|
|
}
|
|
|
|
|
s.Log.Errorf("Failed to fetch marketing product: %+v", err)
|
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing product")
|
|
|
|
|
s.Log.Errorf("Failed to fetch marketing products: %+v", err)
|
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing products")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch marketing_delivery_product by marketing_product_id
|
|
|
|
|
var mdp entity.MarketingDeliveryProduct
|
|
|
|
|
if err := dbTx.Where("marketing_product_id = ?", marketingProduct.Id).
|
|
|
|
|
First(&mdp).Error; err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Delivery product for marketing product %d not found", product.MarketingProductId))
|
|
|
|
|
var foundMarketingProduct *entity.MarketingProduct
|
|
|
|
|
for i := range allMarketingProducts {
|
|
|
|
|
if allMarketingProducts[i].Id == requestedProduct.MarketingProductId {
|
|
|
|
|
foundMarketingProduct = &allMarketingProducts[i]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if foundMarketingProduct == nil {
|
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Marketing product %d not found for this marketing", requestedProduct.MarketingProductId))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deliveryProduct, err := marketingDeliveryProductRepositoryTx.GetByMarketingProductID(c.Context(), foundMarketingProduct.Id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Delivery product for marketing product %d not found", requestedProduct.MarketingProductId))
|
|
|
|
|
}
|
|
|
|
|
s.Log.Errorf("Failed to fetch marketing delivery product: %+v", err)
|
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch delivery product")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse delivery date per item (jika ada), atau gunakan default
|
|
|
|
|
itemDeliveryDate := time.Now()
|
|
|
|
|
if product.DeliveryDate != "" {
|
|
|
|
|
parsedItemDate, err := time.Parse("2006-01-02", product.DeliveryDate)
|
|
|
|
|
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", product.MarketingProductId))
|
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid delivery date format for product %d: %v", requestedProduct.MarketingProductId, err))
|
|
|
|
|
}
|
|
|
|
|
itemDeliveryDate = parsedItemDate
|
|
|
|
|
itemDeliveryDate = parsedDate
|
|
|
|
|
} else {
|
|
|
|
|
itemDeliveryDate = time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update dengan data dari request - PASTIKAN UPDATE LANGSUNG KE FIELD
|
|
|
|
|
updates := map[string]interface{}{
|
|
|
|
|
"qty": product.Qty,
|
|
|
|
|
"unit_price": product.UnitPrice,
|
|
|
|
|
"avg_weight": product.AvgWeight,
|
|
|
|
|
"total_weight": product.TotalWeight,
|
|
|
|
|
"total_price": product.TotalPrice,
|
|
|
|
|
"delivery_date": &itemDeliveryDate,
|
|
|
|
|
"vehicle_number": product.VehicleNumber,
|
|
|
|
|
}
|
|
|
|
|
deliveryProduct.Qty = requestedProduct.Qty
|
|
|
|
|
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
|
|
|
|
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
|
|
|
|
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
|
|
|
|
deliveryProduct.TotalPrice = requestedProduct.TotalPrice
|
|
|
|
|
deliveryProduct.DeliveryDate = &itemDeliveryDate
|
|
|
|
|
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
|
|
|
|
|
|
|
|
|
if err := dbTx.Model(&mdp).Updates(updates).Error; err != nil {
|
|
|
|
|
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 MDP %d: qty=%v, unitPrice=%v, totalPrice=%v", mdp.Id, product.Qty, product.UnitPrice, product.TotalPrice)
|
|
|
|
|
s.Log.Infof("Updated delivery product %d: qty=%v, unitPrice=%v, totalPrice=%v", deliveryProduct.Id, requestedProduct.Qty, requestedProduct.UnitPrice, requestedProduct.TotalPrice)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
@@ -254,9 +262,9 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch marketing dengan delivery products yang sudah di-update
|
|
|
|
|
// Fetch marketing dengan delivery products yang sudah di-updated
|
|
|
|
|
marketing, err := s.MarketingRepo.GetByID(c.Context(), req.MarketingId, func(db *gorm.DB) *gorm.DB {
|
|
|
|
|
return db.Preload("CreatedUser").Preload("Products")
|
|
|
|
|
return db.Preload("CreatedUser").Preload("Products.DeliveryProduct")
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.Log.Errorf("Failed to fetch marketing after update: %+v", err)
|
|
|
|
@@ -264,30 +272,30 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get marketing delivery products
|
|
|
|
|
var deliveryProducts []entity.MarketingDeliveryProduct
|
|
|
|
|
if err := s.Repository.DB().WithContext(c.Context()).
|
|
|
|
|
var allDeliveryProducts []entity.MarketingDeliveryProduct
|
|
|
|
|
if err := s.MarketingDeliveryProductRepo.DB().WithContext(c.Context()).
|
|
|
|
|
Preload("MarketingProduct").
|
|
|
|
|
Where("marketing_product_id IN (?)",
|
|
|
|
|
s.Repository.DB().WithContext(c.Context()).
|
|
|
|
|
s.MarketingProductRepo.DB().WithContext(c.Context()).
|
|
|
|
|
Model(&entity.MarketingProduct{}).
|
|
|
|
|
Select("id").
|
|
|
|
|
Where("marketing_id = ?", req.MarketingId)).
|
|
|
|
|
Find(&deliveryProducts).Error; err != nil {
|
|
|
|
|
Find(&allDeliveryProducts).Error; err != nil {
|
|
|
|
|
s.Log.Errorf("Failed to load delivery products: %+v", err)
|
|
|
|
|
// Continue tanpa delivery products
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create dummy DeliveryOrders untuk dipakai dto mapping
|
|
|
|
|
dummyDO := &entity.DeliveryOrders{
|
|
|
|
|
// Build response DTO
|
|
|
|
|
deliveryOrderResponse := &entity.DeliveryOrders{
|
|
|
|
|
MarketingId: req.MarketingId,
|
|
|
|
|
Notes: req.Notes,
|
|
|
|
|
CreatedUser: &marketing.CreatedUser,
|
|
|
|
|
Marketing: marketing,
|
|
|
|
|
DeliveryProducts: deliveryProducts,
|
|
|
|
|
DeliveryProducts: allDeliveryProducts,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := dto.ToDeliveryOrdersListDTOWithProducts(*dummyDO, deliveryProducts)
|
|
|
|
|
return &result, nil
|
|
|
|
|
responseDTO := dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
|
|
|
|
return &responseDTO, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.DeliveryOrders, error) {
|
|
|
|
|