mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
add sorting transaction, report keuangan
This commit is contained in:
@@ -2,7 +2,7 @@ package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -30,7 +30,7 @@ type CustomerPaymentTransaction struct {
|
||||
type CustomerPaymentRepository interface {
|
||||
GetCustomerPaymentTransactions(ctx context.Context, customerID *uint) ([]CustomerPaymentTransaction, error)
|
||||
GetInitialBalanceByCustomer(ctx context.Context, customerID uint) (float64, error)
|
||||
GetCustomerIDsWithTransactions(ctx context.Context, limit, offset int, allowedCustomerIDs []uint) ([]uint, int64, error)
|
||||
GetCustomerIDsWithTransactions(ctx context.Context, limit, offset int, allowedCustomerIDs []uint, sortBy, sortOrder string) ([]uint, int64, error)
|
||||
}
|
||||
|
||||
type customerPaymentRepositoryImpl struct {
|
||||
@@ -146,21 +146,34 @@ func (r *customerPaymentRepositoryImpl) GetInitialBalanceByCustomer(ctx context.
|
||||
return result.Nominal, nil
|
||||
}
|
||||
|
||||
func (r *customerPaymentRepositoryImpl) GetCustomerIDsWithTransactions(ctx context.Context, limit, offset int, allowedCustomerIDs []uint) ([]uint, int64, error) {
|
||||
subQuery := r.db.WithContext(ctx).
|
||||
Table("(" +
|
||||
"SELECT DISTINCT c.id as customer_id FROM marketing_delivery_products mdp " +
|
||||
"INNER JOIN marketing_products mp ON mp.id = mdp.marketing_product_id " +
|
||||
"INNER JOIN marketings m ON m.id = mp.marketing_id " +
|
||||
"INNER JOIN customers c ON c.id = m.customer_id " +
|
||||
"WHERE mdp.delivery_date IS NOT NULL AND m.deleted_at IS NULL AND c.deleted_at IS NULL " +
|
||||
"UNION " +
|
||||
"SELECT DISTINCT c.id as customer_id FROM payments p " +
|
||||
"INNER JOIN customers c ON c.id = p.party_id " +
|
||||
"WHERE p.party_type = 'CUSTOMER' AND p.direction = 'IN' " +
|
||||
"AND p.transaction_type = 'PENJUALAN' AND p.deleted_at IS NULL AND c.deleted_at IS NULL" +
|
||||
") as customer_ids")
|
||||
func resolveCustomerPaymentSortClause(sortBy, sortOrder string) string {
|
||||
direction := "ASC"
|
||||
if strings.EqualFold(strings.TrimSpace(sortOrder), "desc") {
|
||||
direction = "DESC"
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(sortBy)) {
|
||||
case "customer":
|
||||
return "customer_name " + direction
|
||||
default:
|
||||
return "customer_name ASC"
|
||||
}
|
||||
}
|
||||
|
||||
func (r *customerPaymentRepositoryImpl) GetCustomerIDsWithTransactions(ctx context.Context, limit, offset int, allowedCustomerIDs []uint, sortBy, sortOrder string) ([]uint, int64, error) {
|
||||
unionSQL := "(" +
|
||||
"SELECT DISTINCT c.id as customer_id, c.name as customer_name FROM marketing_delivery_products mdp " +
|
||||
"INNER JOIN marketing_products mp ON mp.id = mdp.marketing_product_id " +
|
||||
"INNER JOIN marketings m ON m.id = mp.marketing_id " +
|
||||
"INNER JOIN customers c ON c.id = m.customer_id " +
|
||||
"WHERE mdp.delivery_date IS NOT NULL AND m.deleted_at IS NULL AND c.deleted_at IS NULL " +
|
||||
"UNION " +
|
||||
"SELECT DISTINCT c.id as customer_id, c.name as customer_name FROM payments p " +
|
||||
"INNER JOIN customers c ON c.id = p.party_id " +
|
||||
"WHERE p.party_type = 'CUSTOMER' AND p.direction = 'IN' " +
|
||||
"AND p.transaction_type = 'PENJUALAN' AND p.deleted_at IS NULL AND c.deleted_at IS NULL" +
|
||||
") as customer_ids"
|
||||
|
||||
subQuery := r.db.WithContext(ctx).Table(unionSQL)
|
||||
if len(allowedCustomerIDs) > 0 {
|
||||
subQuery = subQuery.Where("customer_id IN ?", allowedCustomerIDs)
|
||||
}
|
||||
@@ -170,28 +183,14 @@ func (r *customerPaymentRepositoryImpl) GetCustomerIDsWithTransactions(ctx conte
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var customerIDs []uint
|
||||
query := r.db.WithContext(ctx).
|
||||
Table("(" +
|
||||
"SELECT DISTINCT c.id as customer_id FROM marketing_delivery_products mdp " +
|
||||
"INNER JOIN marketing_products mp ON mp.id = mdp.marketing_product_id " +
|
||||
"INNER JOIN marketings m ON m.id = mp.marketing_id " +
|
||||
"INNER JOIN customers c ON c.id = m.customer_id " +
|
||||
"WHERE mdp.delivery_date IS NOT NULL AND m.deleted_at IS NULL AND c.deleted_at IS NULL " +
|
||||
"UNION " +
|
||||
"SELECT DISTINCT c.id as customer_id FROM payments p " +
|
||||
"INNER JOIN customers c ON c.id = p.party_id " +
|
||||
"WHERE p.party_type = 'CUSTOMER' AND p.direction = 'IN' " +
|
||||
"AND p.transaction_type = 'PENJUALAN' AND p.deleted_at IS NULL AND c.deleted_at IS NULL" +
|
||||
") as customer_ids").
|
||||
Select("customer_id")
|
||||
|
||||
query := r.db.WithContext(ctx).Table(unionSQL).Select("customer_id")
|
||||
if len(allowedCustomerIDs) > 0 {
|
||||
query = query.Where("customer_id IN ?", allowedCustomerIDs)
|
||||
}
|
||||
|
||||
var customerIDs []uint
|
||||
err := query.
|
||||
Order("customer_id ASC").
|
||||
Order(resolveCustomerPaymentSortClause(sortBy, sortOrder)).
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Pluck("customer_id", &customerIDs).
|
||||
|
||||
@@ -52,6 +52,19 @@ func (r *debtSupplierRepositoryImpl) latestPurchaseApproval(ctx context.Context)
|
||||
)
|
||||
}
|
||||
|
||||
func resolveDebtSupplierSortClause(filters *validation.DebtSupplierQuery) string {
|
||||
direction := "ASC"
|
||||
if strings.EqualFold(strings.TrimSpace(filters.SortOrder), "desc") {
|
||||
direction = "DESC"
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(filters.SortBy)) {
|
||||
case "supplier":
|
||||
return "suppliers.name " + direction
|
||||
default:
|
||||
return "suppliers.name ASC"
|
||||
}
|
||||
}
|
||||
|
||||
func resolveDebtSupplierDateColumn(filterBy string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(filterBy)) {
|
||||
case "po_date":
|
||||
@@ -129,15 +142,24 @@ func (r *debtSupplierRepositoryImpl) GetSuppliersWithPurchases(ctx context.Conte
|
||||
offset = 0
|
||||
}
|
||||
|
||||
var supplierIDs []uint
|
||||
if err := query.
|
||||
Select("suppliers.id").
|
||||
Order("suppliers.id ASC").
|
||||
type supplierIDResult struct {
|
||||
ID uint `gorm:"column:id"`
|
||||
Name string `gorm:"column:name"`
|
||||
}
|
||||
var idResults []supplierIDResult
|
||||
if err := r.baseSupplierQuery(ctx, filters).
|
||||
Select("suppliers.id, suppliers.name").
|
||||
Group("suppliers.id, suppliers.name").
|
||||
Order(resolveDebtSupplierSortClause(filters)).
|
||||
Offset(offset).
|
||||
Limit(limit).
|
||||
Pluck("suppliers.id", &supplierIDs).Error; err != nil {
|
||||
Scan(&idResults).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
supplierIDs := make([]uint, 0, len(idResults))
|
||||
for _, r := range idResults {
|
||||
supplierIDs = append(supplierIDs, r.ID)
|
||||
}
|
||||
|
||||
if len(supplierIDs) == 0 {
|
||||
return []entity.Supplier{}, totalSuppliers, nil
|
||||
@@ -146,6 +168,7 @@ func (r *debtSupplierRepositoryImpl) GetSuppliersWithPurchases(ctx context.Conte
|
||||
var suppliers []entity.Supplier
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("id IN ?", supplierIDs).
|
||||
Order(resolveDebtSupplierSortClause(filters)).
|
||||
Find(&suppliers).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user