feat[BE}: change get penjualan repport dto an add more params

This commit is contained in:
aguhh18
2025-12-16 21:10:48 +07:00
parent 062a7937e2
commit afe4b2ffe3
10 changed files with 741 additions and 247 deletions
@@ -2,6 +2,7 @@ package repository
import (
"context"
"strings"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
@@ -31,8 +32,6 @@ func NewMarketingDeliveryProductRepository(db *gorm.DB) MarketingDeliveryProduct
func (r *MarketingDeliveryProductRepositoryImpl) GetDeliveryProductsByProjectFlockID(ctx context.Context, projectFlockID uint, callback func(*gorm.DB) *gorm.DB) ([]entity.MarketingDeliveryProduct, error) {
var deliveryProducts []entity.MarketingDeliveryProduct
// JOIN digunakan untuk filter WHERE clause ke ProjectFlockID yang berada 3 level relasi atas
// Entity relations digunakan di Preload (callback) untuk load data, bukan untuk filter
db := r.DB().WithContext(ctx).
Joins("JOIN marketing_products ON marketing_products.id = marketing_delivery_products.marketing_product_id").
Joins("JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id").
@@ -91,16 +90,17 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetAllWithFilters(ctx context.C
Preload("Marketing.SalesPerson").
Preload("ProductWarehouse").
Preload("ProductWarehouse.Product").
Preload("ProductWarehouse.Warehouse")
Preload("ProductWarehouse.Warehouse").
Preload("ProductWarehouse.ProjectFlockKandang")
}).
Joins("JOIN marketing_products ON marketing_products.id = marketing_delivery_products.marketing_product_id").
Joins("JOIN marketings ON marketings.id = marketing_products.marketing_id")
if filters.ProductId > 0 || filters.WarehouseId > 0 || filters.ProjectFlockKandangId > 0 {
if filters.ProductId > 0 || filters.WarehouseId > 0 || filters.Search != "" {
db = db.Joins("LEFT JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id")
}
if filters.ProductId > 0 {
if filters.ProductId > 0 || filters.Search != "" {
db = db.Joins("LEFT JOIN products ON products.id = product_warehouses.product_id")
}
@@ -109,8 +109,13 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetAllWithFilters(ctx context.C
}
if filters.Search != "" {
db = db.Where("marketing_delivery_products.vehicle_number ILIKE ?",
"%"+filters.Search+"%")
db = db.Joins("LEFT JOIN customers ON customers.id = marketings.customer_id")
}
if filters.Search != "" {
searchPattern := "%" + filters.Search + "%"
db = db.Where("marketing_delivery_products.vehicle_number ILIKE ? OR marketings.so_number ILIKE ? OR customers.name ILIKE ? OR products.name ILIKE ?",
searchPattern, searchPattern, searchPattern, searchPattern)
}
if filters.CustomerId > 0 {
@@ -121,10 +126,6 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetAllWithFilters(ctx context.C
db = db.Where("marketings.sales_person_id = ?", filters.SalesPersonId)
}
if filters.MarketingId > 0 {
db = db.Where("marketings.id = ?", filters.MarketingId)
}
if filters.ProductId > 0 {
db = db.Where("product_warehouses.product_id = ?", filters.ProductId)
}
@@ -133,17 +134,90 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetAllWithFilters(ctx context.C
db = db.Where("product_warehouses.warehouse_id = ?", filters.WarehouseId)
}
if filters.ProjectFlockKandangId > 0 {
db = db.Where("product_warehouses.project_flock_kandang_id = ?", filters.ProjectFlockKandangId)
}
if filters.DeliveryDate != "" {
if deliveryDate, err := utils.ParseDateString(filters.DeliveryDate); err == nil {
nextDate := deliveryDate.AddDate(0, 0, 1)
db = db.Where("marketing_delivery_products.delivery_date >= ? AND marketing_delivery_products.delivery_date < ?", deliveryDate, nextDate)
if filters.FilterBy != "" && (filters.StartDate != "" || filters.EndDate != "") {
if filters.FilterBy == "delivery_date" {
if filters.StartDate != "" {
if startDate, err := utils.ParseDateString(filters.StartDate); err == nil {
db = db.Where("marketing_delivery_products.delivery_date >= ?", startDate)
}
}
if filters.EndDate != "" {
if endDate, err := utils.ParseDateString(filters.EndDate); err == nil {
nextDate := endDate.AddDate(0, 0, 1)
db = db.Where("marketing_delivery_products.delivery_date < ?", nextDate)
}
}
} else if filters.FilterBy == "realization_date" {
if filters.StartDate != "" {
if startDate, err := utils.ParseDateString(filters.StartDate); err == nil {
db = db.Where("marketings.created_at >= ?", startDate)
}
}
if filters.EndDate != "" {
if endDate, err := utils.ParseDateString(filters.EndDate); err == nil {
nextDate := endDate.AddDate(0, 0, 1)
db = db.Where("marketings.created_at < ?", nextDate)
}
}
}
}
sortColumn := "marketing_delivery_products.id"
sortOrder := "DESC"
if filters.SortBy != "" {
switch filters.SortBy {
case "delivery_date":
sortColumn = "marketing_delivery_products.delivery_date"
case "customer":
sortColumn = "customers.name"
if !containsJoin(db, "customers") {
db = db.Joins("LEFT JOIN customers ON customers.id = marketings.customer_id")
}
case "warehouse":
sortColumn = "warehouses.name"
if !containsJoin(db, "warehouses") {
db = db.Joins("LEFT JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id").
Joins("LEFT JOIN warehouses ON warehouses.id = product_warehouses.warehouse_id")
}
case "product":
sortColumn = "products.name"
if !containsJoin(db, "products") {
db = db.Joins("LEFT JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id").
Joins("LEFT JOIN products ON products.id = product_warehouses.product_id")
}
case "sales_person":
sortColumn = "sales_users.name"
if !containsJoin(db, "sales_users") {
db = db.Joins("LEFT JOIN users AS sales_users ON sales_users.id = marketings.sales_person_id")
}
case "vehicle_number":
sortColumn = "marketing_delivery_products.vehicle_number"
case "sales_amount":
sortColumn = "marketing_delivery_products.total_price"
case "hpp_amount":
sortColumn = "marketing_delivery_products.total_price"
case "qty":
sortColumn = "marketing_delivery_products.qty"
case "average_weight":
sortColumn = "marketing_delivery_products.avg_weight"
case "total_weight":
sortColumn = "marketing_delivery_products.total_weight"
case "sales_price":
sortColumn = "marketing_delivery_products.unit_price"
case "hpp_price":
sortColumn = "marketing_delivery_products.unit_price"
case "aging_days":
sortColumn = "marketing_delivery_products.delivery_date"
}
}
if filters.SortOrder != "" && (filters.SortOrder == "asc" || filters.SortOrder == "desc") {
sortOrder = strings.ToUpper(filters.SortOrder)
}
db = db.Order(sortColumn + " " + sortOrder)
if err := db.Count(&total).Error; err != nil {
return nil, 0, err
}
@@ -151,10 +225,15 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetAllWithFilters(ctx context.C
if err := db.
Offset(offset).
Limit(limit).
Order("marketing_delivery_products.id DESC").
Find(&deliveryProducts).Error; err != nil {
return nil, 0, err
}
return deliveryProducts, total, nil
}
func containsJoin(db *gorm.DB, tableName string) bool {
statement := db.Statement
joinSQL := statement.SQL.String()
return strings.Contains(joinSQL, "JOIN "+tableName)
}