Course.php.bak 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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, $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)->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 = 'È possibile aggiungere solo un abbonamento per ciascun tipo';
  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. $cp = \App\Models\Causal::where('name', $causal)->first();
  276. if (!$cp)
  277. {
  278. $cp = new \App\Models\Causal();
  279. $cp->name = $causal;
  280. $cp->type = "IN";
  281. $cp->parent_id = null;
  282. $cp->money = false;
  283. $cp->corrispettivo_fiscale = false;
  284. $cp->no_receipt = false;
  285. $cp->user_status = false;
  286. $cp->no_first = false;
  287. $cp->no_records = false;
  288. $cp->enabled = true;
  289. $cp->save();
  290. }
  291. $course->causal_id = $cp->id;
  292. $causal = "PAGAMENTO ISCRIZIONE " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  293. $ci = \App\Models\Causal::where('name', $causal)->first();
  294. if (!$ci)
  295. {
  296. $ci = new \App\Models\Causal();
  297. $ci->name = $causal;
  298. $ci->type = "IN";
  299. $ci->parent_id = null;
  300. $ci->money = false;
  301. $ci->corrispettivo_fiscale = false;
  302. $ci->no_receipt = false;
  303. $ci->user_status = false;
  304. $ci->no_first = false;
  305. $ci->no_records = false;
  306. $ci->enabled = true;
  307. $ci->save();
  308. }
  309. $course->sub_causal_id = $ci->id;
  310. */
  311. $course->save();
  312. session()->flash('success','Corso creato');
  313. $this->resetFields();
  314. $this->add = false;
  315. $this->emit('setEdit', false);
  316. }
  317. } catch (\Exception $ex) {
  318. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  319. }
  320. }
  321. public function edit($id){
  322. $this->resetFields();
  323. try {
  324. $course = \App\Models\Course::findOrFail($id);
  325. if( !$course) {
  326. session()->flash('error','Corso non trovato');
  327. } else {
  328. $this->name = $course->name;
  329. $this->enabled = $course->enabled;
  330. $this->parent_id = $course->parent_id;
  331. $this->course_type_id = $course->course_type_id;
  332. $this->course_duration_id = $course->course_duration_id;
  333. $this->course_frequency_id = $course->course_frequency_id;
  334. $this->course_level_id = $course->course_level_id;
  335. $this->date_from = $course->date_from;
  336. $this->date_to = $course->date_to;
  337. $this->category_id = $course->category_id;
  338. $this->causal_id = $course->causal_id;
  339. $this->sub_causal_id = $course->sub_causal_id;
  340. $this->max_members = $course->max_members;
  341. $this->instructor_id = $course->instructor_id;
  342. $this->year = $course->year;
  343. $this->price = formatPrice($course->price);
  344. $this->subscription_price = formatPrice($course->subscription_price);
  345. $this->months = json_decode($course->months);
  346. $this->when = array();
  347. if ($course->when != null)
  348. {
  349. foreach(json_decode($course->when) as $z)
  350. {
  351. $this->when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  352. }
  353. }
  354. else
  355. {
  356. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  357. }
  358. $this->prices = array();
  359. if ($course->prices != null)
  360. {
  361. foreach(json_decode($course->prices) as $z)
  362. {
  363. $this->prices[] = array("course_subscription_id" => $z->course_subscription_id, "price" => $z->price);
  364. }
  365. }
  366. else
  367. {
  368. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  369. }
  370. $this->type = $course->type;
  371. $this->dataId = $course->id;
  372. $this->update = true;
  373. $this->add = false;
  374. }
  375. $this->emit('setEdit', true);
  376. } catch (\Exception $ex) {
  377. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  378. }
  379. }
  380. public function update()
  381. {
  382. $this->validate();
  383. try {
  384. \App\Models\Course::whereId($this->dataId)->update([
  385. 'name' => $this->name,
  386. 'parent_id' => $this->parent_id,
  387. 'course_type_id' => $this->course_type_id,
  388. 'course_duration_id' => $this->course_duration_id,
  389. 'course_frequency_id' => $this->course_frequency_id,
  390. 'course_level_id' => $this->course_level_id,
  391. 'date_from' => $this->date_from,
  392. 'date_to' => $this->date_to,
  393. 'category_id' => $this->category_id,
  394. 'causal_id' => $this->causal_id,
  395. 'sub_causal_id' => $this->sub_causal_id,
  396. 'max_members' => $this->max_members,
  397. 'instructor_id' => $this->instructor_id,
  398. 'year' => $this->year,
  399. 'price' => currencyToDouble($this->price),
  400. 'subscription_price' => currencyToDouble($this->subscription_price),
  401. 'months' => json_encode($this->months),
  402. 'type' => $this->type,
  403. 'when' => json_encode($this->when),
  404. 'prices' => json_encode($this->prices),
  405. 'enabled' => $this->enabled
  406. ]);
  407. session()->flash('success','Corso aggiornato');
  408. $this->resetFields();
  409. $this->update = false;
  410. $this->emit('setEdit', false);
  411. } catch (\Exception $ex) {
  412. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  413. }
  414. }
  415. public function cancel()
  416. {
  417. $this->add = false;
  418. $this->update = false;
  419. $this->resetFields();
  420. $this->emit('setEdit', false);
  421. }
  422. public function delete($id)
  423. {
  424. try{
  425. \App\Models\Course::find($id)->delete();
  426. session()->flash('success',"Corso eliminato");
  427. return redirect(request()->header('Referer'));
  428. }catch(\Exception $e){
  429. session()->flash('error','Errore (' . $e->getMessage() . ')');
  430. }
  431. }
  432. public function setCausal($id, $idx)
  433. {
  434. $this->causal_id = $id;
  435. }
  436. public function setSubscriptionCausal($id, $idx)
  437. {
  438. $this->sub_causal_id = $id;
  439. }
  440. public function setCategory($id)
  441. {
  442. $this->category_id = $id;
  443. }
  444. public function duplicate($id, $isMultiple){
  445. $course = \App\Models\Course::findOrFail($id);
  446. $newCourse = $course->replicate();
  447. $newYear = date("Y") . "-" . (date("Y") + 1);
  448. if ($course->year != '')
  449. {
  450. list($u, $y) = explode("-", $course->year);
  451. $newYear = ($u + 1) . "-" . ($u + 2);
  452. }
  453. $newCourse->year = $newYear;
  454. $newCourse->save();
  455. if (!$isMultiple)
  456. $this->edit($newCourse->id);
  457. }
  458. public function duplicateMultiple($ids){
  459. foreach($ids as $id)
  460. {
  461. $this->duplicate($id, true);
  462. }
  463. return redirect()->to('/courses');
  464. }
  465. public function setDay($idx, $d)
  466. {
  467. if (in_array($d, $this->when[$idx]["day"]))
  468. {
  469. $i = array_search($d, $this->when[$idx]["day"]);
  470. array_splice($this->when[$idx]["day"], $i, 1);
  471. }
  472. else
  473. {
  474. $this->when[$idx]["day"][] = $d;
  475. }
  476. }
  477. public function addRow()
  478. {
  479. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  480. }
  481. public function delRow($idx)
  482. {
  483. unset($this->when[$idx]);
  484. }
  485. public function addPrice()
  486. {
  487. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  488. }
  489. public function delPrice($idx)
  490. {
  491. unset($this->prices[$idx]);
  492. }
  493. }