mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 07:45:44 +00:00
feat: export input progress report for expenses, marketings, purchases, and recordings
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/exportprogress"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -28,6 +29,7 @@ type PurchaseRepository interface {
|
||||
SoftDeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) error
|
||||
GetItemsByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.PurchaseItem, error)
|
||||
GetItemsByWarehouseKandang(ctx context.Context, projectFlockID uint) ([]entity.PurchaseItem, error)
|
||||
GetProgressRows(ctx context.Context, startDate, endDate time.Time, allowedLocationIDs []uint, restrict bool) ([]exportprogress.Row, error)
|
||||
}
|
||||
|
||||
type PurchaseRepositoryImpl struct {
|
||||
@@ -284,6 +286,75 @@ func (r *PurchaseRepositoryImpl) UpdateReceivingDetails(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *PurchaseRepositoryImpl) GetProgressRows(ctx context.Context, startDate, endDate time.Time, allowedLocationIDs []uint, restrict bool) ([]exportprogress.Row, error) {
|
||||
const unassignedSQL = "'" + exportprogress.UnassignedKandangName + "'"
|
||||
subQuery := r.DB().WithContext(ctx).
|
||||
Table("purchases AS p").
|
||||
Select(`
|
||||
DISTINCT p.id AS purchase_id,
|
||||
'Purchases' AS module,
|
||||
COALESCE(pf_explicit.flock_name, pf_active.flock_name, kandang_loc.name, warehouse_loc.name, 'Unknown Farm') AS farm_name,
|
||||
COALESCE(k_explicit.name, k_active.name, wk.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name,
|
||||
DATE(p.po_date) AS activity_date
|
||||
`).
|
||||
Joins("JOIN purchase_items pi ON pi.purchase_id = p.id").
|
||||
Joins("JOIN warehouses w ON w.id = pi.warehouse_id").
|
||||
Joins("LEFT JOIN project_flock_kandangs pfk_explicit ON pfk_explicit.id = pi.project_flock_kandang_id").
|
||||
Joins("LEFT JOIN project_flocks pf_explicit ON pf_explicit.id = pfk_explicit.project_flock_id").
|
||||
Joins("LEFT JOIN kandangs k_explicit ON k_explicit.id = pfk_explicit.kandang_id").
|
||||
Joins("LEFT JOIN project_flock_kandangs pfk_active ON pfk_active.kandang_id = w.kandang_id AND pfk_active.closed_at IS NULL").
|
||||
Joins("LEFT JOIN project_flocks pf_active ON pf_active.id = pfk_active.project_flock_id").
|
||||
Joins("LEFT JOIN kandangs k_active ON k_active.id = pfk_active.kandang_id").
|
||||
Joins("LEFT JOIN kandangs wk ON wk.id = w.kandang_id").
|
||||
Joins("LEFT JOIN locations kandang_loc ON kandang_loc.id = COALESCE(k_explicit.location_id, k_active.location_id, wk.location_id)").
|
||||
Joins("LEFT JOIN locations warehouse_loc ON warehouse_loc.id = w.location_id").
|
||||
Where("p.deleted_at IS NULL").
|
||||
Where("p.po_date IS NOT NULL").
|
||||
Where("DATE(p.po_date) >= DATE(?)", startDate).
|
||||
Where("DATE(p.po_date) <= DATE(?)", endDate)
|
||||
|
||||
if restrict {
|
||||
if len(allowedLocationIDs) == 0 {
|
||||
return []exportprogress.Row{}, nil
|
||||
}
|
||||
subQuery = subQuery.Where("w.location_id IN ?", allowedLocationIDs)
|
||||
}
|
||||
|
||||
type progressRowResult struct {
|
||||
Module string
|
||||
FarmName string
|
||||
KandangName string
|
||||
ActivityDate string
|
||||
Count int
|
||||
}
|
||||
scanned := make([]progressRowResult, 0)
|
||||
err := r.DB().WithContext(ctx).
|
||||
Table("(?) AS progress_rows", subQuery).
|
||||
Select("module, farm_name, kandang_name, activity_date, COUNT(*) AS count").
|
||||
Group("module, farm_name, kandang_name, activity_date").
|
||||
Order("activity_date ASC, farm_name ASC, kandang_name ASC").
|
||||
Scan(&scanned).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := make([]exportprogress.Row, 0, len(scanned))
|
||||
for _, item := range scanned {
|
||||
activityDate, err := time.Parse("2006-01-02", item.ActivityDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows = append(rows, exportprogress.Row{
|
||||
Module: item.Module,
|
||||
FarmName: item.FarmName,
|
||||
KandangName: item.KandangName,
|
||||
ActivityDate: activityDate,
|
||||
Count: item.Count,
|
||||
})
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (r *PurchaseRepositoryImpl) DeleteItems(ctx context.Context, purchaseID uint, itemIDs []uint) error {
|
||||
if len(itemIDs) == 0 {
|
||||
return errors.New("itemIDs cannot be empty")
|
||||
|
||||
Reference in New Issue
Block a user