extranet/library/Finance/RatiosGraph.php
2011-09-30 15:16:01 +00:00

338 lines
11 KiB
PHP

<?php
class RatiosGraph
{
protected $path;
protected $siret = null;
protected $id = 0;
public function __construct($siret, $id = 0)
{
require_once 'Vendors/ChartDirector/phpchartdir.php';
require_once 'Scores/Cache.php';
$this->path = APPLICATION_PATH . '/../cache/pages/imgcache/';
$this->siret = $siret;
$this->id = $id;
}
/**
* Enregistre le graphique bilan passif sous forme d'image.
* @param array $data
* Tableau structuré des données
* @param string $filename
* Le nom de fichier généré
* @return string
* Retourne un message d'erreur ou le code HTML d'affichage.
*/
public function bilansgraphpassif($data, $typeBilan, $dateCloture)
{
$file = 'bilansgraphpassif-'.$this->siret.'-'.$this->id.'-'.$typeBilan.$dateCloture.'.png';
$cache = new Cache();
if( $cache->exist($this->path.$file) ){
$return = $file;
} else {
$w = 665;
$h = 210;
$x = round($w/2);
$y = round($h/2);
$radius = 90;
$c = new PieChart($w, $h);
$labels = array('Fonds propres',
'Provisions Risques',
'Compte Courant',
'Dettes Financières',
'Dettes Fournisseurs',
'Dettes fiscales',
'Autres Dettes',
'Trésorerie Passive');
$textBoxObj = $c->addTitle("Composition du passif", "timesbi.ttf", 15);
$c->setPieSize($x, $y, $radius);
$c->setLabelLayout(SideLayout);
$t = $c->setLabelStyle();
$t->setBackground(SameAsMainColor, Transparent, glassEffect());
$t->setRoundedCorners(5);
$c->setLineColor(SameAsMainColor, 0x000000);
$c->setStartAngle(135);
$c->setLabelFormat("<*block,valign=absmiddle*>{label} <*font=timesbi.ttf,size=0*>({percent|0}%)");
$c->setData($data, $labels);
$c->set3D(20);
if($c->makeChart($this->path.$file) === TRUE){
$return = '<img src="./fichier/imgcache/'.$file.'"/>';
}else{
$return = false;
}
}
return $return;
}
/**
* Enregistre le graphique bilan SIG sous forme d'image.
*/
public function bilansgraphsig($data, $typeBilan, $dateCloture)
{
$file = 'bilansgraphsig-'.$this->siret.'-'.$this->id.'-'.$typeBilan.$dateCloture.'.png';
$cache = new Cache();
if( $cache->exist($this->path.$file) ){
$return = $file;
} else {
$w = 665;
$h = 210;
$x = round($w/2);
$y = round($h/2);
$radius=70;
$c = new PieChart($w, $h);
$labels = array('Achats de marchandises.',
'Autres achats externes',
'Charges de fonctionnement',
'Amortissement et provisions',
'Perte financière',
'Impôts sociétés',
'RÉSULTAT NET',
);
$textBoxObj = $c->addTitle("Solde Intermédiaire de Gestion", "timesbi.ttf", 15);
$c->setPieSize($x, $y, $radius);
$c->setLabelLayout(SideLayout);
$t = $c->setLabelStyle();
$t->setBackground(SameAsMainColor, Transparent, glassEffect());
$t->setRoundedCorners(5);
$c->setLineColor(SameAsMainColor, 0x000000);
$c->setLabelFormat("<*block,valign=absmiddle*>{label} <*font=arial.ttf,size=0*>({percent|0}%)");
$c->setStartAngle(135);
$c->setData($data, $labels);
$c->set3D(20);
if($c->makeChart($this->path.$file) === TRUE){
$return = $file;
}else{
$return = false;
}
}
return $return;
}
/**
* Enregistre le graphique bilan actif sous forme d'image.
* @param array $data
* Tableau structuré des données
* @return mixed
*/
public function bilansgraphactif($data, $typeBilan, $dateCloture)
{
$file = 'bilansgraphactif-'.$this->siret.'-'.$this->id.'-'.$typeBilan.$dateCloture.'.png';
$cache = new Cache();
if( $cache->exist($this->path.$file) ){
$return = $file;
} else {
$w = 665;
$h = 210;
$x = round($w/2);
$y = round($h/2);
$radius = 90;
$c = new PieChart($w, $h);
$labels = array('Immo. incorporelles',
'Immo. corporelles',
'Immo. financières',
'Stock et encours',
'Créances Clients',
'Autres créances',
'Trésorerie Active');
$textBoxObj = $c->addTitle("Composition de l'actif", "timesbi.ttf", 15);
$c->setPieSize($x, $y, $radius);
$c->setLabelLayout(SideLayout);
$t = $c->setLabelStyle();
$t->setBackground(SameAsMainColor, Transparent, glassEffect());
$t->setRoundedCorners(5);
# Set the border color of the sector the same color as the fill color. Set the line # color of the join line to black (0x0)
$c->setLineColor(SameAsMainColor, 0x000000);
$c->setStartAngle(135);
$c->setLabelFormat("<*block,valign=absmiddle*>{label} <*font=timesbi.ttf,size=0*>({percent|0}%)");
$c->setData($data, $labels);
$c->set3D(20);
if($c->makeChart($this->path.$file) === TRUE){
$return = $file;
}else{
$return = false;
}
}
return $return;
}
public function ratiosgraph($ratio, $data)
{
$file = 'ratiosgraph-'.$this->siret.'-'.$this->id.'-'.$ratio.'.png';
$cache = new Cache();
if( $cache->exist($this->path.$file) ){
$return = $file;
}else{
if(count($data)<=1){
$return = 0;
}else{
$labelsX = array();
$labelsY = array();
$dataX1 = array();
$dataX2 = array();
foreach($data['data'] as $value){
$dataX1[] = $value['entreprise'];
$dataX2[] = $value['secteur'];
$labelsX[] = substr($value['date'],0,4);
}
$i=-100; while($i>100){
$labelsY[] = $i;
}
$c = new XYChart(350, 250);
$c->setPlotArea(55, 10, 280, 200);
$c->yAxis->setTitle($data['unite']);
$c->xAxis->setTitle("Années");
$c->xAxis->setWidth(2);
$c->yAxis->setWidth(2);
$legendObj = $c->addLegend(50, 10, false, "times.ttf", 9);
$legendObj->setBackground(Transparent);
$c->addLineLayer($dataX1, 0x0000ff, "Entreprise");
$c->addLineLayer($dataX2, 0x008C00, "Secteur");
$c->yAxis->setLabels($labelsY);
$c->yAxis->setLabelStep(10);
$c->xAxis->setLabels($labelsX);
if($c->makeChart($this->path.$file) === TRUE){
$return = $file;
}
else{ $return = FALSE;
}
}
}
return $return;
}
public function syntheseGraphEvol($data, $ratio, $unite)
{
$file = 'syntheseEvol-'.$this->siret.'-'.$this->id.'-'.$ratio.'.png';
$cache = new Cache();
if( $cache->exist($this->path.$file) ){
$return = $file;
} else {
$unite = !empty($unite) ? $unite : 'KEUROS';
$labelsX = array();
$labelsY = array();
$dataX = array();
if(count($data)<=1){
$return = 0;
} else {
foreach($data as $value){
$dataX[] = $value['value'];
$labelsX[] = substr($value['date'],0,4);
}
$i=-100;
while($i>100){
$labelsY[] = $i;
}
# Create a XYChart object of size 250 x 250 pixels
$c = new XYChart(400, 250);
$c->setPlotArea(65, 10, 280, 200);
$c->yAxis->setTitle($unite);
$c->xAxis->setTitle("Années");
$c->xAxis->setWidth(2);
$c->yAxis->setWidth(2);
$c->addLineLayer($dataX);
# Set the labels.
$c->yAxis->setLabels($labelsY);
$c->yAxis->setLabelStep(10);
$c->xAxis->setLabels($labelsX);
if($c->makeChart($this->path.$file) === TRUE){
$return = $file;
} else {
$return = false;
}
}
}
return $return;
}
public function syntheseGraphLineCompare($data, $typeBilan)
{
$file = 'synthese-linecompare-'.$this->siret.'-'.$this->id.'-'.$typeBilan.'.png';
$cache = new Cache();
if( $cache->exist($this->path.$file) ){
$return = $this->path.$file;
} else {
//Tri des données par date et par ordre croissant
foreach ($data as $key => $row) {
$date[$key] = $row['date'];
}
array_multisort($date, SORT_ASC, $data);
//Définition des valeurs pour le graph
$dataBFR = array();
$dataFR = array();
$dataCA = array();
$i=0;
//Parcourir les années
foreach($data as $item){
$anneeFin = substr($item['date'],0,4);
$moisFin = substr($item['date'],4,2);
$jourFin = substr($item['date'],6,2);
//Calcul de la date de début
$dateDebut = date("Ymd", mktime(0, 0, 0, $moisFin-$item['duree'], $jourFin, $anneeFin));
$anneeDebut = substr($dateDebut,0,4);
$moisDebut = substr($dateDebut,4,2);
$jourDebut = substr($dateDebut,6,2);
//Affectation des abscisses
$dataBFR['x'][$i] = $dataFR['x'][$i] = $dataCA['x'][$i] = $dataEBE['x'][$i] = chartTime((int)$anneeDebut, (int)$moisDebut, (int)$jourDebut);
$dataBFR['x'][$i+1] = $dataFR['x'][$i+1] = $dataCA['x'][$i+1] = $dataEBE['x'][$i+1] = chartTime((int)$anneeFin, (int)$moisFin, (int)$jourFin);
//Affectation des ordonnées
$dataBFR['y'][$i] = $dataBFR['y'][$i+1] = $item['r236'];
$dataFR['y'][$i] = $dataFR['y'][$i+1] = $item['r235'];
$dataCA['y'][$i] = $dataCA['y'][$i+1] = $item['r6'];
$dataEBE['y'][$i] = $dataEBE['y'][$i+1] = $item['r146'];
$i+=2;
}
$c = new XYChart(665, 350, 0xcccccc, 0x000000, 1);
$c->addTitle("Synthèse *", "timesbi.ttf", 15, 0x000000);
$c->addText(60, 320, "* Elements financier rapportés à 12 mois" );
$c->setPlotArea(60, 80, 500, 200, 0xffffff, -1, -1, 0xcccccc, 0xcccccc);
$c->yAxis->setTitle("KEUROS","timesbi.ttf",10);
$c->xAxis->setTitle( "<*block,valign=absmiddle*>Années<*/*>");
$c->xAxis->setWidth(2);
$c->yAxis->setWidth(2);
// Add a legend box at (55, 32) (top of the chart) with horizontal layout. Use 9 pts
// Arial Bold font. Set the background and border color to Transparent.
$legendObj = $c->addLegend(55, 30, false, "times.ttf", 9);
$legendObj->setBackground(Transparent);
// Add a blue (0000ff) step line layer to the chart and set the line width to 2 pixels
$layer1 = $c->addStepLineLayer($dataFR['y'], 0x0000ff, "FONDS DE ROULEMENT");
$layer1->setXData($dataFR['x']);
$layer1->setLineWidth(2);
// Add a red (ff0000) step line layer to the chart and set the line width to 2 pixels
$layer0 = $c->addStepLineLayer($dataBFR['y'], 0xff0000, "BESOIN EN FONDS DE ROULEMENT");
$layer0->setXData($dataBFR['x']);
$layer0->setLineWidth(2);
// Add a green (00ff00) step line layer to the chart and set the line width to 2 pixels
$layer2 = $c->addStepLineLayer($dataCA['y'], 0x00ff00, "CHIFFRE D'AFFAIRES");
$layer2->setXData($dataCA['x']);
$layer2->setLineWidth(2);
// Add a black (000000) step line layer style dash to the chart and set the line width to 2 pixels
$layer3 = $c->addStepLineLayer($dataEBE['y'], $c->dashLineColor(0x000000, DashLine), "EXCEDENT BRUT D'EXPLOITATION");
$layer3->setXData($dataEBE['x']);
$layer3->setLineWidth(2);
# If the FR line gets above the BFR line, color to area between the lines red (ff0000)
$c->addInterLineLayer($layer0->getLine(0), $layer1->getLine(0), 0xff0000, Transparent);
# If the FR line gets below the lower BFR line, color to area between the lines light green (8099ff99)
$c->addInterLineLayer($layer0->getLine(0), $layer1->getLine(0), Transparent, 0x8044ff44);
if($c->makeChart($this->path.$file) === TRUE){
$return = $this->path.$file;
}else{
$return = false;
}
}
return $return;
}
}