Rate.php.bak 8.3 KB

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