Course.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. $date_from, $date_to;
  25. public $categories = array();
  26. public $selectedYear = '';
  27. public $msgPrices = '';
  28. public $msgWhen = '';
  29. public $course_types = [];
  30. public $course_durations = [];
  31. public $course_frequencies = [];
  32. public $course_levels = [];
  33. public $course_subscriptions = [];
  34. public $instructors = [];
  35. public $causals = [];
  36. public $course_years = [];
  37. public $monthList = [];
  38. public $typeIN = 'IN';
  39. public $setSubscriptionCausal = 'setSubscriptionCausal';
  40. // public $selectedMonthList = [];
  41. protected $rules = [
  42. 'name' => 'required',
  43. 'course_frequency_id' => 'required',
  44. 'course_level_id' => 'required',
  45. 'date_from' => 'required',
  46. 'date_to' => 'required',
  47. 'year' => 'required',
  48. 'subscription_price' => 'required|min:0|not_in:0',
  49. /*'course_type_id' => 'required',
  50. 'course_duration_id' => 'required',
  51. 'causal_id' => 'required',
  52. 'sub_causal_id' => 'required',*/
  53. ];
  54. protected $messages = [
  55. 'name.required' => 'Il nome è obbligatorio',
  56. 'subscription_price.required' => 'Deve essere maggionre di zero',
  57. 'subscription_price.not_in' => 'Deve essere maggionre di zero',
  58. /*'causal_id' => 'Campo obbligatorio',
  59. 'sub_causal_id' => 'Campo obbligatorio',*/
  60. ];
  61. public function resetFields(){
  62. $this->name = '';
  63. $this->parent_id = null;
  64. $this->course_type_id = null;
  65. $this->course_duration_id = null;
  66. $this->course_frequency_id = null;
  67. $this->course_level_id = null;
  68. $this->category_id = null;
  69. $this->causal_id = null;
  70. $this->sub_causal_id = null;
  71. $this->max_members = 0;
  72. $this->instructor_id = null;
  73. $this->year = null;
  74. $this->price = 0;
  75. $this->subscription_price = 0;
  76. $this->date_from = null;
  77. $this->date_to = null;
  78. $this->months = array();
  79. $this->enabled = true;
  80. $this->type = 'standard';
  81. $this->when = array();
  82. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  83. $this->prices = [];
  84. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  85. $this->emit('load-data-table');
  86. }
  87. public function getCategories($records, $indentation)
  88. {
  89. foreach($records as $record)
  90. {
  91. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  92. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree(), 'indentation' => $indentation);
  93. if(count($record->childs))
  94. $this->getCategories($record->childs, $indentation + 1);
  95. }
  96. }
  97. public function mount(){
  98. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  99. return redirect()->to('/dashboard');
  100. $this->categories = array();
  101. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  102. for($i=date("Y"); $i<=date("Y") + 1; $i++)
  103. {
  104. $this->monthList[$i][1] = "Gennaio";
  105. $this->monthList[$i][2] = "Febbraio";
  106. $this->monthList[$i][3] = "Marzo";
  107. $this->monthList[$i][4] = "Aprile";
  108. $this->monthList[$i][5] = "Maggio";
  109. $this->monthList[$i][6] = "Giugno";
  110. $this->monthList[$i][7] = "Luglio";
  111. $this->monthList[$i][8] = "Agosto";
  112. $this->monthList[$i][9] = "Settembre";
  113. $this->monthList[$i][10] = "Ottobre";
  114. $this->monthList[$i][11] = "Novembre";
  115. $this->monthList[$i][12] = "Dicembre";
  116. }
  117. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  118. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  119. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  120. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  121. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  122. $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
  123. $this->instructors = \App\Models\User::select('*')->where('level', 2)->get();
  124. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  125. }
  126. public function render()
  127. {
  128. if (isset($_GET["year"]))
  129. $this->selectedYear = $_GET["year"];
  130. else
  131. $this->selectedYear = sizeof($this->course_years) > 0 ? $this->course_years[0] : '';
  132. //$this->selectedYear = date("Y") . "-" . (date("Y") + 1);
  133. $this->records = \App\Models\Course::where('parent_id', null)->where('year', $this->selectedYear)->with('type', 'duration')->get();
  134. return view('livewire.course');
  135. }
  136. public function add()
  137. {
  138. $this->resetFields();
  139. $this->add = true;
  140. $this->update = false;
  141. $this->emit('setEdit', true);
  142. }
  143. /*
  144. public function addLevel($parent_id)
  145. {
  146. $this->resetFields();
  147. $this->parent_id = $parent_id;
  148. $this->add = true;
  149. $this->update = false;
  150. }
  151. */
  152. public function store()
  153. {
  154. $this->validate();
  155. try {
  156. $this->msgPrices = '';
  157. $this->msgWhen = '';
  158. if ($this->when[0]['from'] == '')
  159. $this->msgWhen = 'Devi inserire almeno un giorno';
  160. if ($this->prices[0]['course_subscription_id'] == null)
  161. $this->msgPrices = 'Devi inserire almeno un prezzo';
  162. if ($this->msgPrices == '' && $this->msgWhen == '')
  163. {
  164. $course = new \App\Models\Course();
  165. $course->name = $this->name;
  166. $course->parent_id = $this->parent_id;
  167. $course->course_type_id = $this->course_type_id;
  168. $course->course_duration_id = $this->course_duration_id;
  169. $course->course_frequency_id = $this->course_frequency_id;
  170. $course->course_level_id = $this->course_level_id;
  171. $course->date_from = $this->date_from;
  172. $course->date_to = $this->date_to;
  173. $course->category_id = $this->category_id;
  174. //$course->causal_id = $this->causal_id;
  175. //$course->sub_causal_id = $this->sub_causal_id;
  176. $course->max_members = $this->max_members;
  177. $course->instructor_id = $this->instructor_id;
  178. $course->year = $this->year;
  179. $course->price = currencyToDouble($this->price);
  180. $course->subscription_price = currencyToDouble($this->subscription_price);
  181. $course->months = json_encode($this->months);
  182. $course->type = $this->type;
  183. $course->when = json_encode($this->when);
  184. $course->prices = json_encode($this->prices);
  185. $course->enabled = $this->enabled;
  186. $course->save();
  187. $lev = '';
  188. if ($this->course_level_id > 0)
  189. $lev = \App\Models\CourseLevel::findOrFail($this->course_level_id)->name;
  190. $freq = '';
  191. if ($this->course_frequency_id > 0)
  192. $freq = \App\Models\CourseFrequency::findOrFail($this->course_frequency_id)->name;
  193. $mFrom = getMonthName(date("n", strtotime($this->date_from)));
  194. $mTo = getMonthName(date("n", strtotime($this->date_to)));
  195. // Creo le causali di pagamento
  196. //$causal = "PAGAMENTO CORSO [nome corso]-LIVELLO-FREQUENZA-ANNO-[tipo abbonamento]-[mese inizio]/[mese fine]
  197. $causal = "PAGAMENTO CORSO " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  198. $cp = \App\Models\Causal::where('name', $causal)->first();
  199. if (!$cp)
  200. {
  201. $cp = new \App\Models\Causal();
  202. $cp->name = $causal;
  203. $cp->type = "IN";
  204. $cp->parent_id = null;
  205. $cp->money = false;
  206. $cp->corrispettivo_fiscale = false;
  207. $cp->no_receipt = false;
  208. $cp->user_status = false;
  209. $cp->no_first = false;
  210. $cp->no_records = false;
  211. $cp->enabled = true;
  212. $cp->save();
  213. }
  214. $course->causal_id = $cp->id;
  215. $causal = "PAGAMENTO ISCRIZIONE " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year . " - " . $mFrom . "/" . $mTo;
  216. $ci = \App\Models\Causal::where('name', $causal)->first();
  217. if (!$ci)
  218. {
  219. $ci = new \App\Models\Causal();
  220. $ci->name = $causal;
  221. $ci->type = "IN";
  222. $ci->parent_id = null;
  223. $ci->money = false;
  224. $ci->corrispettivo_fiscale = false;
  225. $ci->no_receipt = false;
  226. $ci->user_status = false;
  227. $ci->no_first = false;
  228. $ci->no_records = false;
  229. $ci->enabled = true;
  230. $ci->save();
  231. }
  232. $course->sub_causal_id = $ci->id;
  233. $course->save();
  234. session()->flash('success','Corso creato');
  235. $this->resetFields();
  236. $this->add = false;
  237. $this->emit('setEdit', false);
  238. }
  239. } catch (\Exception $ex) {
  240. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  241. }
  242. }
  243. public function edit($id){
  244. $this->resetFields();
  245. try {
  246. $course = \App\Models\Course::findOrFail($id);
  247. if( !$course) {
  248. session()->flash('error','Corso non trovato');
  249. } else {
  250. $this->name = $course->name;
  251. $this->enabled = $course->enabled;
  252. $this->parent_id = $course->parent_id;
  253. $this->course_type_id = $course->course_type_id;
  254. $this->course_duration_id = $course->course_duration_id;
  255. $this->course_frequency_id = $course->course_frequency_id;
  256. $this->course_level_id = $course->course_level_id;
  257. $this->date_from = $course->date_from;
  258. $this->date_to = $course->date_to;
  259. $this->category_id = $course->category_id;
  260. $this->causal_id = $course->causal_id;
  261. $this->sub_causal_id = $course->sub_causal_id;
  262. $this->max_members = $course->max_members;
  263. $this->instructor_id = $course->instructor_id;
  264. $this->year = $course->year;
  265. $this->price = formatPrice($course->price);
  266. $this->subscription_price = formatPrice($course->subscription_price);
  267. $this->months = json_decode($course->months);
  268. $this->when = array();
  269. if ($course->when != null)
  270. {
  271. foreach(json_decode($course->when) as $z)
  272. {
  273. $this->when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  274. }
  275. }
  276. else
  277. {
  278. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  279. }
  280. $this->prices = array();
  281. if ($course->prices != null)
  282. {
  283. foreach(json_decode($course->prices) as $z)
  284. {
  285. $this->prices[] = array("course_subscription_id" => $z->course_subscription_id, "price" => $z->price);
  286. }
  287. }
  288. else
  289. {
  290. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  291. }
  292. $this->type = $course->type;
  293. $this->dataId = $course->id;
  294. $this->update = true;
  295. $this->add = false;
  296. }
  297. $this->emit('setEdit', true);
  298. } catch (\Exception $ex) {
  299. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  300. }
  301. }
  302. public function update()
  303. {
  304. $this->validate();
  305. try {
  306. \App\Models\Course::whereId($this->dataId)->update([
  307. 'name' => $this->name,
  308. 'parent_id' => $this->parent_id,
  309. 'course_type_id' => $this->course_type_id,
  310. 'course_duration_id' => $this->course_duration_id,
  311. 'course_frequency_id' => $this->course_frequency_id,
  312. 'course_level_id' => $this->course_level_id,
  313. 'date_from' => $this->date_from,
  314. 'date_to' => $this->date_to,
  315. 'category_id' => $this->category_id,
  316. 'causal_id' => $this->causal_id,
  317. 'sub_causal_id' => $this->sub_causal_id,
  318. 'max_members' => $this->max_members,
  319. 'instructor_id' => $this->instructor_id,
  320. 'year' => $this->year,
  321. 'price' => currencyToDouble($this->price),
  322. 'subscription_price' => currencyToDouble($this->subscription_price),
  323. 'months' => json_encode($this->months),
  324. 'type' => $this->type,
  325. 'when' => json_encode($this->when),
  326. 'prices' => json_encode($this->prices),
  327. 'enabled' => $this->enabled
  328. ]);
  329. session()->flash('success','Corso aggiornato');
  330. $this->resetFields();
  331. $this->update = false;
  332. $this->emit('setEdit', false);
  333. } catch (\Exception $ex) {
  334. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  335. }
  336. }
  337. public function cancel()
  338. {
  339. $this->add = false;
  340. $this->update = false;
  341. $this->resetFields();
  342. $this->emit('setEdit', false);
  343. }
  344. public function delete($id)
  345. {
  346. try{
  347. \App\Models\Course::find($id)->delete();
  348. session()->flash('success',"Corso eliminato");
  349. return redirect(request()->header('Referer'));
  350. }catch(\Exception $e){
  351. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  352. }
  353. }
  354. public function setCausal($id, $idx)
  355. {
  356. $this->causal_id = $id;
  357. }
  358. public function setSubscriptionCausal($id, $idx)
  359. {
  360. $this->sub_causal_id = $id;
  361. }
  362. public function setCategory($id)
  363. {
  364. $this->category_id = $id;
  365. }
  366. public function duplicate($id, $isMultiple){
  367. $course = \App\Models\Course::findOrFail($id);
  368. $newCourse = $course->replicate();
  369. $newYear = date("Y") . "-" . (date("Y") + 1);
  370. if ($course->year != '')
  371. {
  372. list($u, $y) = explode("-", $course->year);
  373. $newYear = ($u + 1) . "-" . ($u + 2);
  374. }
  375. $newCourse->year = $newYear;
  376. $newCourse->save();
  377. if (!$isMultiple)
  378. $this->edit($newCourse->id);
  379. }
  380. public function duplicateMultiple($ids){
  381. foreach($ids as $id)
  382. {
  383. $this->duplicate($id, true);
  384. }
  385. return redirect()->to('/courses');
  386. }
  387. public function setDay($idx, $d)
  388. {
  389. if (in_array($d, $this->when[$idx]["day"]))
  390. {
  391. $i = array_search($d, $this->when[$idx]["day"]);
  392. array_splice($this->when[$idx]["day"], $i, 1);
  393. }
  394. else
  395. {
  396. $this->when[$idx]["day"][] = $d;
  397. }
  398. }
  399. public function addRow()
  400. {
  401. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  402. }
  403. public function delRow($idx)
  404. {
  405. unset($this->when[$idx]);
  406. }
  407. public function addPrice()
  408. {
  409. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  410. }
  411. public function delPrice($idx)
  412. {
  413. unset($this->prices[$idx]);
  414. }
  415. }