From 1de9dc15bfc672c2e24f42697cefb0a7f58e3527 Mon Sep 17 00:00:00 2001 From: Christophe Battarel Date: Wed, 18 Jul 2012 15:52:15 +0200 Subject: [PATCH] add margin module --- .../core/tpl/freeproductline_create.tpl.php | 45 ++- htdocs/core/tpl/freeproductline_edit.tpl.php | 8 + htdocs/core/tpl/freeproductline_view.tpl.php | 14 +- htdocs/marges/admin/marges.php | 261 +++++++++++++++ htdocs/marges/agentMargins.php | 274 ++++++++++++++++ htdocs/marges/css/marges.css | 3 + htdocs/marges/customerMargins.php | 287 +++++++++++++++++ htdocs/marges/img/marges.png | Bin 0 -> 1851 bytes htdocs/marges/img/object_marges.png | Bin 0 -> 579 bytes .../includes/modules/modMarges.class.php | 227 ++++++++++++++ htdocs/marges/index.php | 26 ++ htdocs/marges/langs/fr_FR/marges.lang | 45 +++ htdocs/marges/lib/marges.lib.php | 108 +++++++ htdocs/marges/productMargins.php | 296 ++++++++++++++++++ htdocs/marges/tabs/productMargins.php | 262 ++++++++++++++++ htdocs/marges/tabs/thirdpartyMargins.php | 247 +++++++++++++++ 16 files changed, 2098 insertions(+), 5 deletions(-) create mode 100644 htdocs/marges/admin/marges.php create mode 100644 htdocs/marges/agentMargins.php create mode 100644 htdocs/marges/css/marges.css create mode 100644 htdocs/marges/customerMargins.php create mode 100644 htdocs/marges/img/marges.png create mode 100644 htdocs/marges/img/object_marges.png create mode 100644 htdocs/marges/includes/modules/modMarges.class.php create mode 100644 htdocs/marges/index.php create mode 100644 htdocs/marges/langs/fr_FR/marges.lang create mode 100644 htdocs/marges/lib/marges.lib.php create mode 100644 htdocs/marges/productMargins.php create mode 100644 htdocs/marges/tabs/productMargins.php create mode 100644 htdocs/marges/tabs/thirdpartyMargins.php diff --git a/htdocs/core/tpl/freeproductline_create.tpl.php b/htdocs/core/tpl/freeproductline_create.tpl.php index 18f76aa105d..334429cc7b5 100644 --- a/htdocs/core/tpl/freeproductline_create.tpl.php +++ b/htdocs/core/tpl/freeproductline_create.tpl.php @@ -1,6 +1,7 @@ * Copyright (C) 2010-2011 Laurent Destailleur + * Copyright (C) 2012 Christophe Battarel * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,7 +33,19 @@ trans('PriceUHT'); ?> trans('Qty'); ?> trans('ReductionShort'); ?> -   +marges->enabled) { +?> + trans('BuyingPrice'); ?> +global->DISPLAY_MARGIN_RATES) + $colspan++; + if($conf->global->DISPLAY_MARK_RATES) + $colspan++; +} +?> +  
"> "> % - +marges->enabled) { +?> + "> +global->DISPLAY_MARGIN_RATES) + $colspan++; + if($conf->global->DISPLAY_MARK_RATES) + $colspan++; +} +?> + -service->enabled && $dateSelector) { ?> + +service->enabled && $dateSelector) { +if(! empty($conf->global->MAIN_VIEW_LINE_NUMBER) + $colspan = 10; +else + $colspan = 9; +if ($conf->marges->enabled) { + if($conf->global->DISPLAY_MARGIN_RATES) + $colspan++; + if($conf->global->DISPLAY_MARK_RATES) + $colspan++; +} +?> > - global->MAIN_VIEW_LINE_NUMBER) ? ' colspan="10"' : ' colspan="9"'); ?>> + trans('ServiceLimitedDuration').' '.$langs->trans('From').' '; echo $form->select_date('','date_start',$conf->global->MAIN_USE_HOURMIN_IN_DATE_RANGE,$conf->global->MAIN_USE_HOURMIN_IN_DATE_RANGE,1,"addproduct"); diff --git a/htdocs/core/tpl/freeproductline_edit.tpl.php b/htdocs/core/tpl/freeproductline_edit.tpl.php index ab4cc989c70..6b067951401 100644 --- a/htdocs/core/tpl/freeproductline_edit.tpl.php +++ b/htdocs/core/tpl/freeproductline_edit.tpl.php @@ -1,6 +1,7 @@ * Copyright (C) 2010-2012 Laurent Destailleur + * Copyright (C) 2012 Christophe Battarel * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -73,6 +74,13 @@ +marges->enabled) { +?> + + ">
"> diff --git a/htdocs/core/tpl/freeproductline_view.tpl.php b/htdocs/core/tpl/freeproductline_view.tpl.php index f3824738054..3e090c5c92e 100644 --- a/htdocs/core/tpl/freeproductline_view.tpl.php +++ b/htdocs/core/tpl/freeproductline_view.tpl.php @@ -1,6 +1,7 @@ * Copyright (C) 2010-2011 Laurent Destailleur + * Copyright (C) 2012 Christophe Battarel * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -82,7 +83,18 @@ remise_percent,$langs); ?>   - + marges->enabled) { + ?> + pa_ht); ?> + global->DISPLAY_MARGIN_RATES) {?> + pa_ht == 0)?'n/a':price($line->marge_tx).'%'); ?> + global->DISPLAY_MARK_RATES) {?> + marque_tx).'%'; ?> + special_code == 3) { ?> trans('Option'); ?> diff --git a/htdocs/marges/admin/marges.php b/htdocs/marges/admin/marges.php new file mode 100644 index 00000000000..f0abd2e9d02 --- /dev/null +++ b/htdocs/marges/admin/marges.php @@ -0,0 +1,261 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * or see http://www.gnu.org/ + */ + +/** + * \file /marges/admin/marges.php + * \ingroup marges + * \brief Page to setup advanced extra fields module + * \version $Id:$ + */ + +$res=@include("../../main.inc.php"); // For root directory +if (! $res) $res=@include("../../../main.inc.php"); // For "custom" directory + +require_once("../lib/marges.lib.php"); +require_once(DOL_DOCUMENT_ROOT."/lib/admin.lib.php"); + +$langs->load("admin"); +$langs->load("marges@marges"); + +if (!$user->admin) +accessforbidden(); + + +/* + * Action + */ +if (preg_match('/set_(.*)/',$action,$reg)) +{ + $code=$reg[1]; + if (dolibarr_set_const($db, $code, 1, 'yesno', 0, '', $conf->entity) > 0) + { + Header("Location: ".$_SERVER["PHP_SELF"]); + exit; + } + else + { + dol_print_error($db); + } +} + +if (preg_match('/del_(.*)/',$action,$reg)) +{ + $code=$reg[1]; + if (dolibarr_del_const($db, $code, $conf->entity) > 0) + { + Header("Location: ".$_SERVER["PHP_SELF"]); + exit; + } + else + { + dol_print_error($db); + } +} + +if ($_POST["action"] == 'remises') +{ + if (dolibarr_set_const($db, 'MARGIN_METHODE_FOR_DISCOUNT', $_POST['MARGIN_METHODE_FOR_DISCOUNT'], 'chaine', 0, '', $conf->entity) > 0) + { + $conf->global->MARGIN_METHODE_FOR_DISCOUNT = $_POST['MARGIN_METHODE_FOR_DISCOUNT']; + } + else + { + dol_print_error($db); + } +} + +if ($_POST["action"] == 'typemarges') +{ + if (dolibarr_set_const($db, 'MARGIN_TYPE', $_POST['MARGIN_TYPE'], 'chaine', 0, '', $conf->entity) > 0) + { + $conf->global->MARGIN_METHODE_FOR_DISCOUNT = $_POST['MARGIN_TYPE']; + } + else + { + dol_print_error($db); + } +} + +/* + * View + */ + +llxHeader('',$langs->trans("margesSetup")); + + +$linkback=''.$langs->trans("BackToModuleList").''; +print_fiche_titre($langs->trans("margesSetup"),$linkback,'setup'); + + +$head = marges_admin_prepare_head($adh); + +dol_fiche_head($head, 'parameters', $langs->trans("marges"), 0, 'company'); + +print "
"; + + +print_fiche_titre($langs->trans("MemberMainOptions"),'',''); +print ''; +print ''; +print ''; + +print ''."\n"; +print ''."\n"; +print ''; + +$var=true; +$form = new Form($db); + +// GLOBAL DISCOUNT MANAGEMENT +$var=!$var; +print ""; +print ''; +print ""; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; + +// DISPLAY MARGIN RATES +$var=!$var; +print ''; +print ''; + + +print ''; +print ''; +print ''; + +// DISPLAY MARK RATES +$var=!$var; +print ''; +print ''; + + +print ''; +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; + +// GLOBAL DISCOUNT MANAGEMENT +$var=!$var; +print ""; +print ''; +print ""; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; + +print '
'.$langs->trans("Description").''.$langs->trans("Value").''.$langs->trans("Details").'
'.$langs->trans("MARGIN_TYPE").''.$langs->trans('MargeBrute'); +print ' global->MARGIN_TYPE == '1') + print 'checked '; +print '/>
'.$langs->trans('MargeNette'); +print ' global->MARGIN_TYPE == '2') + print 'checked '; +print '/>'; +print '
'; +print ''; +print ''.$langs->trans('MARGIN_TYPE_DETAILS').'
'.$langs->trans("DisplayMarginRates").''; +if ($conf->use_javascript_ajax) +{ + print ajax_constantonoff('DISPLAY_MARGIN_RATES'); +} +else +{ + if($conf->global->DISPLAY_MARGIN_RATES == 0) + { + print ''.img_picto($langs->trans("Disabled"),'off').''; + } + else if($conf->global->DISPLAY_MARGIN_RATES == 1) + { + print ''.img_picto($langs->trans("Enabled"),'on').''; + } +} +print ''.$langs->trans('MarginRate').' = '.$langs->trans('Margin').' / '.$langs->trans('BuyingPrice').'
'.$langs->trans("DisplayMarkRates").''; +if ($conf->use_javascript_ajax) +{ + print ajax_constantonoff('DISPLAY_MARK_RATES'); +} +else +{ + if($conf->global->DISPLAY_MARK_RATES == 0) + { + print ''.img_picto($langs->trans("Disabled"),'off').''; + } + else if($conf->global->DISPLAY_MARK_RATES == 1) + { + print ''.img_picto($langs->trans("Enabled"),'on').''; + } +} +print ''.$langs->trans('MarkRate').' = '.$langs->trans('Margin').' / '.$langs->trans('SellingPrice').'
'.$langs->trans("ForceBuyingPriceIfNull").''; +if ($conf->use_javascript_ajax) +{ + print ajax_constantonoff('ForceBuyingPriceIfNull'); +} +else +{ + if($conf->global->ForceBuyingPriceIfNull == 0) + { + print ''.img_picto($langs->trans("Disabled"),'off').''; + } + else if($conf->global->ForceBuyingPriceIfNull == 1) + { + print ''.img_picto($langs->trans("Enabled"),'on').''; + } +} +print ''.$langs->trans('ForceBuyingPriceIfNullDetails').'
'.$langs->trans("MARGIN_METHODE_FOR_DISCOUNT").''; +print ''; +print ''; +print ''; +print ''.$langs->trans('MARGIN_METHODE_FOR_DISCOUNT_DETAILS').'
'; +print '
'; + + + +$db->close(); + +llxFooter('$Date: 2011/07/31 22:23:21 $ - $Revision: 1.6 $'); +?> diff --git a/htdocs/marges/agentMargins.php b/htdocs/marges/agentMargins.php new file mode 100644 index 00000000000..98067830d5b --- /dev/null +++ b/htdocs/marges/agentMargins.php @@ -0,0 +1,274 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 . + */ + +/** + * \file htdocs/custom/marges/agentMargins.php + * \ingroup marges + * \brief Page des marges par agent commercial + * \version $Id: facture.php,v 1.84 2011/08/08 16:07:47 eldy Exp $ + */ + +require("../../main.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/lib/company.lib.php"); +require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php"); +require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php"); +require_once(DOL_DOCUMENT_ROOT_ALT."/marges/lib/marges.lib.php"); + +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("marges@marges"); + +// Security check +$agentid = isset($_REQUEST["agentid"])?$_REQUEST["agentid"]:''; + +$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 (!empty($_POST['startdatemonth'])) + $startdate = date('Y-m-d', dol_mktime(12, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear'])); +if (!empty($_POST['enddatemonth'])) + $enddate = date('Y-m-d', dol_mktime(12, 0, 0, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear'])); + +/* + * View + */ + +$userstatic = new User($db); +$companystatic = new Societe($db); +$invoicestatic=new Facture($db); + +$form = new Form($db); + +llxHeader('',$langs->trans("Margins").' - '.$langs->trans("Agents")); + +$text=$langs->trans("Margins"); +print_fiche_titre($text); + +// Show tabs +$head=marges_prepare_head($user); +$titre=$langs->trans("Margins"); +$picto='marges@marges'; +dol_fiche_head($head, 'agentMargins', $titre, 0, $picto); + +print '
'; +print ''; + +if ($agentid > 0) { + + print ''; + print ''; + + if (! $sortorder) $sortorder="ASC"; + if (! $sortfield) $sortfield="s.nom"; +} +else { + print ''; + print ''; + if (! $sortorder) $sortorder="ASC"; + if (! $sortfield) $sortfield="u.login"; +} + +// Date début +print ''; +print ''; +print ''; +print ''; +print ''; + +// Total Margin +print ''; + +// Margin Rate +if ($conf->global->DISPLAY_MARGIN_RATES) { + print ''; +} + +// Mark Rate +if ($conf->global->DISPLAY_MARK_RATES) { + print ''; +} + +print "
'.$langs->trans('CommercialAgent').''; + print $form->select_dolusers($selected=$agentid,$htmlname='agentid',$show_empty=1,$exclude='',$disabled=0,$include='',$enableonly=''); + print '
'.$langs->trans('CommercialAgent').''; + print $form->select_dolusers($selected='',$htmlname='agentid',$show_empty=1,$exclude='',$disabled=0,$include='',$enableonly=''); + print '
'.$langs->trans('StartDate').''; +$form->select_date($startdate,'startdate','','',1,"sel",1,1); +print ''.$langs->trans('EndDate').''; +$form->select_date($enddate,'enddate','','',1,"sel",1,1); +print ''; +print ''; +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, s.client, sc.fk_user as agent,"; +$sql.= " u.login,"; +$sql.= " f.facnumber, f.total as total_ht,"; +$sql.= " sum(d.subprice * d.qty * (1 - d.remise_percent / 100)) as selling_price,"; +$sql.= " sum(d.pa_ht * d.qty) as buying_price, sum(((d.subprice * (1 - d.remise_percent / 100)) - d.pa_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"; +$sql.= ", ".MAIN_DB_PREFIX."facturedet as d"; +$sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; +$sql.= ", ".MAIN_DB_PREFIX."user as u"; +$sql.= " WHERE f.fk_soc = s.rowid"; +$sql.= " AND sc.fk_soc = f.fk_soc"; +$sql.= " AND sc.fk_user = u.rowid"; +$sql.= " AND f.fk_statut > 0"; +$sql.= " AND s.entity = ".$conf->entity; +$sql.= " AND d.fk_facture = f.rowid"; +if ($agentid > 0) + $sql.= " AND sc.fk_user = $agentid"; +if (!empty($startdate)) + $sql.= " AND f.datef >= '".$startdate."'"; +if (!empty($enddate)) + $sql.= " AND f.datef <= '".$enddate."'"; +if ($agentid > 0) + $sql.= " GROUP BY s.rowid"; +else + $sql.= " GROUP BY sc.fk_user"; +$sql.= " ORDER BY $sortfield $sortorder "; +$sql.= $db->plimit($conf->liste_limit +1, $offset); + +$result = $db->query($sql); +if ($result) +{ + $num = $db->num_rows($result); + + print '
'; + print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&socid=$societe->id",$sortfield,$sortorder,'',$num,0,''); + + $i = 0; + print ""; + + print ''; + if ($agentid > 0) + print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&agentid=".$_REQUEST["agentid"],'align="center"',$sortfield,$sortorder); + else + print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.login","","&agentid=".$_REQUEST["agentid"],'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&agentid=".$_REQUEST["agentid"],'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buyng_price","","&agentid=".$_REQUEST["agentid"],'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&agentid=".$_REQUEST["agentid"],'align="right"',$sortfield,$sortorder); + if ($conf->global->DISPLAY_MARGIN_RATES) + print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"d.marge_tx","","&agentid=".$_REQUEST["agentid"],'align="right"',$sortfield,$sortorder); + if ($conf->global->DISPLAY_MARK_RATES) + print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"d.marque_tx","","&agentid=".$_REQUEST["agentid"],'align="right"',$sortfield,$sortorder); + print "\n"; + + $cumul_achat = 0; + $cumul_vente = 0; + $cumul_qty = 0; + 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)):0 ; + $markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price ,5)):0 ; + + $var=!$var; + + print ""; + if ($agentid > 0) { + $companystatic->id=$objp->socid; + $companystatic->nom=$objp->nom; + $companystatic->client=$objp->client; + print "\n"; + } + else { + $userstatic->id=$objp->agent; + $userstatic->login=$objp->login; + print "\n"; + } + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($conf->global->DISPLAY_MARK_RATES) + print "\n"; + print "\n"; + $i++; + $cumul_achat += $objp->buying_price; + $cumul_vente += $objp->selling_price; + } + } + + // affichage totaux marges + $var=!$var; + $totalMargin = $cumul_vente - $cumul_achat; + $marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):0 ; + $markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):0 ; + print ''; + if ($client) + print '"; + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($conf->global->DISPLAY_MARK_RATES) + print "\n"; + print "\n"; + + print "
".$companystatic->getNomUrl(1,'customer')."".$userstatic->getLoginUrl(1)."".price($objp->selling_price)."".price($objp->buying_price)."".price($objp->marge)."".price($marginRate)."%".price($markRate)."%
'; + else + print ''; + print $langs->trans('TotalMargin')."".price($cumul_vente)."".price($cumul_achat)."".price($totalMargin)."".price($marginRate)."%".price($markRate)."%
"; +} +else +{ + dol_print_error($db); +} +$db->free($result); + +$db->close(); + +llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $'); +?> + \ No newline at end of file diff --git a/htdocs/marges/css/marges.css b/htdocs/marges/css/marges.css new file mode 100644 index 00000000000..c804c801a24 --- /dev/null +++ b/htdocs/marges/css/marges.css @@ -0,0 +1,3 @@ +div.mainmenu.margins { + background : url('../img/marges.png') no-repeat 20px 0; +} \ No newline at end of file diff --git a/htdocs/marges/customerMargins.php b/htdocs/marges/customerMargins.php new file mode 100644 index 00000000000..afb923525b4 --- /dev/null +++ b/htdocs/marges/customerMargins.php @@ -0,0 +1,287 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 . + */ + +/** + * \file htdocs/custom/marges/customerMargins.php + * \ingroup marges + * \brief Page des marges par client + * \version $Id: facture.php,v 1.84 2011/08/08 16:07:47 eldy Exp $ + */ + +require("../../main.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/lib/company.lib.php"); +require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php"); +require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php"); +require_once(DOL_DOCUMENT_ROOT_ALT."/marges/lib/marges.lib.php"); + +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("marges@marges"); + +// Security check +$socid = isset($_REQUEST["socid"])?$_REQUEST["socid"]:''; +if ($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 (!empty($_POST['startdatemonth'])) + $startdate = date('Y-m-d', dol_mktime(12, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear'])); +if (!empty($_POST['enddatemonth'])) + $enddate = date('Y-m-d', dol_mktime(12, 0, 0, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear'])); + +/* + * View + */ + +$companystatic = new Societe($db); +$invoicestatic=new Facture($db); + +$form = new Form($db); + +llxHeader('',$langs->trans("Margins").' - '.$langs->trans("Clients")); + +$text=$langs->trans("Margins"); +print_fiche_titre($text); + +// Show tabs +$head=marges_prepare_head($user); +$titre=$langs->trans("Margins"); +$picto='marges@marges'; +dol_fiche_head($head, 'customerMargins', $titre, 0, $picto); + +print '
'; +print ''; + +$client = null; +if ($socid > 0) { + + $societe = new Societe($db, $socid); + $societe->fetch($socid); + + if ($societe->client) + { + print ''; + print ''; + + $client = true; + if (! $sortorder) $sortorder="DESC"; + if (! $sortfield) $sortfield="f.datef"; + } +} +if (!$client) { + print ''; + print ''; + if (! $sortorder) $sortorder="ASC"; + if (! $sortfield) $sortfield="s.nom"; +} + +// Date début +print ''; +print ''; +print ''; +print ''; +print ''; + +// Total Margin +print ''; + +// Margin Rate +if ($conf->global->DISPLAY_MARGIN_RATES) { + print ''; +} + +// Mark Rate +if ($conf->global->DISPLAY_MARK_RATES) { + print ''; +} + +print "
'.$langs->trans('ThirdPartyName').''; + $form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,$socid,'socid', $filter='client=1',$showempty=1, $showtype=0, $forcecombo=1); + print '
'.$langs->trans('ThirdPartyName').''; + $form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,null,'socid', $filter='client=1',$showempty=1, $showtype=0, $forcecombo=1); + print '
'.$langs->trans('StartDate').''; +$form->select_date($startdate,'startdate','','',1,"sel",1,1); +print ''.$langs->trans('EndDate').''; +$form->select_date($enddate,'enddate','','',1,"sel",1,1); +print ''; +print ''; +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, s.client,"; +$sql.= " f.facnumber, f.total as total_ht,"; +$sql.= " sum(d.subprice * d.qty * (1 - d.remise_percent / 100)) as selling_price,"; +$sql.= " sum(d.pa_ht * d.qty) as buying_price, sum(((d.subprice * (1 - d.remise_percent / 100)) - d.pa_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"; +$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"; +if ($client) + $sql.= " AND f.fk_soc = $socid"; +if (!empty($startdate)) + $sql.= " AND f.datef >= '".$startdate."'"; +if (!empty($enddate)) + $sql.= " AND f.datef <= '".$enddate."'"; +if ($client) + $sql.= " GROUP BY f.rowid"; +else + $sql.= " GROUP BY s.rowid"; +$sql.= " ORDER BY $sortfield $sortorder "; +$sql.= $db->plimit($conf->liste_limit +1, $offset); + +$result = $db->query($sql); +if ($result) +{ + $num = $db->num_rows($result); + + print '
'; + print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&socid=$societe->id",$sortfield,$sortorder,'',$num,0,''); + + $i = 0; + print ""; + + print ''; + if ($client) { + 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); + } + else + print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&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"],"buyng_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 ($conf->global->DISPLAY_MARGIN_RATES) + print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"d.marge_tx","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder); + if ($conf->global->DISPLAY_MARK_RATES) + print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"d.marque_tx","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder); + print "\n"; + + $cumul_achat = 0; + $cumul_vente = 0; + $cumul_qty = 0; + 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)):0 ; + $markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price ,5)):0 ; + + $var=!$var; + + print ""; + if ($client) { + print '\n"; + print ""; + } + else { + $companystatic->id=$objp->socid; + $companystatic->nom=$objp->nom; + $companystatic->client=$objp->client; + print "\n"; + } + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($conf->global->DISPLAY_MARK_RATES) + print "\n"; + print "\n"; + $i++; + $cumul_achat += $objp->buying_price; + $cumul_vente += $objp->selling_price; + } + } + + // affichage totaux marges + $var=!$var; + $totalMargin = $cumul_vente - $cumul_achat; + $marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):0 ; + $markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):0 ; + print ''; + if ($client) + print '"; + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($conf->global->DISPLAY_MARK_RATES) + print "\n"; + print "\n"; + + print "
'; + $invoicestatic->id=$objp->facid; + $invoicestatic->ref=$objp->facnumber; + print $invoicestatic->getNomUrl(1); + print ""; + print dol_print_date($db->jdate($objp->datef),'day')."".$companystatic->getNomUrl(1,'customer')."".price($objp->selling_price)."".price($objp->buying_price)."".price($objp->marge)."".price($marginRate)."%".price($markRate)."%
'; + else + print ''; + print $langs->trans('TotalMargin')."".price($cumul_vente)."".price($cumul_achat)."".price($totalMargin)."".price($marginRate)."%".price($markRate)."%
"; +} +else +{ + dol_print_error($db); +} +$db->free($result); + +$db->close(); + +llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $'); +?> + \ No newline at end of file diff --git a/htdocs/marges/img/marges.png b/htdocs/marges/img/marges.png new file mode 100644 index 0000000000000000000000000000000000000000..4746f0fc7798c5e9341eacd320f00214a0cf887f GIT binary patch literal 1851 zcmV-B2gLY^P)oVY zDi`e)Qprs&Pz#8(6$K=q1*av2#Om6P6Mw9|>-Fw>XLfdG4i~!#74@c#InwByIrH&* z&hMRf-Xqwiy?*WbY2YI80+1g}H-MQze|<3b&Y+)p>C9t|?cViSO$-6hg{;Tni4w)E zhiMvE%3!Ur1;9qjrx(RE+CIG~VapHM3PL^sIIp!{&{{X*IG%~4sB!V!shNiYI5b|O z+38}N3d4{%mcq3amiq6xAtknDAOPD`Yy}~0KP2o$)SDfGZjZH%7WIt=%PZBHSAP8B z7ynmF4YMZJnnpY5Qwo6;0wD#iWulaUV=0tj+;_)R64NlSErqKT6P^nQrp8Cu*l6(6 zYj2&{PG=~9l#(#GfiOBGv4g-y8V+%k#V|~wcmye93`1f_iIft$_ zxV1pw``hXa2M`h=TNK7zAOU>3ehZzf5{40eGbV{8f)q-+m{tnUE#TN$(z1jm!Amsr zi>q|Q-Y}Wr0EQF@1QHVfjmB{e0Nj)w+@eU)i+`w^c zlrk`s!f~DZ4lwXQqsixY0UCiqh=INk2r-=G0U?m&atD9|0K}0`6#E3hIzcC-7d1(A z2c;xRDQw$DCju!I5)*+y>(4gf0a+mEHdY9mRgelT6^50QMW@cXwuyZ>BKR$g&x8%F_eXq$&<+z zF&vx8@h<}ctuPjXXtF)n0WXuMn=j$FTck2sW@i@Z^?Fn)70$i%GB^JEHnwf! zrt^F$Utqkt$XE7^vgz0aTBFfODF!LM0|)ZiG~?xCNafH9Vjln6Gx$M3c4VAXCX18K zv%YeN?rM!MWNP?VUk9M!q?r8qt7zR{$j$(SU~P4of8L%YNfJh<9wlk5;&~p&j~>F# zWO?rS3k1EmUy_$s(1|8lui_<|L~GD`P$P_o2hbYaY?0F5n9X_>+fDJ&hYJLLhf1Zw z^WS@!-~Rdc6pKZK=`b;Mg!trfviSl^SZLjcxL2ee8X(aLo+T;m9mP6ykos24Q_ozW zzP?In;x@LM!pjy|S*_CQbTRA{vmd_K7nT;!efNg|7>0pi43&A11Da}emGwIx6NVw9 zQ^)D7%~39w8O_@lwI-N#2Y4#nQW+dk@v1gh{x`SE(N|~5u zvNM3*R)d_a8JQTR-j$sC`qOl}5tH+Cc$pmK@hMu38q14|c-aDT|N2M&Ic;$E!Vmgk zG)=5n50iN?fFE>Oo~sasA-VBsZoc<6*=!cevM@(RdFsipqHK?PD`2zLK^ltIW}UDX zqmzCXn3jn(Tz`9jIPSl|&W=!WvxI&dG)+u5!-2z( zQc5c(_fFHrzMqDcWs&HI{1J6J9dr^?&bjnrn_ebOH;NfQ@;F*+_D(;6n@Te}d6@QQ zo#mw^GKDg?Z(c`hO*a-ieg6As=mQMjdTtL;uW!8bi&uYjx?CzSIX*@%lcqdUWNf5B zoFwc@+w`IY=aG~2qL`zJChCP)wnw{Jr_%{Y1|bkY7=}XuhVB%8^vcijz=^@}7_c8W zz3&r1qay{%r2W%d5;U%%hWJ{>tS`2mWt>2Y{U(Kur$% p(*RM_n{PGhKe=-G(rep2_aA=tCs>aR@GJlT002ovPDHLkV1m{eb36b5 literal 0 HcmV?d00001 diff --git a/htdocs/marges/img/object_marges.png b/htdocs/marges/img/object_marges.png new file mode 100644 index 0000000000000000000000000000000000000000..13e4b92c9ce2e0a3698d31fcfc2b82704653f7a0 GIT binary patch literal 579 zcmV-J0=)f+P)AcnU72+nI~k7O&wbPhKp(25S7yLez;ORgKES18>^qZMxkS#t4;4mP!Tkxe+YuKR~p# z$@oMS%NT}+&HxFcfQVpOE|z7b`l*-e*o5sSUdH9q`!{4z zY}=+hGKO=k6tl2Go9vMSK*{xkcH6fG93;^_SAQs`< z{(cHnFI|}=th_KaaqN?Qc zCjh-}k1uO$6!RW|XLIq=Wef@DrfbBaWOD(c3Aube1@!k|XJ_aB{QRpKpjs>h)nXxF zV!T4J5a9bh)6?g%EknKjAqAw@>R4{yeRvJ19-WzA_asS(wl*I>ee~caz)y}P@RPTh R<>vqZ002ovPDHLkV1h?{2Q~lz literal 0 HcmV?d00001 diff --git a/htdocs/marges/includes/modules/modMarges.class.php b/htdocs/marges/includes/modules/modMarges.class.php new file mode 100644 index 00000000000..fcfc6856a5a --- /dev/null +++ b/htdocs/marges/includes/modules/modMarges.class.php @@ -0,0 +1,227 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/** \defgroup mymodule Module MyModule + * \brief Example of a module descriptor. + * Such a file must be copied into htdocs/includes/module directory. + */ + +/** + * \file htdocs/includes/modules/modMyModule.class.php + * \ingroup mymodule + * \brief Description and activation file for module MyModule + * \version $Id: modMyModule.class.php,v 1.26 2008/12/15 18:27:00 eldy Exp $ + */ +include_once(DOL_DOCUMENT_ROOT ."/includes/modules/DolibarrModules.class.php"); + + +/** \class modMyModule + * \brief Description and activation class for module MyModule + */ +class modMarges extends DolibarrModules +{ + /** + * \brief Constructor. Define names, constants, directories, boxes, permissions + * \param DB Database handler + */ + function modMarges($DB) + { + $this->db = $DB; + + // Id for module (must be unique). + // Use here a free id (See in Home -> System information -> Dolibarr for list of used modules id). + $this->numero = 59000; + // Key text used to identify module (for permissions, menus, etc...) + $this->rights_class = 'Marges'; + + // Family can be 'crm','financial','hr','projects','products','ecm','technic','other' + // It is used to group modules in module setup page + $this->family = "financial"; + // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module) + $this->name = mb_ereg_replace('^mod','',get_class($this), "i"); + // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module) + $this->description = "Gestion des marges"; + // Possible values for version are: 'development', 'experimental', 'dolibarr' or version + $this->version = '2.0'; + // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase) + $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name); + // Where to store the module in setup page (0=common,1=interface,2=other) + $this->special = 0; + // Name of png file (without png) used for this module. + // Png file must be in theme/yourtheme/img directory under name object_pictovalue.png. + $this->picto='marges@marges'; + + // Data directories to create when module is enabled. + $this->dirs = array(); + //$this->dirs[0] = DOL_DATA_ROOT.'/Marges'; + //$this->dirs[1] = DOL_DATA_ROOT.'/mymodule/temp; + + // Relative path to module style sheet if exists. Example: '/mymodule/mycss.css'. + $this->style_sheet = '/custom/marges/css/marges.css'; + + // Config pages. Put here list of php page names stored in admmin directory used to setup module. + $this->config_page_url = array("marges.php@marges"); + + // Dependencies + $this->depends = array("modPropale", "modProduct"); // List of modules id that must be enabled if this module is enabled + $this->requiredby = array(); // List of modules id to disable if this one is disabled + $this->phpmin = array(4,1); // Minimum version of PHP required by module + $this->need_dolibarr_version = array(3,1); // Minimum version of Dolibarr required by module + $this->langfiles = array("marges@marges"); + + // Constants + $this->const = array(); // List of particular constants to add when module is enabled + //$this->const = array( 0=>array('MAIN_MODULE_MARGES_HOOKS', 'chaine', 'propalcard', 'Hooks list for displaying Marges data on entity lists', 0, 'current', 1) ); + + // New pages on tabs + $this->tabs = array( + 'product:+marges:Marges:marges@marges:/marges/tabs/productMargins.php?id=__ID__', + 'thirdparty:+marges:Marges:marges@marges:/marges/tabs/thirdpartyMargins.php?socid=__ID__', + ); + + + // Boxes + $this->boxes = array(); // List of boxes + $r=0; + + // Add here list of php file(s) stored in includes/boxes that contains class to show a box. + // Example: + //$this->boxes[$r][1] = "myboxa.php"; + //$r++; + //$this->boxes[$r][1] = "myboxb.php"; + //$r++; + + + // Permissions + $this->rights = array(); // Permission array used by this module + $r=0; + + // Add here list of permission defined by an id, a label, a boolean and two constant strings. + // Example: + // $this->rights[$r][0] = 2000; // Permission id (must not be already used) + // $this->rights[$r][1] = 'Permision label'; // Permission label + // $this->rights[$r][3] = 1; // Permission by default for new user (0/1) + // $this->rights[$r][4] = 'level1'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2) + // $this->rights[$r][5] = 'level2'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2) + // $r++; + + + // Main menu entries + $this->menu = array(); // List of menus to add + $r = 0; + + // left menu entry + $this->menu[$r]=array( 'fk_menu'=>0, // Put 0 if this is a top menu + 'type'=>'top', // This is a Top menu entry + 'titre'=>'Margins', + 'mainmenu'=>'margins', + 'leftmenu'=>'1', // Use 1 if you also want to add left menu entries using this descriptor. Use 0 if left menu entries are defined in a file pre.inc.php (old school). + 'url'=>'/marges/index.php', + 'langs'=>'marges@marges', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. + 'position'=>100, + 'enabled'=>'1', // Define condition to show or hide menu entry. Use '$conf->monmodule->enabled' if entry must be visible if module is enabled. + 'perms'=>'1', // Use 'perms'=>'$user->rights->monmodule->level1->level2' if you want your menu with a permission rules + 'target'=>'', + 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both + $r++; + + // top menu entry + $this->menu[$r]=array( 'fk_menu'=>'r=0', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry) + 'type'=>'left', // This is a Left menu entry + 'titre'=>'ProductMargins', + 'mainmenu'=>'margins', + 'url'=>'/marges/productMargins.php', + 'langs'=>'marges@marges', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. + 'position'=>100, + 'enabled'=>'1', // Define condition to show or hide menu entry. Use '$conf->monmodule->enabled' if entry must be visible if module is enabled. + 'perms'=>'1', // Use 'perms'=>'$user->rights->monmodule->level1->level2' if you want your menu with a permission rules + 'target'=>'', + 'user'=>2); // 0=Menu for internal users,1=external users, 2=both + $r++; + + $this->menu[$r]=array( 'fk_menu'=>'r=0', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry) + 'type'=>'left', // This is a Left menu entry + 'titre'=>'CustomerMargins', + 'mainmenu'=>'margins', + 'url'=>'/marges/customerMargins.php', + 'langs'=>'marges@marges', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. + 'position'=>200, + 'enabled'=>'1', // Define condition to show or hide menu entry. Use '$conf->monmodule->enabled' if entry must be visible if module is enabled. + 'perms'=>'1', // Use 'perms'=>'$user->rights->monmodule->level1->level2' if you want your menu with a permission rules + 'target'=>'', + 'user'=>2); // 0=Menu for internal users,1=external users, 2=both + $r++; + + $this->menu[$r]=array( 'fk_menu'=>'r=0', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry) + 'type'=>'left', // This is a Left menu entry + 'titre'=>'AgentMargins', + 'mainmenu'=>'margins', + 'url'=>'/marges/agentMargins.php', + 'langs'=>'marges@marges', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. + 'position'=>300, + 'enabled'=>'1', // Define condition to show or hide menu entry. Use '$conf->monmodule->enabled' if entry must be visible if module is enabled. + 'perms'=>'1', // Use 'perms'=>'$user->rights->monmodule->level1->level2' if you want your menu with a permission rules + 'target'=>'', + 'user'=>2); // 0=Menu for internal users,1=external users, 2=both + $r++; + + } + + /** + * \brief Function called when module is enabled. + * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database. + * It also creates data directories. + * \return int 1 if OK, 0 if KO + */ + function init() + { + $sql = array(); + + $result=$this->load_tables(); + + return $this->_init($sql); + } + + /** + * \brief Function called when module is disabled. + * Remove from database constants, boxes and permissions from Dolibarr database. + * Data directories are not deleted. + * \return int 1 if OK, 0 if KO + */ + function remove() + { + $sql = array(); + + return $this->_remove($sql); + } + + + /** + * \brief Create tables and keys required by module + * Files mymodule.sql and mymodule.key.sql with create table and create keys + * commands must be stored in directory /mymodule/sql/ + * This function is called by this->init. + * \return int <=0 if KO, >0 if OK + */ + function load_tables() + { + return $this->_load_tables('/marges/sql/'); + } +} + +?> diff --git a/htdocs/marges/index.php b/htdocs/marges/index.php new file mode 100644 index 00000000000..ff28c32f8a0 --- /dev/null +++ b/htdocs/marges/index.php @@ -0,0 +1,26 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 . + */ + +/** + * \file htdocs/custom/marges/index.php + * \ingroup product marges + * \brief Page d'index du module marges + */ + +require("./productMargins.php"); + +?> diff --git a/htdocs/marges/langs/fr_FR/marges.lang b/htdocs/marges/langs/fr_FR/marges.lang new file mode 100644 index 00000000000..d19348d605d --- /dev/null +++ b/htdocs/marges/langs/fr_FR/marges.lang @@ -0,0 +1,45 @@ +# Dolibarr language file - fr_FR - marges +CHARSET=UTF-8 + +Margin=Marge +Margins=Marges +TotalMargin=Marge totale +MarginOnProducts=Marge / Produits +MarginOnServices=Marge / Services +MarginRate=Taux de marge +MarkRate=Taux de marque +DisplayMarginRates=Afficher les taux de marge +DisplayMarkRates=Afficher les taux de marque +InputPrice=Saisir un prix + +marges=Gestion des marges +margesSetup=Paramétrage de la gestion des marges + +MarginDetails=Détails des marges réalisées + +ProductMargins=Marges par produit +CustomerMargins=Marges par client +AgentMargins=Marges par commercial + +ProductService=Produit ou Service +AllProducts=Tous les produits et services +ChooseProduct/Service=Choisissez le produit ou le service +CommercialAgent=Agent commercial + +StartDate=Date de début +EndDate=Date de fin +Launch=Démarrer + +ForceBuyingPriceIfNull=Forcer le prix d'achat si non renseigné +ForceBuyingPriceIfNullDetails=Si "ON", la ligne sera prise en compte avec une marge nulle (on forcera le prix d'achat avec le prix de vente) , sinon ("OFF") la marge sera égale au prix de vente pour cette ligne (prix d'achat à zéro). +MARGIN_METHODE_FOR_DISCOUNT=Méthode de gestion des remises globales +UseDiscountAsProduct=Comme un produit +UseDiscountAsService=Comme un service +UseDiscountOnTotal=Sur le total +MARGIN_METHODE_FOR_DISCOUNT_DETAILS=Définit si une remise globale est prise en compte comme un produit, comme un service ou uniquement sur le total lors du calcul des marges. + +MARGIN_TYPE=Type de marge gérée +MARGIN_TYPE_DETAILS=Marge brute : Prix de vente HT - Prix d'achat HT
Marge nette : Prix de vente HT - Coût de revient + +BuyingCost=Coût de revient +UnitCharges=Charge unitaire \ No newline at end of file diff --git a/htdocs/marges/lib/marges.lib.php b/htdocs/marges/lib/marges.lib.php new file mode 100644 index 00000000000..c8c5fa4f283 --- /dev/null +++ b/htdocs/marges/lib/marges.lib.php @@ -0,0 +1,108 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * or see http://www.gnu.org/ + */ + +/** + * \file /marges/lib/marges.lib.php + * \ingroup marges + * \brief Library for common marges functions + * \version $Id:$ + */ + +/** + * Define head array for tabs of marges tools setup pages + * @return Array of head + */ +function marges_admin_prepare_head() +{ + global $langs, $conf; + + $h = 0; + $head = array(); + + $head[$h][0] = dol_buildpath("/marges/admin/marges.php",1); + $head[$h][1] = $langs->trans("Parameters"); + $head[$h][2] = 'parameters'; + $h++; + + // Show more tabs from modules + // Entries must be declared in modules descriptor with line + // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab + // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab + complete_head_from_modules($conf,$langs,$object,$head,$h,'margesadmin'); + + return $head; +} + +function marges_prepare_head($user) +{ + global $langs, $conf; + $langs->load("marges@marges"); + + $h = 0; + $head = array(); + + $head[$h][0] = DOL_URL_ROOT_ALT."/marges/productMargins.php"; + $head[$h][1] = $langs->trans("ProductMargins"); + $head[$h][2] = 'productMargins'; + $h++; + + $head[$h][0] = DOL_URL_ROOT_ALT."/marges/customerMargins.php"; + $head[$h][1] = $langs->trans("CustomerMargins"); + $head[$h][2] = 'customerMargins'; + $h++; + + $head[$h][0] = DOL_URL_ROOT_ALT."/marges/agentMargins.php"; + $head[$h][1] = $langs->trans("AgentMargins"); + $head[$h][2] = 'agentMargins'; + $h++; + + return $head; +} + +function getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht) { + global $db, $conf; + + if($fk_pa > 0) { + require_once DOL_DOCUMENT_ROOT."/fourn/class/fournisseur.product.class.php"; + $product = new ProductFournisseur($db); + if ($product->fetch_product_fournisseur_price($fk_pa)) { + $paht_ret = $product->fourn_unitprice; + if ($conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0) + $paht_ret += $product->fourn_unitcharges; + } + else + $paht_ret = $paht; + } + else + $paht_ret = $paht; + + require_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); + // calcul pu_ht remisés + $tabprice=calcul_price_total(1, $pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 'HT', $objp->info_bits); + $pu_ht_remise = $tabprice[0]; + // calcul taux marge + if ($paht_ret != 0) + $marge_tx_ret = round((100 * ($pu_ht_remise - $paht_ret)) / $paht_ret, 3); + // calcul taux marque + if ($pu_ht_remise != 0) + $marque_tx_ret = round((100 * ($pu_ht_remise - $paht_ret)) / $pu_ht_remise, 3); + + return array($paht_ret, $marge_tx_ret, $marque_tx_ret); +} +?> \ No newline at end of file diff --git a/htdocs/marges/productMargins.php b/htdocs/marges/productMargins.php new file mode 100644 index 00000000000..9fe4fef4244 --- /dev/null +++ b/htdocs/marges/productMargins.php @@ -0,0 +1,296 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 . + */ + +/** + * \file htdocs/custom/marges/productMargins.php + * \ingroup marges + * \brief Page des marges par produit + * \version $Id: facture.php,v 1.84 2011/08/08 16:07:47 eldy Exp $ + */ + +require("../../main.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/lib/company.lib.php"); +require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php"); +require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php"); +require_once(DOL_DOCUMENT_ROOT_ALT."/marges/lib/marges.lib.php"); + +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("marges@marges"); + +// Security check +if (isset($_REQUEST["id"]) || isset($_REQUEST["ref"])) +{ + $id = isset($_REQUEST["id"])?$_REQUEST["id"]:(isset($_REQUEST["ref"])?$_REQUEST["ref"]:''); +} +$fieldid = isset($_REQUEST["ref"])?'ref':'rowid'; +if ($user->societe_id) $socid=$user->societe_id; +$result=restrictedArea($user,'produit|service',$id,'product','','',$fieldid); + +$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 (!empty($_POST['startdatemonth'])) + $startdate = date('Y-m-d', dol_mktime(12, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear'])); +if (!empty($_POST['enddatemonth'])) + $enddate = date('Y-m-d', dol_mktime(12, 0, 0, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear'])); + +/* + * View + */ + +$product_static = new Product($db); +$invoicestatic=new Facture($db); + +$form = new Form($db); + +llxHeader('',$langs->trans("Margins").' - '.$langs->trans("Products")); + +$text=$langs->trans("Margins"); + +print_fiche_titre($text); + +// Show tabs +$head=marges_prepare_head($user); +$titre=$langs->trans("Margins"); +$picto='marges@marges'; +dol_fiche_head($head, 'productMargins', $titre, 0, $picto); + +print '
'; +print ''; + +if ($id > 0) { + + $societe = new Societe($db, $socid); + $societe->fetch($socid); + + print ''; + print ''; + + print ''; + print ''; + + if (! $sortorder) $sortorder="DESC"; + if (! $sortfield) $sortfield="f.datef"; +} +else { + print ''; + print ''; + if (! $sortorder) $sortorder="ASC"; + if (! $sortfield) $sortfield="p.ref"; +} + +// Date début +print ''; +print ''; +print ''; +print ''; +print ''; + +// Total Margin +print ''; + +// Margin Rate +if ($conf->global->DISPLAY_MARGIN_RATES) { + print ''; +} + +// Mark Rate +if ($conf->global->DISPLAY_MARK_RATES) { + print ''; +} + +print "
'.$langs->trans('ChooseProduct/Service').''; + print $form->select_produits($selected=$id,$htmlname='id',$filtertype='',$limit=20,$price_level=0,$status=1,$finished=2,$selected_input_value='',$hidelabel=1); + print '
'.$langs->trans('AllProducts').'
'.$langs->trans('ChooseProduct/Service').''; + print $form->select_produits($selected='',$htmlname='id',$filtertype='',$limit=20,$price_level=0,$status=1,$finished=2,$selected_input_value='',$hidelabel=1); + print '
'.$langs->trans('StartDate').''; +$form->select_date($startdate,'startdate','','',1,"sel",1,1); +print ''.$langs->trans('EndDate').''; +$form->select_date($enddate,'enddate','','',1,"sel",1,1); +print ''; +print ''; +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 d.fk_product, p.label, p.rowid, p.fk_product_type, p.ref,"; +$sql.= " f.facnumber, f.total as total_ht,"; +$sql.= " sum(d.subprice * d.qty * (1 - d.remise_percent / 100)) as selling_price,"; +$sql.= " sum(d.pa_ht * d.qty) as buying_price, sum(((d.subprice * (1 - d.remise_percent / 100)) - d.pa_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"; +$sql.= ", ".MAIN_DB_PREFIX."facture as f"; +$sql.= ", ".MAIN_DB_PREFIX."facturedet as d"; +$sql.= " WHERE f.fk_soc = s.rowid"; +$sql.= " AND d.fk_product = p.rowid"; +$sql.= " AND f.fk_statut > 0"; +$sql.= " AND s.entity = ".$conf->entity; +$sql.= " AND d.fk_facture = f.rowid"; +if ($id > 0) + $sql.= " AND d.fk_product =".$id; +if (!empty($startdate)) + $sql.= " AND f.datef >= '".$startdate."'"; +if (!empty($enddate)) + $sql.= " AND f.datef <= '".$enddate."'"; +if ($id > 0) + $sql.= " GROUP BY f.rowid"; +else + $sql.= " GROUP BY d.fk_product"; +$sql.= " ORDER BY $sortfield $sortorder "; +$sql.= $db->plimit($conf->liste_limit +1, $offset); + +$result = $db->query($sql); +if ($result) +{ + $num = $db->num_rows($result); + + print '
'; + print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&id=$product->id",$sortfield,$sortorder,'',$num,0,''); + + $i = 0; + print ""; + + print ''; + if ($id > 0) { + print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&id=".$_REQUEST["id"],'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&id=".$_REQUEST["id"],'align="center"',$sortfield,$sortorder); + } + else + print_liste_field_titre($langs->trans("ProductService"),$_SERVER["PHP_SELF"],"p.ref","","&id=".$_REQUEST["id"],'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&id=".$_REQUEST["id"],'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buyng_price","","&id=".$_REQUEST["id"],'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&id=".$_REQUEST["id"],'align="right"',$sortfield,$sortorder); + if ($conf->global->DISPLAY_MARGIN_RATES) + print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"d.marge_tx","","&id=".$_REQUEST["id"],'align="right"',$sortfield,$sortorder); + if ($conf->global->DISPLAY_MARK_RATES) + print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"d.marque_tx","","&id=".$_REQUEST["id"],'align="right"',$sortfield,$sortorder); + print "\n"; + + $cumul_achat = 0; + $cumul_vente = 0; + $cumul_qty = 0; + 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)):0 ; + $markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price ,5)):0 ; + + $var=!$var; + + print ""; + if ($id > 0) { + print '\n"; + print ""; + } + else { + $product_static->type=$objp->fk_product_type; + $product_static->id=$objp->fk_product; + $product_static->ref=$objp->ref; + $product_static->libelle=$objp->label; + $text=$product_static->getNomUrl(1); + $text.= ' - '.$objp->label; + print "\n"; + } + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($conf->global->DISPLAY_MARK_RATES) + print "\n"; + print "\n"; + $i++; + $cumul_achat += $objp->buying_price; + $cumul_vente += $objp->selling_price; + } + } + + // affichage totaux marges + $var=!$var; + $totalMargin = $cumul_vente - $cumul_achat; + $marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):0 ; + $markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):0 ; + print ''; + if ($id > 0) + print '"; + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($conf->global->DISPLAY_MARK_RATES) + print "\n"; + print "\n"; + + print "
'; + $invoicestatic->id=$objp->facid; + $invoicestatic->ref=$objp->facnumber; + print $invoicestatic->getNomUrl(1); + print ""; + print dol_print_date($db->jdate($objp->datef),'day')."".$product_static->getNomUrl(1)."".price($objp->selling_price)."".price($objp->buying_price)."".price($objp->marge)."".price($marginRate)."%".price($markRate)."%
'; + else + print ''; + print $langs->trans('TotalMargin')."".price($cumul_vente)."".price($cumul_achat)."".price($totalMargin)."".price($marginRate)."%".price($markRate)."%
"; +} +else +{ + dol_print_error($db); +} +$db->free($result); + +$db->close(); + +llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $'); +?> + \ No newline at end of file diff --git a/htdocs/marges/tabs/productMargins.php b/htdocs/marges/tabs/productMargins.php new file mode 100644 index 00000000000..c17f7471a19 --- /dev/null +++ b/htdocs/marges/tabs/productMargins.php @@ -0,0 +1,262 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 . + */ + +/** + * \file htdocs/custom/marges/tabs/productMargins.php + * \ingroup product marges + * \brief Page des marges des factures clients pour un produit + * \version $Id: facture.php,v 1.84 2011/08/08 16:07:47 eldy Exp $ + */ + +require("../../../main.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/lib/product.lib.php"); +require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php"); +require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php"); + +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("marges@marges"); + +// Security check +if (isset($_GET["id"]) || isset($_GET["ref"])) +{ + $id = isset($_GET["id"])?$_GET["id"]:(isset($_GET["ref"])?$_GET["ref"]:''); +} +$fieldid = isset($_GET["ref"])?'ref':'rowid'; +if ($user->societe_id) $socid=$user->societe_id; +$result=restrictedArea($user,'produit|service',$id,'product','','',$fieldid); + +$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"; + + +/* + * View + */ + +$invoicestatic=new Facture($db); + +$html = new Form($db); + + +if ($_GET["id"] || $_GET["ref"]) +{ + $product = new Product($db); + if ($_GET["ref"]) + { + $result = $product->fetch('',$_GET["ref"]); + $_GET["id"]=$product->id; + } + if ($_GET["id"]) $result = $product->fetch($_GET["id"]); + + llxHeader("","",$langs->trans("CardProduct".$product->type)); + + /* + * En mode visu + */ + if ($result > 0) + { + $head=product_prepare_head($product, $user); + $titre=$langs->trans("CardProduct".$product->type); + $picto=($product->type==1?'service':'product'); + dol_fiche_head($head, 'marges', $titre, 0, $picto); + + print ''; + + // Reference + print ''; + print ''; + print ''; + + // Libelle + print ''; + print ''; + + // Status (to sell) + print ''; + + // Status (to buy) + print ''; + + // Total Margin + print ''; + + // Margin Rate + if ($conf->global->DISPLAY_MARGIN_RATES) { + print ''; + } + + // Mark Rate + if ($conf->global->DISPLAY_MARK_RATES) { + print ''; + } + + print "
'.$langs->trans("Ref").''; + print $html->showrefnav($product,'ref','',1,'ref'); + print '
'.$langs->trans("Label").''.$product->libelle.'
'.$langs->trans("Status").' ('.$langs->trans("Sell").')'.''; + print $product->getLibStatut(2,0); + print '
'.$langs->trans("Status").' ('.$langs->trans("Buy").')'.''; + print $product->getLibStatut(2,1); + 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.facnumber, f.total as total_ht,"; + $sql.= " (d.subprice * d.qty * (1 - d.remise_percent / 100)) as selling_price, (d.pa_ht * d.qty) as buying_price, d.qty, ((d.subprice - d.pa_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"; + $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 =".$product->id; + if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; + if ($socid) $sql.= " AND f.fk_soc = $socid"; + $sql.= " ORDER BY $sortfield $sortorder "; + $sql.= $db->plimit($conf->liste_limit +1, $offset); + + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + + print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&id=$product->id",$sortfield,$sortorder,'',$num,0,''); + + $i = 0; + print ""; + + print ''; + print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&id=".$_GET["id"],'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&id=".$_GET["id"],'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CustomerCode"),$_SERVER["PHP_SELF"],"s.code_client","","&id=".$_GET["id"],'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&id=".$_GET["id"],'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&id=".$_GET["id"],'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buyng_price","","&id=".$_GET["id"],'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&id=".$_GET["id"],'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&id=".$_GET["id"],'align="right"',$sortfield,$sortorder); + if ($conf->global->DISPLAY_MARGIN_RATES) + print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"d.marge_tx","","&id=".$_GET["id"],'align="right"',$sortfield,$sortorder); + if ($conf->global->DISPLAY_MARK_RATES) + print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"d.marque_tx","","&id=".$_GET["id"],'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&id=".$_GET["id"],'align="right"',$sortfield,$sortorder); + print "\n"; + + $cumul_achat = 0; + $cumul_vente = 0; + $cumul_qty = 0; + if ($num > 0) + { + $var=True; + while ($i < $num && $i < $conf->liste_limit) + { + $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 ""; + print '\n"; + print ''; + print "\n"; + print ""; + print "\n"; + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($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; + $marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):''; + $markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente ,5)):''; + print ''; + print '"; + print "\n"; + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($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 "'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->nom,44).'".$objp->code_client.""; + print dol_print_date($db->jdate($objp->datef),'day')."".price($objp->selling_price)."".price($objp->buying_price)."".price($objp->qty)."".price($objp->marge)."".(($marginRate == '')?'n/a':price($marginRate)."%")."".(($markRate == '')?'n/a':price($markRate)."%")."'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'
'.$langs->trans('TotalMargin')."".price($cumul_vente)."".price($cumul_achat)."".price($cumul_qty)."".price($totalMargin)."".(($marginRate == '')?'n/a':price($marginRate)."%")."".(($markRate == '')?'n/a':price($markRate)."%")." 
"; + print '
'; + $db->free($result); + } +} +else +{ + dol_print_error(); +} + +$db->close(); + +llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $'); +?> + \ No newline at end of file diff --git a/htdocs/marges/tabs/thirdpartyMargins.php b/htdocs/marges/tabs/thirdpartyMargins.php new file mode 100644 index 00000000000..545bc530198 --- /dev/null +++ b/htdocs/marges/tabs/thirdpartyMargins.php @@ -0,0 +1,247 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 . + */ + +/** + * \file htdocs/custom/marges/tabs/thirdpartyMargins.php + * \ingroup product marges + * \brief Page des marges des factures clients pour un tiers + * \version $Id: facture.php,v 1.84 2011/08/08 16:07:47 eldy Exp $ + */ + +require("../../../main.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/lib/company.lib.php"); +require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php"); +require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php"); + +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("marges@marges"); + +// Security check +$socid = isset($_GET["socid"])?$_GET["socid"]:''; +if ($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"; + + +/* + * View + */ + +$invoicestatic=new Facture($db); +$form = new Form($db); + +$help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas'; +llxHeader('',$langs->trans("ThirdParty").' - '.$langs->trans("Margins"),$help_url); + +if ($socid > 0) +{ + $societe = new Societe($db, $socid); + $societe->fetch($socid); + + /* + * Affichage onglets + */ + + $head = societe_prepare_head($societe); + + dol_fiche_head($head, 'marges', $langs->trans("ThirdParty"),0,'company'); + + print ''; + + print ''; + print ''; + + if (! empty($conf->global->SOCIETE_USEPREFIX)) // Old not used prefix field + { + print ''; + } + + if ($societe->client) + { + print ''; + } + + if ($societe->fournisseur) + { + print ''; + } + + // Total Margin + print ''; + + // Margin Rate + if ($conf->global->DISPLAY_MARGIN_RATES) { + print ''; + } + + // Mark Rate + if ($conf->global->DISPLAY_MARK_RATES) { + print ''; + } + + print "
'.$langs->trans('ThirdPartyName').''; + print $form->showrefnav($societe,'socid','',($user->societe_id?0:1),'rowid','nom'); + print '
'.$langs->trans('Prefix').''.$societe->prefix_comm.'
'; + print $langs->trans('CustomerCode').''; + print $societe->code_client; + if ($societe->check_codeclient() <> 0) print ' ('.$langs->trans("WrongCustomerCode").')'; + print '
'; + print $langs->trans('SupplierCode').''; + print $societe->code_fournisseur; + if ($societe->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.facnumber, f.total as total_ht,"; + $sql.= " sum(d.subprice * d.qty * (1 - d.remise_percent / 100)) as selling_price, sum(d.pa_ht * d.qty) as buying_price, sum(((d.subprice * (1 - d.remise_percent / 100)) - d.pa_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"; + $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.= " GROUP BY f.rowid"; + $sql.= " ORDER BY $sortfield $sortorder "; + $sql.= $db->plimit($conf->liste_limit +1, $offset); + + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + + print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&socid=$societe->id",$sortfield,$sortorder,'',$num,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"],"buyng_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 ($conf->global->DISPLAY_MARGIN_RATES) + print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"d.marge_tx","","&socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder); + if ($conf->global->DISPLAY_MARK_RATES) + print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"d.marque_tx","","&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; + $cumul_qty = 0; + 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)):'' ; + + $var=!$var; + + print ""; + print '\n"; + print ""; + print "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($conf->global->DISPLAY_MARK_RATES) + print "\n"; + print ''; + print "\n"; + $i++; + $cumul_achat += $objp->buying_price; + $cumul_vente += $objp->selling_price; + } + } + + // 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 "\n"; + print "\n"; + print "\n"; + if ($conf->global->DISPLAY_MARGIN_RATES) + print "\n"; + if ($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)."".price($objp->buying_price)."".price($objp->marge)."".(($marginRate == '')?'n/a':price($marginRate)."%")."".(($markRate == '')?'n/a':price($markRate)."%")."'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'
'.$langs->trans('TotalMargin')."".price($cumul_vente)."".price($cumul_achat)."".price($totalMargin)."".(($marginRate == '')?'n/a':price($marginRate)."%")."".(($markRate == '')?'n/a':price($markRate)."%")." 
"; + print '
'; + $db->free($result); +} +else +{ + dol_print_error(); +} + +$db->close(); + +llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $'); +?> + \ No newline at end of file