Course.php 16 KB

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