--- /tmp/dsg/dolibarr/htdocs/margin/tabs/github_productMargins.php +++ /tmp/dsg/dolibarr/htdocs/margin/tabs/client_productMargins.php @@ -12,7 +12,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ /** @@ -26,247 +26,338 @@ require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; -$langs->loadLangs(array("companies", "bills", "products", "margins")); +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("margins"); $id = GETPOST('id', 'int'); $ref = GETPOST('ref', 'alpha'); -$action = GETPOST('action', 'aZ09'); -$confirm = GETPOST('confirm', 'alpha'); +$action=GETPOST('action','alpha'); +$confirm=GETPOST('confirm','alpha'); // Security check -$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : '')); -$fieldtype = (!empty($ref) ? 'ref' : 'rowid'); -if (!empty($user->socid)) $socid = $user->socid; -$result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype); -if (empty($user->rights->margins->liretous)) accessforbidden(); +$fieldvalue = (! empty($id) ? $id : (! empty($ref) ? $ref : '')); +$fieldtype = (! empty($ref) ? 'ref' : 'rowid'); +if (! empty($user->societe_id)) $socid=$user->societe_id; +$result=restrictedArea($user,'produit|service',$fieldvalue,'product&product','','',$fieldtype); $object = new Product($db); -$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit; -$sortfield = GETPOST("sortfield", 'alpha'); -$sortorder = GETPOST("sortorder", 'alpha'); -$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; +$mesg = ''; + +$sortfield = GETPOST("sortfield",'alpha'); +$sortorder = GETPOST("sortorder",'alpha'); +$page = GETPOST("page",'int'); +if ($page == -1) { $page = 0; } +$offset = $conf->liste_limit * $page; $pageprev = $page - 1; $pagenext = $page + 1; -if (!$sortorder) $sortorder = "DESC"; -if (!$sortfield) $sortfield = "f.datef"; +if (! $sortorder) $sortorder="DESC"; +if (! $sortfield) $sortfield="f.datef"; /* * View */ -$invoicestatic = new Facture($db); +$invoicestatic=new Facture($db); $form = new Form($db); -if ($id > 0 || !empty($ref)) + +if ($id > 0 || ! empty($ref)) { $result = $object->fetch($id, $ref); - $title = $langs->trans('ProductServiceCard'); - $helpurl = ''; - $shortlabel = dol_trunc($object->label, 16); - if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) - { - $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Card'); - $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:Módulo_Productos'; - } - if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) - { - $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Card'); - $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:Módulo_Servicios'; - } - - llxHeader('', $title, $helpurl); + llxHeader("","",$langs->trans("CardProduct".$object->type)); /* * En mode visu */ if ($result > 0) { - $head = product_prepare_head($object); - $titre = $langs->trans("CardProduct".$object->type); - $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product'); - print dol_get_fiche_head($head, 'margin', $titre, -1, $picto); - - $linkback = ''.$langs->trans("BackToList").''; - - dol_banner_tab($object, 'ref', $linkback, ($user->socid ? 0 : 1), 'ref'); - - - print '
'; - - print '
'; - print ''; + $head=product_prepare_head($object); + $titre=$langs->trans("CardProduct".$object->type); + $picto=($object->type== Product::TYPE_SERVICE?'service':'product'); + dol_fiche_head($head, 'margin', $titre, 0, $picto); + + print '
'; + + // Reference + print ''; + print ''; + print ''; + + // Libelle + print ''; + print ''; + + // Status (to sell) + print ''; + + // Status (to buy) + print ''; // Total Margin - print ''; - + print "\n"; + // marge en cours + $sql = "SELECT price, datec FROM llx_product_fournisseur_price WHERE fk_product = ".$id." ORDER BY datec DESC"; + $calldata = $db->query($sql); + if ($db->num_rows($calldata) > 0) { + $objdata = $db->fetch_object($calldata); + $achat1 = $objdata->price; + $date1 = $objdata->datec; + } + $sql = "SELECT price, datem FROM llx_stock_mouvement WHERE (fk_product = ".$id.") AND (value > 0) ORDER BY datem DESC"; + $calldata = $db->query($sql); + if ($db->num_rows($calldata) > 0) { + $objdata = $db->fetch_object($calldata); + $achat2 = $objdata->price; + $date2 = $objdata->datec; + } + print "\n"; // Margin Rate - if (!empty($conf->global->DISPLAY_MARGIN_RATES)) { - print '"; print ''; // set by jquery (see below) print ''; } - + /* // Mark Rate - if (!empty($conf->global->DISPLAY_MARK_RATES)) { + if (! empty($conf->global->DISPLAY_MARK_RATES)) { print ''; } + */ print "
'.$langs->trans("Ref").''; + print $form->showrefnav($object,'ref','',1,'ref'); + print '
'.$langs->trans("Label").''.$object->label.'
'.$langs->trans("Status").' ('.$langs->trans("Sell").')'; + print $object->getLibStatut(2,0); + print '
'.$langs->trans("Status").' ('.$langs->trans("Buy").')'; + print $object->getLibStatut(2,1); + print '
'.$langs->trans("TotalMargin").''; + //print '
'.$langs->trans("TotalMargin").''; + print '
Marges réalisées'; print ''; // set by jquery (see below) print '
Prix de vente ".number_format($object->price,2,".","")."
Marge en cours"; //achat1 : ".$achat1." achat2: ".$achat2." marge :"; + //if ($date1 > $date2) print number_format($object->price - $achat1,2,".",""); else print number_format($object->price - $achat2,2,".",""); + $marge = 0; + if ($achat2 == 0) { + print number_format($object->price - $achat1,2,".",""); + //if ($object->price != 0) $marge = number_format(100 * ($object->price - $achat1)/$object->price,2,".",""); + if ($achat1 != 0) $marge = number_format(100 * ($object->price - $achat1)/$achat1,2,".",""); + } else { + print number_format($object->price - $achat2,2,".",""); + //if ($object->price != 0) $marge = number_format(100 * ($object->price - $achat2)/$object->price,2,".",""); + if ($achat2 != 0) $marge = number_format(100 * ($object->price - $achat2)/$achat2,2,".",""); + } + print " (prix fournisseur : ".number_format($achat1,2,".","")." - entrée stock à: ".number_format($achat2,2,".","").")
'.$langs->trans("MarginRate").''; + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { + //print '
'.$langs->trans("MarginRate").''; + print " Taux de marge sur dernier prix d'achat".$marge."%
'.$langs->trans("MarkRate").''; print ''; // set by jquery (see below) print '
"; - print '
'; - print '
'; - - print dol_get_fiche_end(); - - - 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 .= " ".$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->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 f.entity IN (".getEntity('invoice').")"; - $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 ''; - 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"], "d.qty", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right '); - print_liste_field_titre("Margin", $_SERVER["PHP_SELF"], "marge", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right '); - if (!empty($conf->global->DISPLAY_MARGIN_RATES)) - print_liste_field_titre("MarginRate", $_SERVER["PHP_SELF"], "", "", "&id=".$object->id, '', $sortfield, $sortorder, 'right '); - if (!empty($conf->global->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 '\n"; - print ''; - print "\n"; - print ""; - print "\n"; - print "\n"; - print "\n"; - print "\n"; - if (!empty($conf->global->DISPLAY_MARGIN_RATES)) - print "\n"; - if (!empty($conf->global->DISPLAY_MARK_RATES)) - print "\n"; - print ''; - 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 '"; - print '\n"; - print '\n"; - print '\n"; - print '\n"; - if (!empty($conf->global->DISPLAY_MARGIN_RATES)) - print '\n"; - if (!empty($conf->global->DISPLAY_MARK_RATES)) - print "\n"; - print ''; - print "\n"; - print "
'; - $invoicestatic->id = $objp->facid; - $invoicestatic->ref = $objp->ref; - print $invoicestatic->getNomUrl(1); - print "'.img_object($langs->trans("ShowCompany"), "company").' '.dol_trunc($objp->name, 44).'".$objp->code_client.""; - print dol_print_date($db->jdate($objp->datef), 'day')."".price(price2num($objp->selling_price, 'MT'))."".price(price2num($objp->buying_price, 'MT'))."".price(price2num($objp->qty, 'MT'))."".price(price2num($objp->marge, 'MT'))."".(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%")."".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")."'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).'
'.$langs->trans('TotalMargin')."'.price(price2num($cumul_vente, 'MT'))."'.price(price2num($cumul_achat, 'MT'))."'.price(price2num($cumul_qty, 'MT'))."'.price(price2num($totalMargin, 'MT'))."'.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%")."".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")." 
"; - print '
'; - } else { - dol_print_error($db); - } - $db->free($result); - } - } + + + 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 "\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; + } + 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; + } + } + //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 '\n"; + print ''; + print "\n"; + print ""; + print "\n"; + /* + print "\n"; + print "\n"; + print "\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 ""; + print "\n"; + print ""; + // coeff + if ($achat != 0) print ""; else print ""; + + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) + //print "\n"; + + if (! empty($conf->global->DISPLAY_MARK_RATES)) + print "\n"; + print ''; + 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 '"; + print '\n"; + print '\n"; + print '\n"; + print '\n"; + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) + print '\n"; + if (! empty($conf->global->DISPLAY_MARK_RATES)) + print "\n"; + print ''; + print "\n"; + 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 "
'; + $invoicestatic->id=$objp->facid; + $invoicestatic->ref=$objp->ref; + print $invoicestatic->getNomUrl(1); + print "'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->name,44).'".$objp->code_client.""; + print dol_print_date($db->jdate($objp->datef),'day')."".price($objp->selling_price, null, null, null, null, $rounding)."".price($objp->buying_price, null, null, null, null, $rounding)."".price($objp->qty, null, null, null, null, $rounding)."".price($objp->marge, null, null, null, null, $rounding)."".number_format($achat, 2,"."," ")."".price($objp->qty, null, null, null, null, $rounding)."".number_format(($objf->total_ht - ($achat * $objf->qty)), 2,"."," ")."".number_format(100 * ($objf->total_ht - ($achat * $objf->qty))/($achat * $objf->qty), 2,"."," ")."%".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'
'.$langs->trans('TotalMargin')."'.price($cumul_vente, null, null, null, null, $rounding)."'.price($cumul_achat, null, null, null, null, $rounding)."'.price($cumul_qty, null, null, null, null, $rounding)."'.price($totalMargin, null, null, null, null, $rounding)."'.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")." 
"; + print '
'; + } else { + dol_print_error($db); + } + $db->free($result); + } + } } else { - dol_print_error(); + dol_print_error(); } print ' '; -// End of page llxFooter(); $db->close(); --- /tmp/dsg/dolibarr/htdocs/margin/tabs/github_thirdpartyMargins.php +++ /tmp/dsg/dolibarr/htdocs/margin/tabs/client_thirdpartyMargins.php @@ -12,13 +12,13 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ /** * \file htdocs/margin/tabs/thirdpartyMargins.php * \ingroup product margins - * \brief Page for invoice margins of a thirdparty + * \brief Page des marges des factures clients pour un tiers */ require '../../main.inc.php'; @@ -26,38 +26,41 @@ require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; -$langs->loadLangs(array("companies", "bills", "products", "margins")); +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("margins"); // Security check -$socid = GETPOST('socid', 'int'); -if (!empty($user->socid)) $socid = $user->socid; -$result = restrictedArea($user, 'societe', '', ''); - - -$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit; -$sortfield = GETPOST("sortfield", 'alpha'); -$sortorder = GETPOST("sortorder", 'alpha'); -$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; $pageprev = $page - 1; $pagenext = $page + 1; -if (!$sortorder) $sortorder = "DESC"; -if (!$sortfield) $sortfield = "f.datef"; +if (! $sortorder) $sortorder="DESC"; +if (! $sortfield) $sortfield="f.datef"; $object = new Societe($db); -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')); + +// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array +$hookmanager->initHooks(array('thirdpartymargins','globalcard')); /* * Actions */ -$parameters = array('id'=>$socid); -$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks +$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'); @@ -66,207 +69,220 @@ * View */ -$invoicestatic = new Facture($db); +$invoicestatic=new Facture($db); $form = new Form($db); -$title = $langs->trans("ThirdParty").' - '.$langs->trans("Margins"); -if (!empty($conf->global->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('', $title, $help_url); +$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); - - /* + $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 ''; + $head = societe_prepare_head($object); + + dol_fiche_head($head, 'margin', $langs->trans("ThirdParty"),0,'company'); + + print '
'; + + print ''; + print ''; + + if (! empty($conf->global->SOCIETE_USEPREFIX)) // Old not used prefix field + { + print ''; + } if ($object->client) { - print ''; } - if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) && $object->fournisseur && !empty($user->rights->fournisseur->lire)) - { - print ''; - } - - // Total Margin - print ''; - - // Margin Rate - if (!empty($conf->global->DISPLAY_MARGIN_RATES)) { - print ''; - } - - // Mark Rate - if (!empty($conf->global->DISPLAY_MARK_RATES)) { - print ''; - } - - print "
'.$langs->trans('ThirdPartyName').''; + print $form->showrefnav($object,'socid','',($user->societe_id?0:1),'rowid','nom'); + print '
'.$langs->trans('Prefix').''.$object->prefix_comm.'
'; + print '
'; print $langs->trans('CustomerCode').''; print $object->code_client; - $tmpcheck = $object->check_codeclient(); - if ($tmpcheck != 0 && $tmpcheck != -5) { - print ' ('.$langs->trans("WrongCustomerCode").')'; - } + if ($object->check_codeclient() <> 0) print ' ('.$langs->trans("WrongCustomerCode").')'; print '
'; - print $langs->trans('SupplierCode').''; - print $object->code_fournisseur; - $tmpcheck = $object->check_codefournisseur(); - if ($tmpcheck != 0 && $tmpcheck != -5) { - print ' ('.$langs->trans("WrongSupplierCode").')'; - } - print '
'.$langs->trans("TotalMargin").''; - print ''; // set by jquery (see below) - print '
'.$langs->trans("MarginRate").''; - print ''; // set by jquery (see below) - print '
'.$langs->trans("MarkRate").''; - print ''; // set by jquery (see below) - 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 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 * (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"; - 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"; - $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 (!empty($conf->global->DISPLAY_MARGIN_RATES)) - print_liste_field_titre("MarginRate", $_SERVER["PHP_SELF"], "", "", "&socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right '); - if (!empty($conf->global->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 = '-'; + if ($object->fournisseur) + { + print ''; + } + + // Total Margin + print ''; + + // Margin Rate + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { + print ''; + } + + // Mark Rate + if (! empty($conf->global->DISPLAY_MARK_RATES)) { + print ''; + } + + print "
'; + print $langs->trans('SupplierCode').''; + print $object->code_fournisseur; + if ($object->check_codefournisseur() <> 0) print ' ('.$langs->trans("WrongSupplierCode").')'; + print '
'.$langs->trans("TotalMargin").''; + print ''; // set by jquery (see below) + print '
'.$langs->trans("MarginRate").''; + print ''; // set by jquery (see below) + print '
'.$langs->trans("MarkRate").''; + print ''; // set by jquery (see below) + 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 '\n"; + print ""; + // selling price integre le port + //print "\n"; + //print "\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 } - - print ''; - print '\n"; - print ""; - print "\n"; - print "\n"; - print "\n"; - if (!empty($conf->global->DISPLAY_MARGIN_RATES)) - print "\n"; - if (!empty($conf->global->DISPLAY_MARK_RATES)) - print "\n"; - print ''; - 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 '"; - print "\n"; - print "\n"; - print "\n"; - if (!empty($conf->global->DISPLAY_MARGIN_RATES)) - print "\n"; - if (!empty($conf->global->DISPLAY_MARK_RATES)) - print "\n"; - print ''; - print "\n"; - } else { - dol_print_error($db); - } - print "
'; + $invoicestatic->id=$objp->facid; + $invoicestatic->ref=$objp->facnumber; + print $invoicestatic->getNomUrl(1); + print ""; + print dol_print_date($db->jdate($objp->datef),'day')."".price($objp->selling_price, null, null, null, null, $rounding)."".price(($objp->type == 2 ? -1 : 1) * $objp->buying_price, null, null, null, null, $rounding)."
'; - $invoicestatic->id = $objp->facid; - $invoicestatic->ref = $objp->ref; - print $invoicestatic->getNomUrl(1); - print ""; - print dol_print_date($db->jdate($objp->datef), 'day')."".price(price2num($objp->selling_price, 'MT'))."".price(price2num(($objp->type == 2 ? -1 : 1) * $objp->buying_price, 'MT'))."".$sign.price(price2num($objp->marge, 'MT'))."".(($marginRate === '') ? 'n/a' : $sign.price(price2num($marginRate, 'MT'))."%")."".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")."'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).'
'.$langs->trans('TotalMargin')."".price(price2num($cumul_vente, 'MT'))."".price(price2num($cumul_achat, 'MT'))."".price(price2num($totalMargin, 'MT'))."".(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%")."".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")." 
"; - print ''; - - print '
'; - $db->free($result); -} else { + $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); +} +else +{ dol_print_error('', 'Parameter socid not defined'); } @@ -274,13 +290,12 @@ print ' '; -// End of page llxFooter(); $db->close();