CasperSecurity
<?php
namespace App\Http\Livewire\Organisation;
use App\Models\SettingsCity;
use App\Models\SettingsState;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;
use App\Models\Organisation;
use Livewire\WithFileUploads;
class OrganisationLivewire extends Component
{
use WithFileUploads;
public $organisationName, $websiteLink, $contactNo, $emailAddress, $gstin, $cin, $panNo, $registeredAddress, $registeredCity, $registeredState, $registeredZip, $presentAddress, $presentCity, $presentState, $presentZip, $invoicePrefix, $organisationLogo, $status;
public $sameAsRegistered;
public $rules = [
'organisationName' => 'required',
'websiteLink' => 'required',
'contactNo' => 'required|numeric|digits:10',
'emailAddress' => 'required|email',
'gstin' => 'required|max:20',
'cin' => 'required|max:20',
'panNo' => 'required|max:20',
'organisationLogo' => 'nullable',
'registeredAddress' => 'required',
'registeredCity' => 'required',
'registeredState' => 'required',
'registeredZip' => 'required|numeric',
'presentAddress' => 'required',
'presentCity' => 'required',
'presentState' => 'required',
'presentZip' => 'required|numeric',
];
public $messages = [
'organisationName.required' => 'The organisation name is required.',
'websiteLink.required' => 'The website link is required.',
'contactNo.required' => 'The contact number is required.',
'contactNo.numeric' => 'The contact number must be numeric.',
'contactNo.digits' => 'The contact number must be exactly 10 digits.',
'emailAddress.required' => 'The email address is required.',
'emailAddress.email' => 'The email address must be a valid email.',
'gstin.required' => 'The GSTIN is required.',
'gstin.max' => 'The GSTIN may not be greater than :max characters.',
'cin.required' => 'The CIN is required.',
'cin.max' => 'The CIN may not be greater than :max characters.',
'panNo.required' => 'The PAN number is required.',
'panNo.max' => 'The PAN number may not be greater than :max characters.',
'registeredAddress.required' => 'The registered address is required.',
'registeredCity.required' => 'The registered city is required.',
'registeredState.required' => 'The registered state is required.',
'registeredZip.required' => 'The registered ZIP code is required.',
'registeredZip.numeric' => 'The registered ZIP code must be numeric.',
'presentAddress.required' => 'The present address is required.',
'presentCity.required' => 'The present city is required.',
'presentState.required' => 'The present state is required.',
'presentZip.required' => 'The present ZIP code is required.',
'presentZip.numeric' => 'The present ZIP code must be numeric.',
];
public function loadData()
{
$organisation = Organisation::first();
if ($organisation) {
$this->organisationName = $organisation->organisation_name;
$this->websiteLink = $organisation->website_ink;
$this->contactNo = $organisation->contact_no;
$this->emailAddress = $organisation->email_address;
$this->gstin = $organisation->gstin;
$this->cin = $organisation->cin;
$this->panNo = $organisation->pan_no;
$this->organisationLogo = $organisation->organisation_logo;
$this->registeredAddress = $organisation->registered_address;
$this->registeredCity = $organisation->registered_city_name;
$this->registeredState = $organisation->registered_state_name;
$this->registeredZip = $organisation->registered_zip_code;
$this->presentAddress = $organisation->present_address;
$this->presentCity = $organisation->present_city_name;
$this->presentState = $organisation->present_state_name;
$this->presentZip = $organisation->present_zip_code;
$this->invoicePrefix = $organisation->invoice_prefix;
$this->status = $organisation->status;
}
}
public function modelData()
{
if ($this->organisationLogo && $this->organisationLogo instanceof \Illuminate\Http\UploadedFile) {
$this->organisationLogo = $this->organisationLogo->store('organisationLogo');
}
$data = [
'organisation_name' => $this->organisationName,
'website_ink' => $this->websiteLink,
'contact_no' => $this->contactNo,
'email_address' => $this->emailAddress,
'gstin' => $this->gstin,
'cin' => $this->cin,
'pan_no' => $this->panNo,
'organisation_logo' => $this->organisationLogo ? $this->organisationLogo : null,
'registered_address' => $this->registeredAddress,
'registered_city_name' => $this->registeredCity,
'registered_state_name' => $this->registeredState,
'registered_zip_code' => $this->registeredZip,
'present_address' => $this->presentAddress,
'present_city_name' => $this->presentCity,
'present_state_name' => $this->presentState,
'present_zip_code' => $this->presentZip,
'invoice_prefix' => $this->invoicePrefix,
];
return $data;
}
public function saveUpdate()
{
$this->validate($this->rules, $this->messages);
$organisation = Organisation::first();
if ($organisation) {
$updated = $organisation->update($this->modelData());
if ($updated) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => "Organisation Updated",
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => "Failed to update organisation details.",
]);
}
} else {
$created = Organisation::create($this->modelData());
if ($created) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => "Organisation Created",
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => "Failed to create organisation.",
]);
}
}
}
public function updatedOrganisationLogo($val)
{
$this->organisationLogo = $this->organisationLogo->store('organisationLogo');
}
public function updatedSameAsRegistered($val)
{
if ($val == true) {
$this->presentAddress = $this->registeredAddress;
$this->presentCity = $this->registeredCity;
$this->presentState = $this->registeredState;
$this->presentZip = $this->registeredZip;
} else {
$this->presentAddress = null;
$this->presentCity = null;
$this->presentState = null;
$this->presentZip = null;
}
}
public function mount()
{
$this->loadData();
}
public function cleanVar()
{
$this->organisationName = null;
$this->websiteLink = null;
$this->contactNo = null;
$this->emailAddress = null;
$this->gstin = null;
$this->cin = null;
$this->panNo = null;
$this->organisationLogo = null;
$this->registeredAddress = null;
$this->registeredCity = null;
$this->registeredState = null;
$this->registeredZip = null;
$this->presentAddress = null;
$this->presentCity = null;
$this->presentState = null;
$this->presentZip = null;
$this->invoicePrefix = null;
}
public function render()
{
$cities = SettingsCity::select('id', 'city_name')->get();
$states = SettingsState::select('id', 'state_name')->get();
return view('livewire.organisation.organisation-livewire', compact('cities', 'states'));
}
}