enrichissement/scripts/calculScoreDate.php
2016-11-25 18:56:03 +01:00

143 lines
4.6 KiB
PHP

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$dateStartIn = '2014-01-01';
$dateStopIn = '2015-10-01';
/*$companies = array(
'552144503',
'341762573',
);*/
$options = getopt("f:");
$file = basename($options['f'], '.csv');
$companies = array();
$row = 1;
if (($handle = fopen(__DIR__.'/'.$file.'.csv', "r")) !== false) {
while (($data = fgetcsv($handle, 0, ",")) !== false) {
$row++;
$companies[] = $data[0];
}
fclose($handle);
}
// --- Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(__DIR__ . '/../application'));
// --- Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// --- Composer autoload
require_once realpath(__DIR__ . '/../vendor/autoload.php');
// --- Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$c = new Zend_Config($application->getOptions());
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table::setDefaultAdapter($db);
$fp = fopen($file.'-'.date('YmdHis').'.csv', 'w');
$delimiter = ',';
$fileHeader = array(
'siren',
'dateCalc',
'DateCreaEn',
'Capital',
'SituationJuridique',
'Indiscore',
'Indiscore20',
'ScoreSolvabilite',
'ScoreDirigeance',
'ScoreConfor',
'encours',
);
fputcsv($fp, $fileHeader, $delimiter, '"');
$cpt = 1;
foreach ($companies as $item)
{
// --- Date de création de l'entreprise
$dateCreaEnSet = null;
$dateReal = new Zend_Date();
// DCREN => insee.identite
$sql = $db->select()->from('identite', array('DCREN'), 'insee')
->where('siren=?', $item)->order('SIEGE DESC')->order('ACTIF DESC')->limit(1);
$result = $db->fetchRow($sql, null, Zend_Db::FETCH_OBJ);
if ($result !== null) {
if ($dateReal->isDate($result->DCREN, 'yyyyMMdd')) {
$dateReal->set($result->DCREN, 'yyyyMMdd') ;
$dateCreaEnSet = 1;
}
}
// DateImma
if ($dateCreaEnSet === null) {
$sql = $db->select()->from('rncs_entrep', array('dateImma'), 'jo')
->where('siren=?', $item)->limit(1);
$result = $db->fetchRow($sql, null, Zend_Db::FETCH_OBJ);
if ($result !== null) {
if ($dateReal->isDate($result->dateImma, 'yyyy-MM-dd')) {
$dateReal->set($result->dateImma, 'yyyyMMdd') ;
$dateCreaEnSet = 1;
}
}
}
if ($dateCreaEnSet === null) {
continue;
}
// --- Date
$dateStop = new Zend_Date($dateStopIn, 'yyyy-MM-dd');
$dateStart = new Zend_Date($dateStartIn, 'yyyy-MM-dd');
echo date('Y-m-d H:i:s').' - '.$cpt.' - '.$item.' - '.$dateStart->toString('yyyy-MM-dd').' '.$dateStop->toString('yyyy-MM-dd')."\n";
$difference = $dateStop->sub($dateStart);
$measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND);
$measure->convertTo(Zend_Measure_Time::MONTH);
$nbMonth = $measure->getValue(0) + 1;
// --- Score
$dateCalcul = $dateStart;
for($i=0; $i<$nbMonth; $i++) {
$line = array();
$line['siren'] = $item;
$line['dateCalc'] = $dateCalcul->toString('yyyy-MM-dd');
echo date('Y-m-d H:i:s').' - '.$cpt."\t".$dateCalcul->toString('yyyy-MM-dd')."\n";
if ($dateCalcul->compare($dateReal) == -1) {
echo "Inconnue à cette date\n";
} else {
$score = new Metier_Scoring_Base();
$score->setEvenLimit($dateCalcul->toString('yyyyMMdd'));
$result = $score->calculIndiScore($item, '', false, 0);
$line['DateCreaEn'] = $result['DateCreaEn'];
$line['Capital'] = $result['Capital'];
$line['SituationJuridique'] = $result['SituationJuridique'];
$line['Indiscore'] = $result['Indiscore'];
$line['Indiscore20'] = $result['Indiscore20'];
$line['ScoreSolvabilite'] = $result['ScoreSolvabilite'];
$line['ScoreDirigeance'] = $result['ScoreDirigeance'];
$line['ScoreConfor'] = $result['ScoreConfor'];
$line['encours'] = $result['encours'];
}
// Ecrire ligne CSV
$row = array();
foreach ($fileHeader as $col) {
if (array_key_exists($col, $line)) {
$row[] = $line[$col];
} else {
$row[] = '';
}
}
fputcsv($fp, $row, $delimiter, '"');
// date +1
$dateCalcul->addMonth(1);
}
$cpt++;
}
fclose($fp);