add sorting transaction, report keuangan

This commit is contained in:
giovanni
2026-05-16 23:40:52 +07:00
parent a40adc22d2
commit 18bd8ad1d9
8 changed files with 127 additions and 49 deletions
@@ -362,6 +362,7 @@ func (c *RepportController) GetDebtSupplier(ctx *fiber.Ctx) error {
StartDate: ctx.Query("start_date", ""),
EndDate: ctx.Query("end_date", ""),
FilterBy: ctx.Query("filter_by", ""),
SortBy: ctx.Query("sort_by", ""),
SortOrder: ctx.Query("sort_order", ""),
}
@@ -459,6 +460,8 @@ func (c *RepportController) GetCustomerPayment(ctx *fiber.Ctx) error {
Limit: ctx.QueryInt("limit", 10),
CustomerIDs: customerIDs,
FilterBy: strings.ToUpper(ctx.Query("filter_by", "")),
SortBy: ctx.Query("sort_by", ""),
SortOrder: ctx.Query("sort_order", ""),
StartDate: ctx.Query("start_date", ""),
EndDate: ctx.Query("end_date", ""),
}
@@ -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
}
@@ -1029,6 +1029,13 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
}
func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.CustomerPaymentQuery) ([]dto.CustomerPaymentReportItem, int64, error) {
if params.SortBy == "" {
params.SortBy = "customer"
}
if params.SortOrder == "" {
params.SortOrder = "asc"
}
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
@@ -1083,7 +1090,7 @@ func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.C
offset := (page - 1) * limit
var err error
customerIDs, totalCustomers, err = s.CustomerPaymentRepo.GetCustomerIDsWithTransactions(ctx.Context(), limit, offset, allowedCustomerIDs)
customerIDs, totalCustomers, err = s.CustomerPaymentRepo.GetCustomerIDsWithTransactions(ctx.Context(), limit, offset, allowedCustomerIDs, params.SortBy, params.SortOrder)
if err != nil {
return nil, 0, err
}
@@ -1755,6 +1762,12 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
if params.FilterBy == "" {
params.FilterBy = "received_date"
}
if params.SortBy == "" {
params.SortBy = "supplier"
}
if params.SortOrder == "" {
params.SortOrder = "asc"
}
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
@@ -58,6 +58,7 @@ type DebtSupplierQuery struct {
StartDate string `query:"start_date" validate:"omitempty,datetime=2006-01-02"`
EndDate string `query:"end_date" validate:"omitempty,datetime=2006-01-02"`
FilterBy string `query:"filter_by" validate:"omitempty,oneof=received_date po_date"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=supplier"`
SortOrder string `query:"sort_order" validate:"omitempty,oneof=asc desc"`
AllowedAreaIDs []int64 `query:"-"`
AllowedLocationIDs []int64 `query:"-"`
@@ -108,6 +109,8 @@ type CustomerPaymentQuery struct {
Limit int `query:"limit" validate:"omitempty,min=1,gt=0"`
CustomerIDs []uint `query:"customer_ids" validate:"omitempty,dive,gt=0"`
FilterBy string `query:"filter_by" validate:"omitempty,oneof=TRANS_DATE REALIZATION_DATE"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=customer"`
SortOrder string `query:"sort_order" validate:"omitempty,oneof=asc desc"`
StartDate string `query:"start_date" validate:"omitempty,datetime=2006-01-02"`
EndDate string `query:"end_date" validate:"omitempty,datetime=2006-01-02"`
}