Example #1
0
 /**
  * import orders
  * @return boolean
  */
 public function importOrders()
 {
     $this->users = load_users();
     $this->products_i18n = load_products_i18n();
     if (isset($this->xml->СписокРасходныеНакладные)) {
         $statuses = array();
         foreach ($this->xml->СписокРасходныеНакладные as $status) {
             if ($status->IDЗаказПокупателя . '') {
                 $statuses[$status->IDЗаказПокупателя . ''] = array('id' => $status->ID . '', 'code' => $status->Номер . '', 'date' => strtotime($status->Дата . ''));
             }
         }
     }
     foreach ($this->xml->СписокЗаказыПокупателя as $order) {
         $data = array();
         $data['date_created'] = strtotime($order->Дата . '');
         $data['user_deliver_to'] = $order->Адрес . '';
         $data['user_phone'] = $order->КонтактныйТелефон . '';
         $data['paid'] = (int) $order->ПризнакПередоплаты;
         $data['external_id'] = $order->ID . '';
         $data['code'] = $order->Номер . '';
         $data['delivery_date'] = strtotime($order->СрокДоставки . '');
         $data['partner_external_id'] = $order->IDОрганизация . '';
         $data['status'] = 1;
         if ($statuses[$data['external_id']]) {
             $data['status'] = 2;
             $data['invoice_external_id'] = $statuses[$data['external_id']]['id'];
             $data['invoice_code'] = $statuses[$data['external_id']]['code'];
             $data['invoice_date'] = $statuses[$data['external_id']]['date'];
         }
         $user = is_user($order->IDКонтрагент, $this->users);
         if ($user) {
             $userInfo = $this->ci->db->where('id', $user['id'])->get('users')->row_array();
             $data['user_id'] = $user['id'];
             $data['user_full_name'] = $userInfo['username'];
             $data['user_email'] = $userInfo['email'];
         } else {
             return;
         }
         $order_exist = is_order($data['external_id'], $this->orders);
         if ((string) $order->IDWeb) {
             $data['id'] = (string) $order->IDWeb . '' ? (string) $order->IDWeb . '' : $order_exist['id'];
             foreach ($this->xml->СписокРасходныеНакладные as $status) {
                 if ((string) $status->IDЗаказПокупателя == $order->ID) {
                     $this->updateOrder($status, $data);
                     //                        var_dumps($status);
                     break;
                 }
             }
             //                $this->updateOrder($order, $data);
         } else {
             $this->insertOrder($order, $data);
         }
     }
     $this->insertData($this->orders_table);
     $this->updateData($this->orders_table, 'id');
     $inserted_orders = load_orders();
     foreach ($inserted_orders as $value) {
         foreach ($this->insert_order_products as $key => $order_product) {
             if ($order_product['external_order_id'] == $value['external_id'] && $order_product['external_order_id'] != NULL) {
                 $this->insert_order_products[$key]['order_id'] = $value['id'];
                 unset($this->insert_order_products[$key]['external_order_id']);
             }
         }
     }
     $this->insert = $this->insert_order_products;
     $this->insertData($this->orders_products_table);
     $this->updateData($this->orders_table, 'id');
     $this->update = $this->update_order_products;
     $this->updateData($this->orders_products_table, 'id');
 }
Example #2
0
 /**
  * update order into db
  * @param object $order
  * @param array $data
  * @return boolean
  */
 public function updateOrder($order, $data)
 {
     $total_price = 0;
     $this->update[] = $data;
     $order_id = is_order($order->ID . '', $this->orders);
     if (isset($order->Строки)) {
         foreach ($order->Строки as $product) {
             $data = array();
             $data['quantity'] = (int) $product->Количество;
             $data['price'] = (double) $product->Цена;
             $data['external_id'] = $product->IDДокумента . '';
             $product_i18n = is_product_i18n($product->IDНоменклатура . '', $this->products_i18n);
             if ($product_i18n) {
                 $data['product_id'] = $product_i18n['id'];
                 $data['product_name'] = $product_i18n['name'];
                 $data['variant_name'] = $product_i18n['name'];
                 $data['variant_id'] = $product_i18n['varId'];
             } else {
                 return false;
             }
             if (is_orders_product($product->IDДокумента . '', $this->orders_products)) {
                 $this->update_order_products[] = $data;
             } else {
                 $data['order_id'] = $order_id['id'];
                 $data['external_id'] = $product->IDДокумента . '';
                 $this->insert_order_products[] = $data;
             }
             $total_price += (int) $product->Сумма;
         }
         //update order total price
         $data = array();
         $data['total_price'] = $total_price;
         $data['external_id'] = $order->ID . '';
         $this->update[] = $data;
     }
 }