Blame view

src/lib/models/orders.php 18.2 KB
1ea3b987   Administrator   maby first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
  <?php
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  namespace models;
  
  class orders extends \db
  {
      /////////////////////////////////////////////////////////////////////////////
  
      public function addOrder( $order)
      {
          $connection = $this->database;
          $this->updateOrderId();
          try
  
          {
              $connection->begin();
              if( !empty( $order['email'] ) )
              {
                  $data_customer_isset = $this->get(
                      '
                          SELECT
                              id,
                              status
                          FROM
                              public.customers
                          WHERE
                              email = :email
                          LIMIT
                              1
                      ',
                      [
                          'email' => $order['email']
                      ],
                      -1
                  );
              }
              else
              {
                  $data_customer_isset = $this->get(
                      '
                          SELECT
                              id,
                              status
                          FROM
                              public.customers
                          WHERE
                              phone = :phone
                          LIMIT
                              1
                      ',
                      [
                          'phone' => $order['phone']
                      ],
                      -1
                  );
              }
  
              if( empty( $data_customer_isset ) ) // new customer
              {
                  $data_confirm_key   = $this->getDi()->get('models')->getCustomers()->addNewCustomer( $order );
                  $confirmed          = 0;
                  $new                = 1;
                  $customer_id        = $data_confirm_key['0']['id'];
              }
              elseif( !empty( $data_customer_isset ) and $data_customer_isset['0']['status'] == 0 ) // the customer do not finish registration
              {
                  $confirmed          = 0;
                  $new                = 0;
                  $customer_id        = $data_customer_isset['0']['id'];
              }
              elseif( !empty( $data_customer_isset ) and $data_customer_isset['0']['status'] == 1 ) // the customer finish registration
              {
                  $confirmed          = 1;
                  $new                = 0;
                  $customer_id        = $data_customer_isset['0']['id'];
              }
              $data_orders = $this->get(
                  '
                      INSERT INTO
                          public.orders
                              (
                                  customer_id,
                                  name,
                                  phone,
                                  city,
                                  address,
                                  delivery,
                                  pay,
                                  email,
                                  comments,
                                  action_discount_id,
                                  firm_total,
                                  promo_code
                              )
                              VALUES
                              (
                                  :customer_id,
                                  :name,
                                  :phone,
                                  :city,
                                  :address,
                                  :delivery,
                                  :pay,
                                  :email,
                                  :comments,
                                  :action_discount_id,
                                  :firm_total,
                                  :promo_code
                              )
                              RETURNING id
                  ',
                  [
                      'customer_id'   => $customer_id,
                      'name'          => $order['name'],
                      'phone'         => $order['phone'],
                      'city'          => $order['city'],
                      'address'       => $order['address'],
                      'delivery'      => $order['delivery'],
                      'pay'           => $order['pay'],
                      'email'         => $order['email'],
                      'comments'      => $order['comments'],
                      'action_discount_id' => isset($order['action_id']) ? $order['action_id'] : null,
                      'firm_total'    => isset($order['firm_total']) ? $order['firm_total'] : null,
                      'promo_code'    => isset($order['promo_code']) ? $order['promo_code'] : null
                  ],
                  -1
              );
  
              foreach( $order['items'] as $i )
              {
                  $data_orders2items = $this->get(
                      '
                          INSERT INTO
                              public.orders2items
                                  (
                                      order_id,
                                      item_id,
                                      item_count,
                                      price
                                  )
                                  VALUES
                                  (
                                      :order_id,
                                      :item_id,
                                      :item_count,
                                      :price
                                  )
                      ',
                      [
                          'order_id'      => $data_orders['0']['id'],
                          'item_id'       => $i['id'],
                          'item_count'    => $i['count'],
                          'price'         => $i['price2']
                      ]
                  );
              }
  			
  			$data =
                  [
                      'proposal_number'   => $data_orders['0']['id'],
                      'confirmed'         => $confirmed,
                      'new'               => $new,
                  ];
  			
  			if( $order['delivery'] == 3 || $order['delivery'] == 4 )
  			{
  				switch( $order['delivery'] )
  				{
  					case 3:
  					default:
  						$order['rcpt_warehouse'] = substr( $order['store_address'], 0, strpos( $order['store_address'], '-' ) );
  						$order['novaposhta_tnn'] = $this->getDi()->get('novaposhta')->ttn( $data_orders['0']['id'], $order['city'], $order['rcpt_warehouse'], NULL, $order['name'], $order['phone'], '10', $order['total_sum'] );
  						//$order['novaposhta_tnn'] = $this->getDi()->get('novaposhta')->ttn_ref( $data_orders['0']['id'], $order['city_ref'], $order['store_ref'], NULL, $order['name'], $order['phone'], '10', $order['total_sum'] );
  						break;
  					case 4:
  						$order['novaposhta_tnn'] = $this->getDi()->get('novaposhta')->ttn( $data_orders['0']['id'], $order['city'], NULL, $order['address'], $order['name'], $order['phone'], '10', $order['total_sum'] );
  						break;
  						
  					
  				}
  				
  				if( !empty( $order['novaposhta_tnn'] ) && !empty( $data_orders['0']['id'] ) )
  				{
  					$this->addNovaposhtaTnn( $order['novaposhta_tnn'], $data_orders['0']['id'] );
  					$data['novaposhta_tnn'] = $order['novaposhta_tnn'];
  				}
  			}
  			
  			$connection->commit();
  			return $data;
  		}
  		catch(\Exception $e)
  		{
  			$connection->rollback();
          }
  
  		return false;
  	}
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function addNovaposhtaTnn( $tnn, $order_id )
      {
          return $this->exec(
              '
                  UPDATE
                      public.orders
                  SET
                      novaposhta_tnn  = :novaposhta_tnn
                  WHERE
                      id     = :id
              ',
              [
                  'novaposhta_tnn'    => $tnn,
                  'id'                => $order_id
              ]
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function getOrdersSumById($id) {
          return $this->get(
              '
              SELECT
                      SUM((price * item_count)) as price
              FROM
                    public.orders2items
              WHERE
              order_id = :id
              GROUP BY
              order_id
              ',
              [
                  'id' => $id,
              ],
              -1
          );
      }
  
      public function getOrdersByCustomerId( $customer_id )
      {
          return $this->get(
              '
                  SELECT
                      id,
                      created_date,
                      status,
                      delivery
                  FROM
                      public.orders
                  WHERE
                      customer_id = :customer_id
                  ORDER BY id DESC
              ',
              [
                  'customer_id' => $customer_id
              ],
              -1
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function getOrdersByOrderId( $order_id, $lang_id )
      {
          return $this->get(
              '
                  SELECT
                      item_id,
                      item_count,
                      (
                        SELECT
                            group_id
                        FROM
                            public.items
                        WHERE
                            id = public.orders2items.item_id
                        LIMIT 1
                      ) AS group_id,
                      (
                          SELECT
                              firm
                          FROM
                              public.items
                          WHERE
                              id = public.orders2items.item_id
                          LIMIT 1
                      ) AS firm,
                      (
                          SELECT
                              price2
                          FROM
                              public.items
                          WHERE
                              id = public.orders2items.item_id
                          LIMIT 1
                      ) AS price2,
                      (
                          SELECT
                              size
                          FROM
                              public.items
                          WHERE
                              id = public.orders2items.item_id
                          LIMIT 1
                      ) AS size,
                      (
                          SELECT
                              title
                          FROM
                              public.items_i18n
                          WHERE
                              item_id = public.orders2items.item_id
                              AND
                              lang_id = :lang_id
                          LIMIT 1
                      ) AS title,
                      (
                          SELECT
                              alias
                          FROM
                              public.items_group_alias
                          WHERE
                              group_id =
                              (
                                  SELECT
                                      group_id
                                  FROM
                                      public.items
                                  WHERE
                                      id = public.orders2items.item_id
                                  LIMIT 1
                              )
                              LIMIT 1
                      ) AS group_alias,
                      (
                          SELECT
                              cover
                          FROM
                              public.items_group
                          WHERE
                              group_id =
                              (
                                  SELECT
                                      group_id
                                  FROM
                                      public.items
                                  WHERE
                                      id = public.orders2items.item_id
                                  LIMIT 1
                              )
                              LIMIT 1
                      ) as cover,
                      (
                          SELECT
                              alias
                          FROM
                              public.types_i18n
                          WHERE
                              type =
                              (
                                  SELECT
                                      type
                                  FROM
                                      public.items
                                  WHERE
                                      id = public.orders2items.item_id
                                  LIMIT 1
                              )
                              AND
                              lang_id = :lang_id
                          LIMIT 1
                      ) AS type_alias,
                      (
                          SELECT
                              alias
                          FROM
                              public.subtypes_i18n
                          WHERE
                              subtype =
                              (
                                  SELECT
                                      subtype
                                  FROM
                                      public.items
                                  WHERE
                                      id = public.orders2items.item_id
                                  LIMIT 1
                              )
                              AND
                              type =
                              (
                                  SELECT
                                      type
                                  FROM
                                      public.items
                                  WHERE
                                      id = public.orders2items.item_id
                                  LIMIT 1
                              )
                              AND
                              lang_id = :lang_id
                      ) AS subtype_alias
                  FROM
                      public.orders2items
                  WHERE
                      order_id = :order_id
              ',
              [
                  'order_id'  => $order_id,
                  'lang_id'   => $lang_id
              ],
              -1
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
      /////////////////////////////////////////////////////////////////////////////
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function getAllOrders( $page )
      {
          return $this->get(
              '
                  SELECT
                      id,
                      name,
                      created_date,
                      phone,
                      comments,
                      status,
                      status_pay,
                      delivery
                  FROM
                      public.orders
                  ORDER BY
                      created_date
                  LIMIT
                      '.\config::get( 'limits/admin_orders' ).'
                  OFFSET
                      '.($page-1)*(\config::get( 'limits/admin_orders' ))
              ,
              [
  
              ],
              -1
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function getSortedOrders( $sort_type, $sort_id, $page )
      {
          return $this->get(
              '
                  SELECT
                      id,
                      name,
                      created_date,
                      phone,
                      comments,
                      status,
                      status_pay,
                      delivery
                  FROM
                      public.orders
                  WHERE
                      '.$sort_type.' = '.$sort_id.'
                  ORDER BY
                      created_date
                  LIMIT
                      '.\config::get( 'limits/admin_orders' ).'
                  OFFSET
                      '.($page-1)*(\config::get( 'limits/admin_orders' ))
              ,
              [
  
              ],
              -1
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function getOrdersWithIds( $orders_ids )
      {
          return $this->get(
              '
                  SELECT
                      order_id,
                      item_count,
                      price
                  FROM
                      public.orders2items
                  WHERE
                      order_id IN ('.join( ',', $orders_ids ).')
              ',
              [
  
              ],
              -1
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function countAllOrders()
      {
          return $this->get(
              '
                  SELECT
                      COUNT(id) AS total
                  FROM
                      public.orders
              ',
              [
  
              ],
              -1
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function countSortedOrders( $sort_type, $sort_id )
      {
          return $this->get(
              '
                  SELECT
                      COUNT(id) AS total
                  FROM
                      public.orders
                  WHERE
                      '.$sort_type.' = '.$sort_id.'
              ',
              [
  
              ],
              -1
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
      public function countStatus()
      {
          return $this->get(
              '
                  SELECT
                      COUNT(status) AS total,
                      status
                  FROM
                      public.orders
                  GROUP BY
                      status
              ',
              [
  
              ],
              -1
          );
      }
  
      public function updateOrderId()
      {
          return $this->get(
              "
                  SELECT setval('orders_id_seq', (SELECT MAX(id) FROM public.orders))
              ",
              [
  
              ],
              -1
          );
      }
  
      /////////////////////////////////////////////////////////////////////////////
  
  
  }
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////