reconstruction of the project
This commit is contained in:
@@ -3,11 +3,15 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DiscountUsage extends BaseModel
|
||||
{
|
||||
protected $table = 'discount_usages';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
// ✅ CI: timestamps enabled (created_at / updated_at)
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'voucher_id',
|
||||
'invoice_id',
|
||||
@@ -21,43 +25,38 @@ class DiscountUsage extends BaseModel
|
||||
'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 $casts = [
|
||||
'voucher_id' => 'integer',
|
||||
'invoice_id' => 'integer',
|
||||
'discount_amount' => 'decimal:2', // adjust precision if needed
|
||||
'updated_by' => 'integer',
|
||||
'parent_id' => 'integer',
|
||||
'used_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $validationMessages = [
|
||||
'voucher_id' => [
|
||||
'required' => 'Voucher ID is required.'
|
||||
],
|
||||
'invoice_id' => [
|
||||
'required' => 'Invoice ID is required.'
|
||||
]
|
||||
];
|
||||
|
||||
public function getTotalDiscountByParentIdAndSchoolYear(int $parentId, string $schoolYear): float
|
||||
/* Optional relationships */
|
||||
public function invoice()
|
||||
{
|
||||
$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;
|
||||
return $this->belongsTo(Invoice::class, 'invoice_id');
|
||||
}
|
||||
|
||||
}
|
||||
public function voucher()
|
||||
{
|
||||
return $this->belongsTo(Voucher::class, 'voucher_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent of CI getTotalDiscountByParentIdAndSchoolYear()
|
||||
*/
|
||||
public static function getTotalDiscountByParentIdAndSchoolYear(int $parentId, string $schoolYear): float
|
||||
{
|
||||
$total = DB::table('discount_usages as du')
|
||||
->join('invoices as i', 'du.invoice_id', '=', 'i.id')
|
||||
->where('i.parent_id', $parentId)
|
||||
->where('i.school_year', $schoolYear)
|
||||
->sum('du.discount_amount');
|
||||
|
||||
return (float) $total;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user