| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace App\Livewire;
- use Livewire\Component;
- use Livewire\Attributes\Validate;
- class Project extends Component
- {
- public $projects;
- #[Validate('required')]
- public $name = '';
- public $code = '';
- public $customer_id = null;
- public $company_service_id = null;
- public $description = '';
- public $start_date = null;
- public $end_date = null;
- public $project_manager_id = null;
- public $project_value = 0;
- public $hours = 0;
- public $status = 0;
- public $billable = 0;
- public $enabled;
- public $current_project = null;
- public $is_edit = false;
- public function resetFields(){
- $this->name = '';
- $this->code = '';
- $this->customer_id = null;
- $this->company_service_id = null;
- $this->description = '';
- $this->start_date = null;
- $this->end_date = null;
- $this->project_manager_id = null;
- $this->project_value = 0;
- $this->hours = 0;
- $this->status = 0;
- $this->billable = 0;
- $this->enabled = true;
- }
- public function render()
- {
- // Get Customers
- $this->projects = \App\Models\Project::all();
- return view('livewire.project');
- }
- public function add()
- {
- $this->resetFields();
- $this->is_edit = true;
- $this->current_project = null;
- }
- public function edit($id)
- {
- $this->resetFields();
- $this->is_edit = true;
- $this->current_project = \App\Models\Project::findOrFail($id);
- $this->name = $this->current_project->name;
- $this->code = $this->current_project->code;
- $this->customer_id = $this->current_project->customer_id;
- $this->company_service_id = $this->current_project->company_service_id;
- $this->description = $this->current_project->description;
- $this->start_date = $this->current_project->start_date;
- $this->end_date = $this->current_project->end_date;
- $this->project_manager_id = $this->current_project->project_manager_id;
- $this->project_value = $this->current_project->project_value;
- $this->hours = $this->current_project->hours;
- $this->status = $this->current_project->status;
- $this->billable = $this->current_project->billable;
- $this->enabled = $this->current_customer->enabled;
- }
- public function save()
- {
- $this->validate();
- try
- {
- if ($this->current_project == null)
- {
- \App\Models\Project::create([
- 'name' => $this->name,
- 'code' => $this->code,
- 'customer_id' => $this->customer_id,
- 'company_service_id' => $this->company_service_id,
- 'description' => $this->description,
- 'start_date' => $this->start_date,
- 'end_date' => $this->end_date,
- 'project_manager_id' => $this->project_manager_id,
- 'project_value' => $this->project_value,
- 'hours' => $this->hours,
- 'status' => $this->status,
- 'billable' => $this->billable,
- 'enabled' => $this->enabled
- ]);
- }
- else
- {
- $this->current_project->update([
- 'name' => $this->name,
- 'code' => $this->code,
- 'customer_id' => $this->customer_id,
- 'company_service_id' => $this->company_service_id,
- 'description' => $this->description,
- 'start_date' => $this->start_date,
- 'end_date' => $this->end_date,
- 'project_manager_id' => $this->project_manager_id,
- 'project_value' => $this->project_value,
- 'hours' => $this->hours,
- 'status' => $this->status,
- 'billable' => $this->billable,
- '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_company = null;
- }
- }
|