self host einvoicing

This commit is contained in:
David Bomba 2024-08-28 12:13:07 +10:00
parent 3ad93ac8aa
commit 17602f5fcd
2 changed files with 34 additions and 25 deletions

View File

@ -14,6 +14,7 @@ namespace App\Livewire\EInvoice;
use Livewire\Component; use Livewire\Component;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
class Portal extends Component class Portal extends Component
{ {
@ -29,31 +30,12 @@ class Portal extends Component
public function mount() public function mount()
{ {
$this->companies = auth()->guard('user')->check() ? auth()->guard('user')->user()->account->companies->map(function ($company) { $this->getCompanies();
return [
'key' => $company->company_key,
'city' => $company->settings->city,
'country' => $company->country()->iso_3166_2,
'county' => $company->settings->state,
'line1' => $company->settings->address1,
'line2' => $company->settings->address2,
'name' => $company->settings->name,
'vat_number' => $company->settings->vat_number,
'zip' => $company->settings->postal_code,
'legal_entity_id' => $company->legal_entity_id
];
})->toArray() : [];
} }
public function login() private function getCompanies(): self
{ {
$credentials = ['email' => $this->email, 'password' => $this->password];
if (Auth::attempt($credentials)) {
session()->flash('message', 'Logged in successfully.');
App::setLocale(auth()->guard('user')->user()->account->companies->first()->getLocale());
$this->companies = auth()->guard('user')->check() ? auth()->guard('user')->user()->account->companies->map(function ($company) { $this->companies = auth()->guard('user')->check() ? auth()->guard('user')->user()->account->companies->map(function ($company) {
return [ return [
@ -73,6 +55,21 @@ class Portal extends Component
]; ];
})->toArray() : []; })->toArray() : [];
return $this;
}
public function login()
{
$credentials = ['email' => $this->email, 'password' => $this->password];
if (Auth::attempt($credentials)) {
session()->flash('message', 'Logged in successfully.');
App::setLocale(auth()->guard('user')->user()->account->companies->first()->getLocale());
$this->getCOmpanies();
} else { } else {
session()->flash('error', 'Invalid credentials.'); session()->flash('error', 'Invalid credentials.');
@ -102,19 +99,31 @@ class Portal extends Component
$register_company = array_merge($company, $register_company); $register_company = array_merge($company, $register_company);
} }
nlog("{$this->api_url}/api/einvoice/createLegalEntity");
nlog($register_company);
$r = Http::withHeaders($this->getHeaders()) $r = Http::withHeaders($this->getHeaders())
->post("{$api_url}/api/einvoice/createLegalEntity", $register_company); ->post("{$this->api_url}/api/einvoice/createLegalEntity", $register_company);
if($r->successful()) if($r->successful())
{ {
nlog($r->json());
nlog($r->body());
$response = $r->json(); $response = $r->json();
$_company = auth()->guard('user')->user()->account->companies()->where('company_id', $company_key)->first(); $_company = auth()->guard('user')->user()->account->companies()->where('company_key', $company_key)->first();
$_company->legal_entity_id = $response['id']; $_company->legal_entity_id = $response['id'];
$_company->save(); $_company->save();
$this->getCompanies();
return; return;
} }
if($r->failed())
nlog($r->getBody()->getContents());
$error = json_decode($r->getBody()->getContents(),true); $error = json_decode($r->getBody()->getContents(),true);
session()->flash('error', $error['message']); session()->flash('error', $error['message']);
@ -125,6 +134,8 @@ class Portal extends Component
{ {
return [ return [
'X-API-SELF-HOST-TOKEN' => config('ninja.license_key'), 'X-API-SELF-HOST-TOKEN' => config('ninja.license_key'),
"X-Requested-With" => "XMLHttpRequest",
"Content-Type" => "application/json",
]; ];
} }

View File

@ -303,8 +303,6 @@ class Storecove
if($r->successful()) { if($r->successful()) {
$data = $r->json(); $data = $r->json();
LightLogs::create(new LegalEntityCreated($data['id'], $legal_entity_id))->batch();
return $data; return $data;
} }