| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace App\Services;
- use App\Models\FranchiseeContract;
- use Illuminate\Database\Eloquent\Collection;
- class FranchiseeContractService
- {
- public function getAll(): Collection
- {
- return FranchiseeContract::orderBy('created_at', 'desc')
- ->get();
- }
- public function getActive(): Collection
- {
- $today = now()->toDateString();
- return FranchiseeContract::with('unit')
- ->whereNotNull('start_date')
- ->whereNotNull('end_date')
- ->where('start_date', '<=', $today)
- ->where('end_date', '>=', $today)
- ->orderBy('start_date')
- ->get();
- }
- public function findById(int $id): ?FranchiseeContract
- {
- return FranchiseeContract::find($id);
- }
- public function getByUnitId(int $unitId): Collection
- {
- return FranchiseeContract::where('unit_id', $unitId)
- ->orderBy('created_at', 'desc')
- ->get();
- }
- public function create(array $data): FranchiseeContract
- {
- $data['protocol'] = (FranchiseeContract::where('unit_id', $data['unit_id'])->max('protocol') ?? 0) + 1;
- $data['signature_date'] = $data['start_date'];
- if (isset($data['start_date'], $data['end_date'])) {
- $data['validity_months'] = (int) \Carbon\Carbon::parse($data['start_date'])
- ->diffInMonths(\Carbon\Carbon::parse($data['end_date']));
- }
- return FranchiseeContract::create($data);
- }
- public function update(int $id, array $data): ?FranchiseeContract
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $model->update($data);
- return $model->fresh();
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- // Add custom business logic methods here
- }
|