"Displays usage information.", 'cron' => "Mandatory option for launch the cli in cron", 'regen=s' => "Specify the file name to execute or regenerate output file", 'manual=s' => "Provide manualy the file path to get result and debug values", 'resume=s' => "Use with --regen and --manual to resume the file", 'debug' => "Send a mail for debug", ) ); $opts->parse(); } catch (Zend_Console_Getopt_Exception $e) { echo $e->getUsageMessage(); exit; } //Usage if( isset($opts->help) || count($opts->getOptions())==0 ) { echo "Place files in right directory for sending to the customer.\n"; echo $opts->getUsageMessage(); exit; } require_once APPLICATION_PATH.'/configs/config.php'; require_once 'framework/fwk.php'; require_once 'framework/common/chiffres.php'; require_once 'framework/common/dates.php'; require_once 'framework/mail/sendMail.php'; require_once 'framework/common/mysql.php'; require_once 'Metier/insee/classMInsee.php'; require_once 'Metier/scores/classMSolvabilite.php'; $c = new Zend_Config($application->getOptions()); $db = Zend_Db::factory($c->profil->db->metier); Zend_Db_Table_Abstract::setDefaultAdapter($db); //Input path $pathIn = $c->profil->path->storage . DIRECTORY_SEPARATOR . 'clients' . DIRECTORY_SEPARATOR . strtolower($client) . DIRECTORY_SEPARATOR . 'send'; //Output path $pathOut = $c->profil->path->storage . DIRECTORY_SEPARATOR . 'clients' . DIRECTORY_SEPARATOR . strtolower($client) . DIRECTORY_SEPARATOR . 'recv'; //Configuration $fileOptionsIn = array( 'dir' => $pathIn, 'type' => 'csv', 'delimiter' => ';', 'enclosure' => '"', 'header' => array('idDemande', 'SSAEmetteur'), 'columns' => array('siren', 'nbLignes', 'dateContrat', 'nbContrats', 'ir'), ); $fileOkOptionsOut = array( 'dir' => $pathOut, 'type' => 'csv', 'delimiter' => ';', 'enclosure' => '"', 'header' => array('idDemande', 'SSAEmetteur'), 'columns' => array('siren', 'idVOR', 'PO', 'commentaire'), 'name' => 'FICH_RCE_RETOUR_', ); $fileErrorOptionsOut = array( 'dir' => $pathOut, 'type' => 'csv', 'delimiter' => ';', 'enclosure' => '"', 'header' => array('idDemande', 'SSAEmetteur'), 'columns' => array('siren', 'code'), 'name' => 'FICH_RCE_ERREUR_', ); $client = 'SFR'; $typeDepot = 'SFTP'; $prestation = 'FICH_RCE'; //Get the file to resume //@todo : Define the real use header return by the internal function to not guess the header if ( $opts->resume ) { if ( file_exists($pathOut . DIRECTORY_SEPARATOR . $opts->resume) ) { if (($handle = fopen($pathOut . DIRECTORY_SEPARATOR . $opts->resume, 'r')) !== false) { $row = 0; while (($data = fgetcsv($handle, 0, $fileOptionsIn['delimiter'], $fileOptionsIn['enclosure'])) !== false) { $row++; $fileOkOptionsOut['line'][] = $data; } } fclose($handle); } else { echo "Fichier introuvable !" . PHP_EOL; exit; } $resumeAt = $row; } // => Debug Mode - Create file with debug values if ( $opts->manual ) { $filepath = $opts->manual; $pathOut = dirname(realpath($filepath)); $fileOptionsIn['dir'] = $pathOut; $fileErrorOptionsOut['dir'] = $pathOut; $fileOkOptionsOut['dir'] = $pathOut; $dateBegin = date('YmdHis'); $filesOutOptions = array( 'ok' => $fileOkOptionsOut, 'error' => $fileErrorOptionsOut ); foreach ($filesOutOptions as $code => $file) { $filesOutOptions[$code]['name'] = $file['name'] . $dateBegin . '.csv'; } //Read file $result = execFileIn($filepath, $fileOptionsIn, $filesOutOptions, true); } // => Automatic mode if ($opts->cron || $opts->regen ) { if ( $opts->cron ) { //Lire les fichiers à traiter dans la table $c = new Zend_Config($application->getOptions()); $db = Zend_Db::factory($c->profil->metier); Zend_Db_Table::setDefaultAdapter($db); //client, name, typeDepot, dateDepot, fileDepot, nblignes, dateInsert, dateExecute $fluxinM = new Application_Model_Sdv1FluxFileIn(); $sql = $fluxinM->select() ->where('client=?',$client) ->where('name=?', $prestation) ->where('typeDepot=?',$typeDepot) ->where('dateExecute=?','0000-00-00 00:00:00') ->order('dateInsert DESC'); $pitems = $fluxinM->fetchAll($sql); if ( $pitems->count()==0 ) { //@todo : Send a mail exit; } foreach ($pitems as $p) { $filepath = $pathIn . DIRECTORY_SEPARATOR . $p->fileDepot; break; } } if ($opts->regen) { $filepath = $pathIn . DIRECTORY_SEPARATOR . $opts->regen; } if ( !file_exists($filepath) ) { echo "Fichier inexistant ! ".PHP_EOL; exit; } $dateBegin = date('YmdHis'); $filesOutOptions = array( 'ok' => $fileOkOptionsOut, 'error' => $fileErrorOptionsOut ); foreach ($filesOutOptions as $code => $file) { $filesOutOptions[$code]['name'] = $file['name'] . $dateBegin . '.csv'; } //Execute file $result = execFileIn($filepath, $fileOptionsIn, $filesOutOptions, false); $fluxoutM = new Application_Model_Sdv1FluxFileOut(); $rows = 0; foreach($result as $code => $file) { $rows = count($file[$code]['line']); $dateEnd = date('YmdHis'); if ( $opts->cron ) { $fluxoutM->insert(array( 'client' => $client, 'name' => $prestation, 'nbLines' => $rows, 'dateBegin' => $dateBegin, 'dateEnd' => $dateEnd, 'fileOut' => $file[$code]['name'], 'depotType' => $typeDepot, 'depotDate' => '0000-00-00 00:00:00', )); } } if ( $opts->cron ) { //Ecrire la date d'execution dans flux_filein $fluxinM->update(array( 'dateExecute' => $dateEnd, ), 'id='.$p->id); } } /** * * @param array $values * @param string $seq * @param string $debug * @return array */ function execInternal($values, $seq, $debug = false) { $error = 0; $siren = $values['siren']; echo "$siren"; $classInsee = new MInsee(); $values['code'] = ''; //Siren valide - 1010 if ( !$classInsee->valideSiren($siren) ) { $error = 1; $code = 'error'; $values['code'] = '1010'; echo " - Erreur 1010."; } //Siren existant - 1020 else if ( !$classInsee->sirenExiste($siren) ) { $error = 1; $code = 'error'; $values['code'] = '1020'; echo " - Erreur 1020."; } //Date incorrecte - 2010 Zend_Date::setOptions(array('extend_month' => true)); if ( $values['dateContrat']!='' && Zend_Date::isDate($values['dateContrat'], array('date_format' => 'yyyyMMdd')) ) { $error = 1; $code = 'error'; $values['code'] = '2010'; echo " - Erreur 2010 "; } if ( $error == 0 ) { //Calculate data require_once 'Metier/Sfr/Sfr.php'; $sfrM = new Metier_Sfr(); //Set Values $sfrM->setVal('ContratDate', $values['dateContrat']); $sfrM->setVal('IR', $values['ir']); //Evaluate $sfrM->evaluate($siren); $values['idVOR'] = $sfrM->getIndicateur(); $values['commentaire'] = $sfrM->getComment(); $values['PO'] = $sfrM->getPo(); $valuesDebug = $sfrM->getValDebug(); if ($debug) { $values = array_merge($values, $valuesDebug); } $code = 'ok'; echo " - VOR=".$values['idVOR']." , commentaire=".$values['commentaire']." , PO=".$values['PO']; } echo PHP_EOL; if ( !$debug ) { $sfrDataM = new Application_Model_JoSfrData(); $sfrDataM->insert(array( 'NumSeq' => $seq, 'siren' => $siren, 'NbLignes' => $values['nbLignes'], 'NbContrats' => $values[''], 'DateContrat' => $values['dateContrat'], 'IR' => $values['ir'], 'IdVOR' => $values['idVOR'], 'comment' => $values['comment'], 'PO' => $values['PO'], 'debug' => json_encode($valuesDebug), 'error' => $values['code'], )); } return array( 'values' => $values, 'code' => $code, ); } /** * * @param string $file * @param array $fileOptionsIn * @param array $filesOptionsOut * @param boolean $debug * @param boolean $resume * @return array */ function execFileIn($file, $fileOptionsIn, $filesOptionsOut = array(), $debug = false, $resume = false) { //Set the input file path $filepathIn = $fileOptionsIn['dir'] . DIRECTORY_SEPARATOR . $file; //Open output file path foreach ($filesOptionsOut as $code => $file) { ${'fp'.$code} = fopen($file['dir'] . DIRECTORY_SEPARATOR . $file['name'], 'w'); } //Read file if ( file_exists($filepathIn) ) { if (($handle = fopen($filepathIn, 'r')) !== false) { $row = 0; while (($data = fgetcsv($handle, 0, $fileOptionsIn['delimiter'], $fileOptionsIn['enclosure'])) !== false) { $row++; echo "Ligne $row : "; //Header if (count($fileOptionsIn['header']) > 0 && $row == 1) { foreach ( $fileOptionsIn['header'] as $i => $column ) { $header[$column] = $data[$i]; } echo "Detection HEADER.".PHP_EOL; if ($debug) { $filesOptionsOut['error']['header'] = array(); $filesOptionsOut['ok']['header'] = array(); } else { $filesOptionsOut['error']['header'] = $header; $filesOptionsOut['ok']['header'] = $header; } if ( $resume ) { fputcsv(${'fp'.$codeRetour}, $filesOptionsOut['ok']['line'][0], $filesOptionsOut['ok']['delimiter'], $filesOptionsOut['ok']['enclosure']); } } //Content else { //Set values from file $values = array(); foreach ( $fileOptionsIn['columns'] as $i => $column ) { $values[$column] = $data[$i]; } $isResume = false; if ( $resume ) { foreach ( $filesOptionsOut['ok']['line'] as $line ) { if ( $line[0] == $data[0] ) { fputcsv(${'fp'.$codeRetour}, $line, $filesOptionsOut['ok']['delimiter'], $filesOptionsOut['ok']['enclosure']); $isResume = true; break; } } } if ( !$isResume ) { //Exec the process $valuesClient = execInternal($values, $header['idDemande'], true); //Format output $codeRetour = $valuesClient['code']; //$filesOptionsOut[$codeRetour]['line'][] = $valuesClient['values']; if ($debug) { $filesOptionsOut[$codeRetour]['columns'] = array_keys($valuesClient['values']); } if ($row == 2) { foreach ($filesOptionsOut as $code => $file) { $line = array(); //Dynamic Header if ( count($file['header'])>0 ) { foreach ( $file['header'] as $i => $column ) { $line[] = $column; } } //Static Header else { foreach ( $file['columns'] as $i => $column ) { $line[] = $column; } } fputcsv(${'fp'.$code}, $line, $filesOptionsOut[$code]['delimiter'], $filesOptionsOut[$code]['enclosure']); } } //Write the line following the return code $line = array(); foreach ( $filesOptionsOut[$codeRetour]['columns'] as $i => $column ) { $line[] = $valuesClient['values'][$column]; } fputcsv(${'fp'.$codeRetour}, $line, $filesOptionsOut[$codeRetour]['delimiter'], $filesOptionsOut[$codeRetour]['enclosure']); } } } } fclose($handle); foreach ($filesOptionsOut as $code => $file) { fclose(${'fp'.$code}); } return $output; } } /** * * @param string $file * @param array $options */ function execFileOut($file, $options) { $fp = fopen($file, 'w'); //=> Header $line = array(); //Dynamic Header if ( count($options['header'])>0 ) { foreach ( $options['header'] as $i => $column ) { $line[] = $column; } } //Static Header else { foreach ( $options['columns'] as $i => $column ) { $line[] = $column; } } fputcsv($fp, $line, $options['delimiter'], $options['enclosure']); //=> Content if ( count($options['line'])>0 ) { foreach ($options['line'] as $c ) { $line = array(); foreach ( $options['columns'] as $i => $column ) { $line[] = $c[$column]; } fputcsv($fp, $line, $options['delimiter'], $options['enclosure']); } } fclose($fp); return count($options['line']); }