diff --git a/htdocs/install/mysql/tables/llx_facturedet.sql b/htdocs/install/mysql/tables/llx_facturedet.sql index 949dee1933c..77204c29873 100644 --- a/htdocs/install/mysql/tables/llx_facturedet.sql +++ b/htdocs/install/mysql/tables/llx_facturedet.sql @@ -38,7 +38,7 @@ create table llx_facturedet remise real DEFAULT 0, -- Montant calcule de la remise % sur PU HT (exemple 20) fk_remise_except integer NULL, -- Lien vers table des remises fixes subprice double(24,8), -- P.U. HT (exemple 100) - price double(24,8), -- P.U. HT apres remise % de ligne + price double(24,8), -- Deprecated (Do not use) total_ht double(24,8), -- Total HT de la ligne toute quantite et incluant remise ligne et globale total_tva double(24,8), -- Total TVA de la ligne toute quantite et incluant remise ligne et globale total_localtax1 double(24,8) DEFAULT 0, -- Total LocalTax1 for total quantity of line diff --git a/htdocs/margin/agentMargins.php b/htdocs/margin/agentMargins.php index 6ca0879a88a..0e953422ef2 100644 --- a/htdocs/margin/agentMargins.php +++ b/htdocs/margin/agentMargins.php @@ -45,7 +45,7 @@ if (! $sortfield) if ($agentid > 0) $sortfield="s.nom"; else - $sortfield="u.name"; + $sortfield="u.lastname"; } $page = GETPOST("page",'int'); if ($page == -1) { $page = 0; } @@ -125,10 +125,10 @@ print ""; print ''; $sql = "SELECT s.nom, s.rowid as socid, s.code_client, s.client, u.rowid as agent,"; -$sql.= " u.login, u.name, u.firstname,"; +$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.price + d.buy_price_ht) * d.qty)','sum((d.price - d.buy_price_ht) * d.qty)')." as marge" ; +$sql.= $db->ifsql('f.type =2','sum(((d.total_ht/d.qty) + d.buy_price_ht) * d.qty)','sum(((d.total_ht/d.qty) - 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); @@ -160,11 +160,12 @@ if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPr if ($agentid > 0) $sql.= " GROUP BY s.rowid"; else - $sql.= " GROUP BY u.rowid"; + $sql.= " GROUP BY u.rowid, s.nom, s.rowid, s.code_client, s.client, u.login, u.lastname, u.firstname, f.type "; $sql.= " ORDER BY $sortfield $sortorder "; // TODO: calculate total to display then restore pagination //$sql.= $db->plimit($conf->liste_limit +1, $offset); +dol_syslog('margin::agentMargins.php sql='.$sql,LOG_DEBUG); $result = $db->query($sql); if ($result) { @@ -180,7 +181,7 @@ if ($result) if ($agentid > 0) print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&agentid=".$agentid,'align="center"',$sortfield,$sortorder); else - print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.name","","&agentid=".$agentid,'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.lastname","","&agentid=".$agentid,'align="center"',$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); diff --git a/htdocs/margin/customerMargins.php b/htdocs/margin/customerMargins.php index a07d3192e9b..5fb491ea5c3 100644 --- a/htdocs/margin/customerMargins.php +++ b/htdocs/margin/customerMargins.php @@ -157,7 +157,7 @@ $sql = "SELECT distinct 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.price + d.buy_price_ht) * d.qty)','sum((d.price - d.buy_price_ht) * d.qty)')." as marge," ; +$sql.= $db->ifsql('f.type =2','sum(((d.total_ht/d.qty) + d.buy_price_ht) * d.qty)','sum(((d.total_ht/d.qty) - 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"; @@ -178,7 +178,7 @@ if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPr if ($client) $sql.= " GROUP BY f.rowid"; else - $sql.= " GROUP BY s.rowid"; + $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, f.facnumber, f.total, f.datef, f.paye, f.fk_statut, f.rowid "; $sql.= " ORDER BY $sortfield $sortorder "; // TODO: calculate total to display then restore pagination //$sql.= $db->plimit($conf->liste_limit +1, $offset); diff --git a/htdocs/margin/productMargins.php b/htdocs/margin/productMargins.php index e5896d1f1b3..d2e3198b5ee 100644 --- a/htdocs/margin/productMargins.php +++ b/htdocs/margin/productMargins.php @@ -159,7 +159,7 @@ $sql = "SELECT DISTINCT d.fk_product, p.label, p.rowid, p.fk_product_type, p.ref $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.price + d.buy_price_ht) * d.qty)','sum((d.price - d.buy_price_ht) * d.qty)')." as marge," ; +$sql.= $db->ifsql('f.type =2','sum(((d.total_ht/d.qty) + d.buy_price_ht) * d.qty)','sum(((d.total_ht/d.qty) - 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."product as p"; @@ -186,7 +186,7 @@ else $sql.= " ORDER BY $sortfield $sortorder "; // TODO: calculate total to display then restore pagination //$sql.= $db->plimit($conf->liste_limit +1, $offset); -dol_syslog('margin::productMargin.php sql='.$sql,LOG_DEBUG); +dol_syslog('margin::productMargins.php sql='.$sql,LOG_DEBUG); $result = $db->query($sql); if ($result) { diff --git a/htdocs/margin/tabs/productMargins.php b/htdocs/margin/tabs/productMargins.php index 326efc526d2..8eee225b42d 100644 --- a/htdocs/margin/tabs/productMargins.php +++ b/htdocs/margin/tabs/productMargins.php @@ -133,7 +133,7 @@ if ($id > 0 || ! empty($ref)) $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.price + d.buy_price_ht) * d.qty)','((d.price - d.buy_price_ht) * d.qty)')." as marge," ; + $sql.= $db->ifsql('f.type =2','(((d.total_ht/d.qty) + d.buy_price_ht) * d.qty)','(((d.total_ht/d.qty) - d.buy_price_ht) * d.qty)')." as marge," ; $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid"; if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user "; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; @@ -153,7 +153,7 @@ if ($id > 0 || ! empty($ref)) $sql.= " ORDER BY $sortfield $sortorder "; // TODO: calculate total to display then restore pagination //$sql.= $db->plimit($conf->liste_limit +1, $offset); - + dol_syslog('margin:tabs:productMargins.php sql='.$sql,LOG_DEBUG); $result = $db->query($sql); if ($result) { diff --git a/htdocs/margin/tabs/thirdpartyMargins.php b/htdocs/margin/tabs/thirdpartyMargins.php index b91955d28c0..fc319b17054 100644 --- a/htdocs/margin/tabs/thirdpartyMargins.php +++ b/htdocs/margin/tabs/thirdpartyMargins.php @@ -131,7 +131,7 @@ if ($socid > 0) $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.price + d.buy_price_ht) * d.qty)','sum((d.price - d.buy_price_ht) * d.qty)')." as marge," ; + $sql.= $db->ifsql('f.type =2','sum(((d.total_ht/d.qty) + d.buy_price_ht) * d.qty)','sum(((d.total_ht/d.qty) - 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"; @@ -149,6 +149,7 @@ if ($socid > 0) // TODO: calculate total to display then restore pagination //$sql.= $db->plimit($conf->liste_limit +1, $offset); + dol_syslog('margin:tabs:thirdpartyMargins.php sql='.$sql,LOG_DEBUG); $result = $db->query($sql); if ($result) {