frontend/models/VoucherForm.php
/**
     * Process voucher.
     *
     * @return bool true on success, false on failure
     */
Process voucher.
@return bool true on success, false on failure
Process voucher . @return bool true on success false on failure
public function process ()
    {
        if ($this->validate()) {
            $user = User::findOne($this->user_id);
            $voucher = Voucher::findByCode($this->voucher);
            $transaction = Yii::$app->db->beginTransaction();
            $order = new Order();
            $order->setScenario(Order::SCENARIO_CREATE);
            $order->user_id = $user->id;
            $order->cost = $voucher->product->price;
            $order->currency = $voucher->product->price_currency;
            $order->description = $voucher->product->description;
            $order->product_id = $voucher->product->id;
            $order->voucher_id = $voucher->id;
            $order->status = Order::STATUS_COMPLETED;
            if ($order->save()) {
                $planUpdated = ($user->plan == null) ? UserPlan::create($user, $voucher->product) : $user->plan->applyProduct($voucher->product);
                if ($planUpdated) {
                    $voucher->setScenario(Voucher::SCENARIO_UPDATE);
                    $voucher->user_id = $this->user_id;
                    $voucher->used = time();
                    $voucher->status = Voucher::STATUS_USED;
                    if ($voucher->save()) {
                        $transaction->commit();
                        return true;
                    }
                }
            }
            $transaction->rollBack();
        }
        return false;
    }
public function process ( ) { if ( $ this -> validate ( ) ) { $ user = User :: findOne ( $ this -> user_id ) ; $ voucher = Voucher :: findByCode ( $ this -> voucher ) ; $ transaction = Yii :: $ app -> db -> beginTransaction ( ) ; $ order = new Order ( ) ; $ order -> setScenario ( Order :: SCENARIO_CREATE ) ; $ order -> user_id = $ user -> id ; $ order -> cost = $ voucher -> product -> price ; $ order -> currency = $ voucher -> product -> price_currency ; $ order -> description = $ voucher -> product -> description ; $ order -> product_id = $ voucher -> product -> id ; $ order -> voucher_id = $ voucher -> id ; $ order -> status = Order :: STATUS_COMPLETED ; if ( $ order -> save ( ) ) { $ planUpdated = ( $ user -> plan == null ) ? UserPlan :: create ( $ user , $ voucher -> product ) : $ user -> plan -> applyProduct ( $ voucher -> product ) ; if ( $ planUpdated ) { $ voucher -> setScenario ( Voucher :: SCENARIO_UPDATE ) ; $ voucher -> user_id = $ this -> user_id ; $ voucher -> used = time ( ) ; $ voucher -> status = Voucher :: STATUS_USED ; if ( $ voucher -> save ( ) ) { $ transaction -> commit ( ) ; return true ; } } } $ transaction -> rollBack ( ) ; } return false ; }
Process voucher.
Process voucher .
//Update voucher status