'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() { $this->emit('refresh'); if ($this->member_id > 0) { $member = \App\Models\Member::findOrFail($this->member_id); $this->virtual = $member->getMoney(); $this->newMemberFirstName = ''; $this->newMemberLastName = ''; $this->newMemberFiscalCode = ''; $this->newMemberFiscalCodeExist = false; } } public function updatedCausalId() { //$this->emit('refresh'); } public function updatedDate() { //$this->emit('refresh'); } public function hydrate() { $this->emit('load-select'); } /*public function updated() { $this->emit('refresh'); }*/ public function updatedPaymentMethodId() { //$this->emit('refresh'); $this->canSave = $this->checkCanSave(); } public function updatedAmount() { // $this->emit('refresh'); $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 = date("n"); $this->year = date("Y"); $this->type = 'IN'; $this->amount = null; $this->commercial = 0; $this->newMemberFirstName = ''; $this->newMemberLastName = ''; $this->newMemberFiscalCode = ''; $this->newMemberFiscalCodeExist = false; } 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() { $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(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get(); $this->payments = \App\Models\PaymentMethod::select('id', 'name')->orderBy('name')->get(); if ($this->first) { if (isset($_GET["new"])) { $this->refreshAfter = 1; $this->add(); } if (isset($_GET["memberId"])) $this->member_id = $_GET["memberId"]; if (isset($_GET["causalId"])) $this->causal_id = $_GET["causalId"]; } $this->first = false; } 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('id', 'DESC')->get(); return view('livewire.records_in'); } 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($generate) { $this->emit('refresh'); $this->validate(); try { $record = \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, ]); $this->dataId = $record->id; if ($generate) $this->createReceipt(); session()->flash('success','Movimento creato'); $this->resetFields(); $this->add = false; } catch (\Exception $ex) { session()->flash('error','Errore (' . $ex->getMessage() . ')'); } } public function duplicate($id){ $record = \App\Models\Record::findOrFail($id); $newRecord = $record->replicate(); $newRecord->save(); $this->edit($newRecord->id); } 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($generate) { $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' => date("Y-m-d", strtotime($this->date)), 'month' => $this->month, 'year' => $this->year, 'type' => $this->type, 'amount' => $this->amount, 'commercial' => $this->commercial, ]); if ($generate) $this->createReceipt(); 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 = ''; } public function createMember() { $this->newMemberFiscalCodeExist = false; $this->validate([ // 'newMemberFiscalCode'=>'required|max:16', 'newMemberFirstName'=>'required', 'newMemberLastName'=>'required' ]); // Check fiscal code exist $exist = false; if ($this->newMemberFiscalCode != '') { $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get(); $exist = $check->count() > 0; } if (!$exist) { $member = \App\Models\Member::create([ 'first_name' => $this->newMemberFirstName, 'last_name' => $this->newMemberLastName, 'fiscal_code' => $this->newMemberFiscalCode, 'status' => true ]); $this->member_id = $member->id; $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->get(); // $this->emit('reloadMembers'); $this->emit('refresh'); $this->newMemberFirstName = ''; $this->newMemberLastName = ''; $this->newMemberFiscalCode = ''; $this->emit('saved'); //$this->validate(); $this->selectId++; } else { $this->newMemberFiscalCodeExist = true; } } public function createReceipt() { /* $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->first(); if ($receipt != null) { $receipt->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, ]); } else { $number = 1; $exist = \App\Models\Receipt::where('year', $this->year)->orderBy('number', 'DESC')->first(); if ($exist != null) $number = $exist->number + 1; $receipt = \App\Models\Receipt::create([ 'record_id' => $this->dataId, 'member_id' => $this->member_id, 'supplier_id' => $this->supplier_id, 'causal_id' => $this->causal_id, 'payment_method_id' => $this->payment_method_id, 'number' => $number, 'date' => $this->date, 'month' => $this->month, 'year' => $this->year, 'type' => $this->type, 'amount' => $this->amount, 'commercial' => $this->commercial, ]); } $data = [ 'member' => $receipt->id ]; $pdf = \PDF::loadView('partials.pdf_generate_connections', $data)->setPaper('a4', 'landscape')->output(); // return response()->streamDownload( fn() => print($pdf), 'export_protocol.pdf' ); */ } }