Commit eb190b1f8c6d212102f92c22e30a1ede32b6c064

Authored by Alexey Boroda
1 parent d16773b7

-Order form add product in create action

controllers/OrderController.php
... ... @@ -158,7 +158,7 @@
158 158 $count = 1;
159 159 }
160 160 $productVariant = ProductVariant::findOne($id);
161   -
  161 +
162 162 $model = OrderProduct::find()
163 163 ->where(
164 164 [
... ... @@ -234,6 +234,7 @@
234 234 $model = new Order();
235 235 $model->phone = '+38(000)000-00-00';
236 236 $model->name = \Yii::t('app', 'Новый заказ');
  237 + $model->published = false;
237 238 $model->save();
238 239  
239 240 $dataProvider = new ActiveDataProvider(
... ... @@ -244,6 +245,8 @@
244 245 );
245 246  
246 247 if ($model->load(Yii::$app->request->post()) && $model->save()) {
  248 + $model->published = true;
  249 + $model->save();
247 250 return $this->redirect([ 'index' ]);
248 251 } else {
249 252 return $this->render(
... ... @@ -413,10 +416,18 @@
413 416  
414 417 public function actionExitOrder($id)
415 418 {
416   - $model = $this->findModel($id);
  419 + try {
  420 + $model = $this->findModel($id);
  421 + } catch (NotFoundHttpException $e) {
  422 + return $this->redirect('index');
  423 + }
417 424 if ($model->edit_id == \Yii::$app->user->id) {
418 425 $this->unblockOrder($id);
419 426 }
  427 +
  428 + if (!$model->published) {
  429 + $model->deleteUnpublished();
  430 + }
420 431 return $this->redirect('index');
421 432 }
422 433  
... ...
models/Order.php
... ... @@ -13,45 +13,47 @@
13 13 *
14 14 * @todo Write docs and refactor
15 15 * @package artweb\artbox\ecommerce\models
16   - * @property integer $created_at
17   - * @property integer $updated_at
18   - * @property integer $deleted_at
19   - * @property integer $deadline
20   - * @property boolean $wasted
21   - * @property string $delivery_cost
22   - * @property integer $reason
23   - * @property string $check
24   - * @property string $sms
25   - * @property int $id
26   - * @property integer $edit_id
27   - * @property integer $edit_time
28   - * @property integer $manager_id
29   - * @property int $user_id
30   - * @property string $name
31   - * @property string $phone
32   - * @property string $phone2
33   - * @property string $email
34   - * @property string $adress
35   - * @property string $body
36   - * @property double $total
37   - * @property string $date_time
38   - * @property string $date_dedline
39   - * @property string $reserve
40   - * @property string $status
41   - * @property string $comment
42   - * @property int $label
43   - * @property int $pay
44   - * @property int $numbercard
45   - * @property int $delivery
46   - * @property string $declaration
47   - * @property string $stock
48   - * @property string $consignment
49   - * @property string $payment
50   - * @property string $insurance
51   - * @property double $amount_imposed
52   - * @property string $shipping_by
53   - * @property string $city
54   - * @property string $deliveryString
  16 + * @property OrderProduct[] $products
  17 + * @property integer $created_at
  18 + * @property integer $updated_at
  19 + * @property integer $deleted_at
  20 + * @property integer $deadline
  21 + * @property boolean $wasted
  22 + * @property string $delivery_cost
  23 + * @property integer $reason
  24 + * @property string $check
  25 + * @property string $sms
  26 + * @property int $id
  27 + * @property integer $edit_id
  28 + * @property integer $edit_time
  29 + * @property integer $manager_id
  30 + * @property int $user_id
  31 + * @property string $name
  32 + * @property string $phone
  33 + * @property string $phone2
  34 + * @property string $email
  35 + * @property string $adress
  36 + * @property string $body
  37 + * @property double $total
  38 + * @property string $date_time
  39 + * @property string $date_dedline
  40 + * @property string $reserve
  41 + * @property string $status
  42 + * @property string $comment
  43 + * @property int $label
  44 + * @property int $pay
  45 + * @property int $numbercard
  46 + * @property int $delivery
  47 + * @property string $declaration
  48 + * @property string $stock
  49 + * @property string $consignment
  50 + * @property string $payment
  51 + * @property string $insurance
  52 + * @property double $amount_imposed
  53 + * @property string $shipping_by
  54 + * @property string $city
  55 + * @property string $deliveryString
  56 + * @property boolean $published
55 57 */
56 58 class Order extends ActiveRecord
57 59 {
... ... @@ -106,7 +108,10 @@
106 108 {
107 109 return [
108 110 [
109   - [ 'pay' ],
  111 + [
  112 + 'pay',
  113 + 'published',
  114 + ],
110 115 'boolean',
111 116 ],
112 117 [
... ... @@ -220,6 +225,7 @@
220 225 'consignment' => Yii::t('app', 'Номер накладной'),
221 226 'manager_id' => Yii::t('app', 'Менеджер'),
222 227 'delivery_cost' => Yii::t('app', 'Стоимость доставки'),
  228 + 'published' => Yii::t('app', 'Опубликован'),
223 229 ];
224 230 }
225 231  
... ... @@ -317,10 +323,25 @@
317 323 $products = $this->products;
318 324 $newTotal = 0;
319 325 foreach ($products as $product) {
320   - if ($product->removed) continue;
  326 + if ($product->removed) {
  327 + continue;
  328 + }
321 329 $newTotal += $product->count * $product->price;
322 330 }
323 331 $this->total = $newTotal;
324 332 $this->save();
325 333 }
  334 +
  335 + public function deleteUnpublished()
  336 + {
  337 + /**
  338 + * @var OrderProduct[] $products
  339 + */
  340 + $products = $this->products;
  341 + foreach ($products as $product) {
  342 + $product->delete();
  343 + }
  344 +
  345 + $this->delete();
  346 + }
326 347 }
327 348 \ No newline at end of file
... ...
models/OrderSearch.php
... ... @@ -68,7 +68,8 @@
68 68 */
69 69 public function search($params)
70 70 {
71   - $query = Order::find();
  71 + $query = Order::find()
  72 + ->where([ 'published' => true ]);
72 73  
73 74 // add conditions that should always apply here
74 75  
... ...
views/order/create.php
... ... @@ -15,6 +15,24 @@
15 15 'url' => [ 'index' ],
16 16 ];
17 17 $this->params[ 'breadcrumbs' ][] = $this->title;
  18 +
  19 + $js = '
  20 + window.onbeforeunload = function(e) {
  21 + $.ajax({
  22 + type: "GET",
  23 + url: "/admin/ecommerce/order/exit-order",
  24 + data: {
  25 + id: ' . $model->id . ',
  26 + },
  27 + success: function() {
  28 + console.log("Exit order");
  29 + }
  30 + });
  31 +};
  32 +';
  33 +
  34 + $this->registerJs($js, View::POS_READY);
  35 +
18 36 ?>
19 37  
20 38 <div class="order-create">
... ...