Files
dolibarr/htdocs/telephonie/pdf/pdfdetail_nodet.modules.php
Rodolphe Quiedeville 670c18a13a Nouvelle presentation
2005-04-25 09:15:58 +00:00

442 lines
11 KiB
PHP

<?PHP
/* Copyright (C) 2005 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.
* or see http://www.gnu.org/
*
* $Id$
* $Source$
*
* Génère un PDF de la première page de résumé et un tableur des communications
*/
require_once DOL_DOCUMENT_ROOT."/facture.class.php";
require_once DOL_DOCUMENT_ROOT."/telephonie/facturetel.class.php";
require_once DOL_DOCUMENT_ROOT."/telephonie/pdf/pdfdetail_standard.modeles.php";
require_once DOL_DOCUMENT_ROOT."/telephonie/pdf/xlsdetail_nodet.modules.php";
class pdfdetail_nodet {
function pdfdetail_nodet ($db=0, $ligne, $year, $month, $factel)
{
$this->db = $db;
$this->description = "Modèle de facture détaillée sans les communications";
$this->ligne = $ligne;
$this->year = $year;
$this->month = $month;
$this->factel = $factel;
}
/*
*
*
*/
function write_pdf_file($factel, $ligne)
{
$fac = new Facture($this->db,"",$factel->fk_facture);
$fac->fetch($factel->fk_facture);
$fac->fetch_client();
$objlignetel = new LigneTel($this->db);
$result = $objlignetel->fetch($ligne);
if (defined("FAC_OUTPUTDIR"))
{
$dir = FAC_OUTPUTDIR . "/" . $fac->ref . "/" ;
$file = $dir . $fac->ref . "-$ligne-detail.pdf";
if (strlen($objlignetel->code_analytique) > 0)
{
$file = $dir . $fac->ref . "-$ligne-$objlignetel->code_analytique-detail.pdf";
}
if (! file_exists($dir))
{
umask(0);
if (! mkdir($dir, 0755))
{
$this->error="Erreur: Le répertoire '$dir' n'existe pas et Dolibarr n'a pu le créer.";
return 0;
}
}
if (file_exists($dir))
{
$this->pdf = new pdfdetail_standard_modeles('P','mm','A4');
$this->pdf->fac = $fac;
$this->pdf->factel = $this->factel;
$this->pdf->client_nom = $fac->client->nom;
$this->pdf->client_adresse = $fac->client->adresse;
$this->pdf->client_cp = $fac->client->cp;
$this->pdf->client_ville = $fac->client->ville;
$this->pdf->ligne = $ligne;
$this->pdf->year = $this->year;
$this->pdf->month = $this->month;
$this->pdf->ligne_ville = '';
if ($objlignetel->code_analytique)
{
$soca = new Societe($this->db);
$soca->fetch($objlignetel->client_id);
$this->pdf->ligne = $ligne . " (".$objlignetel->code_analytique.")";
$this->pdf->ligne_ville = $soca->ville;
}
$this->pdf->AliasNbPages();
$this->pdf->Open();
$this->pdf->SetTitle($fac->ref);
$this->pdf->SetSubject("Facture détaillée");
$this->pdf->SetCreator("Dolibarr");
$this->pdf->SetAuthor("");
$this->pdf->SetMargins(10, 10, 10);
$this->pdf->SetAutoPageBreak(1, 24);
$this->pdf->SetLineWidth(0.1);
$this->pdf->tab_top = 53;
$this->pdf->tab_height = 222;
/*
*
*
*/
$this->pdf->FirstPage = 1;
$this->pdf->AddPage();
$this->pdf->SetFillColor(230,230,230);
/*
* Détails des comm
*
*/
$this->pdf->SetFont('Arial','', 12);
$Y = $this->pdf->tab_top + 4;
$this->pdf->SetXY(10, $Y);
$this->pdf->MultiCell(100, 4, "Détails de vos communications", 0,'L',0);
$this->pdf->SetFont('Arial','', 9);
$Y = $this->pdf->GetY();
$this->pdf->SetXY(140, $Y);
$this->pdf->MultiCell(20, 4, "Durée", 0,'R',0);
$this->pdf->SetXY(160, $Y);
$this->pdf->MultiCell(20, 4, "Nb appels", 0,'R',0);
$this->pdf->SetXY(180, $Y);
$this->pdf->MultiCell(20, 4, "Coût", 0,'R',0);
$sql = "SELECT count(*) as cc, sum(t.cout_vente) as cout_vente, sum(t.duree) as duree, t.dest";
$sql .= " FROM ".MAIN_DB_PREFIX."telephonie_communications_details as t ";
$sql .= " WHERE t.fk_telephonie_facture =".$factel->id;
$sql .= " GROUP BY t.dest";
$sql .= " ORDER BY cout_vente DESC";
$sql .= " LIMIT 10";
$resql = $this->db->query($sql);
$total_duree = 0;
$total_nb = 0;
$total_cout = 0;
if ( $resql )
{
$num = $this->db->num_rows($resql);
$i = 0;
$this->pdf->SetFont('Arial','', 9);
$var = 0;
$line_height = 4;
$graph_values = array();
$graph_values_duree = array();
$graph_labels = array();
while ($i < $num)
{
$obj = $this->db->fetch_object($i);
$var=!$var;
$Y = $this->pdf->GetY();
$this->pdf->SetXY(10, $Y);
$this->pdf->MultiCell(100, $line_height, $obj->dest, 0,'L',$var);
if ($Y > $this->pdf->GetY())
$Y = $this->pdf->GetY() - $line_height;
$h = floor($obj->duree / 3600);
$m = floor(($obj->duree - ($h * 3600)) / 60);
$s = ($obj->duree - ( ($h * 3600 ) + ($m * 60) ) );
if ($h > 0)
{
$dt = $h . " h " . $m ." min " . $s ." sec" ;
}
else
{
if ($m > 0)
{
$dt = $m ." min " . $s ." sec" ;
}
else
{
$dt = $s ." sec" ;
}
}
$this->pdf->SetXY(110, $Y);
$this->pdf->MultiCell(50, $line_height,$dt, 0,'R',$var);
$this->pdf->SetXY(160, $Y);
$this->pdf->MultiCell(20, $line_height,$obj->cc, 0,'R',$var);
$this->pdf->SetXY(180, $Y);
$this->pdf->MultiCell(20, $line_height,sprintf("%01.3f",$obj->cout_vente), 0,'R',$var);
array_push($graph_values, $obj->cc);
array_push($graph_values_duree, $obj->duree);
array_push($graph_labels, $obj->dest);
$total_duree = $total_duree + $obj->duree;
$total_nb = $total_nb + $obj->cc;
$total_cout = $total_cout + $obj->cout_vente;
$i++;
}
}
else
{
dolibarr_syslog("Erreur SQl");
dolibarr_syslog($this->db->error());
}
$h = floor($total_duree / 3600);
$m = floor(($total_duree - ($h * 3600)) / 60);
$s = ($total_duree - ( ($h * 3600 ) + ($m * 60) ) );
if ($h > 0)
{
$dt = $h . " h " . $m ." min " . $s ." sec" ;
}
else
{
if ($m > 0)
{
$dt = $m ." min " . $s ." sec" ;
}
else
{
$dt = $s ." sec" ;
}
}
$var=!$var;
$this->pdf->SetXY(10, $Y + $line_height);
$this->pdf->MultiCell(100, $line_height,"Total : ", 0,'R',$var);
$this->pdf->SetXY(110, $Y + $line_height);
$this->pdf->MultiCell(50, $line_height,$dt, 0,'R',$var);
$this->pdf->SetXY(160, $Y + $line_height);
$this->pdf->MultiCell(20, $line_height,$total_nb, 0,'R',$var);
$this->pdf->SetXY(180, $Y + $line_height);
$this->pdf->MultiCell(20, $line_height,sprintf("%01.3f",$total_cout), 0,'R',$var);
/*
* Appels les plus important
*
*/
$this->pdf->SetFont('Arial','', 12);
$Y = $this->pdf->GetY() + 10;
$this->pdf->SetXY(10, $Y);
$this->pdf->MultiCell(100, 4, "TOP 10 des numéros appelés en coût", 0,'L',0);
$this->pdf->SetFont('Arial','', 9);
$Y = $this->pdf->GetY();
$this->pdf->SetXY(10, $Y);
$this->pdf->MultiCell(150, 4, "Destination", 0,'L',0);
$this->pdf->SetXY(140, $Y);
$this->pdf->MultiCell(20, 4, "Durée", 0,'R',0);
$this->pdf->SetXY(160, $Y);
$this->pdf->MultiCell(20, 4, "Nb appels", 0,'R',0);
$this->pdf->SetXY(180, $Y);
$this->pdf->MultiCell(20, 4, "Coût", 0,'R',0);
$sql = "SELECT count(*) as cc, sum(t.cout_vente) as cout_vente, sum(t.duree) as duree, t.numero, t.dest";
$sql .= " FROM ".MAIN_DB_PREFIX."telephonie_communications_details as t ";
$sql .= " WHERE fk_telephonie_facture =".$factel->id;
$sql .= " GROUP BY t.numero";
$sql .= " ORDER BY cout_vente DESC";
$sql .= " LIMIT 10";
if ( $this->db->query($sql) )
{
$num = $this->db->num_rows();
$this->pdf->SetFont('Arial','', 9);
$i = 0;
$var = 0;
$line_height = 4;
while ($i < $num)
{
$obj = $this->db->fetch_object($i);
$var=!$var;
$Y = $this->pdf->GetY();
$this->pdf->SetXY(10, $Y);
$this->pdf->MultiCell(80, $line_height, $obj->dest, 0,'L',$var);
if ($Y > $this->pdf->GetY())
$Y = $this->pdf->GetY() - $line_height;
$this->pdf->SetXY(90, $Y);
$this->pdf->MultiCell(30, $line_height, $obj->numero, 0,'L',$var);
$h = floor($obj->duree / 3600);
$m = floor(($obj->duree - ($h * 3600)) / 60);
$s = ($obj->duree - ( ($h * 3600 ) + ($m * 60) ) );
if ($h > 0)
{
$dt = $h . " h " . $m ." min " . $s ." sec" ;
}
else
{
if ($m > 0)
{
$dt = $m ." min " . $s ." sec" ;
}
else
{
$dt = $s ." sec" ;
}
}
$this->pdf->SetXY(120, $Y);
$this->pdf->MultiCell(40, $line_height,$dt, 0,'R',$var);
$this->pdf->SetXY(160, $Y);
$this->pdf->MultiCell(20, $line_height,$obj->cc, 0,'R',$var);
$this->pdf->SetXY(180, $Y);
$this->pdf->MultiCell(20, $line_height,sprintf("%01.3f",$obj->cout_vente), 0,'R',$var);
$i++;
}
}
/*
*
*
*/
include_once ("/usr/share/jpgraph/jpgraph.php");
include_once ("/usr/share/jpgraph/jpgraph_pie.php");
$graph = new PieGraph(450,200,"auto");
$graph->img->SetImgFormat("jpeg");
$graph->SetFrame(false);
// Setup title
$graph->title->Set("Répartition des destinations en nombre d'appel");
$graph->title->SetFont(FF_FONT1,FS_BOLD);
$p1 = new PiePlot($graph_values);
$p1->SetCenter(0.25,0.5);
// Label font and color setup
$p1->SetFont(FF_FONT1,FS_BOLD);
$p1->SetFontColor("darkred");
$p1->SetSize(0.3);
$p1->SetLegends($graph_labels);
$graph->legend->Pos(0.05,0.15);
$graph->Add($p1);
$file_graph = "/tmp/graph".$factel->ligne.".jpg";
$handle = $graph->Stroke($file_graph);
$this->pdf->Image($file_graph, 11, ($this->pdf->GetY() + 10), 0, 0, 'JPG');
/*
*
*/
$this->pdf->Close();
$this->pdf->Output($file);
$this->filename = $file;
dolibarr_syslog("Write $file");
if(file_exists($file_graph))
{
unlink($file_graph);
}
/* Génération du tableur */
$xlsdet = new xlsdetail_nodet($this->db);
$xlsdet->GenerateFile($objlignetel, $fac, $factel);
return 0;
}
else
{
$this->error="Erreur: Le répertoire '$dir' n'existe pas et Dolibarr n'a pu le créer.";
return -1;
}
}
else
{
$this->error="Erreur: FAC_OUTPUTDIR non défini !";
return -2;
}
}
}
?>