Course.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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->sortBy('name'), $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)->orderBy('name')->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->type == 'standard') {
  165. if ($this->when[0]['from'] == '')
  166. $this->msgWhen = 'Devi inserire almeno un giorno';
  167. }
  168. if ($this->prices[0]['course_subscription_id'] == null)
  169. $this->msgPrices = 'Devi inserire almeno un prezzo';
  170. $subscriptions = array_column($this->prices, 'course_subscription_id');
  171. $unique_subscriptions = array_unique($subscriptions);
  172. if (count($subscriptions) != count($unique_subscriptions))
  173. $this->msgPrices = 'Non è possibile aggiungere più volte la stessa tipologia di pagamento';
  174. if ($this->msgPrices == '' && $this->msgWhen == '')
  175. {
  176. $course = new \App\Models\Course();
  177. $course->name = $this->name;
  178. $course->parent_id = $this->parent_id;
  179. $course->course_type_id = $this->course_type_id;
  180. $course->course_duration_id = $this->course_duration_id;
  181. $course->course_frequency_id = $this->course_frequency_id;
  182. $course->course_level_id = $this->course_level_id;
  183. $course->date_from = $this->date_from;
  184. $course->date_to = $this->date_to;
  185. $course->category_id = $this->category_id;
  186. $course->causal_id = $this->causal_id;
  187. $course->sub_causal_id = $this->sub_causal_id;
  188. $course->max_members = $this->max_members;
  189. $course->instructor_id = $this->instructor_id;
  190. $course->year = $this->year;
  191. $course->price = currencyToDouble($this->price);
  192. $course->subscription_price = currencyToDouble($this->subscription_price);
  193. $course->months = json_encode($this->months);
  194. $course->type = $this->type;
  195. $course->when = json_encode($this->when);
  196. $course->prices = json_encode($this->prices);
  197. $course->enabled = $this->enabled;
  198. $course->save();
  199. $lev = '';
  200. if ($this->course_level_id > 0)
  201. $lev = \App\Models\CourseLevel::findOrFail($this->course_level_id)->name;
  202. $freq = '';
  203. if ($this->course_frequency_id > 0)
  204. $freq = \App\Models\CourseFrequency::findOrFail($this->course_frequency_id)->name;
  205. $mFrom = getMonthName(date("n", strtotime($this->date_from)));
  206. $mTo = getMonthName(date("n", strtotime($this->date_to)));
  207. $course_name = $course->name;
  208. // creo il calendario
  209. $from = date("Y-m-d", strtotime($this->date_from));
  210. $to = date("Y-m-d", strtotime($this->date_to));
  211. $endDate = strtotime($to);
  212. foreach($this->when as $d)
  213. {
  214. foreach($d["day"] as $dd)
  215. {
  216. $day = '';
  217. switch ($dd) {
  218. case 'lun':
  219. $day = $days[0];
  220. break;
  221. case 'mar':
  222. $day = $days[1];
  223. break;
  224. case 'mer':
  225. $day = $days[2];
  226. break;
  227. case 'gio':
  228. $day = $days[3];
  229. break;
  230. case 'ven':
  231. $day = $days[4];
  232. break;
  233. case 'sab':
  234. $day = $days[5];
  235. break;
  236. case 'dom':
  237. $day = $days[6];
  238. break;
  239. default:
  240. $day = '';
  241. break;
  242. }
  243. if ($day != '')
  244. {
  245. for($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i))
  246. {
  247. // Controllo che non esiste un corso così
  248. $exist = \App\Models\Calendar::where('from', date('Y-m-d ' . $d["from"] . ":00", $i))->where('to', date('Y-m-d ' . $d["to"] . ":00", $i))->where('name', $course_name)->first();
  249. if (!$exist && !in_array(date('Y-m-d', $i), $this->festivita))
  250. {
  251. // Creo il calendario del corso
  252. $calendar = new \App\Models\Calendar();
  253. $calendar->course_id = $this->course->id;
  254. $calendar->court_id = null;
  255. $calendar->name = $course_name;
  256. $calendar->course_type_id = null;
  257. $calendar->course_duration_id = null;
  258. $calendar->course_frequency_id = null;
  259. $calendar->course_level_id = null;
  260. $calendar->instructor_id = null;
  261. $calendar->from = date('Y-m-d ' . $d["from"] . ":00", $i);
  262. $calendar->to = date('Y-m-d ' . $d["to"] . ":00", $i);
  263. $calendar->note = '';
  264. $calendar->status = 0;
  265. $calendar->save();
  266. }
  267. }
  268. }
  269. }
  270. }
  271. // Creo le causali di pagamento
  272. //$causal = "PAGAMENTO CORSO [nome corso]-LIVELLO-FREQUENZA-ANNO-[tipo abbonamento]-[mese inizio]/[mese fine]
  273. /*
  274. // $causal = "PAGAMENTO CORSO " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  275. $causal = "PAGAMENTO CORSO";
  276. $cp = \App\Models\Causal::where('name', $causal)->first();
  277. if (!$cp)
  278. {
  279. $cp = new \App\Models\Causal();
  280. $cp->name = $causal;
  281. $cp->type = "IN";
  282. $cp->parent_id = null;
  283. $cp->money = false;
  284. $cp->corrispettivo_fiscale = false;
  285. $cp->no_receipt = false;
  286. $cp->user_status = false;
  287. $cp->no_first = false;
  288. $cp->no_records = false;
  289. $cp->enabled = true;
  290. $cp->save();
  291. }
  292. $course->causal_id = $cp->id;
  293. // $causal = "PAGAMENTO ISCRIZIONE " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  294. $causal = "PAGAMENTO ISCRIZIONE";
  295. $ci = \App\Models\Causal::where('name', $causal)->first();
  296. if (!$ci)
  297. {
  298. $ci = new \App\Models\Causal();
  299. $ci->name = $causal;
  300. $ci->type = "IN";
  301. $ci->parent_id = null;
  302. $ci->money = false;
  303. $ci->corrispettivo_fiscale = false;
  304. $ci->no_receipt = false;
  305. $ci->user_status = false;
  306. $ci->no_first = false;
  307. $ci->no_records = false;
  308. $ci->enabled = true;
  309. $ci->save();
  310. }
  311. $course->sub_causal_id = $ci->id;
  312. */
  313. $course->save();
  314. session()->flash('success','Corso creato');
  315. $this->resetFields();
  316. $this->add = false;
  317. $this->emit('setEdit', false);
  318. }
  319. } catch (\Exception $ex) {
  320. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  321. }
  322. }
  323. public function edit($id){
  324. $this->resetFields();
  325. try {
  326. $course = \App\Models\Course::findOrFail($id);
  327. if( !$course) {
  328. session()->flash('error','Corso non trovato');
  329. } else {
  330. $this->name = $course->name;
  331. $this->enabled = $course->enabled;
  332. $this->parent_id = $course->parent_id;
  333. $this->course_type_id = $course->course_type_id;
  334. $this->course_duration_id = $course->course_duration_id;
  335. $this->course_frequency_id = $course->course_frequency_id;
  336. $this->course_level_id = $course->course_level_id;
  337. $this->date_from = $course->date_from;
  338. $this->date_to = $course->date_to;
  339. $this->category_id = $course->category_id;
  340. $this->causal_id = $course->causal_id;
  341. $this->sub_causal_id = $course->sub_causal_id;
  342. $this->max_members = $course->max_members;
  343. $this->instructor_id = $course->instructor_id;
  344. $this->year = $course->year;
  345. $this->price = formatPrice($course->price);
  346. $this->subscription_price = formatPrice($course->subscription_price);
  347. $this->months = json_decode($course->months);
  348. $this->when = array();
  349. if ($course->when != null)
  350. {
  351. foreach(json_decode($course->when) as $z)
  352. {
  353. $this->when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  354. }
  355. }
  356. else
  357. {
  358. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  359. }
  360. $this->prices = array();
  361. if ($course->prices != null)
  362. {
  363. foreach(json_decode($course->prices) as $z)
  364. {
  365. $this->prices[] = array("course_subscription_id" => $z->course_subscription_id, "price" => $z->price);
  366. }
  367. }
  368. else
  369. {
  370. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  371. }
  372. $this->type = $course->type;
  373. $this->dataId = $course->id;
  374. $this->update = true;
  375. $this->add = false;
  376. }
  377. $this->emit('setEdit', true);
  378. } catch (\Exception $ex) {
  379. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  380. }
  381. }
  382. public function update()
  383. {
  384. $this->validate();
  385. try {
  386. $this->msgPrices = '';
  387. $this->msgWhen = '';
  388. if ($this->type == 'standard') {
  389. if ($this->when[0]['from'] == '')
  390. $this->msgWhen = 'Devi inserire almeno un giorno';
  391. }
  392. if ($this->prices[0]['course_subscription_id'] == null)
  393. $this->msgPrices = 'Devi inserire almeno un prezzo';
  394. $subscriptions = array_column($this->prices, 'course_subscription_id');
  395. $unique_subscriptions = array_unique($subscriptions);
  396. if (count($subscriptions) != count($unique_subscriptions))
  397. $this->msgPrices = 'Non è possibile aggiungere più volte la stessa tipologia di pagamento';
  398. if ($this->msgPrices == '' && $this->msgWhen == '')
  399. {
  400. \App\Models\Course::whereId($this->dataId)->update([
  401. 'name' => $this->name,
  402. 'parent_id' => $this->parent_id,
  403. 'course_type_id' => $this->course_type_id,
  404. 'course_duration_id' => $this->course_duration_id,
  405. 'course_frequency_id' => $this->course_frequency_id,
  406. 'course_level_id' => $this->course_level_id,
  407. 'date_from' => $this->date_from,
  408. 'date_to' => $this->date_to,
  409. 'category_id' => $this->category_id,
  410. 'causal_id' => $this->causal_id,
  411. 'sub_causal_id' => $this->sub_causal_id,
  412. 'max_members' => $this->max_members,
  413. 'instructor_id' => $this->instructor_id,
  414. 'year' => $this->year,
  415. 'price' => currencyToDouble($this->price),
  416. 'subscription_price' => currencyToDouble($this->subscription_price),
  417. 'months' => json_encode($this->months),
  418. 'type' => $this->type,
  419. 'when' => json_encode($this->when),
  420. 'prices' => json_encode($this->prices),
  421. 'enabled' => $this->enabled
  422. ]);
  423. session()->flash('success','Corso aggiornato');
  424. $this->resetFields();
  425. $this->update = false;
  426. $this->emit('setEdit', false);
  427. }
  428. } catch (\Exception $ex) {
  429. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  430. }
  431. }
  432. public function cancel()
  433. {
  434. $this->add = false;
  435. $this->update = false;
  436. $this->resetFields();
  437. $this->emit('setEdit', false);
  438. }
  439. public function delete($id)
  440. {
  441. try{
  442. \App\Models\Course::find($id)->delete();
  443. session()->flash('success',"Corso eliminato");
  444. return redirect(request()->header('Referer'));
  445. }catch(\Exception $e){
  446. session()->flash('error','Errore (' . $e->getMessage() . ')');
  447. }
  448. }
  449. public function setCausal($id, $idx)
  450. {
  451. $this->causal_id = $id;
  452. }
  453. public function setSubscriptionCausal($id, $idx)
  454. {
  455. $this->sub_causal_id = $id;
  456. }
  457. public function setCategory($id)
  458. {
  459. $this->category_id = $id;
  460. }
  461. public function duplicate($id, $isMultiple){
  462. $course = \App\Models\Course::findOrFail($id);
  463. $newCourse = $course->replicate();
  464. // $newYear = date("Y") . "-" . (date("Y") + 1);
  465. // if ($course->year != '')
  466. // {
  467. // // list($u, $y) = explode("-", $course->year);
  468. // // $newYear = ($u + 1) . "-" . ($u + 2);
  469. // $newYear = $course->year;
  470. // }
  471. // $newCourse->year = $newYear;
  472. $newCourse->date_from = null;
  473. $newCourse->date_to = null;
  474. $newCourse->year = null;
  475. $newCourse->save();
  476. if (!$isMultiple)
  477. $this->edit($newCourse->id);
  478. }
  479. public function duplicateMultiple($ids){
  480. foreach($ids as $id)
  481. {
  482. $this->duplicate($id, true);
  483. }
  484. return redirect()->to('/courses');
  485. }
  486. public function setDay($idx, $d)
  487. {
  488. if (in_array($d, $this->when[$idx]["day"]))
  489. {
  490. $i = array_search($d, $this->when[$idx]["day"]);
  491. array_splice($this->when[$idx]["day"], $i, 1);
  492. }
  493. else
  494. {
  495. $this->when[$idx]["day"][] = $d;
  496. }
  497. }
  498. public function addRow()
  499. {
  500. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  501. }
  502. public function delRow($idx)
  503. {
  504. unset($this->when[$idx]);
  505. }
  506. public function addPrice()
  507. {
  508. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  509. }
  510. public function delPrice($idx)
  511. {
  512. unset($this->prices[$idx]);
  513. }
  514. }