Causal.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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->no_reports = false;
  107. $this->enabled = true;
  108. }
  109. public function render()
  110. {
  111. // Remove the duplicate queries - loadRecords() already handles this
  112. // and make sure it respects the showHidden state
  113. if (!isset($this->recordsIn) || !isset($this->recordsOut)) {
  114. $this->loadRecords();
  115. }
  116. return view('livewire.causal');
  117. }
  118. public function add()
  119. {
  120. $this->resetFields();
  121. $this->add = true;
  122. $this->update = false;
  123. }
  124. public function addLevel($parent_id)
  125. {
  126. $this->resetFields();
  127. $this->parent_id = $parent_id;
  128. $p = \App\Models\Causal::findOrFail($parent_id);
  129. $this->type = $p->type;
  130. $this->parent = $p->name;
  131. $this->add = true;
  132. $this->update = false;
  133. }
  134. public function store()
  135. {
  136. $this->validate();
  137. try {
  138. \App\Models\Causal::create([
  139. 'name' => $this->name,
  140. 'type' => $this->type,
  141. 'parent_id' => $this->parent_id,
  142. 'money' => $this->money,
  143. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  144. 'no_receipt' => $this->no_receipt,
  145. 'user_status' => $this->user_status,
  146. 'no_first' => $this->no_first,
  147. 'no_records' => $this->no_records,
  148. 'no_reports' => $this->no_reports,
  149. 'enabled' => $this->enabled
  150. ]);
  151. session()->flash('success', 'Causale creata');
  152. $this->resetFields();
  153. $this->add = false;
  154. $this->loadRecords();
  155. } catch (\Exception $ex) {
  156. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  157. }
  158. }
  159. public function edit($id)
  160. {
  161. try {
  162. $causal = \App\Models\Causal::findOrFail($id);
  163. if (!$causal) {
  164. session()->flash('error', 'Causale non trovata');
  165. } else {
  166. $this->name = $causal->name;
  167. $this->money = $causal->money;
  168. $this->no_receipt = $causal->no_receipt;
  169. $this->user_status = $causal->user_status;
  170. $this->no_first = $causal->no_first;
  171. $this->no_records = $causal->no_records;
  172. $this->no_reports = $causal->no_reports;
  173. $this->enabled = $causal->enabled;
  174. $this->corrispettivo_fiscale = $causal->corrispettivo_fiscale;
  175. $this->type = $causal->type;
  176. $this->parent_id = $causal->parent_id;
  177. $this->dataId = $causal->id;
  178. $this->update = true;
  179. $this->add = false;
  180. }
  181. } catch (\Exception $ex) {
  182. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  183. }
  184. }
  185. public function update()
  186. {
  187. $this->validate();
  188. try {
  189. \App\Models\Causal::whereId($this->dataId)->update([
  190. 'name' => $this->name,
  191. 'type' => $this->type,
  192. 'parent_id' => $this->parent_id,
  193. 'user_status' => $this->user_status,
  194. 'no_first' => $this->no_first,
  195. 'no_records' => $this->no_records,
  196. 'no_reports' => $this->no_reports,
  197. 'money' => $this->money,
  198. 'no_receipt' => $this->no_receipt,
  199. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  200. 'enabled' => $this->enabled
  201. ]);
  202. session()->flash('success', 'Causale aggiornata');
  203. $this->resetFields();
  204. $this->update = false;
  205. $this->loadRecords();
  206. } catch (\Exception $ex) {
  207. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  208. }
  209. }
  210. public function cancel()
  211. {
  212. $this->add = false;
  213. $this->update = false;
  214. $this->resetFields();
  215. }
  216. public function duplicate($id)
  217. {
  218. $old = \App\Models\Causal::find($id);
  219. $new = $old->replicate();
  220. $new->name = $new->name . ' - COPIA';
  221. $new->save();
  222. $this->duplicateRecursive($old, $new);
  223. $this->loadRecords(); // Refresh after duplicate
  224. }
  225. public function duplicateRecursive($old, $new)
  226. {
  227. foreach ($old->childs as $c) {
  228. $old1 = \App\Models\Causal::find($c->id);
  229. $new1 = $old1->replicate();
  230. $new1->parent_id = $new->id;
  231. $new1->save();
  232. $this->duplicateRecursive($old1, $new1);
  233. }
  234. }
  235. public function reorder()
  236. {
  237. $this->loadRecords(); // Refresh after reorder
  238. }
  239. }