Causal.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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, $no_reports, $type, $dataId, $update = false, $add = false;
  9. public $corrispettivo_causal_id = 0;
  10. public $parent = '';
  11. public $showHidden = false;
  12. protected $rules = [
  13. 'name' => 'required',
  14. 'type' => 'required'
  15. ];
  16. protected $messages = [
  17. 'name.required' => 'Il nome è obbligatorio'
  18. ];
  19. public function boot()
  20. {
  21. app(TenantMiddleware::class)->setupTenantConnection();
  22. }
  23. public function mount()
  24. {
  25. $this->loadRecords();
  26. }
  27. public function hide($id)
  28. {
  29. try {
  30. \App\Models\Causal::whereId($id)->update(['hidden' => true]);
  31. session()->flash('success', 'Causale nascosta');
  32. $this->loadRecords(); // Refresh the data
  33. } catch (\Exception $ex) {
  34. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  35. }
  36. }
  37. public function show($id)
  38. {
  39. try {
  40. \App\Models\Causal::whereId($id)->update(['hidden' => false]);
  41. session()->flash('success', 'Causale ripristinata');
  42. $this->loadRecords(); // Refresh the data
  43. } catch (\Exception $ex) {
  44. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  45. }
  46. }
  47. public function toggleHidden()
  48. {
  49. $this->showHidden = !$this->showHidden;
  50. $this->loadRecords();
  51. }
  52. public function loadRecords()
  53. {
  54. if ($this->showHidden) {
  55. // Show all records including hidden ones
  56. $this->recordsIn = \App\Models\Causal::where('type', 'IN')
  57. ->where('parent_id', null)
  58. ->with(['childs'])
  59. ->orderBy('name')
  60. ->get();
  61. $this->recordsOut = \App\Models\Causal::where('type', 'OUT')
  62. ->where('parent_id', null)
  63. ->with(['childs'])
  64. ->orderBy('name')
  65. ->get();
  66. } else {
  67. // Show only non-hidden records
  68. $this->recordsIn = \App\Models\Causal::where('type', 'IN')
  69. ->where('parent_id', null)
  70. ->where(function ($query) {
  71. $query->where('hidden', false)->orWhereNull('hidden');
  72. })
  73. ->with(['childs' => function ($query) {
  74. $query->where(function ($q) {
  75. $q->where('hidden', false)->orWhereNull('hidden');
  76. });
  77. }])
  78. ->orderBy('name')
  79. ->get();
  80. $this->recordsOut = \App\Models\Causal::where('type', 'OUT')
  81. ->where('parent_id', null)
  82. ->where(function ($query) {
  83. $query->where('hidden', false)->orWhereNull('hidden');
  84. })
  85. ->with(['childs' => function ($query) {
  86. $query->where(function ($q) {
  87. $q->where('hidden', false)->orWhereNull('hidden');
  88. });
  89. }])
  90. ->orderBy('name')
  91. ->get();
  92. }
  93. }
  94. public function resetFields()
  95. {
  96. $this->name = '';
  97. $this->parent_id = null;
  98. $this->parent = '';
  99. $this->type = null;
  100. $this->money = false;
  101. $this->corrispettivo_fiscale = false;
  102. $this->no_receipt = false;
  103. $this->user_status = false;
  104. $this->no_first = false;
  105. $this->no_records = false;
  106. $this->enabled = true;
  107. }
  108. public function render()
  109. {
  110. // Remove the duplicate queries - loadRecords() already handles this
  111. // and make sure it respects the showHidden state
  112. if (!isset($this->recordsIn) || !isset($this->recordsOut)) {
  113. $this->loadRecords();
  114. }
  115. return view('livewire.causal');
  116. }
  117. public function add()
  118. {
  119. $this->resetFields();
  120. $this->add = true;
  121. $this->update = false;
  122. }
  123. public function addLevel($parent_id)
  124. {
  125. $this->resetFields();
  126. $this->parent_id = $parent_id;
  127. $p = \App\Models\Causal::findOrFail($parent_id);
  128. $this->type = $p->type;
  129. $this->parent = $p->name;
  130. $this->add = true;
  131. $this->update = false;
  132. }
  133. public function store()
  134. {
  135. $this->validate();
  136. try {
  137. \App\Models\Causal::create([
  138. 'name' => $this->name,
  139. 'type' => $this->type,
  140. 'parent_id' => $this->parent_id,
  141. 'money' => $this->money,
  142. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  143. 'no_receipt' => $this->no_receipt,
  144. 'user_status' => $this->user_status,
  145. 'no_first' => $this->no_first,
  146. 'no_records' => $this->no_records,
  147. 'enabled' => $this->enabled
  148. ]);
  149. session()->flash('success', 'Causale creata');
  150. $this->resetFields();
  151. $this->add = false;
  152. $this->loadRecords();
  153. } catch (\Exception $ex) {
  154. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  155. }
  156. }
  157. public function edit($id)
  158. {
  159. try {
  160. $causal = \App\Models\Causal::findOrFail($id);
  161. if (!$causal) {
  162. session()->flash('error', 'Causale non trovata');
  163. } else {
  164. $this->name = $causal->name;
  165. $this->money = $causal->money;
  166. $this->no_receipt = $causal->no_receipt;
  167. $this->user_status = $causal->user_status;
  168. $this->no_first = $causal->no_first;
  169. $this->no_records = $causal->no_records;
  170. $this->enabled = $causal->enabled;
  171. $this->corrispettivo_fiscale = $causal->corrispettivo_fiscale;
  172. $this->type = $causal->type;
  173. $this->parent_id = $causal->parent_id;
  174. $this->dataId = $causal->id;
  175. $this->update = true;
  176. $this->add = false;
  177. }
  178. } catch (\Exception $ex) {
  179. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  180. }
  181. }
  182. public function update()
  183. {
  184. $this->validate();
  185. try {
  186. \App\Models\Causal::whereId($this->dataId)->update([
  187. 'name' => $this->name,
  188. 'type' => $this->type,
  189. 'parent_id' => $this->parent_id,
  190. 'user_status' => $this->user_status,
  191. 'no_first' => $this->no_first,
  192. 'no_records' => $this->no_records,
  193. 'money' => $this->money,
  194. 'no_receipt' => $this->no_receipt,
  195. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  196. 'enabled' => $this->enabled
  197. ]);
  198. session()->flash('success', 'Causale aggiornata');
  199. $this->resetFields();
  200. $this->update = false;
  201. $this->loadRecords();
  202. } catch (\Exception $ex) {
  203. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  204. }
  205. }
  206. public function cancel()
  207. {
  208. $this->add = false;
  209. $this->update = false;
  210. $this->resetFields();
  211. }
  212. public function duplicate($id)
  213. {
  214. $old = \App\Models\Causal::find($id);
  215. $new = $old->replicate();
  216. $new->name = $new->name . ' - COPIA';
  217. $new->save();
  218. $this->duplicateRecursive($old, $new);
  219. $this->loadRecords(); // Refresh after duplicate
  220. }
  221. public function duplicateRecursive($old, $new)
  222. {
  223. foreach ($old->childs as $c) {
  224. $old1 = \App\Models\Causal::find($c->id);
  225. $new1 = $old1->replicate();
  226. $new1->parent_id = $new->id;
  227. $new1->save();
  228. $this->duplicateRecursive($old1, $new1);
  229. }
  230. }
  231. public function reorder()
  232. {
  233. $this->loadRecords(); // Refresh after reorder
  234. }
  235. }