/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = SoldDiscountRecord::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['ID' => $this->ID, 'ActionID' => $this->ActionID, 'PersonID' => $this->PersonID, 'DateFrom' => $this->DateFrom, 'DateTo' => $this->DateTo, 'FinanceRowID' => $this->FinanceRowID]);
     return $dataProvider;
 }
 /**
  * Finds the SoldDiscountRecord model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return SoldDiscountRecord the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = SoldDiscountRecord::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 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]);
 }