Course.php 17 KB

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