diff --git a/modules/training/models/CSVWriter.php b/modules/training/models/CSVWriter.php index d0e5ac7..7a6794a 100644 --- a/modules/training/models/CSVWriter.php +++ b/modules/training/models/CSVWriter.php @@ -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); + } + } diff --git a/modules/training/models/TrainingBilan.php b/modules/training/models/TrainingBilan.php index 6c7cc71..d1beef2 100644 --- a/modules/training/models/TrainingBilan.php +++ b/modules/training/models/TrainingBilan.php @@ -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)) { diff --git a/modules/training/models/TrainingFixedCodeClient.php b/modules/training/models/TrainingFixedCodeClient.php index 896986e..d51a1aa 100644 --- a/modules/training/models/TrainingFixedCodeClient.php +++ b/modules/training/models/TrainingFixedCodeClient.php @@ -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 diff --git a/modules/training/models/TrainingPharmacy.php b/modules/training/models/TrainingPharmacy.php index 476dc75..caa29f3 100644 --- a/modules/training/models/TrainingPharmacy.php +++ b/modules/training/models/TrainingPharmacy.php @@ -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) {