<?php

namespace PortalPeople\virtualstock\models;

use Yii;
use yii\helpers\Json;

class Cart extends \backend\models\Cartsbe
{
    public function rules(): array
    {
        $rules = parent::rules();
        $rules[] = [['paymentmethod'], 'default', 'value' => 'VirtualStock'];
        $rules[] = [['data'], 'safe'];
        return $rules;
    }

    public function afterSave($insert, $changedAttributes): void
    {
        if ($insert) {
            $paymentClass = '\\backend\\models\\Paymentresponsesbe';
            if (class_exists($paymentClass)) {
                $payment = new $paymentClass();
                $payment->basketid = $this->id;
                $payment->source   = 'VirtualStock';
                $payment->camount  = 0;
                $payment->amount   = 0;
                $payment->status   = 'Paid';
                $payment->save();
            }
        }

        parent::afterSave($insert, $changedAttributes);
    }
}
