J'arrive un peu après la bataille mais bon, voilà le script que j'utilise :
Code
<?php
/*
$Id: orders_compta.php,v 1.1 13/01/2008 09:48:11 fxm Mod $
osCommerce, Open Source E-Commerce Solutions
[url="http://www.oscommerce.com"]http://www.oscommerce.com[/url]
Copyright © 2003 osCommerce - 2007 WebEspace.fr
Released under the GNU General Public License
*/
require('includes/application_top.php');
if ($_POST['export_x']){
header ("Content-type: application/csv");
header('Content-Disposition: attachment; filename="rapport_comptable.csv"');
$rapport_ventes = base64_decode($_POST['ExportCSV']);
$rapport_ventes = str_replace(".", ",", $rapport_ventes);
echo $rapport_ventes;
exit();
tep_redirect(tep_href_link(FILENAME_SALES_COMPTA));
}
function tep_date_day($raw_date) {
if ( ($raw_date == '0000-00-00 00:00:00') || ($raw_date == '') ) return false;
$year = substr($raw_date, 0, 4);
$month = (int)substr($raw_date, 5, 2);
$day = (int)substr($raw_date, 8, 2);
$hour = (int)substr($raw_date, 11, 2);
$minute = (int)substr($raw_date, 14, 2);
$second = (int)substr($raw_date, 17, 2);
if (@date('Y', mktime($hour, $minute, $second, $month, $day, $year)) == $year) {
$jour = date('w', mktime($hour, $minute, $second, $month, $day, $year));
} else {
$jour = ereg_replace('2037' . '$', $year, date('w', mktime($hour, $minute, $second, $month, $day, 2037)));
}
$jours_semaine = array ('Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi');
$j_semaine = array ('D', 'L', 'M', 'M', 'J', 'V', 'S');
return array($jour, $j_semaine[$jour], $jours_semaine[$jour]);
}
require(DIR_WS_CLASSES . 'currencies.php');
$currencies = new currencies();
include(DIR_WS_CLASSES . 'order.php');
////////////////////////////////////////////////////////////
// calcul du total de vente pour une date ou une période donnée
// $date doit $etre au format Y-m-d : Y donne un rapport annuel Y-m un rapport mensuel et Y-m-d le rapport du jour
function get_order_total($date_in, $date_out = false) {
$mois_array = array('01' => array('id' => '01', 'article' => 'de ', 'mois' => 'Janvier'),
'02' => array('id' => '02', 'article' => 'de ', 'mois' => 'Février'),
'03' => array('id' => '03', 'article' => 'de ', 'mois' => 'Mars'),
'04' => array('id' => '04', 'article' => 'd\'', 'mois' => 'Avril'),
'05' => array('id' => '05', 'article' => 'de ', 'mois' => 'Mai'),
'06' => array('id' => '06', 'article' => 'de ', 'mois' => 'Juin'),
'07' => array('id' => '07', 'article' => 'de ', 'mois' => 'Juillet'),
'08' => array('id' => '08', 'article' => 'd\'', 'mois' => 'Août'),
'09' => array('id' => '09', 'article' => 'de ', 'mois' => 'Septembre'),
'10' => array('id' => '10', 'article' => 'd\'', 'mois' => 'Octobre'),
'11' => array('id' => '11', 'article' => 'de ', 'mois' => 'Novembre'),
'12' => array('id' => '12', 'article' => 'de ', 'mois' => 'Décembre'));
if ($date_out == false) $query_orders = "SELECT orders_id, date_purchased FROM " . TABLE_ORDERS . " WHERE date_purchased LIKE \"$date_in%\" ORDER by orders_id";
if ($date_out && $date_in ) $query_orders = "SELECT distinct orders_id, date_purchased FROM " . TABLE_ORDERS . " WHERE date_purchased <= \"$date_out\" and date_purchased >= \"$date_in\" ORDER by orders_id";
$orders_query = tep_db_query($query_orders);
$i=0;
$orders_list = array();
while ($orders_array = tep_db_fetch_array($orders_query)) {
if ($i == 0) {
$first_id = $orders_array['orders_id'];
$first_date = $orders_array['date_purchased'];
}
$last_id = $orders_array['orders_id'];
$last_date = $orders_array['date_purchased'];
$i++;
if (tep_not_null($orders_array['orders_id']) && is_string($orders_array['orders_id']) ) $orders_list[] = $orders_array['orders_id'];
}
$nb_cmde = $i; // nombre de commandes listées
foreach ( $orders_list as $o_id) {
// Total des commandes facturées dans cette période
$query_total = "SELECT value FROM " . TABLE_ORDERS_TOTAL . " WHERE orders_id = '".$o_id."' and class = 'ot_total' ";
$total_query = tep_db_query($query_total);
while ($total_array = tep_db_fetch_array($total_query)) {
$order_ligne['total_ttc'] = $total_array['value'];
}
// Total Frais de ports facturés pour cette période
$query_shipping = "SELECT value FROM " . TABLE_ORDERS_TOTAL . " WHERE orders_id = '".$o_id."' and class = 'ot_shipping' ";
$shipping_query = tep_db_query($query_shipping);;
while ($shipping_array = tep_db_fetch_array($shipping_query)) {
$order_ligne['total_shipping_ttc'] = $shipping_array['value'];
$order_ligne['total_shipping'] = $shipping_array['value'] / 1.196;
}
$order = new order($o_id);
$order_ligne['id'] = $o_id;
$order_ligne['jour'] = tep_date_day($order->info['date_purchased']);
$order_ligne['mois'] = $mois_array[substr($order->info['date_purchased'], 5,2)];
$order_ligne['date'] = tep_date_short($order->info['date_purchased']);
$order_ligne['customer'] = $order->customer['name'];
$order_ligne['payment'] = $order->info['payment_method'];
$order_ligne['tva55'] = 0;
$order_ligne['tva196'] = 0;
$order_ligne['HT55'] = 0;
$order_ligne['HT196'] = 0;
$order_ligne['total_products'] = 0;
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
// TVA 5.5
if ( $order->products[$i]['tax'] == 5.5 ) {
$order_ligne['tva55'] += (($order->products[$i]['price'] *$order->products[$i]['qty'])* $order->products[$i]['tax'] /100 );
$order_ligne['HT55'] += (($order->products[$i]['price'] *$order->products[$i]['qty']));
}
// TVA 19.6
if ( $order->products[$i]['tax'] == 19.6 ) {
$order_ligne['tva196'] += (($order->products[$i]['price'] *$order->products[$i]['qty'])* $order->products[$i]['tax'] /100 );
$order_ligne['HT196'] += (($order->products[$i]['price'] *$order->products[$i]['qty']));
}
$order_ligne['total_products'] += $order->products[$i]['price'] *$order->products[$i]['qty'];
}
$order_ligne['HT196'] += $order_ligne['total_shipping'];
$order_ligne['tva196'] += ($order_ligne['total_shipping_ttc'] - ($order_ligne['total_shipping']));
$result[] = $order_ligne;
}
return $result;
}
///////////////////////////////////////////////////
$date_debut = ($_POST['date_debut'])
? $_POST['date_debut']
: (date("d/m/Y", mktime(0,0,0, date("m") - 1 , 1 , date("Y"))));
$date_fin = ($_POST['date_fin'])
? $_POST['date_fin']
: (date("d/m/Y", mktime(0,0,0, date("m"), 0 , date("Y"))));
$date_in = (date("Y-m-d", mktime(0,0,0, substr($date_debut,3,2), substr($date_debut,0,2) , substr($date_debut,6,4))));
$mk_in = mktime(0,0,0, substr($date_debut,3,2), substr($date_debut,0,2) , substr($date_debut,6,4));
$mk_out = mktime(0,0,0, substr($date_fin,3,2), substr($date_fin,0,2) , substr($date_fin,6,4));
$nb_jours = (($mk_out - $mk_in) / 86400);
for ($i=$nb_jours;$i>=0;$i--){
$result = get_order_total(date("Y-m-d", mktime(0,0,0, substr($date_fin,3,2), substr($date_fin,0,2) -$i , substr($date_fin,6,4) )));
$resultat[] = $result;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo 'Rapport des ventes'; ?></title>
<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
<style type="text/css">
/* <![CDATA[ */
table.petit {font-size:8px; background-color:#eeeeff;}
tr.TitreTable{background-color: #6666FF;color:#FFFFFF;font-weight:bold;}
tr.TotalTable{background-color: #D3605E;color:#FFFFFF;font-weight:bold;font-size:10px;}
tr.Totaux{background-color: #666666;color:#FFFFFF;font-weight:bold;font-size:12px; text-align:right;}
.warning {color:#990000; font-weight:bold;}
td.bright {border-right:2px solid silver;}
/* ]]> */
</style>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/general.js"></script>
</head>
<body>
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr>
<td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
</table></td>
<!-- body_text //-->
<td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<script type="text/javascript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
<script type="text/javascript"><!--
var dateDebut = new ctlSpiffyCalendarBox("dateDebut", "formulaire_date", "date_debut","btnDate1","<?php echo $date_debut; ?>",scBTNMODE_CUSTOMBLUE);
var dateFin = new ctlSpiffyCalendarBox("dateFin", "formulaire_date", "date_fin","btnDate2","<?php echo $date_fin; ?>",scBTNMODE_CUSTOMBLUE);
//--></script>
<?php
echo tep_draw_form('formulaire_date',FILENAME_SALES_COMPTA, '','post', 'enctype="multipart/form-data" onReset="return ResetCheck()" ');
?><table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td class="pageHeading"><?php echo 'Rapport commandes mensuel'; ?></td>
<td><?php echo tep_draw_separator('pixel_trans.gif', '24', '40') . ' '; ?></td>
<td colspan="2" align="right"></td>
</tr>
<tr>
<td align="right" class="main">Date de début de période : </td>
<td colspan="2" class="main"><script type="text/javascript">dateDebut.writeControl(); dateDebut.dateFormat="dd/MM/yyyy";</script></td>
<td class="main">Par défaut le premier du mois dernier</td>
</tr>
<tr>
<td align="right" class="main">Date de fin de période : </td>
<td colspan="2" class="main"><script type="text/javascript">dateFin.writeControl(); dateFin.dateFormat="dd/MM/yyyy";</script></td>
<td class="main">Par défaut le dernier jour du mois dernier</td>
</tr>
<tr>
<td align="right" class="main"></td>
<td colspan="2" class="main"></td>
<td class="main"><?php echo tep_image_submit('button_send.gif', 'Envoyer'); ?></td>
</tr>
</table><?php echo '</form>'; ?></td>
</tr>
<tr>
<td>
<table border="1" width="100%" cellspacing="0" cellpadding="4" class="petit">
<?php
$ligne_titre = ' <tr class="TitreTable">
<td class="bright">jour</td>
<td class="bright">date</td>
<td align="right" class="bright">Commande N°</td>
<td align="center">Client</td>
<td align="right" class="bright">Montant HT </td>
<td align="right">Frais de port HT</td>
<td align="right" class="bright">Total commande HT</td>
<td align="right">HT de TVA 5.5%</td>
<td align="right">TVA 5.5%</td>
<td align="right">HT de TVA 19.6%</td>
<td align="right">TVA 19.6%</td>
<td align="right">Total TTC</td>
<td align="right">Mode de paiement</td>
</tr>'. "\n";
echo $ligne_titre;
$export_xl = "Jour; Date; N° Commande; Client; Produits HT; Port HT; Total Commande HT; Total HT sur TVA 5.50%; TVA 5.50%; Total HT sur TVA 19.60%; TVA 19.60%; Total TTC; Mode de paiement \n";
$date_in = '';
$date_out = '';
$l=0;
if (sizeof($resultat) < 1) {
echo '<tr class="Totaux"><td colspan="11" align="center">Aucun résultat pour cette période '. "</tr>\n";
} else {
for ($i=0, $n=sizeof($resultat); $i<$n; $i++) {
$lignes = $resultat[$i];
if (tep_not_null($lignes)){
foreach ( $lignes as $ligne) {
if (!isset($mois)) $mois = $ligne['mois']['id'];
$tva_55 = ($ligne['tva55'] > 0 ) ? $currencies->format($ligne['tva55']): "-";
$HT_55 = ($ligne['HT55'] > 0 ) ? $currencies->format($ligne['HT55']): "-";
$tva_196 = ($ligne['tva196'] > 0 ) ? $currencies->format($ligne['tva196']): "-";
$HT_196 = ($ligne['HT196'] > 0 ) ? $currencies->format($ligne['HT196']): "-";
$port_ht = ($ligne['total_shipping'] > 0) ? $currencies->format($ligne['total_shipping']) : "-";
$style_day = ($ligne['jour'][0] == 6)? 'style="background-color:#D3E8FA;"':'';
if ($ligne['jour'][0] == 0)$style_day = 'style="background-color:#BDD2E3;"';
if ($ligne['mois']['id'] != $mois){
echo '<tr class="TotalTable">'."\n";
echo '<td colspan="4">Total '.$total['mois'].'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['total_products']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['total_shipping']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['total_HT']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['HT55']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['tva55']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['HT196']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['tva196']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['total_ttc']).'</td>'."\n";
echo '<td ></td>'."\n";
echo "</tr>\n";
$mois = $ligne['mois']['id'];
$total = array();
echo $ligne_titre;
}
echo "<tr $style_day>\n";
echo "<td class=\"bright\">". $ligne['jour'][2] . '</td><td>'.$ligne['date'] . "</td>\n";
echo '<td align="right">'. $ligne['id']. "</td>\n";
echo '<td align="left">'. $ligne['customer']. "</td>\n";
echo '<td align="right" class="bright">'. $currencies->format($ligne['total_products']). "</td>\n";
$total['total_products'] += $ligne['total_products'];
echo '<td align="right" class="bright">'. $port_ht. "</td>\n";
$total['total_shipping'] += $ligne['total_shipping'];
echo '<td align="right"><b>'. $currencies->format($ligne['total_products']+$ligne['total_shipping']). "</b></td>\n";
$total['total_HT'] += ($ligne['total_products']+$ligne['total_shipping']);
echo '<td align="right">'. $HT_55. "</td>\n";
$total['HT55'] += $ligne['HT55'];
echo '<td align="right">'. $tva_55. "</td>\n";
$total['tva55'] += $ligne['tva55'];
echo '<td align="right">'. $HT_196. "</td>\n";
$total['HT196'] += $ligne['HT196'];
echo '<td align="right">'. $tva_196. "</td>\n";
$total['tva196'] += $ligne['tva196'];
echo '<td align="right"><b>'. $currencies->format($ligne['total_ttc']). "</b></td>\n";
$total['total_ttc'] += $ligne['total_ttc'];
echo '<td align="right">'. $ligne['payment']. "</td>\n";
echo "</tr>\n";
$total['mois'] = $ligne['mois']['article'] . $ligne['mois']['mois'];
$export_xl .= $ligne['jour'][2] . ";";
$export_xl .= $ligne['date'] . ";";
$export_xl .= $ligne['id'] . ";";
$export_xl .= $ligne['customer'] . ";";
$export_xl .= $ligne['total_products'] . ";";
$export_xl .= $ligne['total_shipping'] . ";";
$export_xl .= $ligne['total_products']+$ligne['total_shipping'] . ";";
$export_xl .= $ligne['HT55'] . ";";
$export_xl .= $ligne['tva55'] . ";";
$export_xl .= $ligne['HT196'] . ";";
$export_xl .= $ligne['tva196'] . ";";
$export_xl .= $ligne['total_ttc'] . ";";
$export_xl .= $ligne['payment'] . ";";
$export_xl .= "\n";
}
}
}
echo '<tr class="TotalTable">'."\n";
echo '<td colspan="4">Total '.$total['mois'].'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['total_products']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['total_shipping']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['total_HT']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['HT55']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['tva55']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['HT196']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['tva196']).'</td>'."\n";
echo '<td align="right">'.$currencies->format($total['total_ttc']).'</td>'."\n";
echo '<td ></td>'."\n";
echo "</tr>\n";
}
?>
</table>
</td></tr>
<tr>
<td align="center">
<b>Télécharger le tableau au format Excel (CSV) </b>
<?php
echo tep_draw_form('export_csv', FILENAME_SALES_COMPTA);
echo tep_draw_hidden_field('ExportCSV', base64_encode($export_xl));
echo tep_image_submit('button_upload.gif', 'EXPORTER', 'name="export"');
echo '</form>';
?>
</td>
</tr>
</table></td>
<!-- body_text_eof //-->
</tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
</body>
</html>
NB : il n'est pas localisé et fonctionne uniquement en français.
NB2 : pour qu'il fonctionne correctement, il faut penser à déclarer le nom du script en define dans includes/filename.php :
define('FILENAME_SALES_COMPTA', 'orders_compta.php');
Enfin, ce code n'est pas optimisé pour une utilisation intensive.
La méthode utilisée est très gourmande en ressources sql et pourrait être copieusement allégée.
il ne s'agit pas d'une contribution mais d'un script franco-français qui retourne une ligne par commande avec :
le jour, la date, le N° de cmde($order_id), le client, le montant HT, les frais de port HT, le HT de TVA5.5%, la TVA 5.5, le HT de TVA19.6%, la TVA 19.6, le total TTC et le mode de paiement.
Une ligne de total conclut le tableau et c'est exportable en csv (un bouton)
on peut choisir la date de début et la date de fin. (par défaut le mois dernier du 1 au 31)
Attention, pour un rapport annuel, ça peut demander plusieurs dizaines de secondes en fonction du nombre de commandes. Attention au timeout!
dernier point, je ne fais pas de SAV là dessus, mais c'est utilisable en l'état et ça peut servir de trame pour un script plus abouti.
Bon code