diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 4395b944c190..fcb09b295f33 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -291,9 +291,7 @@ class CheckData extends Command foreach(Client::cursor() as $client) { - $invoice_balance = $client->invoices->where('is_deleted', false)->sum('balance'); - $credt_balances = $client->credits->where('is_deleted', false)->sum('balance'); - + $invoice_balance = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance'); $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); @@ -348,16 +346,20 @@ class CheckData extends Command { $wrong_balances = 0; $wrong_paid_to_dates = 0; - + + //todo reversing an invoice breaks the check data at this point; + Client::cursor()->each(function ($client) use($wrong_balances){ $client->invoices->where('is_deleted', false)->each(function ($invoice) use($wrong_balances, $client){ $total_amount = $invoice->payments->sum('pivot.amount'); $total_refund = $invoice->payments->sum('pivot.refunded'); + $total_credit = $invoice->credits->sum('amount'); + $total_paid = $total_amount - $total_refund; - if($total_paid != ($invoice->amount - $invoice->balance)) { + if($total_paid != ($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}"); @@ -384,13 +386,24 @@ class CheckData extends Command $invoice_balance = $client->invoices->sum('balance'); $invoice_amounts = $client->invoices->sum('amount') - $invoice_balance; + $credit_amounts = 0; + + 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; + $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); - if($ledger && (string)$invoice_amounts != rtrim($client->paid_to_date, "0")) + if($ledger && (string)$invoice_amounts != (string)$client_paid_to_date) { $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_amounts} - " .rtrim($client_paid_to_date, "0")); $this->isValid = false; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index c3128d5a3ded..fb60aa94864f 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -198,7 +198,6 @@ class Invoice extends BaseModel public function payments() { return $this->morphToMany(Payment::class, 'paymentable')->withPivot('amount', 'refunded')->withTimestamps()->withTrashed(); - ; } public function company_ledger() @@ -216,6 +215,10 @@ class Invoice extends BaseModel return $this->hasManyThrough(Backup::class, Activity::class); } + public function credits() + { + return $this->hasMany(Credit::class); + } // public function credits() // { // return $this->belongsToMany(Credit::class)->using(Paymentable::class)->withPivot( diff --git a/app/Services/Invoice/HandleReversal.php b/app/Services/Invoice/HandleReversal.php index 2bb5a43cc8a9..5fd0a3f52c36 100644 --- a/app/Services/Invoice/HandleReversal.php +++ b/app/Services/Invoice/HandleReversal.php @@ -45,6 +45,7 @@ class HandleReversal extends AbstractService return $this->invoice; } + /* If the invoice has been cancelled - we need to unwind the cancellation before reversing*/ if($this->invoice->status_id == Invoice::STATUS_CANCELLED) $this->invoice = $this->invoice->service()->reverseCancellation()->save();