| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class RecordOUT extends Component
- {
- public $records, $dataId, $member_id, $supplier_id,
- $causal_id,
- $payment_method_id,
- $date,
- $month,
- $year,
- $type,
- $amount,
- $commercial, $update = false, $add = false;
- public $selectedFilter = 0;
- public $multipleIds = [];
- public $multipleAction = '';
- public $selectId = 0;
- public $causals = array();
- public $payments = array();
- public $suppliers = array();
- protected $rules = [
- 'supplier_id' => '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 = '';
- }
- }
|