Jelajahi Sumber

Gestione company

Luca Parisio 4 bulan lalu
induk
melakukan
84b9534557
2 mengubah file dengan 149 tambahan dan 1 penghapusan
  1. 80 1
      app/Livewire/CompanyRate.php
  2. 69 0
      app/Livewire/CompanyService.php

+ 80 - 1
app/Livewire/CompanyRate.php

@@ -12,12 +12,91 @@ class CompanyRate extends Component
 
     public $company;
 
+    public $current_company_rate = null;
+
+    public $company_rates = [];
+
     #[Validate('required')] 
-    public $name = '';
+    public $amount = '';
+    public $type = '';
+    public $group = '';
+    public $description = '';
+    public $enabled = 1;
+
+    public $is_edit = false;
+
+    public function resetFields(){
+        $this->amount = '';
+        $this->type = '';
+        $this->group = '';
+        $this->description = '';
+        $this->enabled = 1;
+    }
 
     public function render()
     {
+        $this->company_rates = $this->company->rates ?? [];
         return view('livewire.company_rate');
     }
 
+    public function add()
+    {
+        $this->resetFields();
+        $this->is_edit = true;
+        $this->current_company_rate = null;
+    }
+
+    public function edit($id)
+    {
+        $this->resetFields();
+        $this->is_edit = true;
+        $this->current_company_rate = \App\Models\CompanyRate::findOrFail($id);
+        $this->amount = $this->current_company_rate->amount;
+        $this->type = $this->current_company_rate->type;
+        $this->group = $this->current_company_rate->group;
+        $this->description = $this->current_company_rate->description;
+        $this->enabled = $this->current_company_rate->enabled == 1;
+    }
+
+    public function save()
+    {
+        $this->validate();
+        try 
+        {
+            if ($this->current_company_rate == null)
+            {
+                \App\Models\CompanyActivity::create([
+                    'company_id' => $this->company->id,
+                    'amount' => $this->amount,
+                    'type' => $this->type,
+                    'group' => $this->group,
+                    'description' => $this->description,
+                    'enabled' => $this->enabled
+                ]);
+            }
+            else
+            {
+                $this->current_company_rate->update([
+                    'amount' => $this->amount,
+                    'type' => $this->type,
+                    'group' => $this->group,
+                    'description' => $this->description,
+                    '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_company_rate = null;
+    }
+
 }

+ 69 - 0
app/Livewire/CompanyService.php

@@ -12,12 +12,81 @@ class CompanyService extends Component
 
     public $company;
 
+    public $current_company_service = null;
+
+    public $company_services = [];
+
     #[Validate('required')] 
     public $name = '';
+    public $description = '';
+    public $enabled = 1;
+
+    public $is_edit = false;
+
+    public function resetFields(){
+        $this->name = '';
+        $this->description = '';
+        $this->enabled = 1;
+    }
 
     public function render()
     {
+        $this->company_services = $this->company->services ?? [];
         return view('livewire.company_service');
     }
 
+    public function add()
+    {
+        $this->resetFields();
+        $this->is_edit = true;
+        $this->current_company_service = null;
+    }
+
+    public function edit($id)
+    {
+        $this->resetFields();
+        $this->is_edit = true;
+        $this->current_company_service = \App\Models\CompanyService::findOrFail($id);
+        $this->name = $this->current_company_service->name;
+        $this->description = $this->current_company_service->description;
+        $this->enabled = $this->current_company_service->enabled == 1;
+    }
+
+    public function save()
+    {
+        $this->validate();
+        try 
+        {
+            if ($this->current_company_service == null)
+            {
+                \App\Models\CompanyService::create([
+                    'company_id' => $this->company->id,
+                    'name' => $this->name,
+                    'description' => $this->description,
+                    'enabled' => $this->enabled
+                ]);
+            }
+            else
+            {
+                $this->current_company_service->update([
+                    'name' => $this->name,
+                    'description' => $this->description,
+                    '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_company_service = null;
+    }
+
 }