fix[BE]: Update GetAllWithFilters to enhance search functionality and join conditions

This commit is contained in:
aguhh18
2026-01-18 21:26:31 +07:00
parent af7aabdec8
commit fb193fc61f
@@ -141,25 +141,37 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetAllWithFilters(ctx context.C
Where("marketing_delivery_products.delivery_date IS NOT NULL")
if filters.ProductId > 0 || filters.WarehouseId > 0 || filters.Search != "" || filters.MarketingType != "" {
db = db.Joins("LEFT JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id")
db = db.Joins("LEFT JOIN product_warehouses ON product_warehouses.id = marketing_delivery_products.product_warehouse_id")
}
if filters.ProductId > 0 || filters.Search != "" || filters.MarketingType != "" {
db = db.Joins("LEFT JOIN products ON products.id = product_warehouses.product_id")
}
if filters.WarehouseId > 0 {
if filters.WarehouseId > 0 || filters.Search != "" {
db = db.Joins("LEFT JOIN warehouses ON warehouses.id = product_warehouses.warehouse_id")
}
if filters.Search != "" {
db = db.Joins("LEFT JOIN customers ON customers.id = marketings.customer_id")
}
db = db.Joins("LEFT JOIN users AS sales_users ON sales_users.id = marketings.sales_person_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)
db = db.Where(`(
marketing_delivery_products.vehicle_number ILIKE ? OR
customers.name ILIKE ? OR
warehouses.name ILIKE ? OR
products.name ILIKE ? OR
sales_users.name ILIKE ? OR
CONCAT(
marketings.so_number,
'-',
COALESCE(TO_CHAR(marketing_delivery_products.delivery_date, 'YYYYMMDD'), ''),
'-',
COALESCE(product_warehouses.warehouse_id::text, '')
) ILIKE ?
)`,
searchPattern, searchPattern, searchPattern, searchPattern, searchPattern, searchPattern)
}
if filters.CustomerId > 0 {