extranet/batch/greffeCmdMois.php

126 lines
3.5 KiB
PHP

#!/usr/bin/php -q
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Affiche l'aide.",
));
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(isset($opts->help))
{
echo "Liste les commandes non envoyées du mois dernier";
echo "\n\n";
echo $opts->getUsageMessage();
echo "\n";
echo $argv[0] . ' [AAAAMM] >> /vers/fichier.log';
echo "\n";
exit;
}
$args = $opts->getRemainingArgs();
/**
* Connexion à la base de données
*/
$dbConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/configuration.ini', 'databases');
$db = Zend_Db::factory($dbConfig->db->sdv1);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
require_once 'Scores/Mail.php';
function listCmdMois($statut, $date)
{
$commandes = new Application_Model_Commandes();
$sql = $commandes->select()
->where('typeCommande = ?', 'G')
->where('statutCommande = ?', $statut)
->where('dateCommande LIKE ?', $date.'%')
->where('dateReception = ?', '0000-00-00 00:00:00');
return $commandes->fetchAll($sql);
}
//=> Debut
if (count($args)>1){
echo "Erreur\n";
exit;
} elseif (count($args)==0){
$timeMoisPrecedent = mktime(0, 0, 0, date('m')-1, 1, date('Y'));
$moisPrecedent = date('Y-m', $timeMoisPrecedent);
} else {
$moisPrecedent = substr($args[0],0,4).'-'.substr($args[0],4,2);
}
$listeCmd = listCmdMois(0, $moisPrecedent);
$emailTxt = '';
//Liste commandes non-traites
$emailTxt.= '<b><u>Commandes greffe non receptionné</u></b>';
$emailTxt.= '<br/>';
if(count($listeCmd)>0){
$emailTxt.= '<table border="1" style="border:1px solid; margin:5px; border-collapse:collapse;">';
$emailTxt.= '<thead>';
$emailTxt.= '<tr>';
$emailTxt.= '<th>Ref.</th>';
$emailTxt.= '<th>Siren</th>';
$emailTxt.= '<th>Ref. Document</th>';
$emailTxt.= '<th>Date de commande</th>';
$emailTxt.= '</tr>';
$emailTxt.= '</thead>';
$emailTxt.= '<tbody>';
foreach($listeCmd as $cmd){
$emailTxt.= '<tr>';
$emailTxt.= '<td style="padding:5px">G'.$cmd->idCommande.'</td>';
$emailTxt.= '<td style="padding:5px">'.$cmd->siren.'</a></td>';
if( preg_match('/^([0-9]{4}_).*?$/', $cmd->refDocument, $matches) ){
$type = 'bilans';
}else{
$type = 'actes';
}
$emailTxt.= '<td style="padding:5px"><a href="/pieces/'.$type.'/siret/'.$cmd->siren.'">'.$cmd->refDocument.'</a></td>';
$emailTxt.= '<td style="padding:5px">'.$cmd->dateCommande.'</td>';
$emailTxt.= '</tr>';
}
$emailTxt.= '</tbody>';
$emailTxt.= '</table>';
}else{
$emailTxt.= "Aucune commande<br/>";
}
$emailTxt.= '<br/>';
//Envoi mail
$mail = new Mail();
$mail->setSubject("[Commandes greffe non receptionné] - ".date('d')."/".date('m')."/".date('Y'));
$mail->setFrom('production');
$mail->addTo('production@scores-decisions.com', 'Pieces');
$mail->addTo('supportdev@scores-decisions.com', 'SupportDev');
$mail->setBodyHtml($emailTxt);
$mail->send();