Files
lti-api/internal/modules/purchases/dto/purchase.dto.go
T
2025-11-05 18:58:06 +07:00

156 lines
3.8 KiB
Go

package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
)
type SupplierBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Alias string `json:"alias"`
Type string `json:"type"`
Category string `json:"category"`
}
type AreaBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type LocationBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type WarehouseBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Area *AreaBaseDTO `json:"area,omitempty"`
Location *LocationBaseDTO `json:"location,omitempty"`
}
type ProductBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
SKU *string `json:"sku,omitempty"`
}
type PurchaseItemDTO struct {
Id uint64 `json:"id"`
Product *ProductBaseDTO `json:"product,omitempty"`
Warehouse *WarehouseBaseDTO `json:"warehouse,omitempty"`
ProductWarehouseID *uint64 `json:"product_warehouse_id,omitempty"`
SubQty float64 `json:"sub_qty"`
TotalQty float64 `json:"total_qty"`
TotalUsed float64 `json:"total_used"`
Price float64 `json:"price"`
TotalPrice float64 `json:"total_price"`
}
type PurchaseDetailDTO struct {
Id uint64 `json:"id"`
PrNumber string `json:"pr_number"`
Supplier *SupplierBaseDTO `json:"supplier,omitempty"`
CreditTerm *int `json:"credit_term,omitempty"`
DueDate *time.Time `json:"due_date,omitempty"`
GrandTotal float64 `json:"grand_total"`
Notes *string `json:"notes,omitempty"`
Items []PurchaseItemDTO `json:"items"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func toSupplierBaseDTO(s entity.Supplier) *SupplierBaseDTO {
if s.Id == 0 {
return nil
}
return &SupplierBaseDTO{
Id: s.Id,
Name: s.Name,
Alias: s.Alias,
Type: s.Type,
Category: s.Category,
}
}
func toWarehouseBaseDTO(w *entity.Warehouse) *WarehouseBaseDTO {
if w == nil || w.Id == 0 {
return nil
}
dto := &WarehouseBaseDTO{
Id: w.Id,
Name: w.Name,
}
if w.Area.Id != 0 {
dto.Area = &AreaBaseDTO{
Id: w.Area.Id,
Name: w.Area.Name,
}
}
if w.Location != nil && w.Location.Id != 0 {
dto.Location = &LocationBaseDTO{
Id: w.Location.Id,
Name: w.Location.Name,
}
}
return dto
}
func toProductBaseDTO(p *entity.Product) *ProductBaseDTO {
if p == nil || p.Id == 0 {
return nil
}
dto := &ProductBaseDTO{
Id: p.Id,
Name: p.Name,
}
if p.Sku != nil {
dto.SKU = p.Sku
}
return dto
}
func ToPurchaseItemDTO(item entity.PurchaseItem) PurchaseItemDTO {
dto := PurchaseItemDTO{
Id: item.Id,
ProductWarehouseID: item.ProductWarehouseId,
SubQty: item.SubQty,
TotalQty: item.TotalQty,
TotalUsed: item.TotalUsed,
Price: item.Price,
TotalPrice: item.TotalPrice,
}
if item.Product != nil {
dto.Product = toProductBaseDTO(item.Product)
}
if item.Warehouse != nil {
dto.Warehouse = toWarehouseBaseDTO(item.Warehouse)
}
return dto
}
func ToPurchaseItemDTOs(items []entity.PurchaseItem) []PurchaseItemDTO {
result := make([]PurchaseItemDTO, len(items))
for i, item := range items {
result[i] = ToPurchaseItemDTO(item)
}
return result
}
func ToPurchaseDetailDTO(p entity.Purchase) PurchaseDetailDTO {
return PurchaseDetailDTO{
Id: p.Id,
PrNumber: p.PrNumber,
Supplier: toSupplierBaseDTO(p.Supplier),
CreditTerm: p.CreditTerm,
DueDate: p.DueDate,
GrandTotal: p.GrandTotal,
Notes: p.Notes,
Items: ToPurchaseItemDTOs(p.Items),
CreatedAt: p.CreatedAt,
UpdatedAt: p.UpdatedAt,
}
}