mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ExpenseRepository interface {
|
|
repository.BaseRepository[entity.Expense]
|
|
IdExists(ctx context.Context, id uint) (bool, error)
|
|
GetNextSequence(ctx context.Context) (int, error)
|
|
GetWithSupplier(ctx context.Context, id uint64) (*entity.Expense, error)
|
|
WithProjectFlockKandangFilter(pfkID, kandangID uint) func(*gorm.DB) *gorm.DB
|
|
CountUnfinishedByProjectFlockKandang(ctx context.Context, pfkID, kandangID uint, isFinished func(*entity.Approval) bool) (int64, error)
|
|
}
|
|
|
|
type ExpenseRepositoryImpl struct {
|
|
*repository.BaseRepositoryImpl[entity.Expense]
|
|
}
|
|
|
|
func NewExpenseRepository(db *gorm.DB) ExpenseRepository {
|
|
return &ExpenseRepositoryImpl{
|
|
BaseRepositoryImpl: repository.NewBaseRepository[entity.Expense](db),
|
|
}
|
|
}
|
|
|
|
func (r *ExpenseRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
|
|
return repository.Exists[entity.Expense](ctx, r.DB(), id)
|
|
}
|
|
|
|
func (r *ExpenseRepositoryImpl) GetNextSequence(ctx context.Context) (int, error) {
|
|
var sequence int
|
|
err := r.DB().Raw("SELECT nextval('expenses_ref_seq')").Scan(&sequence).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return sequence, nil
|
|
}
|
|
|
|
func (r *ExpenseRepositoryImpl) GetWithSupplier(ctx context.Context, id uint64) (*entity.Expense, error) {
|
|
var expense entity.Expense
|
|
err := r.DB().WithContext(ctx).
|
|
Where("id = ?", id).
|
|
Preload("Supplier").
|
|
First(&expense).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &expense, nil
|
|
}
|
|
|
|
func (r *ExpenseRepositoryImpl) WithProjectFlockKandangFilter(pfkID, kandangID uint) func(*gorm.DB) *gorm.DB {
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
if pfkID == 0 && kandangID == 0 {
|
|
return db
|
|
}
|
|
q := db.Joins("JOIN expense_nonstocks ON expense_nonstocks.expense_id = expenses.id")
|
|
if pfkID > 0 && kandangID > 0 {
|
|
return q.Where("expense_nonstocks.project_flock_kandang_id = ? OR expense_nonstocks.kandang_id = ?", pfkID, kandangID)
|
|
}
|
|
if pfkID > 0 {
|
|
return q.Where("expense_nonstocks.project_flock_kandang_id = ?", pfkID)
|
|
}
|
|
return q.Where("expense_nonstocks.kandang_id = ?", kandangID)
|
|
}
|
|
}
|
|
|
|
func (r *ExpenseRepositoryImpl) CountUnfinishedByProjectFlockKandang(ctx context.Context, pfkID, kandangID uint, isFinished func(*entity.Approval) bool) (int64, error) {
|
|
if pfkID == 0 && kandangID == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
var ids []uint64
|
|
if err := r.DB().WithContext(ctx).
|
|
Table("expenses").
|
|
Scopes(r.WithProjectFlockKandangFilter(pfkID, kandangID)).
|
|
Group("expenses.id").
|
|
Pluck("expenses.id", &ids).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
if len(ids) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
var unfinished int64
|
|
for _, id := range ids {
|
|
var latest entity.Approval
|
|
err := r.DB().WithContext(ctx).
|
|
Table("approvals").
|
|
Where("approvable_type = ? AND approvable_id = ?", utils.ApprovalWorkflowExpense.String(), id).
|
|
Order("action_at DESC").
|
|
Limit(1).
|
|
First(&latest).Error
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return 0, err
|
|
}
|
|
if isFinished != nil {
|
|
if !isFinished(&latest) {
|
|
unfinished++
|
|
}
|
|
}
|
|
}
|
|
return unfinished, nil
|
|
}
|