1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Don't include drafts when checking data

This commit is contained in:
David Bomba 2020-07-16 13:54:26 +10:00
parent 59ca29d7db
commit b4ad18bfe9
3 changed files with 25 additions and 8 deletions

View File

@ -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;

View File

@ -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(

View File

@ -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();