Rate.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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->course->getDetailsName() . "<br/>" . $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->subscription->name . "<br/>" . $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. // $this->errorMsg = 'Hai selezionato un numero di mesi errato in base all\'abbonamento selezionato' . $this->type .'.'. sizeof($this->months);
  147. $this->errorMsg = 'Mesi selezionati superiori a quelli previsti dall’abbonamento';
  148. } elseif ($this->type == 1 && !$this->month) {
  149. $this->errorMsg = 'Seleziona un mese';
  150. } else {
  151. $rate = new \App\Models\Rate();
  152. $rate->member_id = $this->member_id;
  153. $rate->member_course_id = $this->member_course_id > 0 ? $this->member_course_id : null;
  154. $rate->member_subscription_id = $this->member_subscription_id > 0 ? $this->member_subscription_id : null;
  155. $rate->course_subscription_id = $this->course_subscription_id;
  156. $rate->price = currencyToDouble($this->price);
  157. $rate->date = $this->date;
  158. if ($this->type == '1')
  159. $this->months[] = $this->month;
  160. $rate->months = json_encode($this->months);
  161. $rate->note = '';
  162. $rate->status = 0;
  163. $rate->save();
  164. $this->course_subscription_id = null;
  165. $this->price = 0;
  166. $this->date = null;
  167. $this->month = '';
  168. $this->months = [];
  169. $this->emit('close-popup');
  170. }
  171. }
  172. }
  173. }
  174. public function printReceipt($id)
  175. {
  176. $this->emit('load-data-table');
  177. $receipt = \App\Models\Receipt::findOrFail($id);
  178. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  179. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));//->output();
  180. return $pdf->stream('aaa.pdf');
  181. }
  182. public function search()
  183. {
  184. $this->hasFilter = true;
  185. }
  186. public function disableSearch()
  187. {
  188. $this->filterStatus = "";
  189. $this->filterTo = '';
  190. $this->filterFrom = '';
  191. $this->filteredMemberId = '';
  192. $this->hasFilter = false;
  193. }
  194. public function delete($id)
  195. {
  196. try{
  197. \App\Models\Rate::find($id)->delete();
  198. //$this->emit('load-data-table');
  199. session()->flash('success',"Rata eliminata");
  200. }catch(\Exception $e){
  201. session()->flash('error','Errore (' . $e->getMessage() . ')');
  202. }
  203. }
  204. public function deleteMultiple($ids)
  205. {
  206. try{
  207. foreach($ids as $id)
  208. {
  209. \App\Models\Rate::find($id)->delete();
  210. }
  211. //$this->emit('load-data-table');
  212. session()->flash('success',"Rata eliminata");
  213. }catch(\Exception $e){
  214. session()->flash('error','Errore (' . $e->getMessage() . ')');
  215. }
  216. }
  217. public function suspendMultiple($ids)
  218. {
  219. try{
  220. foreach($ids as $id)
  221. {
  222. $r = \App\Models\Rate::find($id);
  223. $r->status = $r->status == 2 ? 0 : 2;
  224. $r->save();
  225. }
  226. //$this->emit('load-data-table');
  227. session()->flash('success',"Rata eliminata");
  228. }catch(\Exception $e){
  229. session()->flash('error','Errore (' . $e->getMessage() . ')');
  230. }
  231. }
  232. public function addDeleteMonth($m)
  233. {
  234. if (!in_array($m, $this->months)) {
  235. $this->months[] = $m;
  236. } else {
  237. if (($key = array_search($m, $this->months)) !== false) {
  238. unset($this->months[$key]);
  239. $this->months = array_values($this->months);
  240. }
  241. }
  242. }
  243. }