mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-12 14:12:44 +01:00
Enabled automating data check script
This commit is contained in:
parent
852d97fd27
commit
a1f59619ef
@ -1,6 +1,7 @@
|
|||||||
<?php namespace App\Console\Commands;
|
<?php namespace App\Console\Commands;
|
||||||
|
|
||||||
use DB;
|
use DB;
|
||||||
|
use Mail;
|
||||||
use Carbon;
|
use Carbon;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
@ -51,9 +52,12 @@ class CheckData extends Command {
|
|||||||
*/
|
*/
|
||||||
protected $description = 'Check/fix data';
|
protected $description = 'Check/fix data';
|
||||||
|
|
||||||
|
protected $log = '';
|
||||||
|
protected $isValid = true;
|
||||||
|
|
||||||
public function fire()
|
public function fire()
|
||||||
{
|
{
|
||||||
$this->info(date('Y-m-d') . ' Running CheckData...');
|
$this->logMessage(date('Y-m-d') . ' Running CheckData...');
|
||||||
|
|
||||||
if (!$this->option('client_id')) {
|
if (!$this->option('client_id')) {
|
||||||
$this->checkPaidToDate();
|
$this->checkPaidToDate();
|
||||||
@ -66,7 +70,21 @@ class CheckData extends Command {
|
|||||||
$this->checkAccountData();
|
$this->checkAccountData();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->info('Done');
|
$this->logMessage('Done');
|
||||||
|
$errorEmail = env('ERROR_EMAIL');
|
||||||
|
|
||||||
|
if ( ! $this->isValid && $errorEmail) {
|
||||||
|
Mail::raw($this->log, function ($message) use ($errorEmail) {
|
||||||
|
$message->to($errorEmail)
|
||||||
|
->from(CONTACT_EMAIL)
|
||||||
|
->subject('Check-Data');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function logMessage($str)
|
||||||
|
{
|
||||||
|
$this->log .= $str . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkBlankInvoiceHistory()
|
private function checkBlankInvoiceHistory()
|
||||||
@ -76,7 +94,11 @@ class CheckData extends Command {
|
|||||||
->where('json_backup', '=', '')
|
->where('json_backup', '=', '')
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
$this->info($count . ' activities with blank invoice backup');
|
if ($count > 0) {
|
||||||
|
$this->isValid = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->logMessage($count . ' activities with blank invoice backup');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkAccountData()
|
private function checkAccountData()
|
||||||
@ -131,7 +153,8 @@ class CheckData extends Command {
|
|||||||
->get(["{$table}.id", 'clients.account_id', 'clients.user_id']);
|
->get(["{$table}.id", 'clients.account_id', 'clients.user_id']);
|
||||||
|
|
||||||
if (count($records)) {
|
if (count($records)) {
|
||||||
$this->info(count($records) . " {$table} records with incorrect {$entityType} account id");
|
$this->isValid = false;
|
||||||
|
$this->logMessage(count($records) . " {$table} records with incorrect {$entityType} account id");
|
||||||
|
|
||||||
if ($this->option('fix') == 'true') {
|
if ($this->option('fix') == 'true') {
|
||||||
foreach ($records as $record) {
|
foreach ($records as $record) {
|
||||||
@ -161,7 +184,11 @@ class CheckData extends Command {
|
|||||||
->groupBy('clients.id')
|
->groupBy('clients.id')
|
||||||
->havingRaw('clients.paid_to_date != sum(payments.amount - payments.refunded) and clients.paid_to_date != 999999999.9999')
|
->havingRaw('clients.paid_to_date != sum(payments.amount - payments.refunded) and clients.paid_to_date != 999999999.9999')
|
||||||
->get(['clients.id', 'clients.paid_to_date', DB::raw('sum(payments.amount) as amount')]);
|
->get(['clients.id', 'clients.paid_to_date', DB::raw('sum(payments.amount) as amount')]);
|
||||||
$this->info(count($clients) . ' clients with incorrect paid to date');
|
$this->logMessage(count($clients) . ' clients with incorrect paid to date');
|
||||||
|
|
||||||
|
if (count($clients) > 0) {
|
||||||
|
$this->isValid = false;
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->option('fix') == 'true') {
|
if ($this->option('fix') == 'true') {
|
||||||
foreach ($clients as $client) {
|
foreach ($clients as $client) {
|
||||||
@ -178,6 +205,7 @@ class CheckData extends Command {
|
|||||||
$clients = DB::table('clients')
|
$clients = DB::table('clients')
|
||||||
->join('invoices', 'invoices.client_id', '=', 'clients.id')
|
->join('invoices', 'invoices.client_id', '=', 'clients.id')
|
||||||
->join('accounts', 'accounts.id', '=', 'clients.account_id')
|
->join('accounts', 'accounts.id', '=', 'clients.account_id')
|
||||||
|
->where('accounts.id', '!=', 20432)
|
||||||
->where('clients.is_deleted', '=', 0)
|
->where('clients.is_deleted', '=', 0)
|
||||||
->where('invoices.is_deleted', '=', 0)
|
->where('invoices.is_deleted', '=', 0)
|
||||||
->where('invoices.invoice_type_id', '=', INVOICE_TYPE_STANDARD)
|
->where('invoices.invoice_type_id', '=', INVOICE_TYPE_STANDARD)
|
||||||
@ -187,14 +215,18 @@ class CheckData extends Command {
|
|||||||
if ($this->option('client_id')) {
|
if ($this->option('client_id')) {
|
||||||
$clients->where('clients.id', '=', $this->option('client_id'));
|
$clients->where('clients.id', '=', $this->option('client_id'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$clients = $clients->groupBy('clients.id', 'clients.balance', 'clients.created_at')
|
$clients = $clients->groupBy('clients.id', 'clients.balance', 'clients.created_at')
|
||||||
->orderBy('accounts.company_id', 'DESC')
|
->orderBy('accounts.company_id', 'DESC')
|
||||||
->get(['accounts.company_id', 'clients.account_id', 'clients.id', 'clients.balance', 'clients.paid_to_date', DB::raw('sum(invoices.balance) actual_balance')]);
|
->get(['accounts.company_id', 'clients.account_id', 'clients.id', 'clients.balance', 'clients.paid_to_date', DB::raw('sum(invoices.balance) actual_balance')]);
|
||||||
$this->info(count($clients) . ' clients with incorrect balance/activities');
|
$this->logMessage(count($clients) . ' clients with incorrect balance/activities');
|
||||||
|
|
||||||
|
if (count($clients) > 0) {
|
||||||
|
$this->isValid = false;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($clients as $client) {
|
foreach ($clients as $client) {
|
||||||
$this->info("=== Company: {$client->company_id} Account:{$client->account_id} Client:{$client->id} Balance:{$client->balance} Actual Balance:{$client->actual_balance} ===");
|
$this->logMessage("=== Company: {$client->company_id} Account:{$client->account_id} Client:{$client->id} Balance:{$client->balance} Actual Balance:{$client->actual_balance} ===");
|
||||||
$foundProblem = false;
|
$foundProblem = false;
|
||||||
$lastBalance = 0;
|
$lastBalance = 0;
|
||||||
$lastAdjustment = 0;
|
$lastAdjustment = 0;
|
||||||
@ -204,7 +236,7 @@ class CheckData extends Command {
|
|||||||
->where('client_id', '=', $client->id)
|
->where('client_id', '=', $client->id)
|
||||||
->orderBy('activities.id')
|
->orderBy('activities.id')
|
||||||
->get(['activities.id', 'activities.created_at', 'activities.activity_type_id', 'activities.adjustment', 'activities.balance', 'activities.invoice_id']);
|
->get(['activities.id', 'activities.created_at', 'activities.activity_type_id', 'activities.adjustment', 'activities.balance', 'activities.invoice_id']);
|
||||||
//$this->info(var_dump($activities));
|
//$this->logMessage(var_dump($activities));
|
||||||
|
|
||||||
foreach ($activities as $activity) {
|
foreach ($activities as $activity) {
|
||||||
|
|
||||||
@ -251,19 +283,19 @@ class CheckData extends Command {
|
|||||||
|
|
||||||
// **Fix for ninja invoices which didn't have the invoice_type_id value set
|
// **Fix for ninja invoices which didn't have the invoice_type_id value set
|
||||||
if ($noAdjustment && $client->account_id == 20432) {
|
if ($noAdjustment && $client->account_id == 20432) {
|
||||||
$this->info("No adjustment for ninja invoice");
|
$this->logMessage("No adjustment for ninja invoice");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
$clientFix += $invoice->amount;
|
$clientFix += $invoice->amount;
|
||||||
$activityFix = $invoice->amount;
|
$activityFix = $invoice->amount;
|
||||||
// **Fix for allowing converting a recurring invoice to a normal one without updating the balance**
|
// **Fix for allowing converting a recurring invoice to a normal one without updating the balance**
|
||||||
} elseif ($noAdjustment && $invoice->invoice_type_id == INVOICE_TYPE_STANDARD && !$invoice->is_recurring) {
|
} elseif ($noAdjustment && $invoice->invoice_type_id == INVOICE_TYPE_STANDARD && !$invoice->is_recurring) {
|
||||||
$this->info("No adjustment for new invoice:{$activity->invoice_id} amount:{$invoice->amount} invoiceTypeId:{$invoice->invoice_type_id} isRecurring:{$invoice->is_recurring}");
|
$this->logMessage("No adjustment for new invoice:{$activity->invoice_id} amount:{$invoice->amount} invoiceTypeId:{$invoice->invoice_type_id} isRecurring:{$invoice->is_recurring}");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
$clientFix += $invoice->amount;
|
$clientFix += $invoice->amount;
|
||||||
$activityFix = $invoice->amount;
|
$activityFix = $invoice->amount;
|
||||||
// **Fix for updating balance when creating a quote or recurring invoice**
|
// **Fix for updating balance when creating a quote or recurring invoice**
|
||||||
} elseif ($activity->adjustment != 0 && ($invoice->invoice_type_id == INVOICE_TYPE_QUOTE || $invoice->is_recurring)) {
|
} elseif ($activity->adjustment != 0 && ($invoice->invoice_type_id == INVOICE_TYPE_QUOTE || $invoice->is_recurring)) {
|
||||||
$this->info("Incorrect adjustment for new invoice:{$activity->invoice_id} adjustment:{$activity->adjustment} invoiceTypeId:{$invoice->invoice_type_id} isRecurring:{$invoice->is_recurring}");
|
$this->logMessage("Incorrect adjustment for new invoice:{$activity->invoice_id} adjustment:{$activity->adjustment} invoiceTypeId:{$invoice->invoice_type_id} isRecurring:{$invoice->is_recurring}");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
$clientFix -= $activity->adjustment;
|
$clientFix -= $activity->adjustment;
|
||||||
$activityFix = 0;
|
$activityFix = 0;
|
||||||
@ -271,7 +303,7 @@ class CheckData extends Command {
|
|||||||
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_DELETE_INVOICE) {
|
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_DELETE_INVOICE) {
|
||||||
// **Fix for updating balance when deleting a recurring invoice**
|
// **Fix for updating balance when deleting a recurring invoice**
|
||||||
if ($activity->adjustment != 0 && $invoice->is_recurring) {
|
if ($activity->adjustment != 0 && $invoice->is_recurring) {
|
||||||
$this->info("Incorrect adjustment for deleted invoice adjustment:{$activity->adjustment}");
|
$this->logMessage("Incorrect adjustment for deleted invoice adjustment:{$activity->adjustment}");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
if ($activity->balance != $lastBalance) {
|
if ($activity->balance != $lastBalance) {
|
||||||
$clientFix -= $activity->adjustment;
|
$clientFix -= $activity->adjustment;
|
||||||
@ -281,7 +313,7 @@ class CheckData extends Command {
|
|||||||
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_ARCHIVE_INVOICE) {
|
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_ARCHIVE_INVOICE) {
|
||||||
// **Fix for updating balance when archiving an invoice**
|
// **Fix for updating balance when archiving an invoice**
|
||||||
if ($activity->adjustment != 0 && !$invoice->is_recurring) {
|
if ($activity->adjustment != 0 && !$invoice->is_recurring) {
|
||||||
$this->info("Incorrect adjustment for archiving invoice adjustment:{$activity->adjustment}");
|
$this->logMessage("Incorrect adjustment for archiving invoice adjustment:{$activity->adjustment}");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
$activityFix = 0;
|
$activityFix = 0;
|
||||||
$clientFix += $activity->adjustment;
|
$clientFix += $activity->adjustment;
|
||||||
@ -289,12 +321,12 @@ class CheckData extends Command {
|
|||||||
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_UPDATE_INVOICE) {
|
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_UPDATE_INVOICE) {
|
||||||
// **Fix for updating balance when updating recurring invoice**
|
// **Fix for updating balance when updating recurring invoice**
|
||||||
if ($activity->adjustment != 0 && $invoice->is_recurring) {
|
if ($activity->adjustment != 0 && $invoice->is_recurring) {
|
||||||
$this->info("Incorrect adjustment for updated recurring invoice adjustment:{$activity->adjustment}");
|
$this->logMessage("Incorrect adjustment for updated recurring invoice adjustment:{$activity->adjustment}");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
$clientFix -= $activity->adjustment;
|
$clientFix -= $activity->adjustment;
|
||||||
$activityFix = 0;
|
$activityFix = 0;
|
||||||
} else if ((strtotime($activity->created_at) - strtotime($lastCreatedAt) <= 1) && $activity->adjustment > 0 && $activity->adjustment == $lastAdjustment) {
|
} else if ((strtotime($activity->created_at) - strtotime($lastCreatedAt) <= 1) && $activity->adjustment > 0 && $activity->adjustment == $lastAdjustment) {
|
||||||
$this->info("Duplicate adjustment for updated invoice adjustment:{$activity->adjustment}");
|
$this->logMessage("Duplicate adjustment for updated invoice adjustment:{$activity->adjustment}");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
$clientFix -= $activity->adjustment;
|
$clientFix -= $activity->adjustment;
|
||||||
$activityFix = 0;
|
$activityFix = 0;
|
||||||
@ -302,7 +334,7 @@ class CheckData extends Command {
|
|||||||
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_UPDATE_QUOTE) {
|
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_UPDATE_QUOTE) {
|
||||||
// **Fix for updating balance when updating a quote**
|
// **Fix for updating balance when updating a quote**
|
||||||
if ($activity->balance != $lastBalance) {
|
if ($activity->balance != $lastBalance) {
|
||||||
$this->info("Incorrect adjustment for updated quote adjustment:{$activity->adjustment}");
|
$this->logMessage("Incorrect adjustment for updated quote adjustment:{$activity->adjustment}");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
$clientFix += $lastBalance - $activity->balance;
|
$clientFix += $lastBalance - $activity->balance;
|
||||||
$activityFix = 0;
|
$activityFix = 0;
|
||||||
@ -310,7 +342,7 @@ class CheckData extends Command {
|
|||||||
} else if ($activity->activity_type_id == ACTIVITY_TYPE_DELETE_PAYMENT) {
|
} else if ($activity->activity_type_id == ACTIVITY_TYPE_DELETE_PAYMENT) {
|
||||||
// **Fix for deleting payment after deleting invoice**
|
// **Fix for deleting payment after deleting invoice**
|
||||||
if ($activity->adjustment != 0 && $invoice->is_deleted && $activity->created_at > $invoice->deleted_at) {
|
if ($activity->adjustment != 0 && $invoice->is_deleted && $activity->created_at > $invoice->deleted_at) {
|
||||||
$this->info("Incorrect adjustment for deleted payment adjustment:{$activity->adjustment}");
|
$this->logMessage("Incorrect adjustment for deleted payment adjustment:{$activity->adjustment}");
|
||||||
$foundProblem = true;
|
$foundProblem = true;
|
||||||
$activityFix = 0;
|
$activityFix = 0;
|
||||||
$clientFix -= $activity->adjustment;
|
$clientFix -= $activity->adjustment;
|
||||||
@ -339,7 +371,7 @@ class CheckData extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($activity->balance + $clientFix != $client->actual_balance) {
|
if ($activity->balance + $clientFix != $client->actual_balance) {
|
||||||
$this->info("** Creating 'recovered update' activity **");
|
$this->logMessage("** Creating 'recovered update' activity **");
|
||||||
if ($this->option('fix') == 'true') {
|
if ($this->option('fix') == 'true') {
|
||||||
DB::table('activities')->insert([
|
DB::table('activities')->insert([
|
||||||
'created_at' => new Carbon,
|
'created_at' => new Carbon,
|
||||||
@ -353,7 +385,7 @@ class CheckData extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$data = ['balance' => $client->actual_balance];
|
$data = ['balance' => $client->actual_balance];
|
||||||
$this->info("Corrected balance:{$client->actual_balance}");
|
$this->logMessage("Corrected balance:{$client->actual_balance}");
|
||||||
if ($this->option('fix') == 'true') {
|
if ($this->option('fix') == 'true') {
|
||||||
DB::table('clients')
|
DB::table('clients')
|
||||||
->where('id', $client->id)
|
->where('id', $client->id)
|
||||||
|
Loading…
Reference in New Issue
Block a user