From 4bc096c7c6f6969a047dfaa5aa42311165ad84ef Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 1 Jul 2021 20:58:38 +1000 Subject: [PATCH 1/2] Minor fixes for check data --- app/Console/Commands/CheckData.php | 43 +++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index e88d898ad5..b3f4bd809b 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -16,19 +16,21 @@ use App\Factory\ClientContactFactory; use App\Models\Account; use App\Models\Client; use App\Models\ClientContact; +use App\Models\Company; use App\Models\CompanyLedger; use App\Models\Contact; use App\Models\Credit; use App\Models\Invoice; use App\Models\InvoiceInvitation; use App\Models\Payment; +use App\Models\Paymentable; use App\Utils\Ninja; use DB; use Exception; use Illuminate\Console\Command; +use Illuminate\Support\Str; use Mail; use Symfony\Component\Console\Input\InputOption; -use Illuminate\Support\Str; /* @@ -98,6 +100,7 @@ class CheckData extends Command $this->checkInvoiceBalances(); $this->checkInvoicePayments(); $this->checkPaidToDates(); + // $this->checkPaidToCompanyDates(); $this->checkClientBalances(); $this->checkContacts(); $this->checkCompanyData(); @@ -311,6 +314,36 @@ class CheckData extends Command } } + // private function checkPaidToCompanyDates() + // { + // Company::cursor()->each(function ($company){ + + // $payments = Payment::where('is_deleted', 0) + // ->where('company_id', $company->id) + // ->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED]) + // ->pluck('id'); + + // $unapplied = Payment::where('is_deleted', 0) + // ->where('company_id', $company->id) + // ->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]) + // ->sum(\DB::Raw('amount - applied')); + + // $paymentables = Paymentable::whereIn('payment_id', $payments)->sum(\DB::Raw('amount - refunded')); + + // $client_paid_to_date = Client::where('company_id', $company->id)->where('is_deleted', 0)->withTrashed()->sum('paid_to_date'); + + // $total_payments = $paymentables + $unapplied; + + // if (round($total_payments, 2) != round($client_paid_to_date, 2)) { + // $this->wrong_paid_to_dates++; + + // $this->logMessage($company->present()->name.' id = # '.$company->id." - Paid to date does not match Client Paid To Date = {$client_paid_to_date} - Invoice Payments = {$total_payments}"); + // } + + // }); + + // } + private function checkPaidToDates() { $this->wrong_paid_to_dates = 0; @@ -337,12 +370,10 @@ class CheckData extends Command //commented IN 27/06/2021 - sums ALL client payments AND the unapplied amounts to match the client paid to date $p = Payment::where('client_id', $client->id) ->where('is_deleted', 0) - ->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]); + ->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]) + ->sum(DB::Raw('amount - applied')); - $total_amount = $p->sum('amount'); - $total_applied = $p->sum('applied'); - - $total_invoice_payments += ($total_amount - $total_applied); + $total_invoice_payments += $p; // 10/02/21 foreach ($client->payments as $payment) { From 32698b33d7e54717e4a886e38fd55c6b2926dea5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 2 Jul 2021 07:23:25 +1000 Subject: [PATCH 2/2] Document middleware --- app/Http/Kernel.php | 2 ++ app/Http/Middleware/SetDocumentDb.php | 44 +++++++++++++++++++++++++++ app/Libraries/MultiDB.php | 18 +++++++++++ routes/client.php | 2 +- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 app/Http/Middleware/SetDocumentDb.php diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 33e2c42f8e..e788bd0fb7 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -30,6 +30,7 @@ use App\Http\Middleware\QueryLogging; use App\Http\Middleware\RedirectIfAuthenticated; use App\Http\Middleware\SetDb; use App\Http\Middleware\SetDbByCompanyKey; +use App\Http\Middleware\SetDocumentDb; use App\Http\Middleware\SetDomainNameDb; use App\Http\Middleware\SetEmailDb; use App\Http\Middleware\SetInviteDb; @@ -158,6 +159,7 @@ class Kernel extends HttpKernel 'contact_key_login' => ContactKeyLogin::class, 'check_client_existence' => CheckClientExistence::class, 'user_verified' => UserVerified::class, + 'document_db' => SetDocumentDb::class, ]; diff --git a/app/Http/Middleware/SetDocumentDb.php b/app/Http/Middleware/SetDocumentDb.php new file mode 100644 index 0000000000..83d0d0be88 --- /dev/null +++ b/app/Http/Middleware/SetDocumentDb.php @@ -0,0 +1,44 @@ + 'Document not set or not found', + 'errors' => new stdClass, + ]; + + if ($request->has('document_hash') && config('ninja.db.multi_db_enabled')) { + + if (! MultiDB::documentFindAndSetDb($request->input('document_hash'))) + return response()->json($error, 400); + + } + + return $next($request); + } +} diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 1847592d0f..8120da0892 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -206,6 +206,24 @@ class MultiDB return false; } + public static function documentFindAndSetDb($hash) : bool + { + $current_db = config('database.default'); + + //multi-db active + foreach (self::$dbs as $db) { + + if (Document::on($db)->where('hash', $hash)->count() >= 1){ + self::setDb($db); + return true; + } + + } + + self::setDB($current_db); + return false; + } + public static function findAndSetDb($token) :bool { $current_db = config('database.default'); diff --git a/routes/client.php b/routes/client.php index 53e5e2626a..b95d5a95de 100644 --- a/routes/client.php +++ b/routes/client.php @@ -23,7 +23,7 @@ Route::get('tmp_pdf/{hash}', 'ClientPortal\TempRouteController@index')->name('tm Route::get('client/key_login/{contact_key}', 'ClientPortal\ContactHashLoginController@login')->name('client.contact_login')->middleware(['domain_db','contact_key_login']); Route::get('client/magic_link/{magic_link}', 'ClientPortal\ContactHashLoginController@magicLink')->name('client.contact_magic_link')->middleware(['domain_db','contact_key_login']); -Route::get('documents/{document_hash}', 'ClientPortal\DocumentController@publicDownload')->name('documents.public_download'); +Route::get('documents/{document_hash}', 'ClientPortal\DocumentController@publicDownload')->name('documents.public_download')->middleware(['document_db']); Route::get('error', 'ClientPortal\ContactHashLoginController@errorPage')->name('client.error'); Route::group(['middleware' => ['auth:contact', 'locale', 'check_client_existence','domain_db'], 'prefix' => 'client', 'as' => 'client.'], function () {