Optimise charge table, implemented charge recording, optimised payment recording

This commit is contained in:
Deon George
2021-10-01 14:59:04 +10:00
parent c0ad46ba65
commit 7c5369203c
17 changed files with 731 additions and 48 deletions

View File

@@ -16,6 +16,7 @@ use App\Traits\NextKey;
* Attributes for accounts:
* + lid: : Local ID for account
* + sid: : System ID for account
* + name: : Account Name
*
* @package App\Models
*/
@@ -48,6 +49,11 @@ class Account extends Model implements IDs
/* RELATIONS */
public function charges()
{
return $this->hasMany(Charge::class);
}
/**
* Return the country the user belongs to
*/

View File

@@ -3,15 +3,67 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
/**
* CLEANUP NOTES:
* + Charge Date should not be null
* + Attributes should be a collection array
* + type should not be null
*/
class Charge extends Model
{
protected $table = 'ab_charge';
protected $dates = ['date_charge'];
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
protected $dates = ['charge_date'];
public $dateFormat = 'U';
public const sweep = [
// 0 => 'Daily',
// 1 => 'Weekly',
// 2 => 'Monthly',
// 3 => 'Quarterly',
// 4 => 'Semi-Annually',
// 5 => 'Annually',
6 => 'Service Rebill',
];
/* RELATIONS */
public function account()
{
return $this->belongsTo(Account::class);
}
public function service()
{
return $this->belongsTo(Service::class);
}
/* SCOPES */
public function scopeUnprocessed($query)
{
return $query
->where('active',TRUE)
->whereNotNull('charge_date')
->whereNotNull('type')
->where(function($q) {
return $q->where('processed',FALSE)
->orWhereNull('processed');
});
}
/* ATTRIBUTES */
public function getNameAttribute()
{
return sprintf('%s %s',$this->description,$this->getAttribute('attributes') ? join('|',unserialize($this->getAttribute('attributes'))) : '');
}
public function getTypeAttribute($value)
{
return Arr::get(InvoiceItem::type,$value);
}
}

View File

@@ -37,6 +37,22 @@ class InvoiceItem extends Model
// Array of items that can be updated with PushNew
protected $pushable = ['taxes'];
public const type = [
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
];
/* RELATIONS */
public function invoice()
@@ -98,6 +114,7 @@ class InvoiceItem extends Model
public function getItemTypeNameAttribute()
{
// @todo use self::type
$types = [
1=>'Hardware', // *
2=>'Service Relocation Fee', // * Must have corresponding SERVICE_ID
@@ -118,8 +135,11 @@ class InvoiceItem extends Model
{
// * 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')));
if ($this->date_start)
return sprintf('%s [%s]','Product/Service',
(($this->date_start == $this->date_stop) || (! $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')));
else
return 'Product/Service';
// * Excess Service Item, of item 0, must have corresponding SERVICE_ID
case 5:

View File

@@ -3,11 +3,11 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Interfaces\IDs;
use Illuminate\Support\Facades\DB;
use Leenooks\Traits\ScopeActive;
use App\Traits\{NextKey,PushNew};
use App\Interfaces\IDs;
use App\Traits\PushNew;
/**
* Class Payment
@@ -74,7 +74,6 @@ class Payment extends Model implements IDs
public function scopeUnapplied($query)
{
//DB::enableQueryLog();
return $query
->select(['payments.id','payment_date','account_id','checkout_id','total_amt',DB::raw("SUM(alloc_amt) as allocated")])
->leftJoin('payment_items',['payment_items.payment_id'=>'payments.id'])

View File

@@ -17,6 +17,11 @@ class PaymentItem extends Model
/* RELATIONS */
public function invoice()
{
return $this->belongsTo(Invoice::class);
}
public function payment() {
return $this->belongsTo(Payment::class);
}