'.$langs->trans("MarkRate").' | ';
@@ -131,0 +156 @@
+ */
@@ -134 +158,0 @@
-
@@ -136,103 +160,74 @@
- print '';
-
- print dol_get_fiche_end();
-
-
- if ($user->hasRight("facture", "read")) {
- $sql = "SELECT s.nom as name, s.rowid as socid, s.code_client,";
- $sql .= " f.rowid as facid, f.ref, f.total_ht,";
- $sql .= " f.datef, f.paye, f.fk_statut as statut, f.type,";
- if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
- $sql .= " sc.fk_soc, sc.fk_user,";
- }
- $sql .= " sum(d.total_ht) as selling_price,"; // may be negative or positive
- $sql .= " ".$db->ifsql('f.type = 2', -1, 1)." * sum(d.qty) as qty,"; // not always positive in case of Credit note
- $sql .= " ".$db->ifsql('f.type = 2', -1, 1)." * sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,"; // not always positive in case of Credit note
- $sql .= " ".$db->ifsql('f.type = 2', -1, 1)." * sum(abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100))) as marge"; // not always positive in case of Credit note
- $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
- $sql .= ", ".MAIN_DB_PREFIX."facture as f";
- $sql .= ", ".MAIN_DB_PREFIX."facturedet as d";
- if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
- $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- }
- $sql .= " WHERE f.fk_soc = s.rowid";
- $sql .= " AND f.fk_statut > 0";
- $sql .= " AND f.entity IN (".getEntity('invoice').")";
- $sql .= " AND d.fk_facture = f.rowid";
- $sql .= " AND d.fk_product = ".((int) $object->id);
- if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
- $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
- }
- if (!empty($socid)) {
- $sql .= " AND f.fk_soc = ".((int) $socid);
- }
- $sql .= " AND d.buy_price_ht IS NOT NULL";
- // We should not use this here. Option ForceBuyingPriceIfNull should have effect only when inserting data. Once data is recorded, it must be used as it is for report.
- // We keep it with value ForceBuyingPriceIfNull = 2 for retroactive effect but results are unpredicable.
- if (getDolGlobalInt('ForceBuyingPriceIfNull') == 2) {
- $sql .= " AND d.buy_price_ht <> 0";
- }
- $sql .= " GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut, f.type";
- if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
- $sql .= ", sc.fk_soc, sc.fk_user";
- }
- $sql .= $db->order($sortfield, $sortorder);
- // TODO: calculate total to display then restore pagination
- //$sql.= $db->plimit($conf->liste_limit +1, $offset);
- dol_syslog('margin:tabs:productMargins.php', LOG_DEBUG);
- $result = $db->query($sql);
- if ($result) {
- $num = $db->num_rows($result);
-
- print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], "&id=$object->id", $sortfield, $sortorder, '', 0, 0, '');
-
- $i = 0;
-
- print '';
- print ' ';
-
- print '';
- print_liste_field_titre("Invoice", $_SERVER["PHP_SELF"], "f.ref", "", "&id=".$object->id, '', $sortfield, $sortorder);
- print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", "&id=".$object->id, '', $sortfield, $sortorder);
- print_liste_field_titre("CustomerCode", $_SERVER["PHP_SELF"], "s.code_client", "", "&id=".$object->id, '', $sortfield, $sortorder);
- print_liste_field_titre("DateInvoice", $_SERVER["PHP_SELF"], "f.datef", "", "&id=".$object->id, '', $sortfield, $sortorder, 'left ');
- print_liste_field_titre("SellingPrice", $_SERVER["PHP_SELF"], "selling_price", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right ');
- print_liste_field_titre("BuyingPrice", $_SERVER["PHP_SELF"], "buying_price", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right ');
- print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "qty", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right ');
- print_liste_field_titre("Margin", $_SERVER["PHP_SELF"], "marge", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right ');
- if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
- print_liste_field_titre("MarginRate", $_SERVER["PHP_SELF"], "", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right ');
- }
- if (getDolGlobalString('DISPLAY_MARK_RATES')) {
- print_liste_field_titre("MarkRate", $_SERVER["PHP_SELF"], "", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right ');
- }
- print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "f.paye,f.fk_statut", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right ');
- print " \n";
-
- $cumul_achat = 0;
- $cumul_vente = 0;
- $cumul_qty = 0;
-
- if ($num > 0) {
- while ($i < $num /*&& $i < $conf->liste_limit*/) {
- $objp = $db->fetch_object($result);
-
- $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) : '';
- $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) : '';
-
- print '';
- print '';
- $invoicestatic->id = $objp->facid;
- $invoicestatic->ref = $objp->ref;
- print $invoicestatic->getNomUrl(1);
- print " | \n";
- print ''.img_object($langs->trans("ShowCompany"), "company").' '.dol_trunc($objp->name, 44).' | ';
- print "".$objp->code_client." | \n";
- print '';
- print dol_print_date($db->jdate($objp->datef), 'day')." | ";
- print ''.price(price2num($objp->selling_price, 'MT'))." | \n";
- print ''.price(price2num($objp->buying_price, 'MT'))." | \n";
- print ''.price(price2num($objp->qty, 'MT'))." | \n";
- print ''.price(price2num($objp->marge, 'MT'))." | \n";
- if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
- print "".(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%")." | \n";
+
+
+ if ($user->rights->facture->lire) {
+ $sql = "SELECT s.nom as name, s.rowid as socid, s.code_client,";
+ $sql.= " f.rowid as facid, f.ref, f.total as total_ht,";
+ $sql.= " f.datef, f.paye, f.fk_statut as statut, f.type,";
+ if (!$user->rights->societe->client->voir && !$socid) $sql.= " sc.fk_soc, sc.fk_user,";
+ $sql.= " sum(d.total_ht) as selling_price,"; // may be negative or positive
+ $sql.= " sum(d.qty) as qty,";
+ $sql.= " sum(d.qty * d.buy_price_ht) as buying_price,"; // always positive
+ $sql.= " sum(abs(d.total_ht) - (d.buy_price_ht * d.qty)) as marge" ; // always positive
+ $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
+ $sql.= ", ".MAIN_DB_PREFIX."facture as f";
+ $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
+ if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
+ $sql.= " WHERE f.fk_soc = s.rowid";
+ $sql.= " AND f.fk_statut > 0";
+ $sql.= " AND s.entity = ".$conf->entity;
+ $sql.= " AND d.fk_facture = f.rowid";
+ $sql.= " AND d.fk_product =".$object->id;
+ if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
+ if (! empty($socid)) $sql.= " AND f.fk_soc = $socid";
+ $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 s.nom, s.rowid, s.code_client, f.rowid, f.ref, f.total, f.datef, f.paye, f.fk_statut, f.type";
+ if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user";
+ $sql.= $db->order($sortfield,$sortorder);
+ // TODO: calculate total to display then restore pagination
+ //$sql.= $db->plimit($conf->liste_limit +1, $offset);
+ dol_syslog('margin:tabs:productMargins.php', LOG_DEBUG);
+ $result = $db->query($sql);
+ if ($result) {
+ $num = $db->num_rows($result);
+
+ print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&id=$object->id",$sortfield,$sortorder,'',0,0,'');
+
+ $i = 0;
+ print '';
+
+ print '';
+ print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.ref","","&id=".$object->id,'',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&id=".$object->id,'',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("CustomerCode"),$_SERVER["PHP_SELF"],"s.code_client","","&id=".$object->id,'',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&id=".$object->id,'align="center"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&id=".$object->id,'align="right"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&id=".$object->id,'align="right"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&id=".$object->id,'align="right"',$sortfield,$sortorder);
+ //print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&id=".$object->id,'align="right"',$sortfield,$sortorder);
+ print "Marge";
+ if (! empty($conf->global->DISPLAY_MARGIN_RATES))
+ print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&id=".$object->id,'align="right"',$sortfield,$sortorder);
+ if (! empty($conf->global->DISPLAY_MARK_RATES))
+ print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&id=".$object->id,'align="right"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&id=".$object->id,'align="right"',$sortfield,$sortorder);
+ print " | \n";
+
+ $cumul_achat = 0;
+ $cumul_vente = 0;
+ $cumul_qty = 0;
+ $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
+
+ if ($num > 0) {
+ $var=True;
+ /*
+ // marge en cours
+ $achat =0;
+ // pachat dans mvt
+ $sql = "SELECT price, datem FROM llx_stock_mouvement WHERE (fk_product = ".$object->id.") AND (value > 0) ORDER BY datem DESC";
+ //print " sql ".$sql;
+ $calldata = $db->query($sql);
+ $ok = 1;
+ if ($db->num_rows($calldata) == 0) $ok = 0; else {
+ $objdata = $db->fetch_object($calldata);
+ if ($objdata->price >0 ) $achat = $objdata->price; else $ok = 0;
@@ -240,2 +235,8 @@
- if (getDolGlobalString('DISPLAY_MARK_RATES')) {
- print "".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")." | \n";
+ if ($ok == 0 ) {
+ // sinon pachat dans product
+ $sql = "SELECT price, datec FROM llx_product_fournisseur_price WHERE fk_product = ".$object->id." ORDER BY datec DESC";
+ $calldata = $db->query($sql);
+ if ($db->num_rows($calldata) > 0) {
+ $objdata = $db->fetch_object($calldata);
+ $achat = $objdata->price;
+ }
@@ -243,41 +244,104 @@
- print ''.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).' | ';
- print "\n";
- $i++;
- $cumul_achat += $objp->buying_price;
- $cumul_vente += $objp->selling_price;
- $cumul_qty += $objp->qty;
- }
- }
-
- // affichage totaux marges
-
- $totalMargin = $cumul_vente - $cumul_achat;
- if ($totalMargin < 0) {
- $marginRate = ($cumul_achat != 0) ? -1 * (100 * $totalMargin / $cumul_achat) : '';
- $markRate = ($cumul_vente != 0) ? -1 * (100 * $totalMargin / $cumul_vente) : '';
- } else {
- $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) : '';
- $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) : '';
- }
- print '';
- print ''.$langs->trans('TotalMargin')." | ";
- print ''.price(price2num($cumul_vente, 'MT'))." | \n";
- print ''.price(price2num($cumul_achat, 'MT'))." | \n";
- print ''.price(price2num($cumul_qty, 'MT'))." | \n";
- print ''.price(price2num($totalMargin, 'MT'))." | \n";
- if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
- print ''.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%")." | \n";
- }
- if (getDolGlobalString('DISPLAY_MARK_RATES')) {
- print "".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")." | \n";
- }
- print ' | ';
- print " \n";
- print " ";
- print '';
- } else {
- dol_print_error($db);
- }
- $db->free($result);
- }
- }
+ //print " achat :".$achat;
+ */
+ $product = new Product($db);
+ while ($i < $num /*&& $i < $conf->liste_limit*/) {
+ $objp = $db->fetch_object($result);
+ $var=!$var;
+
+ $marginRate = ($objp->buying_price != 0)?(100 * $objp->marge / $objp->buying_price):'' ;
+ $markRate = ($objp->selling_price != 0)?(100 * $objp->marge / $objp->selling_price):'' ;
+
+ print ' ';
+ print '';
+ $invoicestatic->id=$objp->facid;
+ $invoicestatic->ref=$objp->ref;
+ print $invoicestatic->getNomUrl(1);
+ print " | \n";
+ print ''.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->name,44).' | ';
+ print "".$objp->code_client." | \n";
+ print "";
+ print dol_print_date($db->jdate($objp->datef),'day')." | ";
+ print "".price($objp->selling_price, null, null, null, null, $rounding)." | \n";
+ /*
+ print "".price($objp->buying_price, null, null, null, null, $rounding)." | \n";
+ print "".price($objp->qty, null, null, null, null, $rounding)." | \n";
+ print "".price($objp->marge, null, null, null, null, $rounding)." | \n";
+ */
+ $sql = "SELECT f.fk_product, f.qty, f.total_ht FROM llx_facturedet as f INNER JOIN llx_product AS p ON p.rowid = f.fk_product
+ WHERE f.fk_facture = ".$objp->facid." AND f.fk_product = ".$object->id;
+ $resf = $db->query($sql);
+ $objf = $db->fetch_object($resf);
+
+ // $sql2 = "SELECT * FROM llx_product_fournisseur_price as f
+ // WHERE f.datec <= '".$objp->datef."' AND f.fk_product = ".$object->id." ORDER BY f.datec DESC LIMIT 1";
+ // $query2 = $db->query($sql2);
+ // $prix_achat = $db->fetch_object($query2);
+ $achat = null;
+ $sql2 = "SELECT price, datem FROM llx_stock_mouvement WHERE (datem < '".$objp->datef."') AND (price != 0) AND (fk_product = ".$object->id.") AND (value > 0) ORDER BY datem DESC LIMIT 1";
+ $calldata2 = $db->query($sql2);
+ if ($db->num_rows($calldata2) > 0) {
+ $objdata2 = $db->fetch_object($calldata2);
+ $achat = $objdata2->price;
+ }
+ if (!$achat) {
+ $achat = $product->prix_achat($object->id, $objp->datef);
+ }
+
+ // marge
+ // $achat = $product->prix_achat($object->id, $objp->datef);
+
+ print "".number_format($achat, 2,"."," ")." | ";
+ print "".price($objp->qty, null, null, null, null, $rounding)." | \n";
+ print "".number_format(($objf->total_ht - ($achat * $objf->qty)), 2,"."," ")." | ";
+ // coeff
+ if ($achat != 0) print "".number_format(100 * ($objf->total_ht - ($achat * $objf->qty))/($achat * $objf->qty), 2,"."," ")."% | "; else print " | ";
+
+ if (! empty($conf->global->DISPLAY_MARGIN_RATES))
+ //print "".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")." | \n";
+
+ if (! empty($conf->global->DISPLAY_MARK_RATES))
+ print "".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")." | \n";
+ print ''.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).' | ';
+ print " \n";
+ $i++;
+ $cumul_achat += $objp->buying_price;
+ $cumul_vente += $objp->selling_price;
+ $cumul_qty += $objp->qty;
+ }
+ }
+
+ // affichage totaux marges
+ $var=!$var;
+ $totalMargin = $cumul_vente - $cumul_achat;
+ var_dump($cumul_vente);
+ var_dump($cumul_achat);
+ if ($totalMargin < 0)
+ {
+ $marginRate = ($cumul_achat != 0)?-1*(100 * $totalMargin / $cumul_achat):'';
+ $markRate = ($cumul_vente != 0)?-1*(100 * $totalMargin / $cumul_vente):'';
+ }
+ else
+ {
+ $marginRate = ($cumul_achat != 0)?(100 * $totalMargin / $cumul_achat):'';
+ $markRate = ($cumul_vente != 0)?(100 * $totalMargin / $cumul_vente):'';
+ }
+ print '';
+ print ''.$langs->trans('TotalMargin')." | ";
+ print ''.price($cumul_vente, null, null, null, null, $rounding)." | \n";
+ print ''.price($cumul_achat, null, null, null, null, $rounding)." | \n";
+ print ''.price($cumul_qty, null, null, null, null, $rounding)." | \n";
+ print ''.price($totalMargin, null, null, null, null, $rounding)." | \n";
+ if (! empty($conf->global->DISPLAY_MARGIN_RATES))
+ print ''.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")." | \n";
+ if (! empty($conf->global->DISPLAY_MARK_RATES))
+ print "".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")." | \n";
+ print ' | ';
+ print " \n";
+ print " ";
+ print ' ';
+ } else {
+ dol_print_error($db);
+ }
+ $db->free($result);
+ }
+ }
@@ -285 +349 @@
- dol_print_error();
+ dol_print_error();
@@ -291,3 +355,3 @@
- $("#totalMargin").html("'. price(price2num($totalMargin, 'MT')).'");
- $("#marginRate").html("'.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").'");
- $("#markRate").html("'.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'");
+ $("#totalMargin").html("'. price($totalMargin, null, null, null, null, $rounding).'");
+ $("#marginRate").html("'.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%").'");
+ $("#markRate").html("'.(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%").'");
@@ -298 +361,0 @@
-// End of page
--- /tmp/dsg/dolibarr/htdocs/margin/tabs/github_19.0.3_thirdpartyMargins.php
+++ /tmp/dsg/dolibarr/htdocs/margin/tabs/client_thirdpartyMargins.php
@@ -15 +15 @@
- * along with this program. If not, see .
+ * along with this program. If not, see .
@@ -21 +21 @@
- * \brief Page for invoice margins of a thirdparty
+ * \brief Page des marges des factures clients pour un tiers
@@ -24 +23,0 @@
-// Load Dolibarr environment
@@ -30 +29,4 @@
-$langs->loadLangs(array("companies", "bills", "products", "margins"));
+$langs->load("companies");
+$langs->load("bills");
+$langs->load("products");
+$langs->load("margins");
@@ -33,13 +35,12 @@
-$socid = GETPOST('socid', 'int');
-if (!empty($user->socid)) {
- $socid = $user->socid;
-}
-
-$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
-$sortfield = GETPOST('sortfield', 'aZ09comma');
-$sortorder = GETPOST('sortorder', 'aZ09comma');
-$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
-if (empty($page) || $page == -1) {
- $page = 0;
-} // If $page is not defined, or '' or -1
-$offset = $limit * $page;
+$socid = GETPOST('socid','int');
+if (! empty($user->societe_id)) $socid=$user->societe_id;
+$result = restrictedArea($user, 'societe','','');
+
+
+$mesg = '';
+
+$sortfield = GETPOST("sortfield",'alpha');
+$sortorder = GETPOST("sortorder",'alpha');
+$page = GETPOST("page",'int');
+if ($page == -1) { $page = 0; }
+$offset = $conf->liste_limit * $page;
@@ -48,6 +49,2 @@
-if (!$sortorder) {
- $sortorder = "DESC";
-}
-if (!$sortfield) {
- $sortfield = "f.datef";
-}
+if (! $sortorder) $sortorder="DESC";
+if (! $sortfield) $sortfield="f.datef";
@@ -56,12 +53,3 @@
-if ($socid > 0) {
- $object->fetch($socid);
-}
-
-// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
-$hookmanager->initHooks(array('thirdpartymargins', 'globalcard'));
-
-$result = restrictedArea($user, 'societe', $object->id, '');
-
-if (!$user->hasRight('margins', 'liretous')) {
- accessforbidden();
-}
+
+// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
+$hookmanager->initHooks(array('thirdpartymargins','globalcard'));
@@ -74,5 +62,3 @@
-$parameters = array('id'=>$socid);
-$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
-if ($reshook < 0) {
- setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
-}
+$parameters=array('id'=>$socid);
+$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
+if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
@@ -86 +72 @@
-$invoicestatic = new Facture($db);
+$invoicestatic=new Facture($db);
@@ -89,3 +75,208 @@
-$title = $langs->trans("ThirdParty").' - '.$langs->trans("Margins");
-if (getDolGlobalString('MAIN_HTML_TITLE') && preg_match('/thirdpartynameonly/', $conf->global->MAIN_HTML_TITLE) && $object->name) {
- $title = $object->name.' - '.$langs->trans("Files");
+$help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
+llxHeader('',$langs->trans("ThirdParty").' - '.$langs->trans("Margins"),$help_url);
+
+if ($socid > 0)
+{
+ $object = new Societe($db);
+ $object->fetch($socid);
+
+ /*
+ * Affichage onglets
+ */
+
+ $head = societe_prepare_head($object);
+
+ dol_fiche_head($head, 'margin', $langs->trans("ThirdParty"),0,'company');
+
+ print ' ';
+
+ print ''.$langs->trans('ThirdPartyName').' | ';
+ print '';
+ print $form->showrefnav($object,'socid','',($user->societe_id?0:1),'rowid','nom');
+ print ' | ';
+
+ if (! empty($conf->global->SOCIETE_USEPREFIX)) // Old not used prefix field
+ {
+ print ''.$langs->trans('Prefix').' | '.$object->prefix_comm.' | ';
+ }
+
+ if ($object->client)
+ {
+ print '';
+ print $langs->trans('CustomerCode').' | ';
+ print $object->code_client;
+ if ($object->check_codeclient() <> 0) print ' ('.$langs->trans("WrongCustomerCode").')';
+ print ' | ';
+ }
+
+ if ($object->fournisseur)
+ {
+ print '';
+ print $langs->trans('SupplierCode').' | ';
+ print $object->code_fournisseur;
+ if ($object->check_codefournisseur() <> 0) print ' ('.$langs->trans("WrongSupplierCode").')';
+ 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 distinct s.nom, s.rowid as socid, s.code_client,";
+ $sql.= " f.rowid as facid, f.facnumber, f.total as total_ht,";
+ $sql.= " f.datef, f.paye, f.fk_statut as statut, f.type,";
+ $sql.= " sum(d.total_ht) as selling_price,"; // may be negative or positive
+ $sql.= " sum(d.qty * d.buy_price_ht) as buying_price,"; // always positive
+ $sql.= " sum(abs(d.total_ht) - (d.buy_price_ht * d.qty)) as marge"; // always positive
+ $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
+ $sql.= ", ".MAIN_DB_PREFIX."facture as f";
+ $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
+ $sql.= " WHERE f.fk_soc = s.rowid";
+ $sql.= " AND f.fk_statut > 0";
+ $sql.= " AND s.entity = ".$conf->entity;
+ $sql.= " AND d.fk_facture = f.rowid";
+ $sql.= " AND f.fk_soc = $socid";
+ $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 s.nom, s.rowid, s.code_client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut, f.type";
+ $sql.= $db->order($sortfield,$sortorder);
+ // TODO: calculate total to display then restore pagination
+ //$sql.= $db->plimit($conf->liste_limit +1, $offset);
+
+ dol_syslog('margin:tabs:thirdpartyMargins.php', LOG_DEBUG);
+ $result = $db->query($sql);
+ if ($result)
+ {
+ $num = $db->num_rows($result);
+
+ print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&socid=".$object->id,$sortfield,$sortorder,'',0,0,'');
+
+ $i = 0;
+ print "";
+
+ print '';
+ print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&socid=".$_REQUEST["socid"],'',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&socid=".$_REQUEST["socid"],'align="center"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
+ if (! empty($conf->global->DISPLAY_MARGIN_RATES))
+ print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
+ if (! empty($conf->global->DISPLAY_MARK_RATES))
+ print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
+ print " \n";
+
+ $cumul_achat = 0;
+ $cumul_vente = 0;
+
+ $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
+ $product = new Product($db);
+ if ($num > 0) {
+ $var=True;
+ while ($i < $num /*&& $i < $conf->liste_limit*/) {
+ $objp = $db->fetch_object($result);
+ $var=!$var;
+ print "";
+ 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')." | ";
+ // selling price integre le port
+ //print "".price($objp->selling_price, null, null, null, null, $rounding)." | \n";
+ //print "".price(($objp->type == 2 ? -1 : 1) * $objp->buying_price, null, null, null, null, $rounding)." | \n";
+ // CAI calcul de marge
+ $sql = "SELECT f.fk_product, f.qty, f.total_ht FROM llx_facturedet as f INNER JOIN llx_product AS p ON p.rowid = f.fk_product
+ WHERE f.fk_facture = ".$objp->facid." AND f.product_type = 0";
+ //print " sql ".$sql;
+ $resf = $db->query($sql);
+ $achat = 0;
+ $ht = 0;
+ while ($objf = $db->fetch_object($resf)) {
+ $w = $product->prix_achat($objf->fk_product, $objp->datef);
+ // avoir qty > 0 et total_ht < 0
+ if ($objf->total_ht > 0) $achat = $achat + ($w * $objf->qty); else $achat = $achat - ($w * $objf->qty);
+ $ht += $objf->total_ht; // montant negatif pour les avoirs
+ }
+ $cumul_achat += $achat;
+ print "".number_format($ht, 2,"."," ")." | ";
+ print "".number_format($achat, 2,"."," ")." | ";
+ // marge
+ print "".number_format(($ht - $achat), 2,"."," ")." | ";
+ // coeff
+ if ($achat != 0) print "".number_format(100 * ($ht - $achat )/$achat, 2,"."," ")."% | "; else print " | ";
+
+
+ /*
+ $marginRate = ($objp->buying_price != 0)?(100 * $objp->marge / $objp->buying_price):'' ;
+ $markRate = ($objp->selling_price != 0)?(100 * $objp->marge / $objp->selling_price):'' ;
+ print "".price($objp->marge, null, null, null, null, $rounding)." | \n";
+ if (! empty($conf->global->DISPLAY_MARGIN_RATES))
+ print "".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")." | \n";
+ if (! empty($conf->global->DISPLAY_MARK_RATES))
+ print "".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")." | \n";
+ */
+ print ''.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).' | ';
+ print " \n";
+ $i++;
+ //$cumul_achat += $objp->buying_price;
+ $cumul_vente += $ht;
+ }
+ }
+
+ // affichage totaux marges
+ $var=!$var;
+ $totalMargin = $cumul_vente - $cumul_achat;
+ if ($totalMargin < 0)
+ {
+ $marginRate = ($cumul_achat != 0)?-1*(100 * $totalMargin / $cumul_achat):'';
+ $markRate = ($cumul_vente != 0)?-1*(100 * $totalMargin / $cumul_vente):'';
+ }
+ else
+ {
+ $marginRate = ($cumul_achat != 0)?(100 * $totalMargin / $cumul_achat):'';
+ $markRate = ($cumul_vente != 0)?(100 * $totalMargin / $cumul_vente):'';
+ }
+ // totaux
+ print '';
+ print ''.$langs->trans('TotalMargin')." | ";
+ print "".price($cumul_vente, null, null, null, null, $rounding)." | \n";
+ print "".price($cumul_achat, null, null, null, null, $rounding)." | \n";
+ print "".price($totalMargin, null, null, null, null, $rounding)." | \n";
+ if (! empty($conf->global->DISPLAY_MARGIN_RATES))
+ print "".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")." | \n";
+ if (! empty($conf->global->DISPLAY_MARK_RATES))
+ print "".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")." | \n";
+
+ print ' | ';
+ print " \n";
+ }
+ else
+ {
+ dol_print_error($db);
+ }
+ print " ";
+ print ' ';
+ $db->free($result);
@@ -93,207 +284,2 @@
-$help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
-llxHeader('', $title, $help_url);
-
-$totalMargin = 0;
-$marginRate = '';
-$markRate = '';
-if ($socid > 0) {
- $object = new Societe($db);
- $object->fetch($socid);
-
- /*
- * Affichage onglets
- */
-
- $head = societe_prepare_head($object);
-
- print dol_get_fiche_head($head, 'margin', $langs->trans("ThirdParty"), -1, 'company');
-
- $linkback = ''.$langs->trans("BackToList").'';
-
- dol_banner_tab($object, 'socid', $linkback, ($user->socid ? 0 : 1), 'rowid', 'nom');
-
- print '';
-
- print ' ';
- print ' ';
-
- // Type Prospect/Customer/Supplier
- print ''.$langs->trans('NatureOfThirdParty').' | ';
- print $object->getTypeUrl(1);
- print ' | ';
-
- if ($object->client) {
- print '';
- print $langs->trans('CustomerCode').' | ';
- print showValueWithClipboardCPButton(dol_escape_htmltag($object->code_client));
- $tmpcheck = $object->check_codeclient();
- if ($tmpcheck != 0 && $tmpcheck != -5) {
- print ' ('.$langs->trans("WrongCustomerCode").')';
- }
- print ' | ';
- }
-
- if (((isModEnabled("fournisseur") && $user->hasRight('fournisseur', 'lire') && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) || (isModEnabled("supplier_order") && $user->hasRight('supplier_order', 'lire')) || (isModEnabled("supplier_invoice") && $user->hasRight('supplier_invoice', 'lire'))) && $object->fournisseur) {
- print '';
- print $langs->trans('SupplierCode').' | ';
- print showValueWithClipboardCPButton(dol_escape_htmltag($object->code_fournisseur));
- $tmpcheck = $object->check_codefournisseur();
- if ($tmpcheck != 0 && $tmpcheck != -5) {
- print ' ('.$langs->trans("WrongSupplierCode").')';
- }
- print ' | ';
- }
-
- // Total Margin
- print ''.$langs->trans("TotalMargin").' | ';
- print ''; // set by jquery (see below)
- print ' | ';
-
- // Margin Rate
- if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
- print ''.$langs->trans("MarginRate").' | ';
- print ''; // set by jquery (see below)
- print ' | ';
- }
-
- // Mark Rate
- if (getDolGlobalString('DISPLAY_MARK_RATES')) {
- print ''.$langs->trans("MarkRate").' | ';
- print ''; // set by jquery (see below)
- print ' | ';
- }
-
- print " ";
-
- print ' ';
- print '';
-
- print dol_get_fiche_end();
-
- print ' ';
-
- $sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client,";
- $sql .= " f.rowid as facid, f.ref, f.total_ht,";
- $sql .= " f.datef, f.paye, f.fk_statut as statut, f.type,";
- $sql .= " sum(d.total_ht) as selling_price,"; // may be negative or positive
- $sql .= " sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,"; // always positive
- $sql .= " sum(abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100))) as marge"; // always positive
- $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
- $sql .= ", ".MAIN_DB_PREFIX."facture as f";
- $sql .= ", ".MAIN_DB_PREFIX."facturedet as d";
- $sql .= " WHERE f.fk_soc = s.rowid";
- $sql .= " AND f.fk_statut > 0";
- $sql .= " AND f.entity IN (".getEntity('invoice').")";
- $sql .= " AND d.fk_facture = f.rowid";
- $sql .= " AND f.fk_soc = $socid";
- $sql .= " AND d.buy_price_ht IS NOT NULL";
- // We should not use this here. Option ForceBuyingPriceIfNull should have effect only when inserting data. Once data is recorded, it must be used as it is for report.
- // We keep it with value ForceBuyingPriceIfNull = 2 for retroactive effect but results are unpredicable.
- if (getDolGlobalInt('ForceBuyingPriceIfNull') == 2) {
- $sql .= " AND d.buy_price_ht <> 0";
- }
- $sql .= " GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut, f.type";
- $sql .= $db->order($sortfield, $sortorder);
- // TODO: calculate total to display then restore pagination
- //$sql.= $db->plimit($conf->liste_limit +1, $offset);
-
- dol_syslog('margin:tabs:thirdpartyMargins.php', LOG_DEBUG);
- $result = $db->query($sql);
- if ($result) {
- $num = $db->num_rows($result);
-
- print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], "&socid=".$object->id, $sortfield, $sortorder, '', $num, $num, '');
-
- $i = 0;
- print ''; // You can use div-table-responsive-no-min if you dont need reserved height for your table
- print " ";
-
- print '';
- print_liste_field_titre("Invoice", $_SERVER["PHP_SELF"], "f.ref", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder);
- print_liste_field_titre("DateInvoice", $_SERVER["PHP_SELF"], "f.datef", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'center ');
- print_liste_field_titre("SoldAmount", $_SERVER["PHP_SELF"], "selling_price", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
- print_liste_field_titre("PurchasedAmount", $_SERVER["PHP_SELF"], "buying_price", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
- print_liste_field_titre("Margin", $_SERVER["PHP_SELF"], "marge", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
- if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
- print_liste_field_titre("MarginRate", $_SERVER["PHP_SELF"], "", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
- }
- if (getDolGlobalString('DISPLAY_MARK_RATES')) {
- print_liste_field_titre("MarkRate", $_SERVER["PHP_SELF"], "", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
- }
- print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "f.paye,f.fk_statut", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
- print " \n";
-
- $cumul_achat = 0;
- $cumul_vente = 0;
-
- if ($num > 0) {
- while ($i < $num /*&& $i < $conf->liste_limit*/) {
- $objp = $db->fetch_object($result);
-
- $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) : '';
- $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) : '';
-
- $sign = '';
- if ($objp->type == Facture::TYPE_CREDIT_NOTE) {
- $sign = '-';
- }
-
- print '';
- print '';
- $invoicestatic->id = $objp->facid;
- $invoicestatic->ref = $objp->ref;
- print $invoicestatic->getNomUrl(1);
- print " | \n";
- print "";
- print dol_print_date($db->jdate($objp->datef), 'day')." | ";
- print "".price(price2num($objp->selling_price, 'MT'))." | \n";
- print "".price(price2num(($objp->type == 2 ? -1 : 1) * $objp->buying_price, 'MT'))." | \n";
- print "".$sign.price(price2num($objp->marge, 'MT'))." | \n";
- if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
- print "".(($marginRate === '') ? 'n/a' : $sign.price(price2num($marginRate, 'MT'))."%")." | \n";
- }
- if (getDolGlobalString('DISPLAY_MARK_RATES')) {
- print "".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")." | \n";
- }
- print ''.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).' | ';
- print " \n";
- $i++;
- $cumul_vente += $objp->selling_price;
- $cumul_achat += ($objp->type == 2 ? -1 : 1) * $objp->buying_price;
- }
- }
-
- // affichage totaux marges
-
- $totalMargin = $cumul_vente - $cumul_achat;
- if ($totalMargin < 0) {
- $marginRate = ($cumul_achat != 0) ? -1 * (100 * $totalMargin / $cumul_achat) : '';
- $markRate = ($cumul_vente != 0) ? -1 * (100 * $totalMargin / $cumul_vente) : '';
- } else {
- $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) : '';
- $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) : '';
- }
-
- // Total
- print '';
- print ''.$langs->trans('TotalMargin')." | ";
- print "".price(price2num($cumul_vente, 'MT'))." | \n";
- print "".price(price2num($cumul_achat, 'MT'))." | \n";
- print "".price(price2num($totalMargin, 'MT'))." | \n";
- if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
- print "".(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%")." | \n";
- }
- if (getDolGlobalString('DISPLAY_MARK_RATES')) {
- print "".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")." | \n";
- }
- print ' | ';
- print " \n";
- } else {
- dol_print_error($db);
- }
- print " ";
- print ' ';
-
- print ' ';
- $db->free($result);
-} else {
+else
+{
@@ -307,3 +293,3 @@
- $("#totalMargin").html("'. price(price2num($totalMargin, 'MT')).'");
- $("#marginRate").html("'.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").'");
- $("#markRate").html("'.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'");
+ $("#totalMargin").html("'. price($totalMargin, null, null, null, null, $rounding).'");
+ $("#marginRate").html("'.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%").'");
+ $("#markRate").html("'.(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%").'");
@@ -314 +299,0 @@
-// End of page
|