mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 07:45:44 +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).
|
||||
|
||||
Reference in New Issue
Block a user