Initial invoice rendering

This commit is contained in:
Deon George
2018-08-01 17:09:38 +10:00
parent 1cde2a888a
commit c444e1d218
16 changed files with 500 additions and 28 deletions

View File

@@ -3,6 +3,8 @@
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Models\Invoice;
use App\User;
class UserHomeController extends Controller
{
@@ -11,6 +13,11 @@ class UserHomeController extends Controller
$this->middleware('auth');
}
public function invoice(Invoice $o)
{
return View('invoice',['o'=>$o]);
}
public function home()
{
switch (Auth::user()->role()) {
@@ -41,4 +48,10 @@ class UserHomeController extends Controller
{
abort(307,sprintf('http://www.graytech.net.au/u/%s/%s/%s',$type,$action,$id));
}
public function User(User $o)
{
// @todo Check authorised to see this account.
return View('userhome',['o'=>$o]);
}
}

View File

@@ -4,10 +4,10 @@ namespace App\Http\Middleware;
use Illuminate\Support\Facades\Schema;
use Closure;
use App\Models\{Site};
use Config;
use View;
use Theme;
use App\Models\Site;
/**
* Class SetSite
@@ -40,8 +40,6 @@ class SetSite
$so = (new Site)->sample();
}
Theme::set($so->theme);
// Set who we are in SETUP.
Config::set('SITE_SETUP',$so);
View::share('so',$so);

View File

@@ -36,13 +36,52 @@ class Account extends Model
return $this->belongsTo(\App\User::class);
}
public function getActiveDisplayAttribute($value)
{
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'primary' : 'danger',$this->active ? 'Active' : 'Inactive');
}
public function getCompanyAttribute($value)
{
return $value ? $value : $this->user->SurFirstName;
}
public function getActiveDisplayAttribute($value)
public function getAccountIdAttribute()
{
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'primary' : 'danger',$this->active ? 'Active' : 'Inactive');
return sprintf('%02s-%04s',$this->site_id,$this->id);
}
public function getAccountIdUrlAttribute()
{
return sprintf('<a href="/r/account/view/%s">%s</a>',$this->id,$this->account_id);
}
private function _address()
{
$return = [];
if ($this->address1)
array_push($return,$this->address1);
if ($this->address2)
array_push($return,$this->address2);
if ($this->city)
array_push($return,sprintf('%s %s %s',$this->city.(($this->state OR $this->zip) ? ',' : ''),$this->state,$this->zip));
if (! $return)
$return = ['No Address'];
return $return;
}
public function address($type='plain')
{
switch ($type)
{
case 'html' : return join('<br>',$this->_address());
case 'newline': return join("\m",$this->_address());
default:
return join("\n",$this->_address());
}
}
}

15
app/Models/Charge.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Charge extends Model
{
protected $table = 'ab_charge';
public function getNameAttribute()
{
return sprintf('%s %s',$this->description,join('|',unserialize($this->getAttribute('attributes'))));
}
}

View File

@@ -7,8 +7,8 @@ use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
protected $table = 'ab_invoice';
protected $dates = ['due_date'];
protected $with = ['items.taxes','account.country.currency','paymentitems'];
protected $dates = ['date_orig','due_date'];
protected $with = ['items.taxes','items.product','items.service','account.country.currency','paymentitems'];
protected $appends = [
'date_due',
@@ -26,6 +26,7 @@ class Invoice extends Model
];
private $_total = 0;
private $_total_tax = 0;
public function account()
{
@@ -52,14 +53,29 @@ class Invoice extends Model
return $this->due_date->format('Y-m-d');
}
public function getInvoiceDateAttribute()
{
return $this->date_orig->format('Y-m-d');
}
public function getInvoiceIdAttribute()
{
return sprintf('%02s-%04s-%04s',$this->site_id,$this->account_id,$this->id);
return sprintf('%06s',$this->id);
}
public function getInvoiceAccountIdAttribute()
{
return sprintf('%02s-%04s-%06s',$this->site_id,$this->account_id,$this->invoice_id);
}
public function getInvoiceIdUrlAttribute()
{
return sprintf('<a href="/u/invoice/view/%s">%s</a>',$this->id,$this->invoice_id);
return sprintf('<a href="/u/invoice/%s">%s</a>',$this->id,$this->invoice_account_id);
}
public function getInvoiceTextAttribute()
{
return sprintf('Thank you for using %s for your Internet Services.',config('SITE_SETUP')->site_name);
}
public function getPaidAttribute()
@@ -67,6 +83,25 @@ class Invoice extends Model
return $this->currency()->round($this->paymentitems->sum('alloc_amt'));
}
public function getSubTotalAttribute()
{
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total-$this->tax_total);
}
public function getTaxTotalAttribute()
{
if (! $this->_total_tax)
{
foreach ($this->items as $o)
{
if ($o->active)
$this->_total_tax += $this->currency()->round($o->tax);
}
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total_tax);
}
public function getTotalAttribute()
{
if (! $this->_total)
@@ -85,4 +120,44 @@ class Invoice extends Model
{
return $this->account->country->currency;
}
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);
}
$lo = $this->account->user->language;
return $return->sortBy(function ($item) use ($lo) {
return $item->name($lo);
});
}
public function product_services(Product $po)
{
$return = collect();
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');
}
}

View File

@@ -6,21 +6,80 @@ use Illuminate\Database\Eloquent\Model;
class InvoiceItem extends Model
{
protected $dates = ['date_start','date_stop'];
protected $table = 'ab_invoice_item';
protected $with = ['taxes'];
private $_tax = 0;
public function product()
{
return $this->belongsTo(Product::class);
}
public function invoice()
{
return $this->belongsTo(Invoice::class);
}
public function service()
{
return $this->belongsTo(Service::class);
}
public function taxes()
{
return $this->hasMany(InvoiceItemTax::class);
}
public function getItemTypeNameAttribute()
{
$types = [
1=>'Hardware', // *
2=>'Service Relocation Fee', // * Must have corresponding SERVICE_ID
3=>'Service Change', // * Must have corresponding SERVICE_ID
4=>'Service Connection', // * Must have corresponding SERVICE_ID
6=>'Service Cancellation', // * Must have corresponding SERVICE_ID
7=>'Extra Product/Service Charge', // * Service Billing in advance, Must have corresponding SERVICE_ID
8=>'Product Addition', // * Additional Product Customisation, Must have corresponding SERVICE_ID
120=>'Credit/Debit Transfer', // * SERVICE_ID is NULL, MODULE_ID is NULL, MODULE_REF is NULL : INVOICE_ID is NOT NULL
123=>'Shipping',
124=>'Late Payment Fee', // * SERVICE_ID is NULL, MODULE_ID = CHECKOUT MODULE,
125=>'Payment Fee', // * SERVICE_ID is NULL, MODULE_ID = CHECKOUT MODULE, MODULE_REF = CHECKOUT NAME
126=>'Other', // * MODEL_ID should be a module
127=>'Rounding', // * SERVICE_ID is NULL, MODULE_ID is NULL, MODULE_REF is NULL
];
switch ($this->item_type)
{
// * Line Charge Topic on Invoice.
case 0:
return sprintf('%s [%s]','Product/Service',
$this->date_start == $this->date_stop ? $this->date_start->format('Y-m-d') : sprintf('%s -> %s',$this->date_start->format('Y-m-d'),$this->date_stop->format('Y-m-d')));
// * Excess Service Item, of item 0, must have corresponding SERVICE_ID
case 5:
return sprintf('%s [%s] (%s)','Excess Usage',
$this->date_start == $this->date_stop ? $this->date_start->format('Y-m-d') : sprintf('%s -> %s',$this->date_start->format('Y-m-d'),$this->date_stop->format('Y-m-d')),
$this->module_text()
);
default:
return array_get($types,$this->item_type,'Unknown');
}
}
public function module_text()
{
switch ($this->module_id)
{
// Charges Module
case 30: return Charge::findOrFail($this->module_ref)->name;
default: abort(500,'Unable to handle '.$this->module_id);
}
}
public function getSubTotalAttribute()
{
return $this->quantity * $this->price_base;

View File

@@ -19,10 +19,16 @@ class Product extends Model
return $this->hasMany(Service::class);
}
public function getProductIdAttribute()
{
return sprintf('#%04s',$this->id);
}
/**
* Get the language name
*
* @param Language $lo
* @return string Product Name
*/
public function name(Language $lo)
{

View File

@@ -12,11 +12,11 @@ class Service extends Model
protected $appends = [
'category',
'name',
'next_invoice',
'product_name',
'service_id',
'service_id_url',
'service_name',
'status',
];
protected $visible = [
@@ -24,11 +24,11 @@ class Service extends Model
'category',
'data_orig',
'id',
'name',
'next_invoice',
'product_name',
'service_id',
'service_id_url',
'service_name',
'status',
];
@@ -80,6 +80,14 @@ class Service extends Model
return $this->product->prod_plugin_file;
}
public function getNameAttribute()
{
if (! isset($this->getServiceDetail()->name))
return 'Unknown';
return $this->getServiceDetail()->name;
}
public function getNextInvoiceAttribute()
{
return $this->date_next_invoice ? $this->date_next_invoice->format('Y-m-d') : NULL;
@@ -127,14 +135,6 @@ class Service extends Model
return sprintf('<a href="/u/service/view/%s">%s</a>',$this->id,$this->service_id);
}
public function getServiceNameAttribute()
{
if (! isset($this->getServiceDetail()->name))
return 'Unknown';
return $this->getServiceDetail()->name;
}
public function getServiceNumberAttribute()
{
return sprintf('%02s.%04s.%04s',$this->site_id,$this->account_id,$this->id);

View File

@@ -21,6 +21,11 @@ class Site extends Model
return $this->hasMany(SiteDetails::class);
}
public function language()
{
return $this->belongsTo(Language::class);
}
public function __get($key)
{
// @todo Not sure if this is functioning correctly?

View File

@@ -58,6 +58,11 @@ class User extends Authenticatable
return $this->hasMany(static::class,'parent_id','id')->with('clients');
}
public function language()
{
return $this->belongsTo(Models\Language::class);
}
public function invoices()
{
return $this->hasManyThrough(Models\Invoice::class,Models\Account::class);
@@ -120,6 +125,13 @@ class User extends Authenticatable
->filter();
}
public function getLanguageAttribute($value)
{
if (is_null($this->language_id))
return config('SITE_SETUP')->language;
dd(__METHOD__,$value,config('SITE_SETUP')->language);
}
public function getPaymentHistoryAttribute()
{
return $this->payments
@@ -152,6 +164,11 @@ class User extends Authenticatable
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->user_id);
}
public function isAdmin($id)
{
return $id AND in_array($this->role(),['wholesaler','reseller']) AND in_array($id,$this->all_accounts()->pluck('id')->toArray());
}
public function scopeActive()
{
return $this->where('active',TRUE);