Merge branch 'module_training_encoding'

This commit is contained in:
Rodney Figaro 2017-04-13 13:05:32 +02:00
commit 8c5e6bffe3
4 changed files with 42 additions and 16 deletions

View File

@ -92,15 +92,15 @@ class CSVWriter
ob_start();
if (is_array($this->header)) {
fwrite($output, implode(';', $this->header).PHP_EOL);
$this->flushCsvLine($output, $this->header);
}
foreach($this->lines as $line) {
fwrite($output, implode(';', $line).PHP_EOL);
$this->flushCsvLine($output, $line);
}
if (is_array($this->footer)) {
fwrite($output, implode(';', $this->footer).PHP_EOL);
$this->flushCsvLine($output, $this->footer);
}
$this->buffer = ob_get_clean();
@ -108,4 +108,10 @@ class CSVWriter
return $this;
}
private function flushCsvLine($file_handle, array &$line)
{
fwrite($file_handle, implode(';', $line).PHP_EOL);
//fwrite($file_handle, '"'.implode('";"', $line).'"'.PHP_EOL);
}
}

View File

@ -92,7 +92,7 @@ class TrainingBilan
static function computeLineResult(ArrayExtended $cols)
{
$code_client = strtolower(trim($cols[self::COL_CODE_CLIENT]));
$code_client = TrainingFixedCodeClient::cleanupCode($cols[self::COL_CODE_CLIENT]);
$low_module_name = strtolower($cols[self::COL_MODULE_NAME]);
if (empty($code_client)) {

View File

@ -43,6 +43,20 @@ class TrainingFixedCodeClient extends ObjectModel
$csv_output->save();
}
static function cleanupCode($code)
{
$code = strtolower(trim((string)$code));
if (preg_match('/^0+$/', $code)===1) {
$code = '0';
}
else {
$code = ltrim($code, '0');
}
return $code;
}
static function importFromCsvFile($file_path)
{
$imported_codes = array();
@ -51,12 +65,14 @@ class TrainingFixedCodeClient extends ObjectModel
$ok = $reader
->setExpectedHeader(self::$header)
->setOnEachLine(function(ArrayExtended $cols) use (&$imported_codes) {
$wrong_code = strtolower(trim((string)$cols[self::CODE_CLIENT_WRONG]));
$right_code = strtolower(trim((string)$cols[self::CODE_CLIENT_RIGHT]));
$wrong_code = self::cleanupCode($cols[self::CODE_CLIENT_WRONG]);
$right_code = self::cleanupCode($cols[self::CODE_CLIENT_RIGHT]);
if (empty($wrong_code)) {
$wrong_code = self::CODE_EMPTY;
}
// get rid of left zeros
$imported_codes[$wrong_code] = $right_code;
return true;
})
@ -71,7 +87,7 @@ class TrainingFixedCodeClient extends ObjectModel
SELECT `codeclient_from_results`, `codeclient_fixed`
FROM `'._DB_PREFIX_.'training_fixedcodeclient`
') as $row) {
$existing_codes[trim($row['codeclient_from_results'])] = trim($row['codeclient_fixed']);
$existing_codes[self::cleanupCode($row['codeclient_from_results'])] = self::cleanupCode($row['codeclient_fixed']);
}
// insert new wrong codes with their correction
@ -111,12 +127,15 @@ class TrainingFixedCodeClient extends ObjectModel
FROM `'._DB_PREFIX_.'training_fixedcodeclient`
WHERE TRIM(`codeclient_fixed`) <> \'\'';
foreach (Db::getInstance()->executeS($sql) as $row) {
$result[trim($row['codeclient_from_results'])] = trim($row['codeclient_fixed']);
$result[self::cleanupCode($row['codeclient_from_results'])] = self::cleanupCode($row['codeclient_fixed']);
}
return $result;
}
/**
* @param array $pharamacies_not_found assoc array key = code client, value = "1" (value is not used here)
*/
static function addIfNotExist(array $pharmacies_not_found)
{
// get already saved codes to correct
@ -125,24 +144,25 @@ class TrainingFixedCodeClient extends ObjectModel
$sql = 'SELECT LOWER(`codeclient_from_results`) as `codeclient_from_results`
FROM `'._DB_PREFIX_.'training_fixedcodeclient`';
foreach (Db::getInstance()->executeS($sql) as $row) {
$existing_codes[] = trim($row['codeclient_from_results']);
$existing_codes[] = self::cleanupCode($row['codeclient_from_results']);
}
// remove duplicate keys
// remove duplicate codes in the given "pharmacies not found" codes
$unique_pharmacies_not_found = array();
foreach ($pharmacies_not_found as $key => $val) {
$key = strtolower(trim($key));
if (!isset($unique_pharmacies_not_found[$key])) {
$unique_pharmacies_not_found[$key] = $val;
foreach ($pharmacies_not_found as $code_client => $val) {
$code_client = self::cleanupCode($code_client);
if (!isset($unique_pharmacies_not_found[$code_client])) {
$unique_pharmacies_not_found[$code_client] = $val;
}
}
// extract only "codes", we don't need values
$unique_pharmacies_not_found = array_keys($unique_pharmacies_not_found);
// filter new codes not already present in DB (and create the SQL value string)
$values = array();
foreach(array_diff($unique_pharmacies_not_found, $existing_codes) as $code_client) {
$values[] = '\''.pSql(trim((string)$code_client)).'\', \'\'';
$values[] = '\''.pSql($code_client).'\', \'\'';
}
// if any new codes, add them

View File

@ -34,7 +34,7 @@ class TrainingPharmacy extends ObjectModel
self::deleteAll();
})
->setOnEachLine(function(ArrayExtended $cols) use (&$values) {
$values[] = '\''.pSql((string)$cols[self::CODE_CLIENT]).'\'';
$values[] = '\''.pSql(TrainingFixedCodeClient::cleanupCode($cols[self::CODE_CLIENT])).'\'';
return true;
})
->setOnError(function($failed_line_number) {