mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Merge branch 'fix/filter-purchase-order' into 'development'
Fix/filter purchase order See merge request mbugroup/lti-api!403
This commit is contained in:
@@ -195,9 +195,14 @@ func (s *purchaseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
db = db.Where("purchases.po_date >= ?", *poDateStart)
|
||||
}
|
||||
|
||||
if poDateStart != nil {
|
||||
db = db.Where("purchases.po_date >= ?", *poDateStart)
|
||||
}
|
||||
|
||||
if poDateEnd != nil {
|
||||
db = db.Where("purchases.po_date < ?", *poDateEnd)
|
||||
}
|
||||
|
||||
if scope.Restrict {
|
||||
if len(scope.IDs) == 0 {
|
||||
return db.Where("1 = 0")
|
||||
@@ -311,12 +316,119 @@ func (s *purchaseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
WHERE pi.purchase_id = purchases.id
|
||||
AND LOWER(COALESCE(p.name, '')) LIKE ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM purchase_items pi
|
||||
JOIN warehouses w ON w.id = pi.warehouse_id
|
||||
JOIN locations l ON l.id = w.location_id
|
||||
WHERE pi.purchase_id = purchases.id
|
||||
AND LOWER(COALESCE(l.name, '')) LIKE ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM purchase_items pi
|
||||
JOIN expense_nonstocks en ON en.id = pi.expense_nonstock_id
|
||||
JOIN expenses e ON e.id = en.expense_id
|
||||
WHERE pi.purchase_id = purchases.id
|
||||
AND LOWER(COALESCE(e.reference_number, '')) LIKE ?
|
||||
)
|
||||
)`,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
)
|
||||
}
|
||||
|
||||
if len(approvalStatuses) > 0 {
|
||||
approvalConditions := make([]string, 0, len(approvalStatuses))
|
||||
approvalArgs := make([]any, 0, 2+(len(approvalStatuses)*3))
|
||||
approvalArgs = append(approvalArgs, utils.ApprovalWorkflowPurchase.String(), utils.ApprovalWorkflowPurchase.String())
|
||||
for _, status := range approvalStatuses {
|
||||
if status == "" {
|
||||
continue
|
||||
}
|
||||
like := "%" + status + "%"
|
||||
approvalConditions = append(approvalConditions, `(LOWER(COALESCE(a.step_name, '')) LIKE ? OR LOWER(COALESCE(CAST(a.action AS TEXT), '')) LIKE ? OR CAST(a.step_number AS TEXT) = ?)`)
|
||||
approvalArgs = append(approvalArgs, like, like, status)
|
||||
}
|
||||
|
||||
if len(approvalConditions) > 0 {
|
||||
approvalClause := strings.Join(approvalConditions, " OR ")
|
||||
approvalQuery := fmt.Sprintf(
|
||||
`EXISTS (
|
||||
SELECT 1
|
||||
FROM approvals a
|
||||
WHERE a.approvable_type = ?
|
||||
AND a.approvable_id = purchases.id
|
||||
AND a.id = (
|
||||
SELECT a2.id
|
||||
FROM approvals a2
|
||||
WHERE a2.approvable_type = ?
|
||||
AND a2.approvable_id = purchases.id
|
||||
ORDER BY a2.action_at DESC, a2.id DESC
|
||||
LIMIT 1
|
||||
)
|
||||
AND (%s)
|
||||
)`,
|
||||
approvalClause,
|
||||
)
|
||||
db = db.Where(approvalQuery, approvalArgs...)
|
||||
}
|
||||
}
|
||||
|
||||
if search != "" {
|
||||
like := "%" + search + "%"
|
||||
db = db.Where(
|
||||
`(
|
||||
LOWER(COALESCE(purchases.pr_number, '')) LIKE ?
|
||||
OR LOWER(COALESCE(purchases.po_number, '')) LIKE ?
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM suppliers s
|
||||
WHERE s.id = purchases.supplier_id
|
||||
AND LOWER(COALESCE(s.name, '')) LIKE ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM users u
|
||||
WHERE u.id = purchases.created_by
|
||||
AND LOWER(COALESCE(u.name, '')) LIKE ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM purchase_items pi
|
||||
JOIN products p ON p.id = pi.product_id
|
||||
WHERE pi.purchase_id = purchases.id
|
||||
AND LOWER(COALESCE(p.name, '')) LIKE ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM purchase_items pi
|
||||
JOIN warehouses w ON w.id = pi.warehouse_id
|
||||
JOIN locations l ON l.id = w.location_id
|
||||
WHERE pi.purchase_id = purchases.id
|
||||
AND LOWER(COALESCE(l.name, '')) LIKE ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM purchase_items pi
|
||||
JOIN expense_nonstocks en ON en.id = pi.expense_nonstock_id
|
||||
JOIN expenses e ON e.id = en.expense_id
|
||||
WHERE pi.purchase_id = purchases.id
|
||||
AND LOWER(COALESCE(e.reference_number, '')) LIKE ?
|
||||
)
|
||||
)`,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -432,6 +432,7 @@ func (c *RepportController) GetProductionResult(ctx *fiber.Ctx) error {
|
||||
func parseCommaSeparatedInt64s(raw string) ([]int64, error) {
|
||||
return parseCommaSeparatedInt64sWithField(raw, "supplier_ids")
|
||||
}
|
||||
|
||||
func parseCommaSeparatedInt64sWithField(raw, field string) ([]int64, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
|
||||
@@ -71,7 +71,7 @@ func (r *purchaseSupplierRepositoryImpl) baseSupplierQuery(ctx context.Context,
|
||||
if len(filters.ProductCategoryIDs) > 0 {
|
||||
db = db.
|
||||
Joins("JOIN products ON products.id = purchase_items.product_id").
|
||||
Where("products.product_category_id IN ?", filters.ProductCategoryIDs)
|
||||
Where("products.product_category_id IN ?", filters.ProductCategoryIDs)
|
||||
}
|
||||
|
||||
if len(filters.AreaIDs) > 0 || filters.AllowedAreaIDs != nil {
|
||||
@@ -194,7 +194,7 @@ func (r *purchaseSupplierRepositoryImpl) GetItemsBySuppliers(ctx context.Context
|
||||
if len(filters.ProductCategoryIDs) > 0 {
|
||||
db = db.
|
||||
Joins("JOIN products ON products.id = purchase_items.product_id").
|
||||
Where("products.product_category_id IN ?", filters.ProductCategoryIDs)
|
||||
Where("products.product_category_id IN ?", filters.ProductCategoryIDs)
|
||||
}
|
||||
if len(filters.AreaIDs) > 0 || filters.AllowedAreaIDs != nil {
|
||||
db = db.Joins("JOIN warehouses ON warehouses.id = purchase_items.warehouse_id")
|
||||
|
||||
Reference in New Issue
Block a user