FinancialPlanAccountService.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Services;
  3. use App\Models\FinancialPlanAccount;
  4. use Illuminate\Database\Eloquent\Collection;
  5. class FinancialPlanAccountService
  6. {
  7. /**
  8. * Plano de contas por escopo: franqueador (matriz) usa unit_id null;
  9. * franchisee usa o unit_id da unidade. Ordenado por código (1.1, 1.1.1, ...).
  10. */
  11. public function getAll(?int $unitId = null): Collection
  12. {
  13. return FinancialPlanAccount::query()
  14. ->when(
  15. $unitId !== null,
  16. fn ($query) => $query->where('unit_id', $unitId),
  17. fn ($query) => $query->whereNull('unit_id'),
  18. )
  19. ->orderBy('code')
  20. ->get();
  21. }
  22. public function findById(int $id): ?FinancialPlanAccount
  23. {
  24. return FinancialPlanAccount::find($id);
  25. }
  26. public function create(array $data): FinancialPlanAccount
  27. {
  28. $data['unit_id'] = $data['unit_id'] ?? null;
  29. $data['order'] = $data['order'] ?? $data['code'];
  30. return FinancialPlanAccount::create($data);
  31. }
  32. public function update(int $id, array $data): ?FinancialPlanAccount
  33. {
  34. $model = $this->findById($id);
  35. if (!$model) {
  36. return null;
  37. }
  38. $model->update($data);
  39. return $model->fresh();
  40. }
  41. public function delete(int $id): bool
  42. {
  43. $model = $this->findById($id);
  44. if (!$model) {
  45. return false;
  46. }
  47. return $model->delete();
  48. }
  49. // Add custom business logic methods here
  50. }