Refactor for livewire components

This commit is contained in:
David Bomba 2024-11-14 10:40:49 +11:00
parent 1e8a82fd29
commit e01e0f92e4
16 changed files with 108 additions and 90 deletions

View File

@ -28,11 +28,11 @@ class PurchaseOrdersTable extends Component
public $status = []; public $status = [];
public $company; public $db;
public function mount() public function mount()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->db);
$this->sort_asc = false; $this->sort_asc = false;
@ -45,7 +45,7 @@ class PurchaseOrdersTable extends Component
$query = PurchaseOrder::query() $query = PurchaseOrder::query()
->with('vendor.contacts') ->with('vendor.contacts')
->where('company_id', $this->company->id) ->where('company_id', auth()->guard('vendor')->user()->company_id)
->whereIn('status_id', [PurchaseOrder::STATUS_SENT, PurchaseOrder::STATUS_ACCEPTED]) ->whereIn('status_id', [PurchaseOrder::STATUS_SENT, PurchaseOrder::STATUS_ACCEPTED])
->where('is_deleted', false) ->where('is_deleted', false)
->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc'); ->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc');

View File

@ -26,8 +26,6 @@ class QuotesTable extends Component
public array $status = []; public array $status = [];
public Company $company;
public string $sort = 'status_id'; public string $sort = 'status_id';
public bool $sort_asc = true; public bool $sort_asc = true;
@ -39,8 +37,6 @@ class QuotesTable extends Component
public function mount() public function mount()
{ {
MultiDB::setDb($this->db); MultiDB::setDb($this->db);
$this->company = Company::find($this->company_id);
} }
@ -82,7 +78,7 @@ class QuotesTable extends Component
} }
$query = $query $query = $query
->where('company_id', $this->company->id) ->where('company_id', auth()->guard('contact')->user()->company_id)
->where('client_id', auth()->guard('contact')->user()->client_id) ->where('client_id', auth()->guard('contact')->user()->client_id)
->where('is_deleted', 0) ->where('is_deleted', 0)
->where('status_id', '<>', Quote::STATUS_DRAFT) ->where('status_id', '<>', Quote::STATUS_DRAFT)

View File

@ -18,16 +18,14 @@ use Livewire\Component;
class RecurringInvoiceCancellation extends Component class RecurringInvoiceCancellation extends Component
{ {
/**
* @var RecurringInvoice public $invoice_id;
*/
public $invoice;
public $company; public $db;
public function mount() public function mount()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->db);
} }
public function render() public function render()
@ -37,10 +35,15 @@ class RecurringInvoiceCancellation extends Component
public function processCancellation() public function processCancellation()
{ {
if ($this->invoice->subscription) {
return $this->invoice->subscription->service()->handleCancellation($this->invoice); MultiDB::setDb($this->db);
$ri = RecurringInvoice::withTrashed()->find($this->invoice_id);
if ($ri->subscription) {
return $ri->subscription->service()->handleCancellation($ri);
} }
return redirect()->route('client.recurring_invoices.request_cancellation', ['recurring_invoice' => $this->invoice->hashed_id]); return redirect()->route('client.recurring_invoices.request_cancellation', ['recurring_invoice' => $ri->hashed_id]);
} }
} }

View File

@ -25,11 +25,13 @@ class RecurringInvoicesTable extends Component
public $per_page = 10; public $per_page = 10;
public $company; public $company_id;
public $db;
public function mount() public function mount()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->db);
$this->sort_asc = false; $this->sort_asc = false;
@ -43,7 +45,7 @@ class RecurringInvoicesTable extends Component
$query = $query $query = $query
// ->with('client') // ->with('client')
->where('client_id', auth()->guard('contact')->user()->client_id) ->where('client_id', auth()->guard('contact')->user()->client_id)
->where('company_id', $this->company->id) ->where('company_id', auth()->guard('contact')->user()->company_id)
->whereIn('status_id', [RecurringInvoice::STATUS_ACTIVE]) ->whereIn('status_id', [RecurringInvoice::STATUS_ACTIVE])
->orderBy('status_id', 'asc') ->orderBy('status_id', 'asc')
->with('client') ->with('client')

View File

@ -59,7 +59,6 @@ class RequiredClientInfo extends Component
*/ */
public $countries; public $countries;
public $client_name; public $client_name;
public $contact_first_name; public $contact_first_name;
public $contact_last_name; public $contact_last_name;

View File

@ -12,40 +12,30 @@
namespace App\Livewire; namespace App\Livewire;
use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\Subscription;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Livewire\Component; use Livewire\Component;
use App\Libraries\MultiDB;
use Illuminate\Support\Str;
use App\Models\Subscription;
use App\Models\ClientContact;
use App\Models\RecurringInvoice;
use Livewire\Attributes\Computed;
use Illuminate\Support\Facades\Cache;
class SubscriptionPlanSwitch extends Component class SubscriptionPlanSwitch extends Component
{ {
/**
* @var \App\Models\RecurringInvoice
*/
public $recurring_invoice;
/** public $recurring_invoice_id;
* @var Subscription
*/ public $subscription_id;
public $subscription;
/**
* @var ?float
*/
public $amount; public $amount;
/** public $target_id;
* @var Subscription
*/
public $target;
/** public $contact_id;
* @var ClientContact
*/
public ClientContact $contact;
public $db;
/** /**
* @var array * @var array
*/ */
@ -74,8 +64,6 @@ class SubscriptionPlanSwitch extends Component
*/ */
public $hash; public $hash;
public $company;
public ?string $first_name; public ?string $first_name;
public ?string $last_name; public ?string $last_name;
@ -84,21 +72,45 @@ class SubscriptionPlanSwitch extends Component
public function mount() public function mount()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->db);
$this->total = $this->amount; $this->total = $this->amount;
$this->methods = $this->contact->client->service()->getPaymentMethods($this->amount); $this->methods = $this->contact()->client->service()->getPaymentMethods($this->amount);
$this->hash = Str::uuid()->toString(); $this->hash = Str::uuid()->toString();
$this->state['show_rff'] = auth()->guard('contact')->user()->showRff(); $this->state['show_rff'] = auth()->guard('contact')->user()->showRff();
$this->first_name = $this->contact->first_name; $this->first_name = $this->contact()->first_name;
$this->last_name = $this->contact->last_name; $this->last_name = $this->contact()->last_name;
$this->email = $this->contact->email; $this->email = $this->contact()->email;
}
#[Computed]
public function recurring_invoice()
{
return RecurringInvoice::withTrashed()->find($this->recurring_invoice_id);
}
#[Computed]
public function subscription()
{
return Subscription::withTrashed()->find($this->subscription_id);
}
#[Computed]
public function target()
{
return Subscription::withTrashed()->find($this->target_id);
}
#[Computed]
public function contact()
{
return ClientContact::withTrashed()->find($this->contact_id);
} }
public function handleRff() public function handleRff()
@ -109,7 +121,7 @@ class SubscriptionPlanSwitch extends Component
'email' => ['required', 'email'], 'email' => ['required', 'email'],
]); ]);
$this->contact->update([ $this->contact()->update([
'first_name' => $this->first_name, 'first_name' => $this->first_name,
'last_name' => $this->last_name, 'last_name' => $this->last_name,
'email' => $this->email, 'email' => $this->email,
@ -122,28 +134,28 @@ class SubscriptionPlanSwitch extends Component
{ {
$this->state['show_loading_bar'] = true; $this->state['show_loading_bar'] = true;
$payment_required = $this->target->service()->changePlanPaymentCheck([ $payment_required = $this->target()->service()->changePlanPaymentCheck([
'recurring_invoice' => $this->recurring_invoice, 'recurring_invoice' => $this->recurring_invoice(),
'subscription' => $this->subscription, 'subscription' => $this->subscription(),
'target' => $this->target, 'target' => $this->target(),
'hash' => $this->hash, 'hash' => $this->hash,
]); ]);
if ($payment_required) { if ($payment_required) {
$this->state['invoice'] = $this->target->service()->createChangePlanInvoice([ $this->state['invoice'] = $this->target()->service()->createChangePlanInvoice([
'recurring_invoice' => $this->recurring_invoice, 'recurring_invoice' => $this->recurring_invoice(),
'subscription' => $this->subscription, 'subscription' => $this->subscription(),
'target' => $this->target, 'target' => $this->target(),
'hash' => $this->hash, 'hash' => $this->hash,
]); ]);
Cache::put( Cache::put(
$this->hash, $this->hash,
[ [
'subscription_id' => $this->target->hashed_id, 'subscription_id' => $this->target()->hashed_id,
'target_id' => $this->target->hashed_id, 'target_id' => $this->target()->hashed_id,
'recurring_invoice' => $this->recurring_invoice->hashed_id, 'recurring_invoice' => $this->recurring_invoice()->hashed_id,
'client_id' => $this->recurring_invoice->client->hashed_id, 'client_id' => $this->recurring_invoice()->client->hashed_id,
'invoice_id' => $this->state['invoice']->hashed_id, 'invoice_id' => $this->state['invoice']->hashed_id,
'context' => 'change_plan', 'context' => 'change_plan',
now()->addMinutes(60), ] now()->addMinutes(60), ]
@ -176,10 +188,10 @@ class SubscriptionPlanSwitch extends Component
{ {
$this->hide_button = true; $this->hide_button = true;
$response = $this->target->service()->createChangePlanCreditV2([ $response = $this->target()->service()->createChangePlanCreditV2([
'recurring_invoice' => $this->recurring_invoice, 'recurring_invoice' => $this->recurring_invoice(),
'subscription' => $this->subscription, 'subscription' => $this->subscription(),
'target' => $this->target, 'target' => $this->target(),
'hash' => $this->hash, 'hash' => $this->hash,
]); ]);

View File

@ -25,18 +25,20 @@ class SubscriptionRecurringInvoicesTable extends Component
public $per_page = 10; public $per_page = 10;
public $company; public $company_id;
public $db;
public function mount() public function mount()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->db);
} }
public function render() public function render()
{ {
$query = RecurringInvoice::query() $query = RecurringInvoice::query()
->where('client_id', auth()->guard('contact')->user()->client->id) ->where('client_id', auth()->guard('contact')->user()->client->id)
->where('company_id', $this->company->id) ->where('company_id', $this->company_id)
->whereNotNull('subscription_id') ->whereNotNull('subscription_id')
->where('is_deleted', false) ->where('is_deleted', false)
->where('status_id', RecurringInvoice::STATUS_ACTIVE) ->where('status_id', RecurringInvoice::STATUS_ACTIVE)

View File

@ -25,18 +25,20 @@ class SubscriptionsTable extends Component
public $per_page = 10; public $per_page = 10;
public $company; public $company_id;
public $db;
public function mount() public function mount()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->db);
} }
public function render() public function render()
{ {
$query = RecurringInvoice::query() $query = RecurringInvoice::query()
->where('client_id', auth()->guard('contact')->user()->client->id) ->where('client_id', auth()->guard('contact')->user()->client->id)
->where('company_id', $this->company->id) ->where('company_id', $this->company_id)
->whereNotNull('subscription_id') ->whereNotNull('subscription_id')
->where('is_deleted', false) ->where('is_deleted', false)
->whereIn('status_id', [RecurringInvoice::STATUS_ACTIVE, RecurringInvoice::STATUS_PAUSED]) ->whereIn('status_id', [RecurringInvoice::STATUS_ACTIVE, RecurringInvoice::STATUS_PAUSED])

View File

@ -25,17 +25,19 @@ class TasksTable extends Component
public $per_page = 10; public $per_page = 10;
public $company; public $company_id;
public $db;
public function mount() public function mount()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->db);
} }
public function render() public function render()
{ {
$query = Task::query() $query = Task::query()
->where('company_id', $this->company->id) ->where('company_id', $this->company_id)
->where('is_deleted', false) ->where('is_deleted', false)
->where('client_id', auth()->guard('contact')->user()->client_id); ->where('client_id', auth()->guard('contact')->user()->client_id);

View File

@ -51,7 +51,7 @@
<div class="relative flex justify-center text-sm leading-5"> <div class="relative flex justify-center text-sm leading-5">
<span class="font-bold tracking-wide bg-gray-100 px-6 py-0">{{ ctrans('texts.select_payment_method')}}</span> <span class="font-bold tracking-wide bg-gray-100 px-6 py-0">{{ ctrans('texts.select_payment_method')}}</span>
<h1 class="text-2xl font-bold tracking-wide bg-gray-100 px-6 py-0"> <h1 class="text-2xl font-bold tracking-wide bg-gray-100 px-6 py-0">
{{ ctrans('texts.total') }}: {{ \App\Utils\Number::formatMoney($amount, $subscription->company) }} {{ ctrans('texts.total') }}: {{ \App\Utils\Number::formatMoney($amount, $this->subscription->company) }}
</h1> </h1>
</div> </div>
</div> </div>
@ -71,9 +71,9 @@
<input type="hidden" name="action" value="payment"> <input type="hidden" name="action" value="payment">
<input type="hidden" name="company_gateway_id" value="{{ $state['company_gateway_id'] }}"/> <input type="hidden" name="company_gateway_id" value="{{ $state['company_gateway_id'] }}"/>
<input type="hidden" name="payment_method_id" value="{{ $state['payment_method_id'] }}"/> <input type="hidden" name="payment_method_id" value="{{ $state['payment_method_id'] }}"/>
<input type="hidden" name="contact_first_name" value="{{ $contact->first_name }}"> <input type="hidden" name="contact_first_name" value="{{ $this->contact()->first_name }}">
<input type="hidden" name="contact_last_name" value="{{ $contact->last_name }}"> <input type="hidden" name="contact_last_name" value="{{ $this->contact()->last_name }}">
<input type="hidden" name="contact_email" value="{{ $contact->email }}"> <input type="hidden" name="contact_email" value="{{ $this->contact()->email }}">
</form> </form>
<!-- Payment methods --> <!-- Payment methods -->
@ -107,7 +107,7 @@
<div class="relative flex justify-center text-sm leading-5"> <div class="relative flex justify-center text-sm leading-5">
<h1 class="text-2xl font-bold tracking-wide bg-gray-100 px-6 py-0"> <h1 class="text-2xl font-bold tracking-wide bg-gray-100 px-6 py-0">
{{ ctrans('texts.total') }}: {{ \App\Utils\Number::formatMoney($amount, $subscription->company) }} {{ ctrans('texts.total') }}: {{ \App\Utils\Number::formatMoney($amount, $this->subscription()->company) }}
</h1> </h1>
</div> </div>
<div class="relative flex justify-center text-sm leading-5 mt-10"> <div class="relative flex justify-center text-sm leading-5 mt-10">

View File

@ -20,6 +20,6 @@
</form> </form>
</div> </div>
<div class="flex flex-col mt-4"> <div class="flex flex-col mt-4">
@livewire('purchase-orders-table', ['company' => $company]) @livewire('purchase-orders-table', ['company_id' => $company->id, 'db' => $company->db])
</div> </div>
@endsection @endsection

View File

@ -34,7 +34,7 @@
</div> </div>
<div class="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse"> <div class="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
<div class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto"> <div class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto">
@livewire('recurring-invoice-cancellation', ['invoice' => $invoice, 'company' => $company]) @livewire('recurring-invoice-cancellation', ['invoice_id' => $invoice->id, 'db' => $company->db])
</div> </div>
<div class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto"> <div class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto">
<button @click="open = false" type="button" class="button button-secondary button-block"> <button @click="open = false" type="button" class="button button-secondary button-block">

View File

@ -3,6 +3,6 @@
@section('body') @section('body')
<div class="flex flex-col"> <div class="flex flex-col">
@livewire('recurring-invoices-table', ['company' => $company]) @livewire('recurring-invoices-table', ['company_id' => $company->id, 'db' => $company->db])
</div> </div>
@endsection @endsection

View File

@ -3,6 +3,6 @@
@section('body') @section('body')
<div class="flex flex-col"> <div class="flex flex-col">
@livewire('subscriptions-table', ['company' => $company]) @livewire('subscriptions-table', ['db' => $company->db , 'company_id' => $company->id])
</div> </div>
@endsection @endsection

View File

@ -42,7 +42,7 @@
</div> </div>
<!-- Payment box --> <!-- Payment box -->
@livewire('subscription-plan-switch', compact('recurring_invoice', 'subscription', 'target', 'contact', 'amount', 'company')) @livewire('subscription-plan-switch', ['db' => $company->db, 'recurring_invoice_id' => $recurring_invoice->id, 'subscription_id' => $subscription->id, 'target_id' => $target->id, 'contact_id' => $contact->id, 'amount' => $amount] )
</div> </div>
@endsection @endsection

View File

@ -3,6 +3,6 @@
@section('body') @section('body')
<div class="flex flex-col"> <div class="flex flex-col">
@livewire('tasks-table', ['company' => $company]) @livewire('tasks-table', ['db' => $company->db, 'company_id' => $company->id])
</div> </div>
@endsection @endsection