'required', 'payment_method_id' => 'required', 'causal_id' => 'required', 'amount' => 'required|numeric|gt:0' ]; protected $messages = [ 'member_id.required' => 'La persona è obbligatorio', 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio', 'causal_id.required' => 'La causale è obbligatoria', 'amount.required' => 'L\'importo è obbligatorio', ]; public function updatedMemberId() { $member = \App\Models\Member::findOrFail($this->member_id); $this->virtual = $member->getMoney(); } public function updatedPaymentMethodId() { $this->canSave = $this->checkCanSave(); } public function updatedAmount() { $this->canSave = $this->checkCanSave(); } public function checkCanSave() { $ret = true; if ($this->payment_method_id != null) { $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id); if ($payment_method->money) { $ret = $this->virtual >= $this->amount; } } return $ret; } public function resetFields(){ $this->member_id = null; $this->supplier_id = null; $this->causal_id = null; $this->payment_method_id = null; $this->date = date("Y-m-d"); $this->month = null; $this->year = null; $this->type = 'IN'; $this->amount = null; $this->commercial = 0; } public function getMemberProperty() { $ret = null; if ($this->member_id > 0) { $ret = \App\Models\Member::findOrFail($this->member_id); } return $ret; } public function getCausalProperty() { $ret = null; if ($this->causal_id > 0) { $ret = \App\Models\Causal::findOrFail($this->causal_id); } return $ret; } public function getCausale($records, $indentation) { foreach($records as $record) { $this->causals[] = array('id' => $record->id, 'name' => $record->getTree()); if(count($record->childs)) $this->getCausale($record->childs, $indentation + 1); } } public function mount() { if (isset($_GET["new"])) $this->add(); $this->causals = array(); $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0); $this->members = \App\Models\Member::select(\DB::raw("CONCAT('first_name', ' ', 'last_name') AS name"),'id')->get(); $this->payments = \App\Models\PaymentMethod::select('id', 'name')->get(); } public function render() { $fromDate = date("Y-m-d"); $toDate = date("Y-m-d"); if ($this->selectedFilter == 1) { $fromDate = date("Y-m-01"); $toDate = date("Y-m-t"); } if ($this->selectedFilter == 2) { $fromDate = date("Y-01-01"); $toDate = date("Y-12-31"); } if ($this->selectedFilter == 3) { $fromDate = date("2000-01-01"); $toDate = date("Y-12-31"); } $this->records = \App\Models\Record::where('type', 'IN')->whereBetween('date', [$fromDate, $toDate])->with('member', 'causal', 'payment_method')->orderBy('date', 'DESC')->get(); return view('livewire.records_in'); } public function add() { $this->resetFields(); $this->add = true; $this->update = false; } public function store() { $this->validate(); try { \App\Models\Record::create([ 'member_id' => $this->member_id, 'supplier_id' => $this->supplier_id, 'causal_id' => $this->causal_id, 'payment_method_id' => $this->payment_method_id, 'date' => $this->date, 'month' => $this->month, 'year' => $this->year, 'type' => $this->type, 'amount' => $this->amount, 'commercial' => $this->commercial, ]); session()->flash('success','Movimento creato'); $this->resetFields(); $this->add = false; } catch (\Exception $ex) { session()->flash('error','Errore in fase di salvataggio'); } } public function edit($id){ try { $record = \App\Models\Record::findOrFail($id); if( !$record) { session()->flash('error','Movimento non trovato'); } else { $this->member_id = $record->member_id; $this->supplier_id = $record->supplier_id; $this->causal_id = $record->causal_id; $this->payment_method_id = $record->payment_method_id; $this->date = date("Y-m-d", strtotime($record->date)); $this->month = $record->month; $this->year = $record->year; $this->type = $record->type; $this->amount = $record->amount; $this->commercial = $record->commercial; $this->dataId = $record->id; $this->update = true; $this->add = false; } } catch (\Exception $ex) { session()->flash('error','Errore'); } } public function update() { $this->validate(); try { \App\Models\Record::whereId($this->dataId)->update([ 'member_id' => $this->member_id, 'supplier_id' => $this->supplier_id, 'causal_id' => $this->causal_id, 'payment_method_id' => $this->payment_method_id, 'date' => date("Y-m-d", strtotime($this->date)), 'month' => $this->month, 'year' => $this->year, 'type' => $this->type, 'amount' => $this->amount, 'commercial' => $this->commercial, ]); session()->flash('success','Movimento aggiornato'); $this->resetFields(); $this->update = false; } catch (\Exception $ex) { session()->flash('success','Errore'); } } public function cancel() { $this->add = false; $this->update = false; $this->resetFields(); } public function delete($id) { try{ \App\Models\Record::find($id)->delete(); session()->flash('success',"Movimento eliminato"); }catch(\Exception $e){ session()->flash('error',"Errore"); } } }