Nouveau fichier

This commit is contained in:
Rodolphe Quiedeville 2007-07-03 13:30:56 +00:00
parent 67aad3a3f5
commit fa2391a3f4
4 changed files with 644 additions and 0 deletions

View File

@ -0,0 +1,295 @@
<?PHP
/* Copyright (C) 2005-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*
* Script d'import des CDR
*/
require_once(DOL_DOCUMENT_ROOT."/telephonie/fournisseurtel.class.php");
class FacturationImportCdr {
function FacturationImportCdr($dbh)
{
$this->db = $dbh;
$this->messages = array();
$this->message_bad_file_format = array();
}
function CountDataImport()
{
$nb = 0;
$sql = "SELECT count(*)";
$sql .= " FROM ".MAIN_DB_PREFIX."telephonie_import_cdr";
if ($this->db->query($sql))
{
while ($row = $this->db->fetch_row($resql))
{
$nb = $row[0];
}
$this->db->free($resql);
}
return $nb;
}
function Import($id_fourn)
{
$dir = DOL_DATA_ROOT."/telephonie/cdr/atraiter/".$id_fourn."/";
/*
* Traitement
*/
$files = array();
if (is_dir($dir))
{
$handle=opendir($dir);
if ($handle)
{
$i = 0 ;
$var=True;
while (($xfile = readdir($handle))!==false)
{
if (is_file($dir.$xfile) && substr($xfile, -4) == ".csv")
{
$files[$i] = $dir.$xfile;
dolibarr_syslog("FacturationImportCdr ".$xfile." ajouté");
$i++;
}
else
{
dolibarr_syslog("FacturationImportCdr ".$xfile." ignoré");
}
}
closedir($handle);
}
else
{
dolibarr_syslog("FacturationImportCdr Impossible de lire $dir");
exit ;
}
}
else
{
dolibarr_syslog("FacturationImportCdr Impossible de lire $file");
exit ;
}
/*
* Vérification du fournisseur
*
*/
$fourn = new FournisseurTelephonie($this->db);
if ($fourn->fetch($id_fourn) <> 0)
{
dolibarr_syslog("Erreur recherche fournisseur");
}
/*
* Vérification des fichiers traités
*
*/
$fichiers = array();
$sql = "SELECT distinct(fichier_cdr)";
$sql .= " FROM ".MAIN_DB_PREFIX."telephonie_communications_details";
if ($this->db->query($sql))
{
while ($row = $this->db->fetch_row($resql))
{
array_push($fichiers, $row[0]);
}
$this->db->free($resql);
}
else
{
dolibarr_syslog("Erreur recherche si fichiers deja traites");
}
$sql = "SELECT distinct(fichier)";
$sql .= " FROM ".MAIN_DB_PREFIX."telephonie_import_cdr";
if ($this->db->query($sql))
{
while ($row = $this->db->fetch_row($resql))
{
array_push($fichiers, $row[0]);
}
$this->db->free($resql);
}
else
{
dolibarr_syslog("Erreur recherche fournisseur");
exit ;
}
/*
* Charge les ID de lignes
*
*/
$sql = "SELECT ligne, rowid ";
$sql .= " FROM ".MAIN_DB_PREFIX."telephonie_societe_ligne";
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
dolibarr_syslog ($num . " lignes chargées");
$i = 0;
$ligneids = array();
while ($i < $num)
{
$row = $this->db->fetch_row($resql);
$ligneids[$row[0]] = $row[1];
$i++;
}
}
else
{
dolibarr_syslog("Erreur chargement des lignes");
dolibarr_syslog($sql);
exit ;
}
foreach ($files as $xfile)
{
if (is_readable($xfile))
{
if ( $this->_verif($this->db, $xfile, $fichiers) == 0)
{
dolibarr_syslog("Lecture du fichier $xfile", LOG_DEBUG);
require_once(DOL_DOCUMENT_ROOT."/telephonie/fournisseur/cdrformat/cdrformat.messidor.class.php");
$cdrformat = new CdrFormatMessidor();
$cdrformat->ReadFile($xfile);
$error = 0;
$line = 0;
$line_inserted = 0;
$hf = fopen ($xfile, "r");
$line = 0;
if ($this->db->query("BEGIN"))
{
foreach ($cdrformat->datas as $data)
{
$ligne = $data['ligne'];
$duree_secondes = $data['duree'];
if ($ligneids[$ligne] > 0)
{
if ($duree_secondes > 0)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_import_cdr";
$sql .= "(idx,fk_ligne,ligne,date,heure,num,dest,dureetext,tarif,montant,duree";
$sql .= ", fichier, fk_fournisseur)";
$sql .= " VALUES (";
$sql .= $data['index'];
$sql .= ",'".$ligneids[$ligne]."'";
$sql .= ",'".$ligne."'";
$sql .= ",'".$data['date']."'";
$sql .= ",'".$data['heure']."'";
$sql .= ",'".$data['numero']."'";
$sql .= ",'".addslashes(ereg_replace('"','',$tarif))."'";
$sql .= ",'".ereg_replace('"','',$duree_text)."'";
$sql .= ",'".ereg_replace('"','',$tarif_fourn)."'";
$sql .= ",".ereg_replace(',','.',$data['montant']);
$sql .= ",".$duree_secondes;
$sql .= ",'".basename($xfile)."'";
$sql .= " ,".$id_fourn;
$sql .= ")";
if(ereg("^[0-9]+$", $duree_secondes))
{
if ($this->db->query($sql))
{
$line_inserted++;
}
else
{
dolibarr_syslog("Erreur de traitement de ligne $index");
dolibarr_syslog($this->db->error());
dolibarr_syslog($sql);
$error++;
}
}
else
{
print "Ligne : $cont ignorée\n";
$error++;
}
}
}
$line++;
}
dolibarr_syslog($line." lignes traitées dans le fichier");
dolibarr_syslog($line_inserted." insert effectués");
if (sizeof($this->message_bad_file_format))
{
foreach ($this->message_bad_file_format as $key => $value)
{
array_push($this->messages,"$value ligne(s) au mauvais format dans $key");
}
}
if ($error == 0)
{
$this->db->query("COMMIT");
array_push($this->messages, "Fichier ".basename($xfile)." importation reussie");
}
else
{
$this->db->query("ROLLBACK");
dolibarr_syslog("ROLLBACK");
}
}
}
}
else
{
print "Erreur lecture : $xfile";
dolibarr_syslog($xfile . " not readable");
array_push($this->messages, "Fichier ".basename($xfile)." not readable");
}
}
return $error;
}
function _verif($db, $file, $fichiers)
{
$result = 0;
/*
* Vérifie que le fichier n'a pas déjà été chargé
*
*/
if (in_array (basename($file), $fichiers))
{
dolibarr_syslog ("Fichier ".basename($file)." déjà chargé/traité");
array_push($this->messages, "Fichier ".basename($file)." déjà chargé/traité");
$result = -1;
}
return $result;
}
}

View File

@ -0,0 +1,132 @@
<?PHP
/* Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*
*/
require("./pre.inc.php");
$dir = $conf->telephonie->dir_output."/cdr/atraiter/" ;
$handle=opendir($dir);
$files = array();
$var=true;
while (($file = readdir($handle))!==false)
{
if (is_file($dir.'/'.$file))
array_push($files, $file);
}
closedir($handle);
if (!$user->rights->telephonie->facture->ecrire) accessforbidden();
$page = $_GET["page"];
$sortorder = $_GET["sortorder"];
$sortfield = $_GET["sortfield"];
llxHeader();
/*
* Sécurité accés client
*/
if ($user->societe_id > 0)
{
$action = '';
$socid = $user->societe_id;
}
if ($sortorder == "") {
$sortorder="";
}
if ($sortfield == "") {
$sortfield="date ASC";
}
/*
* Recherche
*
*
*/
if ($page == -1) { $page = 0 ; }
$offset = $conf->liste_limit * $page ;
$pageprev = $page - 1;
$pagenext = $page + 1;
/*
* Mode Liste
*
*/
print_barre_liste("CDR a traiter", $page, "cdr.php", "", $sortfield, $sortorder, '', $num);
print '<table class="noborder" width="100%" cellspacing="0" cellpadding="4">';
print '<tr class="liste_titre">';
print '<td>Ligne</td><td>Numero</td><td>Date</td><td align="right">Duree</td>';
print '<td align="right">Montant</td><td>Fichier</td>';
print "</tr>\n";
print '<tr class="liste_titre">';
print '<form action="cdr.php" method="GET">';
print '<td><input type="text" name="search_ligne" value="'. $_GET["search_ligne"].'" size="10"></td>';
print '<td><input type="text" name="search_num" value="'. $_GET["search_num"].'" size="10"></td>';
print '<td>&nbsp;</td>';
print '<td>&nbsp;</td>';
print '<td>&nbsp;</td>';
print '<td><input type="submit" class="button" value="'.$langs->trans("Search").'"></td>';
$var=True;
$sql = "SELECT ligne,date,heure,num, montant, duree,fichier";
$sql .= " FROM ".MAIN_DB_PREFIX."telephonie_import_cdr";
$sql .= " WHERE 1=1";
if ($_GET["search_ligne"])
{
$sel =urldecode($_GET["search_ligne"]);
$sel = ereg_replace("\.","",$sel);
$sel = ereg_replace(" ","",$sel);
$sql .= " AND ligne LIKE '%".$sel."%'";
}
$sql .= " ORDER BY $sortfield $sortorder " . $db->plimit($conf->liste_limit+1, $offset);
$resql = $db->query($sql);
while ($obj = $db->fetch_object($resql))
{
$var=!$var;
print "<tr $bc[$var]>";
print '<td>'.$obj->ligne."</td>\n";
print '<td>'.$obj->num."</td>\n";
print '<td>'.$obj->date." ".$obj->heure."</td>\n";
print '<td align="right">'.$obj->duree."</td>\n";
print '<td align="right">'.$obj->montant."</td>\n";
print '<td align="right">'.$obj->fichier."</td>\n";
}
print "</table>";
$db->close();
llxFooter("<em>Derni&egrave;re modification $Date$ r&eacute;vision $Revision$</em>");
?>

View File

@ -0,0 +1,66 @@
<?PHP
/* Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*
*/
require("./pre.inc.php");
require_once DOL_DOCUMENT_ROOT.'/telephonie/facturation/FacturationEmission.class.php';
if (!$user->rights->telephonie->facture->ecrire) accessforbidden();
llxHeader();
/*
* Sécurité accés client
*/
if ($user->societe_id > 0)
{
$action = '';
$socid = $user->societe_id;
}
/*
*
*
*
*/
print_barre_liste("Emission des factures", $page, "emission.php", "", $sortfield, $sortorder, '', $num);
print '<table class="noborder" width="100%" cellspacing="0" cellpadding="4">';
print '<tr class="liste_titre">';
print '<td>Fichier</td><td>Date</td>';
print "</tr>\n";
$var=True;
$obj = new FacturationEmission($db,$user);
$obj->Emission();
foreach ($obj->messages as $message)
{
$var=!$var;
print "<tr $bc[$var]>";
print '<td>'.$message.'</td></tr>';
}
print "</table>";
$db->close();
llxFooter("<em>Derni&egrave;re modification $Date$ r&eacute;vision $Revision$</em>");
?>

View File

@ -0,0 +1,151 @@
<?PHP
/* Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*
*/
require("./pre.inc.php");
require_once DOL_DOCUMENT_ROOT.'/telephonie/facturation/FacturationImportCdr.class.php';
require_once DOL_DOCUMENT_ROOT.'/telephonie/facturation/FacturationEmission.class.php';
require_once DOL_DOCUMENT_ROOT.'/telephonie/fournisseurtel.class.php';
if (!$user->rights->telephonie->facture->ecrire) accessforbidden();
if ( $_POST["sendit"] && $conf->upload != 0)
{
if ($_POST['fournisseur'] > 0)
{
$upload_dir = DOL_DATA_ROOT."/telephonie/cdr/atraiter/".$_POST['fournisseur'];
if (is_dir($upload_dir))
{
if (doliMoveFileUpload($_FILES['userfile']['tmp_name'], $upload_dir . "/" . $_FILES['userfile']['name']))
{
$mesg = "Le fichier est valide, et a &eacute;t&eacute; t&eacute;l&eacute;charg&eacute; avec succ&egrave;s.\n";
}
else
{
$mesg = "Le fichier n'a pas été téléchargé";
}
}
}
}
llxHeader('','Telephonie - Facturation');
/*
* Sécurité accés client
*/
if ($user->societe_id > 0)
{
$action = '';
$socid = $user->societe_id;
}
/*
*
*/
$fourn = new FournisseurTelephonie($db);
$fourns = $fourn->getActives();
clearstatcache();
print '<table class="noborder" width="100%" cellspacing="0" cellpadding="4">';
print '<tr><td width="50%" valign="top">';
$var = true;
print '<table class="noborder" width="100%" cellspacing="0" cellpadding="4">';
print '<tr class="liste_titre"><td colspan="3">Facturation</td></tr>';
foreach ($fourns as $key => $value)
{
$dir = $conf->telephonie->dir_output."/cdr/atraiter/$key" ;
if (is_dir($dir))
{
$handle=opendir($dir);
$files = array();
while (($file = readdir($handle))!==false)
{
if (is_file($dir.'/'.$file))
array_push($files, $file);
}
closedir($handle);
$nb = sizeof($files);
}
else
{
$nb = 'Rep Inexistant';
}
print "<tr $bc[$var]>";
print "<td>Fichiers a traiter pour $value</td>";
print '<td align="right"><a title="Nombre de fichiers a importer" href="files.php">'.$nb.'</a></td><td align="center">';
if (sizeof($files) > 0)
{
print '<a href="cdr-import.php?id='.$key.'">Importer</a>';
}
print '&nbsp;</td></tr>';
$var =!$var;
}
$obj = new FacturationImportCdr($db);
$nb = $obj->CountDataImport();
print "<tr $bc[$var]>";
print '<td>Donnees a traiter</td><td align="right"><a href="cdr.php">'.$nb.'</a></td>';
print '<td align="center"><a href="calcul.php">Traiter</a></td></tr>';
$obj = new FacturationEmission($db,$user);
$act = $obj->NbFactureToGenerate();
$nb = $obj->nbfac;
print "<tr $bc[1]>";
print '<td>Lignes de facture</td><td align="right"><a href="facture.php">'.$nb.'</a></td>';
print '<td align="center"><a href="emission.php">Emettre</a></td></tr>';
print '</table>';
print '</td><td valign="top" width="50%" rowspan="3">';
print '<form name="userfile" action="index.php" enctype="multipart/form-data" METHOD="POST">';
print '<input type="hidden" name="max_file_size" value="'.$conf->maxfilesize.'">';
print '<table class="noborder" width="100%" cellspacing="0" cellpadding="4">';
print '<tr class="liste_titre"><td>Charger un fichier CDR</td></tr>';
print "<tr $bc[1]><td>";
$form = new Form($db);
$form->select_array("fournisseur",$fourns);
print '<input type="file" name="userfile" size="40" maxlength="80"><br />';
print '<input type="submit" value="'.$langs->trans("Upload").'" name="sendit"> &nbsp; ';
print '<input type="submit" value="'.$langs->trans("Cancel").'" name="cancelit"><br>';
print "</tr>\n";
print '</table></form>';
print '</td></tr>';
print '</table>';
$db->close();
llxFooter("<em>Derni&egrave;re modification $Date$ r&eacute;vision $Revision$</em>");
?>