| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class RecordIN extends Component
- {
- public $records, $dataId, $member_id, $supplier_id,
- $causal_id,
- $payment_method_id,
- $date,
- $month,
- $year,
- $type,
- $amount,
- $virtual,
- $commercial, $update = false, $add = false;
- public $selectedFilter = 0;
- public $multipleIds = [];
- public $multipleAction = '';
- public $first = true;
- public $selectId = 0;
- public $refreshAfter = 0;
- public $canSave = true;
- public $newMemberFirstName = '';
- public $newMemberLastName = '';
- public $newMemberFiscalCode = '';
- public $newMemberFiscalCodeExist = false;
- public $causals = array();
- public $payments = array();
- public $members = array();
- protected $rules = [
- 'member_id' => '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'
- );
- */
- }
- }
|