Add account next invoice
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -252,11 +253,59 @@ class Account extends Model implements IDs
|
||||
|
||||
/* METHODS */
|
||||
|
||||
public function invoice_next(): Collection
|
||||
{
|
||||
// Collect all the invoice items for our active services
|
||||
$nextdate = ($x=$this
|
||||
->services_active
|
||||
->filter(fn($item)=>$item->isBilled() && $item->invoice_next)
|
||||
->sortBy(fn($item)=>(string)$item->invoice_next))
|
||||
->first()
|
||||
?->invoice_next
|
||||
->clone();
|
||||
|
||||
// Add any additional items that will be invoiced 30 days
|
||||
$nextitemsdate = max($nextdate,Carbon::now())
|
||||
->clone()
|
||||
->addMonth()
|
||||
->subDay()
|
||||
->endOfday();
|
||||
|
||||
$items = $x
|
||||
->filter(fn($item)=>$item->invoice_next->lessThan($nextitemsdate))
|
||||
->sortBy(fn($item)=>$item->invoice_next.$item->name)
|
||||
->map(fn($item)=>$item->next_invoice_items($nextitemsdate))
|
||||
->flatten();
|
||||
|
||||
// Add any account charges (charges with no active service)
|
||||
foreach ($this->charges->filter(function($item) { return $item->unprocessed && ((! $this->service_id) || (! $item->service->isBilled())); }) as $oo) {
|
||||
$ii = new InvoiceItem;
|
||||
|
||||
$ii->active = TRUE;
|
||||
$ii->service_id = $oo->service_id;
|
||||
$ii->product_id = $this->product_id;
|
||||
$ii->quantity = $oo->quantity;
|
||||
$ii->item_type = $oo->type;
|
||||
$ii->price_base = $oo->amount;
|
||||
$ii->start_at = $oo->start_at;
|
||||
$ii->stop_at = $oo->stop_at;
|
||||
$ii->module_id = 30; // @todo This shouldnt be hard coded
|
||||
$ii->module_ref = $oo->id;
|
||||
$ii->site_id = 1; // @todo
|
||||
|
||||
$ii->addTaxes($this->country->taxes);
|
||||
$items->push($ii);
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* List of invoices (summary) for this account
|
||||
*
|
||||
* @param Collection|NULL $invoices
|
||||
* @return Collection
|
||||
* @param bool $all
|
||||
* @return Builder
|
||||
*/
|
||||
public function invoiceSummary(Collection $invoices=NULL,bool $all=FALSE): Builder
|
||||
{
|
||||
|
Reference in New Issue
Block a user