Feat[BE-222]: Completed SO and DO API

This commit is contained in:
aguhh18
2025-11-17 07:16:07 +07:00
parent 903b114315
commit 7905bdb0d7
16 changed files with 600 additions and 1011 deletions
@@ -39,7 +39,7 @@ func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.DeliveryOrdersListDTO]{
JSON(response.SuccessWithPaginate[dto.MarketingListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all deliveryOrderss successfully",
@@ -122,44 +122,3 @@ func (u *DeliveryOrdersController) UpdateOne(c *fiber.Ctx) error {
Data: result,
})
}
func (u *DeliveryOrdersController) DeleteOne(c *fiber.Ctx) error {
param := c.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
if err := u.DeliveryOrdersService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete deliveryOrders successfully",
})
}
func (u *DeliveryOrdersController) Approval(c *fiber.Ctx) error {
req := new(validation.Approve)
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
results, err := u.DeliveryOrdersService.Approval(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Submit delivery order approval successfully",
Data: results,
})
}
@@ -7,83 +7,100 @@ import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
productwarehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/dto"
customerDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type MarketingBaseDTO struct {
Id uint `json:"id"`
SoNumber string `json:"so_number"`
SoDate time.Time `json:"so_date"`
Notes string `json:"notes,omitempty"`
}
type MarketingListDTO struct {
MarketingBaseDTO
Customer *customerDTO.CustomerBaseDTO `json:"customer,omitempty"`
SalesPerson *userDTO.UserBaseDTO `json:"sales_person,omitempty"`
SoDocs string `json:"so_docs,omitempty"`
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LatestApproval *approvalDTO.ApprovalBaseDTO `json:"latest_approval,omitempty"`
}
type MarketingDetailDTO struct {
MarketingBaseDTO
Customer *customerDTO.CustomerBaseDTO `json:"customer,omitempty"`
SalesPerson *userDTO.UserBaseDTO `json:"sales_person,omitempty"`
SoDocs string `json:"so_docs,omitempty"`
SalesOrder []MarketingProductDTO `json:"sales_order,omitempty"`
DeliveryOrder []DeliveryGroupDTO `json:"delivery_order,omitempty"`
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LatestApproval *approvalDTO.ApprovalBaseDTO `json:"latest_approval,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"`
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"`
ProductWarehouse *productwarehouseDTO.ProductWarehousNestedDTO `json:"product_warehouse,omitempty"`
}
type DeliveryItemDTO struct {
ProductWarehouse *productwarehouseDTO.ProductWarehousNestedDTO `json:"product_warehouse"`
Qty float64 `json:"qty"`
UnitPrice float64 `json:"unit_price"`
TotalWeight float64 `json:"total_weight"`
AvgWeight float64 `json:"avg_weight"`
TotalPrice float64 `json:"total_price"`
VehicleNumber string `json:"vehicle_number"`
}
// DTO untuk grouping delivery products berdasarkan warehouse dan tanggal
type DeliveryGroupDTO struct {
WarehouseId uint `json:"warehouse_id"`
WarehouseName string `json:"warehouse_name"`
DeliveryDate *time.Time `json:"delivery_date"`
VehicleNumber string `json:"vehicle_number"`
TotalQty float64 `json:"total_qty"`
TotalWeight float64 `json:"total_weight"`
TotalPrice float64 `json:"total_price"`
}
// DTO untuk Delivery Order (DO) - berisi data delivery yang sudah digroup
type DeliveryOrderDTO struct {
DeliveryGroups []DeliveryGroupDTO `json:"delivery_groups"`
}
type DeliveryOrdersBaseDTO struct {
Id uint `json:"id,omitempty"`
DeliveryNumber *string `json:"delivery_number,omitempty"`
DeliveryDate *time.Time `json:"delivery_date,omitempty"`
MarketingId uint `json:"marketing_id"`
Notes string `json:"notes,omitempty"`
DoNumber string `json:"do_number"`
DeliveryDate *time.Time `json:"delivery_date"`
Warehouse *productwarehouseDTO.WarehouseBaseDTO `json:"warehouse,omitempty"`
Deliveries []DeliveryItemDTO `json:"deliveries"`
}
type MarketingProductDTO struct {
Id uint `json:"id"`
MarketingId uint `json:"marketing_id"`
ProductWarehouseId uint `json:"product_warehouse_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"`
// Add product relation if needed
Id uint `json:"id"`
MarketingId uint `json:"marketing_id"`
ProductWarehouseId uint `json:"product_warehouse_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"`
VehicleNumber string `json:"vehicle_number,omitempty"`
}
type MarketingBaseDTO struct {
Id uint `json:"id"`
SoNumber string `json:"so_number"`
SoDate time.Time `json:"so_date"`
Products []MarketingProductDTO `json:"products,omitempty"`
func ToMarketingBaseDTO(marketing *entity.Marketing) MarketingBaseDTO {
return MarketingBaseDTO{
Id: marketing.Id,
SoNumber: marketing.SoNumber,
SoDate: marketing.SoDate,
Notes: marketing.Notes,
}
}
type DeliveryOrdersListDTO struct {
DeliveryOrdersBaseDTO
SalesOrder *MarketingBaseDTO `json:"sales_order,omitempty"` // SO - Sales Order data
DeliveryOrder *DeliveryOrderDTO `json:"delivery_order,omitempty"` // DO - Delivery Order data (grouped)
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Approval *approvalDTO.ApprovalBaseDTO `json:"approval,omitempty"`
}
type DeliveryOrdersDetailDTO struct {
DeliveryOrdersListDTO
}
// === Mapper Functions ===
func ToMarketingProductDTO(e entity.MarketingProduct) MarketingProductDTO {
var productWarehouse *productwarehouseDTO.ProductWarehousNestedDTO
if e.ProductWarehouse.Id != 0 {
mapped := productwarehouseDTO.ToProductWarehouseNestedDTO(e.ProductWarehouse)
productWarehouse = &mapped
}
return MarketingProductDTO{
Id: e.Id,
MarketingId: e.MarketingId,
@@ -93,6 +110,8 @@ func ToMarketingProductDTO(e entity.MarketingProduct) MarketingProductDTO {
AvgWeight: e.AvgWeight,
TotalWeight: e.TotalWeight,
TotalPrice: e.TotalPrice,
ProductWarehouse: productWarehouse,
VehicleNumber: getVehicleNumber(e),
}
}
@@ -110,92 +129,67 @@ func ToMarketingDeliveryProductDTO(e entity.MarketingDeliveryProduct) MarketingD
}
}
func ToDeliveryOrdersBaseDTO(e entity.DeliveryOrders) DeliveryOrdersBaseDTO {
var deliveryNumber *string
if e.DeliveryNumber != "" {
deliveryNumber = &e.DeliveryNumber
}
return DeliveryOrdersBaseDTO{
Id: e.Id,
DeliveryNumber: deliveryNumber,
DeliveryDate: &e.DeliveryDate,
MarketingId: e.MarketingId,
Notes: e.Notes,
}
}
func ToDeliveryOrdersListDTO(e entity.DeliveryOrders) DeliveryOrdersListDTO {
func ToMarketingListDTO(marketing *entity.Marketing, deliveryProducts []entity.MarketingDeliveryProduct) MarketingListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser != nil && e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(*e.CreatedUser)
if marketing.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(marketing.CreatedUser)
createdUser = &mapped
}
var marketing *MarketingBaseDTO
if e.Marketing != nil && e.Marketing.Id != 0 {
var marketingProducts []MarketingProductDTO
if len(e.Marketing.Products) > 0 {
marketingProducts = make([]MarketingProductDTO, len(e.Marketing.Products))
for i, product := range e.Marketing.Products {
marketingProducts[i] = ToMarketingProductDTO(product)
}
}
marketing = &MarketingBaseDTO{
Id: e.Marketing.Id,
SoNumber: e.Marketing.SoNumber,
SoDate: e.Marketing.SoDate,
Products: marketingProducts,
}
var customer *customerDTO.CustomerBaseDTO
if marketing.Customer.Id != 0 {
mapped := customerDTO.ToCustomerBaseDTO(marketing.Customer)
customer = &mapped
}
var deliveryProductsDTOs []MarketingDeliveryProductDTO
if len(e.DeliveryProducts) > 0 {
deliveryProductsDTOs = make([]MarketingDeliveryProductDTO, len(e.DeliveryProducts))
for i, dp := range e.DeliveryProducts {
deliveryProductsDTOs[i] = ToMarketingDeliveryProductDTO(dp)
}
var salesPerson *userDTO.UserBaseDTO
if marketing.SalesPerson.Id != 0 {
mapped := userDTO.ToUserBaseDTO(marketing.SalesPerson)
salesPerson = &mapped
}
// Group delivery products by warehouse and delivery date
deliveryGroups := groupDeliveryProducts(deliveryProductsDTOs)
var latestApproval *approvalDTO.ApprovalBaseDTO
if marketing.LatestApproval != nil {
mapped := approvalDTO.ToApprovalDTO(*marketing.LatestApproval)
latestApproval = &mapped
}
// Create delivery order DTO with summary
deliveryOrder := createDeliveryOrderDTO(deliveryGroups)
return DeliveryOrdersListDTO{
DeliveryOrdersBaseDTO: ToDeliveryOrdersBaseDTO(e),
SalesOrder: marketing,
DeliveryOrder: deliveryOrder,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
return MarketingListDTO{
MarketingBaseDTO: ToMarketingBaseDTO(marketing),
Customer: customer,
SalesPerson: salesPerson,
SoDocs: marketing.SoDocs,
CreatedUser: createdUser,
CreatedAt: marketing.CreatedAt,
UpdatedAt: marketing.UpdatedAt,
LatestApproval: latestApproval,
}
}
func ToDeliveryOrdersListDTOWithProducts(e entity.DeliveryOrders, deliveryProducts []entity.MarketingDeliveryProduct) DeliveryOrdersListDTO {
func ToMarketingDetailDTO(marketing *entity.Marketing, deliveryProducts []entity.MarketingDeliveryProduct) MarketingDetailDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser != nil && e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(*e.CreatedUser)
if marketing.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(marketing.CreatedUser)
createdUser = &mapped
}
var marketing *MarketingBaseDTO
if e.Marketing != nil && e.Marketing.Id != 0 {
var marketingProducts []MarketingProductDTO
if len(e.Marketing.Products) > 0 {
marketingProducts = make([]MarketingProductDTO, len(e.Marketing.Products))
for i, product := range e.Marketing.Products {
marketingProducts[i] = ToMarketingProductDTO(product)
}
}
var customer *customerDTO.CustomerBaseDTO
if marketing.Customer.Id != 0 {
mapped := customerDTO.ToCustomerBaseDTO(marketing.Customer)
customer = &mapped
}
marketing = &MarketingBaseDTO{
Id: e.Marketing.Id,
SoNumber: e.Marketing.SoNumber,
SoDate: e.Marketing.SoDate,
Products: marketingProducts,
var salesPerson *userDTO.UserBaseDTO
if marketing.SalesPerson.Id != 0 {
mapped := userDTO.ToUserBaseDTO(marketing.SalesPerson)
salesPerson = &mapped
}
var salesOrderProducts []MarketingProductDTO
if len(marketing.Products) > 0 {
salesOrderProducts = make([]MarketingProductDTO, len(marketing.Products))
for i, product := range marketing.Products {
salesOrderProducts[i] = ToMarketingProductDTO(product)
}
}
@@ -205,87 +199,108 @@ func ToDeliveryOrdersListDTOWithProducts(e entity.DeliveryOrders, deliveryProduc
for i, dp := range deliveryProducts {
deliveryProductsDTOs[i] = ToMarketingDeliveryProductDTO(dp)
}
deliveryProductsDTOs = enrichDeliveryProductDTOsWithWarehouse(deliveryProductsDTOs, marketing)
}
// Group delivery products by warehouse and delivery date
deliveryGroups := groupDeliveryProducts(deliveryProductsDTOs)
deliveryGroups := groupDeliveryProducts(deliveryProductsDTOs, marketing.SoNumber)
// Create delivery order DTO with summary
deliveryOrder := createDeliveryOrderDTO(deliveryGroups)
var latestApproval *approvalDTO.ApprovalBaseDTO
if marketing.LatestApproval != nil {
mapped := approvalDTO.ToApprovalDTO(*marketing.LatestApproval)
latestApproval = &mapped
}
return DeliveryOrdersListDTO{
DeliveryOrdersBaseDTO: ToDeliveryOrdersBaseDTO(e),
SalesOrder: marketing,
DeliveryOrder: deliveryOrder,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
return MarketingDetailDTO{
MarketingBaseDTO: ToMarketingBaseDTO(marketing),
SoDocs: marketing.SoDocs,
Customer: customer,
SalesPerson: salesPerson,
SalesOrder: salesOrderProducts,
DeliveryOrder: deliveryGroups,
CreatedUser: createdUser,
CreatedAt: marketing.CreatedAt,
UpdatedAt: marketing.UpdatedAt,
LatestApproval: latestApproval,
}
}
func ToDeliveryOrdersListDTOs(e []entity.DeliveryOrders) []DeliveryOrdersListDTO {
result := make([]DeliveryOrdersListDTO, len(e))
for i, r := range e {
result[i] = ToDeliveryOrdersListDTO(r)
func ToMarketingListDTOs(marketings []entity.Marketing) []MarketingListDTO {
result := make([]MarketingListDTO, len(marketings))
for i, m := range marketings {
result[i] = ToMarketingListDTO(&m, []entity.MarketingDeliveryProduct{})
}
return result
}
func ToDeliveryOrdersDetailDTO(e entity.DeliveryOrders) DeliveryOrdersDetailDTO {
return DeliveryOrdersDetailDTO{
DeliveryOrdersListDTO: ToDeliveryOrdersListDTO(e),
func enrichDeliveryProductDTOsWithWarehouse(deliveryProductDTOs []MarketingDeliveryProductDTO, marketing *entity.Marketing) []MarketingDeliveryProductDTO {
if len(deliveryProductDTOs) == 0 || marketing == nil || len(marketing.Products) == 0 {
return deliveryProductDTOs
}
productMap := make(map[uint]*entity.MarketingProduct)
for i := range marketing.Products {
productMap[marketing.Products[i].Id] = &marketing.Products[i]
}
for i := range deliveryProductDTOs {
if product, exists := productMap[deliveryProductDTOs[i].MarketingProductId]; exists {
if product.ProductWarehouse.Id != 0 {
mapped := productwarehouseDTO.ToProductWarehouseNestedDTO(product.ProductWarehouse)
deliveryProductDTOs[i].ProductWarehouse = &mapped
}
}
}
return deliveryProductDTOs
}
// groupDeliveryProducts groups delivery products by warehouse and delivery date
func groupDeliveryProducts(products []MarketingDeliveryProductDTO) []DeliveryGroupDTO {
// Create a map to group products
func groupDeliveryProducts(products []MarketingDeliveryProductDTO, soNumber string) []DeliveryGroupDTO {
groupMap := make(map[string]*DeliveryGroupDTO)
for _, product := range products {
// Create unique key for grouping (warehouse_id + delivery_date)
// Since we're working with DTO, we need to handle the warehouse id differently
warehouseId := uint(0)
warehouseName := ""
// Extract warehouse info from product (assuming it might be available in future)
// For now, we'll use a simple grouping by delivery date and vehicle number
var key string
if product.DeliveryDate != nil {
key = fmt.Sprintf("%s_%s", product.DeliveryDate.Format("2006-01-02"), product.VehicleNumber)
} else {
key = fmt.Sprintf("no_date_%s", product.VehicleNumber)
if product.DeliveryDate == nil {
continue
}
// Get or create group
var warehouseId uint
var warehouseName string
if product.ProductWarehouse != nil {
warehouseId = product.ProductWarehouse.Warehouse.Id
warehouseName = product.ProductWarehouse.Warehouse.Name
}
key := fmt.Sprintf("%d_%s", warehouseId, product.DeliveryDate.Format("2006-01-02"))
group, exists := groupMap[key]
if !exists {
group = &DeliveryGroupDTO{
WarehouseId: warehouseId,
WarehouseName: warehouseName,
DeliveryDate: product.DeliveryDate,
VehicleNumber: product.VehicleNumber,
TotalQty: 0,
TotalWeight: 0,
TotalPrice: 0,
DeliveryDate: product.DeliveryDate,
Warehouse: &productwarehouseDTO.WarehouseBaseDTO{
Id: warehouseId,
Name: warehouseName,
},
Deliveries: []DeliveryItemDTO{},
}
groupMap[key] = group
}
// Update totals
group.TotalQty += product.Qty
group.TotalWeight += product.TotalWeight
group.TotalPrice += product.TotalPrice
deliveryItem := DeliveryItemDTO{
ProductWarehouse: product.ProductWarehouse,
Qty: product.Qty,
UnitPrice: product.UnitPrice,
TotalWeight: product.TotalWeight,
AvgWeight: product.AvgWeight,
TotalPrice: product.TotalPrice,
VehicleNumber: product.VehicleNumber,
}
group.Deliveries = append(group.Deliveries, deliveryItem)
}
// Convert map to slice
var groups []DeliveryGroupDTO
for _, group := range groupMap {
groups = append(groups, *group)
}
// Sort groups by delivery date
sort.Slice(groups, func(i, j int) bool {
if groups[i].DeliveryDate == nil || groups[j].DeliveryDate == nil {
return false
@@ -293,16 +308,20 @@ func groupDeliveryProducts(products []MarketingDeliveryProductDTO) []DeliveryGro
return groups[i].DeliveryDate.Before(*groups[j].DeliveryDate)
})
for i := range groups {
if groups[i].DeliveryDate != nil {
dateStr := groups[i].DeliveryDate.Format("20060102")
groups[i].DoNumber = fmt.Sprintf("%s-%s-%d", soNumber, dateStr, groups[i].Warehouse.Id)
}
}
return groups
}
// createDeliveryOrderDTO creates delivery order DTO
func createDeliveryOrderDTO(deliveryGroups []DeliveryGroupDTO) *DeliveryOrderDTO {
if len(deliveryGroups) == 0 {
return nil
}
return &DeliveryOrderDTO{
DeliveryGroups: deliveryGroups,
// getVehicleNumber mengambil vehicle number dari DeliveryProduct jika ada
func getVehicleNumber(e entity.MarketingProduct) string {
if e.DeliveryProduct != nil && e.DeliveryProduct.VehicleNumber != "" {
return e.DeliveryProduct.VehicleNumber
}
return ""
}
@@ -1,6 +1,8 @@
package delivery_orderss
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
@@ -8,26 +10,29 @@ import (
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
rMarketingDeliveryProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/marketing-delivery-products/repositories"
rDeliveryOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/repositories"
sDeliveryOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/services"
rMarketing "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/repositories"
rMarketingProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/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 DeliveryOrdersModule struct{}
func (DeliveryOrdersModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
deliveryOrdersRepo := rDeliveryOrders.NewDeliveryOrdersRepository(db)
marketingRepo := rMarketing.NewMarketingRepository(db)
marketingProductRepo := rMarketingProduct.NewMarketingProductRepository(db)
marketingProductRepo := rMarketing.NewMarketingProductRepository(db)
marketingDeliveryProductRepo := rMarketingDeliveryProduct.NewMarketingDeliveryProductRepository(db)
userRepo := rUser.NewUserRepository(db)
approvalRepo := commonRepo.NewApprovalRepository(db)
approvalSvc := commonSvc.NewApprovalService(approvalRepo)
deliveryOrdersService := sDeliveryOrders.NewDeliveryOrdersService(deliveryOrdersRepo, marketingRepo, marketingProductRepo, marketingDeliveryProductRepo, approvalSvc, validate)
// Register workflow steps for MARKETINGS approval
if err := approvalSvc.RegisterWorkflowSteps(utils.ApprovalWorkflowMarketing, utils.MarketingApprovalSteps); err != nil {
panic(fmt.Sprintf("failed to register marketing approval workflow: %v", err))
}
deliveryOrdersService := sDeliveryOrders.NewDeliveryOrdersService(marketingRepo, marketingProductRepo, marketingDeliveryProductRepo, approvalSvc, validate)
userService := sUser.NewUserService(userRepo, validate)
DeliveryOrdersRoutes(router, userService, deliveryOrdersService)
@@ -12,6 +12,10 @@ import (
func DeliveryOrdersRoutes(v1 fiber.Router, u user.UserService, s deliveryOrders.DeliveryOrdersService) {
ctrl := controller.NewDeliveryOrdersController(s)
v1.Get("/", ctrl.GetAll)
v1.Get("/:id", ctrl.GetOne)
// Sisanya di group /delivery-orders
route := v1.Group("/delivery-orders")
// route.Get("/", m.Auth(u), ctrl.GetAll)
@@ -20,10 +24,7 @@ func DeliveryOrdersRoutes(v1 fiber.Router, u user.UserService, s deliveryOrders.
// 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,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
}
@@ -2,26 +2,22 @@ package validation
type DeliveryProduct struct {
MarketingProductId uint `json:"marketing_product_id" validate:"required,gt=0"`
Qty float64 `json:"qty" validate:"required,gt=0"`
UnitPrice float64 `json:"unit_price" validate:"required,gt=0"`
AvgWeight float64 `json:"avg_weight" validate:"required,gt=0"`
TotalWeight float64 `json:"total_weight" validate:"required,gt=0"`
TotalPrice float64 `json:"total_price" validate:"required,gt=0"`
Qty float64 `json:"qty" validate:"omitempty,gte=0"`
UnitPrice float64 `json:"unit_price" validate:"omitempty,gte=0"`
AvgWeight float64 `json:"avg_weight" validate:"omitempty,gte=0"`
TotalWeight float64 `json:"total_weight" validate:"omitempty,gte=0"`
TotalPrice float64 `json:"total_price" validate:"omitempty,gte=0"`
DeliveryDate string `json:"delivery_date" validate:"omitempty,datetime=2006-01-02"`
VehicleNumber string `json:"vehicle_number" validate:"omitempty,max=50"`
}
type Create struct {
MarketingId uint `json:"marketing_id" validate:"required,gt=0"`
DeliveryDate string `json:"delivery_date" validate:"omitempty,datetime=2006-01-02"`
DeliveryProducts []DeliveryProduct `json:"delivery_products" validate:"required,min=1,dive"`
Notes string `json:"notes" validate:"omitempty,max=500"`
}
type Update struct {
DeliveryDate string `json:"delivery_date" validate:"omitempty,datetime=2006-01-02"`
DeliveryProducts []DeliveryProduct `json:"delivery_products" validate:"omitempty,min=1,dive"`
Notes string `json:"notes" validate:"omitempty,max=500"`
}
type Query struct {