Member.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use DateTime;
  6. class Member extends Model
  7. {
  8. use HasFactory;
  9. protected $fillable = [
  10. 'first_name',
  11. 'last_name',
  12. 'status',
  13. 'birth_city_id',
  14. 'birth_province_id',
  15. 'birth_nation_id',
  16. 'birth_date',
  17. 'gender',
  18. 'address',
  19. 'zip_code',
  20. 'fiscal_code',
  21. 'nation_id',
  22. 'province_id',
  23. 'city_id',
  24. 'phone',
  25. 'phone2',
  26. 'phone3',
  27. 'email',
  28. 'image',
  29. 'document_type',
  30. 'document_number',
  31. 'document_from',
  32. 'document_expire_date',
  33. 'document_files',
  34. 'father_name',
  35. 'father_mail',
  36. 'father_phone',
  37. 'father_fiscal_code',
  38. 'mother_name',
  39. 'mother_mail',
  40. 'mother_phone',
  41. 'mother_fiscal_code',
  42. 'father_doc_number',
  43. 'father_doc_type',
  44. 'mother_doc_number',
  45. 'mother_doc_type',
  46. 'enabled',
  47. ];
  48. public function nation()
  49. {
  50. return $this->belongsTo(Nation::class);
  51. }
  52. public function province()
  53. {
  54. return $this->belongsTo(Province::class);
  55. }
  56. public function city()
  57. {
  58. return $this->belongsTo(City::class);
  59. }
  60. public function birth_nation()
  61. {
  62. return $this->belongsTo(Nation::class);
  63. }
  64. public function birth_province()
  65. {
  66. return $this->belongsTo(Province::class);
  67. }
  68. public function birth_city()
  69. {
  70. return $this->belongsTo(City::class);
  71. }
  72. public function cards()
  73. {
  74. return $this->hasMany(MemberCard::class);
  75. }
  76. public function categories()
  77. {
  78. return $this->hasMany(MemberCategory::class);
  79. }
  80. public function courses()
  81. {
  82. return $this->hasMany(MemberCourse::class);
  83. }
  84. public function certificates()
  85. {
  86. return $this->hasMany(MemberCertificate::class)->orderBy('expire_date', 'DESC');
  87. }
  88. public function isAdult()
  89. {
  90. $date1 = new DateTime($this->birth_date);
  91. $date2 = new DateTime("now");
  92. $interval = $date1->diff($date2);
  93. return $this->birth_date == '' || $interval->y >= 18;
  94. }
  95. public function getAge()
  96. {
  97. $date1 = new DateTime($this->birth_date);
  98. $date2 = new DateTime("now");
  99. $interval = $date1->diff($date2);
  100. return $interval->y;
  101. }
  102. public function isActive()
  103. {
  104. // Se uno ha una tessera o meno compare nella lista degli utenti (attivo/disattivo),
  105. // per il certificato medico compare la data di scadenza sempre nella lista utenti.
  106. // Se uno invece ha eseguito i pagamenti, al posto del bottone abbiamo detto che mettiamo la scritta (iscritto (ha pagato),
  107. //in sospeso (stato legato alla validità o meno della tessera o del certificato medico, non iscritto (non ha pagato). Io questo ho capito.
  108. $subscribed = $this->isSubscribed();
  109. $hasCard = $subscribed["status"] == 2;
  110. $hasCertificate = $this->hasCertificate()["status"];
  111. $ret = array('status' => 0, 'status_text' => '', 'date' => '');
  112. if ($hasCard)
  113. {
  114. if ($hasCertificate)
  115. $ret = array('status' => 2, 'status_text' => 'Attiva', 'date' => $subscribed["date"]);
  116. else
  117. $ret = array('status' => 1,'status_text' => 'Sospeso', 'date' => $subscribed["date"]);
  118. }
  119. /*$ret = array('status' => 0, 'status_text' => '', 'date' => '');
  120. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('causal')->get();
  121. foreach($records as $record)
  122. {
  123. if ($record->causal->user_status == 1)
  124. {
  125. if ($record->date < date('Y-m-d', strtotime('+1 year')))
  126. $ret = array('status' => 2, 'status_text' => 'Attiva', 'date' => $newDate = date('Y-m-d', strtotime($record->date. ' + 1 years')));
  127. // if (!$hasCard || !$hasCertificate)
  128. if (!$hasCertificate)
  129. $ret = array('status' => 1,'status_text' => 'Sospeso', 'date' => $newDate = date('Y-m-d', strtotime($record->date. ' + 1 years')));
  130. }
  131. }*/
  132. return $ret;
  133. }
  134. public function isSubscribed()
  135. {
  136. $ret = array('status' => 0, 'date' => '');
  137. $cards = \App\Models\MemberCard::where('member_id', $this->id)->with('card')->orderBy('expire_date')->get();
  138. foreach($cards as $card)
  139. {
  140. if ($card->card->use_for_user_check)
  141. {
  142. $ret = array('status' => $card->expire_date > date("Y-m-d") ? 2 : 1, 'date' => $card->expire_date);
  143. }
  144. }
  145. return $ret;
  146. }
  147. public function getStatus()
  148. {
  149. $ret = array('status' => 0, 'date' => '', 'status_text' => 'Non tesserato');
  150. $cards = \App\Models\MemberCard::where('member_id', $this->id)->with('card')->orderBy('expire_date')->get();
  151. foreach($cards as $card)
  152. {
  153. if ($card->card->use_for_user_check)
  154. {
  155. $ret = array('status' => $card->expire_date > date("Y-m-d") ? 2 : 1, 'date' => $card->expire_date, 'status_text' => $card->expire_date > date("Y-m-d") ? 'Tesserato' : 'Sospeso');
  156. }
  157. }
  158. return $ret;
  159. }
  160. public function hasCertificate()
  161. {
  162. $ret = array('status' => false, 'date' => '');
  163. $certificates = \App\Models\MemberCertificate::where('member_id', $this->id)->orderBy('expire_date')->get();
  164. foreach($certificates as $certificate)
  165. {
  166. $ret = array('status' => $certificate->expire_date > date("Y-m-d"), 'date' => $certificate->expire_date);
  167. }
  168. return $ret;
  169. }
  170. public function getMoney()
  171. {
  172. $ret = 0;
  173. // Soldi virtuali caricati
  174. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->get();
  175. //->with('causal')->get();
  176. foreach($records as $record)
  177. {
  178. foreach($record->rows as $r)
  179. {
  180. if ($r->causal->money == 1)
  181. {
  182. $ret += $r->amount;
  183. }
  184. }
  185. }
  186. // Soldi virtuali spesi
  187. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('payment_method')->get();
  188. foreach($records as $record)
  189. {
  190. if ($record->payment_method->money == 1)
  191. {
  192. $ret -= $record->amount;
  193. }
  194. }
  195. return $ret;
  196. }
  197. }