Merge pull request #3088 from atm-maxime/develop

New : add stats by qty on report turnover by product (+ clean code)
This commit is contained in:
Juanjo Menent 2015-06-24 18:23:31 +02:00
commit fc9d54fdaa

View File

@ -47,7 +47,7 @@ if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta");
$sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"]; $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"];
$sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"]; $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"];
if (! $sortorder) $sortorder="asc"; if (! $sortorder) $sortorder="asc";
if (! $sortfield) $sortfield="name"; if (! $sortfield) $sortfield="ref";
// Category // Category
$selected_cat = (int) GETPOST('search_categ', 'int'); $selected_cat = (int) GETPOST('search_categ', 'int');
@ -181,7 +181,8 @@ $catotal=0;
if ($modecompta == 'CREANCES-DETTES') if ($modecompta == 'CREANCES-DETTES')
{ {
$sql = "SELECT DISTINCT p.rowid as rowid, p.ref as ref, p.label as label,"; $sql = "SELECT DISTINCT p.rowid as rowid, p.ref as ref, p.label as label,";
$sql.= " sum(l.total_ht) as amount, sum(l.total_ttc) as amount_ttc"; $sql.= " SUM(l.total_ht) as amount, SUM(l.total_ttc) as amount_ttc,";
$sql.= " SUM(CASE WHEN f.type = 2 THEN -l.qty ELSE l.qty END) as qty";
$sql.= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."facturedet as l, ".MAIN_DB_PREFIX."product as p"; $sql.= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."facturedet as l, ".MAIN_DB_PREFIX."product as p";
if ($selected_cat === -2) // Without any category if ($selected_cat === -2) // Without any category
{ {
@ -214,7 +215,7 @@ if ($modecompta == 'CREANCES-DETTES')
} }
$sql.= " AND f.entity = ".$conf->entity; $sql.= " AND f.entity = ".$conf->entity;
$sql.= " GROUP BY p.rowid, p.ref, p.label"; $sql.= " GROUP BY p.rowid, p.ref, p.label";
$sql.= " ORDER BY p.ref"; $sql.= $db->order($sortfield,$sortorder);
dol_syslog("cabyprodserv", LOG_DEBUG); dol_syslog("cabyprodserv", LOG_DEBUG);
$result = $db->query($sql); $result = $db->query($sql);
@ -225,9 +226,11 @@ if ($modecompta == 'CREANCES-DETTES')
$obj = $db->fetch_object($result); $obj = $db->fetch_object($result);
$amount_ht[$obj->rowid] = $obj->amount; $amount_ht[$obj->rowid] = $obj->amount;
$amount[$obj->rowid] = $obj->amount_ttc; $amount[$obj->rowid] = $obj->amount_ttc;
$qty[$obj->rowid] = $obj->qty;
$name[$obj->rowid] = $obj->ref . ' - ' . $obj->label; $name[$obj->rowid] = $obj->ref . ' - ' . $obj->label;
$catotal_ht+=$obj->amount; $catotal_ht+=$obj->amount;
$catotal+=$obj->amount_ttc; $catotal+=$obj->amount_ttc;
$qtytotal+=$obj->qty;
$i++; $i++;
} }
} else { } else {
@ -255,25 +258,46 @@ if ($modecompta == 'CREANCES-DETTES')
print ' checked'; print ' checked';
} }
print '></td>'; print '></td>';
print '<td colspan="3" align="right">'; print '<td colspan="5" align="right">';
print '<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"),'search.png','','',1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">'; print '<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"),'search.png','','',1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
print '</td></tr>'; print '</td></tr>';
// Array header // Array header
print "<tr class=\"liste_titre\">"; print "<tr class=\"liste_titre\">";
print_liste_field_titre( print_liste_field_titre(
$langs->trans("Product"), $langs->trans("Product"),
$_SERVER["PHP_SELF"], $_SERVER["PHP_SELF"],
"name", "ref",
"", "",
$paramslink, $paramslink,
"", "",
$sortfield, $sortfield,
$sortorder $sortorder
); );
print_liste_field_titre(
$langs->trans('Quantity'),
$_SERVER["PHP_SELF"],
"qty",
"",
$paramslink,
'align="right"',
$sortfield,
$sortorder
);
print_liste_field_titre(
$langs->trans("Percentage"),
$_SERVER["PHP_SELF"],
"qty",
"",
$paramslink,
'align="right"',
$sortfield,
$sortorder
);
print_liste_field_titre( print_liste_field_titre(
$langs->trans('AmountHT'), $langs->trans('AmountHT'),
$_SERVER["PHP_SELF"], $_SERVER["PHP_SELF"],
"amount_ht", "amount",
"", "",
$paramslink, $paramslink,
'align="right"', 'align="right"',
@ -300,40 +324,13 @@ if ($modecompta == 'CREANCES-DETTES')
$sortfield, $sortfield,
$sortorder $sortorder
); );
// TODO: statistics?
print "</tr>\n"; print "</tr>\n";
// Array Data // Array Data
$var=true; $var=true;
if (count($amount)) { if (count($name)) {
$arrayforsort=$name; foreach($name as $key=>$value) {
// defining arrayforsort
if ($sortfield == 'nom' && $sortorder == 'asc') {
asort($name);
$arrayforsort=$name;
}
if ($sortfield == 'nom' && $sortorder == 'desc') {
arsort($name);
$arrayforsort=$name;
}
if ($sortfield == 'amount_ht' && $sortorder == 'asc') {
asort($amount_ht);
$arrayforsort=$amount_ht;
}
if ($sortfield == 'amount_ht' && $sortorder == 'desc') {
arsort($amount_ht);
$arrayforsort=$amount_ht;
}
if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
asort($amount);
$arrayforsort=$amount;
}
if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
arsort($amount);
$arrayforsort=$amount;
}
foreach($arrayforsort as $key=>$value) {
$var=!$var; $var=!$var;
print "<tr ".$bc[$var].">"; print "<tr ".$bc[$var].">";
@ -347,6 +344,14 @@ if ($modecompta == 'CREANCES-DETTES')
print "<td>".$linkname."</td>\n"; print "<td>".$linkname."</td>\n";
// Quantity
print '<td align="right">';
print $qty[$key];
print '</td>';
// Percent;
print '<td align="right">'.($qtytotal > 0 ? round(100 * $qty[$key] / $qtytotal, 2).'%' : '&nbsp;').'</td>';
// Amount w/o VAT // Amount w/o VAT
print '<td align="right">'; print '<td align="right">';
/*if ($key > 0) { /*if ($key > 0) {