/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = ServiceRecord::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, 'Deleted' => $this->Deleted, 'Enabled' => $this->Enabled]);
     $query->andFilterWhere(['like', 'Name', $this->Name])->andFilterWhere(['like', 'Description', $this->Description])->andFilterWhere(['like', 'ClassName', $this->ClassName])->andFilterWhere(['like', 'Settings', $this->Settings]);
     return $dataProvider;
 }
 /**
  * Finds the ServiceRecord model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ServiceRecord the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ServiceRecord::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function actionSellService($id, $serviceId)
 {
     $model = $this->findModel($id);
     /** @var ServiceRecord $service */
     $service = ServiceRecord::findOne($serviceId);
     /** @var SellServiceForm $formModel */
     $formModel = \Yii::createObject(SellServiceForm::className());
     if ($formModel->load(Yii::$app->request->post()) && $formModel->validate()) {
         $formatter = Yii::$app->formatter;
         $cost = floatval($service->getData()->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 = $service->Name . ': ' . $formModel->amount . 'ч x ' . $formatter->asCurrency($cost);
             $finance->CashSum = $payment->getSpendAmount();
             $finance->ContractorID = $model->person->ID;
             $finance->save();
             Yii::$app->getSession()->setFlash('success', 'Услуга продана, ' . ($payment->getSpendBonusesAmount() > 0 ? $formatter->asCurrency($payment->getSpendMoneyAmount()) . ' списано с основного счета, ' . $formatter->asCurrency($payment->getSpendBonusesAmount()) . ' с бонусного' : $formatter->asCurrency($payment->getSpendAmount()) . ' списано со счета'));
             return $this->redirect(['/user/admin/update', 'id' => $model->person->ID]);
         } else {
             Yii::$app->getSession()->setFlash('error', 'Недостаточно денег');
         }
     }
     return $this->render('sellService', ['formModel' => $formModel, 'model' => $model, 'service' => $service]);
 }