diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 17c8b764a62b..fcb09b295f33 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -291,8 +291,8 @@ class CheckData extends Command foreach(Client::cursor() as $client) { - $invoice_balance = $client->invoices->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(); if($ledger && number_format($invoice_balance, 4) != number_format($client->balance, 4)) @@ -346,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}"); @@ -382,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/Console/Commands/DemoMode.php b/app/Console/Commands/DemoMode.php index b878949ce901..e468b3f1c6eb 100644 --- a/app/Console/Commands/DemoMode.php +++ b/app/Console/Commands/DemoMode.php @@ -86,7 +86,7 @@ class DemoMode extends Command private function createSmallAccount() { - $this->count = 100; + $this->count = 10; $this->info('Creating Small Account and Company'); diff --git a/app/Helpers/Email/InvoiceEmail.php b/app/Helpers/Email/InvoiceEmail.php index adac32f6682f..49c7630bbe06 100644 --- a/app/Helpers/Email/InvoiceEmail.php +++ b/app/Helpers/Email/InvoiceEmail.php @@ -45,9 +45,9 @@ class InvoiceEmail extends EmailBuilder if (iconv_strlen($subject_template) == 0) { if ($reminder_template == 'quote') { $subject_template = trans( - 'texts.invoice_subject', + 'texts.quote_subject', [ - 'invoice' => $invoice->present()->invoice_number(), + 'number' => $invoice->number, 'account' => $invoice->company->present()->name() ], null, @@ -55,9 +55,9 @@ class InvoiceEmail extends EmailBuilder ); } else { $subject_template = trans( - 'texts.reminder_subject', + 'texts.invoice_subject', [ - 'invoice' => $invoice->present()->invoice_number(), + 'number' => $invoice->number, 'account' => $invoice->company->present()->name() ], null, 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/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index cc8ec5f8d66b..d76c5033dcdd 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -42,6 +42,8 @@ class AutoBillInvoice extends AbstractService if(!$this->invoice->isPayable()) return $this->invoice; + $this->invoice = $this->invoice->service()->markSent()->save(); + if($this->invoice->balance > 0) $gateway_token = $this->getGateway($this->invoice->balance); else @@ -70,12 +72,12 @@ class AutoBillInvoice extends AbstractService if($payment){ - $this->invoice->service()->toggleFeesPaid()->save(); - + $this->invoice = $this->invoice->service()->toggleFeesPaid()->save(); + } else { - //autobill failed + //TODO autobill failed } 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(); diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 40d587733b2a..10b6ba0a8438 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -182,9 +182,11 @@ class InvoiceService { $this->invoice->line_items = collect($this->invoice->line_items) - ->where('type_id',3)->map(function ($item) { + ->map(function ($item) { - $item->type_id=4; + if($item->type_id == 3) + $item->type_id = 4; + return $item; })->toArray(); diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 8ae30079c316..76591becad86 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -42,6 +42,10 @@ class MarkPaid extends AbstractService $this->invoice->service()->markSent(); } + /*Don't double pay*/ + if($this->invoice->statud_id == Invoice::STATUS_PAID) + return $this->invoice; + /* Create Payment */ $payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); diff --git a/app/Services/Invoice/TriggeredActions.php b/app/Services/Invoice/TriggeredActions.php index e14017fdcc7d..8b807031e825 100644 --- a/app/Services/Invoice/TriggeredActions.php +++ b/app/Services/Invoice/TriggeredActions.php @@ -45,11 +45,11 @@ class TriggeredActions extends AbstractService { if($this->request->has('auto_bill')) { - $this->invoice->service()->autoBill()->save(); + $this->invoice = $this->invoice->service()->autoBill()->save(); } if($this->request->has('paid') && (bool)$this->request->input('paid') !== false) { - $this->invoice->service()->markPaid()->save(); + $this->invoice = $this->invoice->service()->markPaid()->save(); } if($this->request->has('send_email') && (bool)$this->request->input('send_email') !== false) { @@ -62,7 +62,8 @@ class TriggeredActions extends AbstractService private function sendEmail() { - $reminder_template = $this->invoice->calculateTemplate(); + //$reminder_template = $this->invoice->calculateTemplate(); + $reminder_template = 'payment'; $this->invoice->invitations->load('contact.client.country','invoice.client.country','invoice.company')->each(function ($invitation) use($reminder_template){ diff --git a/database/factories/CompanyFactory.php b/database/factories/CompanyFactory.php index 7f60c849b5cc..8d087de9e49e 100644 --- a/database/factories/CompanyFactory.php +++ b/database/factories/CompanyFactory.php @@ -10,6 +10,7 @@ $factory->define(App\Models\Company::class, function (Faker $faker) { 'ip' => $faker->ipv4, 'db' => config('database.default'), 'settings' => CompanySettings::defaults(), + 'is_large' => true, 'custom_fields' => (object) [ //'invoice1' => 'Custom Date|date', // 'invoice2' => '2|switch', diff --git a/resources/views/index/index.blade.php b/resources/views/index/index.blade.php index c67d7d8cb590..0f79a0238a71 100644 --- a/resources/views/index/index.blade.php +++ b/resources/views/index/index.blade.php @@ -5,7 +5,7 @@