if (!empty($result['analysts_business_operations'])) {
$existingOperations = AnalystsBusinessOperations::find()
- ->select(['id'])
- ->column();
+ ->indexBy('id')
+ ->asArray()
+ ->all();
$existingTypes = AnalystsBusinessOperationsTypes::find()
->indexBy('code')
->asArray()
->all();
foreach ($result["analysts_business_operations"] as $operation) {
- if (!in_array($operation['guid'], $existingOperations)) {
+ if (!in_array($operation['guid'], array_keys($existingOperations))) {
$newOperation = new AnalystsBusinessOperations();
$newOperation->id = $operation['guid'];
$newOperation->name = $operation['name'];
$newOperation->type = (int)$operation['type'];
- if (!in_array($newOperation->type, array_column($existingTypes, 'code'))) {
+ if (!in_array((int)$operation['type'], array_column($existingTypes, 'code'))) {
$newType = new AnalystsBusinessOperationsTypes();
$newType->code = (int)$operation['type'];
$newType->name = null;
} else {
$newOperation->type_id = null;
Yii::error('Ошибка сохранение типа ' . json_encode($newType->getErrors(), JSON_UNESCAPED_UNICODE), __METHOD__ );
+ LogService::apiErrorLog(
+ json_encode(
+ ["error_id" => 44.1, "error" => $operation->getErrors()],
+ JSON_UNESCAPED_UNICODE
+ )
+ );
}
} else {
$newOperation->type_id = $existingTypes[$operation['type']]['id'];
if (!$newOperation->save()) {
LogService::apiErrorLog(
json_encode(
- ["error_id" => 44, "error" => $newOperation->getErrors()],
+ ["error_id" => 44.2, "error" => $newOperation->getErrors()],
JSON_UNESCAPED_UNICODE
)
);
}
+ } else {
+ if ($existingOperations[$operation['guid']]['type'] !== (int)$operation['type']) {
+ $operation = AnalystsBusinessOperations::findOne($operation['guid']);
+ if (!in_array((int)$operation['type'], array_column($existingTypes, 'code'))) {
+ $newType = new AnalystsBusinessOperationsTypes();
+ $newType->code = (int)$operation['type'];
+ $newType->name = null;
+ $newType->created_at = date('Y-m-d H:i:s');
+ if ($newType->save()) {
+ $operation->type = (int)$operation['type'];
+ $operation->type_id = $newType->id;
+ } else {
+ Yii::error('Ошибка сохранение типа ' . json_encode($newType->getErrors(), JSON_UNESCAPED_UNICODE), __METHOD__ );
+ LogService::apiErrorLog(
+ json_encode(
+ ["error_id" => 44.3, "error" => $operation->getErrors()],
+ JSON_UNESCAPED_UNICODE
+ )
+ );
+ }
+ } else {
+ $operation->type = (int)$operation['type'];
+ $operation->type_id = AnalystsBusinessOperationsTypes::find()->where(['code' => (int)$operation['type']])->one()->id;
+ if (!$operation->save()) {
+ LogService::apiErrorLog(
+ json_encode(
+ ["error_id" => 44.4, "error" => $operation->getErrors()],
+ JSON_UNESCAPED_UNICODE
+ )
+ );
+ }
+ }
+ }
}
}
namespace app\controllers;
+use yii\web\ForbiddenHttpException;
+use yii_app\records\Admin;
+use yii_app\records\AdminGroup;
use yii_app\records\AnalystsBusinessOperations;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
*/
public function actionUpdate($id)
{
+ if (!$this->canEdit()) {
+ throw new ForbiddenHttpException('Недостаточно прав.');
+ }
$model = $this->findModel($id);
if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
*/
public function actionDelete($id)
{
+ if (!$this->canEdit()) {
+ throw new ForbiddenHttpException('Недостаточно прав.');
+ }
$this->findModel($id)->delete();
return $this->redirect(['index']);
throw new NotFoundHttpException('The requested page does not exist.');
}
+
+ private function canEdit(): bool
+ {
+ $admin = Admin::findOne(['id' => \Yii::$app->user->id]);
+ return $admin && (int)$admin->group_id === AdminGroup::GROUP_IT;
+ }
}
namespace app\controllers;
+use yii\web\ForbiddenHttpException;
+use yii_app\records\Admin;
+use yii_app\records\AdminGroup;
use yii_app\records\AnalystsBusinessOperationsTypes;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
*/
public function actionUpdate($id)
{
+ if (!$this->canEdit()) {
+ throw new ForbiddenHttpException('Недостаточно прав.');
+ }
$model = $this->findModel($id);
if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
*/
public function actionDelete($id)
{
+ if (!$this->canEdit()) {
+ throw new ForbiddenHttpException('Недостаточно прав.');
+ }
$this->findModel($id)->delete();
return $this->redirect(['index']);
throw new NotFoundHttpException('The requested page does not exist.');
}
+
+ private function canEdit(): bool
+ {
+ $admin = Admin::findOne(['id' => \Yii::$app->user->id]);
+ return $admin && (int)$admin->group_id === AdminGroup::GROUP_IT;
+ }
}
<?php
+use yii_app\records\Admin;
+use yii_app\records\AdminGroup;
+use yii_app\records\AnalystsBusinessOperations;
use yii_app\records\AnalystsBusinessOperationsTypes;
use yii\helpers\Html;
use yii\helpers\Url;
$this->title = 'Типы бизнес операций из 1С';
$this->params['breadcrumbs'][] = $this->title;
+$admin = Admin::findOne(['id' => Yii::$app->user->id]);
+$isIt = $admin && (int)$admin->group_id === AdminGroup::GROUP_IT;
?>
<div class="analysts-business-operations-types-index p-4">
<h1><?= Html::encode($this->title) ?></h1>
- <p>
- <?= Html::a('Создать тип', ['create'], ['class' => 'btn btn-success']) ?>
- </p>
-
<?= GridView::widget([
'dataProvider' => $dataProvider,
'created_at',
[
'class' => ActionColumn::class,
- 'urlCreator' => function ($action, AnalystsBusinessOperationsTypes $model, $key, $index, $column) {
- return Url::toRoute([$action, 'id' => $model->id]);
- }
+ 'template' => '{view} {update} {delete}',
+ 'visibleButtons' => [
+ 'update' => function ($model, $key, $index) use ($isIt) {
+ return $isIt;
+ },
+ 'delete' => function ($model, $key, $index) use ($isIt) {
+ return $isIt;
+ },
+
+ ],
+ 'urlCreator' => function ($action, AnalystsBusinessOperationsTypes $model) {
+ return Url::to([$action, 'id' => $model->id]);
+ },
],
],
]); ?>
<?php
+use yii_app\records\Admin;
+use yii_app\records\AdminGroup;
use yii_app\records\AnalystsBusinessOperations;
use yii\helpers\Html;
use yii\helpers\Url;
$this->title = 'Аналитика хозяйственных операций';
$this->params['breadcrumbs'][] = $this->title;
+$admin = Admin::findOne(['id' => Yii::$app->user->id]);
+$isIt = $admin && (int)$admin->group_id === AdminGroup::GROUP_IT;
?>
<div class="analysts-business-operations-index p-4">
'type_id',
'created_at',
[
- 'class' => ActionColumn::class,
- 'urlCreator' => function ($action, AnalystsBusinessOperations $model, $key, $index, $column) {
- return Url::toRoute([$action, 'id' => $model->id]);
- }
+ 'class' => \yii\grid\ActionColumn::class,
+ 'template' => '{view} {update} {delete}',
+ 'visibleButtons' => [
+ 'update' => function ($model, $key, $index) use ($isIt) {
+ return $isIt;
+ },
+ 'delete' => function ($model, $key, $index) use ($isIt) {
+ return $isIt;
+ },
+ ],
+ 'urlCreator' => function ($action, AnalystsBusinessOperations $model) {
+ return Url::to([$action, 'id' => $model->id]);
+ },
],
],
]); ?>