<?php

namespace App\Observers;

use App\Models\DelayedGood;

class DelayedGoodObserver
{
    /**
     * Handle the DelayedGood "saving" event.
     */
    public function saving(DelayedGood $delayedGood): void
    {
        $delayedGood->is_paid = $delayedGood->paid_amount >= $delayedGood->total_amount;

        if ($delayedGood->is_paid && ! $delayedGood->payment_date) {
            $delayedGood->payment_date = now();
        }

        if (! $delayedGood->is_paid) {
            $delayedGood->payment_date = null;
        }
    }
}
