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
@@ -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 ""
}