diff --git a/htdocs/margin/agentMargins.php b/htdocs/margin/agentMargins.php index 919e47771a6..dc858d66b76 100644 --- a/htdocs/margin/agentMargins.php +++ b/htdocs/margin/agentMargins.php @@ -101,34 +101,14 @@ print ''; print ''; print ''; print ''; - -// Total Margin -print ''.$langs->trans("TotalMargin").''; -print ''; // set by jquery (see below) -print ''; - -// Margin Rate -if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { - print ''.$langs->trans("MarginRate").''; - print ''; // set by jquery (see below) - print ''; -} - -// Mark Rate -if (! empty($conf->global->DISPLAY_MARK_RATES)) { - print ''.$langs->trans("MarkRate").''; - print ''; // set by jquery (see below) - print ''; -} - print ""; print ''; $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.= " 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.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','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,"; +$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.= ", ".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); @@ -158,7 +138,7 @@ $sql .= " AND d.buy_price_ht IS NOT NULL"; if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0"; if ($agentid > 0) - $sql.= " GROUP BY s.rowid"; + $sql.= " GROUP BY s.rowid, u.rowid"; else $sql.= " GROUP BY u.rowid"; $sql.= " ORDER BY $sortfield $sortorder "; @@ -179,9 +159,9 @@ if ($result) print ''; if ($agentid > 0) - print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&agentid=".$agentid,'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&agentid=".$agentid,'',$sortfield,$sortorder); else - print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.lastname","","&agentid=".$agentid,'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.lastname","","&agentid=".$agentid,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&agentid=".$agentid,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&agentid=".$agentid,'align="right"',$sortfield,$sortorder); @@ -200,16 +180,29 @@ if ($result) if ($num > 0) { $var=true; + while ($i < $num /*&& $i < $conf->liste_limit*/) { $objp = $db->fetch_object($result); - $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)):''; + $pa = $objp->buying_price; + $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; - print ""; + print ""; if ($agentid > 0) { $companystatic->id=$objp->socid; $companystatic->nom=$objp->nom; @@ -220,9 +213,10 @@ if ($result) $userstatic->fetch($objp->agent); print "".$userstatic->getFullName($langs,0,0,0)."\n"; } - print "".price($objp->selling_price)."\n"; - print "".price($objp->buying_price)."\n"; - print "".price($objp->marge)."\n"; + + print "".price($pv)."\n"; + print "".price($pa)."\n"; + print "".price($marge)."\n"; if (! empty($conf->global->DISPLAY_MARGIN_RATES)) print "".(($marginRate === '')?'n/a':price($marginRate)."%")."\n"; if (! empty($conf->global->DISPLAY_MARK_RATES)) @@ -230,30 +224,10 @@ if ($result) print "\n"; $i++; - $cumul_achat += round($objp->buying_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 ''; - print ''; - print $langs->trans('Total'); - print ""; - print "".price($cumul_vente)."\n"; - print "".price($cumul_achat)."\n"; - print "".price($totalMargin)."\n"; - if (! empty($conf->global->DISPLAY_MARGIN_RATES)) - print "".(($marginRate === '')?'n/a':price($marginRate)."%")."\n"; - if (! empty($conf->global->DISPLAY_MARK_RATES)) - print "".(($markRate === '')?'n/a':price($markRate)."%")."\n"; - print "\n"; - print ""; } else @@ -265,17 +239,14 @@ $db->free($result); llxFooter(); $db->close(); + ?> + diff --git a/htdocs/margin/customerMargins.php b/htdocs/margin/customerMargins.php index f44a6887453..c42f73bb87c 100644 --- a/htdocs/margin/customerMargins.php +++ b/htdocs/margin/customerMargins.php @@ -86,7 +86,7 @@ if ($socid > 0) { { print ''.$langs->trans('ThirdPartyName').''; print ''; - $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 ''; $client = true; @@ -97,7 +97,7 @@ if ($socid > 0) { else { print ''.$langs->trans('ThirdPartyName').''; print ''; - $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 ''; } @@ -153,11 +153,11 @@ if (! empty($conf->global->DISPLAY_MARK_RATES)) { print ""; print ''; -$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.= " 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.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','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,"; +$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.= " FROM ".MAIN_DB_PREFIX."societe as s"; $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) $sql .= " AND d.buy_price_ht <> 0"; if ($client) - $sql.= " GROUP BY f.rowid"; + $sql.= " GROUP BY f.rowid, s.rowid"; else $sql.= " GROUP BY s.rowid"; $sql.= " ORDER BY $sortfield $sortorder "; @@ -189,7 +189,7 @@ if ($result) { $num = $db->num_rows($result); - print '
'; + print '
'; print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',0,0,''); $i = 0; @@ -197,11 +197,11 @@ if ($result) print ''; if (! empty($client)) { - print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&socid=".$socid,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&socid=".$socid,'align="center"',$sortfield,$sortorder); - } - else - print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&socid=".$socid,'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&socid=".$socid,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&socid=".$socid,'align="center"',$sortfield,$sortorder); + } + else + print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&socid=".$socid,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&socid=".$socid,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&socid=".$socid,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&socid=".$socid,'align="right"',$sortfield,$sortorder); @@ -219,39 +219,54 @@ if ($result) if ($num > 0) { $var=True; + while ($i < $num /*&& $i < $conf->liste_limit*/) { $objp = $db->fetch_object($result); - $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)):'' ; + $pa = $objp->buying_price; + $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; - print ""; + print ""; if ($client) { - print ''; - $invoicestatic->id=$objp->facid; - $invoicestatic->ref=$objp->facnumber; - print $invoicestatic->getNomUrl(1); - print "\n"; - print ""; - print dol_print_date($db->jdate($objp->datef),'day').""; - } - else { - $companystatic->id=$objp->socid; - $companystatic->nom=$objp->nom; - $companystatic->client=$objp->client; - print "".$companystatic->getNomUrl(1,'customer')."\n"; - } - print "".price($objp->selling_price)."\n"; - print "".price($objp->buying_price)."\n"; - print "".price($objp->marge)."\n"; + print ''; + $invoicestatic->id=$objp->facid; + $invoicestatic->ref=$objp->facnumber; + print $invoicestatic->getNomUrl(1); + print "\n"; + print ""; + print dol_print_date($db->jdate($objp->datef),'day').""; + } + else { + $companystatic->id=$objp->socid; + $companystatic->nom=$objp->nom; + $companystatic->client=$objp->client; + print "".$companystatic->getNomUrl(1,'customer')."\n"; + } + + print "".price($pv)."\n"; + print "".price($pa)."\n"; + print "".price($marge)."\n"; if (! empty($conf->global->DISPLAY_MARGIN_RATES)) print "".(($marginRate === '')?'n/a':price($marginRate)."%")."\n"; if (! empty($conf->global->DISPLAY_MARK_RATES)) print "".(($markRate === '')?'n/a':price($markRate)."%")."\n"; print "\n"; + $i++; $cumul_achat += round($objp->buying_price, $rounding); $cumul_vente += round($objp->selling_price, $rounding); @@ -261,14 +276,23 @@ if ($result) // 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)):'' ; + if ($totalMargin < 0) + { + $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 ''; if ($client) - print ''; - else - print ''; - print $langs->trans('TotalMargin').""; + print ''; + else + print ''; + print $langs->trans('TotalMargin').""; print "".price($cumul_vente)."\n"; print "".price($cumul_achat)."\n"; print "".price($totalMargin)."\n"; @@ -289,7 +313,9 @@ $db->free($result); llxFooter(); $db->close(); + ?> + \ No newline at end of file + diff --git a/htdocs/margin/tabs/productMargins.php b/htdocs/margin/tabs/productMargins.php index 55a13e83094..9b31514c7c9 100644 --- a/htdocs/margin/tabs/productMargins.php +++ b/htdocs/margin/tabs/productMargins.php @@ -128,13 +128,13 @@ if ($id > 0 || ! empty($ref)) print ''; - $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.= " 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','d.qty *-1','d.qty')." as qty,"; - $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.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid"; + $sql.= " sum(d.total_ht) as selling_price,"; + $sql.= $db->ifsql('f.type =2','sum(d.qty *-1)','sum(d.qty)')." as qty,"; + $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid,"; + $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(-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 "; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; $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"; if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0"; + $sql.= " GROUP BY f.rowid"; $sql.= " ORDER BY $sortfield $sortorder "; // TODO: calculate total to display then restore pagination //$sql.= $db->plimit($conf->liste_limit +1, $offset); @@ -193,9 +194,18 @@ if ($id > 0 || ! empty($ref)) $objp = $db->fetch_object($result); $var=!$var; - $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 ""; + if ($objp->marge < 0) + { + $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 ""; print ''; $invoicestatic->id=$objp->facid; $invoicestatic->ref=$objp->facnumber; @@ -225,8 +235,16 @@ if ($id > 0 || ! empty($ref)) // 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)):''; + if ($totalMargin < 0) + { + $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 ''; print ''.$langs->trans('TotalMargin').""; print "".price($cumul_vente)."\n"; diff --git a/htdocs/margin/tabs/thirdpartyMargins.php b/htdocs/margin/tabs/thirdpartyMargins.php index 04ac4d65d64..aead7467f83 100644 --- a/htdocs/margin/tabs/thirdpartyMargins.php +++ b/htdocs/margin/tabs/thirdpartyMargins.php @@ -130,8 +130,8 @@ if ($socid > 0) $sql.= " f.facnumber, f.total as total_ht,"; $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.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(d.qty * d.buy_price_ht *-1)','sum(d.qty * d.buy_price_ht)')." as buying_price,"; + $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.= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql.= ", ".MAIN_DB_PREFIX."facture as f"; @@ -144,7 +144,7 @@ if ($socid > 0) $sql .= " AND d.buy_price_ht IS NOT NULL"; if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $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 "; // TODO: calculate total to display then restore pagination //$sql.= $db->plimit($conf->liste_limit +1, $offset); @@ -185,12 +185,20 @@ if ($socid > 0) { $objp = $db->fetch_object($result); - $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)):'' ; - + if ($objp->marge < 0) + { + $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; - print ""; + print ""; print ''; $invoicestatic->id=$objp->facid; $invoicestatic->ref=$objp->facnumber; @@ -216,8 +224,16 @@ if ($socid > 0) // 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)):'' ; + if ($totalMargin < 0) + { + $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 ''; print ''.$langs->trans('TotalMargin').""; print "".price($cumul_vente)."\n";