| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Services;
- use App\Models\FinancialPlanAccount;
- use Illuminate\Database\Eloquent\Collection;
- class FinancialPlanAccountService
- {
- /**
- * Plano de contas por escopo: franqueador (matriz) usa unit_id null;
- * franchisee usa o unit_id da unidade. Ordenado por código (1.1, 1.1.1, ...).
- */
- public function getAll(?int $unitId = null): Collection
- {
- return FinancialPlanAccount::query()
- ->when(
- $unitId !== null,
- fn ($query) => $query->where('unit_id', $unitId),
- fn ($query) => $query->whereNull('unit_id'),
- )
- ->orderBy('code')
- ->get();
- }
- public function findById(int $id): ?FinancialPlanAccount
- {
- return FinancialPlanAccount::find($id);
- }
- public function create(array $data): FinancialPlanAccount
- {
- $data['unit_id'] = $data['unit_id'] ?? null;
- $data['order'] = $data['order'] ?? $data['code'];
- return FinancialPlanAccount::create($data);
- }
- public function update(int $id, array $data): ?FinancialPlanAccount
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $model->update($data);
- return $model->fresh();
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- // Add custom business logic methods here
- }
|