Course.php 23 KB

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