Rate.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 $errorMsg = '';
  28. public function mount()
  29. {
  30. // Load members for the dropdown
  31. $this->member_id = isset($_GET["member_id"]) ? $_GET["member_id"] : 0;
  32. $this->member_course_id = isset($_GET["member_course_id"]) ? $_GET["member_course_id"] : 0;
  33. if ($this->member_id > 0 && $this->member_course_id > 0)
  34. {
  35. $this->mc = \App\Models\MemberCourse::findOrFail($this->member_course_id);
  36. $this->detail = $this->mc->member->first_name . " " . $this->mc->member->last_name;
  37. $this->price_list = [];
  38. $c = $this->mc->course;
  39. if ($c->prices != null)
  40. {
  41. foreach(json_decode($c->prices) as $z)
  42. {
  43. $this->price_list[$z->course_subscription_id] = $z->price;
  44. }
  45. }
  46. $course_subscription_ids = [];
  47. if ($c->prices != null) {
  48. foreach (json_decode($c->prices) as $z) {
  49. if ($z->price > 0)
  50. $course_subscription_ids[] = $z->course_subscription_id;
  51. }
  52. }
  53. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->whereIn('id', $course_subscription_ids)->where('enabled', true)->get();
  54. }
  55. //$this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  56. }
  57. public function render()
  58. {
  59. /*if ($this->hasFilter)
  60. {
  61. $r = \App\Models\Receipt::with('member');
  62. if ($this->filterStatus != '')
  63. $r = $r->where('status', $this->filterStatus);
  64. if ($this->filterFrom != '')
  65. $r = $r->where('date', '>=', $this->filterFrom);
  66. if ($this->filterTo != '')
  67. $r = $r->where('date', '<=', $this->filterTo);
  68. if ($this->filteredMemberId != '')
  69. $r = $r->where('member_id', $this->filteredMemberId);
  70. $this->records = $r->get();
  71. }
  72. else
  73. {*/
  74. $this->records = \App\Models\Rate::with('member')->where('member_course_id', $this->member_course_id)->orderBy('date')->get();
  75. //}
  76. $this->disabled = [];
  77. foreach($this->records as $r)
  78. {
  79. foreach (json_decode($r->months) as $m) {
  80. $this->disabled[] = $m;
  81. }
  82. }
  83. //$this->emit('load-data-table');
  84. return view('livewire.rate');
  85. }
  86. public function updatedCourseSubscriptionId()
  87. {
  88. $this->price = 0;
  89. if (isset($this->price_list[$this->course_subscription_id]))
  90. $this->price = $this->price_list[$this->course_subscription_id];
  91. $this->type = '';
  92. if ($this->course_subscription_id > 0)
  93. $this->type = \App\Models\CourseSubscription::findOrFail($this->course_subscription_id)->months;
  94. }
  95. public function add()
  96. {
  97. $this->errorMsg = '';
  98. if ($this->date == '')
  99. {
  100. $this->errorMsg = 'La data di scadenza è obbligatoria';
  101. }
  102. else
  103. {
  104. if (!$this->course_subscription_id > 0)
  105. {
  106. $this->errorMsg = 'Devi selezionare un tipo di abbonamento';
  107. }
  108. else
  109. {
  110. if ($this->type > 1 && $this->type != sizeof($this->months))
  111. {
  112. $this->errorMsg = 'Hai selezionato un numero di mesi errato in base all\'abbonamento selezionato' . $this->type .'.'. sizeof($this->months);
  113. }
  114. else
  115. {
  116. $rate = new \App\Models\Rate();
  117. $rate->member_id = $this->member_id;
  118. $rate->member_course_id = $this->member_course_id;
  119. $rate->course_subscription_id = $this->course_subscription_id;
  120. $rate->price = currencyToDouble($this->price);
  121. $rate->date = $this->date;
  122. if ($this->type == '1')
  123. $this->months[] = $this->month;
  124. $rate->months = json_encode($this->months);
  125. $rate->note = '';
  126. $rate->status = 0;
  127. $rate->save();
  128. $this->course_subscription_id = null;
  129. $this->price = 0;
  130. $this->date = null;
  131. $this->month = '';
  132. $this->months = [];
  133. $this->emit('close-popup');
  134. }
  135. }
  136. }
  137. }
  138. public function printReceipt($id)
  139. {
  140. $this->emit('load-data-table');
  141. $receipt = \App\Models\Receipt::findOrFail($id);
  142. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  143. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));//->output();
  144. return $pdf->stream('aaa.pdf');
  145. }
  146. public function search()
  147. {
  148. $this->hasFilter = true;
  149. }
  150. public function disableSearch()
  151. {
  152. $this->filterStatus = "";
  153. $this->filterTo = '';
  154. $this->filterFrom = '';
  155. $this->filteredMemberId = '';
  156. $this->hasFilter = false;
  157. }
  158. public function delete($id)
  159. {
  160. try{
  161. \App\Models\Rate::find($id)->delete();
  162. //$this->emit('load-data-table');
  163. session()->flash('success',"Rata eliminata");
  164. }catch(\Exception $e){
  165. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  166. }
  167. }
  168. public function deleteMultiple($ids)
  169. {
  170. try{
  171. foreach($ids as $id)
  172. {
  173. \App\Models\Rate::find($id)->delete();
  174. }
  175. //$this->emit('load-data-table');
  176. session()->flash('success',"Rata eliminata");
  177. }catch(\Exception $e){
  178. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  179. }
  180. }
  181. public function addDeleteMonth($m)
  182. {
  183. if (!in_array($m, $this->months))
  184. {
  185. $this->months[] = $m;
  186. }
  187. else
  188. {
  189. if (($key = array_search($m, $this->months)) !== false)
  190. {
  191. $this->months = array_slice($this->months, $key, 1);
  192. }
  193. }
  194. }
  195. }