Merge pull request #3911 from turbo124/v2

V2
This commit is contained in:
David Bomba 2020-07-16 15:50:31 +10:00 committed by GitHub
commit 23f12ef27c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 51 additions and 22 deletions

View File

@ -291,7 +291,7 @@ class CheckData extends Command
foreach(Client::cursor() as $client) 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(); $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
@ -347,15 +347,19 @@ class CheckData extends Command
$wrong_balances = 0; $wrong_balances = 0;
$wrong_paid_to_dates = 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::cursor()->each(function ($client) use($wrong_balances){
$client->invoices->where('is_deleted', false)->each(function ($invoice) use($wrong_balances, $client){ $client->invoices->where('is_deleted', false)->each(function ($invoice) use($wrong_balances, $client){
$total_amount = $invoice->payments->sum('pivot.amount'); $total_amount = $invoice->payments->sum('pivot.amount');
$total_refund = $invoice->payments->sum('pivot.refunded'); $total_refund = $invoice->payments->sum('pivot.refunded');
$total_credit = $invoice->credits->sum('amount');
$total_paid = $total_amount - $total_refund; $total_paid = $total_amount - $total_refund;
if($total_paid != ($invoice->amount - $invoice->balance)) { if($total_paid != ($invoice->amount - $invoice->balance - $total_credit)) {
$wrong_balances++; $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 . " - 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_balance = $client->invoices->sum('balance');
$invoice_amounts = $client->invoices->sum('amount') - $invoice_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(); $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++; $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; $this->isValid = false;

View File

@ -86,7 +86,7 @@ class DemoMode extends Command
private function createSmallAccount() private function createSmallAccount()
{ {
$this->count = 100; $this->count = 10;
$this->info('Creating Small Account and Company'); $this->info('Creating Small Account and Company');

View File

@ -45,9 +45,9 @@ class InvoiceEmail extends EmailBuilder
if (iconv_strlen($subject_template) == 0) { if (iconv_strlen($subject_template) == 0) {
if ($reminder_template == 'quote') { if ($reminder_template == 'quote') {
$subject_template = trans( $subject_template = trans(
'texts.invoice_subject', 'texts.quote_subject',
[ [
'invoice' => $invoice->present()->invoice_number(), 'number' => $invoice->number,
'account' => $invoice->company->present()->name() 'account' => $invoice->company->present()->name()
], ],
null, null,
@ -55,9 +55,9 @@ class InvoiceEmail extends EmailBuilder
); );
} else { } else {
$subject_template = trans( $subject_template = trans(
'texts.reminder_subject', 'texts.invoice_subject',
[ [
'invoice' => $invoice->present()->invoice_number(), 'number' => $invoice->number,
'account' => $invoice->company->present()->name() 'account' => $invoice->company->present()->name()
], ],
null, null,

View File

@ -198,7 +198,6 @@ class Invoice extends BaseModel
public function payments() public function payments()
{ {
return $this->morphToMany(Payment::class, 'paymentable')->withPivot('amount', 'refunded')->withTimestamps()->withTrashed(); return $this->morphToMany(Payment::class, 'paymentable')->withPivot('amount', 'refunded')->withTimestamps()->withTrashed();
;
} }
public function company_ledger() public function company_ledger()
@ -216,6 +215,10 @@ class Invoice extends BaseModel
return $this->hasManyThrough(Backup::class, Activity::class); return $this->hasManyThrough(Backup::class, Activity::class);
} }
public function credits()
{
return $this->hasMany(Credit::class);
}
// public function credits() // public function credits()
// { // {
// return $this->belongsToMany(Credit::class)->using(Paymentable::class)->withPivot( // return $this->belongsToMany(Credit::class)->using(Paymentable::class)->withPivot(

View File

@ -42,6 +42,8 @@ class AutoBillInvoice extends AbstractService
if(!$this->invoice->isPayable()) if(!$this->invoice->isPayable())
return $this->invoice; return $this->invoice;
$this->invoice = $this->invoice->service()->markSent()->save();
if($this->invoice->balance > 0) if($this->invoice->balance > 0)
$gateway_token = $this->getGateway($this->invoice->balance); $gateway_token = $this->getGateway($this->invoice->balance);
else else
@ -70,12 +72,12 @@ class AutoBillInvoice extends AbstractService
if($payment){ if($payment){
$this->invoice->service()->toggleFeesPaid()->save(); $this->invoice = $this->invoice->service()->toggleFeesPaid()->save();
} }
else else
{ {
//autobill failed //TODO autobill failed
} }

View File

@ -45,6 +45,7 @@ class HandleReversal extends AbstractService
return $this->invoice; 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) if($this->invoice->status_id == Invoice::STATUS_CANCELLED)
$this->invoice = $this->invoice->service()->reverseCancellation()->save(); $this->invoice = $this->invoice->service()->reverseCancellation()->save();

View File

@ -182,9 +182,11 @@ class InvoiceService
{ {
$this->invoice->line_items = collect($this->invoice->line_items) $this->invoice->line_items = collect($this->invoice->line_items)
->where('type_id',3)->map(function ($item) { ->map(function ($item) {
if($item->type_id == 3)
$item->type_id = 4;
$item->type_id=4;
return $item; return $item;
})->toArray(); })->toArray();

View File

@ -42,6 +42,10 @@ class MarkPaid extends AbstractService
$this->invoice->service()->markSent(); $this->invoice->service()->markSent();
} }
/*Don't double pay*/
if($this->invoice->statud_id == Invoice::STATUS_PAID)
return $this->invoice;
/* Create Payment */ /* Create Payment */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); $payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);

View File

@ -45,11 +45,11 @@ class TriggeredActions extends AbstractService
{ {
if($this->request->has('auto_bill')) { 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) { 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) { if($this->request->has('send_email') && (bool)$this->request->input('send_email') !== false) {
@ -62,7 +62,8 @@ class TriggeredActions extends AbstractService
private function sendEmail() 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){ $this->invoice->invitations->load('contact.client.country','invoice.client.country','invoice.company')->each(function ($invitation) use($reminder_template){

View File

@ -10,6 +10,7 @@ $factory->define(App\Models\Company::class, function (Faker $faker) {
'ip' => $faker->ipv4, 'ip' => $faker->ipv4,
'db' => config('database.default'), 'db' => config('database.default'),
'settings' => CompanySettings::defaults(), 'settings' => CompanySettings::defaults(),
'is_large' => true,
'custom_fields' => (object) [ 'custom_fields' => (object) [
//'invoice1' => 'Custom Date|date', //'invoice1' => 'Custom Date|date',
// 'invoice2' => '2|switch', // 'invoice2' => '2|switch',

View File

@ -5,7 +5,7 @@
<title>Invoice Ninja</title> <title>Invoice Ninja</title>
<meta name="report_errors" content="{{ $report_errors }}"> <meta name="report_errors" content="{{ $report_errors }}">
<meta name="google-signin-client_id" content="{{ config('services.google.client_id') }}"> <meta name="google-signin-client_id" content="{{ config('services.google.client_id') }}">
<link rel="manifest" href="manifest.json"> <link rel="manifest" href="manifest.json?v={{sha1(time())}}">
</head> </head>
<body style="background-color:#888888;"> <body style="background-color:#888888;">
@ -75,7 +75,7 @@
<script> <script>
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
window.addEventListener('load', function () { window.addEventListener('load', function () {
navigator.serviceWorker.register('/flutter_service_worker.js'); navigator.serviceWorker.register('/flutter_service_worker.js?v={{sha1(time())}}');
}); });
} }