2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
use Carbon\Carbon;
|
2020-05-25 07:45:17 +00:00
|
|
|
use Clarkeash\Doorman\Facades\Doorman;
|
|
|
|
use Clarkeash\Doorman\Models\Invite;
|
2018-05-20 12:53:14 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2020-05-25 07:45:17 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2021-06-29 06:36:34 +00:00
|
|
|
use Leenooks\Traits\ScopeActive;
|
|
|
|
|
|
|
|
use App\Interfaces\IDs;
|
|
|
|
use App\Traits\{NextKey,PushNew};
|
2020-04-01 12:35:06 +00:00
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Class Invoice
|
|
|
|
* Invoices that belong to an Account
|
|
|
|
*
|
|
|
|
* Attributes for services:
|
|
|
|
* + due : Balance due on an invoice
|
|
|
|
* + due_date : Date the invoice is due
|
|
|
|
* + invoice_date : Date the invoice was created
|
|
|
|
* + lid : Local ID for invoice
|
|
|
|
* + paid : Total of payments received (excluding pending)
|
|
|
|
* + paid_date : Date the invoice was paid in full
|
|
|
|
* + paid_pending : Total of pending payments received
|
|
|
|
* + sid : System ID for invoice
|
|
|
|
* + total_sub : Invoice sub-total before taxes
|
|
|
|
* + total_tax : Invoices total of taxes
|
|
|
|
* + total : Invoice total
|
|
|
|
*
|
|
|
|
* @package App\Models
|
|
|
|
*/
|
|
|
|
class Invoice extends Model implements IDs
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2021-06-29 06:36:34 +00:00
|
|
|
use NextKey,PushNew,ScopeActive;
|
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
const RECORD_ID = 'invoice';
|
|
|
|
public $incrementing = FALSE;
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
protected $table = 'ab_invoice';
|
2020-04-01 12:35:06 +00:00
|
|
|
const CREATED_AT = 'date_orig';
|
|
|
|
const UPDATED_AT = 'date_last';
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
protected $dates = ['date_orig','due_date'];
|
2020-04-01 12:35:06 +00:00
|
|
|
public $dateFormat = 'U';
|
2018-06-19 12:31:49 +00:00
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
public const BILL_WEEKLY = 0;
|
|
|
|
public const BILL_MONTHLY = 1;
|
|
|
|
public const BILL_QUARTERLY = 2;
|
|
|
|
public const BILL_SEMI_YEARLY = 3;
|
|
|
|
public const BILL_YEARLY = 4;
|
|
|
|
public const BILL_TWOYEARS = 5;
|
|
|
|
public const BILL_THREEYEARS = 6;
|
|
|
|
public const BILL_FOURYEARS = 7;
|
|
|
|
public const BILL_FIVEYEARS = 8;
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
/* Our available billing periods */
|
2022-04-22 00:36:41 +00:00
|
|
|
// @todo change this to a function - with billing_name()?
|
2021-12-24 01:14:01 +00:00
|
|
|
public const billing_periods = [
|
|
|
|
0 => [
|
|
|
|
'name' => 'Weekly',
|
|
|
|
'interval' => 0.25,
|
|
|
|
],
|
|
|
|
1 => [
|
|
|
|
'name' => 'Monthly',
|
|
|
|
'interval' => 1,
|
|
|
|
],
|
|
|
|
2 => [
|
|
|
|
'name' => 'Quarterly',
|
|
|
|
'interval' => 3,
|
|
|
|
],
|
|
|
|
3 => [
|
|
|
|
'name' => 'Semi-Annually',
|
|
|
|
'interval' => 6,
|
|
|
|
],
|
|
|
|
4 => [
|
|
|
|
'name' => 'Annually',
|
|
|
|
'interval' => 12,
|
|
|
|
],
|
|
|
|
5 => [
|
|
|
|
'name' => 'Two years',
|
|
|
|
'interval' => 24,
|
|
|
|
],
|
|
|
|
6 => [
|
|
|
|
'name' => 'Three Years',
|
|
|
|
'interval' => 36,
|
|
|
|
],
|
|
|
|
7 => [
|
|
|
|
'name' => 'Four Years',
|
|
|
|
'interval' => 48,
|
|
|
|
],
|
|
|
|
8 => [
|
|
|
|
'name' => 'Five Years',
|
|
|
|
'interval' => 60,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-07-06 06:02:59 +00:00
|
|
|
// Array of items that can be updated with PushNew
|
|
|
|
protected $pushable = ['items'];
|
|
|
|
|
2021-07-02 04:35:43 +00:00
|
|
|
/*
|
2020-02-10 11:07:46 +00:00
|
|
|
protected $with = [
|
|
|
|
'account.country.currency',
|
|
|
|
'items.taxes',
|
|
|
|
'paymentitems'
|
|
|
|
];
|
2021-07-02 04:35:43 +00:00
|
|
|
*/
|
2020-02-10 11:07:46 +00:00
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
// Caching variables
|
|
|
|
private int $_paid = 0;
|
|
|
|
private int $_total = 0;
|
|
|
|
private int $_total_tax = 0;
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
/* STATIC */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This works out what multiplier to use to change billing periods
|
|
|
|
*
|
|
|
|
* @param int $source
|
|
|
|
* @param int $target
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public static function billing_change(int $source,int $target): float
|
|
|
|
{
|
|
|
|
return Arr::get(self::billing_periods,$target.'.interval')/Arr::get(self::billing_periods,$source.'.interval');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the name for the billing interval
|
|
|
|
*
|
|
|
|
* @param int $interval
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function billing_name(int $interval): string
|
|
|
|
{
|
|
|
|
$interval = collect(self::billing_periods)->get($interval);
|
|
|
|
|
|
|
|
return Arr::get($interval,'name','Unknown');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of months in the billing interval
|
|
|
|
*
|
|
|
|
* @param int $interval
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function billing_period(int $interval): int
|
|
|
|
{
|
|
|
|
$interval = collect(self::billing_periods)->get($interval);
|
|
|
|
|
|
|
|
return Arr::get($interval,'interval',0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a contract in months, this will calculate the number of billing intervals required
|
|
|
|
*
|
|
|
|
* @param int $contract_term
|
|
|
|
* @param int $source
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function billing_term(int $contract_term,int $source): int
|
|
|
|
{
|
|
|
|
return ceil(($contract_term ?: 1)/(Arr::get(self::billing_periods,$source.'.interval') ?: 1));
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/* RELATIONS */
|
2018-05-20 12:53:14 +00:00
|
|
|
|
|
|
|
public function account()
|
|
|
|
{
|
2018-07-13 04:53:44 +00:00
|
|
|
return $this->belongsTo(Account::class);
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function items()
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2021-06-29 06:36:34 +00:00
|
|
|
return $this->hasMany(InvoiceItem::class)
|
2022-04-19 07:07:39 +00:00
|
|
|
->where('active',TRUE)
|
|
|
|
->with(['taxes','product']);
|
2021-06-29 06:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function payments()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(Payment::class,PaymentItem::class,NULL,'id',NULL,'payment_id');
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentitems()
|
|
|
|
{
|
|
|
|
return $this->hasMany(PaymentItem::class);
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/* SCOPES */
|
2020-01-12 12:42:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for a record
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @param string $term
|
2021-06-29 06:36:34 +00:00
|
|
|
* @return mixed
|
2020-01-12 12:42:32 +00:00
|
|
|
*/
|
|
|
|
public function scopeSearch($query,string $term)
|
|
|
|
{
|
|
|
|
return $query->where('id','like','%'.$term.'%');
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/* ATTRIBUTES */
|
2020-01-12 12:42:32 +00:00
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Balance due on an invoice
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDueAttribute(): float
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2021-07-23 07:25:26 +00:00
|
|
|
return sprintf('%3.2f',$this->getTotalAttribute()-$this->getPaidAttribute());
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @deprecated use self::due_date;
|
|
|
|
*/
|
2018-05-20 12:53:14 +00:00
|
|
|
public function getDateDueAttribute()
|
|
|
|
{
|
|
|
|
return $this->due_date->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Date the invoices was created
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function getInvoiceDateAttribute(): Carbon
|
2018-08-01 07:09:38 +00:00
|
|
|
{
|
2021-06-29 06:36:34 +00:00
|
|
|
return $this->date_orig;
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Get account System ID
|
|
|
|
* @return string
|
|
|
|
* @deprecated use getSIDAttribute()
|
|
|
|
*/
|
2018-08-01 07:09:38 +00:00
|
|
|
public function getInvoiceAccountIdAttribute()
|
|
|
|
{
|
2021-06-29 06:36:34 +00:00
|
|
|
return $this->getSIDAttribute();
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
// @todo Move this to a site configuration
|
2018-08-01 07:09:38 +00:00
|
|
|
public function getInvoiceTextAttribute()
|
|
|
|
{
|
2021-12-17 05:09:03 +00:00
|
|
|
return sprintf('Thank you for using %s for your Internet Services.',config('site')->site_name);
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Invoice Local ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLIDAttribute(): string
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2021-06-29 06:36:34 +00:00
|
|
|
return sprintf('%06s',$this->id);
|
2020-07-27 04:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Total of payments received for this invoice
|
|
|
|
* excluding pending payments
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getPaidAttribute(): float
|
|
|
|
{
|
2021-07-23 07:25:26 +00:00
|
|
|
return $this->paymentitems
|
|
|
|
->filter(function($item) { return ! $item->payment->pending_status; })
|
|
|
|
->sum('alloc_amt');
|
2021-06-29 06:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the date that the invoice was paid in full.
|
2021-07-23 07:25:26 +00:00
|
|
|
* We assume the last payment received pays it in full, if its fully paid.
|
2021-06-29 06:36:34 +00:00
|
|
|
*
|
|
|
|
* @return Carbon|null
|
|
|
|
*/
|
|
|
|
public function getPaidDateAttribute(): ?Carbon
|
|
|
|
{
|
2021-07-23 07:25:26 +00:00
|
|
|
if ($this->getDueAttribute())
|
|
|
|
return NULL;
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
$o = $this->payments
|
|
|
|
->filter(function($item) { return ! $item->pending_status; })
|
|
|
|
->last();
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
return $o ? $o->payment_date : NULL;
|
2021-06-29 06:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Total of pending payments received for this invoice
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getPaidPendingAttribute(): float
|
2020-07-27 04:49:59 +00:00
|
|
|
{
|
2021-07-23 07:25:26 +00:00
|
|
|
return $this->paymentitems
|
|
|
|
->filter(function($item) { return $item->payment->pending_status; })
|
|
|
|
->sum('alloc_amt');
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Total of pending payments received for this invoice
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @deprecated use getPaidPendingAttribute()
|
|
|
|
*/
|
|
|
|
public function getPendingPaidAttribute(): float
|
|
|
|
{
|
|
|
|
return $this->getPaidPendingAttribute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice System ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSIDAttribute(): string
|
|
|
|
{
|
|
|
|
return sprintf('%02s-%04s-%s',$this->site_id,$this->account_id,$this->getLIDAttribute());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get invoice subtotal before taxes
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
* @deprecated use getTotalSubAttribute()
|
|
|
|
*/
|
|
|
|
public function getSubTotalAttribute(): float
|
|
|
|
{
|
|
|
|
return $this->getTotalSubAttribute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get invoice subtotal before taxes
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getTotalSubAttribute(): float
|
2018-08-01 07:09:38 +00:00
|
|
|
{
|
2021-07-23 07:25:26 +00:00
|
|
|
return $this->items->where('active',TRUE)->sum('sub_total');
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Get the invoices taxes total
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
* @deprecated use getTotalTaxAttribute();
|
|
|
|
*/
|
|
|
|
public function getTaxTotalAttribute(): float
|
|
|
|
{
|
|
|
|
return $this->getTotalTaxAttribute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the invoices taxes total
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getTotalTaxAttribute(): float
|
2018-08-01 07:09:38 +00:00
|
|
|
{
|
2021-07-23 07:25:26 +00:00
|
|
|
return $this->items->where('active',TRUE)->sum('tax');
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Invoice total due
|
|
|
|
*
|
2021-07-23 07:25:26 +00:00
|
|
|
* @return float
|
2021-06-29 06:36:34 +00:00
|
|
|
*/
|
|
|
|
public function getTotalAttribute(): float
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2021-07-23 07:25:26 +00:00
|
|
|
return $this->getTotalSubAttribute()+$this->getTotalTaxAttribute();
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->account->country->currency;
|
|
|
|
}
|
2018-08-01 07:09:38 +00:00
|
|
|
|
2020-05-25 07:45:17 +00:00
|
|
|
/**
|
|
|
|
* Return a download link for non-auth downloads
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function download_link(): string
|
|
|
|
{
|
|
|
|
// Re-use an existing code
|
|
|
|
$io = Invite::where('for',$this->account->user->email)->first();
|
|
|
|
|
|
|
|
$tokendate = ($x=Carbon::now()->addDays(21)) > ($y=$this->due_date->addDays(21)) ? $x : $y;
|
|
|
|
|
|
|
|
// Extend the expire date
|
|
|
|
if ($io AND ($tokendate > $io->valid_until)) {
|
|
|
|
$io->valid_until = $tokendate;
|
|
|
|
$io->save();
|
|
|
|
}
|
|
|
|
|
2020-06-02 07:19:24 +00:00
|
|
|
$code = (! $io) ? Doorman::generate()->for($this->account->user->email)->uses(0)->expiresOn($tokendate)->make()->first()->code : $io->code;
|
2020-05-25 07:45:17 +00:00
|
|
|
|
|
|
|
return url('u/invoice',[$this->id,'email',$code]);
|
|
|
|
}
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
public function products()
|
|
|
|
{
|
|
|
|
$return = collect();
|
|
|
|
|
|
|
|
foreach ($this->items->groupBy('product_id') as $o)
|
|
|
|
{
|
|
|
|
$po = $o->first()->product;
|
|
|
|
$po->count = count($o->pluck('service_id')->unique());
|
|
|
|
|
|
|
|
$return->push($po);
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
return $return->sortBy(function ($item) {
|
2021-12-24 01:14:01 +00:00
|
|
|
return $item->name;
|
2018-08-01 07:09:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function product_services(Product $po)
|
|
|
|
{
|
|
|
|
$return = collect();
|
|
|
|
|
2019-07-04 04:55:05 +00:00
|
|
|
$this->items->load(['service']);
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
foreach ($this->items->filter(function ($item) use ($po) {
|
|
|
|
return $item->product_id == $po->id;
|
|
|
|
}) as $o)
|
|
|
|
{
|
|
|
|
$so = $o->service;
|
|
|
|
$return->push($so);
|
|
|
|
};
|
|
|
|
|
|
|
|
return $return->unique()->sortBy('name');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function product_service_items(Product $po,Service $so)
|
|
|
|
{
|
|
|
|
return $this->items->filter(function ($item) use ($po,$so) {
|
|
|
|
return $item->product_id == $po->id AND $item->service_id == $so->id;
|
|
|
|
})->filter()->sortBy('item_type');
|
|
|
|
}
|
2020-04-01 12:35:06 +00:00
|
|
|
|
2020-05-25 07:45:17 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
* @todo Ugly hack to update reminders
|
|
|
|
*/
|
|
|
|
public function reminders(string $key) {
|
|
|
|
$r = unserialize($this->reminders);
|
|
|
|
|
|
|
|
if (! Arr::get($r,$key)) {
|
|
|
|
$r[$key] = time();
|
|
|
|
return serialize($r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
/**
|
|
|
|
* Automatically set our due_date at save time.
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function save(array $options = []) {
|
|
|
|
// Automatically set the date_due attribute for new records.
|
|
|
|
if (! $this->exists AND ! $this->due_date) {
|
|
|
|
$this->due_date = $this->items->min('date_start');
|
|
|
|
|
|
|
|
// @todo This 7 days should be sysetm configurable
|
|
|
|
if (($x=Carbon::now()->addDay(7)) > $this->due_date)
|
|
|
|
$this->due_date = $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::save($options);
|
|
|
|
}
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|