Course.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Course extends Component
  5. {
  6. protected $listeners = ['setCausal' => 'setCausal', 'setSubscriptionCausal' => 'setSubscriptionCausal'];
  7. public $records, $parent_id, $name, $enabled, $dataId, $update = false, $add = false;
  8. public $course_type_id,
  9. $course_duration_id,
  10. $course_frequency_id,
  11. $course_level_id,
  12. $causal_id,
  13. $category_id,
  14. $sub_causal_id,
  15. $max_members,
  16. $instructor_id,
  17. $year,
  18. $price,
  19. $subscription_price,
  20. $months,
  21. $when,
  22. $prices,
  23. $type,
  24. $color,
  25. $date_from, $date_to;
  26. public $categories = array();
  27. public $selectedYear = '';
  28. public $msgPrices = '';
  29. public $msgWhen = '';
  30. public $course_types = [];
  31. public $course_durations = [];
  32. public $course_frequencies = [];
  33. public $course_levels = [];
  34. public $course_subscriptions = [];
  35. public $instructors = [];
  36. public $causals = [];
  37. public $course_years = [];
  38. public $monthList = [];
  39. public $typeIN = 'IN';
  40. public $setSubscriptionCausal = 'setSubscriptionCausal';
  41. // public $selectedMonthList = [];
  42. protected $rules = [
  43. 'name' => 'required',
  44. 'course_frequency_id' => 'required',
  45. 'course_level_id' => 'required',
  46. 'date_from' => 'required',
  47. 'date_to' => 'required',
  48. 'year' => 'required',
  49. 'subscription_price' => 'required|min:0|not_in:0',
  50. /*'course_type_id' => 'required',
  51. 'course_duration_id' => 'required',
  52. 'causal_id' => 'required',
  53. 'sub_causal_id' => 'required',*/
  54. ];
  55. protected $messages = [
  56. 'name.required' => 'Il nome è obbligatorio',
  57. 'subscription_price.required' => 'Deve essere maggionre di zero',
  58. 'subscription_price.not_in' => 'Deve essere maggionre di zero',
  59. /*'causal_id' => 'Campo obbligatorio',
  60. 'sub_causal_id' => 'Campo obbligatorio',*/
  61. ];
  62. public function resetFields(){
  63. $this->name = '';
  64. $this->parent_id = null;
  65. $this->course_type_id = null;
  66. $this->course_duration_id = null;
  67. $this->course_frequency_id = null;
  68. $this->course_level_id = null;
  69. $this->category_id = null;
  70. $this->causal_id = null;
  71. $this->sub_causal_id = null;
  72. $this->max_members = 0;
  73. $this->instructor_id = null;
  74. $this->year = null;
  75. $this->price = 0;
  76. $this->subscription_price = 0;
  77. $this->date_from = null;
  78. $this->date_to = null;
  79. $this->months = array();
  80. $this->enabled = true;
  81. $this->type = 'standard';
  82. $this->color = '';
  83. $this->when = array();
  84. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  85. $this->prices = [];
  86. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  87. $this->emit('load-data-table');
  88. }
  89. public function getCategories($records, $indentation)
  90. {
  91. foreach($records as $record)
  92. {
  93. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  94. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree(), 'indentation' => $indentation);
  95. if(count($record->childs))
  96. $this->getCategories($record->childs, $indentation + 1);
  97. }
  98. }
  99. public function mount(){
  100. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  101. return redirect()->to('/dashboard');
  102. $this->categories = array();
  103. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  104. for($i=date("Y"); $i<=date("Y") + 1; $i++)
  105. {
  106. $this->monthList[$i][1] = "Gennaio";
  107. $this->monthList[$i][2] = "Febbraio";
  108. $this->monthList[$i][3] = "Marzo";
  109. $this->monthList[$i][4] = "Aprile";
  110. $this->monthList[$i][5] = "Maggio";
  111. $this->monthList[$i][6] = "Giugno";
  112. $this->monthList[$i][7] = "Luglio";
  113. $this->monthList[$i][8] = "Agosto";
  114. $this->monthList[$i][9] = "Settembre";
  115. $this->monthList[$i][10] = "Ottobre";
  116. $this->monthList[$i][11] = "Novembre";
  117. $this->monthList[$i][12] = "Dicembre";
  118. }
  119. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  120. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  121. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  122. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  123. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  124. $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
  125. $this->instructors = \App\Models\User::select('*')->where('level', 2)->get();
  126. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  127. }
  128. public function render()
  129. {
  130. if (isset($_GET["year"]))
  131. $this->selectedYear = $_GET["year"];
  132. else
  133. $this->selectedYear = sizeof($this->course_years) > 0 ? $this->course_years[0] : '';
  134. //$this->selectedYear = date("Y") . "-" . (date("Y") + 1);
  135. $this->records = \App\Models\Course::where('parent_id', null)->where('year', $this->selectedYear)->with('type', 'duration')->get();
  136. return view('livewire.course');
  137. }
  138. public function add()
  139. {
  140. $this->resetFields();
  141. $this->add = true;
  142. $this->update = false;
  143. $this->emit('setEdit', true);
  144. }
  145. /*
  146. public function addLevel($parent_id)
  147. {
  148. $this->resetFields();
  149. $this->parent_id = $parent_id;
  150. $this->add = true;
  151. $this->update = false;
  152. }
  153. */
  154. public function store($create=false)
  155. {
  156. $this->validate();
  157. try {
  158. $this->msgPrices = '';
  159. $this->msgWhen = '';
  160. if ($this->when[0]['from'] == '')
  161. $this->msgWhen = 'Devi inserire almeno un giorno';
  162. if ($this->prices[0]['course_subscription_id'] == null)
  163. $this->msgPrices = 'Devi inserire almeno un prezzo';
  164. if ($this->msgPrices == '' && $this->msgWhen == '')
  165. {
  166. $course = new \App\Models\Course();
  167. $course->name = $this->name;
  168. $course->parent_id = $this->parent_id;
  169. $course->course_type_id = $this->course_type_id;
  170. $course->course_duration_id = $this->course_duration_id;
  171. $course->course_frequency_id = $this->course_frequency_id;
  172. $course->course_level_id = $this->course_level_id;
  173. $course->date_from = $this->date_from;
  174. $course->date_to = $this->date_to;
  175. $course->category_id = $this->category_id;
  176. //$course->causal_id = $this->causal_id;
  177. //$course->sub_causal_id = $this->sub_causal_id;
  178. $course->max_members = $this->max_members;
  179. $course->instructor_id = $this->instructor_id;
  180. $course->year = $this->year;
  181. $course->price = currencyToDouble($this->price);
  182. $course->subscription_price = currencyToDouble($this->subscription_price);
  183. $course->months = json_encode($this->months);
  184. $course->type = $this->type;
  185. $course->color = $this->color;
  186. $course->when = json_encode($this->when);
  187. $course->prices = json_encode($this->prices);
  188. $course->enabled = $this->enabled;
  189. $course->save();
  190. $lev = '';
  191. if ($this->course_level_id > 0)
  192. $lev = \App\Models\CourseLevel::findOrFail($this->course_level_id)->name;
  193. $freq = '';
  194. if ($this->course_frequency_id > 0)
  195. $freq = \App\Models\CourseFrequency::findOrFail($this->course_frequency_id)->name;
  196. $mFrom = getMonthName(date("n", strtotime($this->date_from)));
  197. $mTo = getMonthName(date("n", strtotime($this->date_to)));
  198. // Creo le causali di pagamento
  199. //$causal = "PAGAMENTO CORSO [nome corso]-LIVELLO-FREQUENZA-ANNO-[tipo abbonamento]-[mese inizio]/[mese fine]
  200. $causal = "PAGAMENTO CORSO " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  201. $cp = \App\Models\Causal::where('name', $causal)->first();
  202. if (!$cp)
  203. {
  204. $cp = new \App\Models\Causal();
  205. $cp->name = $causal;
  206. $cp->type = "IN";
  207. $cp->parent_id = null;
  208. $cp->money = false;
  209. $cp->corrispettivo_fiscale = false;
  210. $cp->no_receipt = false;
  211. $cp->user_status = false;
  212. $cp->no_first = false;
  213. $cp->no_records = false;
  214. $cp->enabled = true;
  215. $cp->save();
  216. }
  217. $course->causal_id = $cp->id;
  218. $causal = "PAGAMENTO ISCRIZIONE " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  219. $ci = \App\Models\Causal::where('name', $causal)->first();
  220. if (!$ci)
  221. {
  222. $ci = new \App\Models\Causal();
  223. $ci->name = $causal;
  224. $ci->type = "IN";
  225. $ci->parent_id = null;
  226. $ci->money = false;
  227. $ci->corrispettivo_fiscale = false;
  228. $ci->no_receipt = false;
  229. $ci->user_status = false;
  230. $ci->no_first = false;
  231. $ci->no_records = false;
  232. $ci->enabled = true;
  233. $ci->save();
  234. }
  235. $course->sub_causal_id = $ci->id;
  236. $course->save();
  237. if ($create)
  238. $this->createCalendar($course);
  239. session()->flash('success','Corso creato');
  240. $this->resetFields();
  241. $this->add = false;
  242. $this->emit('setEdit', false);
  243. }
  244. } catch (\Exception $ex) {
  245. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  246. }
  247. }
  248. public function edit($id){
  249. $this->resetFields();
  250. try {
  251. $course = \App\Models\Course::findOrFail($id);
  252. if( !$course) {
  253. session()->flash('error','Corso non trovato');
  254. } else {
  255. $this->name = $course->name;
  256. $this->enabled = $course->enabled;
  257. $this->parent_id = $course->parent_id;
  258. $this->course_type_id = $course->course_type_id;
  259. $this->course_duration_id = $course->course_duration_id;
  260. $this->course_frequency_id = $course->course_frequency_id;
  261. $this->course_level_id = $course->course_level_id;
  262. $this->date_from = $course->date_from;
  263. $this->date_to = $course->date_to;
  264. $this->category_id = $course->category_id;
  265. $this->causal_id = $course->causal_id;
  266. $this->sub_causal_id = $course->sub_causal_id;
  267. $this->max_members = $course->max_members;
  268. $this->instructor_id = $course->instructor_id;
  269. $this->year = $course->year;
  270. $this->color = $course->color;
  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($create=false)
  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. 'color' => $this->color,
  332. 'when' => json_encode($this->when),
  333. 'prices' => json_encode($this->prices),
  334. 'enabled' => $this->enabled
  335. ]);
  336. if ($create)
  337. $this->createCalendar(\App\Models\Course::findOrFail($this->dataId));
  338. session()->flash('success','Corso aggiornato');
  339. $this->resetFields();
  340. $this->update = false;
  341. $this->emit('setEdit', false);
  342. } catch (\Exception $ex) {
  343. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  344. }
  345. }
  346. public function cancel()
  347. {
  348. $this->add = false;
  349. $this->update = false;
  350. $this->resetFields();
  351. $this->emit('setEdit', false);
  352. }
  353. public function delete($id)
  354. {
  355. try{
  356. \App\Models\Course::find($id)->delete();
  357. session()->flash('success',"Corso eliminato");
  358. return redirect(request()->header('Referer'));
  359. }catch(\Exception $e){
  360. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  361. }
  362. }
  363. public function setCausal($id, $idx)
  364. {
  365. $this->causal_id = $id;
  366. }
  367. public function setSubscriptionCausal($id, $idx)
  368. {
  369. $this->sub_causal_id = $id;
  370. }
  371. public function setCategory($id)
  372. {
  373. $this->category_id = $id;
  374. }
  375. public function duplicate($id, $isMultiple){
  376. $course = \App\Models\Course::findOrFail($id);
  377. $newCourse = $course->replicate();
  378. $newYear = date("Y") . "-" . (date("Y") + 1);
  379. if ($course->year != '')
  380. {
  381. list($u, $y) = explode("-", $course->year);
  382. $newYear = ($u + 1) . "-" . ($u + 2);
  383. }
  384. $newCourse->year = $newYear;
  385. $newCourse->save();
  386. if (!$isMultiple)
  387. $this->edit($newCourse->id);
  388. }
  389. public function duplicateMultiple($ids){
  390. foreach($ids as $id)
  391. {
  392. $this->duplicate($id, true);
  393. }
  394. return redirect()->to('/courses');
  395. }
  396. public function setDay($idx, $d)
  397. {
  398. if (in_array($d, $this->when[$idx]["day"]))
  399. {
  400. $i = array_search($d, $this->when[$idx]["day"]);
  401. array_splice($this->when[$idx]["day"], $i, 1);
  402. }
  403. else
  404. {
  405. $this->when[$idx]["day"][] = $d;
  406. }
  407. }
  408. public function addRow()
  409. {
  410. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  411. }
  412. public function delRow($idx)
  413. {
  414. unset($this->when[$idx]);
  415. }
  416. public function addPrice()
  417. {
  418. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  419. }
  420. public function delPrice($idx)
  421. {
  422. unset($this->prices[$idx]);
  423. }
  424. public function createCalendar($course)
  425. {
  426. $days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
  427. $from = $course->date_from;
  428. $to = $course->date_to;
  429. $endDate = strtotime($to);
  430. foreach(json_decode($course->when) as $d)
  431. {
  432. foreach($d->day as $dd)
  433. {
  434. $day = '';
  435. switch ($dd) {
  436. case 'lun':
  437. $day = $days[0];
  438. break;
  439. case 'mar':
  440. $day = $days[1];
  441. break;
  442. case 'mer':
  443. $day = $days[2];
  444. break;
  445. case 'gio':
  446. $day = $days[3];
  447. break;
  448. case 'ven':
  449. $day = $days[4];
  450. break;
  451. case 'sab':
  452. $day = $days[5];
  453. break;
  454. case 'dom':
  455. $day = $days[6];
  456. break;
  457. default:
  458. $day = '';
  459. break;
  460. }
  461. if ($day != '')
  462. {
  463. for($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i))
  464. {
  465. $calendar = new \App\Models\Calendar();
  466. $calendar->course_id = $course->id;
  467. $calendar->court_id = null;
  468. $calendar->instructor_id = null;
  469. $calendar->from = date('Y-m-d ' . $d->from . ":00", $i);
  470. $calendar->to = date('Y-m-d ' . $d->to . ":00", $i);
  471. $calendar->note = '';
  472. $calendar->save();
  473. //
  474. }
  475. }
  476. }
  477. }
  478. }
  479. }