FranchisorReceivableService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. return $receivable->fresh(['unit', 'details', 'planAccount']);
  80. });
  81. }
  82. public function reopen(FranchiseeAccountReceive $receivable): FranchiseeAccountReceive
  83. {
  84. if ($receivable->status->value !== 'paid') {
  85. throw ValidationException::withMessages(['status' => 'Esta conta não está paga.']);
  86. }
  87. return DB::transaction(function () use ($receivable) {
  88. $receivable->update([
  89. 'status' => 'pending',
  90. 'paid_value' => 0,
  91. 'payment_date' => null,
  92. ]);
  93. UnitAccountPayable::where('franchisee_account_receive_id', $receivable->id)
  94. ->update(['status' => 'pending', 'paid_value' => 0, 'payment_date' => null]);
  95. return $receivable->fresh(['unit', 'details', 'planAccount']);
  96. });
  97. }
  98. }