reconstruction of the project
This commit is contained in:
@@ -3,13 +3,15 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PaymentTransaction extends BaseModel
|
||||
{
|
||||
protected $table = 'payment_transactions'; // Table name
|
||||
protected $primaryKey = 'id'; // Primary key
|
||||
protected $table = 'payment_transactions';
|
||||
|
||||
// ✅ CI: useTimestamps = true (created_at/updated_at)
|
||||
public $timestamps = false;
|
||||
|
||||
// Allowed fields for mass assignment
|
||||
protected $fillable = [
|
||||
'transaction_id',
|
||||
'payment_id',
|
||||
@@ -22,123 +24,71 @@ class PaymentTransaction extends BaseModel
|
||||
'semester',
|
||||
];
|
||||
|
||||
// Set automatic timestamps
|
||||
public $timestamps = false;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
|
||||
// Validation rules
|
||||
protected $validationRules = [
|
||||
'transaction_id' => 'required|is_unique[payment_transactions.transaction_id]',
|
||||
'payment_id' => 'required|integer', // Reference to payment table
|
||||
'amount' => 'required|decimal',
|
||||
'payment_method' => 'required|string|max_length[50]',
|
||||
'payment_status' => 'required|string|max_length[50]',
|
||||
'payment_reference' => 'permit_empty|string|max_length[255]',
|
||||
'is_full_payment' => 'required|in_list[0,1]', // 0 for installment, 1 for full payment
|
||||
protected $casts = [
|
||||
'payment_id' => 'integer',
|
||||
'amount' => 'decimal:2', // adjust precision if needed
|
||||
'transaction_fee' => 'decimal:2', // adjust precision if needed
|
||||
'transaction_date' => 'datetime',
|
||||
'is_full_payment' => 'boolean',
|
||||
];
|
||||
|
||||
// Custom error messages
|
||||
protected $validationMessages = [
|
||||
'transaction_id' => [
|
||||
'required' => 'The transaction ID is required.',
|
||||
'is_unique' => 'The transaction ID must be unique.',
|
||||
],
|
||||
'payment_id' => [
|
||||
'required' => 'The payment ID is required.',
|
||||
'integer' => 'The payment ID must be an integer.',
|
||||
],
|
||||
'amount' => [
|
||||
'required' => 'The payment amount is required.',
|
||||
'decimal' => 'The payment amount must be a valid decimal number.',
|
||||
],
|
||||
'payment_method' => [
|
||||
'required' => 'The payment method is required.',
|
||||
'max_length' => 'The payment method must not exceed 50 characters.',
|
||||
],
|
||||
'payment_status' => [
|
||||
'required' => 'The payment status is required.',
|
||||
'max_length' => 'The payment status must not exceed 50 characters.',
|
||||
],
|
||||
'payment_reference' => [
|
||||
'max_length' => 'The payment reference must not exceed 255 characters.',
|
||||
],
|
||||
'is_full_payment' => [
|
||||
'required' => 'The full payment flag is required.',
|
||||
'in_list' => 'The full payment flag must be either 0 (installment) or 1 (full payment).',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Get all transactions for a specific payment.
|
||||
*
|
||||
* @param int $paymentId
|
||||
* @return array
|
||||
*/
|
||||
public function getTransactionsByPaymentId($paymentId)
|
||||
/* Optional relationship */
|
||||
public function payment()
|
||||
{
|
||||
return $this->where('payment_id', $paymentId)
|
||||
->orderBy('transaction_date', 'DESC')
|
||||
->findAll();
|
||||
return $this->belongsTo(Payment::class, 'payment_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a single transaction by its transaction ID.
|
||||
*
|
||||
* @param string $transactionId
|
||||
* @return array|null
|
||||
*/
|
||||
public function getTransactionById($transactionId)
|
||||
/* =========================
|
||||
* CI method equivalents
|
||||
* ========================= */
|
||||
|
||||
public static function getTransactionsByPaymentId(int $paymentId): array
|
||||
{
|
||||
return $this->where('transaction_id', $transactionId)->first();
|
||||
return static::query()
|
||||
->where('payment_id', $paymentId)
|
||||
->orderByDesc('transaction_date')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update payment status by transaction ID.
|
||||
*
|
||||
* @param string $transactionId
|
||||
* @param string $status
|
||||
* @return bool
|
||||
*/
|
||||
public function updateTransactionStatus($transactionId, $status)
|
||||
public static function getTransactionById(string $transactionId): ?self
|
||||
{
|
||||
return $this->update($transactionId, ['payment_status' => $status]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total amount paid for a specific payment.
|
||||
*
|
||||
* @param int $paymentId
|
||||
* @return float
|
||||
*/
|
||||
public function getTotalPaidByPaymentId($paymentId)
|
||||
{
|
||||
$this->selectSum('amount');
|
||||
return $this->where('payment_id', $paymentId)->get()->getRow()->amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update transaction fee for a specific transaction.
|
||||
*
|
||||
* @param string $transactionId
|
||||
* @param float $transactionFee
|
||||
* @return bool
|
||||
*/
|
||||
public function updateTransactionFee($transactionId, $transactionFee)
|
||||
{
|
||||
return $this->update($transactionId, ['transaction_fee' => $transactionFee]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest payment transaction for a payment.
|
||||
*
|
||||
* @param int $paymentId
|
||||
* @return array|null
|
||||
*/
|
||||
public function getLatestTransactionByPaymentId($paymentId)
|
||||
{
|
||||
return $this->where('payment_id', $paymentId)
|
||||
->orderBy('transaction_date', 'DESC')
|
||||
return static::query()
|
||||
->where('transaction_id', $transactionId)
|
||||
->first();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update payment_status by transaction_id (not PK).
|
||||
*/
|
||||
public static function updateTransactionStatus(string $transactionId, string $status): bool
|
||||
{
|
||||
return static::query()
|
||||
->where('transaction_id', $transactionId)
|
||||
->update(['payment_status' => $status]) >= 0;
|
||||
}
|
||||
|
||||
public static function getTotalPaidByPaymentId(int $paymentId): float
|
||||
{
|
||||
$sum = static::query()
|
||||
->where('payment_id', $paymentId)
|
||||
->sum('amount');
|
||||
|
||||
return (float) $sum;
|
||||
}
|
||||
|
||||
public static function updateTransactionFee(string $transactionId, float $transactionFee): bool
|
||||
{
|
||||
return static::query()
|
||||
->where('transaction_id', $transactionId)
|
||||
->update(['transaction_fee' => $transactionFee]) >= 0;
|
||||
}
|
||||
|
||||
public static function getLatestTransactionByPaymentId(int $paymentId): ?self
|
||||
{
|
||||
return static::query()
|
||||
->where('payment_id', $paymentId)
|
||||
->orderByDesc('transaction_date')
|
||||
->first();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user