| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\Livewire;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\DB;
- use Livewire\Attributes\Validate;
- use Livewire\Component;
- use Livewire\Attributes\Layout;
- class ResourceRate extends Component
- {
- public $resource;
- public $current_resource_rate = null;
- public $resource_rates = [];
- public $rate_groups = [];
- public $rate_types = [];
- public $company_rates = [];
- // #[Validate('required')]
- public $rate_group_id = null;
- public $rate_type_id = null;
- public $company_rate_id = null;
- public $cost = 0;
- public $price = 0;
- public $enabled = 1;
- public $is_edit = false;
- public function resetFields(){
- $this->rate_group_id = null;
- $this->rate_type_id = null;
- $this->company_rate_id = null;
- $this->cost = 0;
- $this->price = 0;
- $this->enabled = 1;
- }
- public function mount()
- {
- $this->rate_groups = \App\Models\RateGroup::all();
- $this->rate_types = \App\Models\RateType::all();
- $this->company_rates = \App\Models\CompanyRate::all();
- }
- public function render()
- {
- $this->resource_rates = $this->resource->rates ?? [];
- return view('livewire.resource_rate');
- }
- public function add()
- {
- $this->resetFields();
- $this->is_edit = true;
- $this->current_resource_rate = null;
- }
- public function edit($id)
- {
- $this->resetFields();
- $this->is_edit = true;
- $this->current_resource_rate = \App\Models\ResourceRate::findOrFail($id);
- $this->rate_group_id = $this->current_resource_rate->rate_group_id;
- $this->rate_type_id = $this->current_resource_rate->rate_type_id;
- $this->company_rate_id = $this->current_resource_rate->company_rate_id;
- $this->cost = $this->current_resource_rate->cost;
- $this->price = $this->current_resource_rate->price;
- $this->enabled = $this->current_resource_equipment->enabled == 1;
- }
- public function save()
- {
- $this->validate();
- try
- {
- if ($this->current_resource_rate == null)
- {
- \App\Models\ResourceRate::create([
- 'resource_id' => $this->resource->id,
- 'rate_group_id' => $this->rate_group_id,
- 'rate_type_id' => $this->rate_type_id,
- 'company_rate_id' => $this->company_rate_id,
- 'cost' => $this->cost,
- 'price' => $this->price,
- 'enabled' => $this->enabled
- ]);
- }
- else
- {
- $this->current_resource_rate->update([
- 'rate_group_id' => $this->rate_group_id,
- 'rate_type_id' => $this->rate_type_id,
- 'company_rate_id' => $this->company_rate_id,
- 'cost' => $this->cost,
- 'price' => $this->price,
- 'enabled' => $this->enabled
- ]);
- }
- session()->flash('success','Dati salvati con successo');
- $this->resetFields();
- $this->is_edit = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function cancel()
- {
- $this->resetFields();
- $this->is_edit = false;
- $this->current_resource_rate = null;
- }
- }
|