Refactor for livewire components
This commit is contained in:
parent
e01e0f92e4
commit
86ed8044c3
|
|
@ -1,26 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Http\Controllers\EInvoice;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\EInvoice\SignupRequest;
|
||||
|
||||
class SelfhostController extends Controller
|
||||
{
|
||||
|
||||
public function index(SignupRequest $request)
|
||||
{
|
||||
return view('einvoice.index');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,142 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Livewire\EInvoice;
|
||||
|
||||
use Livewire\Component;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class Portal extends Component
|
||||
{
|
||||
public $email = '';
|
||||
public $password = '';
|
||||
|
||||
public array $companies;
|
||||
|
||||
private string $api_url = '';
|
||||
|
||||
public function mount()
|
||||
{
|
||||
|
||||
$this->api_url = config('ninja.hosted_ninja_url');
|
||||
|
||||
$this->getCompanies();
|
||||
|
||||
}
|
||||
|
||||
private function getCompanies(): self
|
||||
{
|
||||
|
||||
$this->companies = auth()->guard('user')->check() ? auth()->guard('user')->user()->account->companies->map(function ($company) {
|
||||
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,
|
||||
'party_name' => $company->settings->name,
|
||||
'vat_number' => $company->settings->vat_number,
|
||||
'zip' => $company->settings->postal_code,
|
||||
'legal_entity_id' => $company->legal_entity_id,
|
||||
'tax_registered' => (bool) strlen($company->settings->vat_number ?? '') > 2,
|
||||
'tenant_id' => $company->company_key,
|
||||
'classification' => strlen($company->settings->classification ?? '') > 2 ? $company->settings->classification : 'business',
|
||||
];
|
||||
})->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 {
|
||||
session()->flash('error', 'Invalid credentials.');
|
||||
}
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
Auth::logout();
|
||||
|
||||
session()->flash('message', 'Logged out!');
|
||||
|
||||
}
|
||||
|
||||
public function register(string $company_key)
|
||||
{
|
||||
|
||||
$register_company = [
|
||||
'acts_as_receiver' => true,
|
||||
'acts_as_sender' => true,
|
||||
'advertisements' => ['invoice']
|
||||
];
|
||||
|
||||
foreach($this->companies as $company)
|
||||
{
|
||||
if($company['key'] == $company_key)
|
||||
$register_company = array_merge($company, $register_company);
|
||||
}
|
||||
|
||||
$r = Http::withHeaders($this->getHeaders())
|
||||
->post("{$this->api_url}/api/einvoice/createLegalEntity", $register_company);
|
||||
|
||||
if($r->successful())
|
||||
{
|
||||
|
||||
nlog($r->body());
|
||||
$response = $r->json();
|
||||
|
||||
$_company = auth()->guard('user')->user()->account->companies()->where('company_key', $company_key)->first();
|
||||
$_company->legal_entity_id = $response['id'];
|
||||
$_company->save();
|
||||
|
||||
$this->getCompanies();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if($r->failed())
|
||||
nlog($r->getBody()->getContents());
|
||||
|
||||
$error = json_decode($r->getBody()->getContents(),true);
|
||||
|
||||
session()->flash('error', $error['message']);
|
||||
|
||||
}
|
||||
|
||||
private function getHeaders()
|
||||
{
|
||||
return [
|
||||
'X-API-SELF-HOST-TOKEN' => config('ninja.license_key'),
|
||||
"X-Requested-With" => "XMLHttpRequest",
|
||||
"Content-Type" => "application/json",
|
||||
];
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.e-invoice.portal');
|
||||
}
|
||||
}
|
||||
|
|
@ -12,39 +12,43 @@
|
|||
|
||||
namespace App\Livewire\PaymentMethods;
|
||||
|
||||
use App\Libraries\MultiDB;
|
||||
use Livewire\Component;
|
||||
use App\Libraries\MultiDB;
|
||||
use Livewire\Attributes\Computed;
|
||||
use App\Models\ClientGatewayToken;
|
||||
|
||||
class UpdateDefaultMethod extends Component
|
||||
{
|
||||
/** @var \App\Models\Company */
|
||||
public $company;
|
||||
public $db;
|
||||
|
||||
/** @var \App\Models\ClientGatewayToken */
|
||||
public $token;
|
||||
|
||||
/** @var \App\Models\Client */
|
||||
public $client;
|
||||
public $token_id;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->company = $this->client->company;
|
||||
MultiDB::setDb($this->db);
|
||||
}
|
||||
|
||||
MultiDB::setDb($this->company->db);
|
||||
|
||||
// $this->is_disabled = $this->token->is_default;
|
||||
#[Computed]
|
||||
public function token()
|
||||
{
|
||||
return ClientGatewayToken::withTrashed()->find($this->token_id);
|
||||
}
|
||||
|
||||
public function makeDefault(): void
|
||||
{
|
||||
if ($this->token->is_default) {
|
||||
|
||||
MultiDB::setDb($this->db);
|
||||
|
||||
|
||||
if ($this->token()->is_default) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->client->gateway_tokens()->update(['is_default' => 0]);
|
||||
$this->token()->client->gateway_tokens()->update(['is_default' => 0]);
|
||||
|
||||
$this->token->is_default = 1;
|
||||
$this->token->save();
|
||||
$token = $this->token();
|
||||
$token->is_default = 1;
|
||||
$token->save();
|
||||
|
||||
$this->dispatch('UpdateDefaultMethod::method-updated');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,13 +12,12 @@
|
|||
|
||||
namespace App\Livewire\Profile\Settings;
|
||||
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Livewire\Component;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class General extends Component
|
||||
{
|
||||
public $profile;
|
||||
|
||||
public $first_name;
|
||||
|
||||
public $last_name;
|
||||
|
|
@ -40,12 +39,18 @@ class General extends Component
|
|||
'phone' => ['sometimes'],
|
||||
];
|
||||
|
||||
#[Computed]
|
||||
public function profile()
|
||||
{
|
||||
return auth()->guard('contact')->user();
|
||||
}
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$profile = auth()->guard('contact')->user();
|
||||
$profile = $this->profile();
|
||||
|
||||
$this->fill([
|
||||
'profile' => $profile,
|
||||
// 'profile' => $profile,
|
||||
'first_name' => $profile->first_name,
|
||||
'last_name' => $profile->last_name,
|
||||
'email' => $profile->email,
|
||||
|
|
@ -61,7 +66,10 @@ class General extends Component
|
|||
|
||||
public function submit()
|
||||
{
|
||||
if ($this->profile->email != $this->email) {
|
||||
|
||||
$profile = $this->profile();
|
||||
|
||||
if ($profile->email != $this->email) {
|
||||
$this->rules['email'][] = 'unique:client_contacts,email';
|
||||
}
|
||||
|
||||
|
|
@ -72,10 +80,10 @@ class General extends Component
|
|||
$data = $this->validate($this->rules);
|
||||
|
||||
if (! empty($this->password)) {
|
||||
$this->profile->password = Hash::make($this->password);
|
||||
$profile->password = Hash::make($this->password);
|
||||
}
|
||||
|
||||
$this->profile
|
||||
$profile
|
||||
->fill($data)
|
||||
->save();
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ use Livewire\Component;
|
|||
|
||||
class NameWebsiteLogo extends Component
|
||||
{
|
||||
public $profile;
|
||||
|
||||
public $name;
|
||||
|
||||
public $vat_number;
|
||||
|
|
@ -28,7 +26,7 @@ class NameWebsiteLogo extends Component
|
|||
public function mount()
|
||||
{
|
||||
$this->fill([
|
||||
'profile' => auth()->guard('contact')->user()->client,
|
||||
// 'profile' => auth()->guard('contact')->user()->client,
|
||||
'name' => auth()->guard('contact')->user()->client->present()->name(),
|
||||
'vat_number' => auth()->guard('contact')->user()->client->vat_number ?: '',
|
||||
'website' => auth()->guard('contact')->user()->client->website,
|
||||
|
|
@ -46,12 +44,14 @@ class NameWebsiteLogo extends Component
|
|||
{
|
||||
$data = $this->validate($this->rules);
|
||||
|
||||
$this->profile->name = $data['name'];
|
||||
$this->profile->vat_number = $data['vat_number'];
|
||||
$this->profile->website = $data['website'];
|
||||
$this->profile->phone = $data['phone'];
|
||||
$profile = auth()->guard('contact')->user()->client;
|
||||
|
||||
$this->profile->save();
|
||||
$profile->name = $data['name'];
|
||||
$profile->vat_number = $data['vat_number'];
|
||||
$profile->website = $data['website'];
|
||||
$profile->phone = $data['phone'];
|
||||
|
||||
$profile->save();
|
||||
|
||||
$this->saved = ctrans('texts.saved_at', ['time' => now()->toTimeString()]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use Livewire\Component;
|
|||
|
||||
class PersonalAddress extends Component
|
||||
{
|
||||
public $profile;
|
||||
|
||||
public $address1;
|
||||
|
||||
|
|
@ -34,7 +33,7 @@ class PersonalAddress extends Component
|
|||
public function mount()
|
||||
{
|
||||
$this->fill([
|
||||
'profile' => auth()->guard('contact')->user()->client,
|
||||
// 'profile' => auth()->guard('contact')->user()->client,
|
||||
'address1' => auth()->guard('contact')->user()->client->address1,
|
||||
'address2' => auth()->guard('contact')->user()->client->address2,
|
||||
'city' => auth()->guard('contact')->user()->client->city,
|
||||
|
|
@ -58,7 +57,9 @@ class PersonalAddress extends Component
|
|||
$data['country_id'] = null;
|
||||
}
|
||||
|
||||
$this->profile
|
||||
$profile = auth()->guard('contact')->user()->client;
|
||||
|
||||
$profile
|
||||
->fill($data)
|
||||
->save();
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use Livewire\Component;
|
|||
|
||||
class ShippingAddress extends Component
|
||||
{
|
||||
public $profile;
|
||||
|
||||
public $shipping_address1;
|
||||
|
||||
|
|
@ -34,7 +33,7 @@ class ShippingAddress extends Component
|
|||
public function mount()
|
||||
{
|
||||
$this->fill([
|
||||
'profile' => auth()->guard('contact')->user()->client,
|
||||
// 'profile' => auth()->guard('contact')->user()->client,
|
||||
'shipping_address1' => auth()->guard('contact')->user()->client->shipping_address1,
|
||||
'shipping_address2' => auth()->guard('contact')->user()->client->shipping_address2,
|
||||
'shipping_city' => auth()->guard('contact')->user()->client->shipping_city,
|
||||
|
|
@ -58,7 +57,9 @@ class ShippingAddress extends Component
|
|||
$data['shipping_country_id'] = null;
|
||||
}
|
||||
|
||||
$this->profile
|
||||
$profile = auth()->guard('contact')->user()->client;
|
||||
|
||||
$profile
|
||||
->fill($data)
|
||||
->save();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
@extends('layouts.ninja')
|
||||
|
||||
@section('body')
|
||||
@livewire('e-invoice.portal')
|
||||
@stop
|
||||
|
|
@ -9,13 +9,13 @@
|
|||
<span class="text-primary mr-1 hidden" data-ref="success-label">{{ ctrans('texts.success') }}!</span>
|
||||
|
||||
<p>
|
||||
{{ $token->is_default ? ctrans('texts.already_default_payment_method') : ctrans('texts.default_payment_method') }}
|
||||
{{ $this->token->is_default ? ctrans('texts.already_default_payment_method') : ctrans('texts.default_payment_method') }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-5 sm:mt-0 sm:ml-6 sm:flex-shrink-0 sm:flex sm:items-center">
|
||||
<form wire:submit="makeDefault">
|
||||
<button class="button button-primary bg-primary" {{ $token->is_default ? 'disabled' : '' }}>
|
||||
<button class="button button-primary bg-primary" {{ $this->token->is_default ? 'disabled' : '' }}>
|
||||
{{ ctrans('texts.save_as_default') }}
|
||||
</button>
|
||||
</form>
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@
|
|||
</div>
|
||||
@endif
|
||||
|
||||
@livewire('payment-methods.update-default-method', ['token' => $payment_method, 'client' => $client])
|
||||
@livewire('payment-methods.update-default-method', ['token_id' => $payment_method->id, 'db' => $client->company->db])
|
||||
|
||||
<div class="mt-4 mb-4 bg-white shadow sm:rounded-lg">
|
||||
<div class="px-4 py-5 sm:p-6">
|
||||
|
|
|
|||
|
|
@ -57,6 +57,4 @@ Route::get('gocardless/oauth/connect/confirm', [GoCardlessOAuthController::class
|
|||
Route::post('gocardless/oauth/connect/webhook', GoCardlessOAuthWebhookController::class)->name('gocardless.oauth.webhook');
|
||||
Route::get('gocardless/oauth/connect/{token}', [GoCardlessOAuthController::class, 'connect']);
|
||||
|
||||
Route::get('einvoice/beta', [SelfhostController::class, 'index'])->name('einvoice.beta');
|
||||
|
||||
\Illuminate\Support\Facades\Broadcast::routes(['middleware' => ['token_auth']]);
|
||||
|
|
|
|||
Loading…
Reference in New Issue