Causal.php 5.8 KB

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