'required', 'payment_method_id' => 'required', 'causal_id' => 'required', 'amount' => 'required|numeric|gt:0' ]; protected $messages = [ 'supplier_id.required' => 'Il fornitore è obbligatorio', 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio', 'causal_id.required' => 'La causale è obbligatoria', 'amount.required' => 'L\'importo è obbligatorio', ]; public function getSupplierProperty() { $ret = null; if ($this->supplier_id > 0) { $ret = \App\Models\Supplier::findOrFail($this->supplier_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 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 = date("n"); $this->year = date("Y"); $this->type = 'OUT'; $this->amount = null; $this->commercial = 0; } 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 updated() { $this->emit('load-select'); } 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', 'OUT')->orderBy('name')->get(), 0); $this->suppliers = \App\Models\Supplier::select('name','id')->orderBy('name')->get(); $this->payments = \App\Models\PaymentMethod::select('id', 'name')->orderBy('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', 'OUT')->whereBetween('date', [$fromDate, $toDate])->with('supplier', 'causal', 'payment_method')->orderBy('id', 'DESC')->get(); return view('livewire.records_out'); } public function executeMultipleAction(){ if ($this->multipleAction == 'delete') $this->multipleDelete(); } public function add() { $this->emit('load-select'); $this->resetFields(); $this->add = true; $this->update = false; } public function store() { $this->emit('refresh'); $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 (' . $ex->getMessage() . ')'); } } public function edit($id){ $this->emit('load-select'); 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 (' . $ex->getMessage() . ')'); } } public function update() { $this->emit('refresh'); $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' => $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('error','Errore (' . $ex->getMessage() . ')'); } } 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 (' . $ex->getMessage() . ')'); } } public function multipleDelete() { try{ foreach($this->multipleIds as $id) { \App\Models\Record::find($id)->delete(); } }catch(\Exception $e){ session()->flash('error','Errore (' . $ex->getMessage() . ')'); } $this->multipleAction = ''; } }