feat/BE/US-278/TASK-288,289-adjust schema database,Create trigger in expense module, add filter in warehouse linked to project flock

This commit is contained in:
ragilap
2025-12-08 01:23:21 +07:00
parent 4638fba318
commit 0a18753dde
10 changed files with 322 additions and 195 deletions
@@ -24,6 +24,7 @@ type PurchaseRepository interface {
DeleteItems(ctx context.Context, purchaseID uint, itemIDs []uint) error
NextPrNumber(ctx context.Context, tx *gorm.DB) (string, error)
NextPoNumber(ctx context.Context, tx *gorm.DB) (string, error)
BackfillProjectFlockKandang(ctx context.Context, purchaseID uint) error
}
type PurchaseRepositoryImpl struct {
@@ -58,6 +59,34 @@ func (r *PurchaseRepositoryImpl) CreateWithItems(ctx context.Context, purchase *
return nil
}
func (r *PurchaseRepositoryImpl) BackfillProjectFlockKandang(ctx context.Context, purchaseID uint) error {
if purchaseID == 0 {
return nil
}
query := `
WITH latest_pfk AS (
SELECT pfk.id, pfk.kandang_id
FROM project_flock_kandangs pfk
JOIN (
SELECT DISTINCT ON (approvable_id) approvable_id, step_name, action_at
FROM approvals
WHERE approvable_type = 'PROJECT_FLOCKS'
ORDER BY approvable_id, action_at DESC
) latest_approval ON latest_approval.approvable_id = pfk.project_flock_id
WHERE LOWER(latest_approval.step_name) = LOWER('Aktif')
)
UPDATE purchase_items pi
SET project_flock_kandang_id = lp.id
FROM warehouses w
JOIN latest_pfk lp ON lp.kandang_id = w.kandang_id
WHERE pi.purchase_id = ?
AND pi.project_flock_kandang_id IS NULL
AND pi.warehouse_id = w.id;
`
return r.DB().WithContext(ctx).Exec(query, purchaseID).Error
}
func (r *PurchaseRepositoryImpl) CreateItems(ctx context.Context, purchaseID uint, items []*entity.PurchaseItem) error {
if len(items) == 0 {
return nil