add margin module

This commit is contained in:
Christophe Battarel 2012-07-18 15:52:15 +02:00
parent c0ac8c9ae1
commit 1de9dc15bf
16 changed files with 2098 additions and 5 deletions

View File

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2010-2011 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2010-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 @@
<td align="right"><?php echo $langs->trans('PriceUHT'); ?></td>
<td align="right"><?php echo $langs->trans('Qty'); ?></td>
<td align="right"><?php echo $langs->trans('ReductionShort'); ?></td>
<td colspan="4">&nbsp;</td>
<?php
$colspan = 4;
if ($conf->marges->enabled) {
?>
<td align="right"><?php echo $langs->trans('BuyingPrice'); ?></td>
<?php
if($conf->global->DISPLAY_MARGIN_RATES)
$colspan++;
if($conf->global->DISPLAY_MARK_RATES)
$colspan++;
}
?>
<td colspan="<?php echo $colspan; ?>">&nbsp;</td>
</tr>
<form name="addproduct" id="addproduct" action="<?php echo $_SERVER["PHP_SELF"].'?id='.$this->id; ?>#add" method="POST">
@ -70,12 +83,36 @@
<td align="right"><input type="text" size="5" name="np_price" value="<?php echo (isset($_POST["np_price"])?$_POST["np_price"]:''); ?>"></td>
<td align="right"><input type="text" size="2" name="qty" value="<?php echo (isset($_POST["qty"])?$_POST["qty"]:1); ?>"></td>
<td align="right" nowrap><input type="text" size="1" value="<?php echo $buyer->remise_client; ?>" name="remise_percent">%</td>
<td align="center" valign="middle" colspan="4"><input type="submit" class="button" value="<?php echo $langs->trans('Add'); ?>" name="addline"></td>
<?php
$colspan = 4;
if ($conf->marges->enabled) {
?>
<td align="right"><input type="text" size="5" name="np_buying_price" value="<?php echo (isset($_POST["np_buying_price"])?$_POST["np_buying_price"]:''); ?>"></td>
<?php
if($conf->global->DISPLAY_MARGIN_RATES)
$colspan++;
if($conf->global->DISPLAY_MARK_RATES)
$colspan++;
}
?>
<td align="center" valign="middle" colspan="<?php echo $colspan; ?>"><input type="submit" class="button" value="<?php echo $langs->trans('Add'); ?>" name="addline"></td>
</tr>
<?php if ($conf->service->enabled && $dateSelector) { ?>
<?php if ($conf->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++;
}
?>
<tr <?php echo $bcnd[$var]; ?>>
<td<?php echo (! empty($conf->global->MAIN_VIEW_LINE_NUMBER) ? ' colspan="10"' : ' colspan="9"'); ?>>
<td colspan="<?php echo $colspan; ?>">
<?php
echo $langs->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");

View File

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2010-2011 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 @@
<?php } ?>
</td>
<?php
if ($conf->marges->enabled) {
?>
<td align="right"><input type="text" size="5" name="buying_price" value="<?php echo price($line->pa_ht,0,'',0); ?>"></td>
<?php
}
?>
<td align="center" colspan="5" valign="middle"><input type="submit" class="button" name="save" value="<?php echo $langs->trans("Save"); ?>">
<br><input type="submit" class="button" name="cancel" value="<?php echo $langs->trans("Cancel"); ?>"></td>
</tr>

View File

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2010-2011 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2010-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 @@
<td align="right"><?php echo dol_print_reduction($line->remise_percent,$langs); ?></td>
<?php } else { ?>
<td>&nbsp;</td>
<?php } ?>
<?php }
if ($conf->marges->enabled) {
?>
<td align="right" nowrap="nowrap"><?php echo price($line->pa_ht); ?></td>
<?php if($conf->global->DISPLAY_MARGIN_RATES) {?>
<td align="right" nowrap="nowrap"><?php echo (($line->pa_ht == 0)?'n/a':price($line->marge_tx).'%'); ?></td>
<?php
}
if($conf->global->DISPLAY_MARK_RATES) {?>
<td align="right" nowrap="nowrap"><?php echo price($line->marque_tx).'%'; ?></td>
<?php } } ?>
<?php if ($line->special_code == 3) { ?>
<td align="right" nowrap="nowrap"><?php echo $langs->trans('Option'); ?></td>

View File

@ -0,0 +1,261 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
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 "<br>";
print_fiche_titre($langs->trans("MemberMainOptions"),'','');
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td width=300>'.$langs->trans("Description").'</td>';
print '<td colspan="2" align="center">'.$langs->trans("Value").'</td>'."\n";
print '<td align="left">'.$langs->trans("Details").'</td>'."\n";
print '</tr>';
$var=true;
$form = new Form($db);
// GLOBAL DISCOUNT MANAGEMENT
$var=!$var;
print "<form method=\"post\" action=\"marges.php\">";
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print "<input type=\"hidden\" name=\"action\" value=\"typemarges\">";
print '<tr '.$bc[$var].'>';
print '<td>'.$langs->trans("MARGIN_TYPE").'</td>';
print '<td align="right">'.$langs->trans('MargeBrute');
print ' <input type="radio" name="MARGIN_TYPE" value="1" ';
if ($conf->global->MARGIN_TYPE == '1')
print 'checked ';
print '/><br/>'.$langs->trans('MargeNette');
print ' <input type="radio" name="MARGIN_TYPE" value="2" ';
if ($conf->global->MARGIN_TYPE == '2')
print 'checked ';
print '/>';
print '</td>';
print '<td>';
print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
print '</td>';
print '<td>'.$langs->trans('MARGIN_TYPE_DETAILS').'</td>';
print '</tr>';
print '</form>';
// DISPLAY MARGIN RATES
$var=!$var;
print '<tr '.$bc[$var].'>';
print '<td>'.$langs->trans("DisplayMarginRates").'</td>';
print '<td colspan="2" align="center">';
if ($conf->use_javascript_ajax)
{
print ajax_constantonoff('DISPLAY_MARGIN_RATES');
}
else
{
if($conf->global->DISPLAY_MARGIN_RATES == 0)
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_DISPLAY_MARGIN_RATES">'.img_picto($langs->trans("Disabled"),'off').'</a>';
}
else if($conf->global->DISPLAY_MARGIN_RATES == 1)
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_DISPLAY_MARGIN_RATES">'.img_picto($langs->trans("Enabled"),'on').'</a>';
}
}
print '</td>';
print '<td>'.$langs->trans('MarginRate').' = '.$langs->trans('Margin').' / '.$langs->trans('BuyingPrice').'</td>';
print '</tr>';
// DISPLAY MARK RATES
$var=!$var;
print '<tr '.$bc[$var].'>';
print '<td>'.$langs->trans("DisplayMarkRates").'</td>';
print '<td colspan="2" align="center">';
if ($conf->use_javascript_ajax)
{
print ajax_constantonoff('DISPLAY_MARK_RATES');
}
else
{
if($conf->global->DISPLAY_MARK_RATES == 0)
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_DISPLAY_MARK_RATES">'.img_picto($langs->trans("Disabled"),'off').'</a>';
}
else if($conf->global->DISPLAY_MARK_RATES == 1)
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_DISPLAY_MARK_RATES">'.img_picto($langs->trans("Enabled"),'on').'</a>';
}
}
print '</td>';
print '<td>'.$langs->trans('MarkRate').' = '.$langs->trans('Margin').' / '.$langs->trans('SellingPrice').'</td>';
print '</tr>';
print '<tr>';
print '<td>'.$langs->trans("ForceBuyingPriceIfNull").'</td>';
print '<td colspan="2" align="center">';
if ($conf->use_javascript_ajax)
{
print ajax_constantonoff('ForceBuyingPriceIfNull');
}
else
{
if($conf->global->ForceBuyingPriceIfNull == 0)
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_ForceBuyingPriceIfNull">'.img_picto($langs->trans("Disabled"),'off').'</a>';
}
else if($conf->global->ForceBuyingPriceIfNull == 1)
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_ForceBuyingPriceIfNull">'.img_picto($langs->trans("Enabled"),'on').'</a>';
}
}
print '</td>';
print '<td>'.$langs->trans('ForceBuyingPriceIfNullDetails').'</td>';
print '</tr>';
// GLOBAL DISCOUNT MANAGEMENT
$var=!$var;
print "<form method=\"post\" action=\"marges.php\">";
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print "<input type=\"hidden\" name=\"action\" value=\"remises\">";
print '<tr '.$bc[$var].'>';
print '<td>'.$langs->trans("MARGIN_METHODE_FOR_DISCOUNT").'</td>';
print '<td align="left">';
print '<select name="MARGIN_METHODE_FOR_DISCOUNT" >';
print '<option value="1" ';
if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1')
print 'selected ';
print '>'.$langs->trans('UseDiscountAsProduct').'</option>';
print '<option value="2" ';
if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2')
print 'selected ';
print '>'.$langs->trans('UseDiscountAsService').'</option>';
print '<option value="3" ';
if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3')
print 'selected ';
print '>'.$langs->trans('UseDiscountOnTotal').'</option>';
print '</select>';
print '</td>';
print '<td>';
print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
print '</td>';
print '<td>'.$langs->trans('MARGIN_METHODE_FOR_DISCOUNT_DETAILS').'</td>';
print '</tr>';
print '</form>';
print '</table>';
print '<br>';
$db->close();
llxFooter('$Date: 2011/07/31 22:23:21 $ - $Revision: 1.6 $');
?>

View File

@ -0,0 +1,274 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \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 '<form method="post" name="sel">';
print '<table class="border" width="100%">';
if ($agentid > 0) {
print '<tr><td width="20%">'.$langs->trans('CommercialAgent').'</td>';
print '<td colspan="4">';
print $form->select_dolusers($selected=$agentid,$htmlname='agentid',$show_empty=1,$exclude='',$disabled=0,$include='',$enableonly='');
print '</td></tr>';
if (! $sortorder) $sortorder="ASC";
if (! $sortfield) $sortfield="s.nom";
}
else {
print '<tr><td width="20%">'.$langs->trans('CommercialAgent').'</td>';
print '<td colspan="4">';
print $form->select_dolusers($selected='',$htmlname='agentid',$show_empty=1,$exclude='',$disabled=0,$include='',$enableonly='');
print '</td></tr>';
if (! $sortorder) $sortorder="ASC";
if (! $sortfield) $sortfield="u.login";
}
// Date début
print '<td>'.$langs->trans('StartDate').'</td>';
print '<td width="20%">';
$form->select_date($startdate,'startdate','','',1,"sel",1,1);
print '</td>';
print '<td width="20%">'.$langs->trans('EndDate').'</td>';
print '<td width="20%">';
$form->select_date($enddate,'enddate','','',1,"sel",1,1);
print '</td>';
print '<td style="text-align: center;">';
print '<input type="submit" value="'.$langs->trans('Launch').'" />';
print '</td></tr>';
// Total Margin
print '<tr style="font-weight: bold"><td>'.$langs->trans("TotalMargin").'</td><td colspan="4">';
print '<span id="totalMargin"></span>'; // set by jquery (see below)
print '</td></tr>';
// Margin Rate
if ($conf->global->DISPLAY_MARGIN_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarginRate").'</td><td colspan="4">';
print '<span id="marginRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
// Mark Rate
if ($conf->global->DISPLAY_MARK_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarkRate").'</td><td colspan="4">';
print '<span id="markRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
print "</table>";
print '</form>';
$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 '<br>';
print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&amp;socid=$societe->id",$sortfield,$sortorder,'',$num,0,'');
$i = 0;
print "<table class=\"noborder\" width=\"100%\">";
print '<tr class="liste_titre">';
if ($agentid > 0)
print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;agentid=".$_REQUEST["agentid"],'align="center"',$sortfield,$sortorder);
else
print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.login","","&amp;agentid=".$_REQUEST["agentid"],'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;agentid=".$_REQUEST["agentid"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buyng_price","","&amp;agentid=".$_REQUEST["agentid"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;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","","&amp;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","","&amp;agentid=".$_REQUEST["agentid"],'align="right"',$sortfield,$sortorder);
print "</tr>\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 "<tr $bc[$var]>";
if ($agentid > 0) {
$companystatic->id=$objp->socid;
$companystatic->nom=$objp->nom;
$companystatic->client=$objp->client;
print "<td>".$companystatic->getNomUrl(1,'customer')."</td>\n";
}
else {
$userstatic->id=$objp->agent;
$userstatic->login=$objp->login;
print "<td>".$userstatic->getLoginUrl(1)."</td>\n";
}
print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
print "<td align=\"right\">".price($objp->buying_price)."</td>\n";
print "<td align=\"right\">".price($objp->marge)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".price($marginRate)."%</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".price($markRate)."%</td>\n";
print "</tr>\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 '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
if ($client)
print '<td colspan=2>';
else
print '<td>';
print $langs->trans('TotalMargin')."</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n";
print "<td align=\"right\">".price($cumul_achat)."</td>\n";
print "<td align=\"right\">".price($totalMargin)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".price($marginRate)."%</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".price($markRate)."%</td>\n";
print "</tr>\n";
print "</table>";
}
else
{
dol_print_error($db);
}
$db->free($result);
$db->close();
llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $');
?>
<script type="text/javascript">
$(document).ready(function() {
$("#agentid").change(function() {
$("div.fiche form").submit();
});
$("#totalMargin").html("<?php echo price($totalMargin); ?>");
$("#marginRate").html("<?php echo price($marginRate); ?>%");
$("#markRate").html("<?php echo price($markRate); ?>%");
});
</script>

View File

@ -0,0 +1,3 @@
div.mainmenu.margins {
background : url('../img/marges.png') no-repeat 20px 0;
}

View File

@ -0,0 +1,287 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \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 '<form method="post" name="sel">';
print '<table class="border" width="100%">';
$client = null;
if ($socid > 0) {
$societe = new Societe($db, $socid);
$societe->fetch($socid);
if ($societe->client)
{
print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>';
print '<td colspan="4">';
$form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,$socid,'socid', $filter='client=1',$showempty=1, $showtype=0, $forcecombo=1);
print '</td></tr>';
$client = true;
if (! $sortorder) $sortorder="DESC";
if (! $sortfield) $sortfield="f.datef";
}
}
if (!$client) {
print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>';
print '<td colspan="4">';
$form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,null,'socid', $filter='client=1',$showempty=1, $showtype=0, $forcecombo=1);
print '</td></tr>';
if (! $sortorder) $sortorder="ASC";
if (! $sortfield) $sortfield="s.nom";
}
// Date début
print '<td>'.$langs->trans('StartDate').'</td>';
print '<td width="20%">';
$form->select_date($startdate,'startdate','','',1,"sel",1,1);
print '</td>';
print '<td width="20%">'.$langs->trans('EndDate').'</td>';
print '<td width="20%">';
$form->select_date($enddate,'enddate','','',1,"sel",1,1);
print '</td>';
print '<td style="text-align: center;">';
print '<input type="submit" value="'.$langs->trans('Launch').'" />';
print '</td></tr>';
// Total Margin
print '<tr style="font-weight: bold"><td>'.$langs->trans("TotalMargin").'</td><td colspan="4">';
print '<span id="totalMargin"></span>'; // set by jquery (see below)
print '</td></tr>';
// Margin Rate
if ($conf->global->DISPLAY_MARGIN_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarginRate").'</td><td colspan="4">';
print '<span id="marginRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
// Mark Rate
if ($conf->global->DISPLAY_MARK_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarkRate").'</td><td colspan="4">';
print '<span id="markRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
print "</table>";
print '</form>';
$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 '<br>';
print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&amp;socid=$societe->id",$sortfield,$sortorder,'',$num,0,'');
$i = 0;
print "<table class=\"noborder\" width=\"100%\">";
print '<tr class="liste_titre">';
if ($client) {
print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;socid=".$_REQUEST["socid"],'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;socid=".$_REQUEST["socid"],'align="center"',$sortfield,$sortorder);
}
else
print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;socid=".$_REQUEST["socid"],'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buyng_price","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;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","","&amp;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","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
print "</tr>\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 "<tr $bc[$var]>";
if ($client) {
print '<td>';
$invoicestatic->id=$objp->facid;
$invoicestatic->ref=$objp->facnumber;
print $invoicestatic->getNomUrl(1);
print "</td>\n";
print "<td align=\"center\">";
print dol_print_date($db->jdate($objp->datef),'day')."</td>";
}
else {
$companystatic->id=$objp->socid;
$companystatic->nom=$objp->nom;
$companystatic->client=$objp->client;
print "<td>".$companystatic->getNomUrl(1,'customer')."</td>\n";
}
print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
print "<td align=\"right\">".price($objp->buying_price)."</td>\n";
print "<td align=\"right\">".price($objp->marge)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".price($marginRate)."%</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".price($markRate)."%</td>\n";
print "</tr>\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 '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
if ($client)
print '<td colspan=2>';
else
print '<td>';
print $langs->trans('TotalMargin')."</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n";
print "<td align=\"right\">".price($cumul_achat)."</td>\n";
print "<td align=\"right\">".price($totalMargin)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".price($marginRate)."%</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".price($markRate)."%</td>\n";
print "</tr>\n";
print "</table>";
}
else
{
dol_print_error($db);
}
$db->free($result);
$db->close();
llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $');
?>
<script type="text/javascript">
$(document).ready(function() {
$("div.fiche form input.button['type=submit']").hide();
$("#socid").change(function() {
$("div.fiche form").submit();
});
$("#totalMargin").html("<?php echo price($totalMargin); ?>");
$("#marginRate").html("<?php echo price($marginRate); ?>%");
$("#markRate").html("<?php echo price($markRate); ?>%");
});
</script>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 579 B

View File

@ -0,0 +1,227 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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/');
}
}
?>

26
htdocs/marges/index.php Normal file
View File

@ -0,0 +1,26 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/custom/marges/index.php
* \ingroup product marges
* \brief Page d'index du module marges
*/
require("./productMargins.php");
?>

View File

@ -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<br/>Marge nette : Prix de vente HT - Coût de revient
BuyingCost=Coût de revient
UnitCharges=Charge unitaire

View File

@ -0,0 +1,108 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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);
}
?>

View File

@ -0,0 +1,296 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \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 '<form method="post" name="sel">';
print '<table class="border" width="100%">';
if ($id > 0) {
$societe = new Societe($db, $socid);
$societe->fetch($socid);
print '<tr><td width="20%">'.$langs->trans('ChooseProduct/Service').'</td>';
print '<td colspan="4">';
print $form->select_produits($selected=$id,$htmlname='id',$filtertype='',$limit=20,$price_level=0,$status=1,$finished=2,$selected_input_value='',$hidelabel=1);
print '</td></tr>';
print '<tr><td width="20%">'.$langs->trans('AllProducts').'</td>';
print '<td colspan="4"><input type="checkbox" id="all" /></td></tr>';
if (! $sortorder) $sortorder="DESC";
if (! $sortfield) $sortfield="f.datef";
}
else {
print '<tr><td width="20%">'.$langs->trans('ChooseProduct/Service').'</td>';
print '<td colspan="4">';
print $form->select_produits($selected='',$htmlname='id',$filtertype='',$limit=20,$price_level=0,$status=1,$finished=2,$selected_input_value='',$hidelabel=1);
print '</td></tr>';
if (! $sortorder) $sortorder="ASC";
if (! $sortfield) $sortfield="p.ref";
}
// Date début
print '<td>'.$langs->trans('StartDate').'</td>';
print '<td width="20%">';
$form->select_date($startdate,'startdate','','',1,"sel",1,1);
print '</td>';
print '<td width="20%">'.$langs->trans('EndDate').'</td>';
print '<td width="20%">';
$form->select_date($enddate,'enddate','','',1,"sel",1,1);
print '</td>';
print '<td style="text-align: center;">';
print '<input type="submit" value="'.$langs->trans('Launch').'" />';
print '</td></tr>';
// Total Margin
print '<tr style="font-weight: bold"><td>'.$langs->trans("TotalMargin").'</td><td colspan="4">';
print '<span id="totalMargin"></span>'; // set by jquery (see below)
print '</td></tr>';
// Margin Rate
if ($conf->global->DISPLAY_MARGIN_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarginRate").'</td><td colspan="4">';
print '<span id="marginRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
// Mark Rate
if ($conf->global->DISPLAY_MARK_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarkRate").'</td><td colspan="4">';
print '<span id="markRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
print "</table>";
print '</form>';
$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 '<br>';
print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&amp;id=$product->id",$sortfield,$sortorder,'',$num,0,'');
$i = 0;
print "<table class=\"noborder\" width=\"100%\">";
print '<tr class="liste_titre">';
if ($id > 0) {
print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;id=".$_REQUEST["id"],'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;id=".$_REQUEST["id"],'align="center"',$sortfield,$sortorder);
}
else
print_liste_field_titre($langs->trans("ProductService"),$_SERVER["PHP_SELF"],"p.ref","","&amp;id=".$_REQUEST["id"],'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;id=".$_REQUEST["id"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buyng_price","","&amp;id=".$_REQUEST["id"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;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","","&amp;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","","&amp;id=".$_REQUEST["id"],'align="right"',$sortfield,$sortorder);
print "</tr>\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 "<tr $bc[$var]>";
if ($id > 0) {
print '<td>';
$invoicestatic->id=$objp->facid;
$invoicestatic->ref=$objp->facnumber;
print $invoicestatic->getNomUrl(1);
print "</td>\n";
print "<td align=\"center\">";
print dol_print_date($db->jdate($objp->datef),'day')."</td>";
}
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 "<td>".$product_static->getNomUrl(1)."</td>\n";
}
print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
print "<td align=\"right\">".price($objp->buying_price)."</td>\n";
print "<td align=\"right\">".price($objp->marge)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".price($marginRate)."%</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".price($markRate)."%</td>\n";
print "</tr>\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 '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
if ($id > 0)
print '<td colspan=2>';
else
print '<td>';
print $langs->trans('TotalMargin')."</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n";
print "<td align=\"right\">".price($cumul_achat)."</td>\n";
print "<td align=\"right\">".price($totalMargin)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".price($marginRate)."%</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".price($markRate)."%</td>\n";
print "</tr>\n";
print "</table>";
}
else
{
dol_print_error($db);
}
$db->free($result);
$db->close();
llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $');
?>
<script type="text/javascript">
$(document).ready(function() {
$("#all").change(function() {
$("#id").val('').change();
});
$("#id").change(function() {
$("div.fiche form").submit();
});
$("#totalMargin").html("<?php echo price($totalMargin); ?>");
$("#marginRate").html("<?php echo price($marginRate); ?>%");
$("#markRate").html("<?php echo price($markRate); ?>%");
});
</script>

View File

@ -0,0 +1,262 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \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 '<table class="border" width="100%">';
// Reference
print '<tr>';
print '<td width="30%">'.$langs->trans("Ref").'</td><td colspan="3">';
print $html->showrefnav($product,'ref','',1,'ref');
print '</td>';
print '</tr>';
// Libelle
print '<tr><td>'.$langs->trans("Label").'</td><td colspan="3">'.$product->libelle.'</td>';
print '</tr>';
// Status (to sell)
print '<tr><td>'.$langs->trans("Status").' ('.$langs->trans("Sell").')'.'</td><td colspan="3">';
print $product->getLibStatut(2,0);
print '</td></tr>';
// Status (to buy)
print '<tr><td>'.$langs->trans("Status").' ('.$langs->trans("Buy").')'.'</td><td colspan="3">';
print $product->getLibStatut(2,1);
print '</td></tr>';
// Total Margin
print '<tr style="font-weight: bold"><td>'.$langs->trans("TotalMargin").'</td><td colspan="3">';
print '<span id="totalMargin"></span>'; // set by jquery (see below)
print '</td></tr>';
// Margin Rate
if ($conf->global->DISPLAY_MARGIN_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarginRate").'</td><td colspan="3">';
print '<span id="marginRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
// Mark Rate
if ($conf->global->DISPLAY_MARK_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarkRate").'</td><td colspan="3">';
print '<span id="markRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
print "</table>";
print '</div>';
$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"],"&amp;id=$product->id",$sortfield,$sortorder,'',$num,0,'');
$i = 0;
print "<table class=\"noborder\" width=\"100%\">";
print '<tr class="liste_titre">';
print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;id=".$_GET["id"],'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&amp;id=".$_GET["id"],'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CustomerCode"),$_SERVER["PHP_SELF"],"s.code_client","","&amp;id=".$_GET["id"],'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;id=".$_GET["id"],'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;id=".$_GET["id"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buyng_price","","&amp;id=".$_GET["id"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&amp;id=".$_GET["id"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;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","","&amp;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","","&amp;id=".$_GET["id"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&amp;id=".$_GET["id"],'align="right"',$sortfield,$sortorder);
print "</tr>\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 "<tr $bc[$var]>";
print '<td>';
$invoicestatic->id=$objp->facid;
$invoicestatic->ref=$objp->facnumber;
print $invoicestatic->getNomUrl(1);
print "</td>\n";
print '<td><a href="'.DOL_URL_ROOT.'/comm/fiche.php?socid='.$objp->socid.'">'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->nom,44).'</a></td>';
print "<td>".$objp->code_client."</td>\n";
print "<td align=\"center\">";
print dol_print_date($db->jdate($objp->datef),'day')."</td>";
print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
print "<td align=\"right\">".price($objp->buying_price)."</td>\n";
print "<td align=\"right\">".price($objp->qty)."</td>\n";
print "<td align=\"right\">".price($objp->marge)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".(($marginRate == '')?'n/a':price($marginRate)."%")."</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".(($markRate == '')?'n/a':price($markRate)."%")."</td>\n";
print '<td align="right">'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'</td>';
print "</tr>\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 '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
print '<td colspan=4>'.$langs->trans('TotalMargin')."</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n";
print "<td align=\"right\">".price($cumul_achat)."</td>\n";
print "<td align=\"right\">".price($cumul_qty)."</td>\n";
print "<td align=\"right\">".price($totalMargin)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".(($marginRate == '')?'n/a':price($marginRate)."%")."</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".(($markRate == '')?'n/a':price($markRate)."%")."</td>\n";
print '<td align="right">&nbsp;</td>';
print "</tr>\n";
}
else
{
dol_print_error($db);
}
print "</table>";
print '<br>';
$db->free($result);
}
}
else
{
dol_print_error();
}
$db->close();
llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $');
?>
<script type="text/javascript">
$(document).ready(function() {
$("#totalMargin").html("<?php echo price($totalMargin); ?>");
$("#marginRate").html("<?php echo price($marginRate); ?>%");
$("#markRate").html("<?php echo price($markRate); ?>%");
});
</script>

View File

@ -0,0 +1,247 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \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 '<table class="border" width="100%">';
print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>';
print '<td colspan="3">';
print $form->showrefnav($societe,'socid','',($user->societe_id?0:1),'rowid','nom');
print '</td></tr>';
if (! empty($conf->global->SOCIETE_USEPREFIX)) // Old not used prefix field
{
print '<tr><td>'.$langs->trans('Prefix').'</td><td colspan="3">'.$societe->prefix_comm.'</td></tr>';
}
if ($societe->client)
{
print '<tr><td>';
print $langs->trans('CustomerCode').'</td><td colspan="3">';
print $societe->code_client;
if ($societe->check_codeclient() <> 0) print ' <font class="error">('.$langs->trans("WrongCustomerCode").')</font>';
print '</td></tr>';
}
if ($societe->fournisseur)
{
print '<tr><td>';
print $langs->trans('SupplierCode').'</td><td colspan="3">';
print $societe->code_fournisseur;
if ($societe->check_codefournisseur() <> 0) print ' <font class="error">('.$langs->trans("WrongSupplierCode").')</font>';
print '</td></tr>';
}
// Total Margin
print '<tr style="font-weight: bold"><td>'.$langs->trans("TotalMargin").'</td><td colspan="3">';
print '<span id="totalMargin"></span>'; // set by jquery (see below)
print '</td></tr>';
// Margin Rate
if ($conf->global->DISPLAY_MARGIN_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarginRate").'</td><td colspan="3">';
print '<span id="marginRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
// Mark Rate
if ($conf->global->DISPLAY_MARK_RATES) {
print '<tr style="font-weight: bold"><td>'.$langs->trans("MarkRate").'</td><td colspan="3">';
print '<span id="markRate"></span>'; // set by jquery (see below)
print '</td></tr>';
}
print "</table>";
print '</div>';
$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"],"&amp;socid=$societe->id",$sortfield,$sortorder,'',$num,0,'');
$i = 0;
print "<table class=\"noborder\" width=\"100%\">";
print '<tr class="liste_titre">';
print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;socid=".$_REQUEST["socid"],'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;socid=".$_REQUEST["socid"],'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buyng_price","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;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","","&amp;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","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
print "</tr>\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 "<tr $bc[$var]>";
print '<td>';
$invoicestatic->id=$objp->facid;
$invoicestatic->ref=$objp->facnumber;
print $invoicestatic->getNomUrl(1);
print "</td>\n";
print "<td align=\"center\">";
print dol_print_date($db->jdate($objp->datef),'day')."</td>";
print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
print "<td align=\"right\">".price($objp->buying_price)."</td>\n";
print "<td align=\"right\">".price($objp->marge)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".(($marginRate == '')?'n/a':price($marginRate)."%")."</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".(($markRate == '')?'n/a':price($markRate)."%")."</td>\n";
print '<td align="right">'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'</td>';
print "</tr>\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 '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
print '<td colspan=2>'.$langs->trans('TotalMargin')."</td>";
print "<td align=\"right\">".price($cumul_vente)."</td>\n";
print "<td align=\"right\">".price($cumul_achat)."</td>\n";
print "<td align=\"right\">".price($totalMargin)."</td>\n";
if ($conf->global->DISPLAY_MARGIN_RATES)
print "<td align=\"right\">".(($marginRate == '')?'n/a':price($marginRate)."%")."</td>\n";
if ($conf->global->DISPLAY_MARK_RATES)
print "<td align=\"right\">".(($markRate == '')?'n/a':price($markRate)."%")."</td>\n";
print '<td align="right">&nbsp;</td>';
print "</tr>\n";
}
else
{
dol_print_error($db);
}
print "</table>";
print '<br>';
$db->free($result);
}
else
{
dol_print_error();
}
$db->close();
llxFooter('$Date: 2011/08/08 16:07:47 $ - $Revision: 1.84 $');
?>
<script type="text/javascript">
$(document).ready(function() {
$("#totalMargin").html("<?php echo price($totalMargin); ?>");
$("#marginRate").html("<?php echo price($marginRate); ?>%");
$("#markRate").html("<?php echo price($markRate); ?>%");
});
</script>