public function renderList()
    {
        $this->toolbar_title = $this->l('Inventory reports');
        if (Tools::isSubmit('id_container') && Tools::getValue('id_container') > 0) {
            self::$currentIndex .= '&id_container=' . (int) Tools::getValue('id_container');
        }
        // Get id container. if noone selected, take the first one
        if (($id_container = $this->getCurrentValue('id_container')) == false) {
            $id_container = (int) ErpInventory::getFirstId();
            $this->tpl_list_vars['id_container'] = $id_container;
        }
        // get total stock gap of inventory
        $total_stock_gap = InventoryProduct::getTotalStockGap($id_container);
        $this->tpl_list_vars['total_gap'] = Tools::displayPrice($total_stock_gap);
        // Query
        $this->_select = 'p.id_product,
						IF(pa.id_product_attribute, pa.reference, p.reference) as reference,
						IFNULL(pa.id_product_attribute, 0) as id_product_attribute,
			IFNULL(CONCAT(pl.name, \' : \', GROUP_CONCAT(DISTINCT agl.`name`, \' - \', al.name SEPARATOR \', \')),pl.name) as name,
			p.id_product, IFNULL(pa.id_product_attribute, 0) as id_product_attribute, w.name as warehouse, a.qte_before, a.qte_after, smrl.name as reason, (qte_after - qte_before) as gap,
			(
						SELECT ps.product_supplier_reference
						FROM ' . _DB_PREFIX_ . 'product_supplier ps
						WHERE ps.id_product = a.id_product
						AND ps.id_product_attribute = a.id_product_attribute
						LIMIT 1
			)as first_supplier_ref';
        $this->_join = 'INNER JOIN ' . _DB_PREFIX_ . 'product_lang pl ON (a.id_product = pl.id_product AND pl.id_lang = ' . (int) $this->context->language->id . ')
				INNER JOIN ' . _DB_PREFIX_ . 'product p ON a.id_product = p.id_product
				INNER JOIN ' . _DB_PREFIX_ . 'stock_mvt_reason_lang smrl ON (a.id_mvt_reason = smrl.id_stock_mvt_reason AND smrl.id_lang = ' . (int) $this->context->language->id . ')
				INNER JOIN ' . _DB_PREFIX_ . 'stock_mvt_reason smr ON a.id_mvt_reason = smr.id_stock_mvt_reason
				INNER JOIN ' . _DB_PREFIX_ . 'warehouse w ON w.id_warehouse = a.id_warehouse
				INNER JOIN ' . _DB_PREFIX_ . 'product_attribute pa ON a.id_product_attribute= pa.id_product_attribute
				INNER JOIN ' . _DB_PREFIX_ . 'product_attribute_combination pac ON pac.id_product_attribute = pa.id_product_attribute
				INNER JOIN ' . _DB_PREFIX_ . 'attribute atr ON atr.id_attribute= pac.id_attribute
				INNER JOIN ' . _DB_PREFIX_ . 'attribute_lang al ON (al.id_attribute= pac.id_attribute AND al.id_lang=' . (int) $this->context->language->id . ')
				INNER JOIN ' . _DB_PREFIX_ . 'attribute_group_lang agl ON (agl.id_attribute_group= atr.id_attribute_group AND agl.id_lang=' . (int) $this->context->language->id . ')
				INNER JOIN ' . _DB_PREFIX_ . 'erpip_inventory i ON a.id_erpip_inventory = i.id_erpip_inventory';
        $this->_where = 'AND i.id_erpip_inventory = ' . $id_container;
        $this->_order = 'a.id_erpip_inventory_product DESC  LIMIT 10';
        $this->_group = 'GROUP BY a.id_product_attribute';
        // Send values to view
        $this->tpl_list_vars['containers'] = ErpInventory::getContainers();
        $list = parent::renderList();
        return $list;
    }
 public function createContainer()
 {
     // only if we get all the necessary values to the inventory
     if ($this->id_erpip_inventory != '' && $this->name != '' && $this->advanced_stock_management != '' && $this->inventory_values != '' && $this->id_employee != '' && $this->firstname != '' && $this->lastname != '' && ($this->id_warehouse != '' || $this->advanced_order_management == false)) {
         $create_container = false;
         // we create a new inventory
         if ($this->id_erpip_inventory == '-1') {
             $inventory = new ErpInventory();
             $inventory->id_erpip_inventory = '';
             $inventory->name = $this->name;
             $create_container = $inventory->add(true);
             self::$id_erpip_inventory_static = $inventory->getLastId();
         } else {
             self::$id_erpip_inventory_static = $this->id_erpip_inventory;
         }
         // create inventory contanier
         if ($create_container || $this->id_erpip_inventory != '-1') {
             // Split to get products
             $products = explode('_', $this->inventory_values);
             // reverse array read to get last values
             $products = array_reverse($products);
             $array_products = array();
             foreach ($products as $key => $product) {
                 // Split to get product values
                 $produc_line = explode('|', $product);
                 if (count($produc_line) > 1) {
                     foreach ($produc_line as $element) {
                         // Split to get key and value
                         $element = explode('==', $element);
                         if (isset($element[1])) {
                             $array_products[$key][$element[0]] = $element[1];
                         }
                     }
                     $ids = $array_products[$key]['idproduct'] . ';' . $array_products[$key]['idproductattribute'];
                     // if already treat by update more recent, continue
                     foreach (self::$local_store as $local_product) {
                         if ($local_product == $ids) {
                             continue 2;
                         }
                     }
                     // Record product
                     $this->productHandler($array_products[$key]);
                     // Store treated product, that only treat the LAST update
                     array_push(self::$local_store, $ids);
                 }
             }
             // if no error
             if (count($this->errors) == 0) {
                 $this->confirmations[] = $this->l('Inventory has been completed successfully');
             }
         } else {
             $this->errors[] = Tools::displayError($this->l('Error while creating a new directory. Please contact the customer service.'));
         }
     } else {
         $this->errors[] = Tools::displayError($this->l('Error : missing data. Please try again.'));
     }
 }