示例#1
0
 /**
  * Save discount action
  * @return array|string
  * @throws HttpException
  * @throws \yii\db\Exception
  */
 public function actionSaveDiscount()
 {
     $request = Yii::$app->request;
     if (!$request->isPost) {
         throw new HttpException(405, 'method not allowed');
     }
     $errors = false;
     $transaction = Discount::getDb()->beginTransaction();
     foreach ($request->post('discount', []) as $id => $data) {
         $data['start_date'] = Date::dateToMysql($data['start_date']);
         $data['end_date'] = Date::dateToMysql($data['end_date']);
         $data['start_date_vip'] = Date::dateToMysql($data['start_date_vip']);
         $data['end_date_vip'] = Date::dateToMysql($data['end_date_vip']);
         $discount = Discount::findOne($id);
         $discount->scenario = 'update';
         $discount->attributes = $data;
         if (!$discount->save()) {
             $errors = true;
             var_dump($discount->errors);
         }
     }
     if ($errors) {
         $transaction->rollBack();
     } else {
         $transaction->commit();
     }
     TagDependency::invalidate(Yii::$app->commonCache, Product::generateTagStatic($request->get('id'), 'productVariant'));
     TagDependency::invalidate(Yii::$app->commonCache, Product::generateTagStatic($request->get('id'), 'productCrossSelling'));
     $component = new View(['models' => $this->loadEditModels(['id' => $request->get('id')]), 'language' => Yii::$app->language, 'addAgain' => $request->get('add-again', true), 'saved' => false, 'uploadConfig' => $this->uploadConfig, 'dropDownList' => function ($id) {
         return $this->getDropDownList($id);
     }]);
     if ($request->isAjax) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ['html' => $this->renderAjax('view/_contentBlock.php', ['component' => $component, 'create' => false]), 'scripts' => $this->registerClientSideAjaxScript(), 'title' => !empty($component->models['i18n']->page_title) ? $component->models['i18n']->page_title : $component->models['i18n']->name];
     } else {
         return $this->render('view/view', ['component' => $component, 'create' => false]);
     }
 }
示例#2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDiscount()
 {
     return $this->hasOne(Discount::className(), ['id' => 'discount_id']);
 }