RecordOUT.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class RecordOUT extends Component
  5. {
  6. public $records, $dataId, $member_id, $supplier_id,
  7. $causal_id,
  8. $payment_method_id,
  9. $date,
  10. $month,
  11. $year,
  12. $type,
  13. $amount,
  14. $commercial, $update = false, $add = false;
  15. public $selectedFilter = 0;
  16. public $multipleIds = [];
  17. public $multipleAction = '';
  18. public $selectId = 0;
  19. public $causals = array();
  20. public $payments = array();
  21. public $suppliers = array();
  22. protected $rules = [
  23. 'supplier_id' => 'required',
  24. 'payment_method_id' => 'required',
  25. 'causal_id' => 'required',
  26. 'amount' => 'required|numeric|gt:0'
  27. ];
  28. protected $messages = [
  29. 'supplier_id.required' => 'Il fornitore è obbligatorio',
  30. 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio',
  31. 'causal_id.required' => 'La causale è obbligatoria',
  32. 'amount.required' => 'L\'importo è obbligatorio',
  33. ];
  34. public function getSupplierProperty()
  35. {
  36. $ret = null;
  37. if ($this->supplier_id > 0)
  38. {
  39. $ret = \App\Models\Supplier::findOrFail($this->supplier_id);
  40. }
  41. return $ret;
  42. }
  43. public function getCausalProperty()
  44. {
  45. $ret = null;
  46. if ($this->causal_id > 0)
  47. {
  48. $ret = \App\Models\Causal::findOrFail($this->causal_id);
  49. }
  50. return $ret;
  51. }
  52. public function resetFields(){
  53. $this->member_id = null;
  54. $this->supplier_id = null;
  55. $this->causal_id = null;
  56. $this->payment_method_id = null;
  57. $this->date = date("Y-m-d");
  58. $this->month = date("n");
  59. $this->year = date("Y");
  60. $this->type = 'OUT';
  61. $this->amount = null;
  62. $this->commercial = 0;
  63. }
  64. public function getCausale($records, $indentation)
  65. {
  66. foreach($records as $record)
  67. {
  68. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree());
  69. if(count($record->childs))
  70. $this->getCausale($record->childs, $indentation + 1);
  71. }
  72. }
  73. public function updated()
  74. {
  75. $this->emit('load-select');
  76. }
  77. public function mount()
  78. {
  79. if (isset($_GET["new"]))
  80. $this->add();
  81. $this->causals = array();
  82. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->orderBy('name')->get(), 0);
  83. $this->suppliers = \App\Models\Supplier::select('name','id')->orderBy('name')->get();
  84. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->orderBy('name')->get();
  85. }
  86. public function render()
  87. {
  88. $fromDate = date("Y-m-d");
  89. $toDate = date("Y-m-d");
  90. if ($this->selectedFilter == 1)
  91. {
  92. $fromDate = date("Y-m-01");
  93. $toDate = date("Y-m-t");
  94. }
  95. if ($this->selectedFilter == 2)
  96. {
  97. $fromDate = date("Y-01-01");
  98. $toDate = date("Y-12-31");
  99. }
  100. if ($this->selectedFilter == 3)
  101. {
  102. $fromDate = date("2000-01-01");
  103. $toDate = date("Y-12-31");
  104. }
  105. $this->records = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [$fromDate, $toDate])->with('supplier', 'causal', 'payment_method')->orderBy('id', 'DESC')->get();
  106. return view('livewire.records_out');
  107. }
  108. public function executeMultipleAction(){
  109. if ($this->multipleAction == 'delete')
  110. $this->multipleDelete();
  111. }
  112. public function add()
  113. {
  114. $this->emit('load-select');
  115. $this->resetFields();
  116. $this->add = true;
  117. $this->update = false;
  118. }
  119. public function store()
  120. {
  121. $this->emit('refresh');
  122. $this->validate();
  123. try {
  124. \App\Models\Record::create([
  125. 'member_id' => $this->member_id,
  126. 'supplier_id' => $this->supplier_id,
  127. 'causal_id' => $this->causal_id,
  128. 'payment_method_id' => $this->payment_method_id,
  129. 'date' => $this->date,
  130. 'month' => $this->month,
  131. 'year' => $this->year,
  132. 'type' => $this->type,
  133. 'amount' => $this->amount,
  134. 'commercial' => $this->commercial,
  135. ]);
  136. session()->flash('success','Movimento creato');
  137. $this->resetFields();
  138. $this->add = false;
  139. } catch (\Exception $ex) {
  140. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  141. }
  142. }
  143. public function edit($id){
  144. $this->emit('load-select');
  145. try {
  146. $record = \App\Models\Record::findOrFail($id);
  147. if( !$record) {
  148. session()->flash('error','Movimento non trovato');
  149. } else {
  150. $this->member_id = $record->member_id;
  151. $this->supplier_id = $record->supplier_id;
  152. $this->causal_id = $record->causal_id;
  153. $this->payment_method_id = $record->payment_method_id;
  154. $this->date = date("Y-m-d", strtotime($record->date));
  155. $this->month = $record->month;
  156. $this->year = $record->year;
  157. $this->type = $record->type;
  158. $this->amount = $record->amount;
  159. $this->commercial = $record->commercial;
  160. $this->dataId = $record->id;
  161. $this->update = true;
  162. $this->add = false;
  163. }
  164. } catch (\Exception $ex) {
  165. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  166. }
  167. }
  168. public function update()
  169. {
  170. $this->emit('refresh');
  171. $this->validate();
  172. try {
  173. \App\Models\Record::whereId($this->dataId)->update([
  174. 'member_id' => $this->member_id,
  175. 'supplier_id' => $this->supplier_id,
  176. 'causal_id' => $this->causal_id,
  177. 'payment_method_id' => $this->payment_method_id,
  178. 'date' => $this->date,
  179. 'month' => $this->month,
  180. 'year' => $this->year,
  181. 'type' => $this->type,
  182. 'amount' => $this->amount,
  183. 'commercial' => $this->commercial,
  184. ]);
  185. session()->flash('success','Movimento aggiornato');
  186. $this->resetFields();
  187. $this->update = false;
  188. } catch (\Exception $ex) {
  189. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  190. }
  191. }
  192. public function cancel()
  193. {
  194. $this->add = false;
  195. $this->update = false;
  196. $this->resetFields();
  197. }
  198. public function delete($id)
  199. {
  200. try{
  201. \App\Models\Record::find($id)->delete();
  202. session()->flash('success',"Movimento eliminato");
  203. }catch(\Exception $e){
  204. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  205. }
  206. }
  207. public function multipleDelete()
  208. {
  209. try{
  210. foreach($this->multipleIds as $id)
  211. {
  212. \App\Models\Record::find($id)->delete();
  213. }
  214. }catch(\Exception $e){
  215. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  216. }
  217. $this->multipleAction = '';
  218. }
  219. }