<?php
/* OBTENIR LA REF DE LA PAGE */ 

/* DEFINIR FORMULAIRE DE FILTRE */
echo '<form action ="" method = "post">';
echo 'OF : <input type="text" name="filtreOF" size="12"><br>';
echo 'Nom du client : <input type="text" name="filtreclient" size="12"><br>';
echo 'Mois : <input type="test" name ="filtremois">';
echo 'Statut :<input type="checkbox" name ="filtrestatut">';
echo '<input type="submit" value="Filtre">';
echo '</form>';

if (isset($_POST['filtreOF'])) {
$filtre = " AND LCKTNUMERO like '%".$_POST['filtreOF']."%' ";
} else {
$filtre = '';
}

if (isset($_POST['filtreclient'])) {
$filtre = " AND CLCTNOM like '%".$_POST['filtreclient']."%' ";
} else {
$filtre = '';
}
if (isset($_POST['filtrestatut'])) {
$filtre = " AND EVCTETAT = 'S' ";
} else {
$filtre = " AND EVCTETAT = ' ' ";
}
if (isset($_POST['filtremois']) & $_POST['filtremois'] != '') {
$filtre = " AND LCCJCRE >= 2014".$_POST['filtremois']."01 AND LCCJCRE <= 2014".$_POST['filtremois']."31 " ;
} else { 
$filtre = " AND LCCJCRE >= 20140901 AND LCCJCRE < 20140902 "; /* definir une date par défaut */
}
var_dump($filtre);
$datepmi = date('Ymd');
/* CONNECTION A PMI */
$pmi_conn = pmi_connect ();

/* DEFINIR LA QUERY */
$query = "SELECT LCKTSOC, CLCTNOM, LCKTNUMERO, LCKTLIGNE, LCKTPSF, LCCTCODART, LCCTCOMART, LCCNQTECDE , LCCNPUNET, LCCTTYPART, LCCJCRE, ECCTDERFA, ECCJDERFA, EVKNID, EVCTLIBRE1, EVCTLIBRE2, EVCTETAT 
FROM LOFFCLI 
LEFT JOIN EOFFCLI ON LOFFCLI.LCKTSOC = EOFFCLI.ECKTSOC AND LOFFCLI.LCKTNUMERO = EOFFCLI.ECKTNUMERO AND LOFFCLI.LCKTPSF = EOFFCLI.ECKTINDICE
LEFT JOIN EVENEMEN ON EVKTSOC = LCKTSOC AND EVCTNUMERO = LCKTNUMERO AND EVCTINDICE = LCKTPSF
LEFT JOIN CLIENT ON LCKTSOC = CLKTSOC AND LCCTCODE = CLKTCODE
WHERE LCKTSOC = '100' AND EVENEMEN.EVCTTYPDOC = 'O' AND EVENEMEN.EVCTTYPCPT = ' 'AND EVCTETAT = ' ' 
".$filtre." ORDER BY LOFFCLI.LCKTNUMERO DESC";

$stmt = sqlsrv_query( $pmi_conn, $query , array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
   $row_count = sqlsrv_num_rows( $stmt );
   if ($row_count === false)
      echo "\nerror\n";
   else if ($row_count >=0)
      echo $row_count." Lignes d'offre en cours" ;
echo '<table width = "100%">';
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo '<tr><td>'.$row['CLCTNOM'].'</td>';
echo '<td>'.$row['LCKTNUMERO'].'</td>';
echo '<td>'.$row['LCKTLIGNE'].'</td>';
if (preg_replace('/\s+/', '', $row['LCCTCOMART']) <> '') {
$dir = '//SERVEUR-APPLI/bureautique/100/PLANS/'.preg_replace('/\s+/', '', $row['LCCTCODART']).'-'.preg_replace('/\s+/', '', $row['LCCTCOMART']);
$path = 'file://serveur-appli/bureautique/100/PLANS/'.preg_replace('/\s+/', '', $row['LCCTCODART']).'-'.preg_replace('/\s+/', '', $row['LCCTCOMART']).'/';
}else{
$dir = '//SERVEUR-APPLI/bureautique/100/PLANS/'.preg_replace('/\s+/', '', $row['LCCTCODART']);
$path = 'file://serveur-appli/bureautique/100/PLANS/'.preg_replace('/\s+/', '', $row['LCCTCODART']);
}
$isFolder = is_dir($dir);
//echo($isFolder);
if ($isFolder = 1) {
$files = scanDir::scan($dir, "pdf");
//print_r($files);
echo '<td><a href="file:'.$files[0].'" target="_blank" >'.$row['LCCTCODART'].'</a></td>';
}else{
echo '<td>'.$row['LCCTCODART'].'</td>';
}
echo '<td>'.$row['LCCTCOMART'].'</td>';
//echo '<td>'.$row['LCCTLIB01'].'</td>';
//echo '<td>'.$row['LCCTLIB02'].'</td>';
echo '<td>'.$row['LCCJCRE'].'</td>';
echo '<td>'.$row['EVCTETAT'].'</td>';
/* MISE A JOUR DU CHAMP LIBRE */
echo '<td>';
if (isset($_POST['STATUT'])) {
$maj = maj_crm_libre($_POST['LCKTSOC'],$_POST['EVKNID'],$_POST['STATUT']);
}
$EVCTLIBRE1 = rtrim($row['EVCTLIBRE1']);
echo '<form action="" method = "post">';
echo '<input type="hidden" name="LCKTSOC" value="'.$row['LCKTSOC'].'">';
echo '<input type="hidden" name="EVKNID" value="'.$row['EVKNID'].'">';
echo 'Statut : C<input type="checkbox" name="STATUT" value="Commande" '.((strcmp($EVCTLIBRE1,"Commande")==0)?'checked':'unchecked').' >';
echo 'P<input type="checkbox" name="STATUT" value="Pas place" '.((strcmp($EVCTLIBRE1,"Pas place")==0)?'checked':'unchecked').' >';
echo 'D<input type="checkbox" name="STATUT" value="Delai" '.((strcmp($EVCTLIBRE1,"Delai")==0)?'checked':'unchecked').' >';
echo 'R<input type="checkbox" name="STATUT" value="Report" '.((strcmp($EVCTLIBRE1,"Report")==0)?'checked':'unchecked').' >';
echo 'O<input type="checkbox" name="STATUT" value="Obsolete" '.((strcmp($EVCTLIBRE1,"Obsolete")==0)?'checked':'unchecked').' >';
echo '<input type="submit" value="MAJ">';
//echo 'maj ='.$maj;
echo '</form></td>';
echo '<td>'.$row['ECCTDERFA'].'</td>';
echo '</tr>';
}
echo '</table>';
echo 'Server date and time is: ';
echo date('l, F j, Y \a\t G:i:s');
//echo date('Ymd');
echo "<br />";

$pmi_conn.close;
?>
