Added client usually pays in average days (#2876)

* Added client usually pays in average days

* Added translation for days label

* Code cleanup

* Added client usually pays in average days

* Added translation for days label

* Code cleanup

* Limit calculation to most recent 20 paid invoices, eager load fix
This commit is contained in:
Christopher Di Carlo 2019-06-27 08:01:46 -04:00 committed by David Bomba
parent 8b4976cacf
commit 048ee7181e
4 changed files with 35 additions and 0 deletions

View File

@ -625,6 +625,29 @@ class Client extends EntityModel
}
}
}
public function getUsuallyPaysIn() {
$paysIn = $this->invoices()
->with('payments')
->where('invoice_status_id', '=', INVOICE_STATUS_PAID)
->orderBy('invoice_date', 'desc')
->take(20)
->get()
->map(function ($item) {
$payments = $item->payments()->orderBy('payment_date', 'asc')->get();
$invoiceTotal = $item->amount;
foreach($payments as $payment) {
if($payment->amount < $invoiceTotal) {
$invoiceTotal -= $payment->amount;
} elseif($payment->amount >= $invoiceTotal) {
return \Carbon::parse($item->invoice_date)->diffInDays($payment->payment_date);
}
}
})->avg();
return $paysIn;
}
}
Client::creating(function ($client) {

View File

@ -46,6 +46,12 @@ class ClientPresenter extends EntityPresenter
return $account->formatMoney($client->paid_to_date, $client);
}
public function usuallyPaysIn() {
$avgDays = $this->entity->getUsuallyPaysIn();
return Utils::roundSignificant($avgDays) . ' ' . trans('texts.usually_pays_in_days');
}
public function paymentTerms()
{
$client = $this->entity;

View File

@ -3161,6 +3161,8 @@ $LANG = array(
'failed_to_find_record' => 'Failed to find record',
'valid_until_days' => 'Valid Until',
'valid_until_days_help' => 'Automatically sets the <b>Valid Until</b> value on quotes to this many days in the future. Leave blank to disable.',
'usually_pays_in_days' => 'Days',
);
return $LANG;

View File

@ -212,6 +212,10 @@
<td style="text-align: right">{{ Utils::formatMoney($credit, $client->getCurrencyId()) }}</td>
</tr>
@endif
<tr>
<td><small>Usually Pays In</small></td>
<td style="text-align: right;">{{ $client->present()->usuallyPaysIn() }}</td>
</tr>
</table>
</h3>
</div>