PaymentMethod.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class PaymentMethod extends Component
  5. {
  6. public $records, $name, $enabled, $dataId, $bank_id, $update = false, $add = false;
  7. public $banks = array();
  8. protected $rules = [
  9. 'name' => 'required'
  10. ];
  11. protected $messages = [
  12. 'name.required' => 'Il nome è obbligatorio'
  13. ];
  14. public function resetFields(){
  15. $this->name = '';
  16. $this->enabled = true;
  17. }
  18. public function mount()
  19. {
  20. $this->banks = \App\Models\Bank::select('id', 'name')->get();
  21. }
  22. public function render()
  23. {
  24. $this->records = \App\Models\PaymentMethod::with('bank')->get();
  25. return view('livewire.payment_method');
  26. }
  27. public function add()
  28. {
  29. $this->resetFields();
  30. $this->add = true;
  31. $this->update = false;
  32. }
  33. public function store()
  34. {
  35. $this->validate();
  36. try {
  37. \App\Models\PaymentMethod::create([
  38. 'name' => $this->name,
  39. 'bank_id' => $this->bank_id,
  40. 'enabled' => $this->enabled
  41. ]);
  42. session()->flash('success','Metodo pagamento creato');
  43. $this->resetFields();
  44. $this->add = false;
  45. } catch (\Exception $ex) {
  46. session()->flash('error','Errore in fase di salvataggio');
  47. }
  48. }
  49. public function edit($id){
  50. try {
  51. $payment_method = \App\Models\PaymentMethod::findOrFail($id);
  52. if( !$payment_method) {
  53. session()->flash('error','Metodo pagamento non trovato');
  54. } else {
  55. $this->name = $payment_method->name;
  56. $this->enabled = $payment_method->enabled;
  57. $this->bank_id = $payment_method->bank_id;
  58. $this->dataId = $payment_method->id;
  59. $this->update = true;
  60. $this->add = false;
  61. }
  62. } catch (\Exception $ex) {
  63. session()->flash('error','Errore');
  64. }
  65. }
  66. public function update()
  67. {
  68. $this->validate();
  69. try {
  70. \App\Models\PaymentMethod::whereId($this->dataId)->update([
  71. 'name' => $this->name,
  72. 'bank_id' => $this->bank_id,
  73. 'enabled' => $this->enabled
  74. ]);
  75. session()->flash('success','Metodo pagamento aggiornato');
  76. $this->resetFields();
  77. $this->update = false;
  78. } catch (\Exception $ex) {
  79. session()->flash('success','Errore');
  80. }
  81. }
  82. public function cancel()
  83. {
  84. $this->add = false;
  85. $this->update = false;
  86. $this->resetFields();
  87. }
  88. public function delete($id)
  89. {
  90. try{
  91. \App\Models\PaymentMethod::find($id)->delete();
  92. session()->flash('success',"Metodo pagamento eliminato");
  93. }catch(\Exception $e){
  94. session()->flash('error',"Errore");
  95. }
  96. }
  97. }