/**
  * Creates a new SoldDiscountRecord model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new SoldDiscountRecord();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->ID]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function actionSellAction($id, $actionId)
 {
     $model = $this->findModel($id);
     /** @var ActionsRecord $action */
     $action = ActionsRecord::findOne($actionId);
     $formatter = Yii::$app->formatter;
     /** @var SellActionForm $formModel */
     $formModel = \Yii::createObject(SellActionForm::className());
     if ($formModel->load(Yii::$app->request->post()) && $formModel->validate()) {
         $sold = new SoldDiscountRecord();
         $sold->ActionID = $action->ID;
         $sold->PersonID = $model->person->id;
         $sold->DateFrom = time();
         $sold->DateTo = time() + 3600 * $formModel->amount;
         $cost = floatval($action->getDiscount()->price);
         $sum = $cost * $formModel->amount;
         $payment = new Payment($model);
         if ($payment->pay($sum) && $model->save(true, ['Money', 'Spend', 'Bonuses', 'SpendBonuses'])) {
             /** @var FinanceRecord $finance */
             $finance = Yii::createObject(FinanceRecord::className());
             $finance->ClassID = 22;
             //Оплата услуги
             $finance->Comment = $action->getDiscount()->description . ' x ' . $formModel->amount . ' x ' . $cost . '(c' . $formatter->asDatetime($sold->DateFrom) . ' по ' . $formatter->asDatetime($sold->DateTo) . ')';
             $finance->CashSum = $payment->getSpendAmount();
             $finance->ContractorID = $model->person->ID;
             $finance->save();
             $sold->FinanceRowID = $finance->id;
             $sold->save();
             Yii::$app->getSession()->setFlash('success', 'Акция продана, ' . Yii::$app->formatter->asCurrency($payment->getSpendAmount()) . ' списано со счета');
             return $this->redirect(['/user/admin/update', 'id' => $model->person->ID]);
         } else {
             Yii::$app->getSession()->setFlash('error', 'Недостаточно денег');
         }
     }
     return $this->render('sellAction', ['formModel' => $formModel, 'model' => $model, 'action' => $action]);
 }