|
@@ -5,6 +5,7 @@
|
|
|
use App\Exceptions\AsaasException;
|
|
use App\Exceptions\AsaasException;
|
|
|
use App\Jobs\SyncStudentChargeJob;
|
|
use App\Jobs\SyncStudentChargeJob;
|
|
|
use App\Models\StudentContractInstallment;
|
|
use App\Models\StudentContractInstallment;
|
|
|
|
|
+use App\Models\UnitAccountReceivable;
|
|
|
use App\Models\UnitPaymentAccount;
|
|
use App\Models\UnitPaymentAccount;
|
|
|
use Carbon\Carbon;
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
@@ -138,4 +139,97 @@ public function reopen(StudentContractInstallment $installment): StudentContract
|
|
|
|
|
|
|
|
return $installment->fresh(['student']);
|
|
return $installment->fresh(['student']);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // ------------------------------------------------------------------
|
|
|
|
|
+ // Recebíveis avulsos (manuais) — tabela unit_account_receivables
|
|
|
|
|
+ // ------------------------------------------------------------------
|
|
|
|
|
+ public function listManual(int $unitId, array $filters = []): Collection
|
|
|
|
|
+ {
|
|
|
|
|
+ return UnitAccountReceivable::with('student')
|
|
|
|
|
+ ->where('unit_id', $unitId)
|
|
|
|
|
+ ->when(
|
|
|
|
|
+ isset($filters['status']),
|
|
|
|
|
+ fn ($q) => $q->where('status', $filters['status']),
|
|
|
|
|
+ )
|
|
|
|
|
+ ->when(
|
|
|
|
|
+ isset($filters['student_id']),
|
|
|
|
|
+ fn ($q) => $q->where('student_id', $filters['student_id']),
|
|
|
|
|
+ )
|
|
|
|
|
+ ->orderBy('due_date')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function findManualForUnit(int $id, int $unitId): ?UnitAccountReceivable
|
|
|
|
|
+ {
|
|
|
|
|
+ return UnitAccountReceivable::where('id', $id)
|
|
|
|
|
+ ->where('unit_id', $unitId)
|
|
|
|
|
+ ->first();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function createManual(int $unitId, array $data): UnitAccountReceivable
|
|
|
|
|
+ {
|
|
|
|
|
+ return UnitAccountReceivable::create([
|
|
|
|
|
+ 'unit_id' => $unitId,
|
|
|
|
|
+ 'origin' => UnitAccountReceivable::ORIGIN_MANUAL,
|
|
|
|
|
+ 'student_id' => $data['student_id'] ?? null,
|
|
|
|
|
+ 'financial_plan_account_id' => $data['financial_plan_account_id'] ?? null,
|
|
|
|
|
+ 'history' => $data['history'],
|
|
|
|
|
+ 'value' => $data['value'],
|
|
|
|
|
+ 'due_date' => $data['due_date'],
|
|
|
|
|
+ 'obs' => $data['obs'] ?? null,
|
|
|
|
|
+ 'paid_value' => 0,
|
|
|
|
|
+ 'discount' => 0,
|
|
|
|
|
+ 'fine' => 0,
|
|
|
|
|
+ 'status' => 'pending',
|
|
|
|
|
+ ])->load('student');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function settleManual(UnitAccountReceivable $receivable, array $data): UnitAccountReceivable
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($receivable->status === 'paid') {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'status' => 'Este recebível já está pago.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($receivable->status === 'cancelled') {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'status' => 'Este recebível está cancelado e não pode receber baixa.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $discount = isset($data['discount']) ? (float) $data['discount'] : (float) $receivable->discount;
|
|
|
|
|
+ $fine = isset($data['fine']) ? (float) $data['fine'] : (float) $receivable->fine;
|
|
|
|
|
+
|
|
|
|
|
+ $paidValue = isset($data['paid_value'])
|
|
|
|
|
+ ? (float) $data['paid_value']
|
|
|
|
|
+ : round((float) $receivable->value - $discount + $fine, 2);
|
|
|
|
|
+
|
|
|
|
|
+ $receivable->update([
|
|
|
|
|
+ 'status' => 'paid',
|
|
|
|
|
+ 'discount' => $discount,
|
|
|
|
|
+ 'fine' => $fine,
|
|
|
|
|
+ 'paid_value' => $paidValue,
|
|
|
|
|
+ 'payment_date' => $data['payment_date'] ?? Carbon::today()->format('Y-m-d'),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ return $receivable->fresh('student');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function reopenManual(UnitAccountReceivable $receivable): UnitAccountReceivable
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($receivable->status !== 'paid') {
|
|
|
|
|
+ throw ValidationException::withMessages([
|
|
|
|
|
+ 'status' => 'Só é possível reabrir recebíveis que estão pagos.',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $receivable->update([
|
|
|
|
|
+ 'status' => 'pending',
|
|
|
|
|
+ 'paid_value' => 0,
|
|
|
|
|
+ 'payment_date' => null,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ return $receivable->fresh('student');
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|