/** * @return string|\yii\web\Response * @throws \yii\web\ServerErrorHttpException */ public function actionCreate($user = null) { $user = User::USER_GUEST !== intval($user) ? User::findOne(['id' => intval($user)]) : null; $customer = Customer::createEmptyCustomer(null === $user ? 0 : $user->id); if (true === \Yii::$app->request->isPost) { $data = \Yii::$app->request->post(); 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(); return $this->redirect(Url::toRoute(['edit', 'id' => $customer->id])); } } return $this->render('create', ['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]); }
<?php /** * @var \yii\web\View $this */ if (Yii::$app->user->isGuest) { echo '<p>' . Yii::t('app', 'Please register to save your profile') . '</p>'; } else { $customer = null === \app\modules\shop\models\Customer::getCustomerByUserId(Yii::$app->user->id) ? \app\modules\shop\models\Customer::createEmptyCustomer(Yii::$app->user->id, false) : \app\modules\shop\models\Customer::getCustomerByUserId(Yii::$app->user->id); echo \app\modules\shop\widgets\Customer::widget(['viewFile' => 'customer/profile', 'model' => $customer, 'formAction' => \yii\helpers\Url::toRoute(['/shop/cabinet/update'], true)]); }
'<strong>' + (data.username || '') + '</strong>' + '<div>' + (data.first_name || '') + ' (' + (data.email || '') + ')</div>' + '</div>'; return tpl; } JSCODE; $_jsDataFunc = <<<'JSCODE' function (term, page) { return { search: {term:term.term, user:$('select#order-user_id').val()} }; } JSCODE; echo \app\backend\widgets\Select2Ajax::widget(['initialData' => [$model->customer_id => null !== $model->customer ? $model->customer->first_name : 'New customer'], 'model' => $model, 'modelAttribute' => 'customer_id', 'form' => $form, 'multiple' => false, 'searchUrl' => \yii\helpers\Url::toRoute(['ajax-customer']), 'pluginOptions' => ['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.first_name || data.text;}'), 'ajax' => ['data' => new \yii\web\JsExpression($_jsDataFunc)]]]); echo Html::tag('div', Html::a(Yii::t('app', 'Clear'), '#clear', ['data-sel' => 'order-customer_id', 'class' => 'col-md-offset-2'])); echo Html::tag('div', \app\modules\shop\widgets\Customer::widget(['viewFile' => 'customer/inherit_form', 'form' => $form, 'model' => $customer = \app\modules\shop\models\Customer::createEmptyCustomer(0)]), ['id' => 'div_customer']); ?> </div> <div class="col-md-4"> <?php $_jsTemplateResultFunc = <<<'JSCODE' function (data) { if (data.loading) return data.text; var tpl = '<div class="s2customer-result">' + '<strong>' + (data.type || '') + '</strong>' + '</div>'; return tpl; } JSCODE; $_jsDataFunc = <<<'JSCODE'
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]); }