Rate.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Barryvdh\DomPDF\Facade\Pdf;
  5. use App\Models\Member;
  6. use App\Http\Middleware\TenantMiddleware;
  7. use Illuminate\Support\Facades\DB;
  8. class Rate extends Component
  9. {
  10. public $records;
  11. public $member_id = 0;
  12. public $member_course_id = 0;
  13. public $member_subscription_id = 0;
  14. public $rate_from = 'course';
  15. public $mc = null;
  16. public $ms = null;
  17. public $filterStatus = '';
  18. public $hasFilter = false;
  19. public $filterFrom = '', $filterTo = '';
  20. public $filteredMemberId = '';
  21. public $members = [];
  22. public $detail = '';
  23. public $course_subscription_id;
  24. public $price;
  25. public $date;
  26. public $month = '';
  27. public $months = [];
  28. public $disabled = [];
  29. public $course_subscriptions = [];
  30. public $price_list = [];
  31. public $type = '';
  32. public $errorMsg = '';
  33. public function boot()
  34. {
  35. app(TenantMiddleware::class)->setupTenantConnection();
  36. }
  37. protected function setupTenantConnection()
  38. {
  39. $user = auth()->user();
  40. config(['database.connections.tenant' => [
  41. 'driver' => 'mysql',
  42. 'host' => '127.0.0.1',
  43. 'port' => '3306',
  44. 'database' => $user->tenant_database,
  45. 'username' => $user->tenant_username,
  46. 'password' => $user->tenant_password,
  47. ]]);
  48. config(['database.default' => 'tenant']);
  49. DB::purge('tenant');
  50. DB::reconnect('tenant');
  51. }
  52. public function mount()
  53. {
  54. // Load members for the dropdown
  55. $this->member_id = isset($_GET["member_id"]) ? $_GET["member_id"] : 0;
  56. $this->member_course_id = isset($_GET["member_course_id"]) ? $_GET["member_course_id"] : 0;
  57. if ($this->member_id > 0 && $this->member_course_id > 0)
  58. {
  59. $this->mc = \App\Models\MemberCourse::findOrFail($this->member_course_id);
  60. $this->detail = $this->mc->member->first_name . " " . $this->mc->member->last_name;
  61. $this->price_list = [];
  62. $c = $this->mc->course;
  63. if ($c->prices != null)
  64. {
  65. foreach(json_decode($c->prices) as $z)
  66. {
  67. $this->price_list[$z->course_subscription_id] = $z->price;
  68. }
  69. }
  70. $course_subscription_ids = [];
  71. if ($c->prices != null) {
  72. foreach (json_decode($c->prices) as $z) {
  73. if ($z->price > 0)
  74. $course_subscription_ids[] = $z->course_subscription_id;
  75. }
  76. }
  77. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->whereIn('id', $course_subscription_ids)->where('enabled', true)->get();
  78. $this->rate_from = 'course';
  79. }
  80. $this->member_subscription_id = isset($_GET["member_subscription_id"]) ? $_GET["member_subscription_id"] : 0;
  81. if ($this->member_id > 0 && $this->member_subscription_id > 0)
  82. {
  83. $this->ms = \App\Models\MemberSubscription::findOrFail($this->member_subscription_id);
  84. $this->detail = $this->ms->member->first_name . " " . $this->ms->member->last_name;
  85. $this->price_list = [];
  86. $s = $this->ms->subscription;
  87. if ($s->prices != null)
  88. {
  89. foreach(json_decode($s->prices) as $z)
  90. {
  91. $this->price_list[$z->course_subscription_id] = $z->price;
  92. }
  93. }
  94. $course_subscription_ids = [];
  95. if ($s->prices != null) {
  96. foreach (json_decode($s->prices) as $z) {
  97. if ($z->price > 0)
  98. $course_subscription_ids[] = $z->course_subscription_id;
  99. }
  100. }
  101. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->whereIn('id', $course_subscription_ids)->where('enabled', true)->get();
  102. $this->rate_from = 'subscription';
  103. }
  104. }
  105. public function render()
  106. {
  107. if ($this->member_id > 0 && $this->member_course_id > 0) {
  108. $this->records = \App\Models\Rate::with('member')->where('member_course_id', $this->member_course_id)->where('member_id', $this->member_id)->orderBy('date')->get();
  109. } elseif ($this->member_id > 0 && $this->member_subscription_id > 0) {
  110. $this->records = \App\Models\Rate::with('member')->where('member_subscription_id', $this->member_subscription_id)->where('member_id', $this->member_id)->orderBy('date')->get();
  111. }
  112. $this->disabled = [];
  113. foreach($this->records as $r)
  114. {
  115. foreach (json_decode($r->months) as $m) {
  116. $this->disabled[] = $m;
  117. }
  118. }
  119. return view('livewire.rate');
  120. }
  121. public function updatedCourseSubscriptionId()
  122. {
  123. $this->price = 0;
  124. if (isset($this->price_list[$this->course_subscription_id]))
  125. $this->price = $this->price_list[$this->course_subscription_id];
  126. $this->type = '';
  127. if ($this->course_subscription_id > 0)
  128. $this->type = \App\Models\CourseSubscription::findOrFail($this->course_subscription_id)->months;
  129. }
  130. public function add()
  131. {
  132. $this->errorMsg = '';
  133. if ($this->date == '')
  134. {
  135. $this->errorMsg = 'La data di scadenza è obbligatoria';
  136. }
  137. else
  138. {
  139. if (!$this->course_subscription_id > 0)
  140. {
  141. $this->errorMsg = 'Devi selezionare un tipo di abbonamento';
  142. }
  143. else
  144. {
  145. if ($this->type > 1 && $this->type < sizeof($this->months))
  146. {
  147. // $this->errorMsg = 'Hai selezionato un numero di mesi errato in base all\'abbonamento selezionato' . $this->type .'.'. sizeof($this->months);
  148. $this->errorMsg = 'Mesi selezionati superiori a quelli previsti dall’abbonamento';
  149. }
  150. elseif ($this->type == 1 && !$this->month) {
  151. $this->errorMsg = 'Seleziona un mese';
  152. } else {
  153. $rate = new \App\Models\Rate();
  154. $rate->member_id = $this->member_id;
  155. $rate->member_course_id = $this->member_course_id;
  156. $rate->member_subscription_id = $this->member_subscription_id;
  157. $rate->course_subscription_id = $this->course_subscription_id;
  158. $rate->price = currencyToDouble($this->price);
  159. $rate->date = $this->date;
  160. if ($this->type == '1')
  161. $this->months[] = $this->month;
  162. $rate->months = json_encode($this->months);
  163. $rate->note = '';
  164. $rate->status = 0;
  165. $rate->save();
  166. $this->course_subscription_id = null;
  167. $this->price = 0;
  168. $this->date = null;
  169. $this->month = '';
  170. $this->months = [];
  171. $this->emit('close-popup');
  172. }
  173. }
  174. }
  175. }
  176. public function printReceipt($id)
  177. {
  178. $this->emit('load-data-table');
  179. $receipt = \App\Models\Receipt::findOrFail($id);
  180. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  181. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));//->output();
  182. return $pdf->stream('aaa.pdf');
  183. }
  184. public function search()
  185. {
  186. $this->hasFilter = true;
  187. }
  188. public function disableSearch()
  189. {
  190. $this->filterStatus = "";
  191. $this->filterTo = '';
  192. $this->filterFrom = '';
  193. $this->filteredMemberId = '';
  194. $this->hasFilter = false;
  195. }
  196. public function delete($id)
  197. {
  198. try{
  199. \App\Models\Rate::find($id)->delete();
  200. //$this->emit('load-data-table');
  201. session()->flash('success',"Rata eliminata");
  202. }catch(\Exception $e){
  203. session()->flash('error','Errore (' . $e->getMessage() . ')');
  204. }
  205. }
  206. public function deleteMultiple($ids)
  207. {
  208. try{
  209. foreach($ids as $id)
  210. {
  211. \App\Models\Rate::find($id)->delete();
  212. }
  213. //$this->emit('load-data-table');
  214. session()->flash('success',"Rata eliminata");
  215. }catch(\Exception $e){
  216. session()->flash('error','Errore (' . $e->getMessage() . ')');
  217. }
  218. }
  219. public function suspendMultiple($ids)
  220. {
  221. try{
  222. foreach($ids as $id)
  223. {
  224. $r = \App\Models\Rate::find($id);
  225. $r->status = $r->status == 2 ? 0 : 2;
  226. $r->save();
  227. }
  228. //$this->emit('load-data-table');
  229. session()->flash('success',"Rata eliminata");
  230. }catch(\Exception $e){
  231. session()->flash('error','Errore (' . $e->getMessage() . ')');
  232. }
  233. }
  234. public function addDeleteMonth($m)
  235. {
  236. if (!in_array($m, $this->months)) {
  237. $this->months[] = $m;
  238. } else {
  239. if (($key = array_search($m, $this->months)) !== false) {
  240. unset($this->months[$key]);
  241. $this->months = array_values($this->months);
  242. }
  243. }
  244. }
  245. }