2019-07-04 04:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\{Account,Invoice};
|
|
|
|
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Show the users next invoice
|
|
|
|
*/
|
|
|
|
public function view_invoice_next(Account $o)
|
|
|
|
{
|
|
|
|
$io = new Invoice;
|
|
|
|
$io->account = $o;
|
|
|
|
|
|
|
|
// Get the account services
|
|
|
|
$s = $o->services(TRUE)
|
|
|
|
->with(['invoice_items','charges'])
|
|
|
|
->get()
|
|
|
|
->filter(function($item) {
|
|
|
|
return ! $item->suspend_billing AND ! $item->external_billing;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get our invoice due date for this invoice
|
2022-04-22 04:41:18 +00:00
|
|
|
$io->due_at = $s->min(function($item) { return $item->invoice_next; });
|
2019-07-04 04:55:05 +00:00
|
|
|
|
|
|
|
// @todo The days in advance is an application parameter
|
2022-04-22 04:41:18 +00:00
|
|
|
$io->created_at = $io->due_at->subDays(30);
|
2019-07-04 04:55:05 +00:00
|
|
|
|
|
|
|
// Work out items to add to this invoice, plus any in the next additional days
|
2022-04-22 04:41:18 +00:00
|
|
|
$days = now()->diffInDays($io->due_at)+1+7;
|
2019-07-04 04:55:05 +00:00
|
|
|
foreach ($s as $so)
|
|
|
|
{
|
|
|
|
if ($so->isInvoiceDueSoon($days))
|
|
|
|
foreach ($so->next_invoice_items() as $o)
|
|
|
|
$io->items->push($o);
|
|
|
|
}
|
|
|
|
|
2020-07-27 04:49:59 +00:00
|
|
|
return View('u.invoice.home',['o'=>$io]);
|
2019-07-04 04:55:05 +00:00
|
|
|
}
|
|
|
|
}
|