Causal.php 5.3 KB

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