FranchisorReceivableService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\NotificationType;
  4. use App\Models\FranchiseeAccountReceive;
  5. use App\Models\UnitAccountPayable;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Validation\ValidationException;
  9. class FranchisorReceivableService
  10. {
  11. public function __construct(
  12. protected NotificationService $notifications,
  13. ) {}
  14. public function createManual(array $data): FranchiseeAccountReceive
  15. {
  16. return DB::transaction(function () use ($data) {
  17. $unitId = $data['account_type'] === 'unit' ? (int) $data['unit_id'] : null;
  18. $receivable = FranchiseeAccountReceive::create([
  19. 'unit_id' => $unitId,
  20. 'tbr_calculation_id' => null,
  21. 'financial_plan_account_id' => $data['financial_plan_account_id'] ?? null,
  22. 'origin' => $unitId ? 'manual_unit' : 'manual_internal',
  23. 'history' => $data['history'],
  24. 'value' => $data['value'],
  25. 'paid_value' => 0,
  26. 'due_date' => $data['due_date'],
  27. 'discount' => 0,
  28. 'fees' => 0,
  29. 'obs' => $data['obs'] ?? null,
  30. 'status' => 'pending',
  31. ]);
  32. if ($unitId) {
  33. UnitAccountPayable::create([
  34. 'unit_id' => $unitId,
  35. 'franchisee_account_receive_id' => $receivable->id,
  36. 'origin' => UnitAccountPayable::ORIGIN_MANUAL,
  37. 'history' => $receivable->history,
  38. 'value' => $receivable->value,
  39. 'paid_value' => 0,
  40. 'discount' => 0,
  41. 'fine' => 0,
  42. 'due_date' => $receivable->due_date,
  43. 'status' => 'pending',
  44. 'obs' => $receivable->obs,
  45. ]);
  46. $this->notifications->dispatch([
  47. 'origin_table' => 'franchisee_account_receives',
  48. 'origin_id' => $receivable->id,
  49. 'unit_id' => $unitId,
  50. 'type' => NotificationType::ACCOUNT_PAYABLE_CREATED->value,
  51. 'title' => 'Nova conta a pagar',
  52. 'message' => "Uma nova conta a pagar foi criada: {$receivable->history}.",
  53. 'url' => '/financial/accounts-payable',
  54. 'action_text' => 'Ver Contas a Pagar',
  55. 'priority' => 'normal',
  56. 'recipient_user_ids' => $this->notifications->unitUserIds($unitId),
  57. ]);
  58. }
  59. return $receivable->load(['unit', 'details', 'planAccount']);
  60. });
  61. }
  62. public function settle(FranchiseeAccountReceive $receivable, array $data): FranchiseeAccountReceive
  63. {
  64. if ($receivable->status->value === 'paid') {
  65. throw ValidationException::withMessages(['status' => 'Esta conta já está paga.']);
  66. }
  67. return DB::transaction(function () use ($receivable, $data) {
  68. $receivable->update([
  69. 'status' => 'paid',
  70. 'paid_value' => $receivable->value,
  71. 'payment_date' => $data['payment_date'] ?? Carbon::today()->format('Y-m-d'),
  72. ]);
  73. UnitAccountPayable::where('franchisee_account_receive_id', $receivable->id)
  74. ->update([
  75. 'status' => 'paid',
  76. 'paid_value' => $receivable->value,
  77. 'payment_date' => $data['payment_date'] ?? Carbon::today()->format('Y-m-d'),
  78. ]);
  79. if ($receivable->unit_id) {
  80. $this->notifications->dispatch([
  81. 'origin_table' => 'franchisee_account_receives',
  82. 'origin_id' => $receivable->id,
  83. 'unit_id' => $receivable->unit_id,
  84. 'type' => NotificationType::PAYMENT_CREDITED->value,
  85. 'title' => 'Pagamento confirmado',
  86. 'message' => "Sua cobrança {$receivable->history} foi devidamente creditada e consta como paga.",
  87. 'url' => '/financial/accounts-payable',
  88. 'action_text' => 'Ver Contas a Pagar',
  89. 'priority' => 'normal',
  90. 'recipient_user_ids' => $this->notifications->unitUserIds($receivable->unit_id),
  91. ]);
  92. }
  93. return $receivable->fresh(['unit', 'details', 'planAccount']);
  94. });
  95. }
  96. public function reopen(FranchiseeAccountReceive $receivable): FranchiseeAccountReceive
  97. {
  98. if ($receivable->status->value !== 'paid') {
  99. throw ValidationException::withMessages(['status' => 'Esta conta não está paga.']);
  100. }
  101. return DB::transaction(function () use ($receivable) {
  102. $receivable->update([
  103. 'status' => 'pending',
  104. 'paid_value' => 0,
  105. 'payment_date' => null,
  106. ]);
  107. UnitAccountPayable::where('franchisee_account_receive_id', $receivable->id)
  108. ->update(['status' => 'pending', 'paid_value' => 0, 'payment_date' => null]);
  109. return $receivable->fresh(['unit', 'details', 'planAccount']);
  110. });
  111. }
  112. }