Add account next invoice

This commit is contained in:
2024-07-31 22:36:28 +10:00
parent 0b5bc9e012
commit f43748e20a
9 changed files with 109 additions and 96 deletions

View File

@@ -37,7 +37,8 @@ class ChargeController extends Controller
public function delete(Charge $o): array
{
if (Gate::allows('delete',$o)) {
$o->delete();
$o->active = FALSE;
$o->save();
return ['ok'];

View File

@@ -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
{

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -11,7 +12,6 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Leenooks\Carbon;
use Leenooks\Casts\LeenooksCarbon;
use App\Models\Product\Type;
@@ -33,8 +33,6 @@ use App\Traits\{ScopeServiceActive,ScopeServiceUserAuthorised};
* + billing_interval : The period that this service is billed for by default
* + billing_interval_string : The period that this service is billed for by default as a name
* + invoiced_to : When this service has been billed to
* + category : The type of service this is, eg: broadband, phone
* + category_name : The type of service this is, eg: Broadband, Telephone (in human friendly)
* + contract_term : The term that this service must be active
* + contract_end : The date that the contract ends for this service
* + name : Service short name with service address
@@ -615,10 +613,13 @@ class Service extends Model implements IDs
*
* @return Carbon
*/
public function getInvoiceNextAttribute(): Carbon
public function getInvoiceNextAttribute(): ?Carbon
{
$last = $this->getInvoicedToAttribute();
if ($this->stop_at && $last->greaterThan($this->stop_at))
return NULL;
return $last
? $last->addDay()
: (min($this->start_at,$this->invoice_next_at) ?: Carbon::now());
@@ -1074,9 +1075,9 @@ class Service extends Model implements IDs
if (is_null($billdate))
$billdate = $invoiced_to->clone()->addDays(config('osb.invoice_days'));
while ($invoiced_to < ($this->stop_at ?: $billdate)) {
while ($invoiced_to <= ($this->stop_at ?: $billdate)) {
$ii = new InvoiceItem;
$period = Invoice::invoice_period($invoiced_to,$this->getBillingIntervalAttribute(),$this->product->price_recur_strict);
$period = Invoice::invoice_period($invoiced_to,$this->getBillingIntervalAttribute(),(bool)$this->product->price_recur_strict);
$ii->active = TRUE;
$ii->service_id = $this->id;

View File

@@ -5,8 +5,6 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Support\Facades\Log;
use Laravel\Passport\HasApiTokens;
use Leenooks\Traits\ScopeActive;
@@ -272,41 +270,6 @@ class User extends Authenticatable implements IDs
return in_array($this->role(),['wholesaler']);
}
/**
* Get all the items for the next invoice
*
* @param bool $future
* @return DatabaseCollection
* @deprecated This should be done in accounts
*/
public function next_invoice_items(bool $future=FALSE): Collection
{
return collect();
$result = new DatabaseCollection;
$this->services->load(['invoice_items.taxes']);
foreach ($this->services as $o) {
if ($future) {
if ($o->invoice_next->subDays(config('app.invoice_inadvance'))->isPast())
continue;
} else {
if ($o->invoice_next->subDays(config('app.invoice_inadvance'))->isFuture())
continue;
}
foreach ($o->next_invoice_items($future) as $oo)
$result->push($oo);
}
$result->load([
'product.translate',
'service.type',
]);
return $result;
}
/**
* Determine what the logged in user's role is
* + Wholesaler - aka Super User