Rate.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Barryvdh\DomPDF\Facade\Pdf;
  5. use App\Models\Member;
  6. class Rate extends Component
  7. {
  8. public $records;
  9. public $member_id = 0;
  10. public $member_course_id = 0;
  11. public $mc = null;
  12. public $filterStatus = '';
  13. public $hasFilter = false;
  14. public $filterFrom = '', $filterTo = '';
  15. public $filteredMemberId = '';
  16. public $members = [];
  17. public $detail = '';
  18. public $course_subscription_id;
  19. public $price;
  20. public $date;
  21. public $month = '';
  22. public $months = [];
  23. public $disabled = [];
  24. public $couse_subscriptions = [];
  25. public $price_list = [];
  26. public $type = '';
  27. public function mount()
  28. {
  29. // Load members for the dropdown
  30. $this->member_id = isset($_GET["member_id"]) ? $_GET["member_id"] : 0;
  31. $this->member_course_id = isset($_GET["member_course_id"]) ? $_GET["member_course_id"] : 0;
  32. if ($this->member_id > 0 && $this->member_course_id > 0)
  33. {
  34. $this->mc = \App\Models\MemberCourse::findOrFail($this->member_course_id);
  35. $this->detail = $this->mc->member->first_name . " " . $this->mc->member->last_name;
  36. $this->price_list = [];
  37. $c = $this->mc->course;
  38. if ($c->prices != null)
  39. {
  40. foreach(json_decode($c->prices) as $z)
  41. {
  42. $this->price_list[$z->course_subscription_id] = $z->price;
  43. }
  44. }
  45. $course_subscription_ids = [];
  46. if ($c->prices != null) {
  47. foreach (json_decode($c->prices) as $z) {
  48. if ($z->price > 0)
  49. $course_subscription_ids[] = $z->course_subscription_id;
  50. }
  51. }
  52. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->whereIn('id', $course_subscription_ids)->where('enabled', true)->get();
  53. }
  54. //$this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  55. }
  56. public function render()
  57. {
  58. /*if ($this->hasFilter)
  59. {
  60. $r = \App\Models\Receipt::with('member');
  61. if ($this->filterStatus != '')
  62. $r = $r->where('status', $this->filterStatus);
  63. if ($this->filterFrom != '')
  64. $r = $r->where('date', '>=', $this->filterFrom);
  65. if ($this->filterTo != '')
  66. $r = $r->where('date', '<=', $this->filterTo);
  67. if ($this->filteredMemberId != '')
  68. $r = $r->where('member_id', $this->filteredMemberId);
  69. $this->records = $r->get();
  70. }
  71. else
  72. {*/
  73. $this->records = \App\Models\Rate::with('member')->where('member_course_id', $this->member_course_id)->orderBy('date')->get();
  74. //}
  75. $this->disabled = [];
  76. foreach($this->records as $r)
  77. {
  78. foreach (json_decode($r->months) as $m) {
  79. $this->disabled[] = $m;
  80. }
  81. }
  82. //$this->emit('load-data-table');
  83. return view('livewire.rate');
  84. }
  85. public function updatedCourseSubscriptionId()
  86. {
  87. $this->price = 0;
  88. if (isset($this->price_list[$this->course_subscription_id]))
  89. $this->price = $this->price_list[$this->course_subscription_id];
  90. $this->type = '';
  91. if ($this->course_subscription_id > 0)
  92. $this->type = \App\Models\CourseSubscription::findOrFail($this->course_subscription_id)->months;
  93. }
  94. public function add()
  95. {
  96. $rate = new \App\Models\Rate();
  97. $rate->member_id = $this->member_id;
  98. $rate->member_course_id = $this->member_course_id;
  99. $rate->course_subscription_id = $this->course_subscription_id;
  100. $rate->price = currencyToDouble($this->price);
  101. $rate->date = $this->date;
  102. if ($this->type == '1')
  103. $this->months[] = $this->month;
  104. $rate->months = json_encode($this->months);
  105. $rate->note = '';
  106. $rate->status = 0;
  107. $rate->save();
  108. $this->course_subscription_id = null;
  109. $this->price = 0;
  110. $this->date = null;
  111. $this->month = '';
  112. $this->months = [];
  113. $this->emit('close-popup');
  114. }
  115. public function printReceipt($id)
  116. {
  117. $this->emit('load-data-table');
  118. $receipt = \App\Models\Receipt::findOrFail($id);
  119. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  120. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));//->output();
  121. return $pdf->stream('aaa.pdf');
  122. }
  123. public function search()
  124. {
  125. $this->hasFilter = true;
  126. }
  127. public function disableSearch()
  128. {
  129. $this->filterStatus = "";
  130. $this->filterTo = '';
  131. $this->filterFrom = '';
  132. $this->filteredMemberId = '';
  133. $this->hasFilter = false;
  134. }
  135. public function delete($id)
  136. {
  137. try{
  138. \App\Models\Rate::find($id)->delete();
  139. //$this->emit('load-data-table');
  140. session()->flash('success',"Rata eliminata");
  141. }catch(\Exception $e){
  142. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  143. }
  144. }
  145. public function deleteMultiple($ids)
  146. {
  147. try{
  148. foreach($ids as $id)
  149. {
  150. \App\Models\Rate::find($id)->delete();
  151. }
  152. //$this->emit('load-data-table');
  153. session()->flash('success',"Rata eliminata");
  154. }catch(\Exception $e){
  155. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  156. }
  157. }
  158. public function addDeleteMonth($m)
  159. {
  160. if (!in_array($m, $this->months))
  161. {
  162. $this->months[] = $m;
  163. }
  164. else
  165. {
  166. }
  167. }
  168. }