Files
lti-api/internal/modules/repports/dto/repportMarketing.dto.go
T
2025-12-15 16:17:37 +07:00

220 lines
7.6 KiB
Go

package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
marketingDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/dto"
customerDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/dto"
productDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type RepportMarketingBaseDTO struct {
Id uint `json:"id"`
SoNumber string `json:"so_number"`
SoDate time.Time `json:"so_date"`
Customer *customerDTO.CustomerRelationDTO `json:"customer,omitempty"`
SalesPerson *userDTO.UserRelationDTO `json:"sales_person,omitempty"`
Notes string `json:"notes"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type RepportMarketingProductDTO struct {
Id uint `json:"id"`
MarketingProductId uint `json:"marketing_product_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"`
Product *productDTO.ProductRelationDTO `json:"product,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
type RepportMarketingDeliveryDTO 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,omitempty"`
VehicleNumber string `json:"vehicle_number"`
DoNumber string `json:"do_number"`
Product *productDTO.ProductRelationDTO `json:"product,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
type RepportMarketingListDTO struct {
RepportMarketingBaseDTO
MarketingProduct RepportMarketingProductDTO `json:"marketing_product"`
MarketingDelivery RepportMarketingDeliveryDTO `json:"marketing_delivery"`
TotalMarketingProduct float64 `json:"total_marketing_product"`
TotalMarketingDelivery float64 `json:"total_marketing_delivery"`
LatestApproval *approvalDTO.ApprovalRelationDTO `json:"latest_approval,omitempty"`
}
// === MAPPERS ===
func ToRepportMarketingBaseDTO(m *entity.Marketing) RepportMarketingBaseDTO {
if m == nil {
return RepportMarketingBaseDTO{}
}
var customer *customerDTO.CustomerRelationDTO
if m.Customer.Id != 0 {
mapped := customerDTO.ToCustomerRelationDTO(m.Customer)
customer = &mapped
}
var salesPerson *userDTO.UserRelationDTO
if m.SalesPerson.Id != 0 {
mapped := userDTO.ToUserRelationDTO(m.SalesPerson)
salesPerson = &mapped
}
return RepportMarketingBaseDTO{
Id: m.Id,
SoNumber: m.SoNumber,
SoDate: m.SoDate,
Customer: customer,
SalesPerson: salesPerson,
Notes: m.Notes,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
}
}
func ToRepportMarketingProductDTO(mp *entity.MarketingProduct) RepportMarketingProductDTO {
if mp == nil {
return RepportMarketingProductDTO{}
}
var product *productDTO.ProductRelationDTO
if mp.ProductWarehouse.Product.Id != 0 {
mapped := productDTO.ToProductRelationDTO(mp.ProductWarehouse.Product)
product = &mapped
}
return RepportMarketingProductDTO{
Id: mp.Id,
MarketingProductId: mp.Id,
Qty: mp.Qty,
UnitPrice: mp.UnitPrice,
AvgWeight: mp.AvgWeight,
TotalWeight: mp.TotalWeight,
TotalPrice: mp.TotalPrice,
Product: product,
CreatedAt: time.Now(),
}
}
func ToRepportMarketingDeliveryDTO(mdp *entity.MarketingDeliveryProduct, soNumber string) RepportMarketingDeliveryDTO {
if mdp == nil {
return RepportMarketingDeliveryDTO{}
}
var product *productDTO.ProductRelationDTO
if mdp.MarketingProduct.ProductWarehouse.Product.Id != 0 {
mapped := productDTO.ToProductRelationDTO(mdp.MarketingProduct.ProductWarehouse.Product)
product = &mapped
}
warehouseId := uint(0)
if mdp.MarketingProduct.ProductWarehouse.Id != 0 {
warehouseId = mdp.MarketingProduct.ProductWarehouse.WarehouseId
}
doNumber := marketingDTO.GenerateDeliveryOrderNumber(soNumber, mdp.DeliveryDate, warehouseId)
return RepportMarketingDeliveryDTO{
Id: mdp.Id,
MarketingProductId: mdp.MarketingProductId,
Qty: mdp.Qty,
UnitPrice: mdp.UnitPrice,
TotalWeight: mdp.TotalWeight,
AvgWeight: mdp.AvgWeight,
TotalPrice: mdp.TotalPrice,
DeliveryDate: mdp.DeliveryDate,
VehicleNumber: mdp.VehicleNumber,
DoNumber: doNumber,
Product: product,
CreatedAt: time.Now(),
}
}
func ToRepportMarketingListDTO(baseDTO RepportMarketingBaseDTO, mp *entity.MarketingProduct, mdp *entity.MarketingDeliveryProduct, latestApproval *approvalDTO.ApprovalRelationDTO) RepportMarketingListDTO {
var marketingProduct RepportMarketingProductDTO
var marketingDelivery RepportMarketingDeliveryDTO
if mp != nil {
marketingProduct = ToRepportMarketingProductDTO(mp)
}
if mdp != nil {
marketingDelivery = ToRepportMarketingDeliveryDTO(mdp, baseDTO.SoNumber)
}
totalMarketingProduct := float64(0)
totalMarketingDelivery := float64(0)
if mp != nil {
totalMarketingProduct = mp.Qty * mp.UnitPrice
}
if mdp != nil {
totalMarketingDelivery = mdp.Qty * mdp.UnitPrice
}
return RepportMarketingListDTO{
RepportMarketingBaseDTO: baseDTO,
MarketingProduct: marketingProduct,
MarketingDelivery: marketingDelivery,
TotalMarketingProduct: totalMarketingProduct,
TotalMarketingDelivery: totalMarketingDelivery,
LatestApproval: latestApproval,
}
}
func ToRepportMarketingListDTOs(deliveryProducts []entity.MarketingDeliveryProduct) []RepportMarketingListDTO {
result := make([]RepportMarketingListDTO, 0, len(deliveryProducts))
marketingMap := make(map[uint]entity.MarketingDeliveryProduct)
for _, dp := range deliveryProducts {
if dp.MarketingProduct.Marketing.Id == 0 {
continue
}
marketingID := dp.MarketingProduct.Marketing.Id
if _, exists := marketingMap[marketingID]; !exists {
marketingMap[marketingID] = dp
}
}
for _, deliveryProduct := range marketingMap {
if deliveryProduct.MarketingProduct.Marketing.Id == 0 {
continue
}
marketing := &deliveryProduct.MarketingProduct.Marketing
baseDTO := ToRepportMarketingBaseDTO(marketing)
var latestApproval *approvalDTO.ApprovalRelationDTO
if marketing.LatestApproval != nil {
mapped := approvalDTO.ToApprovalDTO(*marketing.LatestApproval)
latestApproval = &mapped
}
mdp := &deliveryProduct
dto := ToRepportMarketingListDTO(baseDTO, &deliveryProduct.MarketingProduct, mdp, latestApproval)
result = append(result, dto)
}
return result
}