StudentContract.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Models;
  3. use App\Rules\StudentContractVariationConsistency;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. use Illuminate\Database\Eloquent\Relations\HasMany;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. use Illuminate\Validation\ValidationException;
  11. /**
  12. * @property int $id
  13. * @property string|null $protocol
  14. * @property int $student_id
  15. * @property int $unit_id
  16. * @property int|null $class_package_unit_id
  17. * @property int|null $class_package_unit_variation_id
  18. * @property \Illuminate\Support\Carbon|null $end_date
  19. * @property string|null $start_time
  20. * @property string|null $end_time
  21. * @property string|null $tax_register
  22. * @property string|null $fine_cancelled
  23. * @property string|null $notes
  24. * @property string $status
  25. * @property \Illuminate\Support\Carbon|null $created_at
  26. * @property \Illuminate\Support\Carbon|null $updated_at
  27. * @property \Illuminate\Support\Carbon|null $deleted_at
  28. * @property \Illuminate\Support\Carbon|null $signature_date
  29. * @property int|null $class_quantity
  30. * @property int|null $weekday
  31. * @property int|null $second_weekday
  32. * @property string|null $second_start_time
  33. * @property string|null $second_end_time
  34. * @property int|null $recurring_day
  35. * @property \Illuminate\Database\Eloquent\Collection<int, \App\Models\StudentContractInstallment> $installments
  36. * @property string|null $early_payment_discount
  37. * @property string|null $interest_rate
  38. * @property string|null $payment_method
  39. * @property string|null $file_url
  40. * @property string|null $file_type
  41. * @property \Illuminate\Support\Carbon|null $enrollment_due_date
  42. * @property string|null $package_value
  43. * @property int|null $package_installments
  44. * @property \Illuminate\Support\Carbon|null $package_due_date
  45. * @property int $version
  46. * @property int|null $derived_from_contract_id
  47. * @property int|null $created_by_user_id
  48. * @property string|null $signed_file_url
  49. * @property string|null $signed_file_type
  50. * @property string|null $material_value
  51. * @property int|null $material_installments
  52. * @property \Illuminate\Support\Carbon|null $material_due_date
  53. * @property-read \App\Models\ClassPackageUnit|null $classPackageUnit
  54. * @property-read \App\Models\ClassPackageUnitVariation|null $classPackageUnitVariation
  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 whereMaterialDueDate($value)
  86. * @method static Builder<static>|StudentContract whereMaterialInstallments($value)
  87. * @method static Builder<static>|StudentContract whereMaterialValue($value)
  88. * @method static Builder<static>|StudentContract whereNotes($value)
  89. * @method static Builder<static>|StudentContract wherePackageDueDate($value)
  90. * @method static Builder<static>|StudentContract wherePackageInstallments($value)
  91. * @method static Builder<static>|StudentContract wherePackageValue($value)
  92. * @method static Builder<static>|StudentContract wherePaymentMethod($value)
  93. * @method static Builder<static>|StudentContract whereProtocol($value)
  94. * @method static Builder<static>|StudentContract whereRecurringDay($value)
  95. * @method static Builder<static>|StudentContract whereSecondEndTime($value)
  96. * @method static Builder<static>|StudentContract whereSecondStartTime($value)
  97. * @method static Builder<static>|StudentContract whereSecondWeekday($value)
  98. * @method static Builder<static>|StudentContract whereSignatureDate($value)
  99. * @method static Builder<static>|StudentContract whereSignedFileType($value)
  100. * @method static Builder<static>|StudentContract whereSignedFileUrl($value)
  101. * @method static Builder<static>|StudentContract whereStartTime($value)
  102. * @method static Builder<static>|StudentContract whereStatus($value)
  103. * @method static Builder<static>|StudentContract whereStudentId($value)
  104. * @method static Builder<static>|StudentContract whereTaxRegister($value)
  105. * @method static Builder<static>|StudentContract whereUnitId($value)
  106. * @method static Builder<static>|StudentContract whereUpdatedAt($value)
  107. * @method static Builder<static>|StudentContract whereVariation($value)
  108. * @method static Builder<static>|StudentContract whereVersion($value)
  109. * @method static Builder<static>|StudentContract whereWeekday($value)
  110. * @method static Builder<static>|StudentContract withTrashed(bool $withTrashed = true)
  111. * @method static Builder<static>|StudentContract withoutTrashed()
  112. * @mixin \Eloquent
  113. */
  114. class StudentContract extends Model
  115. {
  116. use HasFactory, SoftDeletes;
  117. protected $table = 'student_contracts';
  118. protected $guarded = ['id'];
  119. protected $casts = [
  120. 'version' => 'integer',
  121. 'signature_date' => 'date:Y-m-d',
  122. 'end_date' => 'date:Y-m-d',
  123. 'enrollment_due_date' => 'date:Y-m-d',
  124. 'package_due_date' => 'date:Y-m-d',
  125. 'material_due_date' => 'date:Y-m-d',
  126. 'created_at' => 'datetime',
  127. 'updated_at' => 'datetime',
  128. ];
  129. protected static function booted(): void
  130. {
  131. // rede de seguranca: nenhum fluxo grava contrato incongruente com a
  132. // proposta, mesmo quando nao passa pelo StudentContractRequest
  133. static::saving(function (StudentContract $contract): void {
  134. $errors = (new StudentContractVariationConsistency)->errors($contract->getAttributes());
  135. if ($errors !== []) {
  136. throw ValidationException::withMessages($errors);
  137. }
  138. });
  139. }
  140. public function scopeLatestVersion(Builder $query): Builder
  141. {
  142. return $query->whereDoesntHave(
  143. 'derivedVersions',
  144. fn (Builder $derivedQuery) => $derivedQuery->withTrashed(),
  145. );
  146. }
  147. public function derivedFrom(): BelongsTo
  148. {
  149. return $this->belongsTo(self::class, 'derived_from_contract_id');
  150. }
  151. public function derivedVersions(): HasMany
  152. {
  153. return $this->hasMany(self::class, 'derived_from_contract_id');
  154. }
  155. public function createdBy(): BelongsTo
  156. {
  157. return $this->belongsTo(User::class, 'created_by_user_id');
  158. }
  159. public function student(): BelongsTo
  160. {
  161. return $this->belongsTo(Student::class);
  162. }
  163. public function unit(): BelongsTo
  164. {
  165. return $this->belongsTo(Unit::class);
  166. }
  167. public function classPackageUnit(): BelongsTo
  168. {
  169. return $this->belongsTo(ClassPackageUnit::class);
  170. }
  171. public function classPackageUnitVariation(): BelongsTo
  172. {
  173. return $this->belongsTo(ClassPackageUnitVariation::class);
  174. }
  175. public function medias(): HasMany
  176. {
  177. return $this->hasMany(StudentMedia::class);
  178. }
  179. public function installments(): HasMany
  180. {
  181. return $this->hasMany(StudentContractInstallment::class);
  182. }
  183. }