private function uninstallStockMvtReason()
 {
     require_once _PS_MODULE_DIR_ . 'erpillicopresta/classes/stock/ErpStockMvtReason.php';
     if (!empty($this->stock_mvt_reason)) {
         // get array keys only
         $stock_mvt_reason_array = array_keys($this->stock_mvt_reason);
         foreach ($stock_mvt_reason_array as $name) {
             $id_stock_mvt_reason = ErpStockMvtReason::existsByName($name);
             if ($id_stock_mvt_reason != false && (int) $id_stock_mvt_reason > 0) {
                 $obj_stock_mvt_reason = new ErpStockMvtReason($id_stock_mvt_reason);
                 if (!$obj_stock_mvt_reason->delete()) {
                     $this->_errors[] = $this->l('Error while deleting stock movement reason !');
                 }
             }
         }
     }
     return true;
 }
 public function ajaxProcess()
 {
     // if a product is selected
     if (Tools::isSubmit('id')) {
         $id_product = (int) Tools::getValue('id');
         $token = Tools::getValue('token');
         // Get declensions
         $datas = $this->getCombinations($id_product);
         $i = 0;
         // get html of new quantity column accorging to advanced stock statut
         $html_new_quantity = $this->renderColumnNewQuantity($id_product, array('have_attribute' => true));
         // Add class and js action on displayed datas
         foreach ($datas as $ligne) {
             // foreach product
             foreach ($ligne as $key => $data) {
                 // Add a class on each column and add a link on the provider reference for the tooltip
                 if ($key == 'product_name' || $key == 'name') {
                     $datas[$i]['product_name'] = '<span class="product_name">' . $data . '</span>';
                 }
                 if ($key == 'first_supplier_ref') {
                     if (!empty($data)) {
                         $datas[$i]['first_supplier_ref'] = '<a href="#" class="supplier_ref" rel="index.php?controller=AdminInventory&ajax=1';
                         $datas[$i]['first_supplier_ref'] .= 'id_product=' . $datas[$i]['id_product'] . '&task=getSupplierReference';
                         $datas[$i]['first_supplier_ref'] .= '&token=' . $token . '">' . $data . '&nbsp';
                         $datas[$i]['first_supplier_ref'] .= '<img src="themes/default/img/icon-search.png" /></a>';
                     } else {
                         $datas[$i]['first_supplier_ref'] = '--';
                     }
                 }
                 if ($key == 'id_product') {
                     $datas[$i]['id_product'] = '<span class="id_product">' . $data . '</span>';
                 }
             }
             $query = new DbQuery();
             // get id_product and id_product_attribute
             $ids = explode(';', $ligne['id_product']);
             $id_product = (int) $ids[0];
             $id_product_attribute = (int) $ids[1];
             $is_selected = '';
             // get the current warehouse
             $id_warehouse = $this->getCookie('id_warehouse');
             // add quantity columns
             // if advanced stock manager inactive, only return the usable quantity in shop
             if (!$this->advanced_stock_management) {
                 $query->select('quantity');
                 $query->from('stock_available');
                 $query->where('id_product = ' . (int) $id_product . ' AND id_product_attribute = ' . (int) $id_product_attribute);
                 // execute query
                 $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query);
                 // add columns to array
                 $datas[$i]['quantity'] = '<span class="quantity">' . (int) $res['quantity'] . '</span>';
             } else {
                 // get the physical and usable quantity
                 $query = new DbQuery();
                 $query->select('physical_quantity');
                 $query->select('usable_quantity ');
                 $query->from('stock');
                 $query->where('id_product = ' . (int) $id_product . ' AND id_product_attribute = ' . (int) $id_product_attribute . ' AND id_warehouse =' . (int) $id_warehouse);
                 // Execute query
                 $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query);
                 // Add columns to array if stock exists
                 if ($res != false) {
                     $datas[$i]['physical_quantity'] = '<span class="physical_quantity">' . $res['physical_quantity'] . '</span>';
                     $datas[$i]['usable_quantity'] = $res['usable_quantity'];
                     // La quantité réelle dépend de l'entrepot
                     // the real quantity depends of the warehouse
                     $manager = StockManagerFactory::getManager();
                     $datas[$i]['real_quantity'] = $manager->getProductRealQuantities($id_product, $id_product_attribute, $this->getCookie('id_warehouse') == -1 ? null : array($this->getCookie('id_warehouse')), true);
                 } else {
                     // else stock 0
                     $datas[$i]['physical_quantity'] = $datas[$i]['usable_quantity'] = $datas[$i]['real_quantity'] = 0;
                 }
                 // JMA
                 // use warehouse cookie to get locations
                 if ($this->getCookie('id_warehouse') != '') {
                     $location = ErpWarehouseProductLocationClass::getCompleteLocation($id_product, $id_product_attribute, $this->getCookie('id_warehouse') == -1 ? 1 : $this->getCookie('id_warehouse'));
                 } else {
                     $location = ErpWarehouseProductLocationClass::getCompleteLocation($id_product, $id_product_attribute, $this->getCookie('id_warehouse') == -1 ? 1 : $this->getCookie('id_warehouse'));
                 }
                 if (!empty($location['CompleteArea'])) {
                     // split to get area, sub area and location
                     $location = explode(';', $location['CompleteArea']);
                     if (count($location) > 0) {
                         // Area, sub area, location
                         $datas[$i]['location'] = '<span>' . $this->l('Area') . ' : </span>' . $location[0];
                         $datas[$i]['location'] .= '<br/><span>' . $this->l('Subarea') . ' : </span>' . $location[1];
                         $datas[$i]['location'] .= '<br/><span>' . $this->l('Location') . ' : </span>' . $location[2];
                     } else {
                         $datas[$i]['location'] = '--';
                     }
                 } else {
                     $datas[$i]['location'] = '--';
                 }
             }
             // add ajustment reason
             $array_reason = ErpStockMvtReason::getStockMvtReasons((int) $this->context->language->id);
             $datas[$i]['mvt_reason'] = '<select name="reason" class="table_select">';
             $datas[$i]['mvt_reason'] .= '<option value="-1" ' . $is_selected . '>--</option>';
             foreach ($array_reason as $reason) {
                 $id_reason = $reason['id_stock_mvt_reason'];
                 $name_reason = $reason['name'];
                 $datas[$i]['mvt_reason'] .= '<option value="' . (int) $id_reason . '">' . $name_reason . '</option>';
             }
             $datas[$i]['mvt_reason'] .= '</select>';
             // get the found quantity
             $datas[$i]['new_quantity'] = $html_new_quantity;
             $i++;
         }
         $attributes = array('data' => $datas, 'fields_display' => $this->fields_list);
         $json = Tools::jsonEncode($attributes);
         echo $json;
         die;
     } elseif (Tools::isSubmit('task') && Tools::getValue('task') == 'getSupplierReference') {
         include_once _PS_MODULE_DIR_ . 'erpillicopresta/ajax/ajax.php';
     }
 }