Course.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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\CourseLevel::findOrFail($this->course_frequency_id)->name;
  177. // Creo le causali di pagamento
  178. $causal = "Pagamento rata " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year;
  179. $cp = \App\Models\Causal::where('name', $causal)->first();
  180. if (!$cp)
  181. {
  182. $cp = new \App\Models\Causal();
  183. $cp->name = $causal;
  184. $cp->type = "IN";
  185. $cp->parent_id = null;
  186. $cp->money = false;
  187. $cp->corrispettivo_fiscale = false;
  188. $cp->no_receipt = false;
  189. $cp->user_status = false;
  190. $cp->no_first = false;
  191. $cp->no_records = false;
  192. $cp->enabled = true;
  193. $cp->save();
  194. }
  195. $course->causal_id = $cp->id;
  196. $causal = "Pagamento iscrizione " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year;
  197. $ci = \App\Models\Causal::where('name', $causal)->first();
  198. if (!$ci)
  199. {
  200. $ci = new \App\Models\Causal();
  201. $ci->name = $causal;
  202. $ci->type = "IN";
  203. $ci->parent_id = null;
  204. $ci->money = false;
  205. $ci->corrispettivo_fiscale = false;
  206. $ci->no_receipt = false;
  207. $ci->user_status = false;
  208. $ci->no_first = false;
  209. $ci->no_records = false;
  210. $ci->enabled = true;
  211. $ci->save();
  212. }
  213. $course->sub_causal_id = $ci->id;
  214. $course->save();
  215. session()->flash('success','Corso creato');
  216. $this->resetFields();
  217. $this->add = false;
  218. $this->emit('setEdit', false);
  219. } catch (\Exception $ex) {
  220. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  221. }
  222. }
  223. public function edit($id){
  224. $this->resetFields();
  225. try {
  226. $course = \App\Models\Course::findOrFail($id);
  227. if( !$course) {
  228. session()->flash('error','Corso non trovato');
  229. } else {
  230. $this->name = $course->name;
  231. $this->enabled = $course->enabled;
  232. $this->parent_id = $course->parent_id;
  233. $this->course_type_id = $course->course_type_id;
  234. $this->course_duration_id = $course->course_duration_id;
  235. $this->course_frequency_id = $course->course_frequency_id;
  236. $this->course_level_id = $course->course_level_id;
  237. $this->date_from = $course->date_from;
  238. $this->date_to = $course->date_to;
  239. $this->category_id = $course->category_id;
  240. $this->causal_id = $course->causal_id;
  241. $this->sub_causal_id = $course->sub_causal_id;
  242. $this->max_members = $course->max_members;
  243. $this->instructor_id = $course->instructor_id;
  244. $this->year = $course->year;
  245. $this->price = formatPrice($course->price);
  246. $this->subscription_price = formatPrice($course->subscription_price);
  247. $this->months = json_decode($course->months);
  248. $this->when = array();
  249. if ($course->when != null)
  250. {
  251. foreach(json_decode($course->when) as $z)
  252. {
  253. $this->when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  254. }
  255. }
  256. else
  257. {
  258. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  259. }
  260. $this->prices = array();
  261. if ($course->prices != null)
  262. {
  263. foreach(json_decode($course->prices) as $z)
  264. {
  265. $this->prices[] = array("course_subscription_id" => $z->course_subscription_id, "price" => $z->price);
  266. }
  267. }
  268. else
  269. {
  270. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  271. }
  272. $this->type = $course->type;
  273. $this->dataId = $course->id;
  274. $this->update = true;
  275. $this->add = false;
  276. }
  277. $this->emit('setEdit', true);
  278. } catch (\Exception $ex) {
  279. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  280. }
  281. }
  282. public function update()
  283. {
  284. $this->validate();
  285. try {
  286. \App\Models\Course::whereId($this->dataId)->update([
  287. 'name' => $this->name,
  288. 'parent_id' => $this->parent_id,
  289. 'course_type_id' => $this->course_type_id,
  290. 'course_duration_id' => $this->course_duration_id,
  291. 'course_frequency_id' => $this->course_frequency_id,
  292. 'course_level_id' => $this->course_level_id,
  293. 'date_from' => $this->date_from,
  294. 'date_to' => $this->date_to,
  295. 'category_id' => $this->category_id,
  296. 'causal_id' => $this->causal_id,
  297. 'sub_causal_id' => $this->sub_causal_id,
  298. 'max_members' => $this->max_members,
  299. 'instructor_id' => $this->instructor_id,
  300. 'year' => $this->year,
  301. 'price' => currencyToDouble($this->price),
  302. 'subscription_price' => currencyToDouble($this->subscription_price),
  303. 'months' => json_encode($this->months),
  304. 'type' => $this->type,
  305. 'when' => json_encode($this->when),
  306. 'prices' => json_encode($this->prices),
  307. 'enabled' => $this->enabled
  308. ]);
  309. session()->flash('success','Corso aggiornato');
  310. $this->resetFields();
  311. $this->update = false;
  312. $this->emit('setEdit', false);
  313. } catch (\Exception $ex) {
  314. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  315. }
  316. }
  317. public function cancel()
  318. {
  319. $this->add = false;
  320. $this->update = false;
  321. $this->resetFields();
  322. $this->emit('setEdit', false);
  323. }
  324. public function delete($id)
  325. {
  326. try{
  327. \App\Models\Course::find($id)->delete();
  328. session()->flash('success',"Corso eliminato");
  329. return redirect(request()->header('Referer'));
  330. }catch(\Exception $e){
  331. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  332. }
  333. }
  334. public function setCausal($id, $idx)
  335. {
  336. $this->causal_id = $id;
  337. }
  338. public function setSubscriptionCausal($id, $idx)
  339. {
  340. $this->sub_causal_id = $id;
  341. }
  342. public function setCategory($id)
  343. {
  344. $this->category_id = $id;
  345. }
  346. public function duplicate($id, $isMultiple){
  347. $course = \App\Models\Course::findOrFail($id);
  348. $newCourse = $course->replicate();
  349. $newYear = date("Y") . "-" . (date("Y") + 1);
  350. if ($course->year != '')
  351. {
  352. list($u, $y) = explode("-", $course->year);
  353. $newYear = ($u + 1) . "-" . ($u + 2);
  354. }
  355. $newCourse->year = $newYear;
  356. $newCourse->save();
  357. if (!$isMultiple)
  358. $this->edit($newCourse->id);
  359. }
  360. public function duplicateMultiple($ids){
  361. foreach($ids as $id)
  362. {
  363. $this->duplicate($id, true);
  364. }
  365. return redirect()->to('/courses');
  366. }
  367. public function setDay($idx, $d)
  368. {
  369. if (in_array($d, $this->when[$idx]["day"]))
  370. {
  371. $i = array_search($d, $this->when[$idx]["day"]);
  372. array_splice($this->when[$idx]["day"], $i, 1);
  373. }
  374. else
  375. {
  376. $this->when[$idx]["day"][] = $d;
  377. }
  378. }
  379. public function addRow()
  380. {
  381. $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
  382. }
  383. public function delRow($idx)
  384. {
  385. unset($this->when[$idx]);
  386. }
  387. public function addPrice()
  388. {
  389. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  390. }
  391. public function delPrice($idx)
  392. {
  393. unset($this->prices[$idx]);
  394. }
  395. }