CompanyRate.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Livewire;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\DB;
  5. use Livewire\Attributes\Validate;
  6. use Livewire\Component;
  7. use Livewire\Attributes\Layout;
  8. class CompanyRate extends Component
  9. {
  10. public $company;
  11. public $current_company_rate = null;
  12. public $company_rates = [];
  13. #[Validate('required')]
  14. public $amount = '';
  15. public $type = '';
  16. public $group = '';
  17. public $description = '';
  18. public $enabled = 1;
  19. public $is_edit = false;
  20. public function resetFields(){
  21. $this->amount = '';
  22. $this->type = '';
  23. $this->group = '';
  24. $this->description = '';
  25. $this->enabled = 1;
  26. }
  27. public function render()
  28. {
  29. $this->company_rates = $this->company->rates ?? [];
  30. return view('livewire.company_rate');
  31. }
  32. public function add()
  33. {
  34. $this->resetFields();
  35. $this->is_edit = true;
  36. $this->current_company_rate = null;
  37. }
  38. public function edit($id)
  39. {
  40. $this->resetFields();
  41. $this->is_edit = true;
  42. $this->current_company_rate = \App\Models\CompanyRate::findOrFail($id);
  43. $this->amount = $this->current_company_rate->amount;
  44. $this->type = $this->current_company_rate->type;
  45. $this->group = $this->current_company_rate->group;
  46. $this->description = $this->current_company_rate->description;
  47. $this->enabled = $this->current_company_rate->enabled == 1;
  48. }
  49. public function save()
  50. {
  51. $this->validate();
  52. try
  53. {
  54. if ($this->current_company_rate == null)
  55. {
  56. \App\Models\CompanyActivity::create([
  57. 'company_id' => $this->company->id,
  58. 'amount' => $this->amount,
  59. 'type' => $this->type,
  60. 'group' => $this->group,
  61. 'description' => $this->description,
  62. 'enabled' => $this->enabled
  63. ]);
  64. }
  65. else
  66. {
  67. $this->current_company_rate->update([
  68. 'amount' => $this->amount,
  69. 'type' => $this->type,
  70. 'group' => $this->group,
  71. 'description' => $this->description,
  72. 'enabled' => $this->enabled ? 1 : 0,
  73. ]);
  74. }
  75. session()->flash('success','Dati salvati con successo');
  76. $this->resetFields();
  77. $this->is_edit = false;
  78. } catch (\Exception $ex) {
  79. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  80. }
  81. }
  82. public function cancel()
  83. {
  84. $this->resetFields();
  85. $this->is_edit = false;
  86. $this->current_company_rate = null;
  87. }
  88. }