2018-05-15 17:51:19 +02:00
< ? php
2018-09-09 09:36:12 +02:00
/* Copyright ( C ) 2018 Laurent Destailleur < eldy @ users . sourceforge . net >
2021-03-15 00:26:50 +01:00
* Copyright ( C ) 2018 - 2021 Frédéric France < frederic . france @ netlogic . fr >
2022-08-20 04:13:09 +02:00
* Copyright ( C ) 2022 Alexandre Spangaro < aspangaro @ open - dsi . fr >
2018-05-15 17:51:19 +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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2018-05-15 17:51:19 +02:00
*/
/**
2018-05-22 23:09:27 +02:00
* \file htdocs / compta / stats / byratecountry . php
2018-05-15 17:51:19 +02:00
* \brief VAT by rate
*/
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 . '/compta/tva/class/tva.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/compta/localtax/class/localtax.class.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 . '/compta/paiement/class/paiement.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/paymentexpensereport.class.php' ;
2018-05-27 09:27:09 +02:00
// Load translation files required by the page
2019-11-08 10:53:31 +01:00
$langs -> loadLangs ( array ( " other " , " compta " , " banks " , " bills " , " companies " , " product " , " trips " , " admin " , " accountancy " ));
2018-05-15 17:51:19 +02:00
2019-07-13 22:39:25 +02:00
$modecompta = ( GETPOST ( 'modecompta' , 'alpha' ) ? GETPOST ( 'modecompta' , 'alpha' ) : $conf -> global -> ACCOUNTING_MODE );
2018-05-15 17:51:19 +02:00
// Date range
2019-11-08 10:53:31 +01:00
$year = GETPOST ( " year " , 'int' );
$month = GETPOST ( " month " , 'int' );
2021-02-23 21:09:01 +01:00
if ( empty ( $year )) {
2021-03-18 14:34:44 +01:00
$year_current = dol_print_date ( dol_now (), '%Y' );
$month_current = dol_print_date ( dol_now (), '%m' );
2018-05-15 17:51:19 +02:00
$year_start = $year_current ;
} else {
$year_current = $year ;
2021-03-18 14:34:44 +01:00
$month_current = dol_print_date ( dol_now (), '%m' );
2018-05-15 17:51:19 +02:00
$year_start = $year ;
}
2021-03-31 17:37:45 +02:00
$date_start = dol_mktime ( 0 , 0 , 0 , GETPOST ( " date_startmonth " ), GETPOST ( " date_startday " ), GETPOST ( " date_startyear " ), 'tzserver' ); // We use timezone of server so report is same from everywhere
$date_end = dol_mktime ( 23 , 59 , 59 , GETPOST ( " date_endmonth " ), GETPOST ( " date_endday " ), GETPOST ( " date_endyear " ), 'tzserver' ); // We use timezone of server so report is same from everywhere
2022-08-20 04:13:09 +02:00
2018-05-15 17:51:19 +02:00
// Quarter
2022-08-20 04:13:09 +02:00
$q = '' ;
2021-02-23 21:09:01 +01:00
if ( empty ( $date_start ) || empty ( $date_end )) { // We define date_start and date_end
2019-11-08 10:53:31 +01:00
$q = GETPOST ( " q " , " int " );
2021-02-23 21:09:01 +01:00
if ( empty ( $q )) {
2018-05-15 17:51:19 +02:00
// We define date_start and date_end
2019-11-08 10:53:31 +01:00
$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 ;
2021-02-23 21:09:01 +01:00
if ( ! GETPOST ( " month " )) { // If month not forced
if ( ! GETPOST ( 'year' ) && $month_start > $month_current ) {
2018-05-15 17:51:19 +02:00
$year_start -- ;
$year_end -- ;
}
2019-11-08 10:53:31 +01:00
$month_end = $month_start - 1 ;
2021-02-23 21:09:01 +01:00
if ( $month_end < 1 ) {
$month_end = 12 ;
} else {
$year_end ++ ;
}
2018-05-15 17:51:19 +02:00
}
2021-03-01 20:37:16 +01:00
$date_start = dol_get_first_day ( $year_start , $month_start , false );
$date_end = dol_get_last_day ( $year_end , $month_end , false );
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 21:09:01 +01:00
if ( $q == 1 ) {
2021-03-01 20:37:16 +01:00
$date_start = dol_get_first_day ( $year_start , 1 , false );
$date_end = dol_get_last_day ( $year_start , 3 , false );
2021-02-23 21:09:01 +01:00
}
if ( $q == 2 ) {
2021-03-01 20:37:16 +01:00
$date_start = dol_get_first_day ( $year_start , 4 , false );
$date_end = dol_get_last_day ( $year_start , 6 , false );
2021-02-23 21:09:01 +01:00
}
if ( $q == 3 ) {
2021-03-01 20:37:16 +01:00
$date_start = dol_get_first_day ( $year_start , 7 , false );
$date_end = dol_get_last_day ( $year_start , 9 , false );
2021-02-23 21:09:01 +01:00
}
if ( $q == 4 ) {
2021-03-01 20:37:16 +01:00
$date_start = dol_get_first_day ( $year_start , 10 , false );
$date_end = dol_get_last_day ( $year_start , 12 , false );
2021-02-23 21:09:01 +01:00
}
2018-05-15 17:51:19 +02:00
}
}
// $date_start and $date_end are defined. We force $year_start and $nbofyear
2019-11-08 10:53:31 +01:00
$tmps = dol_getdate ( $date_start );
2018-05-15 17:51:19 +02:00
$year_start = $tmps [ 'year' ];
2019-11-08 10:53:31 +01:00
$tmpe = dol_getdate ( $date_end );
2018-05-15 17:51:19 +02:00
$year_end = $tmpe [ 'year' ];
$tmp_date_end = dol_time_plus_duree ( $date_start , 1 , 'y' ) - 1 ;
2021-02-23 21:09:01 +01:00
if ( $tmp_date_end < $date_end || $date_end < $date_start ) {
$date_end = $tmp_date_end ;
}
2018-05-15 17:51:19 +02:00
2019-01-27 11:55:16 +01:00
$min = price2num ( GETPOST ( " min " , " alpha " ));
2021-02-23 21:09:01 +01:00
if ( empty ( $min )) {
$min = 0 ;
}
2018-05-15 17:51:19 +02:00
// Define modetax (0 or 1)
// 0=normal, 1=option vat for services is on debit, 2=option on payments for products
2021-03-15 10:56:10 +01:00
$modetax = empty ( $conf -> global -> TAX_MODE ) ? 0 : $conf -> global -> TAX_MODE ;
2021-02-23 21:09:01 +01:00
if ( GETPOSTISSET ( " modetax " )) {
$modetax = GETPOST ( " modetax " , 'int' );
}
if ( empty ( $modetax )) {
$modetax = 0 ;
}
2018-05-15 17:51:19 +02:00
// Security check
2019-01-27 11:55:16 +01:00
$socid = GETPOST ( 'socid' , 'int' );
2021-02-23 21:09:01 +01:00
if ( $user -> socid ) {
$socid = $user -> socid ;
}
2018-05-15 17:51:19 +02:00
$result = restrictedArea ( $user , 'tax' , '' , '' , 'charges' );
/*
* View
*/
2019-11-08 10:53:31 +01:00
$form = new Form ( $db );
$company_static = new Societe ( $db );
$invoice_customer = new Facture ( $db );
$invoice_supplier = new FactureFournisseur ( $db );
$expensereport = new ExpenseReport ( $db );
$product_static = new Product ( $db );
$payment_static = new Paiement ( $db );
$paymentfourn_static = new PaiementFourn ( $db );
$paymentexpensereport_static = new PaymentExpenseReport ( $db );
$morequerystring = '' ;
$listofparams = array ( 'date_startmonth' , 'date_startyear' , 'date_startday' , 'date_endmonth' , 'date_endyear' , 'date_endday' );
2021-02-23 21:09:01 +01:00
foreach ( $listofparams as $param ) {
if ( GETPOST ( $param ) != '' ) {
$morequerystring .= ( $morequerystring ? '&' : '' ) . $param . '=' . GETPOST ( $param );
}
2018-05-15 17:51:19 +02:00
}
2019-01-27 11:55:16 +01:00
llxHeader ( '' , $langs -> trans ( " TurnoverReport " ), '' , '' , 0 , 0 , '' , '' , $morequerystring );
2018-05-15 17:51:19 +02:00
2022-08-20 04:13:09 +02:00
$exportlink = " " ;
$namelink = " " ;
2018-05-15 17:51:19 +02:00
//print load_fiche_titre($langs->trans("VAT"),"");
//$fsearch.='<br>';
2022-08-20 04:13:09 +02:00
$fsearch = '' ;
2019-11-08 10:53:31 +01:00
$fsearch .= ' <input type="hidden" name="year" value="' . $year . '">' ;
$fsearch .= ' <input type="hidden" name="modetax" value="' . $modetax . '">' ;
2018-05-15 17:51:19 +02:00
//$fsearch.=' '.$langs->trans("SalesTurnoverMinimum").': ';
//$fsearch.=' <input type="text" name="min" value="'.$min.'">';
// Show report header
2019-11-08 10:53:31 +01:00
$name = $langs -> trans ( " xxx " );
$calcmode = '' ;
2021-02-23 21:09:01 +01:00
if ( $modetax == 0 ) {
$calcmode = $langs -> trans ( 'OptionVATDefault' );
}
if ( $modetax == 1 ) {
$calcmode = $langs -> trans ( 'OptionVATDebitOption' );
}
if ( $modetax == 2 ) {
$calcmode = $langs -> trans ( 'OptionPaymentForProductAndServices' );
}
2019-11-08 10:53:31 +01:00
$calcmode .= '<br>(' . $langs -> trans ( " TaxModuleSetupToModifyRules " , DOL_URL_ROOT . '/admin/taxes.php' ) . ')' ;
2018-05-15 17:51:19 +02:00
// Set period
2021-03-31 17:37:45 +02:00
$period = $form -> selectDate ( $date_start , 'date_start' , 0 , 0 , 0 , '' , 1 , 0 , 0 , '' , '' , '' , '' , 1 , '' , '' , 'tzserver' );
$period .= ' - ' ;
$period .= $form -> selectDate ( $date_end , 'date_end' , 0 , 0 , 0 , '' , 1 , 0 , 0 , '' , '' , '' , '' , 1 , '' , '' , 'tzserver' );
2021-03-01 20:37:16 +01:00
$prevyear = $year_start ;
$prevquarter = $q ;
2018-05-15 17:51:19 +02:00
if ( $prevquarter > 1 ) {
$prevquarter -- ;
} else {
2021-03-01 20:37:16 +01:00
$prevquarter = 4 ;
$prevyear -- ;
2018-05-15 17:51:19 +02:00
}
2021-03-01 20:37:16 +01:00
$nextyear = $year_start ;
$nextquarter = $q ;
2018-05-15 17:51:19 +02:00
if ( $nextquarter < 4 ) {
$nextquarter ++ ;
} else {
2021-03-01 20:37:16 +01:00
$nextquarter = 1 ;
$nextyear ++ ;
2018-05-15 17:51:19 +02:00
}
2022-08-20 04:13:09 +02:00
$description = $fsearch ;
2019-11-08 10:53:31 +01:00
$builddate = dol_now ();
2022-08-20 04:13:09 +02:00
if ( ! empty ( $conf -> global -> MAIN_MODULE_ACCOUNTING )) {
$description .= '<br>' . $langs -> trans ( " ThisIsAnEstimatedValue " );
}
2021-02-23 21:09:01 +01:00
if ( $conf -> global -> TAX_MODE_SELL_PRODUCT == 'invoice' ) {
2022-08-20 04:13:09 +02:00
$description .= '<br>' . $langs -> trans ( " RulesVATDueProducts " );
2021-02-23 21:09:01 +01:00
}
if ( $conf -> global -> TAX_MODE_SELL_PRODUCT == 'payment' ) {
2022-08-20 04:13:09 +02:00
$description .= '<br>' . $langs -> trans ( " RulesVATInProducts " );
2021-02-23 21:09:01 +01:00
}
if ( $conf -> global -> TAX_MODE_SELL_SERVICE == 'invoice' ) {
$description .= '<br>' . $langs -> trans ( " RulesVATDueServices " );
}
if ( $conf -> global -> TAX_MODE_SELL_SERVICE == 'payment' ) {
$description .= '<br>' . $langs -> trans ( " RulesVATInServices " );
}
2019-11-08 10:53:31 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$description .= '<br>' . $langs -> trans ( " DepositsAreNotIncluded " );
2018-05-15 17:51:19 +02:00
}
2022-10-12 12:14:04 +02:00
if ( ! empty ( $conf -> global -> FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS )) {
$description .= $langs -> trans ( " SupplierDepositsAreNotIncluded " );
}
2018-05-15 17:51:19 +02:00
// Customers invoices
2019-11-08 10:53:31 +01:00
$elementcust = $langs -> trans ( " CustomersInvoices " );
$productcust = $langs -> trans ( " ProductOrService " );
$amountcust = $langs -> trans ( " AmountHT " );
2018-05-15 17:51:19 +02:00
// Suppliers invoices
2019-11-08 10:53:31 +01:00
$elementsup = $langs -> trans ( " SuppliersInvoices " );
$productsup = $productcust ;
$amountsup = $amountcust ;
2018-05-15 17:51:19 +02:00
2018-06-26 11:18:31 +02:00
// TODO Report from bookkeeping not yet available, so we switch on report on business events
2021-02-23 21:09:01 +01:00
if ( $modecompta == " BOOKKEEPING " ) {
$modecompta = " CREANCES-DETTES " ;
}
if ( $modecompta == " BOOKKEEPINGCOLLECTED " ) {
$modecompta = " RECETTES-DEPENSES " ;
}
2018-05-15 17:51:19 +02:00
// Show report header
2019-11-08 10:53:31 +01:00
if ( $modecompta == " CREANCES-DETTES " ) {
$name = $langs -> trans ( " Turnover " ) . ', ' . $langs -> trans ( " ByVatRate " );
$calcmode = $langs -> trans ( " CalcModeDebt " );
2018-06-26 11:18:31 +02:00
//$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')';
2018-05-15 17:51:19 +02:00
2022-08-20 04:13:09 +02:00
$description .= '<br>' . $langs -> trans ( " RulesCADue " );
2019-11-08 10:53:31 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$description .= $langs -> trans ( " DepositsAreNotIncluded " );
2018-05-15 17:51:19 +02:00
} else {
2019-11-08 10:53:31 +01:00
$description .= $langs -> trans ( " DepositsAreIncluded " );
2018-05-15 17:51:19 +02:00
}
2022-10-12 12:14:04 +02:00
if ( ! empty ( $conf -> global -> FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS )) {
$description .= $langs -> trans ( " SupplierDepositsAreNotIncluded " );
}
2018-05-15 17:51:19 +02:00
2019-11-08 10:53:31 +01:00
$builddate = dol_now ();
2021-02-23 21:09:01 +01:00
} elseif ( $modecompta == " RECETTES-DEPENSES " ) {
2019-11-08 10:53:31 +01:00
$name = $langs -> trans ( " TurnoverCollected " ) . ', ' . $langs -> trans ( " ByVatRate " );
$calcmode = $langs -> trans ( " CalcModeEngagement " );
2018-06-26 11:18:31 +02:00
//$calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year_start.'&modecompta=CREANCES-DETTES">','</a>').')';
2018-05-15 17:51:19 +02:00
2022-08-20 04:13:09 +02:00
$description .= $langs -> trans ( " RulesCAIn " );
2019-11-08 10:53:31 +01:00
$description .= $langs -> trans ( " DepositsAreIncluded " );
2018-05-15 17:51:19 +02:00
2019-11-08 10:53:31 +01:00
$builddate = dol_now ();
2021-02-23 21:09:01 +01:00
} elseif ( $modecompta == " BOOKKEEPING " ) {
} elseif ( $modecompta == " BOOKKEEPINGCOLLECTED " ) {
2018-05-15 17:51:19 +02:00
}
2021-03-31 17:37:45 +02:00
$period = $form -> selectDate ( $date_start , 'date_start' , 0 , 0 , 0 , '' , 1 , 0 );
$period .= ' - ' ;
$period .= $form -> selectDate ( $date_end , 'date_end' , 0 , 0 , 0 , '' , 1 , 0 );
2021-02-23 21:09:01 +01:00
if ( $date_end == dol_time_plus_duree ( $date_start , 1 , 'y' ) - 1 ) {
$periodlink = '<a href="' . $_SERVER [ " PHP_SELF " ] . '?year=' . ( $year_start - 1 ) . '&modecompta=' . $modecompta . '">' . img_previous () . '</a> <a href="' . $_SERVER [ " PHP_SELF " ] . '?year=' . ( $year_start + 1 ) . '&modecompta=' . $modecompta . '">' . img_next () . '</a>' ;
} else {
$periodlink = '' ;
}
2018-05-15 17:51:19 +02:00
2019-11-08 10:53:31 +01:00
$description .= ' <input type="hidden" name="modecompta" value="' . $modecompta . '">' ;
2018-05-15 17:51:19 +02:00
2019-01-27 11:55:16 +01:00
report_header ( $name , '' , $period , $periodlink , $description , $builddate , $exportlink , array (), $calcmode );
2018-05-15 17:51:19 +02:00
2022-08-29 11:34:00 +02:00
if ( isModEnabled ( 'accounting' ) && $modecompta != 'BOOKKEEPING' ) {
2018-05-15 17:51:19 +02:00
print info_admin ( $langs -> trans ( " WarningReportNotReliable " ), 0 , 0 , 1 );
}
2019-10-26 18:13:20 +02:00
if ( $modecompta == 'CREANCES-DETTES' ) {
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">' ;
2019-11-08 10:53:31 +01:00
print '<tr class="liste_titre"><td width="6%" class="right">' . $langs -> trans ( " TurnoverbyVatrate " ) . '</td>' ;
print '<td class="left">' . $langs -> trans ( " ProductOrService " ) . '</td>' ;
print '<td class="left">' . $langs -> trans ( " Country " ) . '</td>' ;
$i = 0 ;
while ( $i < 12 ) {
$j = $i + ( empty ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ) ? 1 : $conf -> global -> SOCIETE_FISCAL_MONTH_START );
2021-02-23 21:09:01 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2019-11-08 10:53:31 +01:00
print '<td width="60" class="right">' . $langs -> trans ( 'MonthShort' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT )) . '</td>' ;
2019-10-26 18:13:20 +02:00
$i ++ ;
}
2019-11-08 10:53:31 +01:00
print '<td width="60" class="right"><b>' . $langs -> trans ( " TotalHT " ) . '</b></td></tr>' ;
2018-05-15 17:51:19 +02:00
2021-03-31 17:59:42 +02:00
// Sales invoices
2019-10-26 18:13:20 +02:00
$sql = " SELECT fd.tva_tx AS vatrate, " ;
$sql .= " fd.product_type AS product_type, " ;
2019-12-09 09:16:59 +01:00
$sql .= " cc.code, cc.label AS country, " ;
2019-11-08 10:53:31 +01:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
2021-08-28 00:55:51 +02:00
$sql .= " SUM( " . $db -> ifsql ( " MONTH(f.datef)= " . $i , " fd.total_ht " , " 0 " ) . " ) AS month " . str_pad ( $i , 2 , " 0 " , STR_PAD_LEFT ) . " , " ;
2019-10-26 18:13:20 +02:00
}
$sql .= " SUM(fd.total_ht) as total " ;
2019-11-08 10:53:31 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as fd " ;
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " facture as f ON f.rowid = fd.fk_facture " ;
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " societe as soc ON soc.rowid = f.fk_soc " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_country as cc ON cc.rowid = soc.fk_pays " ;
$sql .= " WHERE f.datef >= ' " . $db -> idate ( $date_start ) . " ' " ;
$sql .= " AND f.datef <= ' " . $db -> idate ( $date_end ) . " ' " ;
$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,5) " ;
2019-10-26 18:13:20 +02:00
} else {
2019-11-08 10:53:31 +01:00
$sql .= " AND f.type IN (0,1,2,3,5) " ;
2019-10-26 18:13:20 +02:00
}
2019-11-08 10:53:31 +01:00
$sql .= " AND f.entity IN ( " . getEntity ( 'invoice' , 0 ) . " ) " ;
2020-06-06 15:46:43 +02:00
$sql .= " GROUP BY fd.tva_tx,fd.product_type, cc.label, cc.code " ;
$sql .= " ORDER BY country, product_type, vatrate " ;
2021-08-27 22:42:04 +02:00
dol_syslog ( " htdocs/compta/tva/index.php " , LOG_DEBUG );
2019-10-26 18:13:20 +02:00
$resql = $db -> query ( $sql );
if ( $resql ) {
$num = $db -> num_rows ( $resql );
$totalpermonth = array ();
2019-11-08 10:53:31 +01:00
while ( $obj = $db -> fetch_object ( $resql )) {
print '<tr class="oddeven"><td class="right">' . vatrate ( $obj -> vatrate ) . '</td>' ;
2019-10-26 18:13:20 +02:00
if ( $obj -> product_type == 0 ) {
2019-11-08 10:53:31 +01:00
print '<td class="left">' . $langs -> trans ( " Product " ) . '</td>' ;
2019-10-26 18:13:20 +02:00
} else {
2019-11-08 10:53:31 +01:00
print '<td class="left">' . $langs -> trans ( " Service " ) . '</td>' ;
2019-10-26 18:13:20 +02:00
}
2019-12-09 09:16:59 +01:00
// Country
print '<td>' ;
print $langs -> trans ( " Country " . $obj -> code ) != " Country " . $obj -> code ? $langs -> trans ( " Country " . $obj -> code ) : $obj -> country ;
print '</td>' ;
2019-11-08 10:53:31 +01:00
for ( $i = 0 ; $i < 12 ; $i ++ ) {
$j = $i + ( empty ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ) ? 1 : $conf -> global -> SOCIETE_FISCAL_MONTH_START );
2021-02-23 21:09:01 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2019-10-26 18:13:20 +02:00
$monthj = 'month' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT );
2019-11-08 10:53:31 +01:00
print '<td class="right" width="6%">' . price ( $obj -> $monthj ) . '</td>' ;
$totalpermonth [ $j ] = ( empty ( $totalpermonth [ $j ]) ? 0 : $totalpermonth [ $j ]) + $obj -> $monthj ;
2019-10-26 18:13:20 +02:00
}
2019-11-08 10:53:31 +01:00
print '<td class="right" width="6%"><b>' . price ( $obj -> total ) . '</b></td>' ;
$totalpermonth [ 'total' ] = ( empty ( $totalpermonth [ 'total' ]) ? 0 : $totalpermonth [ 'total' ]) + $obj -> total ;
2019-10-26 18:13:20 +02:00
print '</tr>' ;
2018-05-15 17:51:19 +02:00
}
2019-10-26 18:13:20 +02:00
$db -> free ( $resql );
// Total
print '<tr class="liste_total"><td class="right"></td>' ;
print '<td class="left"></td>' ;
print '<td></td>' ;
2019-11-08 10:53:31 +01:00
for ( $i = 0 ; $i < 12 ; $i ++ ) {
$j = $i + ( empty ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ) ? 1 : $conf -> global -> SOCIETE_FISCAL_MONTH_START );
2021-02-23 21:09:01 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2018-05-22 23:09:27 +02:00
$monthj = 'month' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT );
2019-11-08 10:53:31 +01:00
print '<td class="right" width="6%">' . price ( $totalpermonth [ $j ]) . '</td>' ;
2018-05-15 17:51:19 +02:00
}
2019-11-08 10:53:31 +01:00
print '<td class="right" width="6%"><b>' . price ( $totalpermonth [ 'total' ]) . '</b></td>' ;
2018-05-15 17:51:19 +02:00
print '</tr>' ;
2019-10-26 18:13:20 +02:00
} else {
print $db -> lasterror (); // Show last sql error
2018-05-15 17:51:19 +02:00
}
2018-05-22 23:09:27 +02:00
2019-11-08 10:53:31 +01:00
print '<tr class="liste_titre"><td width="6%" class="right">' . $langs -> trans ( " PurchasebyVatrate " ) . '</td>' ;
print '<td class="left">' . $langs -> trans ( " ProductOrService " ) . '</td>' ;
print '<td class="left">' . $langs -> trans ( " Country " ) . '</td>' ;
$i = 0 ;
2021-02-23 21:09:01 +01:00
while ( $i < 12 ) {
2019-11-08 10:53:31 +01:00
$j = $i + ( empty ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ) ? 1 : $conf -> global -> SOCIETE_FISCAL_MONTH_START );
2021-02-23 21:09:01 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2019-11-08 10:53:31 +01:00
print '<td width="60" class="right">' . $langs -> trans ( 'MonthShort' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT )) . '</td>' ;
2019-10-26 18:13:20 +02:00
$i ++ ;
2018-05-22 23:09:27 +02:00
}
2019-11-08 10:53:31 +01:00
print '<td width="60" class="right"><b>' . $langs -> trans ( " TotalHT " ) . '</b></td></tr>' ;
2018-05-22 23:09:27 +02:00
2021-03-31 17:59:42 +02:00
// Purchase invoices
2019-10-26 18:13:20 +02:00
$sql2 = " SELECT ffd.tva_tx AS vatrate, " ;
$sql2 .= " ffd.product_type AS product_type, " ;
2019-12-09 09:16:59 +01:00
$sql2 .= " cc.code, cc.label AS country, " ;
2019-11-08 10:53:31 +01:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
2021-08-28 00:55:51 +02:00
$sql2 .= " SUM( " . $db -> ifsql ( " MONTH(ff.datef)= " . $i , " ffd.total_ht " , " 0 " ) . " ) AS month " . str_pad ( $i , 2 , " 0 " , STR_PAD_LEFT ) . " , " ;
2019-10-26 18:13:20 +02:00
}
$sql2 .= " SUM(ffd.total_ht) as total " ;
2019-11-08 10:53:31 +01:00
$sql2 .= " FROM " . MAIN_DB_PREFIX . " facture_fourn_det as ffd " ;
$sql2 .= " INNER JOIN " . MAIN_DB_PREFIX . " facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn " ;
$sql2 .= " INNER JOIN " . MAIN_DB_PREFIX . " societe as soc ON soc.rowid = ff.fk_soc " ;
$sql2 .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_country as cc ON cc.rowid = soc.fk_pays " ;
$sql2 .= " WHERE ff.datef >= ' " . $db -> idate ( $date_start ) . " ' " ;
$sql2 .= " AND ff.datef <= ' " . $db -> idate ( $date_end ) . " ' " ;
$sql .= " AND ff.fk_statut in (1,2) " ;
2022-10-12 12:14:04 +02:00
if ( ! empty ( $conf -> global -> FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS )) {
2019-11-08 10:53:31 +01:00
$sql .= " AND ff.type IN (0,1,2,5) " ;
2019-10-26 18:13:20 +02:00
} else {
2019-11-08 10:53:31 +01:00
$sql .= " AND ff.type IN (0,1,2,3,5) " ;
2019-10-26 18:13:20 +02:00
}
2019-11-08 10:53:31 +01:00
$sql2 .= " AND ff.entity IN ( " . getEntity ( " facture_fourn " , 0 ) . " ) " ;
2020-06-06 15:46:43 +02:00
$sql2 .= " GROUP BY ffd.tva_tx, ffd.product_type, cc.label, cc.code " ;
$sql2 .= " ORDER BY country, product_type, vatrate " ;
2019-10-26 18:13:20 +02:00
//print $sql2;
2021-08-27 22:42:04 +02:00
dol_syslog ( " htdocs/compta/tva/index.php " , LOG_DEBUG );
2019-10-26 18:13:20 +02:00
$resql2 = $db -> query ( $sql2 );
if ( $resql2 ) {
$num = $db -> num_rows ( $resql2 );
$totalpermonth = array ();
2019-11-08 10:53:31 +01:00
while ( $obj = $db -> fetch_object ( $resql2 )) {
print '<tr class="oddeven"><td class="right">' . vatrate ( $obj -> vatrate ) . '</td>' ;
2019-10-26 18:13:20 +02:00
if ( $obj -> product_type == 0 ) {
2019-11-08 10:53:31 +01:00
print '<td class="left">' . $langs -> trans ( " Product " ) . '</td>' ;
2019-10-26 18:13:20 +02:00
} else {
2019-11-08 10:53:31 +01:00
print '<td class="left">' . $langs -> trans ( " Service " ) . '</td>' ;
2019-10-26 18:13:20 +02:00
}
2019-12-09 09:16:59 +01:00
print '<td>' ;
print $langs -> trans ( " Country " . $obj -> code ) != " Country " . $obj -> code ? $langs -> trans ( " Country " . $obj -> code ) : $obj -> country ;
print '</td>' ;
2019-11-08 10:53:31 +01:00
for ( $i = 0 ; $i < 12 ; $i ++ ) {
$j = $i + ( empty ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ) ? 1 : $conf -> global -> SOCIETE_FISCAL_MONTH_START );
2021-02-23 21:09:01 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2019-10-26 18:13:20 +02:00
$monthj = 'month' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT );
2019-11-08 10:53:31 +01:00
print '<td class="right" width="6%">' . price ( $obj -> $monthj ) . '</td>' ;
$totalpermonth [ $j ] = ( empty ( $totalpermonth [ $j ]) ? 0 : $totalpermonth [ $j ]) + $obj -> $monthj ;
2019-10-26 18:13:20 +02:00
}
2019-11-08 10:53:31 +01:00
print '<td class="right" width="6%"><b>' . price ( $obj -> total ) . '</b></td>' ;
$totalpermonth [ 'total' ] = ( empty ( $totalpermonth [ 'total' ]) ? 0 : $totalpermonth [ 'total' ]) + $obj -> total ;
2019-10-26 18:13:20 +02:00
print '</tr>' ;
2018-05-15 17:51:19 +02:00
}
2019-10-26 18:13:20 +02:00
$db -> free ( $resql2 );
// Total
print '<tr class="liste_total"><td class="right"></td>' ;
print '<td class="left"></td>' ;
print '<td></td>' ;
2019-11-08 10:53:31 +01:00
for ( $i = 0 ; $i < 12 ; $i ++ ) {
$j = $i + ( empty ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ) ? 1 : $conf -> global -> SOCIETE_FISCAL_MONTH_START );
2021-02-23 21:09:01 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2018-05-22 23:09:27 +02:00
$monthj = 'month' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT );
2019-11-08 10:53:31 +01:00
print '<td class="right" width="6%">' . price ( $totalpermonth [ $j ]) . '</td>' ;
2018-05-15 17:51:19 +02:00
}
2019-11-08 10:53:31 +01:00
print '<td class="right" width="6%"><b>' . price ( $totalpermonth [ 'total' ]) . '</b></td>' ;
2018-05-15 17:51:19 +02:00
print '</tr>' ;
2019-10-26 18:13:20 +02:00
} else {
print $db -> lasterror (); // Show last sql error
2018-05-15 17:51:19 +02:00
}
2019-10-26 18:13:20 +02:00
print " </table> \n " ;
2018-05-15 17:51:19 +02:00
} else {
// $modecompta != 'CREANCES-DETTES'
// "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.
2019-11-08 10:53:31 +01:00
print '<br>' . $langs -> trans ( " TurnoverPerSaleTaxRateInCommitmentAccountingNotRelevant " ) . '<br>' ;
2018-05-15 17:51:19 +02:00
}
2018-08-08 12:29:36 +02:00
// End of page
2018-05-15 17:51:19 +02:00
llxFooter ();
$db -> close ();