2013-04-21 10:19:07 +02:00
< ? php
2014-01-03 18:16:21 +01:00
/* Copyright ( C ) 2013 Antoine Iauch < aiauch @ gpcsolutions . fr >
* Copyright ( C ) 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2013-04-21 10:19:07 +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
* the Free Software Foundation ; either version 3 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 / compta / stats / cabyprodserv . php
* \brief Page reporting TO by Products & Services
*/
require '../../main.inc.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/report.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/tax.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php' ;
$langs -> load ( " products " );
$langs -> load ( " categories " );
$langs -> load ( " errors " );
// Security pack (data & check)
$socid = GETPOST ( 'socid' , 'int' );
if ( $user -> societe_id > 0 ) $socid = $user -> societe_id ;
if ( ! empty ( $conf -> comptabilite -> enabled )) $result = restrictedArea ( $user , 'compta' , '' , '' , 'resultat' );
if ( ! empty ( $conf -> accounting -> enabled )) $result = restrictedArea ( $user , 'accounting' , '' , '' , 'comptarapport' );
// Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
$modecompta = $conf -> global -> COMPTA_MODE ;
if ( GETPOST ( " modecompta " )) $modecompta = GETPOST ( " modecompta " );
$sortorder = isset ( $_GET [ " sortorder " ]) ? $_GET [ " sortorder " ] : $_POST [ " sortorder " ];
$sortfield = isset ( $_GET [ " sortfield " ]) ? $_GET [ " sortfield " ] : $_POST [ " sortfield " ];
if ( ! $sortorder ) $sortorder = " asc " ;
if ( ! $sortfield ) $sortfield = " name " ;
// Category
2013-04-25 13:01:38 +02:00
$selected_cat = ( int ) GETPOST ( 'search_categ' , 'int' );
2013-04-21 10:19:07 +02:00
$subcat = false ;
if ( GETPOST ( 'subcat' , 'alpha' ) === 'yes' ) {
$subcat = true ;
}
// Date range
$year = GETPOST ( " year " );
$month = GETPOST ( " month " );
$date_startyear = GETPOST ( " date_startyear " );
$date_startmonth = GETPOST ( " date_startmonth " );
$date_startday = GETPOST ( " date_startday " );
$date_endyear = GETPOST ( " date_endyear " );
$date_endmonth = GETPOST ( " date_endmonth " );
$date_endday = GETPOST ( " date_endday " );
if ( empty ( $year ))
{
$year_current = strftime ( " %Y " , dol_now ());
$month_current = strftime ( " %m " , dol_now ());
$year_start = $year_current ;
} else {
$year_current = $year ;
$month_current = strftime ( " %m " , dol_now ());
$year_start = $year ;
}
$date_start = dol_mktime ( 0 , 0 , 0 , $_REQUEST [ " date_startmonth " ], $_REQUEST [ " date_startday " ], $_REQUEST [ " date_startyear " ]);
$date_end = dol_mktime ( 23 , 59 , 59 , $_REQUEST [ " date_endmonth " ], $_REQUEST [ " date_endday " ], $_REQUEST [ " date_endyear " ]);
// Quarter
if ( empty ( $date_start ) || empty ( $date_end )) // We define date_start and date_end
{
$q = GETPOST ( " q " ) ? GETPOST ( " q " ) : 0 ;
if ( $q == 0 )
{
// We define date_start and date_end
$month_start = GETPOST ( " month " ) ? GETPOST ( " month " ) : ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ) : 1 );
$year_end = $year_start ;
$month_end = $month_start ;
if ( ! GETPOST ( " month " )) // If month not forced
{
if ( ! GETPOST ( 'year' ) && $month_start > $month_current )
{
$year_start -- ;
$year_end -- ;
}
$month_end = $month_start - 1 ;
if ( $month_end < 1 ) $month_end = 12 ;
else $year_end ++ ;
}
$date_start = dol_get_first_day ( $year_start , $month_start , false ); $date_end = dol_get_last_day ( $year_end , $month_end , false );
}
if ( $q == 1 ) { $date_start = dol_get_first_day ( $year_start , 1 , false ); $date_end = dol_get_last_day ( $year_start , 3 , false ); }
if ( $q == 2 ) { $date_start = dol_get_first_day ( $year_start , 4 , false ); $date_end = dol_get_last_day ( $year_start , 6 , false ); }
if ( $q == 3 ) { $date_start = dol_get_first_day ( $year_start , 7 , false ); $date_end = dol_get_last_day ( $year_start , 9 , false ); }
if ( $q == 4 ) { $date_start = dol_get_first_day ( $year_start , 10 , false ); $date_end = dol_get_last_day ( $year_start , 12 , false ); }
} else {
// TODO We define q
}
$commonparams = array ();
$commonparams [ 'modecompta' ] = $modecompta ;
$commonparams [ 'sortorder' ] = $sortorder ;
$commonparams [ 'sortfield' ] = $sortfield ;
$headerparams = array ();
$headerparams [ 'date_startyear' ] = $date_startyear ;
$headerparams [ 'date_startmonth' ] = $date_startmonth ;
$headerparams [ 'date_startday' ] = $date_startday ;
$headerparams [ 'date_endyear' ] = $date_endyear ;
$headerparams [ 'date_endmonth' ] = $date_endmonth ;
$headerparams [ 'date_endday' ] = $date_endday ;
$headerparams [ 'q' ] = $q ;
$tableparams = array ();
$tableparams [ 'search_categ' ] = $selected_cat ;
$tableparams [ 'subcat' ] = ( $subcat === true ) ? 'yes' : '' ;
// Adding common parameters
$allparams = array_merge ( $commonparams , $headerparams , $tableparams );
$headerparams = array_merge ( $commonparams , $headerparams );
$tableparams = array_merge ( $commonparams , $tableparams );
foreach ( $allparams as $key => $value ) {
$paramslink .= '&' . $key . '=' . $value ;
}
/*
* View
*/
llxHeader ();
$form = new Form ( $db );
$formother = new FormOther ( $db );
// Show report header
$nom = $langs -> trans ( " SalesTurnover " ) . ', ' . $langs -> trans ( " ByProductsAndServices " );
if ( $modecompta == " CREANCES-DETTES " ) {
2013-10-10 19:26:07 +02:00
$calcmode = $langs -> trans ( " CalcModeDebt " );
$calcmode .= '<br>(' . $langs -> trans ( " SeeReportInInputOutputMode " , '<a href="' . $_SERVER [ " PHP_SELF " ] . '?year=' . $year . '&modecompta=RECETTES-DEPENSES">' , '</a>' ) . ')' ;
2013-04-21 10:19:07 +02:00
$period = $form -> select_date ( $date_start , 'date_start' , 0 , 0 , 0 , '' , 1 , 0 , 1 ) . ' - ' . $form -> select_date ( $date_end , 'date_end' , 0 , 0 , 0 , '' , 1 , 0 , 1 );
$description = $langs -> trans ( " RulesCADue " );
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$description .= $langs -> trans ( " DepositsAreNotIncluded " );
} else {
$description .= $langs -> trans ( " DepositsAreIncluded " );
}
$builddate = time ();
} else {
2013-10-10 19:26:07 +02:00
$calcmode = $langs -> trans ( " CalcModeEngagement " );
$calcmode .= '<br>(' . $langs -> trans ( " SeeReportInDueDebtMode " , '<a href="' . $_SERVER [ " PHP_SELF " ] . '?year=' . $year . '&modecompta=CREANCES-DETTES">' , '</a>' ) . ')' ;
2013-04-21 10:19:07 +02:00
$period = $form -> select_date ( $date_start , 'date_start' , 0 , 0 , 0 , '' , 1 , 0 , 1 ) . ' - ' . $form -> select_date ( $date_end , 'date_end' , 0 , 0 , 0 , '' , 1 , 0 , 1 );
$description = $langs -> trans ( " RulesCAIn " );
$description .= $langs -> trans ( " DepositsAreIncluded " );
$builddate = time ();
}
2013-10-10 19:26:07 +02:00
report_header ( $nom , $nomlink , $period , $periodlink , $description , $builddate , $exportlink , $tableparams , $calcmode );
2013-04-21 10:19:07 +02:00
// SQL request
$catotal = 0 ;
2014-01-03 18:16:21 +01:00
if ( $modecompta == 'CREANCES-DETTES' )
{
2013-04-21 10:19:07 +02:00
$sql = " SELECT DISTINCT p.rowid as rowid, p.ref as ref, p.label as label, " ;
2013-07-16 00:29:03 +02:00
$sql .= " sum(l.total_ht) as amount, sum(l.total_ttc) as amount_ttc " ;
2014-01-03 18:16:21 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f, " . MAIN_DB_PREFIX . " facturedet as l, " . MAIN_DB_PREFIX . " product as p " ;
if ( $selected_cat === - 2 ) // Without any category
{
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " categorie_product as cp ON p.rowid = cp.fk_product " ;
}
else if ( $selected_cat ) // Into a specific category
{
$sql .= " , " . MAIN_DB_PREFIX . " categorie as c, " . MAIN_DB_PREFIX . " categorie_product as cp " ;
2013-04-21 10:19:07 +02:00
}
$sql .= " WHERE l.fk_product = p.rowid " ;
2014-01-03 18:16:21 +01:00
$sql .= " AND l.fk_facture = f.rowid " ;
2013-04-21 10:19:07 +02:00
$sql .= " AND f.fk_statut in (1,2) " ;
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$sql .= " AND f.type IN (0,1,2) " ;
} else {
$sql .= " AND f.type IN (0,1,2,3) " ;
}
if ( $date_start && $date_end ) {
$sql .= " AND f.datef >= ' " . $db -> idate ( $date_start ) . " ' AND f.datef <= ' " . $db -> idate ( $date_end ) . " ' " ;
}
2014-01-03 18:16:21 +01:00
if ( $selected_cat === - 2 ) // Without any category
{
$sql .= " AND cp.fk_product is null " ;
}
else if ( $selected_cat ) { // Into a specific category
$sql .= " AND (c.rowid = " . $selected_cat ;
if ( $subcat ) $sql .= " OR c.fk_parent = " . $selected_cat ;
$sql .= " ) " ;
$sql .= " AND cp.fk_categorie = c.rowid AND cp.fk_product = p.rowid " ;
}
2013-04-21 10:19:07 +02:00
$sql .= " AND f.entity = " . $conf -> entity ;
2014-01-03 18:16:21 +01:00
$sql .= " GROUP BY p.rowid " ;
$sql .= " ORDER BY p.ref " ;
2013-04-21 10:19:07 +02:00
2014-01-03 18:16:21 +01:00
dol_syslog ( " cabyprodserv sql= " . $sql );
2013-04-21 10:19:07 +02:00
$result = $db -> query ( $sql );
if ( $result ) {
$num = $db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
$amount_ht [ $obj -> rowid ] = $obj -> amount ;
$amount [ $obj -> rowid ] = $obj -> amount_ttc ;
$name [ $obj -> rowid ] = $obj -> ref . ' - ' . $obj -> label ;
$catotal_ht += $obj -> amount ;
$catotal += $obj -> amount_ttc ;
$i ++ ;
}
} else {
dol_print_error ( $db );
}
// Show Array
$i = 0 ;
print '<form method="POST" action="' . $_SERVER [ " PHP_SELF " ] . '">' ;
// Extra parameters management
foreach ( $headerparams as $key => $value )
{
print '<input type="hidden" name="' . $key . '" value="' . $value . '">' ;
}
print '<table class="noborder" width="100%">' ;
// Category filter
print '<tr class="liste_titre">' ;
print '<td>' ;
print $langs -> trans ( " Category " ) . ': ' . $formother -> select_categories ( 0 , $selected_cat , 'search_categ' , true );
print ' ' ;
print $langs -> trans ( " SubCats " ) . '? ' ;
print '<input type="checkbox" name="subcat" value="yes"' ;
if ( $subcat ) {
print ' checked' ;
}
print '></td>' ;
print '<td colspan="3" align="right">' ;
2014-01-13 10:10:17 +01:00
print '<input type="image" class="liste_titre" name="button_search" src="' . img_picto ( $langs -> trans ( " Search " ), 'search.png' , '' , '' , 1 ) . '" value="' . dol_escape_htmltag ( $langs -> trans ( " Search " )) . '" title="' . dol_escape_htmltag ( $langs -> trans ( " Search " )) . '">' ;
2013-04-21 10:19:07 +02:00
print '</td></tr>' ;
// Array header
print " <tr class= \" liste_titre \" > " ;
print_liste_field_titre (
$langs -> trans ( " Product " ),
$_SERVER [ " PHP_SELF " ],
" name " ,
" " ,
$paramslink ,
" " ,
$sortfield ,
$sortorder
);
print_liste_field_titre (
$langs -> trans ( 'AmountHT' ),
$_SERVER [ " PHP_SELF " ],
" amount_ht " ,
" " ,
$paramslink ,
'align="right"' ,
$sortfield ,
$sortorder
);
print_liste_field_titre (
$langs -> trans ( " AmountTTC " ),
$_SERVER [ " PHP_SELF " ],
" amount_ttc " ,
" " ,
$paramslink ,
'align="right"' ,
$sortfield ,
$sortorder
);
print_liste_field_titre (
$langs -> trans ( " Percentage " ),
$_SERVER [ " PHP_SELF " ],
" amount_ttc " ,
" " ,
$paramslink ,
'align="right"' ,
$sortfield ,
$sortorder
);
// TODO: statistics?
print " </tr> \n " ;
// Array Data
$var = true ;
if ( count ( $amount )) {
$arrayforsort = $name ;
// defining arrayforsort
if ( $sortfield == 'nom' && $sortorder == 'asc' ) {
asort ( $name );
$arrayforsort = $name ;
}
if ( $sortfield == 'nom' && $sortorder == 'desc' ) {
arsort ( $name );
$arrayforsort = $name ;
}
if ( $sortfield == 'amount_ht' && $sortorder == 'asc' ) {
asort ( $amount_ht );
$arrayforsort = $amount_ht ;
}
if ( $sortfield == 'amount_ht' && $sortorder == 'desc' ) {
arsort ( $amount_ht );
$arrayforsort = $amount_ht ;
}
if ( $sortfield == 'amount_ttc' && $sortorder == 'asc' ) {
asort ( $amount );
$arrayforsort = $amount ;
}
if ( $sortfield == 'amount_ttc' && $sortorder == 'desc' ) {
arsort ( $amount );
$arrayforsort = $amount ;
}
foreach ( $arrayforsort as $key => $value ) {
$var =! $var ;
print " <tr " . $bc [ $var ] . " > " ;
2013-06-25 14:54:29 +02:00
// Product
2013-04-21 10:19:07 +02:00
$fullname = $name [ $key ];
if ( $key >= 0 ) {
$linkname = '<a href="' . DOL_URL_ROOT . '/product/fiche.php?id=' . $key . '">' . img_object ( $langs -> trans ( " ShowProduct " ), 'product' ) . ' ' . $fullname . '</a>' ;
} else {
$linkname = $langs -> trans ( " PaymentsNotLinkedToProduct " );
}
print " <td> " . $linkname . " </td> \n " ;
// Amount w/o VAT
print '<td align="right">' ;
2013-06-25 14:54:29 +02:00
/* if ( $key > 0 ) {
2013-04-21 10:19:07 +02:00
print '<a href="' . DOL_URL_ROOT . '/compta/facture/list.php?productid=' . $key . '">' ;
} else {
print '<a href="#">' ;
2013-06-25 14:54:29 +02:00
} */
2013-04-21 10:19:07 +02:00
print price ( $amount_ht [ $key ]);
2013-06-25 14:54:29 +02:00
//print '</a>';
2013-04-21 10:19:07 +02:00
print '</td>' ;
// Amount with VAT
print '<td align="right">' ;
2013-06-25 14:54:29 +02:00
/* if ( $key > 0 ) {
2013-04-21 10:19:07 +02:00
print '<a href="' . DOL_URL_ROOT . '/compta/facture/list.php?productid=' . $key . '">' ;
} else {
print '<a href="#">' ;
2013-06-25 14:54:29 +02:00
} */
2013-04-21 10:19:07 +02:00
print price ( $amount [ $key ]);
2013-06-25 14:54:29 +02:00
//print '</a>';
2013-04-21 10:19:07 +02:00
print '</td>' ;
// Percent;
print '<td align="right">' . ( $catotal > 0 ? round ( 100 * $amount [ $key ] / $catotal , 2 ) . '%' : ' ' ) . '</td>' ;
// TODO: statistics?
print " </tr> \n " ;
$i ++ ;
}
// Total
print '<tr class="liste_total">' ;
print '<td>' . $langs -> trans ( " Total " ) . '</td>' ;
print '<td align="right">' . price ( $catotal_ht ) . '</td>' ;
print '<td align="right">' . price ( $catotal ) . '</td>' ;
print '<td> </td>' ;
print '</tr>' ;
$db -> free ( $result );
}
print " </table> " ;
print '</form>' ;
} else {
// $modecompta != 'CREANCES-DETTES'
2014-01-03 18:16:21 +01:00
// "Calculation of part of each product for accountancy in this mode is not possible. When a partial payment (for example 5 euros) is done on an
// invoice with 2 product (product A for 10 euros and product B for 20 euros), what is part of paiment for product A and part of paiment for product B ?
// Because there is no way to know this, this report is not relevant.
2014-01-03 18:28:00 +01:00
print '<br>' . $langs -> trans ( " TurnoverPerProductInCommitmentAccountingNotRelevant " ) . '<br>' ;
2013-04-21 10:19:07 +02:00
}
llxFooter ();
$db -> close ();
?>