Course.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. $sub_causal_id,
  14. $max_members,
  15. $instructor,
  16. $year,
  17. $price,
  18. $subscription_price,
  19. $months,
  20. $date_from, $date_to;
  21. public $course_types = [];
  22. public $course_durations = [];
  23. public $course_frequencies = [];
  24. public $course_levels = [];
  25. public $causals = [];
  26. public $monthList = [];
  27. public $typeIN = 'IN';
  28. public $setSubscriptionCausal = 'setSubscriptionCausal';
  29. // public $selectedMonthList = [];
  30. protected $rules = [
  31. 'name' => 'required',
  32. 'course_type_id' => 'required',
  33. 'course_duration_id' => 'required',
  34. 'course_frequency_id' => 'required',
  35. 'course_level_id' => 'required',
  36. 'causal_id' => 'required',
  37. 'sub_causal_id' => 'required',
  38. ];
  39. protected $messages = [
  40. 'name.required' => 'Il nome è obbligatorio',
  41. 'causal_id' => 'Campo obbligatorio',
  42. 'sub_causal_id' => 'Campo obbligatorio',
  43. ];
  44. public function resetFields(){
  45. $this->name = '';
  46. $this->parent_id = null;
  47. $this->course_type_id = null;
  48. $this->course_duration_id = null;
  49. $this->course_frequency_id = null;
  50. $this->course_level_id = null;
  51. $this->causal_id = null;
  52. $this->sub_causal_id = null;
  53. $this->max_members = 0;
  54. $this->instructor = '';
  55. $this->year = date("Y");
  56. $this->price = 0;
  57. $this->subscription_price = 0;
  58. $this->date_from = null;
  59. $this->date_to = null;
  60. $this->months = array();
  61. $this->enabled = true;
  62. }
  63. public function mount()
  64. {
  65. for($i=date("Y"); $i<=date("Y") + 1; $i++)
  66. {
  67. $this->monthList[$i][1] = "Gennaio";
  68. $this->monthList[$i][2] = "Febbraio";
  69. $this->monthList[$i][3] = "Marzo";
  70. $this->monthList[$i][4] = "Aprile";
  71. $this->monthList[$i][5] = "Maggio";
  72. $this->monthList[$i][6] = "Giugno";
  73. $this->monthList[$i][7] = "Luglio";
  74. $this->monthList[$i][8] = "Agosto";
  75. $this->monthList[$i][9] = "Settembre";
  76. $this->monthList[$i][10] = "Ottobre";
  77. $this->monthList[$i][11] = "Novembre";
  78. $this->monthList[$i][12] = "Dicembre";
  79. }
  80. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  81. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  82. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  83. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  84. $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
  85. }
  86. public function render()
  87. {
  88. $this->records = \App\Models\Course::where('parent_id', null)->with('type', 'duration')->get();
  89. return view('livewire.course');
  90. }
  91. public function add()
  92. {
  93. $this->resetFields();
  94. $this->add = true;
  95. $this->update = false;
  96. $this->emit('setEdit', true);
  97. }
  98. /*
  99. public function addLevel($parent_id)
  100. {
  101. $this->resetFields();
  102. $this->parent_id = $parent_id;
  103. $this->add = true;
  104. $this->update = false;
  105. }
  106. */
  107. public function store()
  108. {
  109. $this->validate();
  110. try {
  111. \App\Models\Course::create([
  112. 'name' => $this->name,
  113. 'parent_id' => $this->parent_id,
  114. 'course_type_id' => $this->course_type_id,
  115. 'course_duration_id' => $this->course_duration_id,
  116. 'course_frequency_id' => $this->course_frequency_id,
  117. 'course_level_id' => $this->course_level_id,
  118. 'date_from' => $this->date_from,
  119. 'date_to' => $this->date_to,
  120. 'causal_id' => $this->causal_id,
  121. 'sub_causal_id' => $this->sub_causal_id,
  122. 'max_members' => $this->max_members,
  123. 'instructor' => $this->instructor,
  124. 'year' => $this->year,
  125. 'price' => currencyToDouble($this->price),
  126. 'subscription_price' => currencyToDouble($this->subscription_price),
  127. 'months' => json_encode($this->months),
  128. 'enabled' => $this->enabled
  129. ]);
  130. session()->flash('success','Corso creato');
  131. $this->resetFields();
  132. $this->add = false;
  133. $this->emit('setEdit', false);
  134. } catch (\Exception $ex) {
  135. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  136. }
  137. }
  138. public function edit($id){
  139. try {
  140. $course = \App\Models\Course::findOrFail($id);
  141. if( !$course) {
  142. session()->flash('error','Corso non trovato');
  143. } else {
  144. $this->name = $course->name;
  145. $this->enabled = $course->enabled;
  146. $this->parent_id = $course->parent_id;
  147. $this->course_type_id = $course->course_type_id;
  148. $this->course_duration_id = $course->course_duration_id;
  149. $this->course_frequency_id = $course->course_frequency_id;
  150. $this->course_level_id = $course->course_level_id;
  151. $this->date_from = $course->date_from;
  152. $this->date_to = $course->date_to;
  153. $this->causal_id = $course->causal_id;
  154. $this->sub_causal_id = $course->sub_causal_id;
  155. $this->max_members = $course->max_members;
  156. $this->instructor = $course->instructor;
  157. $this->year = $course->year;
  158. $this->price = formatPrice($course->price);
  159. $this->subscription_price = formatPrice($course->subscription_price);
  160. $this->months = json_decode($course->months);
  161. $this->dataId = $course->id;
  162. $this->update = true;
  163. $this->add = false;
  164. }
  165. $this->emit('setEdit', true);
  166. } catch (\Exception $ex) {
  167. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  168. }
  169. }
  170. public function update()
  171. {
  172. $this->validate();
  173. try {
  174. \App\Models\Course::whereId($this->dataId)->update([
  175. 'name' => $this->name,
  176. 'parent_id' => $this->parent_id,
  177. 'course_type_id' => $this->course_type_id,
  178. 'course_duration_id' => $this->course_duration_id,
  179. 'course_frequency_id' => $this->course_frequency_id,
  180. 'course_level_id' => $this->course_level_id,
  181. 'date_from' => $this->date_from,
  182. 'date_to' => $this->date_to,
  183. 'causal_id' => $this->causal_id,
  184. 'sub_causal_id' => $this->sub_causal_id,
  185. 'max_members' => $this->max_members,
  186. 'instructor' => $this->instructor,
  187. 'year' => $this->year,
  188. 'price' => currencyToDouble($this->price),
  189. 'subscription_price' => currencyToDouble($this->subscription_price),
  190. 'months' => json_encode($this->months),
  191. 'enabled' => $this->enabled
  192. ]);
  193. session()->flash('success','Corso aggiornato');
  194. $this->resetFields();
  195. $this->update = false;
  196. $this->emit('setEdit', false);
  197. } catch (\Exception $ex) {
  198. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  199. }
  200. }
  201. public function cancel()
  202. {
  203. $this->add = false;
  204. $this->update = false;
  205. $this->resetFields();
  206. $this->emit('setEdit', false);
  207. }
  208. public function delete($id)
  209. {
  210. try{
  211. \App\Models\Course::find($id)->delete();
  212. session()->flash('success',"Corso eliminato");
  213. }catch(\Exception $e){
  214. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  215. }
  216. }
  217. public function setCausal($id, $idx)
  218. {
  219. $this->causal_id = $id;
  220. }
  221. public function setSubscriptionCausal($id, $idx)
  222. {
  223. $this->sub_causal_id = $id;
  224. }
  225. }