From 61788b77f1641a4a96e4f1d2ed6f1e188cc1b684 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2021 08:58:53 +1100 Subject: [PATCH 1/5] Add PostMark --- composer.json | 6 ++-- composer.lock | 78 ++++++++++++++++++++++----------------------- config/services.php | 7 ++-- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/composer.json b/composer.json index 6ca6f1b0cedc..82fb2f764bb7 100644 --- a/composer.json +++ b/composer.json @@ -64,7 +64,8 @@ "stripe/stripe-php": "^7.50", "turbo124/beacon": "^1", "turbo124/laravel-gmail": "^5.0", - "webpatser/laravel-countries": "dev-master#75992ad" + "webpatser/laravel-countries": "dev-master#75992ad", + "wildbit/postmark-php": "^4.0" }, "require-dev": { "php": "^7.4", @@ -79,8 +80,7 @@ "mockery/mockery": "^1.3.1", "nunomaduro/collision": "^5.0", "phpunit/phpunit": "^9.0", - "vimeo/psalm": "^4.0", - "wildbit/postmark-php": "^4.0" + "vimeo/psalm": "^4.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 99adca936662..ef67f580749a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "49484c0e046a5663433fef2353ee6ee8", + "content-hash": "559255a3f2573bb2598a3f97f926c088", "packages": [ { "name": "asgrim/ofxparser", @@ -9735,6 +9735,44 @@ "source": "https://github.com/webpatser/laravel-countries" }, "time": "2019-07-12T14:06:05+00:00" + }, + { + "name": "wildbit/postmark-php", + "version": "4.0.0", + "source": { + "type": "git", + "url": "https://github.com/wildbit/postmark-php.git", + "reference": "136e3d0884411ca23d0bac02470cfa1d14be676c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wildbit/postmark-php/zipball/136e3d0884411ca23d0bac02470cfa1d14be676c", + "reference": "136e3d0884411ca23d0bac02470cfa1d14be676c", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.0|^7.0", + "php": ">=7.0.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "Postmark\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "The officially supported client for Postmark (http://postmarkapp.com)", + "support": { + "issues": "https://github.com/wildbit/postmark-php/issues", + "source": "https://github.com/wildbit/postmark-php/tree/4.0.0" + }, + "time": "2020-09-10T16:36:51+00:00" } ], "packages-dev": [ @@ -13629,44 +13667,6 @@ }, "time": "2015-12-17T08:42:14+00:00" }, - { - "name": "wildbit/postmark-php", - "version": "4.0.0", - "source": { - "type": "git", - "url": "https://github.com/wildbit/postmark-php.git", - "reference": "136e3d0884411ca23d0bac02470cfa1d14be676c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/wildbit/postmark-php/zipball/136e3d0884411ca23d0bac02470cfa1d14be676c", - "reference": "136e3d0884411ca23d0bac02470cfa1d14be676c", - "shasum": "" - }, - "require": { - "guzzlehttp/guzzle": "^6.0|^7.0", - "php": ">=7.0.0" - }, - "require-dev": { - "phpunit/phpunit": "^6.0.0" - }, - "type": "library", - "autoload": { - "psr-0": { - "Postmark\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "The officially supported client for Postmark (http://postmarkapp.com)", - "support": { - "issues": "https://github.com/wildbit/postmark-php/issues", - "source": "https://github.com/wildbit/postmark-php/tree/4.0.0" - }, - "time": "2020-09-10T16:36:51+00:00" - }, { "name": "zircote/swagger-php", "version": "3.1.0", diff --git a/config/services.php b/config/services.php index 517918628814..ba7b192c3dcc 100644 --- a/config/services.php +++ b/config/services.php @@ -36,10 +36,9 @@ return [ 'gmail' => [ 'token' => '', ], - 'postmark' => env('POSTMARK_API_TOKEN', ''), - 'postmark_ticket' => env('POSTMARK_API_TICKET_TOKEN'), - 'postmark_ticket_2' => env('POSTMARK_API_TICKET_TOKEN_2'), - + 'postmark' => [ + 'token' => env('POSTMARK_API_TOKEN', ''), + ], 'stripe' => [ 'model' => App\Models\User::class, 'key' => env('STRIPE_KEY'), From dc9feee9ff16a79158b183a90b4359922689e8e6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2021 09:25:21 +1100 Subject: [PATCH 2/5] Fixes for dependencies --- composer.json | 2 +- composer.lock | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 82fb2f764bb7..c46ad17ce94a 100644 --- a/composer.json +++ b/composer.json @@ -65,7 +65,7 @@ "turbo124/beacon": "^1", "turbo124/laravel-gmail": "^5.0", "webpatser/laravel-countries": "dev-master#75992ad", - "wildbit/postmark-php": "^4.0" + "wildbit/swiftmailer-postmark": "^3.3" }, "require-dev": { "php": "^7.4", diff --git a/composer.lock b/composer.lock index ef67f580749a..3415f0b38265 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "559255a3f2573bb2598a3f97f926c088", + "content-hash": "7b93ec16ae5791e0767c92eaf7061cc7", "packages": [ { "name": "asgrim/ofxparser", @@ -9737,22 +9737,22 @@ "time": "2019-07-12T14:06:05+00:00" }, { - "name": "wildbit/postmark-php", - "version": "4.0.0", + "name": "wildbit/swiftmailer-postmark", + "version": "3.3.0", "source": { "type": "git", - "url": "https://github.com/wildbit/postmark-php.git", - "reference": "136e3d0884411ca23d0bac02470cfa1d14be676c" + "url": "https://github.com/wildbit/swiftmailer-postmark.git", + "reference": "44ccab7834de8b220d292647ecb2cb683f9962ee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/wildbit/postmark-php/zipball/136e3d0884411ca23d0bac02470cfa1d14be676c", - "reference": "136e3d0884411ca23d0bac02470cfa1d14be676c", + "url": "https://api.github.com/repos/wildbit/swiftmailer-postmark/zipball/44ccab7834de8b220d292647ecb2cb683f9962ee", + "reference": "44ccab7834de8b220d292647ecb2cb683f9962ee", "shasum": "" }, "require": { "guzzlehttp/guzzle": "^6.0|^7.0", - "php": ">=7.0.0" + "swiftmailer/swiftmailer": "^6.0.0" }, "require-dev": { "phpunit/phpunit": "^6.0.0" @@ -9767,12 +9767,18 @@ "license": [ "MIT" ], - "description": "The officially supported client for Postmark (http://postmarkapp.com)", + "authors": [ + { + "name": "Postmark", + "email": "support@postmarkapp.com" + } + ], + "description": "A Swiftmailer Transport for Postmark.", "support": { - "issues": "https://github.com/wildbit/postmark-php/issues", - "source": "https://github.com/wildbit/postmark-php/tree/4.0.0" + "issues": "https://github.com/wildbit/swiftmailer-postmark/issues", + "source": "https://github.com/wildbit/swiftmailer-postmark/tree/3.3.0" }, - "time": "2020-09-10T16:36:51+00:00" + "time": "2020-09-10T10:54:20+00:00" } ], "packages-dev": [ From 313488dc60a9715817ba88ce07b30d436948e470 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2021 12:59:30 +1100 Subject: [PATCH 3/5] Working on handling check data within migrations" --- app/Console/Commands/CheckData.php | 22 +++---- app/Jobs/Mail/BaseMailerJob.php | 5 +- app/Jobs/Ninja/CheckCompanyData.php | 60 +++++++++++-------- app/Jobs/Util/Import.php | 9 ++- app/Jobs/Util/StartMigration.php | 5 +- app/Mail/MigrationCompleted.php | 8 ++- app/Mail/MigrationFailed.php | 1 + .../views/email/import/completed.blade.php | 4 ++ 8 files changed, 70 insertions(+), 44 deletions(-) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 06dec862bbad..fb8c092fc375 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -73,6 +73,7 @@ class CheckData extends Command protected $description = 'Check/fix data'; protected $log = ''; + protected $isValid = true; public function handle() @@ -90,13 +91,10 @@ class CheckData extends Command $this->checkContacts(); $this->checkCompanyData(); - //$this->checkLogoFiles(); if (! $this->option('client_id')) { $this->checkOAuth(); - //$this->checkInvitations(); - - $this->checkFailedJobs(); + //$this->checkFailedJobs(); } $this->logMessage('Done: '.strtoupper($this->isValid ? Account::RESULT_SUCCESS : Account::RESULT_FAILURE)); @@ -298,8 +296,6 @@ class CheckData extends Command $total_invoice_payments = 0; foreach ($client->invoices->where('is_deleted', false)->where('status_id', '>', 1) as $invoice) { - // $total_amount = $invoice->payments->whereNull('deleted_at')->sum('pivot.amount'); - // $total_refund = $invoice->payments->whereNull('deleted_at')->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])->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'); @@ -307,15 +303,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')); - } + //10/02/21 + // foreach ($client->payments as $payment) { + // $credit_total_applied += $payment->paymentables->where('paymentable_type', App\Models\Credit::class)->sum(DB::raw('amount')); + // } - if ($credit_total_applied < 0) { - $total_invoice_payments += $credit_total_applied; - } //todo this is contentious + // if ($credit_total_applied < 0) { + // $total_invoice_payments += $credit_total_applied; + // } - // nlog("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++; diff --git a/app/Jobs/Mail/BaseMailerJob.php b/app/Jobs/Mail/BaseMailerJob.php index d65d39b2bca5..5be8542e9038 100644 --- a/app/Jobs/Mail/BaseMailerJob.php +++ b/app/Jobs/Mail/BaseMailerJob.php @@ -42,7 +42,10 @@ class BaseMailerJob implements ShouldQueue public function setMailDriver() { + /* Singletons need to be rebooted each time just in case our Locale is changing*/ App::forgetInstance('translator'); + + /* Inject custom translations if any exist */ Lang::replace(Ninja::transformTranslations($this->settings)); switch ($this->settings->email_sending_method) { @@ -96,7 +99,7 @@ class BaseMailerJob implements ShouldQueue public function failed($exception = null) { - nlog('the job failed'); + nlog('mailer job failed'); nlog($exception->getMessage()); $job_failure = new EmailFailure(); diff --git a/app/Jobs/Ninja/CheckCompanyData.php b/app/Jobs/Ninja/CheckCompanyData.php index 8561171ff6a9..14e250e27f6d 100644 --- a/app/Jobs/Ninja/CheckCompanyData.php +++ b/app/Jobs/Ninja/CheckCompanyData.php @@ -72,8 +72,16 @@ class CheckCompanyData implements ShouldQueue Cache::put($this->hash, $cache_instance, now()->addMinutes(30)); - nlog(Cache::get($this->hash)); + + nlog($this->company_data); + + if(!$this->isValid) + $this->company_data['status'] = 'errors'; + else + $this->company_data['status'] = 'success'; + + return $this->company_data; } @@ -90,17 +98,18 @@ class CheckCompanyData implements ShouldQueue foreach ($this->company->clients->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'); + //$credit_balance = $client->credits->where('is_deleted', false)->sum('balance'); - if($client->balance != $invoice_balance) - $invoice_balance -= $credit_balance;//doesn't make sense to remove the credit amount + // if($client->balance != $invoice_balance) + // $invoice_balance -= $credit_balance;//doesn't make sense to remove the credit amount $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); if ($ledger && number_format($invoice_balance, 4) != number_format($client->balance, 4)) { + $wrong_balances++; - $this->company_data[] = "# {$client->id} " . $client->present()->name.' - '.$client->number." - Balance Failure - Invoice Balances = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance}"; + $this->company_data[] = "# {$client->id} " . $client->present()->name.' - '.$client->number." - Balance Failure - Invoice Balances = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance} \n"; $this->isValid = false; } @@ -126,14 +135,14 @@ class CheckCompanyData implements ShouldQueue if ((string)$total_paid != (string)($invoice->amount - $invoice->balance - $total_credit)) { $wrong_balances++; - $this->company_data[] = $client->present()->name.' - '.$client->id." - Total Amount = {$total_amount} != Calculated Total = {$calculated_paid_amount} - Total Refund = {$total_refund} Total credit = {$total_credit}"; + $this->company_data[] = $client->present()->name.' - '.$client->id." - Total Amount = {$total_amount} != Calculated Total = {$calculated_paid_amount} - Total Refund = {$total_refund} Total credit = {$total_credit}\n"; $this->isValid = false; } }); }); - $this->company_data[] = "{$wrong_balances} clients with incorrect invoice balances"; + $this->company_data[] = "{$wrong_balances} clients with incorrect invoice balances\n"; } private function checkPaidToDates() @@ -145,8 +154,7 @@ class CheckCompanyData implements ShouldQueue $total_invoice_payments = 0; foreach ($client->invoices->where('is_deleted', false)->where('status_id', '>', 1) as $invoice) { - // $total_amount = $invoice->payments->whereNull('deleted_at')->sum('pivot.amount'); - // $total_refund = $invoice->payments->whereNull('deleted_at')->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])->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'); @@ -154,20 +162,21 @@ class CheckCompanyData implements ShouldQueue $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')); - } + //10/02/21 + // foreach ($client->payments as $payment) { + // $credit_total_applied += $payment->paymentables->where('paymentable_type', App\Models\Credit::class)->sum(DB::raw('amount')); + // } - if ($credit_total_applied < 0) { - $total_invoice_payments += $credit_total_applied; - } //todo this is contentious + // if ($credit_total_applied < 0) { + // $total_invoice_payments += $credit_total_applied; + // } //todo this is contentious // nlog("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++; - $this->company_data[] = $client->present()->name.'id = # '.$client->id." - Paid to date does not match Client Paid To Date = {$client->paid_to_date} - Invoice Payments = {$total_invoice_payments}"; + $this->company_data[] = $client->present()->name.'id = # '.$client->id." - Paid to date does not match Client Paid To Date = {$client->paid_to_date} - Invoice Payments = {$total_invoice_payments}\n"; $this->isValid = false; } @@ -187,22 +196,23 @@ class CheckCompanyData implements ShouldQueue $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'); - /*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; + //10/02/21 + // 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; $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); if ($ledger && (string) $invoice_balance != (string) $client->balance) { $wrong_paid_to_dates++; - $this->company_data[] = $client->present()->name.' - '.$client->id." - calculated client balances do not match {$invoice_balance} - ".rtrim($client->balance, '0'); + $this->company_data[] = $client->present()->name.' - '.$client->id." - calculated client balances do not match {$invoice_balance} - ".rtrim($client->balance, '0')."\n"; $this->isValid = false; } } - $this->company_data[] = "{$wrong_paid_to_dates} clients with incorrect client balances"; + $this->company_data[] = "{$wrong_paid_to_dates} clients with incorrect client balances\n"; } private function checkContacts() @@ -214,7 +224,7 @@ class CheckCompanyData implements ShouldQueue ->orderBy('id') ->get(['id']); - $this->company_data[] = $contacts->count().' contacts without a contact_key'; + $this->company_data[] = $contacts->count().' contacts without a contact_key\n'; if ($contacts->count() > 0) { $this->isValid = false; @@ -248,7 +258,7 @@ class CheckCompanyData implements ShouldQueue $clients = $clients->get(['clients.id', 'clients.user_id', 'clients.company_id']); - $this->company_data[] = $clients->count().' clients without any contacts'; + $this->company_data[] = $clients->count().' clients without any contacts\n'; if ($clients->count() > 0) { $this->isValid = false; @@ -283,7 +293,7 @@ class CheckCompanyData implements ShouldQueue } $clients = $clients->get(['clients.id', DB::raw('count(client_contacts.id)')]); - $this->company_data[] = $clients->count().' clients without a single primary contact'; + $this->company_data[] = $clients->count().' clients without a single primary contact\n'; if ($clients->count() > 0) { $this->isValid = false; @@ -326,7 +336,7 @@ class CheckCompanyData implements ShouldQueue if ($records->count()) { $this->isValid = false; - $this->company_data[] = $records->count()." {$table} records with incorrect {$entityType} company id"; + $this->company_data[] = $records->count()." {$table} records with incorrect {$entityType} company id\n"; } } } diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index f33aa38b4aec..7bdca9c721d5 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -14,6 +14,7 @@ namespace App\Jobs\Util; use App\DataMapper\Analytics\MigrationFailure; use App\DataMapper\CompanySettings; use App\Exceptions\MigrationValidatorFailed; +use App\Exceptions\ProcessingMigrationArchiveFailed; use App\Exceptions\ResourceDependencyMissing; use App\Exceptions\ResourceNotAvailableForMigration; use App\Factory\ClientFactory; @@ -31,6 +32,7 @@ use App\Http\Requests\Company\UpdateCompanyRequest; use App\Http\ValidationRules\ValidCompanyGatewayFeesAndLimitsRule; use App\Http\ValidationRules\ValidUserForCompany; use App\Jobs\Company\CreateCompanyToken; +use App\Jobs\Ninja\CheckCompanyData; use App\Jobs\Ninja\CompanySizeCheck; use App\Libraries\MultiDB; use App\Mail\MigrationCompleted; @@ -206,9 +208,14 @@ class Import implements ShouldQueue $this->setInitialCompanyLedgerBalances(); // $this->fixClientBalances(); + $check_data = CheckCompanyData::dispatchNow($this->company); + + + if($check_data['status'] == 'errors') + throw new ProcessingMigrationArchiveFailed($check_data); Mail::to($this->user) - ->send(new MigrationCompleted($this->company)); + ->send(new MigrationCompleted($this->company, $check_data)); /*After a migration first some basic jobs to ensure the system is up to date*/ VersionCheck::dispatch(); diff --git a/app/Jobs/Util/StartMigration.php b/app/Jobs/Util/StartMigration.php index 84509b6a500a..337a57f1e661 100644 --- a/app/Jobs/Util/StartMigration.php +++ b/app/Jobs/Util/StartMigration.php @@ -108,15 +108,16 @@ class StartMigration implements ShouldQueue throw new NonExistingMigrationFile('Migration file does not exist, or it is corrupted.'); } - //$data = json_decode(file_get_contents($file), 1); - //Import::dispatchNow($data['data'], $this->company, $this->user); Import::dispatchNow($file, $this->company, $this->user); + } catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) { + Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage())); if (app()->environment() !== 'production') { info($e->getMessage()); } + } //always make sure we unset the migration as running diff --git a/app/Mail/MigrationCompleted.php b/app/Mail/MigrationCompleted.php index db06b6531644..3ad8807b2cb8 100644 --- a/app/Mail/MigrationCompleted.php +++ b/app/Mail/MigrationCompleted.php @@ -13,14 +13,17 @@ class MigrationCompleted extends Mailable public $company; + public $check_data; + /** * Create a new message instance. * * @return void */ - public function __construct(Company $company) + public function __construct(Company $company, $check_data) { $this->company = $company; + $this->check_data = $check_data; } /** @@ -33,7 +36,8 @@ class MigrationCompleted extends Mailable $data['settings'] = $this->company->settings; $data['company'] = $this->company; $data['whitelabel'] = $this->company->account->isPaid() ? true : false; - + $data['check_data'] = $this->check_data; + $result = $this->from(config('mail.from.address'), config('mail.from.name')) ->view('email.import.completed', $data); diff --git a/app/Mail/MigrationFailed.php b/app/Mail/MigrationFailed.php index 438caf049be4..8f9cc26826fd 100644 --- a/app/Mail/MigrationFailed.php +++ b/app/Mail/MigrationFailed.php @@ -22,6 +22,7 @@ class MigrationFailed extends Mailable public function __construct($exception, $content = null) { $this->exception = $exception; + $this->content = $content; } /** diff --git a/resources/views/email/import/completed.blade.php b/resources/views/email/import/completed.blade.php index 91c63b136012..fcaef0824bbc 100644 --- a/resources/views/email/import/completed.blade.php +++ b/resources/views/email/import/completed.blade.php @@ -73,6 +73,10 @@

Documents Imported: {{ count($company->documents) }}

@endif +

Data Quality:

+ +

{{ $check_data }}

+ {{ ctrans('texts.account_login')}}

{{ ctrans('texts.email_signature')}}
{{ ctrans('texts.email_from') }}

From 6cc02243f5e00611771f3d42727e2f2b08ddbedb Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2021 13:26:05 +1100 Subject: [PATCH 4/5] Working on multi-db --- app/Console/Commands/DesignUpdate.php | 18 ++++++++++++++++ app/Console/Commands/SendRemindersCron.php | 24 +++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/DesignUpdate.php b/app/Console/Commands/DesignUpdate.php index 4a93cb51d03d..317e2e4d8729 100644 --- a/app/Console/Commands/DesignUpdate.php +++ b/app/Console/Commands/DesignUpdate.php @@ -11,6 +11,7 @@ namespace App\Console\Commands; +use App\Libraries\MultiDB; use App\Models\Design; use Illuminate\Console\Command; use stdClass; @@ -47,6 +48,23 @@ class DesignUpdate extends Command * @return mixed */ public function handle() + { + + + if (! config('ninja.db.multi_db_enabled')) { + $this->processReminders(); + } else { + //multiDB environment, need to + foreach (MultiDB::$dbs as $db) { + MultiDB::setDB($db); + + $this->processReminders($db); + } + } + + } + + private function handleOnDb() { foreach (Design::whereIsCustom(false)->get() as $design) { $invoice_design = new \App\Services\PdfMaker\Design(strtolower($design->name)); diff --git a/app/Console/Commands/SendRemindersCron.php b/app/Console/Commands/SendRemindersCron.php index 65271cbf1f8c..a41a7e9b25f1 100644 --- a/app/Console/Commands/SendRemindersCron.php +++ b/app/Console/Commands/SendRemindersCron.php @@ -13,6 +13,7 @@ namespace App\Console\Commands; use App\Jobs\Ninja\SendReminders; use App\Jobs\Util\WebHookHandler; +use App\Libraries\MultiDB; use App\Models\Invoice; use App\Models\Quote; use App\Models\Webhook; @@ -58,6 +59,24 @@ class SendRemindersCron extends Command } private function webHookOverdueInvoices() + { + + + if (! config('ninja.db.multi_db_enabled')) { + $this->executeWebhooks(); + } else { + //multiDB environment, need to + foreach (MultiDB::$dbs as $db) { + MultiDB::setDB($db); + + $this->executeWebhooks(); + } + } + + } + + + private function executeWebhooks() { $invoices = Invoice::where('is_deleted', 0) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) @@ -68,10 +87,7 @@ class SendRemindersCron extends Command $invoices->each(function ($invoice) { WebHookHandler::dispatch(Webhook::EVENT_LATE_INVOICE, $invoice, $invoice->company); }); - } - private function webHookExpiredQuotes() - { $quotes = Quote::where('is_deleted', 0) ->where('status_id', Quote::STATUS_SENT) ->whereDate('due_date', '<=', now()->subDays(1)->startOfDay()) @@ -81,4 +97,6 @@ class SendRemindersCron extends Command WebHookHandler::dispatch(Webhook::EVENT_EXPIRED_QUOTE, $quote, $quote->company); }); } + + } From 3956184560852e0dc1930d1762a51a2e2067c864 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2021 14:18:23 +1100 Subject: [PATCH 5/5] Working on multi-db --- app/Console/Commands/DesignUpdate.php | 4 +- app/Jobs/Ninja/CheckCompanyData.php | 76 +++++++++++++-------------- app/Jobs/Util/Import.php | 2 +- app/Mail/MigrationCompleted.php | 2 +- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/app/Console/Commands/DesignUpdate.php b/app/Console/Commands/DesignUpdate.php index 317e2e4d8729..c0d1ac8c5a4a 100644 --- a/app/Console/Commands/DesignUpdate.php +++ b/app/Console/Commands/DesignUpdate.php @@ -52,13 +52,13 @@ class DesignUpdate extends Command if (! config('ninja.db.multi_db_enabled')) { - $this->processReminders(); + $this->handleOnDb(); } else { //multiDB environment, need to foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); - $this->processReminders($db); + $this->handleOnDb($db); } } diff --git a/app/Jobs/Ninja/CheckCompanyData.php b/app/Jobs/Ninja/CheckCompanyData.php index 14e250e27f6d..35bf20201016 100644 --- a/app/Jobs/Ninja/CheckCompanyData.php +++ b/app/Jobs/Ninja/CheckCompanyData.php @@ -41,7 +41,7 @@ class CheckCompanyData implements ShouldQueue * * @return void */ - public function __construct(Company $company, string $hash) + public function __construct(Company $company, string $hash = '') { $this->company = $company; $this->hash = $hash; @@ -80,7 +80,7 @@ class CheckCompanyData implements ShouldQueue $this->company_data['status'] = 'errors'; else $this->company_data['status'] = 'success'; - + return $this->company_data; } @@ -96,7 +96,7 @@ class CheckCompanyData implements ShouldQueue $wrong_balances = 0; $wrong_paid_to_dates = 0; - foreach ($this->company->clients->where('is_deleted', 0)->cursor() as $client) { + foreach ($this->company->clients->where('is_deleted', 0) 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'); @@ -115,7 +115,7 @@ class CheckCompanyData implements ShouldQueue } } - $this->logMessage("{$wrong_balances} clients with incorrect balances"); + $this->company_data[] = "{$wrong_balances} clients with incorrect balances\n"; } private function checkInvoicePayments() @@ -123,7 +123,7 @@ class CheckCompanyData implements ShouldQueue $wrong_balances = 0; $wrong_paid_to_dates = 0; - $this->company->clients->where('is_deleted', 0)->cursor()->each(function ($client) use ($wrong_balances) { + $this->company->clients->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_PAID, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.amount'); $total_refund = $invoice->payments->whereIn('status_id', [Payment::STATUS_PAID, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.refunded'); @@ -150,7 +150,7 @@ class CheckCompanyData implements ShouldQueue $wrong_paid_to_dates = 0; $credit_total_applied = 0; - $this->company->clients->where('is_deleted', 0)->cursor()->each(function ($client) use ($wrong_paid_to_dates, $credit_total_applied) { + $this->company->clients->where('is_deleted', 0)->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) { @@ -191,7 +191,7 @@ class CheckCompanyData implements ShouldQueue $wrong_balances = 0; $wrong_paid_to_dates = 0; - foreach ($this->company->clients->where('is_deleted', 0)->cursor() as $client) { + foreach ($this->company->clients->where('is_deleted', 0) 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'); @@ -219,7 +219,7 @@ class CheckCompanyData implements ShouldQueue { // check for contacts with the contact_key value set $contacts = DB::table('client_contacts') - ->where('company_id', $this->company_id) + ->where('company_id', $this->company->id) ->whereNull('contact_key') ->orderBy('id') ->get(['id']); @@ -230,17 +230,17 @@ class CheckCompanyData implements ShouldQueue $this->isValid = false; } - if ($this->option('fix') == 'true') { - foreach ($contacts as $contact) { - DB::table('client_contacts') - ->where('company_id', $this->company->id) - ->where('id', '=', $contact->id) - ->whereNull('contact_key') - ->update([ - 'contact_key' => str_random(config('ninja.key_length')), - ]); - } - } + // if ($this->option('fix') == 'true') { + // foreach ($contacts as $contact) { + // DB::table('client_contacts') + // ->where('company_id', $this->company->id) + // ->where('id', '=', $contact->id) + // ->whereNull('contact_key') + // ->update([ + // 'contact_key' => str_random(config('ninja.key_length')), + // ]); + // } + // } // check for missing contacts $clients = DB::table('clients') @@ -252,9 +252,9 @@ class CheckCompanyData implements ShouldQueue ->groupBy('clients.id', 'clients.user_id', 'clients.company_id') ->havingRaw('count(client_contacts.id) = 0'); - if ($this->option('client_id')) { - $clients->where('clients.id', '=', $this->option('client_id')); - } + // if ($this->option('client_id')) { + // $clients->where('clients.id', '=', $this->option('client_id')); + // } $clients = $clients->get(['clients.id', 'clients.user_id', 'clients.company_id']); @@ -264,22 +264,22 @@ class CheckCompanyData implements ShouldQueue $this->isValid = false; } - if ($this->option('fix') == 'true') { - foreach ($clients as $client) { - $contact = new ClientContact(); - $contact->company_id = $client->company_id; - $contact->user_id = $client->user_id; - $contact->client_id = $client->id; - $contact->is_primary = true; - $contact->send_invoice = true; - $contact->contact_key = str_random(config('ninja.key_length')); - $contact->save(); - } - } + // if ($this->option('fix') == 'true') { + // foreach ($clients as $client) { + // $contact = new ClientContact(); + // $contact->company_id = $client->company_id; + // $contact->user_id = $client->user_id; + // $contact->client_id = $client->id; + // $contact->is_primary = true; + // $contact->send_invoice = true; + // $contact->contact_key = str_random(config('ninja.key_length')); + // $contact->save(); + // } + // } // check for more than one primary contact $clients = DB::table('clients') - ->where('company_id', $this->company->id) + ->where('clients.company_id', $this->company->id) ->leftJoin('client_contacts', function ($join) { $join->on('client_contacts.client_id', '=', 'clients.id') ->where('client_contacts.is_primary', '=', true) @@ -288,9 +288,9 @@ class CheckCompanyData implements ShouldQueue ->groupBy('clients.id') ->havingRaw('count(client_contacts.id) != 1'); - if ($this->option('client_id')) { - $clients->where('clients.id', '=', $this->option('client_id')); - } + // if ($this->option('client_id')) { + // $clients->where('clients.id', '=', $this->option('client_id')); + // } $clients = $clients->get(['clients.id', DB::raw('count(client_contacts.id)')]); $this->company_data[] = $clients->count().' clients without a single primary contact\n'; diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 7bdca9c721d5..427e10510a16 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -208,7 +208,7 @@ class Import implements ShouldQueue $this->setInitialCompanyLedgerBalances(); // $this->fixClientBalances(); - $check_data = CheckCompanyData::dispatchNow($this->company); + $check_data = CheckCompanyData::dispatchNow($this->company, md5(time())); if($check_data['status'] == 'errors') diff --git a/app/Mail/MigrationCompleted.php b/app/Mail/MigrationCompleted.php index 3ad8807b2cb8..5e7baf470f87 100644 --- a/app/Mail/MigrationCompleted.php +++ b/app/Mail/MigrationCompleted.php @@ -20,7 +20,7 @@ class MigrationCompleted extends Mailable * * @return void */ - public function __construct(Company $company, $check_data) + public function __construct(Company $company, $check_data = '') { $this->company = $company; $this->check_data = $check_data;