115 lines
2.4 KiB
PHP
115 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Leenooks\Traits\ScopeActive;
|
|
|
|
use App\Interfaces\IDs;
|
|
use App\Traits\PushNew;
|
|
|
|
/**
|
|
* Class Payment
|
|
* Payments that belong to an account
|
|
*
|
|
* Attributes for payments:
|
|
* + lid : Local ID for payment
|
|
* + 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 PushNew;
|
|
|
|
protected $dates = [
|
|
'paid_at',
|
|
];
|
|
|
|
// 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()
|
|
{
|
|
return $this->belongsTo(Account::class);
|
|
}
|
|
|
|
public function checkout()
|
|
{
|
|
return $this->belongsTo(Checkout::class);
|
|
}
|
|
|
|
public function items()
|
|
{
|
|
return $this->hasMany(PaymentItem::class);
|
|
}
|
|
|
|
/* SCOPES */
|
|
|
|
/**
|
|
* Search for a record
|
|
*
|
|
* @param $query
|
|
* @param string $term
|
|
* @return mixed
|
|
*/
|
|
public function scopeSearch($query,string $term)
|
|
{
|
|
// Build our where clause
|
|
$query->where('id','like','%'.$term.'%');
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function scopeUnapplied($query)
|
|
{
|
|
return $query
|
|
->select(['payments.id','payment_date','account_id','checkout_id','total_amt',DB::raw("SUM(amount) as allocated")])
|
|
->leftJoin('payment_items',['payment_items.payment_id'=>'payments.id'])
|
|
->groupBy(['payments.id','payment_date','total_amt','account_id','checkout_id'])
|
|
->having(DB::raw('ROUND(total_amt-IFNULL(allocated,0),2)'),'>',self::threshold);
|
|
}
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
public function getBalanceAttribute(): float
|
|
{
|
|
$balance = $this->getTotalAttribute()-$this->items->sum('amount');
|
|
|
|
return ($balance < self::threshold) ? 0 : $balance;
|
|
}
|
|
|
|
/**
|
|
* Payment Local ID
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getLIDattribute(): string
|
|
{
|
|
return sprintf('%06s',$this->id);
|
|
}
|
|
|
|
/**
|
|
* Payment System ID
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getSIDAttribute(): string
|
|
{
|
|
return sprintf('%02s-%04s#%s',$this->site_id,$this->account_id,$this->getLIDattribute());
|
|
}
|
|
|
|
public function getTotalAttribute(): float
|
|
{
|
|
return sprintf('%3.2f',$this->total_amt);
|
|
}
|
|
} |