From b37c4754143476fe1d0165f127f9dbc8cd9f29d8 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 2 Jul 2020 15:25:34 +1000 Subject: [PATCH] working on checkdata script --- app/Console/Commands/CheckData.php | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index e7f84fabba25..42f657fb415f 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -82,6 +82,7 @@ class CheckData extends Command $this->checkInvoiceBalances(); $this->checkInvoicePayments(); + $this->checkPaidToDates(); $this->checkClientBalances(); $this->checkContacts(); $this->checkCompanyData(); @@ -317,9 +318,48 @@ class CheckData extends Command } + private function checkPaidToDates() + { + + $wrong_paid_to_dates = 0; + + Client::withTrashed()->cursor()->each(function ($client) use($wrong_paid_to_dates){ + + $total_invoice_payments = 0; + + $client->invoices->where('is_deleted', false)->each(function ($invoice) use($total_invoice_payments, $wrong_paid_to_dates){ + + $total_amount = $invoice->payments->sum('pivot.amount'); + $total_refund = $invoice->payments->sum('pivot.refunded'); + + info("Pivot = " . $total_amount . " - " . $total_refund); + + $total_invoice_payments += ($total_amount - $total_refund); + + info($total_invoice_payments); + }); + + info($total_invoice_payments . " = ". $client->paid_to_date); + + if($total_invoice_payments != $client->paid_to_date) { + $wrong_paid_to_dates++; + + $this->logMessage($client->present()->name . " - " . $client->id . " - Paid to date does not match Client Paid To Date = {$client->paid_to_date} - Invoice Payments = {$total_invoice_payments}"); + + $this->isValid = false; + + } + + }); + + $this->logMessage("{$wrong_paid_to_dates} clients with incorrect paid to dates"); + + } + private function checkInvoicePayments() { $wrong_balances = 0; + $wrong_paid_to_dates = 0; Client::cursor()->each(function ($client) use($wrong_balances){