Causal.php 4.6 KB

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