mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
add sorting transaction, report keuangan
This commit is contained in:
@@ -97,6 +97,8 @@ func (u *TransactionController) GetAll(c *fiber.Ctx) error {
|
||||
CustomerIDs: customerIDs,
|
||||
SupplierIDs: supplierIDs,
|
||||
SortDate: c.Query("sort_date", ""),
|
||||
SortBy: c.Query("sort_by", ""),
|
||||
SortOrder: c.Query("sort_order", ""),
|
||||
StartDate: c.Query("start_date", ""),
|
||||
EndDate: c.Query("end_date", ""),
|
||||
}
|
||||
|
||||
@@ -72,17 +72,24 @@ func (s transactionService) GetAll(c *fiber.Ctx, params *validation.Query) ([]en
|
||||
transactions, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
|
||||
if params.Search != "" {
|
||||
like := "%" + strings.ToLower(strings.TrimSpace(params.Search)) + "%"
|
||||
needsPartyJoin := params.Search != "" || params.SortBy == "customer_name"
|
||||
needsBankJoin := params.Search != "" || params.SortBy == "bank"
|
||||
|
||||
if needsPartyJoin {
|
||||
db = db.Joins(
|
||||
"LEFT JOIN customers ON customers.id = payments.party_id AND payments.party_type = ? AND customers.deleted_at IS NULL",
|
||||
string(utils.PaymentPartyCustomer),
|
||||
).Joins(
|
||||
"LEFT JOIN suppliers ON suppliers.id = payments.party_id AND payments.party_type = ? AND suppliers.deleted_at IS NULL",
|
||||
string(utils.PaymentPartySupplier),
|
||||
).Joins(
|
||||
"LEFT JOIN banks ON banks.id = payments.bank_id AND banks.deleted_at IS NULL",
|
||||
)
|
||||
}
|
||||
if needsBankJoin {
|
||||
db = db.Joins("LEFT JOIN banks ON banks.id = payments.bank_id AND banks.deleted_at IS NULL")
|
||||
}
|
||||
|
||||
if params.Search != "" {
|
||||
like := "%" + strings.ToLower(strings.TrimSpace(params.Search)) + "%"
|
||||
db = db.Where(
|
||||
`LOWER(payment_code) LIKE ? OR
|
||||
LOWER(COALESCE(reference_number, '')) LIKE ? OR
|
||||
@@ -138,7 +145,7 @@ func (s transactionService) GetAll(c *fiber.Ctx, params *validation.Query) ([]en
|
||||
db = db.Where("payment_date < ?", *endDate)
|
||||
}
|
||||
|
||||
return applyTransactionSort(db, params.SortDate)
|
||||
return applyTransactionSort(db, params.SortBy, params.SortOrder, params.SortDate)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -270,13 +277,39 @@ func parseTransactionDateRange(startDate, endDate string) (*time.Time, *time.Tim
|
||||
return startPtr, endPtr, nil
|
||||
}
|
||||
|
||||
func applyTransactionSort(db *gorm.DB, sortDate string) *gorm.DB {
|
||||
func applyTransactionSort(db *gorm.DB, sortBy, sortOrder, sortDate string) *gorm.DB {
|
||||
order := "DESC"
|
||||
if strings.ToUpper(strings.TrimSpace(sortOrder)) == "ASC" {
|
||||
order = "ASC"
|
||||
}
|
||||
|
||||
switch strings.ToLower(strings.TrimSpace(sortBy)) {
|
||||
case "payment_code":
|
||||
return db.Order("payments.payment_code " + order)
|
||||
case "reference_number":
|
||||
return db.Order("payments.reference_number " + order)
|
||||
case "transaction_type":
|
||||
return db.Order("payments.transaction_type " + order)
|
||||
case "customer_name":
|
||||
return db.Order("COALESCE(customers.name, suppliers.name) " + order)
|
||||
case "payment_date":
|
||||
return db.Order("payments.payment_date " + order)
|
||||
case "created_at":
|
||||
return db.Order("payments.created_at " + order)
|
||||
case "payment_method":
|
||||
return db.Order("payments.payment_method " + order)
|
||||
case "bank":
|
||||
return db.Order("banks.account_number " + order)
|
||||
case "expense_amount":
|
||||
return db.Order("CASE WHEN payments.direction = 'OUT' THEN payments.nominal ELSE 0 END " + order)
|
||||
case "income_amount":
|
||||
return db.Order("CASE WHEN payments.direction = 'IN' THEN payments.nominal ELSE 0 END " + order)
|
||||
}
|
||||
|
||||
switch strings.ToLower(strings.TrimSpace(sortDate)) {
|
||||
case "created_at":
|
||||
return db.Order("created_at DESC").Order("payment_date DESC")
|
||||
case "payment_date":
|
||||
return db.Order("payment_date DESC").Order("created_at DESC")
|
||||
return db.Order("payments.created_at DESC").Order("payments.payment_date DESC")
|
||||
default:
|
||||
return db.Order("payment_date DESC").Order("created_at DESC")
|
||||
return db.Order("payments.payment_date DESC").Order("payments.created_at DESC")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ type Query struct {
|
||||
CustomerIDs []uint `query:"customer_ids" validate:"omitempty,dive,gt=0"`
|
||||
SupplierIDs []uint `query:"supplier_ids" validate:"omitempty,dive,gt=0"`
|
||||
SortDate string `query:"sort_date" validate:"omitempty,oneof=created_at payment_date"`
|
||||
SortBy string `query:"sort_by" validate:"omitempty,oneof=payment_code reference_number transaction_type customer_name payment_date created_at payment_method bank expense_amount income_amount"`
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -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 " +
|
||||
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 FROM payments p " +
|
||||
"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")
|
||||
") 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"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user