Member.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Member extends Model
  6. {
  7. use HasFactory;
  8. protected $fillable = [
  9. 'first_name',
  10. 'last_name',
  11. 'status',
  12. 'birth_city_id',
  13. 'birth_province_id',
  14. 'birth_nation_id',
  15. 'birth_date',
  16. 'gender',
  17. 'address',
  18. 'zip_code',
  19. 'fiscal_code',
  20. 'nation_id',
  21. 'province_id',
  22. 'city_id',
  23. 'phone',
  24. 'email',
  25. 'image',
  26. 'enabled',
  27. ];
  28. public function nation()
  29. {
  30. return $this->belongsTo(Nation::class);
  31. }
  32. public function province()
  33. {
  34. return $this->belongsTo(Province::class);
  35. }
  36. public function city()
  37. {
  38. return $this->belongsTo(City::class);
  39. }
  40. public function birth_nation()
  41. {
  42. return $this->belongsTo(Nation::class);
  43. }
  44. public function birth_province()
  45. {
  46. return $this->belongsTo(Province::class);
  47. }
  48. public function birth_city()
  49. {
  50. return $this->belongsTo(City::class);
  51. }
  52. public function cards()
  53. {
  54. return $this->hasMany(MemberCard::class);
  55. }
  56. public function categories()
  57. {
  58. return $this->hasMany(MemberCategory::class);
  59. }
  60. public function certificates()
  61. {
  62. return $this->hasMany(MemberCertificate::class);
  63. }
  64. public function isActive()
  65. {
  66. // Se uno ha una tessera o meno compare nella lista degli utenti (attivo/disattivo),
  67. // per il certificato medico compare la data di scadenza sempre nella lista utenti.
  68. // Se uno invece ha eseguito i pagamenti, al posto del bottone abbiamo detto che mettiamo la scritta (iscritto (ha pagato),
  69. //in sospeso (stato legato alla validità o meno della tessera o del certificato medico, non iscritto (non ha pagato). Io questo ho capito.
  70. $hasCard = $this->isSubscribed()["status"];
  71. $hasCertificate = $this->hasCertificate()["status"];
  72. $ret = array('status' => 0, 'status_text' => 'Non iscritto', 'date' => '');
  73. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('causal')->get();
  74. foreach($records as $record)
  75. {
  76. if ($record->causal->user_status == 1)
  77. {
  78. if ($record->date < date('Y-m-d', strtotime('+1 year')))
  79. $ret = array('status' => 2, 'status_text' => 'Iscritto', 'date' => $newDate = date('Y-m-d', strtotime($record->date. ' + 1 years')));
  80. // if (!$hasCard || !$hasCertificate)
  81. if (!$hasCertificate)
  82. $ret = array('status' => 1,'status_text' => 'In sospeso', 'date' => $newDate = date('Y-m-d', strtotime($record->date. ' + 1 years')));
  83. }
  84. }
  85. return $ret;
  86. }
  87. public function isSubscribed()
  88. {
  89. $ret = array('status' => false, 'date' => '');
  90. $cards = \App\Models\MemberCard::where('member_id', $this->id)->with('card')->orderBy('expire_date')->get();
  91. foreach($cards as $card)
  92. {
  93. if ($card->card->use_for_user_check)
  94. {
  95. $ret = array('status' => $card->expire_date > date("Y-m-d"), 'date' => $card->expire_date);
  96. }
  97. }
  98. return $ret;
  99. }
  100. public function hasCertificate()
  101. {
  102. $ret = array('status' => false, 'date' => '');
  103. $certificates = \App\Models\MemberCertificate::where('member_id', $this->id)->orderBy('expire_date')->get();
  104. foreach($certificates as $certificate)
  105. {
  106. $ret = array('status' => $certificate->expire_date > date("Y-m-d"), 'date' => $certificate->expire_date);
  107. }
  108. return $ret;
  109. }
  110. public function getMoney()
  111. {
  112. $ret = 0;
  113. // Soldi virtuali caricati
  114. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('causal')->get();
  115. foreach($records as $record)
  116. {
  117. if ($record->causal->money == 1)
  118. {
  119. $ret += $record->amount;
  120. }
  121. }
  122. // Soldi virtuali spesi
  123. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('payment_method')->get();
  124. foreach($records as $record)
  125. {
  126. if ($record->payment_method->money == 1)
  127. {
  128. $ret -= $record->amount;
  129. }
  130. }
  131. return $ret;
  132. }
  133. }