Feat[BE-222]: creating update DO(Unfinished)

This commit is contained in:
Asep Teguh Hidayat
2025-11-13 19:28:50 +07:00
parent 74ec25db5b
commit 2f5fab9f80
6 changed files with 367 additions and 80 deletions
@@ -1,6 +1,8 @@
package dto
import (
"fmt"
"sort"
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
@@ -22,6 +24,22 @@ type MarketingDeliveryProductDTO struct {
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"`
@@ -30,20 +48,33 @@ type DeliveryOrdersBaseDTO struct {
Notes string `json:"notes,omitempty"`
}
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
}
type MarketingBaseDTO struct {
Id uint `json:"id"`
SoNumber string `json:"so_number"`
SoDate time.Time `json:"so_date"`
Id uint `json:"id"`
SoNumber string `json:"so_number"`
SoDate time.Time `json:"so_date"`
Products []MarketingProductDTO `json:"products,omitempty"`
}
type DeliveryOrdersListDTO struct {
DeliveryOrdersBaseDTO
Marketing *MarketingBaseDTO `json:"marketing,omitempty"`
DeliveryProducts []MarketingDeliveryProductDTO `json:"delivery_products,omitempty"`
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Approval *approvalDTO.ApprovalBaseDTO `json:"approval,omitempty"`
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 {
@@ -52,6 +83,19 @@ type DeliveryOrdersDetailDTO struct {
// === Mapper Functions ===
func ToMarketingProductDTO(e entity.MarketingProduct) MarketingProductDTO {
return MarketingProductDTO{
Id: e.Id,
MarketingId: e.MarketingId,
ProductWarehouseId: e.ProductWarehouseId,
Qty: e.Qty,
UnitPrice: e.UnitPrice,
AvgWeight: e.AvgWeight,
TotalWeight: e.TotalWeight,
TotalPrice: e.TotalPrice,
}
}
func ToMarketingDeliveryProductDTO(e entity.MarketingDeliveryProduct) MarketingDeliveryProductDTO {
return MarketingDeliveryProductDTO{
Id: e.Id,
@@ -90,10 +134,19 @@ func ToDeliveryOrdersListDTO(e entity.DeliveryOrders) DeliveryOrdersListDTO {
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,
}
}
@@ -105,10 +158,16 @@ func ToDeliveryOrdersListDTO(e entity.DeliveryOrders) DeliveryOrdersListDTO {
}
}
// Group delivery products by warehouse and delivery date
deliveryGroups := groupDeliveryProducts(deliveryProductsDTOs)
// Create delivery order DTO with summary
deliveryOrder := createDeliveryOrderDTO(deliveryGroups)
return DeliveryOrdersListDTO{
DeliveryOrdersBaseDTO: ToDeliveryOrdersBaseDTO(e),
Marketing: marketing,
DeliveryProducts: deliveryProductsDTOs,
SalesOrder: marketing,
DeliveryOrder: deliveryOrder,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
@@ -124,10 +183,19 @@ func ToDeliveryOrdersListDTOWithProducts(e entity.DeliveryOrders, deliveryProduc
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,
}
}
@@ -139,10 +207,16 @@ func ToDeliveryOrdersListDTOWithProducts(e entity.DeliveryOrders, deliveryProduc
}
}
// Group delivery products by warehouse and delivery date
deliveryGroups := groupDeliveryProducts(deliveryProductsDTOs)
// Create delivery order DTO with summary
deliveryOrder := createDeliveryOrderDTO(deliveryGroups)
return DeliveryOrdersListDTO{
DeliveryOrdersBaseDTO: ToDeliveryOrdersBaseDTO(e),
Marketing: marketing,
DeliveryProducts: deliveryProductsDTOs,
SalesOrder: marketing,
DeliveryOrder: deliveryOrder,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
@@ -162,3 +236,73 @@ func ToDeliveryOrdersDetailDTO(e entity.DeliveryOrders) DeliveryOrdersDetailDTO
DeliveryOrdersListDTO: ToDeliveryOrdersListDTO(e),
}
}
// groupDeliveryProducts groups delivery products by warehouse and delivery date
func groupDeliveryProducts(products []MarketingDeliveryProductDTO) []DeliveryGroupDTO {
// Create a map to group products
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)
}
// Get or create group
group, exists := groupMap[key]
if !exists {
group = &DeliveryGroupDTO{
WarehouseId: warehouseId,
WarehouseName: warehouseName,
DeliveryDate: product.DeliveryDate,
VehicleNumber: product.VehicleNumber,
TotalQty: 0,
TotalWeight: 0,
TotalPrice: 0,
}
groupMap[key] = group
}
// Update totals
group.TotalQty += product.Qty
group.TotalWeight += product.TotalWeight
group.TotalPrice += product.TotalPrice
}
// 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
}
return groups[i].DeliveryDate.Before(*groups[j].DeliveryDate)
})
return groups
}
// createDeliveryOrderDTO creates delivery order DTO
func createDeliveryOrderDTO(deliveryGroups []DeliveryGroupDTO) *DeliveryOrderDTO {
if len(deliveryGroups) == 0 {
return nil
}
return &DeliveryOrderDTO{
DeliveryGroups: deliveryGroups,
}
}