fix bugs on margin reports

This commit is contained in:
Christophe Battarel 2013-10-05 10:21:38 +02:00
parent b695204259
commit 37e47272c7
6 changed files with 196 additions and 134 deletions

View File

@ -101,34 +101,14 @@ print '</td>';
print '<td style="text-align: center;">'; print '<td style="text-align: center;">';
print '<input type="submit" class="button" value="'.$langs->trans('Launch').'" />'; print '<input type="submit" class="button" value="'.$langs->trans('Launch').'" />';
print '</td></tr>'; print '</td></tr>';
// Total Margin
print '<tr style="font-weight: bold"><td>'.$langs->trans("TotalMargin").'</td><td colspan="4">';
print '<span id="totalMargin"></span>'; // set by jquery (see below)
print '</td></tr>';
// Margin Rate
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarginRate").'</td><td colspan="4">';
print '<span id="marginRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
// Mark Rate
if (! empty($conf->global->DISPLAY_MARK_RATES)) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarkRate").'</td><td colspan="4">';
print '<span id="markRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
print "</table>"; print "</table>";
print '</form>'; print '</form>';
$sql = "SELECT s.nom, s.rowid as socid, s.code_client, s.client, u.rowid as agent,"; $sql = "SELECT s.nom, s.rowid as socid, s.code_client, s.client, u.rowid as agent,";
$sql.= " u.login, u.lastname, u.firstname,"; $sql.= " u.login, u.lastname, u.firstname,";
$sql.= " sum(d.total_ht) as selling_price,"; $sql.= " sum(d.total_ht) as selling_price,";
$sql.= $db->ifsql('f.type =2','sum(d.buy_price_ht * d.qty *-1)','sum(d.buy_price_ht * d.qty)')." as buying_price, "; $sql.= "sum(".$db->ifsql('d.total_ht <=0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,";
$sql.= $db->ifsql('f.type =2','sum(d.total_ht + (d.buy_price_ht * d.qty))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge" ; $sql.= " sum(".$db->ifsql('d.total_ht <=0','-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))','d.total_ht - (d.buy_price_ht * d.qty)').") as marge" ;
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture as f"; $sql.= ", ".MAIN_DB_PREFIX."facture as f";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact e ON e.element_id = f.rowid and e.statut = 4 and e.fk_c_type_contact = ".(empty($conf->global->AGENT_CONTACT_TYPE)?-1:$conf->global->AGENT_CONTACT_TYPE); $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact e ON e.element_id = f.rowid and e.statut = 4 and e.fk_c_type_contact = ".(empty($conf->global->AGENT_CONTACT_TYPE)?-1:$conf->global->AGENT_CONTACT_TYPE);
@ -158,7 +138,7 @@ $sql .= " AND d.buy_price_ht IS NOT NULL";
if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
$sql .= " AND d.buy_price_ht <> 0"; $sql .= " AND d.buy_price_ht <> 0";
if ($agentid > 0) if ($agentid > 0)
$sql.= " GROUP BY s.rowid"; $sql.= " GROUP BY s.rowid, u.rowid";
else else
$sql.= " GROUP BY u.rowid"; $sql.= " GROUP BY u.rowid";
$sql.= " ORDER BY $sortfield $sortorder "; $sql.= " ORDER BY $sortfield $sortorder ";
@ -179,9 +159,9 @@ if ($result)
print '<tr class="liste_titre">'; print '<tr class="liste_titre">';
if ($agentid > 0) if ($agentid > 0)
print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;agentid=".$agentid,'align="center"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;agentid=".$agentid,'',$sortfield,$sortorder);
else else
print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.lastname","","&amp;agentid=".$agentid,'align="center"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.lastname","","&amp;agentid=".$agentid,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
@ -200,16 +180,29 @@ if ($result)
if ($num > 0) if ($num > 0)
{ {
$var=true; $var=true;
while ($i < $num /*&& $i < $conf->liste_limit*/) while ($i < $num /*&& $i < $conf->liste_limit*/)
{ {
$objp = $db->fetch_object($result); $objp = $db->fetch_object($result);
$marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):''; $pa = $objp->buying_price;
$markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):''; $pv = $objp->selling_price;
$marge = $objp->marge;
if ($marge < 0)
{
$marginRate = ($pa != 0)?-1*(100 * round($marge / $pa, 5)):'' ;
$markRate = ($pv != 0)?-1*(100 * round($marge / $pv, 5)):'' ;
}
else
{
$marginRate = ($pa != 0)?(100 * round($marge / $pa, 5)):'' ;
$markRate = ($pv != 0)?(100 * round($marge / $pv, 5)):'' ;
}
$var=!$var; $var=!$var;
print "<tr $bc[$var]>"; print "<tr ".$bc[$var].">";
if ($agentid > 0) { if ($agentid > 0) {
$companystatic->id=$objp->socid; $companystatic->id=$objp->socid;
$companystatic->nom=$objp->nom; $companystatic->nom=$objp->nom;
@ -220,9 +213,10 @@ if ($result)
$userstatic->fetch($objp->agent); $userstatic->fetch($objp->agent);
print "<td>".$userstatic->getFullName($langs,0,0,0)."</td>\n"; print "<td>".$userstatic->getFullName($langs,0,0,0)."</td>\n";
} }
print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
print "<td align=\"right\">".price($objp->buying_price)."</td>\n"; print "<td align=\"right\">".price($pv)."</td>\n";
print "<td align=\"right\">".price($objp->marge)."</td>\n"; print "<td align=\"right\">".price($pa)."</td>\n";
print "<td align=\"right\">".price($marge)."</td>\n";
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) if (! empty($conf->global->DISPLAY_MARGIN_RATES))
print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n"; print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n";
if (! empty($conf->global->DISPLAY_MARK_RATES)) if (! empty($conf->global->DISPLAY_MARK_RATES))
@ -230,30 +224,10 @@ if ($result)
print "</tr>\n"; print "</tr>\n";
$i++; $i++;
$cumul_achat += round($objp->buying_price, $rounding); $cumul_achat += round($objp->buying_price, $rounding);
$cumul_vente += round($objp->selling_price, $rounding); $cumul_vente += round($objp->selling_price, $rounding);
} }
} }
// affichage totaux marges
$var=!$var;
$totalMargin = $cumul_vente - $cumul_achat;
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'';
print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
print '<td>';
print $langs->trans('Total');
print "</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n";
print "<td align=\"right\">".price($cumul_achat)."</td>\n";
print "<td align=\"right\">".price($totalMargin)."</td>\n";
if (! empty($conf->global->DISPLAY_MARGIN_RATES))
print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n";
if (! empty($conf->global->DISPLAY_MARK_RATES))
print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n";
print "</tr>\n";
print "</table>"; print "</table>";
} }
else else
@ -265,17 +239,14 @@ $db->free($result);
llxFooter(); llxFooter();
$db->close(); $db->close();
?> ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$("#agentid").change(function() { $("#agentid").change(function() {
$("div.fiche form").submit(); $("div.fiche form").submit();
}); });
$("#totalMargin").html("<?php echo price($totalMargin); ?>");
$("#marginRate").html("<?php echo (($marginRate === '')?'n/a':price($marginRate)."%"); ?>");
$("#markRate").html("<?php echo (($markRate === '')?'n/a':price($markRate)."%"); ?>");
}); });
</script> </script>

View File

@ -86,7 +86,7 @@ if ($socid > 0) {
{ {
print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>'; print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>';
print '<td colspan="4">'; print '<td colspan="4">';
$form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,$socid,'socid','client=1',1,0,1); $form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,$socid,'socid','client=1 OR client=3',1,0,1);
print '</td></tr>'; print '</td></tr>';
$client = true; $client = true;
@ -97,7 +97,7 @@ if ($socid > 0) {
else { else {
print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>'; print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>';
print '<td colspan="4">'; print '<td colspan="4">';
$form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,null,'socid','client=1',1,0,1); $form->form_thirdparty($_SERVER['PHP_SELF'],null,'socid','client=1 OR client=3',1,0,1);
print '</td></tr>'; print '</td></tr>';
} }
@ -153,11 +153,11 @@ if (! empty($conf->global->DISPLAY_MARK_RATES)) {
print "</table>"; print "</table>";
print '</form>'; print '</form>';
$sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client, s.client,"; $sql = "SELECT s.nom, s.rowid as socid, s.code_client, s.client,";
$sql.= " f.facnumber, f.total as total_ht,"; $sql.= " f.facnumber, f.total as total_ht,";
$sql.= " sum(d.total_ht) as selling_price,"; $sql.= " sum(d.total_ht) as selling_price,";
$sql.= $db->ifsql('f.type =2','sum(d.buy_price_ht * d.qty *-1)','sum(d.buy_price_ht * d.qty)')." as buying_price, "; $sql.= "sum(".$db->ifsql('d.total_ht <=0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,";
$sql.= $db->ifsql('f.type =2','sum(d.total_ht + (d.buy_price_ht * d.qty))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge," ; $sql.= "sum(".$db->ifsql('d.total_ht <=0','-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))','d.total_ht - (d.buy_price_ht * d.qty)').") as marge," ;
$sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid"; $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture as f"; $sql.= ", ".MAIN_DB_PREFIX."facture as f";
@ -176,7 +176,7 @@ $sql .= " AND d.buy_price_ht IS NOT NULL";
if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
$sql .= " AND d.buy_price_ht <> 0"; $sql .= " AND d.buy_price_ht <> 0";
if ($client) if ($client)
$sql.= " GROUP BY f.rowid"; $sql.= " GROUP BY f.rowid, s.rowid";
else else
$sql.= " GROUP BY s.rowid"; $sql.= " GROUP BY s.rowid";
$sql.= " ORDER BY $sortfield $sortorder "; $sql.= " ORDER BY $sortfield $sortorder ";
@ -189,7 +189,7 @@ if ($result)
{ {
$num = $db->num_rows($result); $num = $db->num_rows($result);
print '<br>'; print '<br>';
print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',0,0,''); print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',0,0,'');
$i = 0; $i = 0;
@ -197,11 +197,11 @@ if ($result)
print '<tr class="liste_titre">'; print '<tr class="liste_titre">';
if (! empty($client)) { if (! empty($client)) {
print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;socid=".$socid,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;socid=".$socid,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;socid=".$socid,'align="center"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;socid=".$socid,'align="center"',$sortfield,$sortorder);
} }
else else
print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;socid=".$socid,'align="center"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;socid=".$socid,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder);
@ -219,39 +219,54 @@ if ($result)
if ($num > 0) if ($num > 0)
{ {
$var=True; $var=True;
while ($i < $num /*&& $i < $conf->liste_limit*/) while ($i < $num /*&& $i < $conf->liste_limit*/)
{ {
$objp = $db->fetch_object($result); $objp = $db->fetch_object($result);
$marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):'' ; $pa = $objp->buying_price;
$markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):'' ; $pv = $objp->selling_price;
$marge = $objp->marge;
if ($marge < 0)
{
$marginRate = ($pa != 0)?-1*(100 * round($marge / $pa, 5)):'' ;
$markRate = ($pv != 0)?-1*(100 * round($marge / $pv, 5)):'' ;
}
else
{
$marginRate = ($pa != 0)?(100 * round($marge / $pa, 5)):'' ;
$markRate = ($pv != 0)?(100 * round($marge / $pv, 5)):'' ;
}
$var=!$var; $var=!$var;
print "<tr $bc[$var]>"; print "<tr ".$bc[$var].">";
if ($client) { if ($client) {
print '<td>'; print '<td>';
$invoicestatic->id=$objp->facid; $invoicestatic->id=$objp->facid;
$invoicestatic->ref=$objp->facnumber; $invoicestatic->ref=$objp->facnumber;
print $invoicestatic->getNomUrl(1); print $invoicestatic->getNomUrl(1);
print "</td>\n"; print "</td>\n";
print "<td align=\"center\">"; print "<td align=\"center\">";
print dol_print_date($db->jdate($objp->datef),'day')."</td>"; print dol_print_date($db->jdate($objp->datef),'day')."</td>";
} }
else { else {
$companystatic->id=$objp->socid; $companystatic->id=$objp->socid;
$companystatic->nom=$objp->nom; $companystatic->nom=$objp->nom;
$companystatic->client=$objp->client; $companystatic->client=$objp->client;
print "<td>".$companystatic->getNomUrl(1,'customer')."</td>\n"; print "<td>".$companystatic->getNomUrl(1,'customer')."</td>\n";
} }
print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
print "<td align=\"right\">".price($objp->buying_price)."</td>\n"; print "<td align=\"right\">".price($pv)."</td>\n";
print "<td align=\"right\">".price($objp->marge)."</td>\n"; print "<td align=\"right\">".price($pa)."</td>\n";
print "<td align=\"right\">".price($marge)."</td>\n";
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) if (! empty($conf->global->DISPLAY_MARGIN_RATES))
print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n"; print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n";
if (! empty($conf->global->DISPLAY_MARK_RATES)) if (! empty($conf->global->DISPLAY_MARK_RATES))
print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n"; print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n";
print "</tr>\n"; print "</tr>\n";
$i++; $i++;
$cumul_achat += round($objp->buying_price, $rounding); $cumul_achat += round($objp->buying_price, $rounding);
$cumul_vente += round($objp->selling_price, $rounding); $cumul_vente += round($objp->selling_price, $rounding);
@ -261,14 +276,23 @@ if ($result)
// affichage totaux marges // affichage totaux marges
$var=!$var; $var=!$var;
$totalMargin = $cumul_vente - $cumul_achat; $totalMargin = $cumul_vente - $cumul_achat;
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'' ; if ($totalMargin < 0)
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'' ; {
$marginRate = ($cumul_achat != 0)?-1*(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?-1*(100 * round($totalMargin / $cumul_vente, 5)):'';
}
else
{
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'';
}
print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">'; print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
if ($client) if ($client)
print '<td colspan=2>'; print '<td colspan=2>';
else else
print '<td>'; print '<td>';
print $langs->trans('TotalMargin')."</td>"; print $langs->trans('TotalMargin')."</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n"; print "<td align=\"right\">".price($cumul_vente)."</td>\n";
print "<td align=\"right\">".price($cumul_achat)."</td>\n"; print "<td align=\"right\">".price($cumul_achat)."</td>\n";
print "<td align=\"right\">".price($totalMargin)."</td>\n"; print "<td align=\"right\">".price($totalMargin)."</td>\n";
@ -289,7 +313,9 @@ $db->free($result);
llxFooter(); llxFooter();
$db->close(); $db->close();
?> ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {

View File

@ -117,12 +117,18 @@ function getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localta
// calcul pu_ht remisés // calcul pu_ht remisés
$tabprice=calcul_price_total(1, $pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 'HT', 0, 0); // FIXME Parameter type is missing, i put 0 to avoid blocking error $tabprice=calcul_price_total(1, $pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 'HT', 0, 0); // FIXME Parameter type is missing, i put 0 to avoid blocking error
$pu_ht_remise = $tabprice[0]; $pu_ht_remise = $tabprice[0];
// calcul marge
if ($pu_ht_remise < 0)
$marge = -1 * (abs($pu_ht_remise) - $paht_ret);
else
$marge = $pu_ht_remise - $paht_ret;
// calcul taux marge // calcul taux marge
if ($paht_ret != 0) if ($paht_ret != 0)
$marge_tx_ret = round((100 * ($pu_ht_remise - $paht_ret)) / $paht_ret, 3); $marge_tx_ret = round((100 * $marge) / $paht_ret, 3);
// calcul taux marque // calcul taux marque
if ($pu_ht_remise != 0) if ($pu_ht_remise != 0)
$marque_tx_ret = round((100 * ($pu_ht_remise - $paht_ret)) / $pu_ht_remise, 3); $marque_tx_ret = round((100 * $marge) / $pu_ht_remise, 3);
return array($paht_ret, $marge_tx_ret, $marque_tx_ret); return array($paht_ret, $marge_tx_ret, $marque_tx_ret);
} }

View File

@ -155,11 +155,11 @@ if (! empty($conf->global->DISPLAY_MARK_RATES)) {
print "</table>"; print "</table>";
print '</form>'; print '</form>';
$sql = "SELECT DISTINCT d.fk_product, p.label, p.rowid, p.fk_product_type, p.ref,"; $sql = "SELECT d.fk_product, p.label, p.rowid, p.fk_product_type, p.ref,";
$sql.= " f.facnumber, f.total as total_ht,"; $sql.= " f.facnumber, f.total as total_ht,";
$sql.= " sum(d.total_ht) as selling_price,"; $sql.= " sum(d.total_ht) as selling_price,";
$sql.= $db->ifsql('f.type =2','sum(d.buy_price_ht * d.qty *-1)','sum(d.buy_price_ht * d.qty)')." as buying_price, "; $sql.= "sum(".$db->ifsql('d.total_ht <=0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,";
$sql.= $db->ifsql('f.type =2','sum(d.total_ht + (d.buy_price_ht * d.qty))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge," ; $sql.= "sum(".$db->ifsql('d.total_ht <=0','-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))','d.total_ht - (d.buy_price_ht * d.qty)').") as marge," ;
$sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid"; $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."product as p"; $sql.= ", ".MAIN_DB_PREFIX."product as p";
@ -180,9 +180,10 @@ $sql .= " AND d.buy_price_ht IS NOT NULL";
if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
$sql .= " AND d.buy_price_ht <> 0"; $sql .= " AND d.buy_price_ht <> 0";
if ($id > 0) if ($id > 0)
$sql.= " GROUP BY f.rowid"; $sql.= " GROUP BY f.rowid, d.fk_product";
else else
$sql.= " GROUP BY d.fk_product"; $sql.= " GROUP BY d.fk_product";
$sql.= " ORDER BY $sortfield $sortorder "; $sql.= " ORDER BY $sortfield $sortorder ";
// TODO: calculate total to display then restore pagination // TODO: calculate total to display then restore pagination
//$sql.= $db->plimit($conf->liste_limit +1, $offset); //$sql.= $db->plimit($conf->liste_limit +1, $offset);
@ -200,11 +201,11 @@ if ($result)
print '<tr class="liste_titre">'; print '<tr class="liste_titre">';
if ($id > 0) { if ($id > 0) {
print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;id=".$id,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;id=".$id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","",'align="center"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;id=".$id,'align="center"',$sortfield,$sortorder);
} }
else else
print_liste_field_titre($langs->trans("ProductService"),$_SERVER["PHP_SELF"],"p.ref","","&amp;id=".$id,'align="center"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("ProductService"),$_SERVER["PHP_SELF"],"p.ref","","&amp;id=".$id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;id=".$id,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;id=".$id,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;id=".$id,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;id=".$id,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;id=".$id,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;id=".$id,'align="right"',$sortfield,$sortorder);
@ -222,12 +223,24 @@ if ($result)
if ($num > 0) if ($num > 0)
{ {
$var=True; $var=True;
while ($i < $num /*&& $i < $conf->liste_limit*/) while ($i < $num /*&& $i < $conf->liste_limit*/)
{ {
$objp = $db->fetch_object($result); $objp = $db->fetch_object($result);
$pa = $objp->buying_price;
$pv = $objp->selling_price;
$marge = $objp->marge;
$marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):'' ; if ($marge < 0)
$markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):'' ; {
$marginRate = ($pa != 0)?-1*(100 * round($marge / $pa, 5)):'' ;
$markRate = ($pv != 0)?-1*(100 * round($marge / $pv, 5)):'' ;
}
else
{
$marginRate = ($pa != 0)?(100 * round($marge / $pa, 5)):'' ;
$markRate = ($pv != 0)?(100 * round($marge / $pv, 5)):'' ;
}
$var=!$var; $var=!$var;
@ -250,25 +263,35 @@ if ($result)
$text.= ' - '.$objp->label; $text.= ' - '.$objp->label;
print "<td>".$product_static->getNomUrl(1)."</td>\n"; print "<td>".$product_static->getNomUrl(1)."</td>\n";
} }
print "<td align=\"right\">".price($objp->selling_price)."</td>\n"; print "<td align=\"right\">".price($pv)."</td>\n";
print "<td align=\"right\">".price($objp->buying_price)."</td>\n"; print "<td align=\"right\">".price($pa)."</td>\n";
print "<td align=\"right\">".price($objp->marge)."</td>\n"; print "<td align=\"right\">".price($marge)."</td>\n";
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) if (! empty($conf->global->DISPLAY_MARGIN_RATES))
print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n"; print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n";
if (! empty($conf->global->DISPLAY_MARK_RATES)) if (! empty($conf->global->DISPLAY_MARK_RATES))
print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n"; print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n";
print "</tr>\n"; print "</tr>\n";
$i++; $i++;
$cumul_achat += round($objp->buying_price, $rounding); $cumul_achat += round($objp->buying_price, $rounding);
$cumul_vente += round($objp->selling_price, $rounding); $cumul_vente += round($objp->selling_price, $rounding);
} }
}
}
// affichage totaux marges // affichage totaux marges
$var=!$var; $var=!$var;
$totalMargin = $cumul_vente - $cumul_achat; $totalMargin = $cumul_vente - $cumul_achat;
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'' ; if ($totalMargin < 0)
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'' ; {
$marginRate = ($cumul_achat != 0)?-1*(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?-1*(100 * round($totalMargin / $cumul_vente, 5)):'';
}
else
{
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'';
}
print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">'; print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
if ($id > 0) if ($id > 0)
print '<td colspan=2>'; print '<td colspan=2>';
@ -295,7 +318,9 @@ $db->free($result);
llxFooter(); llxFooter();
$db->close(); $db->close();
?> ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
@ -312,4 +337,4 @@ $(document).ready(function() {
$("#markRate").html("<?php echo (($markRate === '')?'n/a':price($markRate)."%"); ?>"); $("#markRate").html("<?php echo (($markRate === '')?'n/a':price($markRate)."%"); ?>");
}); });
</script> </script>

View File

@ -128,13 +128,13 @@ if ($id > 0 || ! empty($ref))
print '</div>'; print '</div>';
$sql = "SELECT DISTINCT s.nom, s.rowid as socid, s.code_client,"; $sql = "SELECT s.nom, s.rowid as socid, s.code_client,";
$sql.= " f.facnumber, f.total as total_ht,"; $sql.= " f.facnumber, f.total as total_ht,";
$sql.= " d.total_ht as selling_price,"; $sql.= " sum(d.total_ht) as selling_price,";
$sql.= $db->ifsql('f.type =2','(d.buy_price_ht * d.qty *-1)','(d.buy_price_ht * d.qty)')." as buying_price, "; $sql.= $db->ifsql('f.type =2','sum(d.qty *-1)','sum(d.qty)')." as qty,";
$sql.= $db->ifsql('f.type =2','d.qty *-1','d.qty')." as qty,"; $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid,";
$sql.= $db->ifsql('f.type =2','d.total_ht + (d.buy_price_ht * d.qty)','d.total_ht - (d.buy_price_ht * d.qty)')." as marge," ; $sql.= $db->ifsql('f.type =2','sum(d.qty * d.buy_price_ht *-1)','sum(d.qty * d.buy_price_ht)')." as buying_price,";
$sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid"; $sql.= $db->ifsql('f.type =2','sum(-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty)))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge" ;
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user "; if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user ";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture as f"; $sql.= ", ".MAIN_DB_PREFIX."facture as f";
@ -150,6 +150,7 @@ if ($id > 0 || ! empty($ref))
$sql .= " AND d.buy_price_ht IS NOT NULL"; $sql .= " AND d.buy_price_ht IS NOT NULL";
if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
$sql .= " AND d.buy_price_ht <> 0"; $sql .= " AND d.buy_price_ht <> 0";
$sql.= " GROUP BY f.rowid";
$sql.= " ORDER BY $sortfield $sortorder "; $sql.= " ORDER BY $sortfield $sortorder ";
// TODO: calculate total to display then restore pagination // TODO: calculate total to display then restore pagination
//$sql.= $db->plimit($conf->liste_limit +1, $offset); //$sql.= $db->plimit($conf->liste_limit +1, $offset);
@ -193,9 +194,18 @@ if ($id > 0 || ! empty($ref))
$objp = $db->fetch_object($result); $objp = $db->fetch_object($result);
$var=!$var; $var=!$var;
$marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):'' ; if ($objp->marge < 0)
$markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):'' ; {
print "<tr $bc[$var]>"; $marginRate = ($objp->buying_price != 0)?-1*(100 * round($objp->marge / $objp->buying_price, 5)):'' ;
$markRate = ($objp->selling_price != 0)?-1*(100 * round($objp->marge / $objp->selling_price, 5)):'' ;
}
else
{
$marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):'' ;
$markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):'' ;
}
print "<tr ".$bc[$var].">";
print '<td>'; print '<td>';
$invoicestatic->id=$objp->facid; $invoicestatic->id=$objp->facid;
$invoicestatic->ref=$objp->facnumber; $invoicestatic->ref=$objp->facnumber;
@ -225,8 +235,16 @@ if ($id > 0 || ! empty($ref))
// affichage totaux marges // affichage totaux marges
$var=!$var; $var=!$var;
$totalMargin = $cumul_vente - $cumul_achat; $totalMargin = $cumul_vente - $cumul_achat;
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):''; if ($totalMargin < 0)
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):''; {
$marginRate = ($cumul_achat != 0)?-1*(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?-1*(100 * round($totalMargin / $cumul_vente, 5)):'';
}
else
{
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'';
}
print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">'; print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
print '<td colspan=4>'.$langs->trans('TotalMargin')."</td>"; print '<td colspan=4>'.$langs->trans('TotalMargin')."</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n"; print "<td align=\"right\">".price($cumul_vente)."</td>\n";

View File

@ -130,8 +130,8 @@ if ($socid > 0)
$sql.= " f.facnumber, f.total as total_ht,"; $sql.= " f.facnumber, f.total as total_ht,";
$sql.= " sum(d.total_ht) as selling_price,"; $sql.= " sum(d.total_ht) as selling_price,";
$sql.= $db->ifsql('f.type =2','sum(d.buy_price_ht * d.qty *-1)','sum(d.buy_price_ht * d.qty)')." as buying_price, "; $sql.= $db->ifsql('f.type =2','sum(d.qty * d.buy_price_ht *-1)','sum(d.qty * d.buy_price_ht)')." as buying_price,";
$sql.= $db->ifsql('f.type =2','sum(d.total_ht + (d.buy_price_ht * d.qty))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge," ; $sql.= $db->ifsql('f.type =2','sum(-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty)))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge," ;
$sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid"; $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture as f"; $sql.= ", ".MAIN_DB_PREFIX."facture as f";
@ -144,7 +144,7 @@ if ($socid > 0)
$sql .= " AND d.buy_price_ht IS NOT NULL"; $sql .= " AND d.buy_price_ht IS NOT NULL";
if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
$sql .= " AND d.buy_price_ht <> 0"; $sql .= " AND d.buy_price_ht <> 0";
$sql.= " GROUP BY f.rowid, s.nom, s.rowid, s.code_client, f.facnumber, f.total, f.datef, f.paye, f.fk_statut"; $sql.= " GROUP BY f.rowid";
$sql.= " ORDER BY $sortfield $sortorder "; $sql.= " ORDER BY $sortfield $sortorder ";
// TODO: calculate total to display then restore pagination // TODO: calculate total to display then restore pagination
//$sql.= $db->plimit($conf->liste_limit +1, $offset); //$sql.= $db->plimit($conf->liste_limit +1, $offset);
@ -185,12 +185,20 @@ if ($socid > 0)
{ {
$objp = $db->fetch_object($result); $objp = $db->fetch_object($result);
$marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):'' ; if ($objp->marge < 0)
$markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):'' ; {
$marginRate = ($objp->buying_price != 0)?-1*(100 * round($objp->marge / $objp->buying_price, 5)):'' ;
$markRate = ($objp->selling_price != 0)?-1*(100 * round($objp->marge / $objp->selling_price, 5)):'' ;
}
else
{
$marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):'' ;
$markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):'' ;
}
$var=!$var; $var=!$var;
print "<tr $bc[$var]>"; print "<tr ".$bc[$var].">";
print '<td>'; print '<td>';
$invoicestatic->id=$objp->facid; $invoicestatic->id=$objp->facid;
$invoicestatic->ref=$objp->facnumber; $invoicestatic->ref=$objp->facnumber;
@ -216,8 +224,16 @@ if ($socid > 0)
// affichage totaux marges // affichage totaux marges
$var=!$var; $var=!$var;
$totalMargin = $cumul_vente - $cumul_achat; $totalMargin = $cumul_vente - $cumul_achat;
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'' ; if ($totalMargin < 0)
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'' ; {
$marginRate = ($cumul_achat != 0)?-1*(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?-1*(100 * round($totalMargin / $cumul_vente, 5)):'';
}
else
{
$marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'';
$markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'';
}
print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">'; print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
print '<td colspan=2>'.$langs->trans('TotalMargin')."</td>"; print '<td colspan=2>'.$langs->trans('TotalMargin')."</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n"; print "<td align=\"right\">".price($cumul_vente)."</td>\n";