mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
feat(BE-74-76-78-278):adjustment project flock,recording,purchase getall
This commit is contained in:
@@ -25,6 +25,7 @@ type PurchaseRepository interface {
|
||||
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
|
||||
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)
|
||||
}
|
||||
@@ -89,6 +90,44 @@ WHERE pi.purchase_id = ?
|
||||
return r.DB().WithContext(ctx).Exec(query, purchaseID).Error
|
||||
}
|
||||
|
||||
func (r *PurchaseRepositoryImpl) SoftDeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) error {
|
||||
if len(projectFlockKandangIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return r.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var purchaseIDs []uint
|
||||
query := `
|
||||
SELECT pi.purchase_id
|
||||
FROM purchase_items pi
|
||||
WHERE pi.project_flock_kandang_id IN (?)
|
||||
GROUP BY pi.purchase_id
|
||||
HAVING COUNT(*) = COUNT(CASE WHEN pi.project_flock_kandang_id IN (?) THEN 1 END)
|
||||
`
|
||||
if err := tx.Raw(query, projectFlockKandangIDs, projectFlockKandangIDs).Scan(&purchaseIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
if len(purchaseIDs) > 0 {
|
||||
if err := tx.Model(&entity.Purchase{}).
|
||||
Where("id IN (?) AND deleted_at IS NULL", purchaseIDs).
|
||||
Update("deleted_at", now).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("purchase_id IN (?)", purchaseIDs).Delete(&entity.PurchaseItem{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
deleteItems := tx.Where("project_flock_kandang_id IN (?)", projectFlockKandangIDs)
|
||||
if len(purchaseIDs) > 0 {
|
||||
deleteItems = deleteItems.Where("purchase_id NOT IN (?)", purchaseIDs)
|
||||
}
|
||||
return deleteItems.Delete(&entity.PurchaseItem{}).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (r *PurchaseRepositoryImpl) CreateItems(ctx context.Context, purchaseID uint, items []*entity.PurchaseItem) error {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
|
||||
@@ -133,6 +133,7 @@ func (s *purchaseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
|
||||
purchases, total, err := s.PurchaseRepo.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
db = db.Where("purchases.deleted_at IS NULL")
|
||||
|
||||
if params.SupplierID > 0 {
|
||||
db = db.Where("supplier_id = ?", params.SupplierID)
|
||||
|
||||
Reference in New Issue
Block a user