Course.php 24 KB

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