2020-01-14 14:14:16 -05:00
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2025-06-30 22:08:08 +12:00
|
|
|
|
2020-01-14 14:14:16 -05:00
|
|
|
"github.com/openaccounting/oa-server/core/model/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type BudgetInterface interface {
|
|
|
|
|
GetBudget(string, string) (*types.Budget, error)
|
|
|
|
|
CreateBudget(*types.Budget, string) error
|
|
|
|
|
DeleteBudget(string, string) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (model *Model) GetBudget(orgId string, userId string) (*types.Budget, error) {
|
|
|
|
|
belongs, err := model.UserBelongsToOrg(userId, orgId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 22:08:08 +12:00
|
|
|
if !belongs {
|
|
|
|
|
return nil, errors.New("user does not belong to org")
|
2020-01-14 14:14:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return model.db.GetBudget(orgId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (model *Model) CreateBudget(budget *types.Budget, userId string) error {
|
|
|
|
|
belongs, err := model.UserBelongsToOrg(userId, budget.OrgId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 22:08:08 +12:00
|
|
|
if !belongs {
|
|
|
|
|
return errors.New("user does not belong to org")
|
2020-01-14 14:14:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if budget.OrgId == "" {
|
|
|
|
|
return errors.New("orgId required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return model.db.InsertAndReplaceBudget(budget)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (model *Model) DeleteBudget(orgId string, userId string) error {
|
|
|
|
|
belongs, err := model.UserBelongsToOrg(userId, orgId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 22:08:08 +12:00
|
|
|
if !belongs {
|
|
|
|
|
return errors.New("user does not belong to org")
|
2020-01-14 14:14:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return model.db.DeleteBudget(orgId)
|
|
|
|
|
}
|