255], [['cart'], 'string' ,'max' => 20], [['id_product' ], 'validateProduct'], [['count' ], 'validateCount'], [['id_currency' ], 'validateCurrency'], [['id_account' ], 'validateAccount'], [['id_discount' ], 'validateDiscount'], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id_product' => Yii::t("frontend/product", "Product"), 'count' => Yii::t("frontend/product", "Count"), 'id_currency' => Yii::t("frontend/product", "Currency"), 'id_account' => Yii::t("frontend/product", "Account"), 'id_discount' => Yii::t("frontend/product", "Discount"), 'comment' => Yii::t("frontend/product", "Comment"), ]; } 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') ); } } } public function save(){ if ( $this->validate() ){ $connection = \Yii::$app->db; $transaction = $connection->beginTransaction(); try { $this->saveSale(); $this->saveTransfer(); $this->saveProduct(); $this->appendToUserCart(); $this->appendToCustomerCart(); $transaction->commit(); return true; } catch(Exception $e) { $transaction->rollback(); return false; } return true; } return false; } protected function saveSale(){ $this->sale = Sale::createSale($this->account, $this->discount, $this->currency, $this->count, $this->product ); if ( isset($this->comment)){ $this->sale->comment = $this->comment; } $this->sale->id_user = Yii::$app->user->id; $this->sale->save(); } protected function saveTransfer(){ $customer = null; $status = Transfer::STATUS_PAID; if ( $this->isAppendToUserCart() ){ $status = Transfer::STATUS_NOT_PAID; }else if ( $this->isAppendToCustomerCart() ){ $status = Transfer::STATUS_NOT_PAID; $customer = $this->customer; } $this->transfer = Transfer::createProductTransfer($this->sale,$this->account, $this->discount, $this->currency, $this->count, $this->product,$status,$customer); 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(); } public function isAppendToUserCart(){ $result = false; if ( isset( $this->cart ) && $this->cart == 'user' ){ $result = true; } return $result; } public function appendToUserCart(){ if ( $this->isAppendToUserCart() ){ $item = new UserSoldItem(); $item->id_transfer = $this->transfer->id_transfer; $item->id_user = Yii::$app->user->id; $item->save(false); } } public function isAppendToCustomerCart(){ $result = false; if ( isset( $this->cart ) && $this->cart == 'customer' ){ $result = true; } return $result; } public function appendToCustomerCart(){ if ( $this->isAppendToCustomerCart() && isset($this->customer) ){ $item = new ShoppingCart(); $item->id_customer = $this->customer->id_customer; $item->id_transfer = $this->transfer->id_transfer; $item->save(false); } } }