2012-07-18 15:52:15 +02:00
< ? php
2013-03-22 17:10:17 +01:00
/* Copyright ( C ) 2012 - 2013 Christophe Battarel < christophe . battarel @ altairis . fr >
2014-03-28 18:22:44 +01:00
* Copyright ( C ) 2014 Ferran Marcet < fmarcet @ 2 byte . es >
2015-01-18 16:42:11 +01:00
* Copyright ( C ) 2015 Marcos García < marcosgdf @ gmail . com >
2012-07-18 15:52:15 +02:00
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-07-18 15:52:15 +02:00
* ( 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 />.
*/
/**
2012-07-20 09:57:50 +02:00
* \file htdocs / margin / agentMargins . php
* \ingroup margin
2012-07-18 15:52:15 +02:00
* \brief Page des marges par agent commercial
*/
2012-08-22 23:24:21 +02:00
require '../main.inc.php' ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/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 . '/margin/lib/margins.lib.php' ;
2012-07-18 15:52:15 +02:00
$langs -> load ( " companies " );
$langs -> load ( " bills " );
$langs -> load ( " products " );
2012-07-20 09:57:50 +02:00
$langs -> load ( " margins " );
2012-07-18 15:52:15 +02:00
// Security check
2015-01-18 16:42:11 +01:00
if ( $user -> rights -> margin -> read -> all ) {
$agentid = GETPOST ( 'agentid' , 'int' );
} else {
$agentid = $user -> id ;
}
2012-07-18 15:52:15 +02:00
$mesg = '' ;
$sortfield = GETPOST ( " sortfield " , 'alpha' );
$sortorder = GETPOST ( " sortorder " , 'alpha' );
2012-07-23 16:10:54 +02:00
if ( ! $sortorder ) $sortorder = " ASC " ;
2013-03-22 17:10:17 +01:00
if ( ! $sortfield )
{
if ( $agentid > 0 )
$sortfield = " s.nom " ;
else
2013-05-03 11:50:13 +02:00
$sortfield = " u.lastname " ;
2013-03-22 17:10:17 +01:00
}
2012-07-18 15:52:15 +02:00
$page = GETPOST ( " page " , 'int' );
if ( $page == - 1 ) { $page = 0 ; }
$offset = $conf -> liste_limit * $page ;
$pageprev = $page - 1 ;
$pagenext = $page + 1 ;
2012-07-23 16:10:54 +02:00
$startdate = $enddate = '' ;
2012-07-18 15:52:15 +02:00
if ( ! empty ( $_POST [ 'startdatemonth' ]))
2014-12-23 16:00:04 +01:00
$startdate = dol_mktime ( 0 , 0 , 0 , $_POST [ 'startdatemonth' ], $_POST [ 'startdateday' ], $_POST [ 'startdateyear' ]);
2012-07-18 15:52:15 +02:00
if ( ! empty ( $_POST [ 'enddatemonth' ]))
2014-12-23 16:00:04 +01:00
$enddate = dol_mktime ( 23 , 59 , 59 , $_POST [ 'enddatemonth' ], $_POST [ 'enddateday' ], $_POST [ 'enddateyear' ]);
2012-07-18 15:52:15 +02:00
/*
* View
*/
2012-07-20 09:57:50 +02:00
2012-07-18 15:52:15 +02:00
$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 " );
2014-06-29 14:04:51 +02:00
//print_fiche_titre($text);
2012-07-20 09:57:50 +02:00
2012-07-18 15:52:15 +02:00
// Show tabs
$head = marges_prepare_head ( $user );
$titre = $langs -> trans ( " Margins " );
2012-07-23 16:10:54 +02:00
$picto = 'margin' ;
2012-07-18 15:52:15 +02:00
dol_fiche_head ( $head , 'agentMargins' , $titre , 0 , $picto );
2012-07-20 09:57:50 +02:00
2013-03-22 17:10:17 +01:00
print '<form method="post" name="sel" action="' . $_SERVER [ 'PHP_SELF' ] . '">' ;
2012-07-18 15:52:15 +02:00
print '<table class="border" width="100%">' ;
2015-01-18 16:42:11 +01:00
if ( $user -> rights -> margin -> read -> all ) {
print '<tr><td width="20%">' . $langs -> trans ( 'SalesRepresentative' ) . '</td>' ;
print '<td colspan="4">' ;
print $form -> select_dolusers ( $agentid , 'agentid' , 1 );
print '</td></tr>' ;
}
2012-07-18 15:52:15 +02:00
2012-07-20 09:57:50 +02:00
// Start date
2014-02-16 17:07:17 +01:00
print '<td>' . $langs -> trans ( 'StartDate' ) . ' (' . $langs -> trans ( " DateValidation " ) . ')</td>' ;
2012-07-18 15:52:15 +02:00
print '<td width="20%">' ;
$form -> select_date ( $startdate , 'startdate' , '' , '' , 1 , " sel " , 1 , 1 );
print '</td>' ;
2014-02-16 17:07:17 +01:00
print '<td width="20%">' . $langs -> trans ( 'EndDate' ) . ' (' . $langs -> trans ( " DateValidation " ) . ')</td>' ;
2012-07-18 15:52:15 +02:00
print '<td width="20%">' ;
$form -> select_date ( $enddate , 'enddate' , '' , '' , 1 , " sel " , 1 , 1 );
print '</td>' ;
print '<td style="text-align: center;">' ;
2014-02-16 17:07:17 +01:00
print '<input type="submit" class="button" value="' . dol_escape_htmltag ( $langs -> trans ( 'Launch' )) . '" />' ;
2012-07-18 15:52:15 +02:00
print '</td></tr>' ;
print " </table> " ;
print '</form>' ;
2014-03-30 16:08:30 +02:00
$sql = " SELECT " ;
2014-10-04 17:20:17 +02:00
if ( $agentid > 0 ) $sql .= " s.rowid as socid, s.nom as name, s.code_client, s.client, " ;
2013-10-07 16:17:51 +02:00
$sql .= " u.rowid as agent, u.login, u.lastname, u.firstname, " ;
2013-03-22 17:10:17 +01:00
$sql .= " sum(d.total_ht) as selling_price, " ;
2014-11-19 18:19:27 +01:00
$sql .= " sum( " . $db -> ifsql ( 'd.total_ht < 0' , 'd.qty * d.buy_price_ht * -1' , 'd.qty * d.buy_price_ht' ) . " ) as buying_price, " ;
$sql .= " sum( " . $db -> ifsql ( 'd.total_ht < 0' , '-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))' , 'd.total_ht - (d.buy_price_ht * d.qty)' ) . " ) as marge " ;
2012-07-18 15:52:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " , " . MAIN_DB_PREFIX . " facture as f " ;
2013-10-07 16:17:51 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_contact e ON e.element_id = f.rowid and e.statut = 4 and e.fk_c_type_contact = " . ( empty ( $conf -> global -> AGENT_CONTACT_TYPE ) ? - 1 : $conf -> global -> AGENT_CONTACT_TYPE );
2012-07-18 15:52:15 +02:00
$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 " ;
2014-12-15 14:13:57 +01:00
$sql .= " AND (d.product_type = 0 OR d.product_type = 1) " ;
2012-09-18 09:44:34 +02:00
if ( ! empty ( $conf -> global -> AGENT_CONTACT_TYPE ))
$sql .= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = u.rowid) OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = u.rowid)) " ;
else
$sql .= " AND sc.fk_user = u.rowid " ;
2012-07-18 15:52:15 +02:00
$sql .= " AND f.fk_statut > 0 " ;
$sql .= " AND s.entity = " . $conf -> entity ;
$sql .= " AND d.fk_facture = f.rowid " ;
2012-09-18 09:44:34 +02:00
if ( $agentid > 0 ) {
if ( ! empty ( $conf -> global -> AGENT_CONTACT_TYPE ))
$sql .= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = " . $agentid . " ) OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = " . $agentid . " )) " ;
else
$sql .= " AND sc.fk_user = " . $agentid ;
}
2014-03-28 18:22:44 +01:00
if ( ! empty ( $startdate ))
$sql .= " AND f.datef >= ' " . $db -> idate ( $startdate ) . " ' " ;
if ( ! empty ( $enddate ))
$sql .= " AND f.datef <= ' " . $db -> idate ( $enddate ) . " ' " ;
2012-09-18 09:44:34 +02:00
$sql .= " AND d.buy_price_ht IS NOT NULL " ;
2013-10-07 16:17:51 +02:00
if ( isset ( $conf -> global -> ForceBuyingPriceIfNull ) && $conf -> global -> ForceBuyingPriceIfNull == 1 ) $sql .= " AND d.buy_price_ht <> 0 " ;
2014-03-30 16:08:30 +02:00
if ( $agentid > 0 ) $sql .= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname " ;
2014-03-28 18:22:44 +01:00
else $sql .= " GROUP BY u.rowid, u.login, u.lastname, u.firstname " ;
2014-03-30 16:08:30 +02:00
$sql .= $db -> order ( $sortfield , $sortorder );
2013-03-22 17:10:17 +01:00
// TODO: calculate total to display then restore pagination
//$sql.= $db->plimit($conf->liste_limit +1, $offset);
2012-07-18 15:52:15 +02:00
2014-06-13 01:34:39 +02:00
dol_syslog ( 'margin::agentMargins.php' , LOG_DEBUG );
2012-07-18 15:52:15 +02:00
$result = $db -> query ( $sql );
if ( $result )
{
$num = $db -> num_rows ( $result );
2012-07-23 16:10:54 +02:00
print '<br>' ;
2013-03-22 17:10:17 +01:00
print_barre_liste ( $langs -> trans ( " MarginDetails " ), $page , $_SERVER [ " PHP_SELF " ], " " , $sortfield , $sortorder , '' , 0 , 0 , '' );
2012-07-20 09:57:50 +02:00
2012-07-18 15:52:15 +02:00
$i = 0 ;
print " <table class= \" noborder \" width= \" 100% \" > " ;
print '<tr class="liste_titre">' ;
2012-07-20 09:57:50 +02:00
if ( $agentid > 0 )
2013-10-05 10:21:38 +02:00
print_liste_field_titre ( $langs -> trans ( " Customer " ), $_SERVER [ " PHP_SELF " ], " s.nom " , " " , " &agentid= " . $agentid , '' , $sortfield , $sortorder );
2012-07-23 16:10:54 +02:00
else
2014-03-20 00:21:52 +01:00
print_liste_field_titre ( $langs -> trans ( " SalesRepresentative " ), $_SERVER [ " PHP_SELF " ], " u.lastname " , " " , " &agentid= " . $agentid , '' , $sortfield , $sortorder );
2012-07-23 16:10:54 +02:00
print_liste_field_titre ( $langs -> trans ( " SellingPrice " ), $_SERVER [ " PHP_SELF " ], " selling_price " , " " , " &agentid= " . $agentid , 'align="right"' , $sortfield , $sortorder );
2012-09-18 09:44:34 +02:00
print_liste_field_titre ( $langs -> trans ( " BuyingPrice " ), $_SERVER [ " PHP_SELF " ], " buying_price " , " " , " &agentid= " . $agentid , 'align="right"' , $sortfield , $sortorder );
2012-07-23 16:10:54 +02:00
print_liste_field_titre ( $langs -> trans ( " Margin " ), $_SERVER [ " PHP_SELF " ], " marge " , " " , " &agentid= " . $agentid , 'align="right"' , $sortfield , $sortorder );
if ( ! empty ( $conf -> global -> DISPLAY_MARGIN_RATES ))
2012-09-18 09:44:34 +02:00
print_liste_field_titre ( $langs -> trans ( " MarginRate " ), $_SERVER [ " PHP_SELF " ], " " , " " , " &agentid= " . $agentid , 'align="right"' , $sortfield , $sortorder );
2012-07-23 16:10:54 +02:00
if ( ! empty ( $conf -> global -> DISPLAY_MARK_RATES ))
2012-09-18 09:44:34 +02:00
print_liste_field_titre ( $langs -> trans ( " MarkRate " ), $_SERVER [ " PHP_SELF " ], " " , " " , " &agentid= " . $agentid , 'align="right"' , $sortfield , $sortorder );
2012-07-18 15:52:15 +02:00
print " </tr> \n " ;
2012-09-18 09:44:34 +02:00
$rounding = min ( $conf -> global -> MAIN_MAX_DECIMALS_UNIT , $conf -> global -> MAIN_MAX_DECIMALS_TOT );
2012-07-18 15:52:15 +02:00
if ( $num > 0 )
{
2012-07-23 16:10:54 +02:00
$var = true ;
2013-10-05 10:21:38 +02:00
2013-03-22 17:10:17 +01:00
while ( $i < $num /*&& $i < $conf->liste_limit*/ )
2012-07-18 15:52:15 +02:00
{
$objp = $db -> fetch_object ( $result );
2013-10-05 10:21:38 +02:00
$pa = $objp -> buying_price ;
$pv = $objp -> selling_price ;
$marge = $objp -> marge ;
if ( $marge < 0 )
{
2013-10-30 18:17:37 +01:00
$marginRate = ( $pa != 0 ) ? - 1 * ( 100 * $marge / $pa ) : '' ;
$markRate = ( $pv != 0 ) ? - 1 * ( 100 * $marge / $pv ) : '' ;
2013-10-05 10:21:38 +02:00
}
else
{
2013-10-30 18:17:37 +01:00
$marginRate = ( $pa != 0 ) ? ( 100 * $marge / $pa ) : '' ;
$markRate = ( $pv != 0 ) ? ( 100 * $marge / $pv ) : '' ;
2013-10-05 10:21:38 +02:00
}
2012-07-18 15:52:15 +02:00
$var =! $var ;
2013-06-20 09:18:12 +02:00
print " <tr " . $bc [ $var ] . " > " ;
2012-07-18 15:52:15 +02:00
if ( $agentid > 0 ) {
2012-07-23 16:10:54 +02:00
$companystatic -> id = $objp -> socid ;
2014-10-04 17:20:17 +02:00
$companystatic -> name = $objp -> name ;
2012-07-23 16:10:54 +02:00
$companystatic -> client = $objp -> client ;
print " <td> " . $companystatic -> getNomUrl ( 1 , 'customer' ) . " </td> \n " ;
}
else {
2013-03-22 17:10:17 +01:00
$userstatic -> fetch ( $objp -> agent );
print " <td> " . $userstatic -> getFullName ( $langs , 0 , 0 , 0 ) . " </td> \n " ;
2012-07-23 16:10:54 +02:00
}
2013-10-05 10:21:38 +02:00
2013-10-30 18:17:37 +01:00
print " <td align= \" right \" > " . price ( $pv , null , null , null , null , $rounding ) . " </td> \n " ;
print " <td align= \" right \" > " . price ( $pa , null , null , null , null , $rounding ) . " </td> \n " ;
print " <td align= \" right \" > " . price ( $marge , null , null , null , null , $rounding ) . " </td> \n " ;
2012-07-23 16:10:54 +02:00
if ( ! empty ( $conf -> global -> DISPLAY_MARGIN_RATES ))
2013-10-30 18:17:37 +01:00
print " <td align= \" right \" > " . (( $marginRate === '' ) ? 'n/a' : price ( $marginRate , null , null , null , null , $rounding ) . " % " ) . " </td> \n " ;
2012-07-23 16:10:54 +02:00
if ( ! empty ( $conf -> global -> DISPLAY_MARK_RATES ))
2013-10-30 18:17:37 +01:00
print " <td align= \" right \" > " . (( $markRate === '' ) ? 'n/a' : price ( $markRate , null , null , null , null , $rounding ) . " % " ) . " </td> \n " ;
2012-07-18 15:52:15 +02:00
print " </tr> \n " ;
2012-12-08 17:42:08 +01:00
2012-07-18 15:52:15 +02:00
$i ++ ;
}
}
2012-07-23 16:10:54 +02:00
print " </table> " ;
2012-07-18 15:52:15 +02:00
}
else
{
dol_print_error ( $db );
}
$db -> free ( $result );
2012-07-20 09:57:50 +02:00
llxFooter ();
2012-07-18 15:52:15 +02:00
$db -> close ();
2013-10-05 10:21:38 +02:00
2012-07-18 15:52:15 +02:00
?>
2013-10-05 10:21:38 +02:00
2012-07-18 15:52:15 +02:00
< script type = " text/javascript " >
$ ( document ) . ready ( function () {
2012-07-20 09:57:50 +02:00
$ ( " #agentid " ) . change ( function () {
2012-07-18 15:52:15 +02:00
$ ( " div.fiche form " ) . submit ();
});
});
2013-05-07 13:08:30 +02:00
</ script >