From b7470798d4b1f9af797e72a3bf630f2b182e1555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Gayot?= Date: Sat, 17 Oct 2020 01:32:53 +0200 Subject: [PATCH 1/5] Fix BaseRepository Fix BaseRepository (remove useless ";") --- app/Repositories/BaseRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 4f7c68b4f03b..f2e14b03c0bf 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -265,7 +265,7 @@ class BaseRepository //make sure we are creating an invite for a contact who belongs to the client only! $contact = ClientContact::find($invitation['client_contact_id']); - if ($contact && $model->client_id == $contact->client_id); + if ($contact && $model->client_id == $contact->client_id) { $invitation_class = sprintf('App\\Models\\%sInvitation', $resource); From 675e211e337815fef7a0cefb70a22baec029f6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Gayot?= Date: Sat, 17 Oct 2020 02:24:02 +0200 Subject: [PATCH 2/5] Support PDF variables with UTF-8 characters --- app/Services/PdfMaker/PdfMakerUtilities.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Services/PdfMaker/PdfMakerUtilities.php b/app/Services/PdfMaker/PdfMakerUtilities.php index 2078c16b6b15..8ffe05594295 100644 --- a/app/Services/PdfMaker/PdfMakerUtilities.php +++ b/app/Services/PdfMaker/PdfMakerUtilities.php @@ -161,7 +161,7 @@ trait PdfMakerUtilities $html = strtr($html, $variables['values']); - @$this->document->loadHTML($html); + @$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); $this->document->saveHTML(); } From 708db5a991fa85c9759a9abcde28f510a636e120 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 18 Oct 2020 18:46:10 +1100 Subject: [PATCH 3/5] Improve eager loading - first load --- app/Console/Commands/CheckData.php | 7 ++++--- app/Http/Controllers/BaseController.php | 13 +++++++------ app/Http/Middleware/QueryLogging.php | 2 +- app/Jobs/Util/Import.php | 10 +++++++--- .../Migration/PaymentMigrationRepository.php | 3 ++- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 73c9fc5f7c6e..f7c63b8085da 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -303,7 +303,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; } @@ -352,11 +352,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; } diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index a69893e728bf..ba7111ef860d 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -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'); }, ] ); diff --git a/app/Http/Middleware/QueryLogging.php b/app/Http/Middleware/QueryLogging.php index dc343832d86e..32d854d17b3b 100644 --- a/app/Http/Middleware/QueryLogging.php +++ b/app/Http/Middleware/QueryLogging.php @@ -52,7 +52,7 @@ class QueryLogging Log::info($request->method().' - '.$request->url().": $count queries - ".$time); - // if($count > 50) + // if($count > 50) // Log::info($queries); } } diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index b556b5c3d99a..400c393d605f 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -105,11 +105,10 @@ class Import implements ShouldQueue 'invoices', 'recurring_invoices', 'quotes', - 'payments', 'credits', + 'payments', 'company_gateways', 'client_gateway_tokens', - // //'documents', ]; @@ -722,7 +721,12 @@ 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']); + } } diff --git a/app/Repositories/Migration/PaymentMigrationRepository.php b/app/Repositories/Migration/PaymentMigrationRepository.php index 46b46ea114dd..b8a057b4b1ef 100644 --- a/app/Repositories/Migration/PaymentMigrationRepository.php +++ b/app/Repositories/Migration/PaymentMigrationRepository.php @@ -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) { @@ -129,7 +130,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()); } From 6f1f0ecebe8309db30c1a836572fba7fbff3cd0b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 18 Oct 2020 20:25:32 +1100 Subject: [PATCH 4/5] Adjust the way we import credits from V4 to V5 - refactor check data script to reflect changes --- app/Console/Commands/CheckData.php | 31 +++++++++++++------ app/Jobs/Util/Import.php | 4 ++- .../Migration/PaymentMigrationRepository.php | 14 +++++++++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index f7c63b8085da..b681a12bce6d 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -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; @@ -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++; @@ -374,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; } diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 400c393d605f..b2754b8ec08c 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -724,8 +724,10 @@ class Import implements ShouldQueue if($modified['amount'] >= 0) $modified['invoices'][$key]['invoice_id'] = $this->transformId('invoices', $invoice['invoice_id']); - else + else{ $modified['credits'][$key]['credit_id'] = $this->transformId('credits', $invoice['invoice_id']); + $modified['credits'][$key]['amount'] = $modified['invoices'][$key]['amount']; + } } } diff --git a/app/Repositories/Migration/PaymentMigrationRepository.php b/app/Repositories/Migration/PaymentMigrationRepository.php index b8a057b4b1ef..505576b13f57 100644 --- a/app/Repositories/Migration/PaymentMigrationRepository.php +++ b/app/Repositories/Migration/PaymentMigrationRepository.php @@ -117,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; From 93ea785131def4df28d0746610e9ba539a93e104 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 19 Oct 2020 08:21:14 +1100 Subject: [PATCH 5/5] Fix for incorrect company country being displayed --- app/Http/ValidationRules/Project/ValidProjectForClient.php | 3 +++ app/Models/Presenters/CompanyPresenter.php | 2 +- app/Utils/HtmlEngine.php | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/Http/ValidationRules/Project/ValidProjectForClient.php b/app/Http/ValidationRules/Project/ValidProjectForClient.php index a23e421de398..e33d5366a2bd 100644 --- a/app/Http/ValidationRules/Project/ValidProjectForClient.php +++ b/app/Http/ValidationRules/Project/ValidProjectForClient.php @@ -35,6 +35,9 @@ class ValidProjectForClient implements Rule */ public function passes($attribute, $value) { + if(empty($this->input['project_id'])) + return true; + if(is_string($this->input['project_id'])) $this->input['project_id'] = $this->decodePrimaryKey($this->input['project_id']); diff --git a/app/Models/Presenters/CompanyPresenter.php b/app/Models/Presenters/CompanyPresenter.php index aac2ff141ce7..dfe272e13518 100644 --- a/app/Models/Presenters/CompanyPresenter.php +++ b/app/Models/Presenters/CompanyPresenter.php @@ -76,7 +76,7 @@ class CompanyPresenter extends EntityPresenter $settings = $this->entity->settings; } - $country = Country::find($settings->country_id)->first(); + $country = Country::find($settings->country_id); $swap = $country && $country->swap_postal_code; diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 42700c8a6b79..2075ace404bc 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -427,7 +427,7 @@ class HtmlEngine private function getCountryName() :string { - $country = Country::find($this->settings->country_id)->first(); + $country = Country::find($this->settings->country_id); if ($country) { return $country->name;