| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?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 ResourceContract extends Component
- {
- public $resource;
- public $current_resource_contract = null;
- public $resource_contracts = [];
- public $contract_types = [];
- // #[Validate('required')]
- public $contract_type_id = null;
- public $contract_date = null;
- public $contract_number = '';
- public $position = '';
- public $role = '';
- public $start_date = null;
- public $end_date = null;
- public $notes = '';
- public $enabled = 1;
- public $is_edit = false;
- public function resetFields(){
- $this->contract_type_id = null;
- $this->contract_date = null;
- $this->contract_number = '';
- $this->position = '';
- $this->role = '';
- $this->start_date = null;
- $this->end_date = null;
- $this->notes = '';
- $this->enabled = 1;
- }
- public function mount()
- {
- $this->contract_types = \App\Models\ContractType::all();
- }
- public function render()
- {
- $this->resource_contracts = $this->resource->contracts ?? [];
- return view('livewire.resource_contract');
- }
- public function add()
- {
- $this->resetFields();
- $this->is_edit = true;
- $this->current_resource_contract = null;
- }
- public function edit($id)
- {
- $this->resetFields();
- $this->is_edit = true;
- $this->current_resource_contract = \App\Models\ResourceContract::findOrFail($id);
- $this->contract_type_id = $this->current_resource_contract->contract_type_id;
- $this->contract_date = $this->current_resource_contract->contract_date;
- $this->contract_number = $this->current_resource_contract->contract_number;
- $this->position = $this->current_resource_contract->position;
- $this->role = $this->current_resource_contract->role;
- $this->start_date = $this->current_resource_contract->start_date;
- $this->end_date = $this->current_resource_contract->end_date;
- $this->notes = $this->current_resource_contract->notes;
- $this->enabled = $this->current_resource_contract->enabled == 1;
- }
- public function save()
- {
- $this->validate();
- try
- {
- if ($this->current_resource_contract == null)
- {
- \App\Models\ResourceContract::create([
- 'resource_id' => $this->resource->id,
- 'contract_type_id' => $this->contract_type_id,
- 'contract_date' => $this->contract_date,
- 'contract_number' => $this->contract_number,
- 'position' => $this->position,
- 'role' => $this->role,
- 'start_date' => $this->start_date,
- 'end_date' => $this->end_date,
- 'notes' => $this->notes,
- 'enabled' => $this->enabled
- ]);
- }
- else
- {
- $this->current_resource_contract->update([
- 'contract_type_id' => $this->contract_type_id,
- 'contract_date' => $this->contract_date,
- 'contract_number' => $this->contract_number,
- 'position' => $this->position,
- 'role' => $this->role,
- 'start_date' => $this->start_date,
- 'end_date' => $this->end_date,
- 'notes' => $this->notes,
- 'enabled' => $this->enabled ? 1 : 0,
- ]);
- }
- 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_contract = null;
- }
- }
|