feat[BE-384]: enhance closing reports by introducing calculation context and improving data handling; refactor related functions for better clarity and maintainability

This commit is contained in:
aguhh18
2025-12-19 08:30:05 +07:00
parent e551995c66
commit fa6d82b79a
8 changed files with 286 additions and 167 deletions
@@ -26,6 +26,7 @@ type PurchaseRepository interface {
NextPoNumber(ctx context.Context, tx *gorm.DB) (string, error)
BackfillProjectFlockKandang(ctx context.Context, purchaseID uint) error
GetItemsByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.PurchaseItem, error)
GetItemsByWarehouseKandang(ctx context.Context, projectFlockID uint) ([]entity.PurchaseItem, error)
}
type PurchaseRepositoryImpl struct {
@@ -291,13 +292,34 @@ func (r *PurchaseRepositoryImpl) numberExists(ctx context.Context, db *gorm.DB,
}
func (r *PurchaseRepositoryImpl) GetItemsByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.PurchaseItem, error) {
return r.GetItemsByWarehouseKandang(ctx, projectFlockID)
}
func (r *PurchaseRepositoryImpl) GetItemsByWarehouseKandang(ctx context.Context, projectFlockID uint) ([]entity.PurchaseItem, error) {
var items []entity.PurchaseItem
var kandangIDs []uint
err := r.DB().WithContext(ctx).
Table("project_flock_kandangs").
Where("project_flock_id = ?", projectFlockID).
Pluck("kandang_id", &kandangIDs).Error
if err != nil {
return nil, err
}
if len(kandangIDs) == 0 {
return []entity.PurchaseItem{}, nil
}
err = r.DB().WithContext(ctx).
Preload("Product").
Preload("Product.Flags").
Joins("JOIN project_flock_kandangs ON project_flock_kandangs.id = purchase_items.project_flock_kandang_id").
Where("project_flock_kandangs.project_flock_id = ?", projectFlockID).
Joins("JOIN warehouses ON warehouses.id = purchase_items.warehouse_id").
Where("warehouses.kandang_id IN ?", kandangIDs).
Find(&items).Error
return items, err
}