/** * Creates a new PurchaseOrder model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $username = Yii::$app->user->identity ? Yii::$app->user->identity->username : null; $model = new PurchaseOrder(); if (Yii::$app->request->isPost) { if (is_object($cart = Yii::$app->session->get('cart'))) { if ($cart->totalQuantity() > 0) { if ($model = PurchaseOrder::create(Yii::$app->request->post())) { // if ($model->customer_id !== null) { $customer = Customer::findOne(['id' => $model->customer_id, 'status' => Customer::STATUS_ACTIVE]); // } foreach ($cart->data as $item) { $p = $item['product']; $qty = $item['quantity']; if (PurchaseOrderDetail::create(['PurchaseOrderDetail' => ['purchase_order_id' => $model->id, 'product_id' => $p->id, 'product_name' => $p->t('name'), 'product_code' => $p->code, 'quantity' => $qty, 'unit_price' => $p->price, 'discount_percent' => round(100 * ($p->original_price - $p->price) / $p->original_price, 4), 'is_offer' => $p->original_price > $p->price ? 1 : 0]]) !== false) { if ($customer) { $customer->total_purchase_products += $qty; $customer->total_purchase_value += $qty * $p->price; } $p->available_quantity -= $qty; $p->order_quantity += $qty; // $p->total_revenue += $qty * $p->price; $p->save(); } } if ($customer) { $customer->total_purchase_orders++; $customer->save(false); } try { Yii::$app->orderMailer->compose(['html' => 'purchaseOrderReview-html', 'text' => 'purchaseOrderReview-text'], ['purchase_order' => $model])->setFrom([Yii::$app->params['order_email'] => Yii::$app->name])->setTo($model->customer_email)->setSubject(I18n::t('Purchase order review'))->send(); } catch (Exception $e) { } Yii::$app->session->set('cart', new Cart()); $model = new PurchaseOrder(); Yii::$app->session->setFlash('success', I18n::t('Your purchase order submitted successfully') . '. ' . I18n::t('Thank you')); // $this->goHome(); } else { Yii::$app->session->setFlash('error', I18n::t('Some errors occurred') . '. ' . I18n::t('Please try again') . '.'); } } else { Yii::$app->session->setFlash('error', I18n::t('Your cart is empty') . '. ' . I18n::t('Please try again') . '.'); } } else { Yii::$app->session->setFlash('error', I18n::t('Some errors occurred') . '. ' . I18n::t('Please try again') . '.'); } } return $this->render('create', ['username' => $username, 'model' => $model]); }
/** * Sends an email with a link, for resetting the password. * * @return boolean whether the email was send */ public function sendEmail() { /* @var $user User */ $user = Customer::findOne(['status' => Customer::STATUS_ACTIVE, 'email' => $this->email]); if ($user) { if (!Customer::isPasswordResetTokenValid($user->password_reset_token)) { $user->generatePasswordResetToken(); } if ($user->save(false)) { return Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])->setFrom([Yii::$app->params['noreply_email'] => Yii::$app->name])->setTo($this->email)->setSubject(I18n::t('Password reset for your account'))->send(); } } // var_dump($user->getErrors()); // return false; }
/** * Finds the Customer model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Customer the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Customer::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }