64 lines
1.7 KiB
PHP
Executable File
64 lines
1.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\BaseModel;
|
|
|
|
class DiscountUsage extends BaseModel
|
|
{
|
|
protected $table = 'discount_usages';
|
|
protected $primaryKey = 'id';
|
|
protected $fillable = [
|
|
'voucher_id',
|
|
'invoice_id',
|
|
'parent_id',
|
|
'discount_amount',
|
|
'description',
|
|
'school_year',
|
|
'updated_by',
|
|
'used_at',
|
|
'created_at',
|
|
'updated_at',
|
|
'semester',
|
|
];
|
|
|
|
// Enable automatic timestamps if desired (optional: only if you add created_at/updated_at columns)
|
|
public $timestamps = true;
|
|
const CREATED_AT = 'created_at';
|
|
const UPDATED_AT = 'updated_at';
|
|
|
|
// Add validation rules
|
|
protected $validationRules = [
|
|
'voucher_id' => 'required|is_natural_no_zero',
|
|
'invoice_id' => 'required|is_natural_no_zero',
|
|
'user_id' => 'permit_empty|is_natural_no_zero',
|
|
'used_at' => 'valid_date'
|
|
];
|
|
|
|
protected $validationMessages = [
|
|
'voucher_id' => [
|
|
'required' => 'Voucher ID is required.'
|
|
],
|
|
'invoice_id' => [
|
|
'required' => 'Invoice ID is required.'
|
|
]
|
|
];
|
|
|
|
public function getTotalDiscountByParentIdAndSchoolYear(int $parentId, string $schoolYear): float
|
|
{
|
|
$db = \Config\Database::connect();
|
|
|
|
$result = $db->table('discount_usages du')
|
|
->selectSum('du.discount_amount', 'total_discount')
|
|
->join('invoices i', 'du.invoice_id = i.id')
|
|
->where('i.parent_id', $parentId)
|
|
->where('i.school_year', $schoolYear)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
return $result && isset($result['total_discount']) ? (float) $result['total_discount'] : 0.00;
|
|
}
|
|
|
|
}
|
|
|