Causal.php 5.4 KB

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