fix tests
This commit is contained in:
@@ -2,9 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DiscountVoucher extends BaseModel
|
||||
{
|
||||
@@ -31,11 +29,11 @@ class DiscountVoucher extends BaseModel
|
||||
|
||||
protected $casts = [
|
||||
'discount_value' => 'decimal:2',
|
||||
'max_uses' => 'integer',
|
||||
'times_used' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
'valid_from' => 'date',
|
||||
'valid_until' => 'date',
|
||||
'max_uses' => 'integer',
|
||||
'times_used' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
'valid_from' => 'date',
|
||||
'valid_until' => 'date',
|
||||
];
|
||||
|
||||
/* =========================
|
||||
@@ -90,8 +88,8 @@ class DiscountVoucher extends BaseModel
|
||||
|
||||
private function validateDatesOrder(): void
|
||||
{
|
||||
if (!empty($this->valid_from) && !empty($this->valid_until)) {
|
||||
$from = Carbon::parse($this->valid_from)->toDateString();
|
||||
if (! empty($this->valid_from) && ! empty($this->valid_until)) {
|
||||
$from = Carbon::parse($this->valid_from)->toDateString();
|
||||
$until = Carbon::parse($this->valid_until)->toDateString();
|
||||
if ($from > $until) {
|
||||
// In Laravel, prefer validating in FormRequest.
|
||||
@@ -123,24 +121,24 @@ class DiscountVoucher extends BaseModel
|
||||
// Left join usages for this student only
|
||||
->leftJoin('discount_usages as u', function ($join) use ($studentId) {
|
||||
$join->on('u.voucher_id', '=', 'v.id')
|
||||
->where('u.student_id', '=', (int) $studentId);
|
||||
->where('u.student_id', '=', (int) $studentId);
|
||||
})
|
||||
->where('v.code', $code)
|
||||
->where('v.is_active', 1)
|
||||
// valid_from is null or <= today
|
||||
->where(function ($w) use ($today) {
|
||||
$w->whereNull('v.valid_from')
|
||||
->orWhere('v.valid_from', '<=', $today);
|
||||
->orWhere('v.valid_from', '<=', $today);
|
||||
})
|
||||
// valid_until is null or >= today
|
||||
->where(function ($w) use ($today) {
|
||||
$w->whereNull('v.valid_until')
|
||||
->orWhere('v.valid_until', '>=', $today);
|
||||
->orWhere('v.valid_until', '>=', $today);
|
||||
})
|
||||
// max_uses is null or times_used < max_uses
|
||||
->where(function ($w) {
|
||||
$w->whereNull('v.max_uses')
|
||||
->orWhereColumn('v.times_used', '<', 'v.max_uses');
|
||||
->orWhereColumn('v.times_used', '<', 'v.max_uses');
|
||||
})
|
||||
// Not previously used by this student (no usage row)
|
||||
->whereNull('u.id')
|
||||
@@ -148,4 +146,4 @@ class DiscountVoucher extends BaseModel
|
||||
|
||||
return $q->first();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user