RecordIN.php 7.1 KB

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