| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class PaymentMethod extends Component
- {
- public $records, $name, $enabled, $money, $corrispettivo_fiscale, $dataId, $bank_id, $update = false, $add = false;
- public $banks = array();
- protected $rules = [
- 'name' => 'required'
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio'
- ];
- public $sortField ='name';
- public $sortAsc = true;
- public function sortBy($field)
- {
- if($this->sortField === $field)
- {
- $this->sortAsc = ! $this->sortAsc;
- } else {
- $this->sortAsc = true;
- }
- $this->sortField = $field;
- }
- public function resetFields(){
- $this->name = '';
- $this->money = false;
- $this->corrispettivo_fiscale = false;
- $this->enabled = true;
- $this->emit('load-data-table');
- }
- public function mount()
- {
- $this->banks = \App\Models\Bank::select('id', 'name')->get();
- }
- public function render()
- {
- $this->records = \App\Models\PaymentMethod::all();
- foreach($this->records as $r)
- {
- $r->bank = $r->bank ? $r->bank->name : '';
- }
- /*if ($this->sortAsc)
- $this->records = $this->records->sortBy($this->sortField);
- else
- $this->records = $this->records->sortByDesc($this->sortField);*/
- return view('livewire.payment_method');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- public function store()
- {
- $this->validate();
- try {
- \App\Models\PaymentMethod::create([
- 'name' => $this->name,
- 'bank_id' => $this->bank_id,
- 'money' => $this->money,
- 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Metodo pagamento creato');
- $this->resetFields();
- $this->add = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function edit($id){
- try {
- $payment_method = \App\Models\PaymentMethod::findOrFail($id);
- if( !$payment_method) {
- session()->flash('error','Metodo pagamento non trovato');
- } else {
- $this->name = $payment_method->name;
- $this->enabled = $payment_method->enabled;
- $this->corrispettivo_fiscale = $payment_method->corrispettivo_fiscale;
- $this->money = $payment_method->money;
- $this->bank_id = $payment_method->bank_id;
- $this->dataId = $payment_method->id;
- $this->update = true;
- $this->add = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function update()
- {
- $this->validate();
- try {
- \App\Models\PaymentMethod::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'bank_id' => $this->bank_id,
- 'money' => $this->money,
- 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Metodo pagamento 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\PaymentMethod::find($id)->delete();
- session()->flash('success',"Metodo pagamento eliminato");
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- }
|