RecordIN.php 6.4 KB

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