Causal.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Causal extends Component
  5. {
  6. public $recordsIn, $recordsOut, $parent_id, $name, $enabled, $corrispettivo_fiscale, $no_receipt, $money, $user_status, $no_first, $no_records, $no_reports, $type, $dataId, $update = false, $add = false;
  7. public $corrispettivo_causal_id = 0;
  8. public $parent = '';
  9. protected $rules = [
  10. 'name' => 'required',
  11. 'type' => 'required'
  12. ];
  13. protected $messages = [
  14. 'name.required' => 'Il nome è obbligatorio'
  15. ];
  16. public function mount(){
  17. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  18. return redirect()->to('/dashboard');
  19. $fisc = \App\Models\Causal::where('corrispettivo_fiscale', true)->first();
  20. if ($fisc)
  21. $this->corrispettivo_causal_id = $fisc->id;
  22. }
  23. public function resetFields(){
  24. $this->name = '';
  25. $this->parent_id = null;
  26. $this->parent = '';
  27. $this->type = null;
  28. $this->money = false;
  29. $this->corrispettivo_fiscale = false;
  30. $this->no_receipt = false;
  31. $this->user_status = false;
  32. $this->no_first = false;
  33. $this->no_records = false;
  34. $this->no_reports = false;
  35. $this->enabled = true;
  36. }
  37. public function render()
  38. {
  39. $this->recordsIn = \App\Models\Causal::where('parent_id', null)->where('type', 'IN')->get();
  40. $this->recordsOut = \App\Models\Causal::where('parent_id', null)->where('type', 'OUT')->get();
  41. return view('livewire.causal');
  42. }
  43. public function add()
  44. {
  45. $this->resetFields();
  46. $this->add = true;
  47. $this->update = false;
  48. }
  49. public function addLevel($parent_id)
  50. {
  51. $this->resetFields();
  52. $this->parent_id = $parent_id;
  53. $p = \App\Models\Causal::findOrFail($parent_id);
  54. $this->type = $p->type;
  55. $this->parent = $p->name;
  56. $this->add = true;
  57. $this->update = false;
  58. }
  59. public function store()
  60. {
  61. $this->validate();
  62. try {
  63. \App\Models\Causal::create([
  64. 'name' => $this->name,
  65. 'type' => $this->type,
  66. 'parent_id' => $this->parent_id,
  67. 'money' => $this->money,
  68. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  69. 'no_receipt' => $this->no_receipt,
  70. 'user_status' => $this->user_status,
  71. 'no_first' => $this->no_first,
  72. 'no_records' => $this->no_records,
  73. 'no_reports' => $this->no_reports,
  74. 'enabled' => $this->enabled
  75. ]);
  76. session()->flash('success','Causale creata');
  77. $this->resetFields();
  78. $this->add = false;
  79. } catch (\Exception $ex) {
  80. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  81. }
  82. }
  83. public function edit($id){
  84. try {
  85. $causal = \App\Models\Causal::findOrFail($id);
  86. if( !$causal) {
  87. session()->flash('error','Causale non trovata');
  88. } else {
  89. $this->name = $causal->name;
  90. $this->money = $causal->money;
  91. $this->no_receipt = $causal->no_receipt;
  92. $this->user_status = $causal->user_status;
  93. $this->no_first = $causal->no_first;
  94. $this->no_records = $causal->no_records;
  95. $this->no_reports = $causal->no_reports;
  96. $this->enabled = $causal->enabled;
  97. $this->type = $causal->type;
  98. $this->parent_id = $causal->parent_id;
  99. $this->dataId = $causal->id;
  100. $this->update = true;
  101. $this->add = false;
  102. }
  103. } catch (\Exception $ex) {
  104. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  105. }
  106. }
  107. public function update()
  108. {
  109. $this->validate();
  110. try {
  111. \App\Models\Causal::whereId($this->dataId)->update([
  112. 'name' => $this->name,
  113. 'type' => $this->type,
  114. 'parent_id' => $this->parent_id,
  115. 'user_status' => $this->user_status,
  116. 'no_first' => $this->no_first,
  117. 'no_records' => $this->no_records,
  118. 'no_reports' => $this->no_reports,
  119. 'money' => $this->money,
  120. 'no_receipt' => $this->no_receipt,
  121. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  122. 'enabled' => $this->enabled
  123. ]);
  124. session()->flash('success','Tessera aggiornata');
  125. $this->resetFields();
  126. $this->update = false;
  127. } catch (\Exception $ex) {
  128. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  129. }
  130. }
  131. public function cancel()
  132. {
  133. $this->add = false;
  134. $this->update = false;
  135. $this->resetFields();
  136. }
  137. public function delete($id)
  138. {
  139. try{
  140. \App\Models\Causal::find($id)->delete();
  141. session()->flash('success',"Tessera eliminata");
  142. return redirect(request()->header('Referer'));
  143. }catch(\Exception $e){
  144. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  145. }
  146. }
  147. public function duplicate($id)
  148. {
  149. $old = \App\Models\Causal::find($id);
  150. $new = $old->replicate();
  151. $new->name = $new->name . ' - COPIA';
  152. $new->save();
  153. $this->duplicateRecursive($old, $new);
  154. }
  155. public function duplicateRecursive($old, $new)
  156. {
  157. foreach($old->childs as $c)
  158. {
  159. $old1 = \App\Models\Causal::find($c->id);
  160. $new1 = $old1->replicate();
  161. $new1->parent_id = $new->id;
  162. $new1->save();
  163. $this->duplicateRecursive($old1, $new1);
  164. }
  165. }
  166. public function reorder()
  167. {
  168. }
  169. }