From f4bfa6c0fd2a3b7b2d4af8720d252387a57627d4 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 9 Jun 2021 19:57:03 +1000 Subject: [PATCH 1/2] Add rules for adding users to hosted platform --- app/Http/Requests/User/StoreUserRequest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Requests/User/StoreUserRequest.php b/app/Http/Requests/User/StoreUserRequest.php index fd91149a7931..2f0c9f7077f2 100644 --- a/app/Http/Requests/User/StoreUserRequest.php +++ b/app/Http/Requests/User/StoreUserRequest.php @@ -18,6 +18,7 @@ use App\Http\ValidationRules\User\AttachableUser; use App\Http\ValidationRules\ValidUserForCompany; use App\Libraries\MultiDB; use App\Models\User; +use App\Utils\Ninja; use Illuminate\Validation\Rule; class StoreUserRequest extends Request @@ -45,8 +46,7 @@ class StoreUserRequest extends Request $rules['email'] = ['email', new AttachableUser()]; } - - if (auth()->user()->company()->account->isFreeHostedClient()) { + if (Ninja::isHosted()) { $rules['hosted_users'] = new CanAddUserRule(auth()->user()->company()->account); } From 7627d3566d556bf006ee76a91aea8dd72696da28 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 9 Jun 2021 20:19:23 +1000 Subject: [PATCH 2/2] Improve query efficiency --- app/Console/Commands/CheckData.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index f76f64fc9a0b..a18948a134e4 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -311,17 +311,17 @@ class CheckData extends Command Client::withTrashed()->where('is_deleted', 0)->cursor()->each(function ($client) use ($wrong_paid_to_dates, $credit_total_applied) { $total_invoice_payments = 0; - foreach ($client->invoices->where('is_deleted', false)->where('status_id', '>', 1) as $invoice) { + foreach ($client->invoices()->where('is_deleted', false)->where('status_id', '>', 1)->get() as $invoice) { - $total_amount = $invoice->payments->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])->sum('pivot.amount'); - $total_refund = $invoice->payments->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])->sum('pivot.refunded'); + $total_amount = $invoice->payments()->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])->get()->sum('pivot.amount'); + $total_refund = $invoice->payments()->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])->get()->sum('pivot.refunded'); $total_invoice_payments += ($total_amount - $total_refund); } // 10/02/21 foreach ($client->payments as $payment) { - $credit_total_applied += $payment->paymentables->where('paymentable_type', App\Models\Credit::class)->sum(DB::raw('amount')); + $credit_total_applied += $payment->paymentables()->where('paymentable_type', App\Models\Credit::class)->get()->sum(DB::raw('amount')); } if ($credit_total_applied < 0) { @@ -347,10 +347,11 @@ class CheckData extends Command $wrong_paid_to_dates = 0; Client::cursor()->where('is_deleted', 0)->each(function ($client) use ($wrong_balances) { + $client->invoices->where('is_deleted', false)->whereIn('status_id', '!=', Invoice::STATUS_DRAFT)->each(function ($invoice) use ($wrong_balances, $client) { - $total_amount = $invoice->payments->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.amount'); - $total_refund = $invoice->payments->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.refunded'); - $total_credit = $invoice->credits->sum('amount'); + $total_amount = $invoice->payments()->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->get()->sum('pivot.amount'); + $total_refund = $invoice->payments()->get()->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.refunded'); + $total_credit = $invoice->credits()->get()->sum('amount'); $total_paid = $total_amount - $total_refund; $calculated_paid_amount = $invoice->amount - $invoice->balance - $total_credit; @@ -363,6 +364,7 @@ class CheckData extends Command $this->isValid = false; } }); + }); $this->logMessage("{$wrong_balances} clients with incorrect invoice balances"); @@ -408,8 +410,8 @@ class CheckData extends Command $wrong_paid_to_dates = 0; foreach (Client::where('is_deleted', 0)->cursor() as $client) { - $invoice_balance = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance'); - $credit_balance = $client->credits->where('is_deleted', false)->sum('balance'); + $invoice_balance = $client->invoices()->where('is_deleted', false)->where('status_id', '>', 1)->get()->sum('balance'); + $credit_balance = $client->credits()->where('is_deleted', false)->get()->sum('balance'); // if($client->balance != $invoice_balance) // $invoice_balance -= $credit_balance;//doesn't make sense to remove the credit amount