Member.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 'enabled',
  26. ];
  27. public function nation()
  28. {
  29. return $this->belongsTo(Nation::class);
  30. }
  31. public function province()
  32. {
  33. return $this->belongsTo(Province::class);
  34. }
  35. public function city()
  36. {
  37. return $this->belongsTo(City::class);
  38. }
  39. public function birth_nation()
  40. {
  41. return $this->belongsTo(Nation::class);
  42. }
  43. public function birth_province()
  44. {
  45. return $this->belongsTo(Province::class);
  46. }
  47. public function birth_city()
  48. {
  49. return $this->belongsTo(City::class);
  50. }
  51. public function isActive()
  52. {
  53. $ret = array('status' => false, 'date' => '');
  54. $cards = \App\Models\MemberCard::where('member_id', $this->id)->with('card')->orderBy('expire_date')->get();
  55. foreach($cards as $card)
  56. {
  57. if ($card->card->use_for_user_check)
  58. {
  59. $ret = array('status' => $card->expire_date > date("Y-m-d"), 'date' => $card->expire_date);
  60. }
  61. }
  62. return $ret;
  63. }
  64. public function getMoney()
  65. {
  66. $ret = 0;
  67. // Soldi virtuali caricati
  68. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('causal')->get();
  69. foreach($records as $record)
  70. {
  71. if ($record->causal->money == 1)
  72. {
  73. $ret += $record->amount;
  74. }
  75. }
  76. // Soldi virtuali spesi
  77. $records = \App\Models\Record::where('member_id', $this->id)->where('type', 'IN')->with('payment_method')->get();
  78. foreach($records as $record)
  79. {
  80. if ($record->payment_method->money == 1)
  81. {
  82. $ret -= $record->amount;
  83. }
  84. }
  85. return $ret;
  86. }
  87. }