Member.php 7.3 KB

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