Member.php 7.6 KB

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