Exemple #1
0
 public function init()
 {
     parent::init();
     if ($this->customer instanceof CustomerModel && !$this->model instanceof ContragentModel) {
         $this->model = ContragentModel::createEmptyContragent($this->customer);
     } elseif (!$this->customer instanceof CustomerModel && $this->model instanceof ContragentModel) {
         $this->customer = $this->model->customer;
     }
 }
 /**
  * @param null $customer
  * @return string|\yii\web\Response
  * @throws \yii\web\ServerErrorHttpException
  */
 public function actionCreate($customer = null)
 {
     $customer = intval($customer) > 0 ? Customer::findOne(['id' => intval($customer)]) : null;
     $contragent = Contragent::createEmptyContragent(null === $customer ? Customer::createEmptyCustomer() : $customer);
     if (true === \Yii::$app->request->isPost) {
         $data = \Yii::$app->request->post();
         if ($contragent->load($data) && $contragent->save()) {
             if (!empty($contragent->getPropertyGroup())) {
                 $contragent->getPropertyGroup()->appendToObjectModel($contragent);
                 $data[$contragent->getAbstractModel()->formName()] = isset($data['ContragentNew']) ? $data['ContragentNew'] : [];
             }
             $contragent->saveModelWithProperties($data);
             $contragent->refresh();
             $deliveryInformation = DeliveryInformation::createNewDeliveryInformation($contragent, false);
             return $this->redirect(Url::toRoute(['edit', 'id' => $contragent->id]));
         }
     }
     return $this->render('create', ['model' => $contragent]);
 }
 /**
  * @return string|Response
  * @throws \yii\base\Exception
  * @throws \yii\web\ServerErrorHttpException
  */
 public function actionCreate()
 {
     $model = Order::create(false, false, true);
     $model->setScenario('backend');
     if (Yii::$app->request->isPost) {
         $data = Yii::$app->request->post();
         if ($model->load($data) && $model->validate()) {
             if (User::USER_GUEST === intval($model->user_id)) {
                 $model->customer_id = 0;
             } elseif (null === Customer::findOne(['user_id' => intval($model->user_id), 'id' => intval($model->customer_id)])) {
                 $model->customer_id = 0;
             }
             if (0 === intval($model->customer_id)) {
                 $customer = Customer::createEmptyCustomer(intval($model->user_id));
                 if ($customer->load($data) && $customer->save()) {
                     if (!empty($customer->getPropertyGroup())) {
                         $customer->getPropertyGroup()->appendToObjectModel($customer);
                         $data[$customer->getAbstractModel()->formName()] = isset($data['CustomerNew']) ? $data['CustomerNew'] : [];
                     }
                     $customer->saveModelWithProperties($data);
                     $customer->refresh();
                     $model->customer_id = $customer->id;
                 }
             } else {
                 $customer = Customer::findOne(['id' => $model->customer_id]);
             }
             if (0 === $model->contragent_id || null === Contragent::findOne(['id' => $model->contragent_id, 'customer_id' => $model->customer_id])) {
                 $contragent = Contragent::createEmptyContragent($customer);
                 if ($contragent->load($data) && $contragent->save()) {
                     if (!empty($contragent->getPropertyGroup())) {
                         $contragent->getPropertyGroup()->appendToObjectModel($contragent);
                         $data[$contragent->getAbstractModel()->formName()] = isset($data['ContragentNew']) ? $data['ContragentNew'] : [];
                     }
                     $contragent->saveModelWithProperties($data);
                     $contragent->refresh();
                     $model->contragent_id = $contragent->id;
                 }
             } else {
                 $contragent = Contragent::findOne(['id' => $model->contragent_id]);
             }
             if ($model->save()) {
                 OrderDeliveryInformation::createNewOrderDeliveryInformation($model, false);
                 DeliveryInformation::createNewDeliveryInformation($contragent, false);
                 return $this->redirect(Url::toRoute(['view', 'id' => $model->id]));
             }
         }
     }
     return $this->render('create', ['model' => $model]);
 }
    var tpl = '<div class="s2customer-result">' +
        '<strong>' + (data.type || '') + '</strong>' +
        '</div>';
    return tpl;
}
JSCODE;
$_jsDataFunc = <<<'JSCODE'
function (term, page) {
    return {
        search: {customer:$('select#order-customer_id').val()}
    };
}
JSCODE;
echo \app\backend\widgets\Select2Ajax::widget(['initialData' => [$model->contragent_id => null !== $model->contragent ? $model->contragent->type : 'New contragent'], 'model' => $model, 'modelAttribute' => 'contragent_id', 'form' => $form, 'multiple' => false, 'searchUrl' => \yii\helpers\Url::toRoute(['ajax-contragent']), 'pluginOptions' => ['minimumInputLength' => null, 'allowClear' => false, 'escapeMarkup' => new \yii\web\JsExpression('function (markup) {return markup;}'), 'templateResult' => new \yii\web\JsExpression($_jsTemplateResultFunc), 'templateSelection' => new \yii\web\JsExpression('function (data) {return data.type || data.text;}'), 'ajax' => ['data' => new \yii\web\JsExpression($_jsDataFunc)]]]);
echo Html::tag('div', Html::a(Yii::t('app', 'Clear'), '#clear', ['data-sel' => 'order-contragent_id', 'class' => 'col-md-offset-2']));
echo Html::tag('div', \app\modules\shop\widgets\Contragent::widget(['viewFile' => 'contragent/inherit_form', 'model' => \app\modules\shop\models\Contragent::createEmptyContragent($customer), 'form' => $form]), ['id' => 'div_contragent']);
?>
    </div>
</div>
<?php 
BackendWidget::end();
$form->end();
$_js = <<<'JSCODE'
$(function(){
    $('select#order-user_id').on('change', function(event) {
        $('select#order-customer_id').val(0).trigger('change');
    });

    $('select#order-customer_id').on('change', function(event) {
        if (0 == $(this).val()) {
            $('div#div_customer').removeClass('hide');
 public static function handleStageCustomer(OrderStageEvent $event)
 {
     $order = $event->eventData()['order'];
     /** @var Customer $customer */
     $customer = !empty($order->customer) ? $order->customer : (!empty(Customer::getCustomerByUserId($order->user_id)) ? Customer::getCustomerByUserId($order->user_id) : Customer::createEmptyCustomer($order->user_id));
     /** @var Contragent[] $contragents */
     $contragents = array_reduce($customer->contragents, function ($result, $item) use($customer) {
         /** @var \app\modules\shop\models\Contragent $item */
         $result[$item->id] = $item;
         return $result;
     }, [0 => \app\modules\shop\models\Contragent::createEmptyContragent($customer)]);
     $event->addEventData(['user_id' => $order->user_id, 'customer' => $customer, 'contragents' => $contragents]);
 }