_form.php 14.5 KB
<?php
    
    use artweb\artbox\ecommerce\models\Label;
    use artweb\artbox\ecommerce\models\Order;
    use artweb\artbox\ecommerce\models\OrderPayment;
    use artweb\artbox\ecommerce\models\OrderProduct;
    use backend\models\SmsTemplate;
    use kartik\grid\GridView;
    use kartik\widgets\DatePicker;
    use kartik\widgets\Select2;
    use kartik\widgets\SwitchInput;
    use yii\bootstrap\Dropdown;
    use yii\data\ActiveDataProvider;
    use yii\helpers\Html;
    use yii\bootstrap\ActiveForm;
    use yii\helpers\ArrayHelper;
    use artweb\artbox\ecommerce\models\Delivery;
    use yii\web\View;
    use yii\web\JsExpression;
    
    /**
     * @var View               $this
     * @var Order              $model
     * @var ActiveForm         $form
     * @var ActiveDataProvider $dataProvider
     */
    
    $js = <<< JS
$(document).on('submit', '#add-product-form', function(e) {
    e.preventDefault();
    var addFormData = $(this).serializeArray();
    var addFormAction = this.action;
    $.ajax({
        url: addFormAction,
        type: "POST",
        data: addFormData,
        success: function (data) {
             if (data.status === "success") {
                 $.pjax.reload({container:"#order-products-grid"});  //Reload GridView
             }
        },
        error: function () {
        }
    });
});
JS;
    
    $this->registerJs($js, View::POS_READY);
    
    $js = <<< JS
$('#order-phone, #order-phone2').mask('+38(000)000-00-00', {
    placeholder: '+38(___)___-__-__'
});
JS;
    
    $this->registerJs($js, View::POS_READY);
    
    $js = <<< JS
$(document).on('change', '#sms-template-selector', function(event) {
    var text = $('#select2-sms-template-selector-container').attr('title');
    var val = $('option:contains(' + text + ')')
    console.log(text);
    console.log(val);
});
JS;
    
    $this->registerJs($js, View::POS_READY);
?>

<?php $form = ActiveForm::begin(); ?>
<div class="container">
    <div class="form-group">
        <div class="row">
            <?= Html::submitButton(
                $model->isNewRecord ? \Yii::t('app', 'Create') : \Yii::t('app', 'Update'),
                [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary' ]
            ) ?>
            <?= Html::a(
                \Yii::t('app', 'Печать'),
                yii\helpers\Url::to(
                    [
                        'order/print',
                        'order_id' => $model->id,
                    ]
                ),
                [
                    'class'  => $model->isNewRecord ? 'btn btn-info disabled' : 'btn btn-info',
                    'target' => '_blank',
                ]
            ) ?>
        </div>
        <div class="row">
            <div class="col-sm-6">
                
                <?= $form->field($model, 'deadline')
                         ->widget(
                             DatePicker::className(),
                             [
                    
                             ]
                         ) ?>
                
                <?php
                    echo $form->field($model, 'pay')
                              ->widget(
                                  SwitchInput::className(),
                                  [
                                      'name'          => 'pay',
                                      'pluginOptions' => [
                                          'onText'  => \Yii::t('app', 'Оплачено'),
                                          'offText' => \Yii::t('app', 'Не оплачено'),
                                      ],
                                  ]
                              ); ?>
                
                <?= $form->field($model, 'reason')
                         ->dropDownList(
                             [
                                 1 => 'То',
                                 2 => 'Сё',
                             ],
                             [ 'prompt' => 'Выберите причину' ]
                         ) ?>
                
                <?= $form->field($model, 'label')
                         ->dropDownList(
                             ArrayHelper::map(
                                 Label::find()
                                      ->asArray()
                                      ->all(),
                                 'id',
                                 'label'
                             ),
                             [ 'prompt' => 'Выберите метку' ]
                         ); ?>
                
                <?= $form->field($model, 'name') ?>
                
                <?= $form->field($model, 'phone') ?>
                
                <?= $form->field($model, 'phone2') ?>
                
                <?= $form->field($model, 'email') ?>
                
                <?= $form->field(
                    $model,
                    'numbercard'
                )
                         ->textInput([ 'readonly' => true ]) ?>
                
                <?= $form->field($model, 'comment')
                         ->textarea([ 'rows' => '3' ]) ?>
                <?= $form->field($model, 'delivery')
                         ->dropDownList(
                             ArrayHelper::map(
                                 Delivery::find()
                                         ->joinWith('lang')
                                         ->asArray()
                                         ->all(),
                                 'id',
                                 'lang.title'
                             ),
                             [ 'prompt' => \Yii::t('app', 'Выберите доставку ...') ]
                         ) ?>
            
            </div>
            <div class="col-sm-6">
                
                <?= $form->field($model, 'declaration') ?>
                
                <?= $form->field($model, 'stock') ?>
                
                <?= $form->field($model, 'consignment') ?>
                
                <?= $form->field($model, 'payment')
                         ->dropDownList(
                             ArrayHelper::map(
                                 OrderPayment::find()
                                             ->joinWith('lang')
                                             ->where([ 'status' => OrderPayment::ACTIVE ])
                                             ->asArray()
                                             ->all(),
                                 'id',
                                 'lang.title'
                             ),
                             [ 'prompt' => 'Способ оплаты ...' ]
                         ); ?>
                
                <?= $form->field($model, 'insurance') ?>
                
                <?= $form->field($model, 'amount_imposed') ?>
                
                <?= $form->field($model, 'shipping_by')
                         ->dropDownList(
                             ArrayHelper::getColumn(Order::SHIPPING_BY, 'label'),
                             [ 'prompt' => 'Оплата доставки ...' ]
                         ); ?>
                
                <?= $form->field($model, 'city') ?>
                
                <?= $form->field($model, 'adress') ?>
                
                <?= $form->field($model, 'body')
                         ->textarea([ 'rows' => '3' ]) ?>
                
                <?= $form->field($model, 'check') ?>
                
                <?= $form->field($model, 'sms') ?>
            
            </div>
        </div>
    </div>
</div>
<?php ActiveForm::end(); ?>


<div class="container">
    <div class="row">
        <?php
            echo GridView::widget(
                [
                    'dataProvider' => $dataProvider,
                    'layout'       => '{items}{pager}',
                    'columns'      => [
                        'id',
                        'sku',
                        'product_name',
                        'productVariant.product.brand.lang.title',
                        'productVariant.lang.title',
                        'price',
                        [
                            'class'           => 'kartik\grid\EditableColumn',
                            'attribute'       => 'count',
                            'editableOptions' => [
                                'header'       => \Yii::t('app', 'Количество'),
                                'inputType'    => kartik\editable\Editable::INPUT_SPIN,
                                'options'      => [
                                    'pluginOptions' => [
                                        'min' => 0,
                                        'max' => 5000,
                                    ],
                                ],
                                'pluginEvents' => [
                                    'editableSuccess' => 'function(event) { $.pjax.reload({container:"#order-products-grid"}); }',
                                ],
                            ],
                            'format'          => [
                                'decimal',
                                0,
                            ],
                            'pageSummary'     => false,
                        ],
                        'sum_cost',
                        [
                            'class'    => 'yii\grid\ActionColumn',
                            'template' => '{delete}',
                        ],
                    ],
                    'responsive'   => true,
                    'hover'        => true,
                    'pjax'         => true,
                    'pjaxSettings' => [
                        'options' => [
                            'scrollTo' => 'false',
                            'id'       => 'order-products-grid',
                        ],
                    ],
                ]
            );
        ?>
    </div>
</div>

<div class="container">
    <div class="row">
        <?php $newProductForm = ActiveForm::begin(
            [
                'action' => yii\helpers\Url::to([ 'add' ]),
                'id'     => 'add-product-form',
            ]
        );
            $newOrderProduct = new OrderProduct();
        ?>
        <div class="col-md-8">
            <?php echo $newProductForm->field($newOrderProduct, 'id')
                                      ->widget(
                                          Select2::className(),
                                          [
                                              'options'       => [ 'placeholder' => 'Search for a product ...' ],
                                              'pluginOptions' => [
                                                  'allowClear'         => true,
                                                  'disabled'           => $model->isNewRecord ? true : false,
                                                  'minimumInputLength' => 3,
                                                  'language'           => [
                                                      'errorLoading' => new JsExpression(
                                                          "function () { return 'Waiting for results...'; }"
                                                      ),
                                                  ],
                                                  'ajax'               => [
                                                      'url'      => \yii\helpers\Url::to([ 'find-product' ]),
                                                      'dataType' => 'json',
                                                      'data'     => new JsExpression(
                                                          'function(params) { return {q:params.term}; }'
                                                      ),
                                                  ],
                                                  'escapeMarkup'       => new JsExpression(
                                                      'function (markup) { return markup; }'
                                                  ),
                                                  'templateResult'     => new JsExpression(
                                                      'function(data) { return data.sku; }'
                                                  ),
                                                  'templateSelection'  => new JsExpression(
                                                      'function (data) { return data.sku; }'
                                                  ),
                                              ],
                                          ]
                                      );
            
            ?>
        </div>
        <div class="col-md-2">
            <?php echo $newProductForm->field($newOrderProduct, 'count')
                                      ->input('number'); ?>
        </div>
        <div class="col-md-2">
            <?php echo Html::submitButton(
                'Add',
                [
                    'class' => $model->isNewRecord ? 'btn btn-primary disabled' : 'btn btn-primary',
                ]
            ) ?>
        </div>
        <?php echo $newProductForm->field($newOrderProduct, 'order_id')
                                  ->hiddenInput(
                                      [
                                          'value' => $model->id,
                                      ]
                                  )
                                  ->label(false) ?>
        <?php ActiveForm::end(); ?>
    </div>
</div>
<br>
<br>
<br>
<br>
<br>
<div class="container">
    <div class="row">
        <?php
            echo Select2::widget(
                [
                    'id' => 'sms-template-selector',
                    'name'          => 'select-sms-template',
                    'data'          => ArrayHelper::map(
                        SmsTemplate::find()
                                   ->asArray()
                                   ->all(),
                        'text',
                        'title'
                    ),
                    'options'       => [ 'placeholder' => \Yii::t('app', 'Выберите шаблон') ],
                    'pluginOptions' => [
                        'allowClear' => true,
                    ],
                ]
            );
        
        ?>
        
        <?php
            echo Html::textarea(
                'sms-text',
                '',
                [
                    'rows' => 5,
                ]
            );
        ?>
    </div>
</div>