Course.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 $festivita = [];
  43. // public $selectedMonthList = [];
  44. protected $rules = [
  45. 'name' => 'required',
  46. 'course_frequency_id' => 'required',
  47. 'course_level_id' => 'required',
  48. 'date_from' => 'required',
  49. 'date_to' => 'required',
  50. 'year' => 'required',
  51. 'subscription_price' => 'required|min:0|not_in:0',
  52. /*'course_type_id' => 'required',
  53. 'course_duration_id' => 'required',
  54. 'causal_id' => 'required',
  55. 'sub_causal_id' => 'required',*/
  56. ];
  57. protected $messages = [
  58. 'name.required' => 'Il nome è obbligatorio',
  59. 'subscription_price.required' => 'Deve essere maggionre di zero',
  60. 'subscription_price.not_in' => 'Deve essere maggionre di zero',
  61. /*'causal_id' => 'Campo obbligatorio',
  62. 'sub_causal_id' => 'Campo obbligatorio',*/
  63. ];
  64. public function boot()
  65. {
  66. app(TenantMiddleware::class)->setupTenantConnection();
  67. }
  68. public function resetFields(){
  69. $this->name = '';
  70. $this->parent_id = null;
  71. $this->course_type_id = null;
  72. $this->course_duration_id = null;
  73. $this->course_frequency_id = null;
  74. $this->course_level_id = null;
  75. $this->category_id = null;
  76. // $this->causal_id = null;
  77. // $this->sub_causal_id = null;
  78. $this->max_members = 0;
  79. $this->instructor_id = null;
  80. $this->year = null;
  81. $this->price = 0;
  82. $this->subscription_price = 0;
  83. $this->date_from = null;
  84. $this->date_to = null;
  85. $this->months = array();
  86. $this->enabled = true;
  87. $this->type = 'standard';
  88. $this->when = array();
  89. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  90. $this->prices = [];
  91. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  92. $this->emit('load-data-table');
  93. }
  94. public function getCategories($records, $indentation)
  95. {
  96. foreach($records as $record)
  97. {
  98. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  99. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree(), 'indentation' => $indentation);
  100. if(count($record->childs))
  101. $this->getCategories($record->childs->sortBy('name'), $indentation + 1);
  102. }
  103. }
  104. public function mount(){
  105. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  106. return redirect()->to('/dashboard');
  107. $this->categories = array();
  108. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->orderBy('name')->get(), 0);
  109. for($i=date("Y"); $i<=date("Y") + 1; $i++)
  110. {
  111. $this->monthList[$i][1] = "Gennaio";
  112. $this->monthList[$i][2] = "Febbraio";
  113. $this->monthList[$i][3] = "Marzo";
  114. $this->monthList[$i][4] = "Aprile";
  115. $this->monthList[$i][5] = "Maggio";
  116. $this->monthList[$i][6] = "Giugno";
  117. $this->monthList[$i][7] = "Luglio";
  118. $this->monthList[$i][8] = "Agosto";
  119. $this->monthList[$i][9] = "Settembre";
  120. $this->monthList[$i][10] = "Ottobre";
  121. $this->monthList[$i][11] = "Novembre";
  122. $this->monthList[$i][12] = "Dicembre";
  123. }
  124. // $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  125. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  126. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  127. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  128. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  129. $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
  130. $this->instructors = \App\Models\User::select('*')->where('level', 2)->get();
  131. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  132. // Anni dal 2025 al 2037
  133. for ($anno = 2025; $anno <= 2037; $anno++) {
  134. // Festività fisse
  135. $this->festivita[] = "$anno-01-01"; // Capodanno
  136. $this->festivita[] = "$anno-01-06"; // Epifania
  137. $this->festivita[] = "$anno-04-25"; // Festa della Liberazione
  138. $this->festivita[] = "$anno-05-01"; // Festa del Lavoro
  139. $this->festivita[] = "$anno-06-02"; // Festa della Repubblica
  140. $this->festivita[] = "$anno-08-15"; // Ferragosto
  141. $this->festivita[] = "$anno-11-01"; // Ognissanti
  142. $this->festivita[] = "$anno-12-08"; // Immacolata Concezione
  143. $this->festivita[] = "$anno-12-25"; // Natale
  144. $this->festivita[] = "$anno-12-26"; // Santo Stefano
  145. // Calcolo Pasqua e Pasquetta
  146. $pasqua = date("Y-m-d", easter_date($anno));
  147. $pasquetta = date("Y-m-d", strtotime("$pasqua +1 day"));
  148. $this->festivita[] = $pasqua; // Domenica di Pasqua
  149. $this->festivita[] = $pasquetta; // Lunedì dell'Angelo
  150. }
  151. }
  152. public function render()
  153. {
  154. if (isset($_GET["year"]))
  155. $this->selectedYear = $_GET["year"];
  156. else
  157. // $this->selectedYear = sizeof($this->course_years) > 0 ? $this->course_years[0] : '';
  158. $this->selectedYear = date("Y") . "-" . (date("Y") + 1);
  159. $this->records = \App\Models\Course::where('parent_id', null)->where('year', $this->selectedYear)->with('type', 'duration')->get();
  160. return view('livewire.course');
  161. }
  162. public function add()
  163. {
  164. $this->resetFields();
  165. $this->add = true;
  166. $this->update = false;
  167. $this->emit('setEdit', true);
  168. }
  169. /*
  170. public function addLevel($parent_id)
  171. {
  172. $this->resetFields();
  173. $this->parent_id = $parent_id;
  174. $this->add = true;
  175. $this->update = false;
  176. }
  177. */
  178. public function store()
  179. {
  180. $this->validate();
  181. try {
  182. $this->msgPrices = '';
  183. $this->msgWhen = '';
  184. if ($this->type == 'standard') {
  185. if ($this->when[0]['from'] == '')
  186. $this->msgWhen = 'Devi inserire almeno un giorno';
  187. }
  188. if ($this->prices[0]['course_subscription_id'] == null)
  189. $this->msgPrices = 'Devi inserire almeno un prezzo';
  190. $subscriptions = array_column($this->prices, 'course_subscription_id');
  191. $unique_subscriptions = array_unique($subscriptions);
  192. if (count($subscriptions) != count($unique_subscriptions))
  193. $this->msgPrices = 'Non è possibile aggiungere più volte la stessa tipologia di pagamento';
  194. if ($this->msgPrices == '' && $this->msgWhen == '')
  195. {
  196. $course = new \App\Models\Course();
  197. $course->name = $this->name;
  198. $course->parent_id = $this->parent_id;
  199. $course->course_type_id = $this->course_type_id;
  200. $course->course_duration_id = $this->course_duration_id;
  201. $course->course_frequency_id = $this->course_frequency_id;
  202. $course->course_level_id = $this->course_level_id;
  203. $course->date_from = $this->date_from;
  204. $course->date_to = $this->date_to;
  205. $course->category_id = $this->category_id;
  206. // $course->causal_id = $this->causal_id;
  207. // $course->sub_causal_id = $this->sub_causal_id;
  208. $course->max_members = $this->max_members;
  209. $course->instructor_id = $this->instructor_id;
  210. $course->year = $this->year;
  211. $course->price = currencyToDouble($this->price);
  212. $course->subscription_price = currencyToDouble($this->subscription_price);
  213. $course->months = json_encode($this->months);
  214. $course->type = $this->type;
  215. $course->when = json_encode($this->when);
  216. $course->prices = json_encode($this->prices);
  217. $course->enabled = $this->enabled;
  218. $course->save();
  219. $lev = '';
  220. if ($this->course_level_id > 0)
  221. $lev = \App\Models\CourseLevel::findOrFail($this->course_level_id)->name;
  222. $freq = '';
  223. if ($this->course_frequency_id > 0)
  224. $freq = \App\Models\CourseFrequency::findOrFail($this->course_frequency_id)->name;
  225. $mFrom = getMonthName(date("n", strtotime($this->date_from)));
  226. $mTo = getMonthName(date("n", strtotime($this->date_to)));
  227. $course_name = $course->name;
  228. // creo il calendario
  229. $from = date("Y-m-d", strtotime($this->date_from));
  230. $to = date("Y-m-d", strtotime($this->date_to));
  231. $days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
  232. $endDate = strtotime($to);
  233. foreach($this->when as $d)
  234. {
  235. foreach($d["day"] as $dd)
  236. {
  237. $day = '';
  238. switch ($dd) {
  239. case 'lun':
  240. $day = $days[0];
  241. break;
  242. case 'mar':
  243. $day = $days[1];
  244. break;
  245. case 'mer':
  246. $day = $days[2];
  247. break;
  248. case 'gio':
  249. $day = $days[3];
  250. break;
  251. case 'ven':
  252. $day = $days[4];
  253. break;
  254. case 'sab':
  255. $day = $days[5];
  256. break;
  257. case 'dom':
  258. $day = $days[6];
  259. break;
  260. default:
  261. $day = '';
  262. break;
  263. }
  264. if ($day != '')
  265. {
  266. for($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i))
  267. {
  268. // Controllo che non esiste un corso così
  269. $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();
  270. if (!$exist && !in_array(date('Y-m-d', $i), $this->festivita))
  271. {
  272. // Creo il calendario del corso
  273. $calendar = new \App\Models\Calendar();
  274. $calendar->course_id = $course->id;
  275. $calendar->court_id = null;
  276. $calendar->name = $course_name;
  277. $calendar->course_type_id = null;
  278. $calendar->course_duration_id = null;
  279. $calendar->course_frequency_id = null;
  280. $calendar->course_level_id = null;
  281. $calendar->instructor_id = null;
  282. $calendar->from = date('Y-m-d ' . $d["from"] . ":00", $i);
  283. $calendar->to = date('Y-m-d ' . $d["to"] . ":00", $i);
  284. $calendar->note = '';
  285. $calendar->status = 0;
  286. $calendar->save();
  287. }
  288. }
  289. }
  290. }
  291. }
  292. // Creo le causali di pagamento
  293. //$causal = "PAGAMENTO CORSO [nome corso]-LIVELLO-FREQUENZA-ANNO-[tipo abbonamento]-[mese inizio]/[mese fine]
  294. // $causal = "PAGAMENTO CORSO " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  295. $causal = "PAGAMENTO CORSO";
  296. $cp = \App\Models\Causal::where('name', $causal)->first();
  297. if (!$cp)
  298. {
  299. $cp = new \App\Models\Causal();
  300. $cp->name = $causal;
  301. $cp->type = "IN";
  302. $cp->parent_id = null;
  303. $cp->money = false;
  304. $cp->corrispettivo_fiscale = false;
  305. $cp->no_receipt = false;
  306. $cp->user_status = false;
  307. $cp->no_first = false;
  308. $cp->no_records = false;
  309. $cp->enabled = true;
  310. $cp->save();
  311. }
  312. $course->causal_id = $cp->id;
  313. // $causal = "PAGAMENTO ISCRIZIONE " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  314. $causal = "PAGAMENTO ISCRIZIONE";
  315. $ci = \App\Models\Causal::where('name', $causal)->first();
  316. if (!$ci)
  317. {
  318. $ci = new \App\Models\Causal();
  319. $ci->name = $causal;
  320. $ci->type = "IN";
  321. $ci->parent_id = null;
  322. $ci->money = false;
  323. $ci->corrispettivo_fiscale = false;
  324. $ci->no_receipt = false;
  325. $ci->user_status = false;
  326. $ci->no_first = false;
  327. $ci->no_records = false;
  328. $ci->enabled = true;
  329. $ci->save();
  330. }
  331. $course->sub_causal_id = $ci->id;
  332. $course->save();
  333. session()->flash('success','Corso creato');
  334. $this->resetFields();
  335. $this->add = false;
  336. $this->emit('setEdit', false);
  337. }
  338. } catch (\Exception $ex) {
  339. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  340. }
  341. }
  342. public function edit($id){
  343. $this->resetFields();
  344. try {
  345. $course = \App\Models\Course::findOrFail($id);
  346. if( !$course) {
  347. session()->flash('error','Corso non trovato');
  348. } else {
  349. $this->name = $course->name;
  350. $this->enabled = $course->enabled;
  351. $this->parent_id = $course->parent_id;
  352. $this->course_type_id = $course->course_type_id;
  353. $this->course_duration_id = $course->course_duration_id;
  354. $this->course_frequency_id = $course->course_frequency_id;
  355. $this->course_level_id = $course->course_level_id;
  356. $this->date_from = $course->date_from;
  357. $this->date_to = $course->date_to;
  358. $this->category_id = $course->category_id;
  359. // $this->causal_id = $course->causal_id;
  360. // $this->sub_causal_id = $course->sub_causal_id;
  361. $this->max_members = $course->max_members;
  362. $this->instructor_id = $course->instructor_id;
  363. $this->year = $course->year;
  364. $this->price = formatPrice($course->price);
  365. $this->subscription_price = formatPrice($course->subscription_price);
  366. $this->months = json_decode($course->months);
  367. $this->when = array();
  368. if ($course->when != null)
  369. {
  370. foreach(json_decode($course->when) as $z)
  371. {
  372. $this->when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  373. }
  374. }
  375. else
  376. {
  377. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  378. }
  379. $this->prices = array();
  380. if ($course->prices != null)
  381. {
  382. foreach(json_decode($course->prices) as $z)
  383. {
  384. $this->prices[] = array("course_subscription_id" => $z->course_subscription_id, "price" => $z->price);
  385. }
  386. }
  387. else
  388. {
  389. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  390. }
  391. $this->type = $course->type;
  392. $this->dataId = $course->id;
  393. $this->update = true;
  394. $this->add = false;
  395. }
  396. $this->emit('setEdit', true);
  397. } catch (\Exception $ex) {
  398. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  399. }
  400. }
  401. public function update()
  402. {
  403. $this->validate();
  404. try {
  405. $this->msgPrices = '';
  406. $this->msgWhen = '';
  407. if ($this->type == 'standard') {
  408. if ($this->when[0]['from'] == '')
  409. $this->msgWhen = 'Devi inserire almeno un giorno';
  410. }
  411. if ($this->prices[0]['course_subscription_id'] == null)
  412. $this->msgPrices = 'Devi inserire almeno un prezzo';
  413. $subscriptions = array_column($this->prices, 'course_subscription_id');
  414. $unique_subscriptions = array_unique($subscriptions);
  415. if (count($subscriptions) != count($unique_subscriptions))
  416. $this->msgPrices = 'Non è possibile aggiungere più volte la stessa tipologia di pagamento';
  417. if ($this->msgPrices == '' && $this->msgWhen == '')
  418. {
  419. \App\Models\Course::whereId($this->dataId)->update([
  420. 'name' => $this->name,
  421. 'parent_id' => $this->parent_id,
  422. 'course_type_id' => $this->course_type_id,
  423. 'course_duration_id' => $this->course_duration_id,
  424. 'course_frequency_id' => $this->course_frequency_id,
  425. 'course_level_id' => $this->course_level_id,
  426. 'date_from' => $this->date_from,
  427. 'date_to' => $this->date_to,
  428. 'category_id' => $this->category_id,
  429. // 'causal_id' => $this->causal_id,
  430. // 'sub_causal_id' => $this->sub_causal_id,
  431. 'max_members' => $this->max_members,
  432. 'instructor_id' => $this->instructor_id,
  433. 'year' => $this->year,
  434. 'price' => currencyToDouble($this->price),
  435. 'subscription_price' => currencyToDouble($this->subscription_price),
  436. 'months' => json_encode($this->months),
  437. 'type' => $this->type,
  438. 'when' => json_encode($this->when),
  439. 'prices' => json_encode($this->prices),
  440. 'enabled' => $this->enabled
  441. ]);
  442. session()->flash('success','Corso aggiornato');
  443. $this->resetFields();
  444. $this->update = false;
  445. $this->emit('setEdit', false);
  446. }
  447. } catch (\Exception $ex) {
  448. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  449. }
  450. }
  451. public function cancel()
  452. {
  453. $this->add = false;
  454. $this->update = false;
  455. $this->resetFields();
  456. $this->emit('setEdit', false);
  457. }
  458. public function delete($id)
  459. {
  460. try{
  461. \App\Models\Course::find($id)->delete();
  462. session()->flash('success',"Corso eliminato");
  463. return redirect(request()->header('Referer'));
  464. }catch(\Exception $e){
  465. session()->flash('error','Errore (' . $e->getMessage() . ')');
  466. }
  467. }
  468. // public function setCausal($id, $idx)
  469. // {
  470. // $this->causal_id = $id;
  471. // }
  472. // public function setSubscriptionCausal($id, $idx)
  473. // {
  474. // $this->sub_causal_id = $id;
  475. // }
  476. public function setCategory($id)
  477. {
  478. $this->category_id = $id;
  479. }
  480. public function duplicate($id, $isMultiple){
  481. $course = \App\Models\Course::findOrFail($id);
  482. $newCourse = $course->replicate();
  483. // $newYear = date("Y") . "-" . (date("Y") + 1);
  484. // if ($course->year != '')
  485. // {
  486. // // list($u, $y) = explode("-", $course->year);
  487. // // $newYear = ($u + 1) . "-" . ($u + 2);
  488. // $newYear = $course->year;
  489. // }
  490. // $newCourse->year = $newYear;
  491. $newCourse->date_from = null;
  492. $newCourse->date_to = null;
  493. $newCourse->year = null;
  494. $newCourse->save();
  495. if (!$isMultiple)
  496. $this->edit($newCourse->id);
  497. }
  498. public function duplicateMultiple($ids){
  499. foreach($ids as $id)
  500. {
  501. $this->duplicate($id, true);
  502. }
  503. return redirect()->to('/courses');
  504. }
  505. public function setDay($idx, $d)
  506. {
  507. if (in_array($d, $this->when[$idx]["day"]))
  508. {
  509. $i = array_search($d, $this->when[$idx]["day"]);
  510. array_splice($this->when[$idx]["day"], $i, 1);
  511. }
  512. else
  513. {
  514. $this->when[$idx]["day"][] = $d;
  515. }
  516. }
  517. public function addRow()
  518. {
  519. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  520. }
  521. public function delRow($idx)
  522. {
  523. unset($this->when[$idx]);
  524. }
  525. public function addPrice()
  526. {
  527. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  528. }
  529. public function delPrice($idx)
  530. {
  531. unset($this->prices[$idx]);
  532. }
  533. }