Member.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 'no_send_mail'
  48. ];
  49. public function nation()
  50. {
  51. return $this->belongsTo(Nation::class);
  52. }
  53. public function province()
  54. {
  55. return $this->belongsTo(Province::class);
  56. }
  57. public function city()
  58. {
  59. return $this->belongsTo(City::class);
  60. }
  61. public function birth_nation()
  62. {
  63. return $this->belongsTo(Nation::class);
  64. }
  65. public function birth_province()
  66. {
  67. return $this->belongsTo(Province::class);
  68. }
  69. public function birth_city()
  70. {
  71. return $this->belongsTo(City::class);
  72. }
  73. public function cards()
  74. {
  75. return $this->hasMany(MemberCard::class);
  76. }
  77. public function categories()
  78. {
  79. return $this->hasMany(MemberCategory::class);
  80. }
  81. public function courses()
  82. {
  83. return $this->hasMany(MemberCourse::class);
  84. }
  85. public function certificates()
  86. {
  87. return $this->hasMany(MemberCertificate::class)->orderBy('expire_date', 'DESC');
  88. }
  89. public function isAdult()
  90. {
  91. $date1 = new DateTime($this->birth_date);
  92. $date2 = new DateTime("now");
  93. $interval = $date1->diff($date2);
  94. return $this->birth_date == '' || $interval->y >= 18;
  95. }
  96. public function getAge()
  97. {
  98. $date1 = new DateTime($this->birth_date);
  99. $date2 = new DateTime("now");
  100. $interval = $date1->diff($date2);
  101. return strval($interval->y);
  102. return $interval->y;
  103. }
  104. public function isActive()
  105. {
  106. // Se uno ha una tessera o meno compare nella lista degli utenti (attivo/disattivo),
  107. // per il certificato medico compare la data di scadenza sempre nella lista utenti.
  108. // Se uno invece ha eseguito i pagamenti, al posto del bottone abbiamo detto che mettiamo la scritta (iscritto (ha pagato),
  109. //in sospeso (stato legato alla validità o meno della tessera o del certificato medico, non iscritto (non ha pagato). Io questo ho capito.
  110. $subscribed = $this->isSubscribed();
  111. $hasCard = $subscribed["status"] == 2;
  112. $hasCertificate = $this->hasCertificate()["status"];
  113. $ret = array('status' => 0, 'status_text' => '', 'date' => '');
  114. if ($hasCard)
  115. {
  116. if ($hasCertificate)
  117. $ret = array('status' => 2, 'status_text' => 'Attiva', 'date' => $subscribed["date"]);
  118. else
  119. $ret = array('status' => 1,'status_text' => 'Sospeso', 'date' => $subscribed["date"]);
  120. }
  121. /*$ret = array('status' => 0, 'status_text' => '', 'date' => '');
  122. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('causal')->get();
  123. foreach($records as $record)
  124. {
  125. if ($record->causal->user_status == 1)
  126. {
  127. if ($record->date < date('Y-m-d', strtotime('+1 year')))
  128. $ret = array('status' => 2, 'status_text' => 'Attiva', 'date' => $newDate = date('Y-m-d', strtotime($record->date. ' + 1 years')));
  129. // if (!$hasCard || !$hasCertificate)
  130. if (!$hasCertificate)
  131. $ret = array('status' => 1,'status_text' => 'Sospeso', 'date' => $newDate = date('Y-m-d', strtotime($record->date. ' + 1 years')));
  132. }
  133. }*/
  134. return $ret;
  135. }
  136. public function isSubscribed()
  137. {
  138. $ret = array('status' => 0, 'date' => '');
  139. $cards = \App\Models\MemberCard::where('member_id', $this->id)->with('card')->orderBy('expire_date')->get();
  140. foreach($cards as $card)
  141. {
  142. if ($card->card->use_for_user_check)
  143. {
  144. $ret = array('status' => $card->expire_date . " 23:59:59" > date("Y-m-d") ? 2 : 1, 'date' => $card->expire_date);
  145. }
  146. }
  147. return $ret;
  148. }
  149. public function getStatus()
  150. {
  151. $ret = array('status' => 0, 'date' => '', 'status_text' => 'Non tesserato');
  152. $cards = \App\Models\MemberCard::where('member_id', $this->id)->with('card')->orderBy('expire_date')->get();
  153. foreach($cards as $card)
  154. {
  155. if ($card->card->use_for_user_check)
  156. {
  157. $ret = array('status' => $card->expire_date . " 23:59:59" > date("Y-m-d") ? 2 : 1, 'date' => $card->expire_date, 'status_text' => $card->expire_date . " 23:59:59" > date("Y-m-d") ? 'Tesserato' : 'Sospeso');
  158. }
  159. }
  160. return $ret;
  161. }
  162. public function hasCertificate()
  163. {
  164. $ret = array('status' => false, 'date' => '');
  165. $certificates = \App\Models\MemberCertificate::where('member_id', $this->id)->orderBy('expire_date')->get();
  166. foreach($certificates as $certificate)
  167. {
  168. $ret = array('status' => $certificate->expire_date . " 23:59:59" > date("Y-m-d"), 'date' => $certificate->expire_date);
  169. }
  170. return $ret;
  171. }
  172. public function getMoney()
  173. {
  174. $ret = 0;
  175. // Soldi virtuali caricati
  176. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')
  177. ->where(function ($query) {
  178. $query->where('deleted', false)->orWhere('deleted', null);
  179. })->get();
  180. //->with('causal')->get();
  181. foreach($records as $record)
  182. {
  183. foreach($record->rows as $r)
  184. {
  185. if ($r->causal->money == 1)
  186. {
  187. $ret += $r->amount;
  188. }
  189. }
  190. }
  191. // Soldi virtuali spesi
  192. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('payment_method')
  193. ->where(function ($query) {
  194. $query->where('deleted', false)->orWhere('deleted', null);
  195. })->get();
  196. foreach($records as $record)
  197. {
  198. if ($record->payment_method->money == 1)
  199. {
  200. $ret -= $record->amount;
  201. }
  202. }
  203. return $ret;
  204. }
  205. }