2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-07-23 07:25:26 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Leenooks\Traits\ScopeActive;
|
2021-10-01 04:59:04 +00:00
|
|
|
|
|
|
|
use App\Interfaces\IDs;
|
|
|
|
use App\Traits\PushNew;
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Class Payment
|
|
|
|
* Payments that belong to an account
|
|
|
|
*
|
|
|
|
* Attributes for payments:
|
|
|
|
* + lid : Local ID for payment
|
2022-06-13 05:46:38 +00:00
|
|
|
* + paid_at : Date payment received
|
2021-06-29 06:36:34 +00:00
|
|
|
* + sid : System ID for payment
|
|
|
|
* + total : Payment total
|
2021-07-23 07:25:26 +00:00
|
|
|
* + balance : Remaining credit on payment
|
2021-06-29 06:36:34 +00:00
|
|
|
*
|
|
|
|
* @package App\Models
|
|
|
|
*/
|
|
|
|
class Payment extends Model implements IDs
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2022-06-13 07:24:43 +00:00
|
|
|
use PushNew,ScopeActive;
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2022-04-22 05:23:08 +00:00
|
|
|
protected $dates = [
|
|
|
|
'paid_at',
|
|
|
|
];
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2020-07-27 04:49:59 +00:00
|
|
|
// Array of items that can be updated with PushNew
|
|
|
|
protected $pushable = ['items'];
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
// Any balance below this we'll assume its all used.
|
|
|
|
private const threshold = 0.05;
|
|
|
|
|
2022-05-11 23:09:49 +00:00
|
|
|
/* INTERFACES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function account()
|
|
|
|
{
|
2018-07-13 04:53:44 +00:00
|
|
|
return $this->belongsTo(Account::class);
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
public function checkout()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Checkout::class);
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function items()
|
|
|
|
{
|
|
|
|
return $this->hasMany(PaymentItem::class);
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
/* SCOPES */
|
2021-06-29 06:36:34 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-23 07:25:26 +00:00
|
|
|
* Search for a record
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @param string $term
|
2021-06-29 06:36:34 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-07-23 07:25:26 +00:00
|
|
|
public function scopeSearch($query,string $term)
|
|
|
|
{
|
|
|
|
// Build our where clause
|
|
|
|
$query->where('id','like','%'.$term.'%');
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeUnapplied($query)
|
|
|
|
{
|
|
|
|
return $query
|
2022-06-13 05:46:38 +00:00
|
|
|
->select(['payments.id','paid_at','account_id','checkout_id','total_amt',DB::raw("SUM(amount) as allocated")])
|
2021-07-29 03:11:14 +00:00
|
|
|
->leftJoin('payment_items',['payment_items.payment_id'=>'payments.id'])
|
2022-06-13 05:46:38 +00:00
|
|
|
->groupBy(['payments.id','paid_at','total_amt','account_id','checkout_id'])
|
2021-07-29 03:11:14 +00:00
|
|
|
->having(DB::raw('ROUND(total_amt-IFNULL(allocated,0),2)'),'>',self::threshold);
|
2021-07-23 07:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
2022-05-11 23:09:49 +00:00
|
|
|
/**
|
|
|
|
* Work out the remaining amount from this payment that has not been allocated.
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
2021-07-23 07:25:26 +00:00
|
|
|
public function getBalanceAttribute(): float
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2022-04-22 05:23:08 +00:00
|
|
|
$balance = $this->getTotalAttribute()-$this->items->sum('amount');
|
2021-07-23 07:25:26 +00:00
|
|
|
|
|
|
|
return ($balance < self::threshold) ? 0 : $balance;
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
2018-06-19 12:31:49 +00:00
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
2022-05-11 23:09:49 +00:00
|
|
|
* The total of the payment.
|
2021-06-29 06:36:34 +00:00
|
|
|
*
|
2022-05-11 23:09:49 +00:00
|
|
|
* @return float
|
|
|
|
* @todo Change the DB column to total
|
2021-06-29 06:36:34 +00:00
|
|
|
*/
|
2021-07-23 07:25:26 +00:00
|
|
|
public function getTotalAttribute(): float
|
2018-06-19 12:31:49 +00:00
|
|
|
{
|
2021-07-23 07:25:26 +00:00
|
|
|
return sprintf('%3.2f',$this->total_amt);
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|