PaymentMethod.php 3.2 KB

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