Causal.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, $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->enabled = true;
  35. }
  36. public function render()
  37. {
  38. $this->recordsIn = \App\Models\Causal::where('parent_id', null)->where('type', 'IN')->get();
  39. $this->recordsOut = \App\Models\Causal::where('parent_id', null)->where('type', 'OUT')->get();
  40. return view('livewire.causal');
  41. }
  42. public function add()
  43. {
  44. $this->resetFields();
  45. $this->add = true;
  46. $this->update = false;
  47. }
  48. public function addLevel($parent_id)
  49. {
  50. $this->resetFields();
  51. $this->parent_id = $parent_id;
  52. $p = \App\Models\Causal::findOrFail($parent_id);
  53. $this->type = $p->type;
  54. $this->parent = $p->name;
  55. $this->add = true;
  56. $this->update = false;
  57. }
  58. public function store()
  59. {
  60. $this->validate();
  61. try {
  62. \App\Models\Causal::create([
  63. 'name' => $this->name,
  64. 'type' => $this->type,
  65. 'parent_id' => $this->parent_id,
  66. 'money' => $this->money,
  67. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  68. 'no_receipt' => $this->no_receipt,
  69. 'user_status' => $this->user_status,
  70. 'no_first' => $this->no_first,
  71. 'no_records' => $this->no_records,
  72. 'enabled' => $this->enabled
  73. ]);
  74. session()->flash('success','Causale creata');
  75. $this->resetFields();
  76. $this->add = false;
  77. } catch (\Exception $ex) {
  78. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  79. }
  80. }
  81. public function edit($id){
  82. try {
  83. $causal = \App\Models\Causal::findOrFail($id);
  84. if( !$causal) {
  85. session()->flash('error','Causale non trovata');
  86. } else {
  87. $this->name = $causal->name;
  88. $this->money = $causal->money;
  89. $this->no_receipt = $causal->no_receipt;
  90. $this->user_status = $causal->user_status;
  91. $this->no_first = $causal->no_first;
  92. $this->no_records = $causal->no_records;
  93. $this->enabled = $causal->enabled;
  94. $this->type = $causal->type;
  95. $this->parent_id = $causal->parent_id;
  96. $this->dataId = $causal->id;
  97. $this->update = true;
  98. $this->add = false;
  99. }
  100. } catch (\Exception $ex) {
  101. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  102. }
  103. }
  104. public function update()
  105. {
  106. $this->validate();
  107. try {
  108. \App\Models\Causal::whereId($this->dataId)->update([
  109. 'name' => $this->name,
  110. 'type' => $this->type,
  111. 'parent_id' => $this->parent_id,
  112. 'user_status' => $this->user_status,
  113. 'no_first' => $this->no_first,
  114. 'no_records' => $this->no_records,
  115. 'money' => $this->money,
  116. 'no_receipt' => $this->no_receipt,
  117. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  118. 'enabled' => $this->enabled
  119. ]);
  120. session()->flash('success','Tessera aggiornata');
  121. $this->resetFields();
  122. $this->update = false;
  123. } catch (\Exception $ex) {
  124. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  125. }
  126. }
  127. public function cancel()
  128. {
  129. $this->add = false;
  130. $this->update = false;
  131. $this->resetFields();
  132. }
  133. public function delete($id)
  134. {
  135. try{
  136. \App\Models\Causal::find($id)->delete();
  137. session()->flash('success',"Tessera eliminata");
  138. return redirect(request()->header('Referer'));
  139. }catch(\Exception $e){
  140. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  141. }
  142. }
  143. public function duplicate($id)
  144. {
  145. $old = \App\Models\Causal::find($id);
  146. $new = $old->replicate();
  147. $new->name = $new->name . ' - COPIA';
  148. $new->save();
  149. $this->duplicateRecursive($old, $new);
  150. }
  151. public function duplicateRecursive($old, $new)
  152. {
  153. foreach($old->childs as $c)
  154. {
  155. $old1 = \App\Models\Causal::find($c->id);
  156. $new1 = $old1->replicate();
  157. $new1->parent_id = $new->id;
  158. $new1->save();
  159. $this->duplicateRecursive($old1, $new1);
  160. }
  161. }
  162. public function reorder()
  163. {
  164. }
  165. }