| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Course extends Component
- {
- protected $listeners = ['setCausal' => 'setCausal', 'setSubscriptionCausal' => 'setSubscriptionCausal'];
- public $records, $parent_id, $name, $enabled, $dataId, $update = false, $add = false;
- public $course_type_id,
- $course_duration_id,
- $course_frequency_id,
- $course_level_id,
- $causal_id,
- $category_id,
- $sub_causal_id,
- $max_members,
- $instructor_id,
- $year,
- $price,
- $subscription_price,
- $months,
- $when,
- $prices,
- $type,
- $date_from, $date_to;
- public $categories = array();
- public $selectedYear = '';
- public $course_types = [];
- public $course_durations = [];
- public $course_frequencies = [];
- public $course_levels = [];
- public $course_subscriptions = [];
- public $instructors = [];
- public $causals = [];
- public $course_years = [];
- public $monthList = [];
- public $typeIN = 'IN';
- public $setSubscriptionCausal = 'setSubscriptionCausal';
- // public $selectedMonthList = [];
- protected $rules = [
- 'name' => 'required',
- /*'course_type_id' => 'required',
- 'course_duration_id' => 'required',
- 'course_frequency_id' => 'required',
- 'course_level_id' => 'required',
- 'causal_id' => 'required',
- 'sub_causal_id' => 'required',*/
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio',
- /*'causal_id' => 'Campo obbligatorio',
- 'sub_causal_id' => 'Campo obbligatorio',*/
- ];
- public function resetFields(){
- $this->name = '';
- $this->parent_id = null;
- $this->course_type_id = null;
- $this->course_duration_id = null;
- $this->course_frequency_id = null;
- $this->course_level_id = null;
- $this->category_id = null;
- $this->causal_id = null;
- $this->sub_causal_id = null;
- $this->max_members = 0;
- $this->instructor_id = null;
- $this->year = date("Y");
- $this->price = 0;
- $this->subscription_price = 0;
- $this->date_from = null;
- $this->date_to = null;
- $this->months = array();
- $this->enabled = true;
- $this->type = 'standard';
- $this->when = array();
- $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
- $this->prices = [];
- $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
- $this->emit('load-data-table');
- }
- public function getCategories($records, $indentation)
- {
- foreach($records as $record)
- {
- // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
- $this->categories[] = array('id' => $record->id, 'name' => $record->getTree(), 'indentation' => $indentation);
- if(count($record->childs))
- $this->getCategories($record->childs, $indentation + 1);
- }
- }
- public function mount(){
- if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
- return redirect()->to('/dashboard');
- $this->categories = array();
- $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
- for($i=date("Y"); $i<=date("Y") + 1; $i++)
- {
- $this->monthList[$i][1] = "Gennaio";
- $this->monthList[$i][2] = "Febbraio";
- $this->monthList[$i][3] = "Marzo";
- $this->monthList[$i][4] = "Aprile";
- $this->monthList[$i][5] = "Maggio";
- $this->monthList[$i][6] = "Giugno";
- $this->monthList[$i][7] = "Luglio";
- $this->monthList[$i][8] = "Agosto";
- $this->monthList[$i][9] = "Settembre";
- $this->monthList[$i][10] = "Ottobre";
- $this->monthList[$i][11] = "Novembre";
- $this->monthList[$i][12] = "Dicembre";
- }
- $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
- $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
- $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
- $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
- $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
- $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
- $this->instructors = \App\Models\User::select('*')->where('level', 2)->get();
- $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
- }
- public function render()
- {
- if (isset($_GET["year"]))
- $this->selectedYear = $_GET["year"];
- else
- $this->selectedYear = sizeof($this->course_years) > 0 ? $this->course_years[0] : '';
- //$this->selectedYear = date("Y") . "-" . (date("Y") + 1);
- $this->records = \App\Models\Course::where('parent_id', null)->where('year', $this->selectedYear)->with('type', 'duration')->get();
- return view('livewire.course');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- $this->emit('setEdit', true);
- }
- /*
- public function addLevel($parent_id)
- {
- $this->resetFields();
- $this->parent_id = $parent_id;
- $this->add = true;
- $this->update = false;
- }
- */
- public function store()
- {
- $this->validate();
- try {
- $course = new \App\Models\Course();
- $course->name = $this->name;
- $course->parent_id = $this->parent_id;
- $course->course_type_id = $this->course_type_id;
- $course->course_duration_id = $this->course_duration_id;
- $course->course_frequency_id = $this->course_frequency_id;
- $course->course_level_id = $this->course_level_id;
- $course->date_from = $this->date_from;
- $course->date_to = $this->date_to;
- $course->category_id = $this->category_id;
- //$course->causal_id = $this->causal_id;
- //$course->sub_causal_id = $this->sub_causal_id;
- $course->max_members = $this->max_members;
- $course->instructor_id = $this->instructor_id;
- $course->year = $this->year;
- $course->price = currencyToDouble($this->price);
- $course->subscription_price = currencyToDouble($this->subscription_price);
- $course->months = json_encode($this->months);
- $course->type = $this->type;
- $course->when = json_encode($this->when);
- $course->prices = json_encode($this->prices);
- $course->enabled = $this->enabled;
- $course->save();
- $lev = '';
- if ($this->course_level_id > 0)
- $lev = \App\Models\CourseLevel::findOrFail($this->course_level_id)->name;
- $freq = '';
- if ($this->course_frequency_id > 0)
- $freq = \App\Models\CourseLevel::findOrFail($this->course_frequency_id)->name;
- // Creo le causali di pagamento
- $causal = "Pagamento rata " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year;
- $cp = \App\Models\Causal::where('name', $causal)->first();
- if (!$cp)
- {
- $cp = new \App\Models\Causal();
- $cp->name = $causal;
- $cp->type = "IN";
- $cp->parent_id = null;
- $cp->money = false;
- $cp->corrispettivo_fiscale = false;
- $cp->no_receipt = false;
- $cp->user_status = false;
- $cp->no_first = false;
- $cp->no_records = false;
- $cp->enabled = true;
- $cp->save();
- }
- $course->causal_id = $cp->id;
- $causal = "Pagamento iscrizione " . $this->name . " - " . $lev . " - " . $freq . " - " . $this->year;
- $ci = \App\Models\Causal::where('name', $causal)->first();
- if (!$ci)
- {
- $ci = new \App\Models\Causal();
- $ci->name = $causal;
- $ci->type = "IN";
- $ci->parent_id = null;
- $ci->money = false;
- $ci->corrispettivo_fiscale = false;
- $ci->no_receipt = false;
- $ci->user_status = false;
- $ci->no_first = false;
- $ci->no_records = false;
- $ci->enabled = true;
- $ci->save();
- }
- $course->sub_causal_id = $ci->id;
- $course->save();
-
- session()->flash('success','Corso creato');
- $this->resetFields();
- $this->add = false;
- $this->emit('setEdit', false);
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function edit($id){
- $this->resetFields();
- try {
- $course = \App\Models\Course::findOrFail($id);
- if( !$course) {
- session()->flash('error','Corso non trovato');
- } else {
- $this->name = $course->name;
- $this->enabled = $course->enabled;
- $this->parent_id = $course->parent_id;
- $this->course_type_id = $course->course_type_id;
- $this->course_duration_id = $course->course_duration_id;
- $this->course_frequency_id = $course->course_frequency_id;
- $this->course_level_id = $course->course_level_id;
- $this->date_from = $course->date_from;
- $this->date_to = $course->date_to;
- $this->category_id = $course->category_id;
- $this->causal_id = $course->causal_id;
- $this->sub_causal_id = $course->sub_causal_id;
- $this->max_members = $course->max_members;
- $this->instructor_id = $course->instructor_id;
- $this->year = $course->year;
- $this->price = formatPrice($course->price);
- $this->subscription_price = formatPrice($course->subscription_price);
- $this->months = json_decode($course->months);
- $this->when = array();
- if ($course->when != null)
- {
- foreach(json_decode($course->when) as $z)
- {
- $this->when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
- }
- }
- else
- {
- $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
- }
-
- $this->prices = array();
- if ($course->prices != null)
- {
- foreach(json_decode($course->prices) as $z)
- {
- $this->prices[] = array("course_subscription_id" => $z->course_subscription_id, "price" => $z->price);
- }
- }
- else
- {
- $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
- }
- $this->type = $course->type;
- $this->dataId = $course->id;
- $this->update = true;
- $this->add = false;
- }
- $this->emit('setEdit', true);
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function update()
- {
- $this->validate();
- try {
- \App\Models\Course::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'parent_id' => $this->parent_id,
- 'course_type_id' => $this->course_type_id,
- 'course_duration_id' => $this->course_duration_id,
- 'course_frequency_id' => $this->course_frequency_id,
- 'course_level_id' => $this->course_level_id,
- 'date_from' => $this->date_from,
- 'date_to' => $this->date_to,
- 'category_id' => $this->category_id,
- 'causal_id' => $this->causal_id,
- 'sub_causal_id' => $this->sub_causal_id,
- 'max_members' => $this->max_members,
- 'instructor_id' => $this->instructor_id,
- 'year' => $this->year,
- 'price' => currencyToDouble($this->price),
- 'subscription_price' => currencyToDouble($this->subscription_price),
- 'months' => json_encode($this->months),
- 'type' => $this->type,
- 'when' => json_encode($this->when),
- 'prices' => json_encode($this->prices),
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Corso aggiornato');
- $this->resetFields();
- $this->update = false;
- $this->emit('setEdit', false);
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function cancel()
- {
- $this->add = false;
- $this->update = false;
- $this->resetFields();
- $this->emit('setEdit', false);
- }
- public function delete($id)
- {
- try{
- \App\Models\Course::find($id)->delete();
- session()->flash('success',"Corso eliminato");
- return redirect(request()->header('Referer'));
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function setCausal($id, $idx)
- {
- $this->causal_id = $id;
- }
- public function setSubscriptionCausal($id, $idx)
- {
- $this->sub_causal_id = $id;
- }
- public function setCategory($id)
- {
- $this->category_id = $id;
- }
- public function duplicate($id, $isMultiple){
- $course = \App\Models\Course::findOrFail($id);
- $newCourse = $course->replicate();
- $newYear = date("Y") . "-" . (date("Y") + 1);
- if ($course->year != '')
- {
- list($u, $y) = explode("-", $course->year);
- $newYear = ($u + 1) . "-" . ($u + 2);
- }
- $newCourse->year = $newYear;
- $newCourse->save();
- if (!$isMultiple)
- $this->edit($newCourse->id);
- }
- public function duplicateMultiple($ids){
- foreach($ids as $id)
- {
- $this->duplicate($id, true);
- }
- return redirect()->to('/courses');
- }
- public function setDay($idx, $d)
- {
- if (in_array($d, $this->when[$idx]["day"]))
- {
- $i = array_search($d, $this->when[$idx]["day"]);
- array_splice($this->when[$idx]["day"], $i, 1);
- }
- else
- {
- $this->when[$idx]["day"][] = $d;
- }
- }
- public function addRow()
- {
- $this->when[] = array('day' => array(), 'from' => '', 'to' => '');
- }
- public function delRow($idx)
- {
- unset($this->when[$idx]);
- }
- public function addPrice()
- {
- $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
- }
- public function delPrice($idx)
- {
- unset($this->prices[$idx]);
- }
- }
|