index.php 10.9 KB
<?php
    /**
     * @var ActiveDataProvider $dataProvider
     * @var OrderSearch        $searchModel
     * @var View               $this
     */
    
    use artweb\artbox\ecommerce\models\Delivery;
    use artweb\artbox\ecommerce\models\Label;
    use artweb\artbox\ecommerce\models\Order;
    use artweb\artbox\ecommerce\models\OrderSearch;
    use common\models\User;
    use kartik\daterange\DateRangePicker;
    use kartik\select2\Select2;
    use yii\data\ActiveDataProvider;
    use yii\helpers\ArrayHelper;
    use yii\helpers\Html;
    use kartik\grid\GridView;
    use yii\helpers\StringHelper;
    use yii\helpers\Url;
    use yii\web\JsExpression;
    use yii\web\View;
    use yii\widgets\ActiveForm;
    use yii\widgets\Pjax;
    
    $this->title = 'Заказы';
    $this->params[ 'breadcrumbs' ][] = $this->title;
    
    $js = <<< JS
$('[name="OrderSearch[phone]"]').mask('+38(000)000-00-00', {
    placeholder: '+38(___)___-__-__'
});
JS;
    
    $this->registerJs($js, View::POS_READY);

?>
<h1>Заказы</h1>
<p>
    <?= Html::a('Add order', [ 'create' ], [ 'class' => 'btn btn-success' ]) ?>
</p>
<?php Pjax::begin(); ?>
<?php
    $searchForm = ActiveForm::begin(
        [
            'method' => 'GET',
            'action' => Url::to([ 'order/index' ]),
            'id'     => 'search-form',
        ]
    );
?>

<p>
    
    <?php echo Html::submitButton(
        'Search',
        [
            'class' => 'btn btn-primary',
        ]
    ) ?>

</p>

<?php
    echo $searchForm->field($searchModel, 'label')
                    ->widget(
                        Select2::className(),
                        [
                            'data'          => Label::find()
                                                    ->joinWith('lang')
                                                    ->select(
                                                        [
                                                            'CONCAT(order_label.label,order_label_lang.title) AS name',
                                                            'id',
                                                        ]
                                                    )
                                                    ->indexBy('id')
                                                    ->asArray()
                                                    ->column(),
                            'options'       => [ 'placeholder' => 'Select a state ...' ],
                            'pluginOptions' => [
                                'allowClear' => true,
                                'multiple'   => true,
                            ],
                        ]
                    );
?>
<br>
<?php
    echo $searchForm->field($searchModel, 'delivery')
                    ->widget(
                        Select2::className(),
                        [
                            'data'          => Delivery::find()
                                                       ->joinWith('lang')
                                                       ->select('order_delivery_lang.title, id')
                                                       ->indexBy('id')
                                                       ->asArray()
                                                       ->column(),
                            'options'       => [ 'placeholder' => 'Select a state ...' ],
                            'pluginOptions' => [
                                'allowClear' => true,
                                'multiple'   => true,
                            ],
                        ]
                    );
?>

<?php
    $query = new JsExpression(
        'function(params) { return {q:params.term}; }'
    );
    echo $searchForm->field($searchModel, 'sku')
                    ->widget(
                        Select2::className(),
                        [
                            'options'       => [ 'placeholder' => 'Search for a product ...' ],
                            'pluginOptions' => [
                                'allowClear'         => true,
                                'minimumInputLength' => 3,
                                'language'           => [
                                    'errorLoading' => new JsExpression(
                                        "function () { return 'Waiting for results...'; }"
                                    ),
                                ],
                                'ajax'               => [
                                    'url'      => \yii\helpers\Url::to([ 'find-product' ]),
                                    'dataType' => 'json',
                                    'data'     => $query,
                                ],
                                'escapeMarkup'       => new JsExpression(
                                    'function (markup) { return markup; }'
                                ),
                                'templateResult'     => new JsExpression(
                                    'function(data) { return data.sku; }'
                                ),
                                'templateSelection'  => new JsExpression(
                                    'function (data) {
                                     if(data.sku == undefined) {
                                         return "sku"; 
                                     } else {
                                         return data.sku;
                                     }
                                    }'
                                ),
                            ],
                        ]
                    );

?>

<?= $searchForm->field($searchModel, 'manager_id')
               ->dropDownList(
                   ArrayHelper::map(
                       User::find()
                           ->asArray()
                           ->all(),
                       'id',
                       'username'
                   ),
                   [ 'prompt' => \Yii::t('app', 'Выберите менеджера ...') ]
               ) ?>

<?= $searchForm->field($searchModel, 'email')
               ->textInput() ?>

<?= $searchForm->field($searchModel, 'declaration')
               ->textInput() ?>

<?= $searchForm->field($searchModel, 'consignment')
               ->textInput() ?>


<p>
    <?php
        echo GridView::widget(
            [
                'dataProvider' => $dataProvider,
                'filterModel'  => $searchModel,
                'rowOptions'   => function($model) {
                    if ($model->wasted) {
                        return [ 'class' => 'danger' ];
                    } else {
                        return [];
                    }
                },
                'columns'      => [
                    [
                        'attribute' => 'id',
                        'filter'    => $searchForm->field($searchModel, 'id')
                                                  ->textInput(),
                        'content' => function($model) {
                            $manager = $model->manager;
                            if (empty($manager)) {
                                return $model->id;
                            } else {
                                return $model->id . ' ' . $manager->username;
                            }
                        }
                    ],
                    [
                        'attribute' => 'created_at',
                        'format'    => 'date',
                        'filter'    => $searchForm->field($searchModel, 'date_range')
                                                  ->widget(
                                                      DateRangePicker::className(),
                                                      [
                                                          'convertFormat' => false,
                                                          'pluginOptions' => [
                                                              'locale' => [
                                                                  'format'    => 'D-M-Y',
                                                                  'separator' => ' to ',
                                                              ],
                                                              'opens'  => 'left',
                                                          ],
                                                      ]
                                                  )
                                                  ->label(false)
                                                  ->render(),
                    ],
                    [
                        'attribute' => 'deadline',
                        'content'   => function($model) {
                            if ($model->deadline == '') {
                                return '';
                            } else {
                                return \Yii::$app->formatter->asDate($model->deadline);
                            }
                        },
                    ],
                    'name',
                    'total',
                    [
                        'attribute' => 'pay',
                        'content'   => function($model) {
                            if ($model->pay == false) {
                                return '<span class="glyphicon glyphicon-remove"></span>';
                            } else {
                                return '<span class="glyphicon glyphicon-ok"></span>';
                            }
                        },
                    ],
                    'phone',
                    'adress',
                    [
                        'attribute' => 'label',
                        'filter'    => false,
                        'value'     => function($model) {
                            /**
                             * @var Order $modl
                             */
                            if (empty( $model->orderLabel )) {
                                return '--';
                            } else {
                                return $model->orderLabel->label;
                            }
                        },
                    ],
                    
                    [
                        'attribute' => 'body',
                        'content'   => function($model) {
                            if (!empty( $model->body )) {
                                return StringHelper::truncate($model->body, 12, '...');
                            } else {
                                return '';
                            }
                        },
                    ],
                    'sms',
                    [
                        'class' => 'yii\grid\ActionColumn',
                        'template' => \Yii::$app->user->identity->isAdmin() ? '{view} {update} {delete}' : '{view} {update}'
                    ],
                ],
            ]
        );
    ?>
</p>
<?php
    ActiveForm::end();
?>
<?php Pjax::end(); ?>