Course.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. namespace App\Http\Livewire;
  3. use App\Http\Middleware\TenantMiddleware;
  4. use Livewire\Component;
  5. use Illuminate\Support\Facades\Auth;
  6. class Course extends Component
  7. {
  8. protected $listeners = ['setCausal' => 'setCausal', 'setSubscriptionCausal' => 'setSubscriptionCausal'];
  9. public $records, $parent_id, $name, $enabled, $dataId, $update = false, $add = false;
  10. public $course_type_id,
  11. $course_duration_id,
  12. $course_frequency_id,
  13. $course_level_id,
  14. $causal_id,
  15. $category_id,
  16. $sub_causal_id,
  17. $max_members,
  18. $instructor_id,
  19. $year,
  20. $price,
  21. $subscription_price,
  22. $months,
  23. $when,
  24. $prices,
  25. $type,
  26. $date_from, $date_to;
  27. public $categories = array();
  28. public $selectedYear = '';
  29. public $msgPrices = '';
  30. public $msgWhen = '';
  31. public $course_types = [];
  32. public $course_durations = [];
  33. public $course_frequencies = [];
  34. public $course_levels = [];
  35. public $course_subscriptions = [];
  36. public $instructors = [];
  37. public $causals = [];
  38. public $course_years = [];
  39. public $monthList = [];
  40. public $typeIN = 'IN';
  41. public $setSubscriptionCausal = 'setSubscriptionCausal';
  42. // public $selectedMonthList = [];
  43. protected $rules = [
  44. 'name' => 'required',
  45. 'course_frequency_id' => 'required',
  46. 'course_level_id' => 'required',
  47. 'date_from' => 'required',
  48. 'date_to' => 'required',
  49. 'year' => 'required',
  50. 'subscription_price' => 'required|min:0|not_in:0',
  51. /*'course_type_id' => 'required',
  52. 'course_duration_id' => 'required',
  53. 'causal_id' => 'required',
  54. 'sub_causal_id' => 'required',*/
  55. ];
  56. protected $messages = [
  57. 'name.required' => 'Il nome è obbligatorio',
  58. 'subscription_price.required' => 'Deve essere maggionre di zero',
  59. 'subscription_price.not_in' => 'Deve essere maggionre di zero',
  60. /*'causal_id' => 'Campo obbligatorio',
  61. 'sub_causal_id' => 'Campo obbligatorio',*/
  62. ];
  63. public function boot()
  64. {
  65. app(TenantMiddleware::class)->setupTenantConnection();
  66. }
  67. public function resetFields(){
  68. $this->name = '';
  69. $this->parent_id = null;
  70. $this->course_type_id = null;
  71. $this->course_duration_id = null;
  72. $this->course_frequency_id = null;
  73. $this->course_level_id = null;
  74. $this->category_id = null;
  75. $this->causal_id = null;
  76. $this->sub_causal_id = null;
  77. $this->max_members = 0;
  78. $this->instructor_id = null;
  79. $this->year = null;
  80. $this->price = 0;
  81. $this->subscription_price = 0;
  82. $this->date_from = null;
  83. $this->date_to = null;
  84. $this->months = array();
  85. $this->enabled = true;
  86. $this->type = 'standard';
  87. $this->when = array();
  88. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  89. $this->prices = [];
  90. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  91. $this->emit('load-data-table');
  92. }
  93. public function getCategories($records, $indentation)
  94. {
  95. foreach($records as $record)
  96. {
  97. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  98. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree(), 'indentation' => $indentation);
  99. if(count($record->childs))
  100. $this->getCategories($record->childs, $indentation + 1);
  101. }
  102. }
  103. public function mount(){
  104. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  105. return redirect()->to('/dashboard');
  106. $this->categories = array();
  107. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  108. for($i=date("Y"); $i<=date("Y") + 1; $i++)
  109. {
  110. $this->monthList[$i][1] = "Gennaio";
  111. $this->monthList[$i][2] = "Febbraio";
  112. $this->monthList[$i][3] = "Marzo";
  113. $this->monthList[$i][4] = "Aprile";
  114. $this->monthList[$i][5] = "Maggio";
  115. $this->monthList[$i][6] = "Giugno";
  116. $this->monthList[$i][7] = "Luglio";
  117. $this->monthList[$i][8] = "Agosto";
  118. $this->monthList[$i][9] = "Settembre";
  119. $this->monthList[$i][10] = "Ottobre";
  120. $this->monthList[$i][11] = "Novembre";
  121. $this->monthList[$i][12] = "Dicembre";
  122. }
  123. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  124. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  125. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  126. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  127. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  128. $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
  129. $this->instructors = \App\Models\User::select('*')->where('level', 2)->get();
  130. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  131. }
  132. public function render()
  133. {
  134. if (isset($_GET["year"]))
  135. $this->selectedYear = $_GET["year"];
  136. else
  137. $this->selectedYear = sizeof($this->course_years) > 0 ? $this->course_years[0] : '';
  138. //$this->selectedYear = date("Y") . "-" . (date("Y") + 1);
  139. $this->records = \App\Models\Course::where('parent_id', null)->where('year', $this->selectedYear)->with('type', 'duration')->get();
  140. return view('livewire.course');
  141. }
  142. public function add()
  143. {
  144. $this->resetFields();
  145. $this->add = true;
  146. $this->update = false;
  147. $this->emit('setEdit', true);
  148. }
  149. /*
  150. public function addLevel($parent_id)
  151. {
  152. $this->resetFields();
  153. $this->parent_id = $parent_id;
  154. $this->add = true;
  155. $this->update = false;
  156. }
  157. */
  158. public function store()
  159. {
  160. $this->validate();
  161. try {
  162. $this->msgPrices = '';
  163. $this->msgWhen = '';
  164. if ($this->when[0]['from'] == '')
  165. $this->msgWhen = 'Devi inserire almeno un giorno';
  166. if ($this->prices[0]['course_subscription_id'] == null)
  167. $this->msgPrices = 'Devi inserire almeno un prezzo';
  168. if ($this->msgPrices == '' && $this->msgWhen == '')
  169. {
  170. $course = new \App\Models\Course();
  171. $course->name = $this->name;
  172. $course->parent_id = $this->parent_id;
  173. $course->course_type_id = $this->course_type_id;
  174. $course->course_duration_id = $this->course_duration_id;
  175. $course->course_frequency_id = $this->course_frequency_id;
  176. $course->course_level_id = $this->course_level_id;
  177. $course->date_from = $this->date_from;
  178. $course->date_to = $this->date_to;
  179. $course->category_id = $this->category_id;
  180. //$course->causal_id = $this->causal_id;
  181. //$course->sub_causal_id = $this->sub_causal_id;
  182. $course->max_members = $this->max_members;
  183. $course->instructor_id = $this->instructor_id;
  184. $course->year = $this->year;
  185. $course->price = currencyToDouble($this->price);
  186. $course->subscription_price = currencyToDouble($this->subscription_price);
  187. $course->months = json_encode($this->months);
  188. $course->type = $this->type;
  189. $course->when = json_encode($this->when);
  190. $course->prices = json_encode($this->prices);
  191. $course->enabled = $this->enabled;
  192. $course->save();
  193. $lev = '';
  194. if ($this->course_level_id > 0)
  195. $lev = \App\Models\CourseLevel::findOrFail($this->course_level_id)->name;
  196. $freq = '';
  197. if ($this->course_frequency_id > 0)
  198. $freq = \App\Models\CourseFrequency::findOrFail($this->course_frequency_id)->name;
  199. $mFrom = getMonthName(date("n", strtotime($this->date_from)));
  200. $mTo = getMonthName(date("n", strtotime($this->date_to)));
  201. // Creo le causali di pagamento
  202. //$causal = "PAGAMENTO CORSO [nome corso]-LIVELLO-FREQUENZA-ANNO-[tipo abbonamento]-[mese inizio]/[mese fine]
  203. $causal = "PAGAMENTO CORSO " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  204. $cp = \App\Models\Causal::where('name', $causal)->first();
  205. if (!$cp)
  206. {
  207. $cp = new \App\Models\Causal();
  208. $cp->name = $causal;
  209. $cp->type = "IN";
  210. $cp->parent_id = null;
  211. $cp->money = false;
  212. $cp->corrispettivo_fiscale = false;
  213. $cp->no_receipt = false;
  214. $cp->user_status = false;
  215. $cp->no_first = false;
  216. $cp->no_records = false;
  217. $cp->enabled = true;
  218. $cp->save();
  219. }
  220. $course->causal_id = $cp->id;
  221. $causal = "PAGAMENTO ISCRIZIONE " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  222. $ci = \App\Models\Causal::where('name', $causal)->first();
  223. if (!$ci)
  224. {
  225. $ci = new \App\Models\Causal();
  226. $ci->name = $causal;
  227. $ci->type = "IN";
  228. $ci->parent_id = null;
  229. $ci->money = false;
  230. $ci->corrispettivo_fiscale = false;
  231. $ci->no_receipt = false;
  232. $ci->user_status = false;
  233. $ci->no_first = false;
  234. $ci->no_records = false;
  235. $ci->enabled = true;
  236. $ci->save();
  237. }
  238. $course->sub_causal_id = $ci->id;
  239. $course->save();
  240. session()->flash('success','Corso creato');
  241. $this->resetFields();
  242. $this->add = false;
  243. $this->emit('setEdit', false);
  244. }
  245. } catch (\Exception $ex) {
  246. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  247. }
  248. }
  249. public function edit($id){
  250. $this->resetFields();
  251. try {
  252. $course = \App\Models\Course::findOrFail($id);
  253. if( !$course) {
  254. session()->flash('error','Corso non trovato');
  255. } else {
  256. $this->name = $course->name;
  257. $this->enabled = $course->enabled;
  258. $this->parent_id = $course->parent_id;
  259. $this->course_type_id = $course->course_type_id;
  260. $this->course_duration_id = $course->course_duration_id;
  261. $this->course_frequency_id = $course->course_frequency_id;
  262. $this->course_level_id = $course->course_level_id;
  263. $this->date_from = $course->date_from;
  264. $this->date_to = $course->date_to;
  265. $this->category_id = $course->category_id;
  266. $this->causal_id = $course->causal_id;
  267. $this->sub_causal_id = $course->sub_causal_id;
  268. $this->max_members = $course->max_members;
  269. $this->instructor_id = $course->instructor_id;
  270. $this->year = $course->year;
  271. $this->price = formatPrice($course->price);
  272. $this->subscription_price = formatPrice($course->subscription_price);
  273. $this->months = json_decode($course->months);
  274. $this->when = array();
  275. if ($course->when != null)
  276. {
  277. foreach(json_decode($course->when) as $z)
  278. {
  279. $this->when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  280. }
  281. }
  282. else
  283. {
  284. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  285. }
  286. $this->prices = array();
  287. if ($course->prices != null)
  288. {
  289. foreach(json_decode($course->prices) as $z)
  290. {
  291. $this->prices[] = array("course_subscription_id" => $z->course_subscription_id, "price" => $z->price);
  292. }
  293. }
  294. else
  295. {
  296. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  297. }
  298. $this->type = $course->type;
  299. $this->dataId = $course->id;
  300. $this->update = true;
  301. $this->add = false;
  302. }
  303. $this->emit('setEdit', true);
  304. } catch (\Exception $ex) {
  305. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  306. }
  307. }
  308. public function update()
  309. {
  310. $this->validate();
  311. try {
  312. \App\Models\Course::whereId($this->dataId)->update([
  313. 'name' => $this->name,
  314. 'parent_id' => $this->parent_id,
  315. 'course_type_id' => $this->course_type_id,
  316. 'course_duration_id' => $this->course_duration_id,
  317. 'course_frequency_id' => $this->course_frequency_id,
  318. 'course_level_id' => $this->course_level_id,
  319. 'date_from' => $this->date_from,
  320. 'date_to' => $this->date_to,
  321. 'category_id' => $this->category_id,
  322. 'causal_id' => $this->causal_id,
  323. 'sub_causal_id' => $this->sub_causal_id,
  324. 'max_members' => $this->max_members,
  325. 'instructor_id' => $this->instructor_id,
  326. 'year' => $this->year,
  327. 'price' => currencyToDouble($this->price),
  328. 'subscription_price' => currencyToDouble($this->subscription_price),
  329. 'months' => json_encode($this->months),
  330. 'type' => $this->type,
  331. 'when' => json_encode($this->when),
  332. 'prices' => json_encode($this->prices),
  333. 'enabled' => $this->enabled
  334. ]);
  335. session()->flash('success','Corso aggiornato');
  336. $this->resetFields();
  337. $this->update = false;
  338. $this->emit('setEdit', false);
  339. } catch (\Exception $ex) {
  340. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  341. }
  342. }
  343. public function cancel()
  344. {
  345. $this->add = false;
  346. $this->update = false;
  347. $this->resetFields();
  348. $this->emit('setEdit', false);
  349. }
  350. public function delete($id)
  351. {
  352. try{
  353. \App\Models\Course::find($id)->delete();
  354. session()->flash('success',"Corso eliminato");
  355. return redirect(request()->header('Referer'));
  356. }catch(\Exception $e){
  357. session()->flash('error','Errore (' . $e->getMessage() . ')');
  358. }
  359. }
  360. public function setCausal($id, $idx)
  361. {
  362. $this->causal_id = $id;
  363. }
  364. public function setSubscriptionCausal($id, $idx)
  365. {
  366. $this->sub_causal_id = $id;
  367. }
  368. public function setCategory($id)
  369. {
  370. $this->category_id = $id;
  371. }
  372. public function duplicate($id, $isMultiple){
  373. $course = \App\Models\Course::findOrFail($id);
  374. $newCourse = $course->replicate();
  375. $newYear = date("Y") . "-" . (date("Y") + 1);
  376. if ($course->year != '')
  377. {
  378. list($u, $y) = explode("-", $course->year);
  379. $newYear = ($u + 1) . "-" . ($u + 2);
  380. }
  381. $newCourse->year = $newYear;
  382. $newCourse->save();
  383. if (!$isMultiple)
  384. $this->edit($newCourse->id);
  385. }
  386. public function duplicateMultiple($ids){
  387. foreach($ids as $id)
  388. {
  389. $this->duplicate($id, true);
  390. }
  391. return redirect()->to('/courses');
  392. }
  393. public function setDay($idx, $d)
  394. {
  395. if (in_array($d, $this->when[$idx]["day"]))
  396. {
  397. $i = array_search($d, $this->when[$idx]["day"]);
  398. array_splice($this->when[$idx]["day"], $i, 1);
  399. }
  400. else
  401. {
  402. $this->when[$idx]["day"][] = $d;
  403. }
  404. }
  405. public function addRow()
  406. {
  407. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  408. }
  409. public function delRow($idx)
  410. {
  411. unset($this->when[$idx]);
  412. }
  413. public function addPrice()
  414. {
  415. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  416. }
  417. public function delPrice($idx)
  418. {
  419. unset($this->prices[$idx]);
  420. }
  421. }