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]); }
/** * Finds the ActionsRecord model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ActionsRecord the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ActionsRecord::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }