Causal.php 5.5 KB

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