|
@@ -3,9 +3,12 @@
|
|
|
namespace App\Services;
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Enums\NotificationType;
|
|
use App\Enums\NotificationType;
|
|
|
|
|
+use App\Jobs\SyncFranchiseeChargeJob;
|
|
|
|
|
+use App\Models\CompanyPaymentAccount;
|
|
|
use App\Models\FranchiseeAccountReceive;
|
|
use App\Models\FranchiseeAccountReceive;
|
|
|
use App\Models\UnitAccountPayable;
|
|
use App\Models\UnitAccountPayable;
|
|
|
use Carbon\Carbon;
|
|
use Carbon\Carbon;
|
|
|
|
|
+use Exception;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Validation\ValidationException;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
|
|
@@ -107,23 +110,47 @@ public function settle(FranchiseeAccountReceive $receivable, array $data): Franc
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function reopen(FranchiseeAccountReceive $receivable): FranchiseeAccountReceive
|
|
|
|
|
|
|
+ public function generateCharge(FranchiseeAccountReceive $receivable): FranchiseeAccountReceive
|
|
|
{
|
|
{
|
|
|
- if ($receivable->status->value !== 'paid') {
|
|
|
|
|
- throw ValidationException::withMessages(['status' => 'Esta conta não está paga.']);
|
|
|
|
|
|
|
+ if (! $receivable->unit_id) {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'unit' => 'Cobranças Asaas só podem ser geradas para contas vinculadas a uma unidade.',
|
|
|
|
|
+ ]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return DB::transaction(function () use ($receivable) {
|
|
|
|
|
- $receivable->update([
|
|
|
|
|
- 'status' => 'pending',
|
|
|
|
|
- 'paid_value' => 0,
|
|
|
|
|
- 'payment_date' => null,
|
|
|
|
|
|
|
+ if (in_array($receivable->status->value, ['paid', 'cancelled'], true)) {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'status' => 'Contas pagas ou canceladas não podem gerar cobrança.',
|
|
|
]);
|
|
]);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- UnitAccountPayable::where('franchisee_account_receive_id', $receivable->id)
|
|
|
|
|
- ->update(['status' => 'pending', 'paid_value' => 0, 'payment_date' => null]);
|
|
|
|
|
|
|
+ if ($receivable->asaas_id) {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'asaas' => 'Esta conta já possui cobrança gerada.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return $receivable->fresh(['unit', 'details', 'planAccount']);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ if (empty(CompanyPaymentAccount::resolveApiKey())) {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'asaas' => 'A franqueadora não possui chave Asaas configurada.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ SyncFranchiseeChargeJob::dispatchSync($receivable);
|
|
|
|
|
+ } catch (Exception) {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'asaas' => 'Não foi possível gerar a cobrança na Asaas. Tente novamente.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $receivable->fresh(['unit', 'details', 'planAccount']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function reopen(FranchiseeAccountReceive $receivable): FranchiseeAccountReceive
|
|
|
|
|
+ {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'status' => 'Uma conta paga não pode voltar para um status anterior.',
|
|
|
|
|
+ ]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|