CompanyAccountReceivableService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Services;
  3. use App\Models\CompanyAccountReceivable;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Validation\ValidationException;
  7. /**
  8. * Contas a Receber avulsas da matriz (franqueadora). Baixa manual.
  9. */
  10. class CompanyAccountReceivableService
  11. {
  12. public function list(array $filters = []): Collection
  13. {
  14. return CompanyAccountReceivable::with('planAccount:id,code,description')
  15. ->when(
  16. isset($filters['status']),
  17. fn ($q) => $q->where('status', $filters['status']),
  18. )
  19. ->orderBy('due_date')
  20. ->get();
  21. }
  22. public function findById(int $id): ?CompanyAccountReceivable
  23. {
  24. return CompanyAccountReceivable::find($id);
  25. }
  26. public function createManual(array $data): CompanyAccountReceivable
  27. {
  28. return CompanyAccountReceivable::create([
  29. 'origin' => CompanyAccountReceivable::ORIGIN_MANUAL,
  30. 'financial_plan_account_id' => $data['financial_plan_account_id'] ?? null,
  31. 'history' => $data['history'],
  32. 'value' => $data['value'],
  33. 'due_date' => $data['due_date'],
  34. 'obs' => $data['obs'] ?? null,
  35. 'paid_value' => 0,
  36. 'discount' => 0,
  37. 'fine' => 0,
  38. 'status' => 'pending',
  39. ])->load('planAccount:id,code,description');
  40. }
  41. public function settle(CompanyAccountReceivable $receivable, array $data): CompanyAccountReceivable
  42. {
  43. if ($receivable->status === 'paid') {
  44. throw ValidationException::withMessages(['status' => 'Este recebível já está pago.']);
  45. }
  46. if ($receivable->status === 'cancelled') {
  47. throw ValidationException::withMessages([
  48. 'status' => 'Este recebível está cancelado e não pode receber baixa.',
  49. ]);
  50. }
  51. $discount = isset($data['discount']) ? (float) $data['discount'] : (float) $receivable->discount;
  52. $fine = isset($data['fine']) ? (float) $data['fine'] : (float) $receivable->fine;
  53. $paidValue = isset($data['paid_value'])
  54. ? (float) $data['paid_value']
  55. : round((float) $receivable->value - $discount + $fine, 2);
  56. $receivable->update([
  57. 'status' => 'paid',
  58. 'discount' => $discount,
  59. 'fine' => $fine,
  60. 'paid_value' => $paidValue,
  61. 'payment_date' => $data['payment_date'] ?? Carbon::today()->format('Y-m-d'),
  62. ]);
  63. return $receivable->fresh('planAccount:id,code,description');
  64. }
  65. public function reopen(CompanyAccountReceivable $receivable): CompanyAccountReceivable
  66. {
  67. if ($receivable->status !== 'paid') {
  68. throw ValidationException::withMessages([
  69. 'status' => 'Só é possível reabrir recebíveis que estão pagos.',
  70. ]);
  71. }
  72. $receivable->update([
  73. 'status' => 'pending',
  74. 'paid_value' => 0,
  75. 'payment_date' => null,
  76. ]);
  77. return $receivable->fresh('planAccount:id,code,description');
  78. }
  79. }