[
'class' => VerbFilter::className(),
'actions' => [
'delete' => [ 'POST' ],
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => [
'login',
'error',
],
'allow' => true,
],
[
'allow' => true,
'roles' => [ '@' ],
],
],
],
];
}
public function actions()
{
return [
'index' => [
'class' => Index::className(),
'columns' => [
'id' => [
'type' => Index::NUMBER_COL,
],
'name' => [
'type' => Index::ACTION_COL,
'columnConfig' => [
'class' => ActionColumn::class,
'buttonsTemplate' => '{edit}{delete}',
'columnTemplate' => function (Order $model){
if ($model->fast) {
if (empty($model->name)) {
return "(не указано)";
} else {
return "" . $model->name . "";
}
}
return $model->name;
}
],
],
'phone' => [
'type' => Index::STRING_COL,
],
'email' => [
'type' => Index::STRING_COL,
],
'label' => [
'type' => Index::RELATION_COL,
'columnConfig' => [
'relationField' => 'title',
],
],
'created_at' => [
'type' => Index::DATETIME_COL,
],
],
'model' => Order::className(),
'hasLanguage' => false,
'enableMassDelete' => false,
'modelPrimaryKey' => 'id',
'defaultSort' => [
'created_at' => 'DESC'
]
],
];
}
/**
* Creates a new Order model.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* @return mixed
*/
public function actionCreate()
{
$model = new Order();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if (!empty(\Yii::$app->request->post('Product'))) {
foreach (\Yii::$app->request->post('Product') as $id => $count) {
/**
* @var Variant $variant
*/
$variant = Variant::findOne($id);
if ($variant) {
$orderProduct = new OrderProduct(
[
'variant_id' => $variant->id,
'order_id' => $model->id,
'sku' => $variant->sku,
'price' => $variant->price,
'count' => $count,
]
);
$orderProduct->save();
}
}
}
return $this->redirect(
[
'index',
]
);
} else {
$labels = Label::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
$deliveries = Delivery::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
$payments = Payment::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
return $this->render(
'create',
[
'model' => $model,
'labels' => $labels,
'payments' => $payments,
'deliveries' => $deliveries,
]
);
}
}
/**
* Updates an existing Order model.
* If update is successful, the browser will be redirected to the 'view' page.
*
* @param integer $id
*
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \backend\incust\components\IncustErrorLoginException
* @throws \backend\incust\components\IncustException
* @throws \yii\base\InvalidConfigException
* @throws \yii\web\NotFoundHttpException
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save() && !empty(
\Yii::$app->request->post(
'OrderProduct'
)
)) {
OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $id);
if ($model->paid and class_exists(Incust::className())) {
/**
* @var \backend\incust\components\Incust $incust;
*/
$incust = \Yii::$app->get('incust');
$check = $incust->generateCheck($model);
$data = $incust->processCheck($check);
if (is_array($data) and !empty($data)) {
\Yii::info('The check process ID' . $model->id);
$model->save(true, [ 'status_incust' => 'process_check' ]);
$data = $incust->finalCheck($data);
if (!empty($data) and is_array($data)){
$model->status_incust = 'final_check';
$model->id_incust = $data['id'];
$model->bonuses_add = $data['bonuses_added'];
\Yii::info('The check final ID'.$model->id);
$model->save();
}
}
}
return $this->redirect(
[
'index',
]
);
} else {
$labels = Label::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
$deliveries = Delivery::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
$payments = Payment::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
return $this->render(
'update',
[
'model' => $model,
'labels' => $labels,
'payments' => $payments,
'deliveries' => $deliveries,
]
);
}
}
/**
* Deletes an existing Order model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*
* @param integer $id
*
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)
->delete();
return $this->redirect([ 'index' ]);
}
public function actionProductList($q = null, $id = null)
{
$response = \Yii::$app->response;
$response->format = $response::FORMAT_JSON;
$out = [
'results' => [
'id' => '',
'text' => '',
],
];
if (!is_null($q)) {
$out[ 'results' ] = [];
/**
* @var Variant[] $variants
*/
$variants = Variant::find()
->joinWith('product.lang', false)
->where(
[
'like',
'product_lang.title',
$q,
]
)
->orWhere([ 'variant.sku' => $q ])
->all();
foreach ($variants as $variant) {
$out[ 'results' ][] = [
'id' => $variant->id,
'text' => $variant->product->lang->title,
];
}
} elseif ($id > 0) {
/**
* @var Variant $variant
*/
$variant = Variant::find()
->with('lang', 'product.lang')
->where([ 'id' => $id ])
->one();
$out[ 'results' ] = [
'id' => $id,
'text' => $variant->product->lang->title,
];
}
return $out;
}
public function actionAddToOrder()
{
/**
* @var Variant $variant
* @var ActiveForm $form
* @var OrderProduct $orderProduct
*/
\Yii::$app->response->format = Response::FORMAT_JSON;
$id = \Yii::$app->request->post('id');
$count = \Yii::$app->request->post('count') ? \Yii::$app->request->post('count') : 1;
$variant = Variant::find()
->with('product.lang')
->where(
[
'id' => $id,
]
)
->one();
if ($variant) {
$form = new ActiveForm();
$orderProduct = new OrderProduct(
[
'count' => $count,
'variant_id' => $variant->id,
]
);
$row = $this->renderPartial(
'_order_product',
[
'orderProduct' => $orderProduct,
'index' => rand(0, 10000),
'variant' => $variant,
'price' => $variant->price,
'form' => $form,
]
);
return [
'success' => true,
'row' => $row,
'price' => $variant->price * $count,
];
}
return [
'success' => false,
'message' => \Yii::t('app', 'Product not found'),
];
}
/**
* Finds the Order model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer $id
*
* @return Order the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (( $model = Order::findOne($id) ) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionPrint($id)
{
$this->layout = false;
$model = $this->findModel($id);
return $this->renderPartial(
'@backend/views/layouts/print',
[
'model' => $model,
]
);
}
protected function hasProducts()
{
if (empty(\Yii::$app->request->post('OrderProduct'))) {
\Yii::$app->session->setFlash('error', \Yii::t('app', 'Заказ не может быть без товаров'));
return false;
}
return true;
}
/**
* @return array
*/
protected function getLabels()
{
return Label::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
}
/**
* @return array
*/
protected function getPayments()
{
return Payment::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
}
/**
* @return array
*/
protected function getDeliveries()
{
return Delivery::find()
->joinWith('language')
->select(
[
'title',
'id',
]
)
->where(
[
'status' => true,
]
)
->indexBy('id')
->column();
}
}