Rework payment tables, enable payment editing
This commit is contained in:
@@ -95,7 +95,7 @@ class Account extends Model implements IDs
|
||||
*
|
||||
* @param $query
|
||||
* @param string $term
|
||||
* @return
|
||||
* @return mixed
|
||||
*/
|
||||
public function scopeSearch($query,string $term)
|
||||
{
|
||||
|
@@ -106,7 +106,7 @@ class Invoice extends Model implements IDs
|
||||
*/
|
||||
public function getDueAttribute(): float
|
||||
{
|
||||
return sprintf('%4.'.$this->currency()->rounding.'f',$this->total-$this->paid);
|
||||
return sprintf('%3.2f',$this->getTotalAttribute()-$this->getPaidAttribute());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,28 +162,27 @@ class Invoice extends Model implements IDs
|
||||
*/
|
||||
public function getPaidAttribute(): float
|
||||
{
|
||||
if (! $this->_paid)
|
||||
$this->_paid = $this->currency()->round(
|
||||
$this->paymentitems
|
||||
->filter(function($item) { return ! $item->payment->pending_status; })
|
||||
->sum('alloc_amt'));
|
||||
|
||||
return $this->_paid;
|
||||
return $this->paymentitems
|
||||
->filter(function($item) { return ! $item->payment->pending_status; })
|
||||
->sum('alloc_amt');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date that the invoice was paid in full.
|
||||
* We assume the last payment received pays it in full.
|
||||
* We assume the last payment received pays it in full, if its fully paid.
|
||||
*
|
||||
* @return Carbon|null
|
||||
*/
|
||||
public function getPaidDateAttribute(): ?Carbon
|
||||
{
|
||||
if ($this->getDueAttribute())
|
||||
return NULL;
|
||||
|
||||
$o = $this->payments
|
||||
->filter(function($item) { return ! $item->pending_status; })
|
||||
->last();
|
||||
|
||||
return $o ? $o->date_payment : NULL;
|
||||
return $o ? $o->payment_date : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,10 +192,9 @@ class Invoice extends Model implements IDs
|
||||
*/
|
||||
public function getPaidPendingAttribute(): float
|
||||
{
|
||||
return $this->currency()->round(
|
||||
$this->paymentitems
|
||||
->filter(function($item) { return $item->payment->pending_status; })
|
||||
->sum('alloc_amt'));
|
||||
return $this->paymentitems
|
||||
->filter(function($item) { return $item->payment->pending_status; })
|
||||
->sum('alloc_amt');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -238,7 +236,7 @@ class Invoice extends Model implements IDs
|
||||
*/
|
||||
public function getTotalSubAttribute(): float
|
||||
{
|
||||
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total-$this->tax_total);
|
||||
return $this->items->where('active',TRUE)->sum('sub_total');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -259,29 +257,17 @@ class Invoice extends Model implements IDs
|
||||
*/
|
||||
public function getTotalTaxAttribute(): float
|
||||
{
|
||||
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);
|
||||
return $this->items->where('active',TRUE)->sum('tax');
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoice total due
|
||||
*
|
||||
* @return string
|
||||
* @return float
|
||||
*/
|
||||
public function getTotalAttribute(): float
|
||||
{
|
||||
if (! $this->_total)
|
||||
foreach ($this->items as $o) {
|
||||
if ($o->active)
|
||||
$this->_total += $this->currency()->round($o->total);
|
||||
}
|
||||
|
||||
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total);
|
||||
return $this->getTotalSubAttribute()+$this->getTotalTaxAttribute();
|
||||
}
|
||||
|
||||
public function currency()
|
||||
|
@@ -10,6 +10,17 @@ use App\Traits\NextKey;
|
||||
use App\Traits\PushNew;
|
||||
use Leenooks\Carbon;
|
||||
|
||||
/**
|
||||
* Class Invoice Items
|
||||
* Items that belong on an invoice
|
||||
*
|
||||
* Attributes for services:
|
||||
* + date_start : Start date
|
||||
* + date_stop : End date
|
||||
* + sub_total : Value of item
|
||||
* + tax : Total of all taxes
|
||||
* + total : Total including taxes
|
||||
*/
|
||||
class InvoiceItem extends Model
|
||||
{
|
||||
use NextKey,PushNew;
|
||||
@@ -26,7 +37,7 @@ class InvoiceItem extends Model
|
||||
// Array of items that can be updated with PushNew
|
||||
protected $pushable = ['taxes'];
|
||||
|
||||
private $_tax = 0;
|
||||
/* RELATIONS */
|
||||
|
||||
public function invoice()
|
||||
{
|
||||
@@ -53,7 +64,7 @@ class InvoiceItem extends Model
|
||||
return $this->hasMany(InvoiceItemTax::class);
|
||||
}
|
||||
|
||||
/** ATTRIBUTES **/
|
||||
/* ATTRIBUTES */
|
||||
|
||||
/**
|
||||
* Start date for the invoice item line
|
||||
@@ -122,30 +133,37 @@ class InvoiceItem extends Model
|
||||
}
|
||||
}
|
||||
|
||||
public function getSubTotalAttribute()
|
||||
/**
|
||||
* Sub total of item
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getSubTotalAttribute(): float
|
||||
{
|
||||
return $this->quantity * $this->price_base;
|
||||
return sprintf('%3.2f',$this->quantity * $this->price_base);
|
||||
}
|
||||
|
||||
public function getTaxAttribute()
|
||||
/**
|
||||
* Total of all taxes
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTaxAttribute(): float
|
||||
{
|
||||
if (! $this->_tax)
|
||||
{
|
||||
foreach ($this->taxes as $o)
|
||||
{
|
||||
$this->_tax += $o->amount;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_tax;
|
||||
return sprintf('%3.2f',$this->taxes->sum('amount'));
|
||||
}
|
||||
|
||||
public function getTotalAttribute()
|
||||
/**
|
||||
* Total including taxes
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getTotalAttribute(): float
|
||||
{
|
||||
return $this->tax + $this->sub_total;
|
||||
return sprintf('%3.2f',$this->getSubTotalAttribute()+$this->getTaxAttribute());
|
||||
}
|
||||
|
||||
/** FUNCTIONS **/
|
||||
/* METHODS */
|
||||
|
||||
/**
|
||||
* Add taxes to this record
|
||||
|
@@ -5,6 +5,8 @@ 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};
|
||||
|
||||
/**
|
||||
@@ -16,27 +18,26 @@ use App\Traits\{NextKey,PushNew};
|
||||
* + payment_date : Date payment received
|
||||
* + sid : System ID for payment
|
||||
* + total : Payment total
|
||||
* + balance : Remaining credit on payment
|
||||
*
|
||||
* @package App\Models
|
||||
*/
|
||||
class Payment extends Model implements IDs
|
||||
{
|
||||
use NextKey,PushNew;
|
||||
|
||||
const RECORD_ID = 'payment';
|
||||
public $incrementing = FALSE;
|
||||
use PushNew;
|
||||
|
||||
const CREATED_AT = 'date_orig';
|
||||
const UPDATED_AT = 'date_last';
|
||||
|
||||
protected $table = 'ab_payment';
|
||||
protected $dates = ['date_payment'];
|
||||
protected $dates = ['payment_date'];
|
||||
protected $dateFormat = 'U';
|
||||
//protected $with = ['account.country.currency','items'];
|
||||
|
||||
// Array of items that can be updated with PushNew
|
||||
protected $pushable = ['items'];
|
||||
|
||||
// Any balance below this we'll assume its all used.
|
||||
private const threshold = 0.05;
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
public function account()
|
||||
@@ -44,20 +45,49 @@ class Payment extends Model implements IDs
|
||||
return $this->belongsTo(Account::class);
|
||||
}
|
||||
|
||||
public function checkout()
|
||||
{
|
||||
return $this->belongsTo(Checkout::class);
|
||||
}
|
||||
|
||||
public function items()
|
||||
{
|
||||
return $this->hasMany(PaymentItem::class);
|
||||
}
|
||||
|
||||
/* ATTRIBUTES */
|
||||
/* SCOPES */
|
||||
|
||||
/**
|
||||
* Search for a record
|
||||
*
|
||||
* @param $query
|
||||
* @param string $term
|
||||
* @return mixed
|
||||
* @deprecated use date_payment directly.
|
||||
*/
|
||||
public function getDatePaidAttribute()
|
||||
public function scopeSearch($query,string $term)
|
||||
{
|
||||
return $this->date_payment->format('Y-m-d');
|
||||
// Build our where clause
|
||||
$query->where('id','like','%'.$term.'%');
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function scopeUnapplied($query)
|
||||
{
|
||||
return $query
|
||||
->select([DB::raw('payment_id AS id'),'payment_date','account_id','checkout_id','total_amt',DB::raw("SUM(alloc_amt) as allocated")])
|
||||
->join('payment_items',['payment_items.payment_id'=>'payments.id'])
|
||||
->groupBy(['payment_id','payment_date','total_amt','account_id','checkout_id'])
|
||||
->having(DB::raw("ROUND(total_amt-allocated,2)"),'>',self::threshold);
|
||||
}
|
||||
|
||||
/* ATTRIBUTES */
|
||||
|
||||
public function getBalanceAttribute(): float
|
||||
{
|
||||
$balance = $this->getTotalAttribute()-$this->items->sum('alloc_amt');
|
||||
|
||||
return ($balance < self::threshold) ? 0 : $balance;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,13 +110,8 @@ class Payment extends Model implements IDs
|
||||
return sprintf('%02s-%04s#%s',$this->site_id,$this->account_id,$this->getLIDattribute());
|
||||
}
|
||||
|
||||
public function getTotalAttribute()
|
||||
public function getTotalAttribute(): float
|
||||
{
|
||||
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total_amt);
|
||||
}
|
||||
|
||||
public function currency()
|
||||
{
|
||||
return $this->account->country->currency;
|
||||
return sprintf('%3.2f',$this->total_amt);
|
||||
}
|
||||
}
|
@@ -8,19 +8,29 @@ use App\Traits\{NextKey,PushNew};
|
||||
|
||||
class PaymentItem extends Model
|
||||
{
|
||||
use NextKey,PushNew;
|
||||
use PushNew;
|
||||
const RECORD_ID = 'payment_item';
|
||||
public $incrementing = FALSE;
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
const CREATED_AT = 'date_orig';
|
||||
const UPDATED_AT = 'date_last';
|
||||
|
||||
protected $table = 'ab_payment_item';
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
public function payment() {
|
||||
return $this->belongsTo(Payment::class);
|
||||
}
|
||||
|
||||
/* ATTRIBUTES */
|
||||
|
||||
/**
|
||||
* If our amount is negative, and invoice_id is null, then this is a reversal.
|
||||
*
|
||||
* @param $value
|
||||
* @return float
|
||||
*/
|
||||
public function getAllocAmtAttribute($value): float
|
||||
{
|
||||
return (is_null($this->invoice_id) && $value < 0) ? -$value : $value;
|
||||
}
|
||||
}
|
@@ -241,7 +241,7 @@ class User extends Authenticatable
|
||||
public function getPaymentHistoryAttribute()
|
||||
{
|
||||
return $this->payments
|
||||
->sortBy('date_payment')
|
||||
->sortBy('payment_date')
|
||||
->reverse();
|
||||
}
|
||||
|
||||
@@ -367,6 +367,8 @@ class User extends Authenticatable
|
||||
$result->push($o);
|
||||
}
|
||||
|
||||
$result->load('user.accounts');
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -623,7 +625,7 @@ class User extends Authenticatable
|
||||
->select([
|
||||
DB::raw('ab_payment.id AS id'),
|
||||
'date_orig',
|
||||
'date_payment',
|
||||
'payment_date',
|
||||
'total_amt',
|
||||
//'fees_amt',
|
||||
DB::raw('total_amt-allocate AS balance'),
|
||||
|
Reference in New Issue
Block a user