ResourceRate.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 ResourceRate extends Component
  9. {
  10. public $resource;
  11. public $current_resource_rate = null;
  12. public $resource_rates = [];
  13. public $rate_groups = [];
  14. public $rate_types = [];
  15. public $company_rates = [];
  16. // #[Validate('required')]
  17. public $rate_group_id = null;
  18. public $rate_type_id = null;
  19. public $company_rate_id = null;
  20. public $cost = 0;
  21. public $price = 0;
  22. public $enabled = 1;
  23. public $is_edit = false;
  24. public function resetFields(){
  25. $this->rate_group_id = null;
  26. $this->rate_type_id = null;
  27. $this->company_rate_id = null;
  28. $this->cost = 0;
  29. $this->price = 0;
  30. $this->enabled = 1;
  31. }
  32. public function mount()
  33. {
  34. $this->rate_groups = \App\Models\RateGroup::all();
  35. $this->rate_types = \App\Models\RateType::all();
  36. $this->company_rates = \App\Models\CompanyRate::all();
  37. }
  38. public function render()
  39. {
  40. $this->resource_rates = $this->resource->rates ?? [];
  41. return view('livewire.resource_rate');
  42. }
  43. public function add()
  44. {
  45. $this->resetFields();
  46. $this->is_edit = true;
  47. $this->current_resource_rate = null;
  48. }
  49. public function edit($id)
  50. {
  51. $this->resetFields();
  52. $this->is_edit = true;
  53. $this->current_resource_rate = \App\Models\ResourceRate::findOrFail($id);
  54. $this->rate_group_id = $this->current_resource_rate->rate_group_id;
  55. $this->rate_type_id = $this->current_resource_rate->rate_type_id;
  56. $this->company_rate_id = $this->current_resource_rate->company_rate_id;
  57. $this->cost = $this->current_resource_rate->cost;
  58. $this->price = $this->current_resource_rate->price;
  59. $this->enabled = $this->current_resource_equipment->enabled == 1;
  60. }
  61. public function save()
  62. {
  63. $this->validate();
  64. try
  65. {
  66. if ($this->current_resource_rate == null)
  67. {
  68. \App\Models\ResourceRate::create([
  69. 'resource_id' => $this->resource->id,
  70. 'rate_group_id' => $this->rate_group_id,
  71. 'rate_type_id' => $this->rate_type_id,
  72. 'company_rate_id' => $this->company_rate_id,
  73. 'cost' => $this->cost,
  74. 'price' => $this->price,
  75. 'enabled' => $this->enabled
  76. ]);
  77. }
  78. else
  79. {
  80. $this->current_resource_rate->update([
  81. 'rate_group_id' => $this->rate_group_id,
  82. 'rate_type_id' => $this->rate_type_id,
  83. 'company_rate_id' => $this->company_rate_id,
  84. 'cost' => $this->cost,
  85. 'price' => $this->price,
  86. 'enabled' => $this->enabled
  87. ]);
  88. }
  89. session()->flash('success','Dati salvati con successo');
  90. $this->resetFields();
  91. $this->is_edit = false;
  92. } catch (\Exception $ex) {
  93. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  94. }
  95. }
  96. public function cancel()
  97. {
  98. $this->resetFields();
  99. $this->is_edit = false;
  100. $this->current_resource_rate = null;
  101. }
  102. }