|
@@ -2,7 +2,10 @@
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
+use App\Exceptions\AsaasException;
|
|
|
|
|
+use App\Jobs\SyncStudentChargeJob;
|
|
|
use App\Models\StudentContractInstallment;
|
|
use App\Models\StudentContractInstallment;
|
|
|
|
|
+use App\Models\UnitPaymentAccount;
|
|
|
use Carbon\Carbon;
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
use Illuminate\Validation\ValidationException;
|
|
use Illuminate\Validation\ValidationException;
|
|
@@ -80,6 +83,42 @@ public function settle(StudentContractInstallment $installment, array $data): St
|
|
|
return $installment->fresh(['student']);
|
|
return $installment->fresh(['student']);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gera a cobrança da parcela na conta Asaas da unidade (sob demanda).
|
|
|
|
|
+ */
|
|
|
|
|
+ public function generateCharge(StudentContractInstallment $installment): StudentContractInstallment
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($installment->status === 'cancelled') {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'status' => 'Parcela cancelada não pode ser cobrada.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($installment->asaas_id) {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'asaas' => 'Esta parcela já possui cobrança gerada.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $account = UnitPaymentAccount::where('unit_id', $installment->unit_id)->first();
|
|
|
|
|
+
|
|
|
|
|
+ if (!$account || empty($account->asaas_api_key)) {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'asaas' => 'A unidade não possui chave Asaas configurada.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ SyncStudentChargeJob::dispatchSync($installment);
|
|
|
|
|
+ } catch (AsaasException $e) {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'asaas' => 'Não foi possível gerar a cobrança na Asaas. Tente novamente.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $installment->fresh(['student']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Reabre uma parcela paga (estorno da baixa manual).
|
|
* Reabre uma parcela paga (estorno da baixa manual).
|
|
|
*/
|
|
*/
|