Member.php 7.8 KB

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