mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 20:00:33 -04:00
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:
parent
8b4976cacf
commit
048ee7181e
@ -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) {
|
Client::creating(function ($client) {
|
||||||
|
@ -46,6 +46,12 @@ class ClientPresenter extends EntityPresenter
|
|||||||
return $account->formatMoney($client->paid_to_date, $client);
|
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()
|
public function paymentTerms()
|
||||||
{
|
{
|
||||||
$client = $this->entity;
|
$client = $this->entity;
|
||||||
|
@ -3161,6 +3161,8 @@ $LANG = array(
|
|||||||
'failed_to_find_record' => 'Failed to find record',
|
'failed_to_find_record' => 'Failed to find record',
|
||||||
'valid_until_days' => 'Valid Until',
|
'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.',
|
'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;
|
return $LANG;
|
||||||
|
@ -212,6 +212,10 @@
|
|||||||
<td style="text-align: right">{{ Utils::formatMoney($credit, $client->getCurrencyId()) }}</td>
|
<td style="text-align: right">{{ Utils::formatMoney($credit, $client->getCurrencyId()) }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
@endif
|
@endif
|
||||||
|
<tr>
|
||||||
|
<td><small>Usually Pays In</small></td>
|
||||||
|
<td style="text-align: right;">{{ $client->present()->usuallyPaysIn() }}</td>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</h3>
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user