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]);
 }
Пример #2
0
 public function actionCreateAccount($hardId, $groupId = '')
 {
     /** @var PersonsGroupRecord $group */
     if ($groupId !== '') {
         $group = PersonsGroupRecord::findOne($groupId);
     } else {
         $group = PersonsGroupRecord::getDefault();
     }
     /** @var PersonsRecord $user */
     $user = Yii::$app->user->identity;
     if ($group->ID < $user->GroupID) {
         throw new ForbiddenHttpException('Вы не можете зарегистрировать карту более высокого уровня');
     }
     /** @var PersonsRecord $user */
     $user = Yii::createObject(['class' => PersonsRecord::className(), 'scenario' => 'create']);
     $user->GroupID = $group->id;
     $user->ServiceCard = $group->IsService;
     //from parent controller (not my behavior)
     $this->performAjaxValidation($user);
     //data already loaded with ajax validation
     if ($user->load(Yii::$app->request->post()) && $user->validate()) {
         $card = null;
         /** @var CardForm $card */
         $cardForm = \Yii::createObject(CardForm::className());
         $cardForm->HardID = $hardId;
         if (!$cardForm->isExist()) {
             $card = $cardForm->create();
         } else {
             if ($cardForm->isUsed()) {
                 throw new HttpException(400, 'Данная карта уже зарегистрирована в системе');
             } else {
                 $card = $cardForm->find();
             }
         }
         if (!$card->ID) {
             throw new HttpException(400, 'Не удалось зарегистрировать карту');
         }
         $user->CardID = $card->id;
         if ($user->create()) {
             /** @var FinanceRecord $finance */
             $finance = Yii::createObject(FinanceRecord::className());
             $finance->ClassID = 2;
             //Регистраци карты
             $finance->ContractorID = $user->ID;
             $finance->save();
             //Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been created'));
             return $this->redirect(['/person/children', 'personId' => $user->id]);
         }
     }
     return $this->render('createAccount', ['user' => $user]);
 }
Пример #3
0
 public function actionChangeCardSwipe($id, $broken = false)
 {
     /** @var PersonsRecord $user */
     $user = $this->findModel($id);
     /** @var CardForm $model */
     $model = \Yii::createObject(CardForm::className());
     if ($model->load(Yii::$app->request->post(), '') && $model->validate()) {
         if (!$model->isExist()) {
             $card = $model->create();
         } else {
             $card = $model->find();
         }
         if ($user->CardID !== $card->ID) {
             $personGroup = $user->group;
             if ($broken && null !== $personGroup && $personGroup->CardRepairCost > 0) {
                 if ($user->account->Money < $personGroup->CardRepairCost) {
                     throw new HttpException(400, 'У вас не достаточно средств для замены карты. Стоимость замены карты: ' . Yii::$app->formatter->asCurrency($personGroup->CardRepairCost));
                 } else {
                     if (!$user->updateAttributes(['CardID' => $card->ID])) {
                         throw new HttpException(400, 'Не удалось обновить номер карты');
                     }
                     $user->account->Money = $user->account->Money - $personGroup->CardRepairCost;
                     if ($user->account->save(false, ['Money'])) {
                         /** @var FinanceRecord $finance */
                         $finance = Yii::createObject(['class' => FinanceRecord::className()]);
                         $finance->ClassID = 19;
                         //Плата за восстановление карты
                         $finance->CashSum = $personGroup->CardRepairCost;
                         $finance->ContractorID = $id;
                         $finance->Comment = "Замена карты для пользователя " . Html::a($id, ['/user/admin/update', 'id' => $id]);
                         Yii::getLogger()->log($finance->Comment, Logger::LEVEL_WARNING);
                         $finance->save();
                     }
                     $flashMsg = 'Замена карты выполнена, со счета сянто: ' . Yii::$app->formatter->asCurrency($personGroup->CardRepairCost);
                     Yii::getLogger()->log($flashMsg, Logger::LEVEL_WARNING);
                     Yii::$app->getSession()->setFlash('success', $flashMsg);
                 }
             } else {
                 if (!$user->updateAttributes(['CardID' => $card->ID])) {
                     throw new HttpException(400, 'Не удалось обновить номер карты');
                 }
                 Yii::getLogger()->log("Замена карты для пользователя " . Html::a($id, ['/user/admin/update', 'id' => $id]), Logger::LEVEL_WARNING);
                 Yii::getLogger()->log('Замена карты выполнена', Logger::LEVEL_WARNING);
                 Yii::getLogger()->log('Замена карты выполнена', Logger::LEVEL_WARNING);
             }
         } else {
             throw new HttpException(400, 'Проведите другую карту');
         }
     } else {
         throw new HttpException(400, $model->getFirstError('HardID'));
     }
     return $this->redirect(['/user/admin/update', 'id' => $id]);
 }
 /**
  * Step 3
  * @param $hardId
  * @return string|\yii\web\Response
  * @throws HttpException
  */
 public function actionCreateAccount($hardId, $groupId = '')
 {
     /** @var PersonsGroupRecord $group */
     if ($groupId !== '') {
         $group = PersonsGroupRecord::findOne($groupId);
     } else {
         $group = PersonsGroupRecord::getDefault();
     }
     /** @var PersonsRecord $user */
     $user = Yii::createObject(['class' => PersonsRecord::className(), 'scenario' => 'create']);
     /** @var CardForm $card */
     $card = \Yii::createObject(CardForm::className());
     $card->HardID = $hardId;
     if (!$card->isExist()) {
         $card = $card->create();
     } else {
         $card = $card->find();
     }
     if (!$card->ID) {
         throw new HttpException(400, 'Не удалось сохранить номер карты');
     }
     $user->CardID = $card->id;
     $user->GroupID = $group->id;
     $user->ServiceCard = $group->IsService;
     $this->performAjaxValidation($user);
     if ($user->load(Yii::$app->request->post()) && $user->create()) {
         /** @var FinanceRecord $finance */
         $finance = Yii::createObject(FinanceRecord::className());
         $finance->ClassID = 2;
         //Регистраци карты
         $finance->ContractorID = $user->ID;
         $finance->save();
         Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been created'));
         return $this->redirect(['/accounts/charge-balance', 'id' => $user->account->id]);
     }
     return $this->render('createAccount', ['user' => $user]);
 }
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['AccountantID', 'ContractorID'], 'integer'], [['Items'], 'safe'], [['CashSum'], 'number', 'min' => 0], [['CashSum'], 'default', 'value' => 0], ['FinanceID', 'exist', 'targetClass' => FinanceRecord::className(), 'targetAttribute' => 'ID']];
 }