Rework payment tables, enable payment editing
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user