From fd6822b202afd3d96133bb8abe04068cf979d481 Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Thu, 1 Apr 2021 09:34:14 +0200 Subject: [PATCH] Debug feature to import stock movement --- htdocs/langs/en_US/stocks.lang | 2 +- htdocs/product/stock/massstockmove.php | 104 +++++++++++++++++++------ 2 files changed, 83 insertions(+), 23 deletions(-) diff --git a/htdocs/langs/en_US/stocks.lang b/htdocs/langs/en_US/stocks.lang index 24dc4cef0c6..301d26333cc 100644 --- a/htdocs/langs/en_US/stocks.lang +++ b/htdocs/langs/en_US/stocks.lang @@ -252,4 +252,4 @@ InfoTemplateImport=Uploaded file needs to have this format (* are mandatory fiel LabelOfInventoryMovemement=Inventory %s ReOpen=Reopen ConfirmFinish=Confirm closing - +ObjectNotFound=%s not found \ No newline at end of file diff --git a/htdocs/product/stock/massstockmove.php b/htdocs/product/stock/massstockmove.php index 0627a6a1cf7..aac4d66fc88 100644 --- a/htdocs/product/stock/massstockmove.php +++ b/htdocs/product/stock/massstockmove.php @@ -315,7 +315,7 @@ if ($action == 'importCSV' && !empty($user->rights->stock->mouvement->creer)) { $importcsv->import_open_file($fullpath); $labelsrecord = $importcsv->import_read_record(); - if ($nblinesrecord <= 1) { + if ($nblinesrecord < 1) { setEventMessages($langs->trans("BadNumberOfLinesMustHaveAtLeastOneLinePlusTitle"), null, 'errors'); } else { $i=0; @@ -329,28 +329,46 @@ if ($action == 'importCSV' && !empty($user->rights->stock->mouvement->creer)) { continue; } //var_dump($data); - + $formproduct = new FormProduct($db); + $productstatic = new Product($db); + $warehousestatics = new Entrepot($db); + $warehousestatict = new Entrepot($db); $tmp_id_sw = $data[$i][0]['val']; $tmp_id_tw = $data[$i][1]['val']; $tmp_id_product = $data[$i][2]['val']; $tmp_qty = $data[$i][3]['val']; $tmp_batch = $data[$i][4]['val']; - // TODO If product is a ref (not numeric or starts with "ref:..."), retreive the id of product from the ref + if (!is_numeric($tmp_id_product)) { + $result = fetchref($productstatic, $tmp_id_product); + $tmp_id_product = $result; + $data[$i][2]['val'] = $result; + } if (!($tmp_id_product > 0)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product")), null, 'errors'); } - // TODO If warehouse is a ref (not numeric or starts with "ref:..."), retreive the id of product from the ref + + if (!is_numeric($tmp_id_sw)) { + $result = fetchref($warehousestatics, $tmp_id_sw); + $tmp_id_sw = $result; + $data[$i][0]['val'] = $result; + } if (!($tmp_id_sw > 0)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WarehouseSource")), null, 'errors'); } - // TODO If warehouse is a ref (not numeric or starts with "ref:..."), retreive the id of product from the ref + + if (!is_numeric($tmp_id_tw)) { + $result = fetchref($warehousestatict, $tmp_id_tw); + $tmp_id_tw = $result; + $data[$i][1]['val'] = $result; + } if (!($tmp_id_tw > 0)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WarehouseTarget")), null, 'errors'); } + if ($tmp_id_sw > 0 && $tmp_id_tw == $tmp_id_sw) { $error++; $langs->load("errors"); @@ -585,24 +603,39 @@ foreach ($listofdata as $key => $val) { $warehousestatics->fetch($val['id_sw']); $warehousestatict->fetch($val['id_tw']); - print ''; - print ''; - print $warehousestatics->getNomUrl(1); - print ''; - print ''; - print $warehousestatict->getNomUrl(1); - print ''; - print ''; - print $productstatic->getNomUrl(1).' - '.$productstatic->label; - print ''; - if ($conf->productbatch->enabled) { - print ''; - print $val['batch']; - print ''; + if ($productstatic->id <= 0) { + $error++; + setEventMessages($langs->trans("ObjectNotFound", $langs->transnoentitiesnoconv("Product")), null, 'errors'); + } + if ($warehousestatics->id <= 0) { + $error++; + setEventMessages($langs->trans("ObjectNotFound", $langs->transnoentitiesnoconv("WarehouseSource")), null, 'errors'); + } + if ($warehousestatics->id <= 0) { + $error++; + setEventMessages($langs->trans("ObjectNotFound", $langs->transnoentitiesnoconv("WarehouseTarget")), null, 'errors'); + } + + if (!$error) { + print ''; + print ''; + print $warehousestatics->getNomUrl(1); + print ''; + print ''; + print $warehousestatict->getNomUrl(1); + print ''; + print ''; + print $productstatic->getNomUrl(1).' - '.$productstatic->label; + print ''; + if ($conf->productbatch->enabled) { + print ''; + print $val['batch']; + print ''; + } + print ''.$val['qty'].''; + print ''.img_delete($langs->trans("Remove")).''; + print ''; } - print ''.$val['qty'].''; - print ''.img_delete($langs->trans("Remove")).''; - print ''; } print ''; @@ -645,3 +678,30 @@ if ($action == 'delete') { // End of page llxFooter(); $db->close(); + +/** + * Verify if $haystack startswith $needle + * @param String $haystack string to test + * @param String $needle string to find + * @return false if Ko true else + */ +function startsWith($haystack, $needle) +{ + $length = strlen($needle); + return substr($haystack, 0, $length) === $needle; +} + +/** + * Fetch object with ref + * @param Object $static_object static object to fetch + * @param String $tmp_ref ref of the object to fetch + * @return <0 if Ko or Id of object + */ +function fetchref($static_object, $tmp_ref) +{ + if (startsWith($tmp_ref, 'ref:')) { + $tmp_ref = str_replace('ref:', '', $tmp_ref); + } + $static_object->fetch('', $tmp_ref); + return $static_object->id; +}