如何根据 Laravel 9 + API 中的付款和 due_on 交易更新交易状态

how to update the transaction status based on payments and due_on transaction in Laravel 9 + API

我有一个交易和支付系统。我想根据付款和交易历史更新交易状态,例如

如果已支付全额且在创建交易到期日之前,状态变为已支付

逾期未付款状态变为逾期

如果金额未完成但未超过付款到期日,则状态变为未完成

 public function up()
    {
        Schema::create('transactions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('payer_id');
            $table->decimal('amount', 8, 2);
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('subcategory_id');
            $table->string('status');
            $table->date('due_on');
            $table->timestamps();
        });
    }
 public function up()
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('transaction_id');
            $table->unsignedBigInteger('buyer_id');
            $table->decimal('amount', 8, 2);
            $table->string('payment_method');
            $table->date('paid_at');
            $table->string('details');
            $table->timestamps();
        });
    }
class Transaction extends Model
{
    use HasFactory;

    protected $fillable = [
        'payer_id',
        'category_id',
        'subcategory_id',
        'amount',
        'status',
        'due_on'
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'payer_id', 'id');
    }

    public function payments(): HasMany
    {
        return $this->hasMany(Payment::class);
    }
class Payment extends Model
{
    use HasFactory;
    protected $fillable =[
        'transaction_id'   ,
        'buyer_id',
        'amount'        ,
        'payment_method',
        'paid_at'       ,
        'details'       ,
    ];

    public function transaction(): BelongsTo
    {
        return $this->belongsTo(Transaction::class,'transaction_id');
    }

public function user(): BelongsTo
{
    return $this->belongsTo(User::class,'buyer_id', 'id');
}
}

我必须做什么并解决这个问题 请注意,我创建了这个 SQL 查询,但我不知道如何在 Laravel

中使用它
SELECT 
 case 
     when  (sum(p.amount) < t.amount)  AND  t.due_on <  NOW() then 'Overdue'
     when  (sum(p.amount) <= t.amount) AND t.due_on > NOW() then 'Outstanding'  
          ELSE 'paid'
    END AS status  
FROM 
  transactions t
    JOIN 
   payments p ON t.id = p.transaction_id
WHERE t.id = 2 AND p.buyer_id = 1 ;

这个解决方案对我有用

public function show(Transaction $transaction)
    {
        $status = DB::table('transactions as t')
            ->select(DB::raw("case
         when  (sum(p.amount) < t.amount) AND t.due_on < now() then 'Overdue'
         when  (sum(p.amount) < t.amount) AND t.due_on > now() then 'Outstanding'
             ELSE 'paid'
                END AS status"))
            ->join('payments as p', 't.id', '=', 'p.transaction_id')
            ->where('t.id', '=', $transaction->id)
            ->where('p.buyer_id', '=', Auth::id())
            ->groupBy('t.id')
            ->get();
        return $status;
    }