Files
alrahma_sunday_school_api/app/Models/Reimbursement.php
T
2026-06-09 02:32:58 -04:00

170 lines
5.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Reimbursement extends BaseModel
{
protected $table = 'reimbursements';
protected $fillable = [
'amount',
'reimbursed_to',
'approved_by',
'receipt_path',
'description',
'status',
'expense_id',
'added_by',
'school_year',
'semester',
'check_number',
'reimbursement_method',
'batch_number',
];
/**
* If your table has created_at/updated_at, keep timestamps (default).
* If it does NOT, set to false.
*/
public $timestamps = true;
protected $casts = [
'amount' => 'decimal:2',
'expense_id' => 'integer',
'approved_by' => 'integer',
'added_by' => 'integer',
'batch_number' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/* ============================================================
* Status (align these strings to your DB values)
* ============================================================
*/
public const STATUS_DRAFT = 'draft';
public const STATUS_PENDING = 'pending';
public const STATUS_APPROVED = 'approved';
public const STATUS_REIMBURSED = 'reimbursed';
public const STATUS_REJECTED = 'rejected';
public static function allowedStatuses(): array
{
return [
self::STATUS_DRAFT,
self::STATUS_PENDING,
self::STATUS_APPROVED,
self::STATUS_REIMBURSED,
self::STATUS_REJECTED,
];
}
/* ============================================================
* Relationships (optional)
* ============================================================
*/
public function expense(): BelongsTo
{
return $this->belongsTo(Expense::class, 'expense_id');
}
public function approver(): BelongsTo
{
// Change Admin::class to your actual admin/user model
return $this->belongsTo(Admin::class, 'approved_by');
}
public function addedBy(): BelongsTo
{
// Change Admin::class to your actual admin/user model
return $this->belongsTo(Admin::class, 'added_by');
}
/* ============================================================
* Scopes
* ============================================================
*/
public function scopeForSchoolYear(Builder $q, string $schoolYear): Builder
{
return $q->where('school_year', $schoolYear);
}
public function scopeForSemester(Builder $q, string $semester): Builder
{
return $q->where('semester', $semester);
}
public function scopeStatus(Builder $q, string $status): Builder
{
return $q->where('status', $status);
}
public function scopeInBatch(Builder $q, int $batchNumber): Builder
{
return $q->where('batch_number', $batchNumber);
}
/* ============================================================
* Helpers
* ============================================================
*/
public function markApproved(int $adminId): self
{
$this->status = self::STATUS_APPROVED;
$this->approved_by = $adminId;
$this->save();
return $this;
}
public function markReimbursed(?string $method = null, ?string $checkNumber = null): self
{
$this->status = self::STATUS_REIMBURSED;
if ($method !== null) {
$this->reimbursement_method = $method;
}
if ($checkNumber !== null) {
$this->check_number = $checkNumber;
}
$this->save();
return $this;
}
/* ============================================================
* Optional validation helper (FormRequest/controller)
* ============================================================
*/
public static function rules(bool $updating = false): array
{
return [
'amount' => [$updating ? 'sometimes' : 'required', 'numeric', 'min:0'],
'reimbursed_to' => ['nullable', 'string', 'max:255'],
'expense_id' => ['nullable', 'integer', 'exists:expenses,id'],
'description' => ['nullable', 'string', 'max:5000'],
'status' => ['nullable', 'string', 'in:' . implode(',', self::allowedStatuses())],
'receipt_path' => ['nullable', 'string', 'max:255'],
'approved_by' => ['nullable', 'integer'],
'added_by' => ['nullable', 'integer'],
'school_year' => ['nullable', 'string', 'max:20'],
'semester' => ['nullable', 'string', 'max:20'],
'check_number' => ['nullable', 'string', 'max:80'],
'reimbursement_method' => ['nullable', 'string', 'max:50'],
'batch_number' => ['nullable', 'integer', 'min:1'],
];
}
}