| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\SoftDeletes;
- /**
- * @property int $id
- * @property string|null $protocol
- * @property int $student_id
- * @property int $unit_id
- * @property int|null $class_package_unit_id
- * @property string|null $pricing_mode
- * @property string|null $materials_value
- * @property int|null $materials_installments
- * @property \Illuminate\Support\Carbon|null $materials_due_date
- * @property string|null $total_value
- * @property int|null $total_installments
- * @property \Illuminate\Support\Carbon|null $total_due_date
- * @property string|null $down_payment_value
- * @property \Illuminate\Support\Carbon|null $down_payment_date
- * @property \Illuminate\Support\Carbon|null $end_date
- * @property string|null $start_time
- * @property string|null $end_time
- * @property string|null $tax_register
- * @property string|null $fine_cancelled
- * @property string|null $notes
- * @property string $status
- * @property \Illuminate\Support\Carbon|null $created_at
- * @property \Illuminate\Support\Carbon|null $updated_at
- * @property \Illuminate\Support\Carbon|null $deleted_at
- * @property \Illuminate\Support\Carbon|null $signature_date
- * @property int|null $class_quantity
- * @property int|null $weekday
- * @property int|null $second_weekday
- * @property string|null $second_start_time
- * @property string|null $second_end_time
- * @property int|null $recurring_day
- * @property \Illuminate\Database\Eloquent\Collection<int, \App\Models\StudentContractInstallment> $installments
- * @property string|null $payment_method
- * @property string|null $file_url
- * @property string|null $file_type
- * @property \Illuminate\Support\Carbon|null $enrollment_due_date
- * @property string|null $package_value
- * @property int|null $package_installments
- * @property \Illuminate\Support\Carbon|null $package_due_date
- * @property int $version
- * @property int|null $derived_from_contract_id
- * @property int|null $created_by_user_id
- * @property string|null $signed_file_url
- * @property string|null $signed_file_type
- * @property-read \App\Models\ClassPackageUnit|null $classPackageUnit
- * @property-read \App\Models\User|null $createdBy
- * @property-read StudentContract|null $derivedFrom
- * @property-read \Illuminate\Database\Eloquent\Collection<int, StudentContract> $derivedVersions
- * @property-read int|null $derived_versions_count
- * @property-read int|null $installments_count
- * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\StudentMedia> $medias
- * @property-read int|null $medias_count
- * @property-read \App\Models\Student $student
- * @property-read \App\Models\Unit $unit
- * @method static Builder<static>|StudentContract latestVersion()
- * @method static Builder<static>|StudentContract newModelQuery()
- * @method static Builder<static>|StudentContract newQuery()
- * @method static Builder<static>|StudentContract onlyTrashed()
- * @method static Builder<static>|StudentContract query()
- * @method static Builder<static>|StudentContract whereClassPackageUnitId($value)
- * @method static Builder<static>|StudentContract whereClassQuantity($value)
- * @method static Builder<static>|StudentContract whereCreatedAt($value)
- * @method static Builder<static>|StudentContract whereCreatedByUserId($value)
- * @method static Builder<static>|StudentContract whereDeletedAt($value)
- * @method static Builder<static>|StudentContract whereDerivedFromContractId($value)
- * @method static Builder<static>|StudentContract whereEarlyPaymentDiscount($value)
- * @method static Builder<static>|StudentContract whereEndDate($value)
- * @method static Builder<static>|StudentContract whereEndTime($value)
- * @method static Builder<static>|StudentContract whereEnrollmentDueDate($value)
- * @method static Builder<static>|StudentContract whereFileType($value)
- * @method static Builder<static>|StudentContract whereFileUrl($value)
- * @method static Builder<static>|StudentContract whereFineCancelled($value)
- * @method static Builder<static>|StudentContract whereId($value)
- * @method static Builder<static>|StudentContract whereInstallments($value)
- * @method static Builder<static>|StudentContract whereInterestRate($value)
- * @method static Builder<static>|StudentContract whereNotes($value)
- * @method static Builder<static>|StudentContract wherePackageDueDate($value)
- * @method static Builder<static>|StudentContract wherePackageInstallments($value)
- * @method static Builder<static>|StudentContract wherePackageValue($value)
- * @method static Builder<static>|StudentContract wherePaymentMethod($value)
- * @method static Builder<static>|StudentContract whereProtocol($value)
- * @method static Builder<static>|StudentContract whereRecurringDay($value)
- * @method static Builder<static>|StudentContract whereSecondEndTime($value)
- * @method static Builder<static>|StudentContract whereSecondStartTime($value)
- * @method static Builder<static>|StudentContract whereSecondWeekday($value)
- * @method static Builder<static>|StudentContract whereSignatureDate($value)
- * @method static Builder<static>|StudentContract whereSignedFileType($value)
- * @method static Builder<static>|StudentContract whereSignedFileUrl($value)
- * @method static Builder<static>|StudentContract whereStartTime($value)
- * @method static Builder<static>|StudentContract whereStatus($value)
- * @method static Builder<static>|StudentContract whereStudentId($value)
- * @method static Builder<static>|StudentContract whereTaxRegister($value)
- * @method static Builder<static>|StudentContract whereUnitId($value)
- * @method static Builder<static>|StudentContract whereUpdatedAt($value)
- * @method static Builder<static>|StudentContract whereVersion($value)
- * @method static Builder<static>|StudentContract whereWeekday($value)
- * @method static Builder<static>|StudentContract withTrashed(bool $withTrashed = true)
- * @method static Builder<static>|StudentContract withoutTrashed()
- * @mixin \Eloquent
- */
- class StudentContract extends Model
- {
- use HasFactory, SoftDeletes;
- protected $table = 'student_contracts';
- protected $guarded = ['id'];
- protected $casts = [
- 'version' => 'integer',
- 'signature_date' => 'date:Y-m-d',
- 'end_date' => 'date:Y-m-d',
- 'enrollment_due_date' => 'date:Y-m-d',
- 'package_due_date' => 'date:Y-m-d',
- 'materials_due_date' => 'date:Y-m-d',
- 'total_due_date' => 'date:Y-m-d',
- 'down_payment_date' => 'date:Y-m-d',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- public function scopeLatestVersion(Builder $query): Builder
- {
- return $query->whereDoesntHave(
- 'derivedVersions',
- fn (Builder $derivedQuery) => $derivedQuery->withTrashed(),
- );
- }
- protected static function booted(): void
- {
- static::saving(function (StudentContract $contract) {
- $contract->class_quantity = $contract->calculateClassQuantity();
- });
- $updateStudentStatus = function (StudentContract $contract): void {
- if ($contract->student_id) {
- // Use withoutGlobalScopes or just normal relation if no scopes affect it
- $contract->student()->first()?->updateStatus();
- }
- };
- static::saved($updateStudentStatus);
- static::deleted($updateStudentStatus);
- static::restored($updateStudentStatus);
- }
- public function calculateClassQuantity(): int
- {
- if (!$this->signature_date || !$this->end_date) {
- return (int) $this->class_quantity; // Fallback to current if missing
- }
- $start = $this->signature_date->copy()->startOfDay();
- $end = $this->end_date->copy()->startOfDay();
- if ($start->greaterThan($end)) {
- return 0;
- }
- $count = 0;
- for ($date = $start; $date->lessThanOrEqualTo($end); $date->addDay()) {
- $dayOfWeek = $date->dayOfWeek; // 0 (Sunday) to 6 (Saturday)
- if ($this->weekday !== null && $dayOfWeek === (int) $this->weekday) {
- $count++;
- }
- if ($this->second_weekday !== null && $dayOfWeek === (int) $this->second_weekday) {
- $count++;
- }
- }
- return $count;
- }
- public function derivedFrom(): BelongsTo
- {
- return $this->belongsTo(self::class, 'derived_from_contract_id');
- }
- public function derivedVersions(): HasMany
- {
- return $this->hasMany(self::class, 'derived_from_contract_id');
- }
- public function createdBy(): BelongsTo
- {
- return $this->belongsTo(User::class, 'created_by_user_id');
- }
- public function student(): BelongsTo
- {
- return $this->belongsTo(Student::class);
- }
- public function unit(): BelongsTo
- {
- return $this->belongsTo(Unit::class);
- }
- public function classPackageUnit(): BelongsTo
- {
- return $this->belongsTo(ClassPackageUnit::class);
- }
- public function medias(): HasMany
- {
- return $this->hasMany(StudentMedia::class);
- }
- public function installments(): HasMany
- {
- return $this->hasMany(StudentContractInstallment::class);
- }
- }
|