1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Merge pull request #4180 from turbo124/v5-develop

Improve eager loading - first load
This commit is contained in:
David Bomba 2020-10-19 07:56:38 +11:00 committed by GitHub
commit 5a720325b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 26 deletions

View File

@ -1 +1 @@
5.0.18
5.0.19

View File

@ -18,6 +18,7 @@ use App\Models\Client;
use App\Models\ClientContact;
use App\Models\CompanyLedger;
use App\Models\Contact;
use App\Models\Credit;
use App\Models\Invitation;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
@ -303,7 +304,7 @@ class CheckData extends Command
if ($ledger && number_format($invoice_balance, 4) != number_format($client->balance, 4)) {
$wrong_balances++;
$this->logMessage($client->present()->name.' - '.$client->id." - balances do not match Invoice Balance = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance}");
$this->logMessage($client->present()->name.' - '.$client->number." - Balance Failure - Invoice Balances = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance}");
$this->isValid = false;
}
@ -315,8 +316,9 @@ class CheckData extends Command
private function checkPaidToDates()
{
$wrong_paid_to_dates = 0;
$credit_total_applied = 0;
Client::withTrashed()->cursor()->each(function ($client) use ($wrong_paid_to_dates) {
Client::withTrashed()->cursor()->each(function ($client) use ($wrong_paid_to_dates, $credit_total_applied) {
$total_invoice_payments = 0;
foreach ($client->invoices as $invoice) {
@ -326,6 +328,15 @@ class CheckData extends Command
$total_invoice_payments += ($total_amount - $total_refund);
}
foreach($client->payments as $payment)
{
$credit_total_applied += $payment->paymentables->where('paymentable_type', App\Models\Credit::class)->sum(\DB::raw('amount'));
}
$total_invoice_payments += $credit_total_applied;
info("total invoice payments = {$total_invoice_payments} with client paid to date of of {$client->paid_to_date}");
if (round($total_invoice_payments, 2) != round($client->paid_to_date, 2)) {
$wrong_paid_to_dates++;
@ -352,11 +363,12 @@ class CheckData extends Command
$total_credit = $invoice->credits->sum('amount');
$total_paid = $total_amount - $total_refund;
$calculated_paid_amount = $invoice->amount - $invoice->balance - $total_credit;
if ($total_paid != ($invoice->amount - $invoice->balance - $total_credit)) {
if ((string)$total_paid != (string)($invoice->amount - $invoice->balance - $total_credit)) {
$wrong_balances++;
$this->logMessage($client->present()->name.' - '.$client->id." - balances do not match Invoice Amount = {$invoice->amount} - Invoice Balance = {$invoice->balance} Total paid = {$total_paid}");
$this->logMessage($client->present()->name.' - '.$client->id." - Total Amount = {$total_amount} != Calculated Total = {$calculated_paid_amount} - Total Refund = {$total_refund} Total credit = {$total_credit}");
$this->isValid = false;
}
@ -373,22 +385,22 @@ class CheckData extends Command
foreach (Client::cursor() as $client) {
$invoice_balance = $client->invoices->sum('balance');
$invoice_amounts = $client->invoices->sum('amount') - $invoice_balance;
// $invoice_amounts = $client->invoices->sum('amount') - $invoice_balance;
$credit_amounts = 0;
// $credit_amounts = 0;
foreach ($client->invoices as $invoice) {
$credit_amounts += $invoice->credits->sum('amount');
}
// foreach ($client->invoices as $invoice) {
// $credit_amounts += $invoice->credits->sum('amount');
// }
/*To handle invoice reversals, we need to "ADD BACK" the credit amounts here*/
$client_paid_to_date = $client->paid_to_date + $credit_amounts;
// /*To handle invoice reversals, we need to "ADD BACK" the credit amounts here*/
// $client_paid_to_date = $client->paid_to_date + $credit_amounts;
$ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
if ($ledger && (string) $invoice_amounts != (string) $client_paid_to_date) {
if ($ledger && (string) $invoice_balance != (string) $client->balance) {
$wrong_paid_to_dates++;
$this->logMessage($client->present()->name.' - '.$client->id." - client paid to dates do not match {$invoice_amounts} - ".rtrim($client_paid_to_date, '0'));
$this->logMessage($client->present()->name.' - '.$client->id." - client paid to dates do not match {$invoice_balance} - ".rtrim($client->balance, '0'));
$this->isValid = false;
}

View File

@ -61,7 +61,7 @@ class BaseController extends Controller
private $first_load = [
'account',
'user.company_user',
'token',
'token.company_user',
'company.activities',
'company.users.company_user',
'company.tax_rates',
@ -81,6 +81,7 @@ class BaseController extends Controller
'company.recurring_invoices.invitations.company',
'company.recurring_invoices.documents',
'company.payments.paymentables',
'company.payments.documents',
'company.quotes.invitations.contact',
'company.quotes.invitations.company',
'company.quotes.documents',
@ -89,10 +90,10 @@ class BaseController extends Controller
'company.credits.documents',
'company.payment_terms.company',
'company.vendors.contacts',
'company.expenses',
'company.tasks',
'company.projects',
'company.designs',
'company.expenses.documents',
'company.tasks.documents',
'company.projects.documents',
'company.designs.company',
'company.documents',
'company.webhooks',
'company.tokens_hashed',
@ -255,7 +256,7 @@ class BaseController extends Controller
$query->where('updated_at', '>=', $updated_at);
},
'company.designs'=> function ($query) use ($updated_at) {
$query->where('updated_at', '>=', $updated_at);
$query->where('updated_at', '>=', $updated_at)->with('company');
},
]
);

View File

@ -52,7 +52,7 @@ class QueryLogging
Log::info($request->method().' - '.$request->url().": $count queries - ".$time);
// if($count > 50)
// if($count > 50)
// Log::info($queries);
}
}

View File

@ -105,11 +105,10 @@ class Import implements ShouldQueue
'invoices',
'recurring_invoices',
'quotes',
'payments',
'credits',
'payments',
'company_gateways',
'client_gateway_tokens',
// //'documents',
];
@ -722,7 +721,14 @@ class Import implements ShouldQueue
if (isset($modified['invoices'])) {
foreach ($modified['invoices'] as $key => $invoice) {
$modified['invoices'][$key]['invoice_id'] = $this->transformId('invoices', $invoice['invoice_id']);
if($modified['amount'] >= 0)
$modified['invoices'][$key]['invoice_id'] = $this->transformId('invoices', $invoice['invoice_id']);
else{
$modified['credits'][$key]['credit_id'] = $this->transformId('credits', $invoice['invoice_id']);
$modified['credits'][$key]['amount'] = $modified['invoices'][$key]['amount'];
}
}
}

View File

@ -101,6 +101,7 @@ class PaymentMigrationRepository extends BaseRepository
$invoice_totals = 0;
$credit_totals = 0;
$invoices = false;
/*Iterate through invoices and apply payments*/
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {
@ -116,6 +117,20 @@ class PaymentMigrationRepository extends BaseRepository
});
}
if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) {
$credit_totals = array_sum(array_column($data['credits'], 'amount'));
$credits = Credit::whereIn('id', array_column($data['credits'], 'credit_id'))->get();
$payment->credits()->saveMany($credits);
$payment->credits->each(function ($cre) use ($credit_totals) {
$cre->pivot->amount = $credit_totals;
$cre->pivot->save();
});
}
$fields = new \stdClass;
$fields->payment_id = $payment->id;
@ -129,7 +144,7 @@ class PaymentMigrationRepository extends BaseRepository
$this->activity_repo->save($fields, $invoice, Ninja::eventVars());
}
if (count($invoices) == 0) {
if ($invoices && count($invoices) == 0) {
$this->activity_repo->save($fields, $payment, Ninja::eventVars());
}

View File

@ -12,7 +12,7 @@ return [
'require_https' => env('REQUIRE_HTTPS', true),
'app_url' => rtrim(env('APP_URL', ''), '/').'/',
'app_domain' => env('APP_DOMAIN', ''),
'app_version' => '5.0.18',
'app_version' => '5.0.19',
'minimum_client_version' => '5.0.16',
'terms_version' => '1.0.1',
'api_secret' => env('API_SECRET', ''),