2010-12-04 15:54:29 +01:00
< ? php
2018-09-09 09:36:12 +02:00
/* Copyright ( C ) 2007 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2007 - 2010 Jean Heimburger < jean @ tiaris . info >
* Copyright ( C ) 2011 - 2014 Juanjo Menent < jmenent @ 2 byte . es >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2012 Regis Houssin < regis . houssin @ inodbox . com >
2019-01-28 21:39:22 +01:00
* Copyright ( C ) 2011 - 2012 Alexandre Spangaro < aspangaro @ open - dsi . fr >
2018-09-09 09:36:12 +02:00
* Copyright ( C ) 2012 Cédric Salvador < csalvador @ gpcsolutions . fr >
* Copyright ( C ) 2013 Marcos García < marcosgdf @ gmail . com >
* Copyright ( C ) 2014 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
* Copyright ( C ) 2018 Frédéric France < frederic . france @ netlogic . fr >
2010-12-04 15:54:29 +01: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
2010-12-04 15:54:29 +01: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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2010-12-04 15:54:29 +01:00
*/
2011-09-21 00:28:31 +02:00
/**
* \file htdocs / compta / journal / sellsjournal . php
* \ingroup societe , facture
* \brief Page with sells journal
*/
2013-04-13 14:44:56 +02:00
global $mysoc ;
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/report.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/societe/class/client.class.php' ;
2010-12-04 15:54:29 +01:00
2018-05-27 09:27:09 +02:00
// Load translation files required by the page
2019-05-09 18:33:50 +02:00
$langs -> loadLangs ( array ( 'companies' , 'other' , 'bills' , 'compta' ));
2010-12-04 15:54:29 +01:00
2020-04-10 10:59:32 +02:00
$date_startmonth = GETPOST ( 'date_startmonth' );
$date_startday = GETPOST ( 'date_startday' );
$date_startyear = GETPOST ( 'date_startyear' );
$date_endmonth = GETPOST ( 'date_endmonth' );
$date_endday = GETPOST ( 'date_endday' );
$date_endyear = GETPOST ( 'date_endyear' );
2012-08-03 20:09:37 +02:00
2013-01-10 08:27:12 +01:00
// Security check
2021-02-23 21:09:01 +01:00
if ( $user -> socid > 0 ) {
$socid = $user -> socid ;
}
2022-08-29 11:24:48 +02:00
if ( isModEnabled ( 'comptabilite' )) {
2021-02-23 21:09:01 +01:00
$result = restrictedArea ( $user , 'compta' , '' , '' , 'resultat' );
}
2022-08-29 11:24:48 +02:00
if ( isModEnabled ( 'accounting' )) {
2021-02-23 21:09:01 +01:00
$result = restrictedArea ( $user , 'accounting' , '' , '' , 'comptarapport' );
}
2010-12-04 15:54:29 +01:00
2012-06-13 11:32:09 +02:00
/*
* Actions
*/
2010-12-04 15:54:29 +01:00
2012-06-13 11:32:09 +02:00
// None
2010-12-04 15:54:29 +01:00
2012-06-13 11:32:09 +02:00
/*
* View
*/
2010-12-04 15:54:29 +01:00
2020-04-10 10:59:32 +02:00
$form = new Form ( $db );
2010-12-04 15:54:29 +01:00
2020-04-10 10:59:32 +02:00
$morequery = '&date_startyear=' . $date_startyear . '&date_startmonth=' . $date_startmonth . '&date_startday=' . $date_startday . '&date_endyear=' . $date_endyear . '&date_endmonth=' . $date_endmonth . '&date_endday=' . $date_endday ;
2013-11-25 16:51:30 +01:00
2019-01-27 11:55:16 +01:00
llxHeader ( '' , $langs -> trans ( " SellsJournal " ), '' , '' , 0 , 0 , '' , '' , $morequery );
2012-06-13 11:32:09 +02:00
2010-12-04 15:54:29 +01:00
2019-01-27 11:55:16 +01:00
$year_current = strftime ( " %Y " , dol_now ());
$pastmonth = strftime ( " %m " , dol_now ()) - 1 ;
2011-07-31 23:24:20 +02:00
$pastmonthyear = $year_current ;
2021-02-23 21:09:01 +01:00
if ( $pastmonth == 0 ) {
2011-01-05 16:15:19 +01:00
$pastmonth = 12 ;
$pastmonthyear -- ;
}
2010-12-04 15:54:29 +01:00
2020-04-10 10:59:32 +02:00
$date_start = dol_mktime ( 0 , 0 , 0 , $date_startmonth , $date_startday , $date_startyear );
$date_end = dol_mktime ( 23 , 59 , 59 , $date_endmonth , $date_endday , $date_endyear );
2010-12-04 15:54:29 +01:00
2021-02-23 21:09:01 +01:00
if ( empty ( $date_start ) || empty ( $date_end )) { // We define date_start and date_end
2021-03-01 20:37:16 +01:00
$date_start = dol_get_first_day ( $pastmonthyear , $pastmonth , false );
$date_end = dol_get_last_day ( $pastmonthyear , $pastmonth , false );
2010-12-04 15:54:29 +01:00
}
2020-04-10 10:59:32 +02:00
$name = $langs -> trans ( " SellsJournal " );
$periodlink = '' ;
$exportlink = '' ;
$builddate = dol_now ();
$description = $langs -> trans ( " DescSellsJournal " ) . '<br>' ;
2021-02-23 21:09:01 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$description .= $langs -> trans ( " DepositsAreNotIncluded " );
} else {
$description .= $langs -> trans ( " DepositsAreIncluded " );
}
2020-04-10 10:59:32 +02:00
$period = $form -> selectDate ( $date_start , 'date_start' , 0 , 0 , 0 , '' , 1 , 0 ) . ' - ' . $form -> selectDate ( $date_end , 'date_end' , 0 , 0 , 0 , '' , 1 , 0 );
2019-01-27 11:55:16 +01:00
report_header ( $name , '' , $period , $periodlink , $description , $builddate , $exportlink );
2010-12-04 15:54:29 +01:00
2013-02-25 17:56:41 +01:00
$p = explode ( " : " , $conf -> global -> MAIN_INFO_SOCIETE_COUNTRY );
2010-12-04 15:54:29 +01:00
$idpays = $p [ 0 ];
2018-12-02 14:31:45 +01:00
$sql = " SELECT f.rowid, f.ref, f.type, f.datef, f.ref_client, " ;
2020-04-10 10:59:32 +02:00
$sql .= " fd.product_type, fd.total_ht, fd.total_tva, fd.tva_tx, fd.total_ttc, fd.localtax1_tx, fd.localtax2_tx, fd.total_localtax1, fd.total_localtax2, fd.rowid as id, fd.situation_percent, " ;
$sql .= " s.rowid as socid, s.nom as name, s.code_compta, s.client, " ;
2021-03-18 17:08:55 +01:00
$sql .= " p.rowid as pid, p.ref as pref, " ;
2021-03-27 05:23:20 +01:00
if ( ! empty ( $conf -> global -> MAIN_PRODUCT_PERENTITY_SHARED )) {
2021-04-14 04:45:17 +02:00
$sql .= " ppe.accountancy_code_sell, " ;
2021-03-18 17:08:55 +01:00
} else {
$sql .= " p.accountancy_code_sell, " ;
}
2020-04-10 10:59:32 +02:00
$sql .= " ct.accountancy_code_sell as account_tva, ct.recuperableonly " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as fd " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product as p ON p.rowid = fd.fk_product " ;
2021-03-27 05:23:20 +01:00
if ( ! empty ( $conf -> global -> MAIN_PRODUCT_PERENTITY_SHARED )) {
2021-04-14 04:45:17 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . (( int ) $conf -> entity );
2021-03-18 17:08:55 +01:00
}
2020-04-10 10:59:32 +02:00
$sql .= " JOIN " . MAIN_DB_PREFIX . " facture as f ON f.rowid = fd.fk_facture " ;
$sql .= " JOIN " . MAIN_DB_PREFIX . " societe as s ON s.rowid = f.fk_soc " ;
2020-09-19 23:30:29 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_tva ct ON fd.tva_tx = ct.taux AND fd.info_bits = ct.recuperableonly AND ct.fk_pays = " . (( int ) $idpays );
2020-04-10 10:59:32 +02:00
$sql .= " WHERE f.entity IN ( " . getEntity ( 'invoice' ) . " ) " ;
$sql .= " AND f.fk_statut > 0 " ;
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_SITUATION . " ) " ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_DEPOSIT . " , " . Facture :: TYPE_SITUATION . " ) " ;
2015-08-13 11:32:31 +02:00
}
2020-04-10 10:59:32 +02:00
$sql .= " AND fd.product_type IN (0,1) " ;
2021-02-23 21:09:01 +01:00
if ( $date_start && $date_end ) {
$sql .= " AND f.datef >= ' " . $db -> idate ( $date_start ) . " ' AND f.datef <= ' " . $db -> idate ( $date_end ) . " ' " ;
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY f.rowid " ;
2010-12-04 15:54:29 +01:00
2013-10-31 14:39:41 +01:00
// TODO Find a better trick to avoid problem with some mysql installations
2021-02-23 21:09:01 +01:00
if ( in_array ( $db -> type , array ( 'mysql' , 'mysqli' ))) {
$db -> query ( 'SET SQL_BIG_SELECTS=1' );
}
2013-10-31 14:39:41 +01:00
2010-12-04 15:54:29 +01:00
$result = $db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2010-12-04 15:54:29 +01:00
$tabfac = array ();
$tabht = array ();
$tabtva = array ();
2013-04-13 14:44:56 +02:00
$tablocaltax1 = array ();
$tablocaltax2 = array ();
2010-12-04 15:54:29 +01:00
$tabttc = array ();
2012-06-13 11:32:09 +02:00
$tabcompany = array ();
2020-04-10 10:59:32 +02:00
$account_localtax1 = 0 ;
$account_localtax2 = 0 ;
2010-12-06 03:25:13 +01:00
2010-12-04 15:54:29 +01:00
$num = $db -> num_rows ( $result );
2021-02-23 21:09:01 +01:00
$i = 0 ;
$resligne = array ();
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
// les variables
$cptcli = (( $conf -> global -> ACCOUNTING_ACCOUNT_CUSTOMER != " " ) ? $conf -> global -> ACCOUNTING_ACCOUNT_CUSTOMER : $langs -> trans ( " CodeNotDef " ));
$compta_soc = ( ! empty ( $obj -> code_compta ) ? $obj -> code_compta : $cptcli );
2010-12-04 15:54:29 +01:00
$compta_prod = $obj -> accountancy_code_sell ;
2021-02-23 21:09:01 +01:00
if ( empty ( $compta_prod )) {
if ( $obj -> product_type == 0 ) {
$compta_prod = ( ! empty ( $conf -> global -> ACCOUNTING_PRODUCT_SOLD_ACCOUNT ) ? $conf -> global -> ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs -> trans ( " CodeNotDef " ));
} else {
$compta_prod = ( ! empty ( $conf -> global -> ACCOUNTING_SERVICE_SOLD_ACCOUNT ) ? $conf -> global -> ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs -> trans ( " CodeNotDef " ));
}
2010-12-06 03:25:13 +01:00
}
2020-04-10 10:59:32 +02:00
$cpttva = ( ! empty ( $conf -> global -> ACCOUNTING_VAT_SOLD_ACCOUNT ) ? $conf -> global -> ACCOUNTING_VAT_SOLD_ACCOUNT : $langs -> trans ( " CodeNotDef " ));
$compta_tva = ( ! empty ( $obj -> account_tva ) ? $obj -> account_tva : $cpttva );
2010-12-06 03:25:13 +01:00
2020-04-10 10:59:32 +02:00
$account_localtax1 = getLocalTaxesFromRate ( $obj -> tva_tx , 1 , $obj -> thirdparty , $mysoc );
$compta_localtax1 = ( ! empty ( $account_localtax1 [ 3 ]) ? $account_localtax1 [ 3 ] : $langs -> trans ( " CodeNotDef " ));
$account_localtax2 = getLocalTaxesFromRate ( $obj -> tva_tx , 2 , $obj -> thirdparty , $mysoc );
$compta_localtax2 = ( ! empty ( $account_localtax2 [ 3 ]) ? $account_localtax2 [ 3 ] : $langs -> trans ( " CodeNotDef " ));
2013-08-28 13:05:14 +02:00
2014-11-14 16:43:49 +01:00
// Situation invoices handling
$line = new FactureLigne ( $db );
2020-04-10 10:59:32 +02:00
$line -> fetch ( $obj -> id ); // id of line
2016-03-13 01:11:42 +01:00
$prev_progress = 0 ;
2020-04-10 10:59:32 +02:00
if ( $obj -> type == Facture :: TYPE_SITUATION ) {
2015-08-13 11:23:27 +02:00
// Avoid divide by 0
2017-08-31 15:44:36 +02:00
if ( $obj -> situation_percent == 0 ) {
2015-08-13 11:23:27 +02:00
$situation_ratio = 0 ;
} else {
2020-10-31 14:32:18 +01:00
$prev_progress = $line -> get_prev_progress ( $obj -> rowid ); // id on invoice
$situation_ratio = ( $obj -> situation_percent - $prev_progress ) / $obj -> situation_percent ;
2015-08-13 11:23:27 +02:00
}
2015-08-13 11:32:31 +02:00
} else {
2015-08-13 11:23:27 +02:00
$situation_ratio = 1 ;
2014-11-14 16:43:49 +01:00
}
2020-10-31 14:32:18 +01:00
//la ligne facture
2021-02-23 21:09:01 +01:00
$tabfac [ $obj -> rowid ][ " date " ] = $obj -> datef ;
$tabfac [ $obj -> rowid ][ " ref " ] = $obj -> ref ;
$tabfac [ $obj -> rowid ][ " type " ] = $obj -> type ;
if ( ! isset ( $tabttc [ $obj -> rowid ][ $compta_soc ])) {
$tabttc [ $obj -> rowid ][ $compta_soc ] = 0 ;
}
if ( ! isset ( $tabht [ $obj -> rowid ][ $compta_prod ])) {
$tabht [ $obj -> rowid ][ $compta_prod ] = 0 ;
}
if ( ! isset ( $tabtva [ $obj -> rowid ][ $compta_tva ])) {
$tabtva [ $obj -> rowid ][ $compta_tva ] = 0 ;
}
if ( ! isset ( $tablocaltax1 [ $obj -> rowid ][ $compta_localtax1 ])) {
$tablocaltax1 [ $obj -> rowid ][ $compta_localtax1 ] = 0 ;
}
if ( ! isset ( $tablocaltax2 [ $obj -> rowid ][ $compta_localtax2 ])) {
$tablocaltax2 [ $obj -> rowid ][ $compta_localtax2 ] = 0 ;
}
2014-11-14 16:43:49 +01:00
$tabttc [ $obj -> rowid ][ $compta_soc ] += $obj -> total_ttc * $situation_ratio ;
$tabht [ $obj -> rowid ][ $compta_prod ] += $obj -> total_ht * $situation_ratio ;
2021-02-23 21:09:01 +01:00
if ( $obj -> recuperableonly != 1 ) {
$tabtva [ $obj -> rowid ][ $compta_tva ] += $obj -> total_tva * $situation_ratio ;
}
$tablocaltax1 [ $obj -> rowid ][ $compta_localtax1 ] += $obj -> total_localtax1 ;
$tablocaltax2 [ $obj -> rowid ][ $compta_localtax2 ] += $obj -> total_localtax2 ;
$tabcompany [ $obj -> rowid ] = array ( 'id' => $obj -> socid , 'name' => $obj -> name , 'client' => $obj -> client );
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-10-31 14:32:18 +01:00
dol_print_error ( $db );
2010-12-04 15:54:29 +01:00
}
2010-12-06 03:25:13 +01:00
2010-12-04 15:54:29 +01:00
/*
* Show result array
*/
2010-12-06 03:25:13 +01:00
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">' ;
2013-04-20 12:55:31 +02:00
print '<tr class="liste_titre">' ;
2010-12-06 03:25:13 +01:00
//print "<td>".$langs->trans("JournalNum")."</td>";
2013-04-20 12:55:31 +02:00
print '<td>' . $langs -> trans ( 'Date' ) . '</td><td>' . $langs -> trans ( 'Piece' ) . ' (' . $langs -> trans ( 'InvoiceRef' ) . ')</td>' ;
print '<td>' . $langs -> trans ( 'Account' ) . '</td>' ;
2019-02-10 10:25:20 +01:00
print '<td>' . $langs -> trans ( 'Type' ) . '</td><td class="right">' . $langs -> trans ( 'Debit' ) . '</td><td class="right">' . $langs -> trans ( 'Credit' ) . '</td>' ;
2010-12-04 15:54:29 +01:00
print " </tr> \n " ;
2020-04-10 10:59:32 +02:00
$invoicestatic = new Facture ( $db );
$companystatic = new Client ( $db );
2010-12-07 10:10:23 +01:00
2021-02-23 21:09:01 +01:00
foreach ( $tabfac as $key => $val ) {
2020-04-10 10:59:32 +02:00
$invoicestatic -> id = $key ;
$invoicestatic -> ref = $val [ " ref " ];
$invoicestatic -> type = $val [ " type " ];
2011-07-31 23:24:20 +02:00
2020-04-10 10:59:32 +02:00
$companystatic -> id = $tabcompany [ $key ][ 'id' ];
$companystatic -> name = $tabcompany [ $key ][ 'name' ];
$companystatic -> client = $tabcompany [ $key ][ 'client' ];
2013-04-20 12:55:31 +02:00
$lines = array (
array (
'var' => $tabttc [ $key ],
'label' => $langs -> trans ( 'ThirdParty' ) . ' (' . $companystatic -> getNomUrl ( 0 , 'customer' , 16 ) . ')' ,
'nomtcheck' => true ,
'inv' => true
),
array (
'var' => $tabht [ $key ],
'label' => $langs -> trans ( 'Products' ),
),
array (
'var' => $tabtva [ $key ],
'label' => $langs -> trans ( 'VAT' )
),
array (
'var' => $tablocaltax1 [ $key ],
'label' => $langs -> transcountry ( 'LT1' , $mysoc -> country_code )
),
array (
'var' => $tablocaltax2 [ $key ],
'label' => $langs -> transcountry ( 'LT2' , $mysoc -> country_code )
2013-08-28 13:05:14 +02:00
)
2013-04-20 12:55:31 +02:00
);
2021-02-23 21:09:01 +01:00
foreach ( $lines as $line ) {
foreach ( $line [ 'var' ] as $k => $mt ) {
if ( isset ( $line [ 'nomtcheck' ]) || $mt ) {
2017-05-10 16:41:19 +02:00
print '<tr class="oddeven">' ;
2016-08-25 10:07:19 +02:00
print " <td> " . dol_print_date ( $db -> jdate ( $val [ " date " ])) . " </td> " ;
2013-04-20 12:55:31 +02:00
print " <td> " . $invoicestatic -> getNomUrl ( 1 ) . " </td> " ;
print " <td> " . $k . " </td><td> " . $line [ 'label' ] . " </td> " ;
2021-02-23 21:09:01 +01:00
if ( isset ( $line [ 'inv' ])) {
2020-04-10 10:59:32 +02:00
print '<td class="right">' . ( $mt >= 0 ? price ( $mt ) : '' ) . " </td> " ;
print '<td class="right">' . ( $mt < 0 ? price ( - $mt ) : '' ) . " </td> " ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
print '<td class="right">' . ( $mt < 0 ? price ( - $mt ) : '' ) . " </td> " ;
2020-10-31 14:32:18 +01:00
print '<td class="right">' . ( $mt >= 0 ? price ( $mt ) : '' ) . " </td> " ;
2013-04-20 12:55:31 +02:00
}
print " </tr> " ;
}
2010-12-15 01:35:37 +01:00
}
2010-12-04 15:54:29 +01:00
}
}
print " </table> " ;
// End of page
2011-08-27 16:24:16 +02:00
llxFooter ();
2012-08-03 20:09:37 +02:00
$db -> close ();