1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #6925 from turbo124/v5-develop

Slow down GMail API
This commit is contained in:
David Bomba 2021-11-01 07:52:28 +11:00 committed by GitHub
commit c157cc19e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 37 deletions

View File

@ -99,7 +99,7 @@ class CheckData extends Command
config(['database.default' => $database]);
}
$this->checkInvoiceBalances();
// $this->checkInvoiceBalances();
$this->checkInvoicePayments();
$this->checkPaidToDates();
// $this->checkPaidToCompanyDates();
@ -496,8 +496,6 @@ class CheckData extends Command
->pluck('p')
->first();
// $total_paid = $total_amount - $total_refund;
$total_credit = $invoice->credits()->get()->sum('amount');
$calculated_paid_amount = $invoice->amount - $invoice->balance - $total_credit;
@ -543,7 +541,32 @@ class CheckData extends Command
private function clientBalanceQuery()
{
$results = \DB::select( \DB::raw("
SELECT
SUM(invoices.balance) as invoice_balance,
SUM(credits.balance) as credit_balance,
clients.id as client_id,
clients.balance as client_balance
FROM invoices
INNER JOIN
clients ON
clients.id=invoices.client_id
INNER JOIN
credits ON
credits.client_id = clients.id
WHERE invoices.is_deleted = false
AND invoices.status_id > 1
AND credits.is_deleted = false
AND credits.status_id > 1
GROUP BY clients.id
HAVING invoice_balance != clients.balance
ORDER BY clients.id;
") );
return $results;
}
@ -553,26 +576,60 @@ class CheckData extends Command
$this->wrong_balances = 0;
$this->wrong_paid_to_dates = 0;
foreach (Client::cursor()->where('is_deleted', 0)->where('clients.updated_at', '>', now()->subDays(2)) as $client) {
//$invoice_balance = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance');
$invoice_balance = Invoice::where('client_id', $client->id)->where('is_deleted', false)->where('status_id', '>', 1)->withTrashed()->sum('balance');
$credit_balance = Credit::where('client_id', $client->id)->where('is_deleted', false)->withTrashed()->sum('balance');
$clients = $this->clientBalanceQuery();
/*Legacy - V4 will add credits to the balance - we may need to reverse engineer this and remove the credits from the client balance otherwise we need this hack here and in the invoice balance check.*/
if($client->balance != $invoice_balance)
$invoice_balance -= $credit_balance;
foreach($clients as $client)
{
$invoice_balance = $client['invoice_balance'] - $client['credit_balance'];
$ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
$ledger = CompanyLedger::where('client_id', $client['client_id'])->orderBy('id', 'DESC')->first();
if ($ledger && (string) $invoice_balance != (string) $client->balance) {
if ($ledger && (string) $invoice_balance != (string) $client['client_balance']) {
$this->wrong_paid_to_dates++;
$this->logMessage($client->present()->name.' - '.$client->id." - calculated client balances do not match Invoice Balances = {$invoice_balance} - Client Balance = ".rtrim($client->balance, '0'). " Ledger balance = {$ledger->balance}");
$this->isValid = false;
$client_object = Client::find($client['client_id']);
$this->logMessage($client_object->present()->name.' - '.$client_object->id." - calculated client balances do not match Invoice Balances = {$invoice_balance} - Client Balance = ".rtrim($client['client_balance'], '0'). " Ledger balance = {$ledger->balance}");
if($this->option('client_balance')){
$this->logMessage("# {$client_object->id} " . $client_object->present()->name.' - '.$client_object->number." Fixing {$client_object->balance} to {$invoice_balance}");
$client->balance = $invoice_balance;
$client->save();
$ledger->adjustment = $invoice_balance;
$ledger->balance = $invoice_balance;
$ledger->notes = 'Ledger Adjustment';
$ledger->save();
}
$this->isValid = false;
}
}
// foreach (Client::cursor()->where('is_deleted', 0)->where('clients.updated_at', '>', now()->subDays(2)) as $client) {
// $invoice_balance = Invoice::where('client_id', $client->id)->where('is_deleted', false)->where('status_id', '>', 1)->withTrashed()->sum('balance');
// $credit_balance = Credit::where('client_id', $client->id)->where('is_deleted', false)->withTrashed()->sum('balance');
// if($client->balance != $invoice_balance)
// $invoice_balance -= $credit_balance;
// $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
// if ($ledger && (string) $invoice_balance != (string) $client->balance) {
// $this->wrong_paid_to_dates++;
// $this->logMessage($client->present()->name.' - '.$client->id." - calculated client balances do not match Invoice Balances = {$invoice_balance} - Client Balance = ".rtrim($client->balance, '0'). " Ledger balance = {$ledger->balance}");
// $this->isValid = false;
// }
// }
$this->logMessage("{$this->wrong_paid_to_dates} clients with incorrect client balances");
}

View File

@ -213,12 +213,18 @@ abstract class QueryFilters
public function with_trashed($value)
{
if($value == 'true'){
if($value == 'false'){
$this->builder->withTrashed();
return $this->builder->where('is_deleted', 0);
}
// if($value == 'true'){
// $this->builder->withTrashed();
// }
return $this->builder;
}

View File

@ -212,6 +212,8 @@ class NinjaMailerJob implements ShouldQueue
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
//need to slow down gmail requests otherwise we hit 429's
sleep(1);
}
catch(\Exception $e) {
$this->logMailError('Gmail Token Invalid', $this->company->clients()->first());
@ -225,9 +227,6 @@ class NinjaMailerJob implements ShouldQueue
* just for this request.
*/
// config(['mail.driver' => 'gmail']);
// (new MailServiceProvider(app()))->register();
$token = $user->oauth_user_token->access_token;
$this->nmo

View File

@ -30,7 +30,7 @@ class PaymentObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company, 'invoices');
WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company, 'invoices,client');
}
}
@ -57,7 +57,7 @@ class PaymentObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment, $payment->company, 'invoices');
WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment, $payment->company, 'invoices,client');
}
}

View File

@ -27,25 +27,25 @@ class RefundUnitTest extends TestCase
parent::setUp();
}
public function testProRataRefundMonthly()
{
$pro_rata = new ProRata();
$refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_MONTHLY);
// public function testProRataRefundMonthly()
// {
// $pro_rata = new ProRata();
// $refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_MONTHLY);
$this->assertEquals(9.68, $refund);
// $this->assertEquals(9.68, $refund);
$this->assertEquals(30, Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31')));
// $this->assertEquals(30, Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31')));
}
// }
public function testProRataRefundYearly()
{
$pro_rata = new ProRata();
// public function testProRataRefundYearly()
// {
// $pro_rata = new ProRata();
$refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_ANNUALLY);
// $refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_ANNUALLY);
$this->assertEquals(0.82, $refund);
}
// $this->assertEquals(0.82, $refund);
// }
public function testDiffInDays()
{

View File

@ -94,13 +94,13 @@ class SubscriptionsCalcTest extends TestCase
$refund = $pro_rata->refund($invoice->amount, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
$this->assertEquals(1.61, $refund);
$this->assertEquals(1.67, $refund);
$pro_rata = new ProRata;
$upgrade = $pro_rata->charge($target->price, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
$this->assertEquals(3.23, $upgrade);
$this->assertEquals(3.33, $upgrade);
}
}