return ['id'];
}
- public static function getOrders($params = [])
+ public const SOURCES = [
+ 'amo' => 'AMO',
+ 'yandex' => 'ЯндексМаркет',
+ 'flowwow' => 'Flowwow',
+ ];
+
+ public static function getOrders($params = null)
{
$queryAmo = (new Query())
->select([
])
->from('orders_amo o');
+
$queryMarketplace = (new Query())
->select([
- new \yii\db\Expression("'marketplace' AS source"),
- 'mo.id',
- 'mod.delivery_end AS delivery_date',
- 'mo.status_id',
- 'mo.store_id',
- 'mo.payment_method AS payment_type_id',
- 'mo.total AS price',
+ new \yii\db\Expression("
+ CASE
+ WHEN ms.warehouse_id = 2 THEN 'yandex'
+ WHEN ms.warehouse_id = 1 THEN 'flowwow'
+ ELSE 'Marketplace'
+ END AS source"),
+ 'mo.id',
+ 'mod.delivery_end AS delivery_date',
+ 'mo.status_id',
+ 'mo.store_id',
+ 'payment_method',
+ 'mo.total',
new \yii\db\Expression("NULLIF(concat(country, ' ', city, ' ', street, ' ', house, ' ', apartment), '')::TEXT AS delivery_address"),
new \yii\db\Expression("(
SELECT jsonb_agg(jsonb_build_object(
) AS products_json")
])
->from(['mo' => 'marketplace_orders'])
- ->leftJoin(['mod' => 'marketplace_order_delivery'], 'mo.id = mod.order_id');
+ ->leftJoin(['mod' => 'marketplace_order_delivery'], 'mo.id = mod.order_id')
+ ->leftJoin(['ms' => 'marketplace_store'], 'ms.warehouse_guid::TEXT = mo.warehouse_guid::TEXT');
$query = (new Query())
- ->from(['orders' => $queryAmo->union($queryMarketplace, true)]);
-
- // Фильтрация данных
- if (!empty($params['source'])) {
- $query->andWhere(['orders.source' => $params['source']]);
- }
-
- if (!empty($params['delivery_date'])) {
- $query->andWhere(['orders.delivery_date' => $params['delivery_date']]);
- }
-
- if (!empty($params['store_id'])) {
- $query->andWhere(['orders.store_id' => (int) $params['store_id']]);
- }
+ ->andFilterWhere(['source' => $params['source']])
+ ->andFilterWhere(['store_id' => $params['store_id']])
+ ->andFilterWhere(['delivery_date' => $params['delivery_date']])
+ ->from(['orders' => $queryAmo->union($queryMarketplace, true)])
+ ->orderBy('id desc');
return new \yii\data\ArrayDataProvider([
'allModels' => $query->all(),
<?php
+use app\records\OrdersUnion;
+use OpenAPI\Client\Model\OrderPaymentMethodType;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\date\DatePicker;
use kartik\select2\Select2;
use yii\grid\GridView;
+use yii_app\records\CityStore;
+use yii_app\records\MarketplaceStatus;
+use yii_app\records\OrdersAmo;
$this->title = 'Все заказы';
?>
+<div class="all-orders p-5">
+ <h1><?= Html::encode($this->title) ?></h1>
-<h1><?= Html::encode($this->title) ?></h1>
+ <div class="order-filter">
+ <?php $form = ActiveForm::begin([
+ 'method' => 'get',
+ 'action' => ['all-orders'],
-<div class="order-filter">
- <?php $form = ActiveForm::begin([
- 'method' => 'get',
- 'action' => ['all-orders'],
-
- ]); ?>
-
- <div style="display: flex; gap: 15px; align-items: center; flex-wrap: wrap;">
- <?= Select2::widget([
- 'name' => 'source',
- 'value' => Yii::$app->request->get('source'),
- 'data' => ['amo' => 'Amo', 'marketplace' => 'Marketplace'],
- 'options' => ['placeholder' => 'Выберите источник'],
- 'pluginOptions' => ['allowClear' => true],
]); ?>
- <?= DatePicker::widget([
- 'name' => 'delivery_date',
- 'value' => Yii::$app->request->get('delivery_date'),
- 'options' => ['placeholder' => 'Выберите дату'],
- 'pluginOptions' => [
- 'format' => 'yyyy-mm-dd',
- 'autoclose' => true,
- ],
- ]); ?>
+ <div class="row">
+ <div class="col-md-3">
+ <?= Select2::widget([
+ 'name' => 'source',
+ 'value' => Yii::$app->request->get('source'),
+ 'data' => OrdersUnion::SOURCES,
+ 'options' => ['placeholder' => 'Выберите источник'],
+ 'pluginOptions' => ['allowClear' => true],
+ ]); ?>
+ </div>
+ <div class="col-md-4">
- <?= Select2::widget([
- 'name' => 'store_id',
- 'value' => Yii::$app->request->get('store_id'),
- 'data' => [1 => 'Магазин 1', 2 => 'Магазин 2', 3 => 'Магазин 3'], // Должны быть реальные магазины
- 'options' => ['placeholder' => 'Выберите магазин'],
- 'pluginOptions' => ['allowClear' => true],
- ]); ?>
-
- <div class="form-group">
- <?= Html::submitButton('Фильтровать', ['class' => 'btn btn-primary']) ?>
- <?= Html::a('Сбросить', ['index'], ['class' => 'btn btn-default']) ?>
+ <?= DatePicker::widget([
+ 'name' => 'delivery_date',
+ 'value' => Yii::$app->request->get('delivery_date'),
+ 'options' => ['placeholder' => 'Выберите дату'],
+ 'pluginOptions' => [
+ 'format' => 'yyyy-mm-dd',
+ 'autoclose' => true,
+ ],
+ ]); ?>
+ </div>
+ <div class="col-md-3">
+ <?= Select2::widget([
+ 'name' => 'store_id',
+ 'value' => Yii::$app->request->get('store_id'),
+ 'data' => \yii\helpers\ArrayHelper::map(CityStore::find()->all(), 'id', 'name'),
+ 'options' => ['placeholder' => 'Выберите магазин'],
+ 'pluginOptions' => ['allowClear' => true],
+ ]); ?>
+ </div>
+ <div class="col-md-1">
+ <div class="form-group">
+ <?= Html::submitButton('Фильтровать', ['class' => 'btn btn-primary']) ?>
+ </div>
+ </div>
</div>
+
+ <?php ActiveForm::end(); ?>
</div>
- <?php ActiveForm::end(); ?>
-</div>
+ <?= GridView::widget([
+ 'dataProvider' => $dataProvider,
+ 'columns' => [
+ 'id',
+ [
+ 'attribute' => 'source',
+ 'label' => 'Источник',
+ 'value' => function ($model) {
+ return $model['source'] ? OrdersUnion::SOURCES[$model['source']] : 'Неизвестный маркетплейс';
+ }
+ ],
+ [
+ 'attribute' => 'delivery_date',
+ 'label' => 'Дата доставки',
+ ],
+ [
+ 'attribute' => 'store_id',
+ 'label' => 'Магазин сборщик',
+ 'value' => function ($model) {
+ return $model['store_id'] ? CityStore::findOne($model['store_id'])->name : 'ул. Аэродромная 28 (к-р Москва)';
+ }
+ ],
-<?= GridView::widget([
- 'dataProvider' => $dataProvider,
- 'columns' => [
- 'id',
- [
- 'attribute' => 'source',
- 'label' => 'Источник',
- ],
- [
- 'attribute' => 'delivery_date',
- 'label' => 'Дата доставки',
- ],
- [
- 'attribute' => 'store_id',
- 'label' => 'Магазин сборщик',
- ],
- [
- 'attribute' => 'status_id',
- 'label' => 'Статус',
- ],
- [
- 'attribute' => 'payment_type_id',
- 'label' => 'Способ оплаты',
- ],
- [
- 'attribute' => 'price',
- 'label' => 'Цена',
- ],
- [
- 'attribute' => 'delivery_address',
- 'label' => 'Адрес доставки',
- ],
- [
- 'attribute' => 'products_json',
- 'label' => 'Состав',
- 'format' => 'raw',
- 'value' => function ($model) {
- $products = json_decode($model['products_json'], true);
- if (empty($products)) {
- return '<i>(Нет данных)</i>';
+ [
+ 'attribute' => 'status_id',
+ 'label' => 'Статус',
+ 'value' => function ($model) {
+ if ($model['source'] == 'amo') {
+ return OrdersAmo::STATUS_NAMES[intval($model['status_id'])] ?? 'Неизвестный статус';
+ } else {
+ return MarketplaceStatus::findOne($model['status_id'])->name ?? 'Неизвестный статус';
+ }
}
+ ],
- $output = '';
- foreach ($products as $product) {
- $output .= "<h4>{$product['name']}</h4>";
- $output .= '<table class="table table-bordered"><tr><th>Название</th><th>Кол-во</th><th>Цена</th></tr>';
- foreach ($product['items'] as $item) {
- $output .= "<tr><td>{$item['name']}</td><td>{$item['quantity']}</td><td>{$item['price']} ₽</td></tr>";
+ [
+ 'attribute' => 'payment_type_id',
+ 'label' => 'Способ оплаты',
+ 'value' => function ($model) {
+ if ($model['source'] != 'amo') {
+ return OrderPaymentMethodType::PAYMENT_LABELS[$model['payment_type_id']];
}
- $output .= '</table>';
+ return 'payment_type_id';
}
- return $output;
- }
+ ],
+ [
+ 'attribute' => 'price',
+ 'label' => 'Цена',
+ ],
+ [
+ 'attribute' => 'delivery_address',
+ 'label' => 'Адрес доставки',
+ ],
+ [
+ 'attribute' => 'products_json',
+ 'label' => 'Состав',
+ 'format' => 'raw',
+ 'value' => function ($model) {
+ $products = json_decode($model['products_json'], true);
+
+ if (empty($products)) {
+ return '<i>(Нет данных)</i>';
+ }
+
+ $output = '';
+ foreach ($products as $product) {
+ $output .= "<h4>{$product['name']}</h4>";
+ $output .= '<table class="table table-bordered"><tr><th>Название</th><th>Кол-во</th><th>Цена</th></tr>';
+ foreach ($product['items'] as $item) {
+ $output .= "<tr><td>{$item['name']}</td><td>{$item['quantity']}</td><td>{$item['price']} ₽</td></tr>";
+ }
+ $output .= '</table>';
+ }
+ return $output;
+ }
+ ],
],
- ],
-]); ?>
+ ]); ?>
+</div>