/** * Import the pricelist of supplier * * There is a supplier specific logic for converting pricelists. * For new supplliers you should add some peace of code in this method * before trying to convert its pricelists. * * @param int $supplier_id Identifier of supplier * @param array $file_path An information array of uploaded file, receved * directly from $_FILES array * * @throws Exception Throws an exception if something critical happens, * should be catched for informative purposes only. * * @return bool Returns true on success. */ public static function importSupplierPricelist($supplier_id, $file_path) { global $STOCKS, $implemented_convertors; $suppliers = Suppliers::getSuppliers(); if (!in_array($suppliers[$supplier_id]['prog_name'], $implemented_convertors)) { throw new Exception('Convertion does not implemented for this supplier.'); } $tmp_file_name = ''; if (is_array($file_path)) { if ($file_path['type'] == 'text/csv') { // do not convert if csv file $file_path = $file_path['tmp_name']; } else { if ($file_path['type'] == 'application/vnd.ms-excel') { //convert xls file to csv if (filesize($file_path['tmp_name'])) { $tmp_file_name = tempnam('/tmp', 'xls2csv_pricelist_'); $additional_conf = ''; // add supplier specific parameters to the xls converter switch ($suppliers[$supplier_id]['prog_name']) { case 'SouthPalmira': case 'medterm': $additional_conf = ' -scp1251 '; break; } $res = shell_exec('export LANG=en_US.UTF-8 && xls2csv -dUTF-8 -q3 -c, -x ' . $additional_conf . $file_path['tmp_name'] . ' > ' . $tmp_file_name); $file_path = $tmp_file_name; } else { throw new Exception('Empty file.'); } } else { // unknown file type throw new Exception('Incorrect file.'); } } } if (filesize($file_path)) { $fp = fopen($file_path, 'r'); if ($fp) { $row = 0; $new_fields = array('prov_sku' => '', 'price' => '', 'stock' => ''); Product::deleteTempProd($supplier_id); Product::startInsertTempProd(); while (($data = fgetcsv($fp, 0, ',', '"')) !== false) { $row++; //reset $new_fields for each new line $new_fields['prov_sku'] = ''; $new_fields['price'] = '0'; $new_fields['stock'] = $STOCKS['not in stock']; // supplier specific code for importing goes here: switch ($suppliers[$supplier_id]['prog_name']) { case 'preximD': if (!strlen($data[1])) { continue 2; } $new_fields['prov_sku'] = $data[0]; $new_fields['price'] = (string) $data[1]; $new_fields['stock'] = $STOCKS['in_stock']; break; case 'SouthPalmira': if ($row < 3 || !strlen($data[2])) { continue 2; } $new_fields['prov_sku'] = $data[2]; $new_fields['price'] = (string) $data[3]; $new_fields['stock'] = $STOCKS['in_stock']; break; case 'NTCom': if ($row < 9 || !strlen($data[2])) { continue 2; } $new_fields['prov_sku'] = $data[2]; switch (trim($data[8])) { case '+': case '+/-': $new_fields['price'] = (string) $data[4]; $new_fields['stock'] = $STOCKS['in_stock']; break; case '-': $new_fields['stock'] = $STOCKS['not_in_stock']; break; case 'в пути': $new_fields['stock'] = $STOCKS['wait']; break; case 'под заказ': $new_fields['stock'] = $STOCKS['order']; break; } break; case 'medterm': if ($row < 9 || !strlen($data[1]) || !strlen($data[0])) { continue 2; } $new_fields['prov_sku'] = $data[1]; $new_fields['stock'] = $STOCKS['adjust']; break; case 'vodovorot': if ($row < 7 || !strlen($data[1]) || !strlen($data[2])) { continue 2; } $new_fields['prov_sku'] = $data[1]; $new_fields['stock'] = $STOCKS['adjust']; break; case 'foxtrot': if (!strlen($data[0]) || !strlen($data[1])) { continue 2; } $new_fields['prov_sku'] = $data[0]; $new_fields['price'] = (string) $data[1]; $new_fields['stock'] = $STOCKS['in_stock']; break; } try { Product::insertTempProd($supplier_id, $new_fields['prov_sku'], $new_fields['price'], $new_fields['stock']); } catch (Exception $e) { echo 'There was an error while importing: ' . $e->getMessage() . "\n"; } } Product::endInsertTempProd(); fclose($fp); } else { throw new Exception('Cant get access to ' . $file_path); } } else { throw new Exception('Empty file.'); } // remove some trash if (strlen($tmp_file_name)) { unlink($tmp_file_name); } return true; }