FinancialPlanAccountService.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 findByIdForUnit(int $id, int $unitId): ?FinancialPlanAccount
  27. {
  28. return FinancialPlanAccount::where('id', $id)->where('unit_id', $unitId)->first();
  29. }
  30. public function create(array $data): FinancialPlanAccount
  31. {
  32. $data['unit_id'] = $data['unit_id'] ?? null;
  33. $data['order'] = $data['order'] ?? $data['code'];
  34. return FinancialPlanAccount::create($data);
  35. }
  36. public function update(int $id, array $data): ?FinancialPlanAccount
  37. {
  38. $model = $this->findById($id);
  39. if (!$model) {
  40. return null;
  41. }
  42. $model->update($data);
  43. return $model->fresh();
  44. }
  45. public function delete(int $id): bool
  46. {
  47. $model = $this->findById($id);
  48. if (!$model) {
  49. return false;
  50. }
  51. return $model->delete();
  52. }
  53. // Add custom business logic methods here
  54. }