255], [['id_product' ], 'validateProduct'], [['count' ], 'validateCount'], [['id_currency' ], 'validateCurrency'], [['id_account' ], 'validateAccount'], [['id_discount' ], 'validateDiscount'], ]; } public function validateProduct($attribute,$params){ $this->product = Product::findOne($this->id_product); if ( !isset( $this->product ) ){ $this->addError($attribute, Yii::t('frontend/product', 'Product not found!')); } } public function validateCount($attribute,$params){ if ( $this->product != null ){ if ( $this->product->stock < $this->count ){ $this->addError($attribute, Yii::t('frontend/product', 'Stock {stock} lower then {count}!', [ 'count' => $this->count, 'stock' => $this->product->stock ] )); } } } public function validateCurrency($attribute,$params){ if ( isset($this->id_currency ) ){ $this->currency = Currency::findOne($this->id_currency); if ( !isset( $this->currency ) ){ $this->addError($attribute,Yii::t('frontend/product', 'Currency not found') ); } } } public function validateAccount($attribute,$params){ $this->account = Account::findOne($this->id_account); } public function validateDiscount($attribute,$params){ if ( isset( $this->id_discount ) ){ $this->discount = Discount::findOne($this->id_discount); if ( !isset( $this->discount ) ){ $this->addError($attribute,Yii::t('frontend/product', 'Discount not found') ); } } } /** * @inheritdoc */ public function attributeLabels() { return [ 'verifyCode' => 'Verification Code', ]; } public function save(){ if ( $this->validate() ){ $this->saveTransfer(); $this->saveProduct(); return true; } return false; } protected function saveTransfer(){ $this->transfer = Transfer::createProductTransfer($this->account, $this->discount, $this->currency, $this->count, $this->product); /* */ $this->transfer->status = Transfer::STATUS_PAID; if ( isset($this->comment)){ $this->transfer->comment = $this->comment; } $this->transfer->id_user = Yii::$app->user->id; $this->transfer->save(); } protected function saveProduct(){ Product::sellProduct($this->product, $this->count); $this->product->save(); } }