0byt3m1n1
Path:
/
home
/
kassiope
/
www
/
administrer
/
[
Home
]
File: stats_export.php
<?php include("../configuration.inc.php"); necessite_identification(); necessite_priv("admin"); /* ini_set('display_errors',1); error_reporting(E_ALL); */ header("Content-type: text/csv"); header("Content-disposition: attachment; filename=\"".$_GET['type']."-".date('Y-m-d').".csv\""); if(isset($_GET['year'])) $year = $_GET['year']; else $year = date('Y'); if(isset($_GET['priv'])) $priv = $_GET['priv']; else $priv = ''; if(isset($_GET['user'])) $user = $_GET['user']; else $user = ''; if(isset($_GET['code'])) $code = $_GET['code']; else $code = ''; if(isset($_GET['taille'])) $taille = $_GET['taille']; else $taille = ''; if(!empty($code)) $c = " and c.code_promo = '".$code."' "; else $c = ''; $sql_priv = " WHERE "; if(!empty($user)) $sql_priv = " , gaia_utilisateurs u where c.id_utilisateur = u.id_utilisateur and u.priv = '".$priv."' and u.id_utilisateur = '".$user."' ".$c." and "; elseif(!empty($priv)) $sql_priv = " , gaia_utilisateurs u where c.id_utilisateur = u.id_utilisateur and u.priv = '".$priv."' and "; if(!empty($code)) $sql_priv .= " c.code_promo = '".$code."' and "; if(!empty($taille)) $sql_priv .= " ca.taille = '".$taille."' and "; //################################################################# //################################################################# // STATISTIQUES GLOBALES if($_GET['type']=='global') { $url = "select sum(c.montant_ht - c.cout_transport_ht) / count(ca.id) as tot_ht, sum(c.montant - c.cout_transport) / count(ca.id) as tot_ttc, sum(ca.marge) - sum(remise_produit_ht + remise_client_ht + remise_code_ht + remise_cheque_ht + remise_bon_ht) / count(ca.id) as marge_ht, sum(ca.quantite) as volume FROM gaia_commandes c, gaia_commandes_articles ca, gaia_produits_categories pc ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = pc.produit_id and (statut = '3' or statut = '5' or statut = '8') and o_timestamp like "; //echo $url. "2014-07%"; echo return_stat($url, '', 0, 1); } //################################################################# //################################################################# // STATISTIQUES GLOBALES if($_GET['type']=='globald') { $url = " select sum(c.montant_ht - c.cout_transport_ht) / count(ca.id) as tot_ht, sum(c.montant - c.cout_transport) / count(ca.id) as tot_ttc, sum(ca.marge) - sum(remise_produit_ht + remise_client_ht + remise_code_ht + remise_cheque_ht + remise_bon_ht) / count(ca.id) as marge_ht, sum(ca.quantite) as volume from gaia_commandes c, gaia_commandes_articles ca, gaia_produits_categories pc ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = pc.produit_id and (statut = '3' or statut = '5' or statut = '8') and o_timestamp between "; echo 'C.A.;Marge;Volume;C.A. (n-1);Marge (n-1);Volume (n-1);Progression'."\n"; echo return_stat2($url,'', 0, 1); } //################################################################# //################################################################# //################################################################# // STATISTIQUES GAMMES if($_GET['type']=='gamme') { $recc = mysql_query_override("select * from gaia_categories where parent_id = 0 order by position, nom_fr"); while ($allcat = mysql_fetch_array_override($recc)) { echo recup_cat_stat($allcat['id']); $recsous = mysql_query_override("select c.* from gaia_categories c, gaia_produits_categories pc where pc.categorie_id = c.id and c.parent_id = '".$allcat['id']."' group by c.id order by c.position, c.nom_fr"); if(mysql_num_rows_override($recsous)>0) { while ($allcatsous = mysql_fetch_array_override($recsous)) { echo recup_cat_stat($allcatsous['id'], 1); } } } } //################################################################# //################################################################# //################################################################# // STATISTIQUES GAMMES if($_GET['type']=='gammed') { echo ';C.A.;Marge;Volume;C.A. (n-1);Marge (n-1);Volume (n-1);Progression'."\n"; $recc = mysql_query_override("select * from gaia_categories where parent_id = 0 order by position, nom_fr"); while ($allcat = mysql_fetch_array_override($recc)) { echo recup_cat_stat2($allcat['id'], 2); echo "\n"; $recsous = mysql_query_override("select c.* from gaia_categories c, gaia_produits_categories pc where pc.categorie_id = c.id and c.parent_id = '".$allcat['id']."' group by c.id order by c.position, c.nom_fr"); if(mysql_num_rows_override($recsous)>0) { while ($allcatsous = mysql_fetch_array_override($recsous)) { echo recup_cat_stat2($allcatsous['id'], 1); echo "\n"; } } } } //################################################################# //################################################################# //################################################################# // STATISTIQUES PAR PRODUITS if($_GET['type']=='produit') { $recc = mysql_query_override("select sum(ca.prix), ca.produit_id, ca.nom_produit as nom_fr from gaia_commandes_articles ca, gaia_commandes c ".$sql_priv." ca.commande_id = c.id AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp like '".$year."%' group by ca.produit_id order by sum(ca.prix) desc"); //$recc = mysql_query_override("select * from gaia_produits"); while ($allcat = mysql_fetch_array_override($recc)) { $totca = $totcaold = 0; $entete = $encaisse = $ca = $caold = $progress = ''; //$url = " select sum(ca.prix_ht) as tot_ht, sum(ca.prix) as tot_ttc, sum(ca.marge)/1.2 as marge_ht, sum(ca.quantite) as volume from gaia_commandes c, gaia_commandes_articles ca ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = ".$allcat['produit_id']." AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp like "; $url = " select sum(ca.total_prix_ht) as tot_ht, sum(ca.total_prix) as tot_ttc, sum(ca.marge) as marge_ht, sum(ca.quantite) as volume from gaia_commandes c, gaia_commandes_articles ca ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = ".$allcat['produit_id']." AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp like "; echo return_stat($url, $allcat['nom_fr'], 0); } } //################################################################# //################################################################# //################################################################# // STATISTIQUES PAR PRODUITS if($_GET['type']=='produitd') { echo ';C.A.;Marge;Volume;C.A. (n-1);Marge (n-1);Volume (n-1);Progression'."\n"; $recc = mysql_query_override("select sum(ca.prix), p.* from gaia_produits p, gaia_commandes_articles ca, gaia_commandes c ".$sql_priv." p.id = ca.produit_id and ca.commande_id = c.id AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp like '".$year."%' group by p.id order by sum(ca.prix) desc"); while ($allcat = mysql_fetch_array_override($recc)) { $totca = $totcaold = 0; $entete = $encaisse = $ca = $caold = $progress = ''; $url = " select sum(ca.total_prix_ht) as tot_ht, sum(ca.marge) as marge_ht, sum(ca.quantite) as volume from gaia_commandes c, gaia_commandes_articles ca ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = ".$allcat['id']." AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp between "; echo return_stat2($url, $allcat['nom_fr'], 1); echo "\n"; } //################################################################# } //################################################################# //################################################################# // FUNCTIONS function recup_cat_stat($id, $t=0) { global $sql_priv, $year; $totca = $totmarge = $totcaold = $totmargeold = 0; $entete = $marge = $totalmarge = $encaisse = $ca = $caold = $margeold = $progress = ''; $cat = mysql_query_override("select * from gaia_categories where id = '".$id."'"); $allcat = mysql_fetch_array_override($cat); $url = " select sum(ca.total_prix_ht) as tot_ht, sum(ca.total_prix) as tot_ttc, sum(ca.marge) as marge_ht, sum(ca.quantite) as volume from gaia_commandes c, gaia_commandes_articles ca, gaia_produits_categories pc ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = pc.produit_id AND pc.categorie_id in (select distinct(id) from gaia_categories where parent_id = '".$id."') AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp like "; if ($t>0) $url = " select sum(ca.total_prix_ht) as tot_ht, sum(ca.total_prix) as tot_ttc, sum(ca.marge) as marge_ht, sum(ca.quantite) as volume from gaia_commandes c, gaia_commandes_articles ca, gaia_produits_categories pc ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = pc.produit_id AND pc.categorie_id = '".$id."' AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp like "; return return_stat($url, $allcat['nom_fr'], $t); } function return_stat($url, $nom, $t, $global=0) { global $sql_priv, $year; $totca = $totmarge = $totcaold = $totmois = $totmargeold = $totvolume = $totvolumeold = 0; $entete = $marge = $totalmarge = $volume = $totalvolume = $encaisse = $ca = $caold = $margeold = $volumeold = $progress = ''; /* $recfrais = mysql_query_override("select sum(frais) as totfrais from gaia_fraisrd where annee = '".$year."'"); $totfrais = mysql_result_override($recfrais,0,'totfrais'); if($global==0) $totfrais = 0; $recfrais = mysql_query_override("select sum(frais) as totfrais from gaia_fraisrd where annee = '".($year-1)."'"); $totfraisold = mysql_result_override($recfrais,0,'totfrais'); if($global==0) $totfraisold = 0; */ for($i=1;$i<13;$i++) { $entete .= substr('0'.$i,-2,2).'/'.$year.';'; //################################ //####### ANNEE EN COURS ######### $reccom = mysql_query_override($url." '".$year."-".substr('0'.$i,-2,2)."%' group by c.id "); $tot_ht = $marge_ht = $cvolume = 0; while($t = mysql_fetch_array_override($reccom)) { $tot_ht += $t['tot_ht']; $marge_ht += $t['marge_ht']; $cvolume += $t['volume']; } if($year.'-'.substr('0'.$i,-2,2)>date('Y-m')) $totfrais = $totfraisold = 0; //### Calcul encaissé ### if($year.'-'.substr('0'.$i,-2,2)<=date('Y-m')) $totmois = $tot_ht; else $totmois = $totca = 0; $encaisse .= number_format($totmois,2,'.',' ').';'; //### Calcul marge ### if($year.'-'.substr('0'.$i,-2,2)<=date('Y-m')) $totmargemois = $marge_ht; else $totmargemois = $totmarge = 0; $marge .= number_format($totmargemois,2,'.',' ').';'; //### Calcul cumul marge ### $totmarge += $marge_ht; $totalmarge .= number_format($totmarge-$totfrais,2,'.',' ').';'; //### Calcul volume ### if($year.'-'.substr('0'.$i,-2,2)<=date('Y-m')) $totvolumemois = (!empty($cvolume)?$cvolume:0); else $totvolumemois = $totvolume = 0; $volume .= ($totvolumemois).';'; //### Calcul cumul volume ### $totvolume += $cvolume; $totalvolume .= ($totvolume).';'; //### Calcul ca ht ### $totca += $totmois; $ca .= number_format($totca,2,'.',' ').';'; //################################## //####### ANNEE PRECEDENTE ######### $recoldcom = mysql_query_override($url." '".($year-1)."-".substr('0'.$i,-2,2)."%' group by c.id "); $tot_old_ht = $marge_old_ht = $volume_old = 0; while($t = mysql_fetch_array_override($recoldcom)) { $tot_old_ht += $t['tot_ht']; $marge_old_ht += $t['marge_ht']; $volume_old += $t['volume']; } //### Calcul ca ht n-1 ### $totcaold += $tot_old_ht; $caold .= number_format($tot_old_ht,2,'.',' ').'|'.number_format($totcaold,2,'.',' ').';'; //### Calcul marge n-1 ### $totmargeold += $marge_old_ht; $margeold .= number_format($marge_old_ht,2,'.',' ').'|'.number_format($totmargeold-$totfraisold,2,'.',' ').';'; //### Calcul volume n-1 ### $totvolumeold += $volume_old; $volumeold .= (!empty($volume_old)?$volume_old:0).'|'.($totvolumeold).';'; //### Calcul progression mois ### $pourc = 0; if($year.'-'.substr('0'.$i,-2,2)<=date('Y-m')) { if($tot_ht - $tot_old_ht==0) $pourc = 0; elseif($totmois==0) $pourc = -100; else { $ecart = $totmois - $tot_old_ht; $pourc = number_format($ecart * 100 / $totmois,2,'.',''); } } $progress .= $pourc.' %;'; //################################## //################################## } $ret = ' '.stripslashes($nom).';'.$entete."\n". 'C.A.;'.$encaisse."\n". 'CUMUL;'.$ca."\n". 'VOLUME;'.$volume."\n". 'CUMUL;'.$totalvolume."\n". 'C.A. (n-1);'.$caold."\n". 'VOLUME (n-1);'.$volumeold."\n". 'Progression;'.$progress; //'CUMUL;'.$totalmarge."\n". //'MARGE;'.$marge."\n". //'MARGE (n-1);'.$margeold."\n". return $ret; } //################################################################ function recup_cat_stat2($id, $t=0) { global $sql_priv, $year; $totca = $totmarge = $totcaold = $totmargeold = 0; $entete = $marge = $totalmarge = $encaisse = $ca = $caold = $margeold = $progress = ''; $cat = mysql_query_override("select * from gaia_categories where id = '".$id."'"); $allcat = mysql_fetch_array_override($cat); $url = " select sum(ca.total_prix_ht) as tot_ht, sum(ca.marge) as marge_ht, sum(ca.quantite) as volume from gaia_commandes c, gaia_commandes_articles ca, gaia_produits_categories pc ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = pc.produit_id AND pc.categorie_id in (select distinct(id) from gaia_categories where parent_id = '".$id."') AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp between "; if ($t==1) $url = " select sum(ca.total_prix_ht) as tot_ht, sum(ca.marge) as marge_ht, sum(ca.quantite) as volume from gaia_commandes c, gaia_commandes_articles ca, gaia_produits_categories pc ".$sql_priv." c.id = ca.commande_id AND ca.produit_id = pc.produit_id AND pc.categorie_id = '".$id."' AND (statut = '3' or statut = '5' or statut = '8') and o_timestamp between "; //echo $url; return return_stat2($url, $allcat['nom_fr'], $t); } function return_stat2($url, $nom, $t, $global=0) { global $sql_priv, $year; $totca = $totmarge = $totcaold = $totmois = $totmargeold = $totvolume = $totvolumeold = 0; $entete = $marge = $totalmarge = $volume = $totalvolume = $encaisse = $ca = $caold = $margeold = $volumeold = $progress = ''; $urlnew = $url ." '".$_GET['an1']."-".substr('0'.$_GET['mois1'],-2,2)."-".substr('0'.$_GET['jour1'],-2,2)." 00:00:00' and '".$_GET['an2']."-".substr('0'.$_GET['mois2'],-2,2)."-".substr('0'.$_GET['jour2'],-2,2)." 23:59:59' group by c.id "; $urlold = $url ." '".($_GET['an1']-1)."-".substr('0'.$_GET['mois1'],-2,2)."-".substr('0'.$_GET['jour1'],-2,2)." 00:00:00' and '".($_GET['an2']-1)."-".substr('0'.$_GET['mois2'],-2,2)."-".substr('0'.$_GET['jour2'],-2,2)." 23:59:59' group by c.id "; $reccom = mysql_query_override($urlnew); $tot_ht = $marge_ht = $cvolume = 0; while($ta = mysql_fetch_array_override($reccom)) { $tot_ht += $ta['tot_ht']; $marge_ht += $ta['marge_ht']; $cvolume += $ta['volume']; } $reccomold = mysql_query_override($urlold); $tot_old_ht = $marge_old_ht = $volume_old = 0; while($ta = mysql_fetch_array_override($reccomold)) { $tot_old_ht += $ta['tot_ht']; $marge_old_ht += $ta['marge_ht']; $volume_old += $ta['volume']; } if($t>0) $entete = stripslashes($nom).';'; //### Calcul ca ht ### $ca = number_format($tot_ht,2,'.',' '); //### Calcul marge ht ### $marge = number_format($marge_ht,2,'.',' '); //### Calcul volume ### $volume = (!empty($cvolume)?$cvolume:0); //### Calcul ca ht n-1 ### $caold = number_format($tot_old_ht,2,'.',' '); //### Calcul marge ht n-1 ### $margeold = number_format($marge_old_ht,2,'.',' '); //### Calcul volume n-1 ### $volumeold = (!empty($volume_old)?$volume_old:0); //### Calcul progression mois ### $pourc = 0; if($tot_ht - $tot_old_ht==0) $pourc = 0; elseif($tot_ht==0) $pourc = -100; else { $ecart = $tot_ht - $tot_old_ht; $pourc = number_format($ecart * 100 / $tot_ht,2,'.',''); } $progress .= $pourc.' %'; $ret = $entete.$ca.';'.$volume.';'.$caold.';'.$volumeold.';'.$progress; //$marge.';'.$margeold.';'. return $ret; } ?>