| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?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 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
- }
|