mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat[BE]: refine customer payment report structure by removing unused fields and enhancing query logic for better performance
This commit is contained in:
@@ -20,9 +20,7 @@ type CustomerPaymentTransaction struct {
|
||||
Weight float64 `gorm:"column:weight"`
|
||||
AverageWeight float64 `gorm:"column:average_weight"`
|
||||
Price float64 `gorm:"column:price"`
|
||||
CreditNote float64 `gorm:"column:credit_note"`
|
||||
FinalPrice float64 `gorm:"column:final_price"`
|
||||
PPN float64 `gorm:"column:ppn"`
|
||||
TotalPrice float64 `gorm:"column:total_price"`
|
||||
PaymentAmount float64 `gorm:"column:payment_amount"`
|
||||
PickupInfo string `gorm:"column:pickup_info"`
|
||||
@@ -44,50 +42,41 @@ func NewCustomerPaymentRepository(db *gorm.DB) CustomerPaymentRepository {
|
||||
}
|
||||
|
||||
func (r *customerPaymentRepositoryImpl) GetCustomerPaymentTransactions(ctx context.Context, customerID *uint) ([]CustomerPaymentTransaction, error) {
|
||||
// Build SALES subquery
|
||||
salesQuery := r.db.WithContext(ctx).
|
||||
Table("marketings m").
|
||||
Table("marketing_delivery_products mdp").
|
||||
Select(`
|
||||
'SALES' AS transaction_type,
|
||||
m.id::BIGINT AS transaction_id,
|
||||
mdp.id::BIGINT AS transaction_id,
|
||||
c.id::BIGINT AS customer_id,
|
||||
m.so_date::DATE AS trans_date,
|
||||
MAX(mdp.delivery_date)::DATE AS delivery_date,
|
||||
m.so_number AS reference,
|
||||
COALESCE(STRING_AGG(DISTINCT mdp.vehicle_number, ', ') FILTER (WHERE mdp.vehicle_number IS NOT NULL), '') AS vehicle_numbers,
|
||||
mdp.delivery_date::DATE AS delivery_date,
|
||||
m.so_number || '-' || TO_CHAR(mdp.delivery_date, 'YYYYMMDD') || '-' || CAST(pw.warehouse_id AS VARCHAR) AS reference,
|
||||
COALESCE(mdp.vehicle_number, '') AS vehicle_numbers,
|
||||
|
||||
COALESCE(SUM(COALESCE(mp.qty, 0)), 0)::NUMERIC(15,3) AS qty,
|
||||
COALESCE(SUM(COALESCE(mdp.total_weight, mp.total_weight, 0)), 0)::NUMERIC(15,3) AS weight,
|
||||
CASE WHEN COALESCE(SUM(COALESCE(mp.qty, 0)), 0) > 0
|
||||
THEN (COALESCE(SUM(COALESCE(mdp.total_weight, mp.total_weight, 0)), 0) / COALESCE(SUM(COALESCE(mp.qty, 0)), 0))::NUMERIC(15,3)
|
||||
ELSE 0::NUMERIC(15,3)
|
||||
END AS average_weight,
|
||||
COALESCE(AVG(COALESCE(mdp.unit_price, mp.unit_price, 0)), 0)::NUMERIC(15,3) AS price,
|
||||
0::NUMERIC(15,3) AS credit_note,
|
||||
COALESCE(SUM(COALESCE(mdp.total_price, mp.total_price)), 0)::NUMERIC(15,3) AS final_price,
|
||||
0::NUMERIC(15,3) AS ppn,
|
||||
COALESCE(SUM(COALESCE(mdp.total_price, mp.total_price)), 0)::NUMERIC(15,3) AS total_price,
|
||||
COALESCE(mdp.usage_qty, 0)::NUMERIC(15,3) AS qty,
|
||||
COALESCE(mdp.total_weight, 0)::NUMERIC(15,3) AS weight,
|
||||
COALESCE(mdp.avg_weight, 0)::NUMERIC(15,3) AS average_weight,
|
||||
COALESCE(mdp.unit_price, 0)::NUMERIC(15,3) AS price,
|
||||
COALESCE(mdp.total_price, 0)::NUMERIC(15,3) AS final_price,
|
||||
COALESCE(mdp.total_price, 0)::NUMERIC(15,3) AS total_price,
|
||||
0::NUMERIC(15,3) AS payment_amount,
|
||||
COALESCE(STRING_AGG(DISTINCT w.name, ', ') FILTER (WHERE w.name IS NOT NULL), '') AS pickup_info,
|
||||
MAX(u.name) AS sales_person
|
||||
w.name AS pickup_info,
|
||||
u.name AS sales_person
|
||||
`).
|
||||
Joins("INNER JOIN marketing_products mp ON mp.id = mdp.marketing_product_id").
|
||||
Joins("INNER JOIN marketings m ON m.id = mp.marketing_id").
|
||||
Joins("INNER JOIN customers c ON c.id = m.customer_id").
|
||||
Joins("LEFT JOIN marketing_products mp ON mp.marketing_id = m.id").
|
||||
Joins("LEFT JOIN marketing_delivery_products mdp ON mdp.marketing_product_id = mp.id").
|
||||
Joins("LEFT JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id").
|
||||
Joins("LEFT JOIN warehouses w ON w.id = pw.warehouse_id").
|
||||
Joins("LEFT JOIN users u ON u.id = m.sales_person_id").
|
||||
Joins("INNER JOIN product_warehouses pw ON pw.id = mdp.product_warehouse_id").
|
||||
Joins("INNER JOIN warehouses w ON w.id = pw.warehouse_id").
|
||||
Joins("INNER JOIN users u ON u.id = m.sales_person_id").
|
||||
Where("mdp.delivery_date IS NOT NULL").
|
||||
Where("m.deleted_at IS NULL").
|
||||
Where("c.deleted_at IS NULL").
|
||||
Where("mdp.delivery_date IS NOT NULL")
|
||||
Where("c.deleted_at IS NULL")
|
||||
|
||||
if customerID != nil {
|
||||
salesQuery = salesQuery.Where("c.id = ?", *customerID)
|
||||
}
|
||||
|
||||
salesQuery = salesQuery.Group("m.id, c.id, m.so_date, m.so_number")
|
||||
|
||||
// Build PAYMENT subquery
|
||||
paymentQuery := r.db.WithContext(ctx).
|
||||
Table("payments p").
|
||||
Select(`
|
||||
@@ -102,9 +91,7 @@ func (r *customerPaymentRepositoryImpl) GetCustomerPaymentTransactions(ctx conte
|
||||
0::NUMERIC(15,3) AS weight,
|
||||
0::NUMERIC(15,3) AS average_weight,
|
||||
0::NUMERIC(15,3) AS price,
|
||||
0::NUMERIC(15,3) AS credit_note,
|
||||
0::NUMERIC(15,3) AS final_price,
|
||||
0::NUMERIC(15,3) AS ppn,
|
||||
0::NUMERIC(15,3) AS total_price,
|
||||
p.nominal::NUMERIC(15,3) AS payment_amount,
|
||||
'-' AS pickup_info,
|
||||
@@ -121,7 +108,6 @@ func (r *customerPaymentRepositoryImpl) GetCustomerPaymentTransactions(ctx conte
|
||||
paymentQuery = paymentQuery.Where("c.id = ?", *customerID)
|
||||
}
|
||||
|
||||
// Combine with UNION ALL and execute
|
||||
var results []CustomerPaymentTransaction
|
||||
err := r.db.WithContext(ctx).
|
||||
Raw("? UNION ALL ? ORDER BY customer_id, trans_date, transaction_type DESC, transaction_id",
|
||||
@@ -161,12 +147,13 @@ func (r *customerPaymentRepositoryImpl) GetInitialBalanceByCustomer(ctx context.
|
||||
}
|
||||
|
||||
func (r *customerPaymentRepositoryImpl) GetCustomerIDsWithTransactions(ctx context.Context, limit, offset int) ([]uint, int64, error) {
|
||||
// Subquery to get all distinct customer IDs with transactions
|
||||
subQuery := r.db.WithContext(ctx).
|
||||
Table("(" +
|
||||
"SELECT DISTINCT c.id as customer_id FROM marketings m " +
|
||||
"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 m.deleted_at IS NULL AND c.deleted_at IS NULL " +
|
||||
"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 " +
|
||||
@@ -174,19 +161,19 @@ func (r *customerPaymentRepositoryImpl) GetCustomerIDsWithTransactions(ctx conte
|
||||
"AND p.transaction_type = 'PENJUALAN' AND p.deleted_at IS NULL AND c.deleted_at IS NULL" +
|
||||
") as customer_ids")
|
||||
|
||||
// Count total customers
|
||||
var total int64
|
||||
if err := subQuery.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Get paginated customer IDs
|
||||
var customerIDs []uint
|
||||
err := r.db.WithContext(ctx).
|
||||
Table("("+
|
||||
"SELECT DISTINCT c.id as customer_id FROM marketings m "+
|
||||
"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 m.deleted_at IS NULL AND c.deleted_at IS NULL "+
|
||||
"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 "+
|
||||
|
||||
Reference in New Issue
Block a user