StudentContract.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. /**
  10. * @property int $id
  11. * @property string|null $protocol
  12. * @property int $student_id
  13. * @property int $unit_id
  14. * @property int|null $class_package_unit_id
  15. * @property string|null $pricing_mode
  16. * @property string|null $materials_value
  17. * @property int|null $materials_installments
  18. * @property \Illuminate\Support\Carbon|null $materials_due_date
  19. * @property string|null $total_value
  20. * @property int|null $total_installments
  21. * @property \Illuminate\Support\Carbon|null $total_due_date
  22. * @property string|null $down_payment_value
  23. * @property \Illuminate\Support\Carbon|null $down_payment_date
  24. * @property \Illuminate\Support\Carbon|null $end_date
  25. * @property string|null $start_time
  26. * @property string|null $end_time
  27. * @property string|null $tax_register
  28. * @property string|null $fine_cancelled
  29. * @property string|null $notes
  30. * @property string $status
  31. * @property \Illuminate\Support\Carbon|null $created_at
  32. * @property \Illuminate\Support\Carbon|null $updated_at
  33. * @property \Illuminate\Support\Carbon|null $deleted_at
  34. * @property \Illuminate\Support\Carbon|null $signature_date
  35. * @property int|null $class_quantity
  36. * @property int|null $weekday
  37. * @property int|null $second_weekday
  38. * @property string|null $second_start_time
  39. * @property string|null $second_end_time
  40. * @property int|null $recurring_day
  41. * @property \Illuminate\Database\Eloquent\Collection<int, \App\Models\StudentContractInstallment> $installments
  42. * @property string|null $payment_method
  43. * @property string|null $file_url
  44. * @property string|null $file_type
  45. * @property \Illuminate\Support\Carbon|null $enrollment_due_date
  46. * @property string|null $package_value
  47. * @property int|null $package_installments
  48. * @property \Illuminate\Support\Carbon|null $package_due_date
  49. * @property int $version
  50. * @property int|null $derived_from_contract_id
  51. * @property int|null $created_by_user_id
  52. * @property string|null $signed_file_url
  53. * @property string|null $signed_file_type
  54. * @property-read \App\Models\ClassPackageUnit|null $classPackageUnit
  55. * @property-read \App\Models\User|null $createdBy
  56. * @property-read StudentContract|null $derivedFrom
  57. * @property-read \Illuminate\Database\Eloquent\Collection<int, StudentContract> $derivedVersions
  58. * @property-read int|null $derived_versions_count
  59. * @property-read int|null $installments_count
  60. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\StudentMedia> $medias
  61. * @property-read int|null $medias_count
  62. * @property-read \App\Models\Student $student
  63. * @property-read \App\Models\Unit $unit
  64. * @method static Builder<static>|StudentContract latestVersion()
  65. * @method static Builder<static>|StudentContract newModelQuery()
  66. * @method static Builder<static>|StudentContract newQuery()
  67. * @method static Builder<static>|StudentContract onlyTrashed()
  68. * @method static Builder<static>|StudentContract query()
  69. * @method static Builder<static>|StudentContract whereClassPackageUnitId($value)
  70. * @method static Builder<static>|StudentContract whereClassQuantity($value)
  71. * @method static Builder<static>|StudentContract whereCreatedAt($value)
  72. * @method static Builder<static>|StudentContract whereCreatedByUserId($value)
  73. * @method static Builder<static>|StudentContract whereDeletedAt($value)
  74. * @method static Builder<static>|StudentContract whereDerivedFromContractId($value)
  75. * @method static Builder<static>|StudentContract whereEarlyPaymentDiscount($value)
  76. * @method static Builder<static>|StudentContract whereEndDate($value)
  77. * @method static Builder<static>|StudentContract whereEndTime($value)
  78. * @method static Builder<static>|StudentContract whereEnrollmentDueDate($value)
  79. * @method static Builder<static>|StudentContract whereFileType($value)
  80. * @method static Builder<static>|StudentContract whereFileUrl($value)
  81. * @method static Builder<static>|StudentContract whereFineCancelled($value)
  82. * @method static Builder<static>|StudentContract whereId($value)
  83. * @method static Builder<static>|StudentContract whereInstallments($value)
  84. * @method static Builder<static>|StudentContract whereInterestRate($value)
  85. * @method static Builder<static>|StudentContract whereNotes($value)
  86. * @method static Builder<static>|StudentContract wherePackageDueDate($value)
  87. * @method static Builder<static>|StudentContract wherePackageInstallments($value)
  88. * @method static Builder<static>|StudentContract wherePackageValue($value)
  89. * @method static Builder<static>|StudentContract wherePaymentMethod($value)
  90. * @method static Builder<static>|StudentContract whereProtocol($value)
  91. * @method static Builder<static>|StudentContract whereRecurringDay($value)
  92. * @method static Builder<static>|StudentContract whereSecondEndTime($value)
  93. * @method static Builder<static>|StudentContract whereSecondStartTime($value)
  94. * @method static Builder<static>|StudentContract whereSecondWeekday($value)
  95. * @method static Builder<static>|StudentContract whereSignatureDate($value)
  96. * @method static Builder<static>|StudentContract whereSignedFileType($value)
  97. * @method static Builder<static>|StudentContract whereSignedFileUrl($value)
  98. * @method static Builder<static>|StudentContract whereStartTime($value)
  99. * @method static Builder<static>|StudentContract whereStatus($value)
  100. * @method static Builder<static>|StudentContract whereStudentId($value)
  101. * @method static Builder<static>|StudentContract whereTaxRegister($value)
  102. * @method static Builder<static>|StudentContract whereUnitId($value)
  103. * @method static Builder<static>|StudentContract whereUpdatedAt($value)
  104. * @method static Builder<static>|StudentContract whereVersion($value)
  105. * @method static Builder<static>|StudentContract whereWeekday($value)
  106. * @method static Builder<static>|StudentContract withTrashed(bool $withTrashed = true)
  107. * @method static Builder<static>|StudentContract withoutTrashed()
  108. * @mixin \Eloquent
  109. */
  110. class StudentContract extends Model
  111. {
  112. use HasFactory, SoftDeletes;
  113. protected $table = 'student_contracts';
  114. protected $guarded = ['id'];
  115. protected $casts = [
  116. 'version' => 'integer',
  117. 'signature_date' => 'date:Y-m-d',
  118. 'end_date' => 'date:Y-m-d',
  119. 'enrollment_due_date' => 'date:Y-m-d',
  120. 'package_due_date' => 'date:Y-m-d',
  121. 'materials_due_date' => 'date:Y-m-d',
  122. 'total_due_date' => 'date:Y-m-d',
  123. 'down_payment_date' => 'date:Y-m-d',
  124. 'created_at' => 'datetime',
  125. 'updated_at' => 'datetime',
  126. ];
  127. public function scopeLatestVersion(Builder $query): Builder
  128. {
  129. return $query->whereDoesntHave(
  130. 'derivedVersions',
  131. fn (Builder $derivedQuery) => $derivedQuery->withTrashed(),
  132. );
  133. }
  134. protected static function booted(): void
  135. {
  136. static::saving(function (StudentContract $contract) {
  137. $contract->class_quantity = $contract->calculateClassQuantity();
  138. });
  139. $updateStudentStatus = function (StudentContract $contract): void {
  140. if ($contract->student_id) {
  141. // Use withoutGlobalScopes or just normal relation if no scopes affect it
  142. $contract->student()->first()?->updateStatus();
  143. }
  144. };
  145. static::saved($updateStudentStatus);
  146. static::deleted($updateStudentStatus);
  147. static::restored($updateStudentStatus);
  148. }
  149. public function calculateClassQuantity(): int
  150. {
  151. if (!$this->signature_date || !$this->end_date) {
  152. return (int) $this->class_quantity; // Fallback to current if missing
  153. }
  154. $start = $this->signature_date->copy()->startOfDay();
  155. $end = $this->end_date->copy()->startOfDay();
  156. if ($start->greaterThan($end)) {
  157. return 0;
  158. }
  159. $count = 0;
  160. for ($date = $start; $date->lessThanOrEqualTo($end); $date->addDay()) {
  161. $dayOfWeek = $date->dayOfWeek; // 0 (Sunday) to 6 (Saturday)
  162. if ($this->weekday !== null && $dayOfWeek === (int) $this->weekday) {
  163. $count++;
  164. }
  165. if ($this->second_weekday !== null && $dayOfWeek === (int) $this->second_weekday) {
  166. $count++;
  167. }
  168. }
  169. return $count;
  170. }
  171. public function derivedFrom(): BelongsTo
  172. {
  173. return $this->belongsTo(self::class, 'derived_from_contract_id');
  174. }
  175. public function derivedVersions(): HasMany
  176. {
  177. return $this->hasMany(self::class, 'derived_from_contract_id');
  178. }
  179. public function createdBy(): BelongsTo
  180. {
  181. return $this->belongsTo(User::class, 'created_by_user_id');
  182. }
  183. public function student(): BelongsTo
  184. {
  185. return $this->belongsTo(Student::class);
  186. }
  187. public function unit(): BelongsTo
  188. {
  189. return $this->belongsTo(Unit::class);
  190. }
  191. public function classPackageUnit(): BelongsTo
  192. {
  193. return $this->belongsTo(ClassPackageUnit::class);
  194. }
  195. public function medias(): HasMany
  196. {
  197. return $this->hasMany(StudentMedia::class);
  198. }
  199. public function installments(): HasMany
  200. {
  201. return $this->hasMany(StudentContractInstallment::class);
  202. }
  203. }