dolibarr/htdocs/compta/resultat/clientfourn.php

761 lines
26 KiB
PHP
Raw Normal View History

<?php
2006-12-23 16:40:17 +01:00
/* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
2011-02-22 10:01:34 +01:00
* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
2012-12-30 15:13:49 +01:00
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
*
* 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
2011-08-03 02:45:22 +02:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
2010-02-02 22:23:23 +01:00
* \file htdocs/compta/resultat/clientfourn.php
* \brief Page reporting
2010-02-02 22:23:23 +01:00
*/
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.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';
$langs->load("bills");
2012-08-03 20:09:37 +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');
2010-02-03 00:23:20 +01:00
// Security check
2012-02-27 22:26:22 +01:00
$socid = GETPOST('socid','int');
2010-02-03 00:23:20 +01:00
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');
2010-02-02 22:23:23 +01:00
// Date range
$year=GETPOST("year");
2010-02-02 22:23:23 +01:00
if (empty($year))
{
2011-05-28 14:19:49 +02:00
$year_current = strftime("%Y",dol_now());
$month_current = strftime("%m",dol_now());
$year_start = $year_current;
2010-02-02 22:23:23 +01:00
} else {
2011-05-28 14:19:49 +02:00
$year_current = $year;
$month_current = strftime("%m",dol_now());
$year_start = $year;
2010-02-02 22:23:23 +01:00
}
2012-08-03 20:09:37 +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-02-02 22:23:23 +01:00
// Quarter
if (empty($date_start) || empty($date_end)) // We define date_start and date_end
{
2011-05-28 14:19:49 +02:00
$q=GETPOST("q")?GETPOST("q"):0;
if ($q==0)
{
// We define date_start and date_end
$year_end=$year_start;
$month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
if (! GETPOST('month'))
{
if (! GETPOST("year") && $month_start > $month_current)
2011-05-28 14:19:49 +02:00
{
$year_start--;
$year_end--;
}
$month_end=$month_start-1;
if ($month_end < 1) $month_end=12;
else $year_end++;
}
else $month_end=$month_start;
$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); }
2010-02-02 22:23:23 +01:00
}
else
{
2011-05-28 14:19:49 +02:00
// TODO We define q
2010-02-02 22:23:23 +01:00
}
2008-02-16 18:50:25 +01:00
// Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
2012-08-03 20:09:37 +02:00
$modecompta=(GETPOST("modecompta")?GETPOST("modecompta"):$conf->global->COMPTA_MODE);
2010-02-03 00:23:20 +01:00
/*
* View
*/
llxHeader();
$form=new Form($db);
2012-08-03 20:09:37 +02:00
$nomlink='';
$periodlink='';
$exportlink='';
$total_ht=0;
$total_ttc=0;
// Affiche en-tete de rapport
if ($modecompta=="CREANCES-DETTES")
{
2011-05-28 14:19:49 +02:00
$nom=$langs->trans("AnnualByCompaniesDueDebtMode");
$calcmode=$langs->trans("CalcModeDebt");
$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month")>0?'&month='.GETPOST("month"):'').'&modecompta=RECETTES-DEPENSES">','</a>').')';
$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);
2011-05-28 14:19:49 +02:00
//$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
$description=$langs->trans("RulesResultDue");
if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
else $description.= $langs->trans("DepositsAreIncluded");
2011-05-28 14:19:49 +02:00
$builddate=time();
//$exportlink=$langs->trans("NotYetAvailable");
}
else {
2011-05-28 14:19:49 +02:00
$nom=$langs->trans("AnnualByCompaniesInputOutputMode");
$calcmode=$langs->trans("CalcModeEngagement");
$calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month")>0?'&month='.GETPOST("month"):'').'&modecompta=CREANCES-DETTES">','</a>').')';
//$period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',1,1,0,'',1,0,1);
$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);
2011-05-28 14:19:49 +02:00
//$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
$description=$langs->trans("RulesResultInOut");
$builddate=time();
//$exportlink=$langs->trans("NotYetAvailable");
}
report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink,array('modecompta'=>$modecompta),$calcmode);
2010-02-03 00:23:20 +01:00
// Show report array
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
2011-07-20 21:03:31 +02:00
print '<td width="10%">&nbsp;</td><td>&nbsp;</td>';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print "<td align=\"right\">".$langs->trans("AmountHT")."</td>";
print "<td align=\"right\">".$langs->trans("AmountTTC")."</td>";
print "</tr>\n";
/*
* Factures clients
*/
2010-02-03 00:23:20 +01:00
print '<tr><td colspan="4">'.$langs->trans("CustomersInvoices").'</td></tr>';
if ($modecompta == 'CREANCES-DETTES')
{
2011-05-28 14:19:49 +02:00
$sql = "SELECT s.nom, s.rowid as socid, sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture as f";
$sql.= " WHERE f.fk_soc = s.rowid";
$sql.= " AND f.fk_statut IN (1,2)";
2012-08-03 20:09:37 +02:00
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 (! empty($date_start) && ! empty($date_end))
$sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
}
else
{
2011-05-28 14:19:49 +02:00
/*
* Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
* vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
*/
$sql = "SELECT s.nom as nom, s.rowid as socid, sum(pf.amount) as amount_ttc";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture as f";
$sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
$sql.= ", ".MAIN_DB_PREFIX."paiement as p";
$sql.= " WHERE p.rowid = pf.fk_paiement";
$sql.= " AND pf.fk_facture = f.rowid";
$sql.= " AND f.fk_soc = s.rowid";
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
2009-02-11 22:15:47 +01:00
}
$sql.= " AND f.entity = ".$conf->entity;
if ($socid) $sql.= " AND f.fk_soc = ".$socid;
2010-11-16 23:04:07 +01:00
$sql.= " GROUP BY s.nom, s.rowid";
$sql.= " ORDER BY s.nom, s.rowid";
2010-02-07 00:10:54 +01:00
dol_syslog("get customer invoices sql=".$sql);
$result = $db->query($sql);
if ($result) {
2011-05-28 14:19:49 +02:00
$num = $db->num_rows($result);
$i = 0;
$var=true;
while ($i < $num)
{
$objp = $db->fetch_object($result);
$var=!$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
print "<td>".$langs->trans("Bills").' <a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$objp->socid.'">'.$objp->nom."</td>\n";
2011-05-28 14:19:49 +02:00
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print "<td align=\"right\">".price($objp->amount_ht)."</td>\n";
2011-05-28 14:19:49 +02:00
print "<td align=\"right\">".price($objp->amount_ttc)."</td>\n";
2012-08-03 20:09:37 +02:00
$total_ht += (isset($objp->amount_ht)?$objp->amount_ht:0);
$total_ttc += $objp->amount_ttc;
2011-05-28 14:19:49 +02:00
print "</tr>\n";
$i++;
}
$db->free($result);
} else {
2011-05-28 14:19:49 +02:00
dol_print_error($db);
}
2009-02-11 22:15:47 +01:00
// On ajoute les paiements clients anciennes version, non lie par paiement_facture
if ($modecompta != 'CREANCES-DETTES')
2009-02-11 22:15:47 +01:00
{
2011-05-28 14:19:49 +02:00
$sql = "SELECT 'Autres' as nom, '0' as idp, sum(p.amount) as amount_ttc";
$sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
$sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
$sql.= ", ".MAIN_DB_PREFIX."paiement as p";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
$sql.= " WHERE pf.rowid IS NULL";
$sql.= " AND p.fk_bank = b.rowid";
$sql.= " AND b.fk_account = ba.rowid";
$sql.= " AND ba.entity = ".$conf->entity;
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
2011-05-28 14:19:49 +02:00
$sql.= " GROUP BY nom, idp";
$sql.= " ORDER BY nom";
dol_syslog("get old customer payments not linked to invoices sql=".$sql);
$result = $db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$i = 0;
if ($num) {
while ($i < $num)
{
$objp = $db->fetch_object($result);
$var=!$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print "<td>".$langs->trans("Bills")." ".$langs->trans("Other")." (".$langs->trans("PaymentsNotLinkedToInvoice").")\n";
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print "<td align=\"right\">".price($objp->amount_ht)."</td>\n";
2011-05-28 14:19:49 +02:00
print "<td align=\"right\">".price($objp->amount_ttc)."</td>\n";
2012-08-03 20:09:37 +02:00
$total_ht += (isset($objp->amount_ht)?$objp->amount_ht:0);
$total_ttc += $objp->amount_ttc;
2011-05-28 14:19:49 +02:00
print "</tr>\n";
$i++;
}
}
$db->free($result);
} else {
dol_print_error($db);
}
}
if ($total_ttc == 0)
{
2011-05-28 14:19:49 +02:00
$var=!$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print '<td colspan="3">'.$langs->trans("None").'</td>';
print '</tr>';
}
print '<tr class="liste_total">';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print '<td colspan="3" align="right">'.price($total_ht).'</td>';
print '<td colspan="3" align="right">'.price($total_ttc).'</td>';
print '</tr>';
/*
* Suppliers invoices
*/
if ($modecompta == 'CREANCES-DETTES')
2009-02-11 22:15:47 +01:00
{
$sql = "SELECT s.nom, s.rowid as socid, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";
2011-05-28 14:19:49 +02:00
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture_fourn as f";
$sql.= " WHERE f.fk_soc = s.rowid";
$sql.= " AND f.fk_statut IN (1,2)";
2012-08-03 20:09:37 +02:00
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 (! empty($date_start) && ! empty($date_end))
$sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
}
else
{
$sql = "SELECT s.nom, s.rowid as socid, sum(pf.amount) as amount_ttc";
2012-02-20 23:51:00 +01:00
$sql.= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";
2011-05-28 14:19:49 +02:00
$sql.= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";
2012-02-20 23:51:00 +01:00
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f";
$sql.= " ON pf.fk_facturefourn = f.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s";
$sql.= " ON f.fk_soc = s.rowid";
$sql.= " WHERE p.rowid = pf.fk_paiementfourn ";
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
}
$sql.= " AND f.entity = ".$conf->entity;
if ($socid) $sql.= " AND f.fk_soc = ".$socid;
$sql .= " GROUP BY s.nom, s.rowid";
2010-11-16 23:04:07 +01:00
$sql .= " ORDER BY s.nom, s.rowid";
2010-02-03 00:23:20 +01:00
print '<tr><td colspan="4">'.$langs->trans("SuppliersInvoices").'</td></tr>';
2010-02-07 00:10:54 +01:00
$subtotal_ht = 0;
$subtotal_ttc = 0;
2010-02-07 00:10:54 +01:00
dol_syslog("get suppliers invoices sql=".$sql);
$result = $db->query($sql);
if ($result) {
2011-05-28 14:19:49 +02:00
$num = $db->num_rows($result);
$i = 0;
$var=true;
if ($num > 0)
{
while ($i < $num)
{
2011-05-28 14:19:49 +02:00
$objp = $db->fetch_object($result);
$var=!$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2013-07-26 11:35:00 +02:00
print "<td>".$langs->trans("Bills")." <a href=\"".DOL_URL_ROOT."/fourn/facture/list.php?socid=".$objp->socid."\">".$objp->nom."</a></td>\n";
2011-05-28 14:19:49 +02:00
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print "<td align=\"right\">".price(-$objp->amount_ht)."</td>\n";
2011-05-28 14:19:49 +02:00
print "<td align=\"right\">".price(-$objp->amount_ttc)."</td>\n";
2012-08-03 20:09:37 +02:00
$total_ht -= (isset($objp->amount_ht)?$objp->amount_ht:0);
$total_ttc -= $objp->amount_ttc;
$subtotal_ht += (isset($objp->amount_ht)?$objp->amount_ht:0);
$subtotal_ttc += $objp->amount_ttc;
2011-05-28 14:19:49 +02:00
print "</tr>\n";
$i++;
}
}
else
{
2011-05-28 14:19:49 +02:00
$var=!$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print '<td colspan="3">'.$langs->trans("None").'</td>';
print '</tr>';
}
$db->free($result);
} else {
2011-05-28 14:19:49 +02:00
dol_print_error($db);
}
print '<tr class="liste_total">';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
print '</tr>';
2009-02-11 22:15:47 +01:00
/*
2009-02-11 22:15:47 +01:00
* Charges sociales non deductibles
*/
print '<tr><td colspan="4">'.$langs->trans("SocialContributions").' ('.$langs->trans("Type").' 0)</td></tr>';
2009-02-11 22:15:47 +01:00
if ($modecompta == 'CREANCES-DETTES')
{
$sql = "SELECT c.id, c.libelle as nom, sum(cs.amount) as amount";
2011-05-28 14:19:49 +02:00
$sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
2012-02-01 11:32:55 +01:00
$sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
$sql.= " WHERE cs.fk_type = c.id";
2011-05-28 14:19:49 +02:00
$sql.= " AND c.deductible = 0";
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";
2009-02-11 22:15:47 +01:00
}
else
{
$sql = "SELECT c.id, c.libelle as nom, sum(p.amount) as amount";
2011-05-28 14:19:49 +02:00
$sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
2012-02-01 11:32:55 +01:00
$sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
2011-05-28 14:19:49 +02:00
$sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
2012-02-01 11:32:55 +01:00
$sql.= " WHERE p.fk_charge = cs.rowid";
$sql.= " AND cs.fk_type = c.id";
2011-05-28 14:19:49 +02:00
$sql.= " AND c.deductible = 0";
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
2009-02-11 22:15:47 +01:00
}
2012-02-01 11:32:55 +01:00
$sql.= " AND cs.entity = ".$conf->entity;
$sql.= " GROUP BY c.libelle, c.id";
$sql.= " ORDER BY c.libelle, c.id";
2010-02-07 00:10:54 +01:00
dol_syslog("get social contributions deductible=0 sql=".$sql);
2009-02-11 22:15:47 +01:00
$result=$db->query($sql);
$subtotal_ht = 0;
$subtotal_ttc = 0;
if ($result) {
2011-05-28 14:19:49 +02:00
$num = $db->num_rows($result);
$var=true;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
2012-08-03 20:09:37 +02:00
$total_ht -= $obj->amount;
$total_ttc -= $obj->amount;
$subtotal_ht += $obj->amount;
$subtotal_ttc += $obj->amount;
2011-05-28 14:19:49 +02:00
$var = !$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print '<td>'.$obj->nom.'</td>';
if ($modecompta == 'CREANCES-DETTES') print '<td align="right">'.price(-$obj->amount).'</td>';
print '<td align="right">'.price(-$obj->amount).'</td>';
print '</tr>';
$i++;
}
}
else {
$var = !$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print '<td colspan="3">'.$langs->trans("None").'</td>';
print '</tr>';
}
2009-02-11 22:15:47 +01:00
} else {
2011-05-28 14:19:49 +02:00
dol_print_error($db);
2009-02-11 22:15:47 +01:00
}
print '<tr class="liste_total">';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
2009-02-11 22:15:47 +01:00
print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
print '</tr>';
/*
* Charges sociales deductibles
*/
print '<tr><td colspan="4">'.$langs->trans("SocialContributions").' ('.$langs->trans("Type").' 1)</td></tr>';
2009-02-11 22:15:47 +01:00
if ($modecompta == 'CREANCES-DETTES')
{
$sql = "SELECT c.id, c.libelle as nom, sum(cs.amount) as amount";
2011-05-28 14:19:49 +02:00
$sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
2012-02-01 11:32:55 +01:00
$sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
$sql.= " WHERE cs.fk_type = c.id";
2011-05-28 14:19:49 +02:00
$sql.= " AND c.deductible = 1";
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";
2012-02-01 11:32:55 +01:00
$sql.= " AND cs.entity = ".$conf->entity;
$sql.= " GROUP BY c.libelle, c.id";
$sql.= " ORDER BY c.libelle, c.id";
2009-02-11 22:15:47 +01:00
}
else
{
$sql = "SELECT c.id, c.libelle as nom, sum(p.amount) as amount";
2012-02-20 23:51:00 +01:00
$sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
2012-02-01 11:32:55 +01:00
$sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
2011-05-28 14:19:49 +02:00
$sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
2012-02-20 23:51:00 +01:00
$sql.= " WHERE p.fk_charge = cs.rowid";
2012-02-01 11:32:55 +01:00
$sql.= " AND cs.fk_type = c.id";
2011-05-28 14:19:49 +02:00
$sql.= " AND c.deductible = 1";
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
2012-02-01 11:32:55 +01:00
$sql.= " AND cs.entity = ".$conf->entity;
$sql.= " GROUP BY c.libelle, c.id";
$sql.= " ORDER BY c.libelle, c.id";
2009-02-11 22:15:47 +01:00
}
2010-02-07 00:10:54 +01:00
dol_syslog("get social contributions deductible=1 sql=".$sql);
2009-02-11 22:15:47 +01:00
$result=$db->query($sql);
$subtotal_ht = 0;
$subtotal_ttc = 0;
if ($result) {
2011-05-28 14:19:49 +02:00
$num = $db->num_rows($result);
$var=true;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
2012-08-03 20:09:37 +02:00
$total_ht -= $obj->amount;
$total_ttc -= $obj->amount;
$subtotal_ht += $obj->amount;
$subtotal_ttc += $obj->amount;
2011-05-28 14:19:49 +02:00
$var = !$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print '<td>'.$obj->nom.'</td>';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print '<td align="right">'.price(-$obj->amount).'</td>';
2011-05-28 14:19:49 +02:00
print '<td align="right">'.price(-$obj->amount).'</td>';
print '</tr>';
$i++;
}
}
else {
$var = !$var;
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print '<td colspan="3">'.$langs->trans("None").'</td>';
print '</tr>';
}
2009-02-11 22:15:47 +01:00
} else {
2011-05-28 14:19:49 +02:00
dol_print_error($db);
2009-02-11 22:15:47 +01:00
}
print '<tr class="liste_total">';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
2009-02-11 22:15:47 +01:00
print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
print '</tr>';
if ($mysoc->tva_assuj == 'franchise') // Non assujeti
{
2011-05-28 14:19:49 +02:00
// Total
print '<tr>';
print '<td colspan="4">&nbsp;</td>';
print '</tr>';
print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print '<td class="border" align="right">'.price($total_ht).'</td>';
2011-05-28 14:19:49 +02:00
print '<td align="right">'.price($total_ttc).'</td>';
print '</tr>';
print '<tr>';
print '<td colspan="4">&nbsp;</td>';
print '</tr>';
2009-02-11 22:15:47 +01:00
}
/*
* VAT
*/
print '<tr><td colspan="4">'.$langs->trans("VAT").'</td></tr>';
$subtotal_ht = 0;
$subtotal_ttc = 0;
2005-08-16 01:39:04 +02:00
if ($modecompta == 'CREANCES-DETTES')
{
2011-05-28 14:19:49 +02:00
// TVA a payer
$amount=0;
$sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.tva) as amount";
$sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
$sql.= " WHERE f.fk_statut IN (1,2)";
2012-08-03 20:09:37 +02:00
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 (! empty($date_start) && ! empty($date_end))
$sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
2011-05-28 14:19:49 +02:00
$sql.= " AND f.entity = ".$conf->entity;
$sql.= " GROUP BY dm";
$sql.= " ORDER BY dm";
2011-05-28 14:19:49 +02:00
dol_syslog("get vat to pay sql=".$sql);
$result=$db->query($sql);
if ($result)
{
2011-05-28 14:19:49 +02:00
$num = $db->num_rows($result);
$var=false;
$i = 0;
if ($num)
{
while ($i < $num)
{
2011-05-28 14:19:49 +02:00
$obj = $db->fetch_object($result);
2012-08-03 20:09:37 +02:00
$amount -= $obj->amount;
//$total_ht -= $obj->amount;
$total_ttc -= $obj->amount;
//$subtotal_ht -= $obj->amount;
$subtotal_ttc -= $obj->amount;
2011-05-28 14:19:49 +02:00
$i++;
}
}
} else {
dol_print_error($db);
}
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print "<td>".$langs->trans("VATToPay")."</td>\n";
print "<td align=\"right\">&nbsp;</td>\n";
2011-05-28 14:19:49 +02:00
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
// TVA a recuperer
$amount=0;
$sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_tva) as amount";
$sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
$sql.= " WHERE f.fk_statut IN (1,2)";
2012-08-03 20:09:37 +02:00
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 (! empty($date_start) && ! empty($date_end))
$sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
2011-05-28 14:19:49 +02:00
$sql.= " AND f.entity = ".$conf->entity;
$sql.= " GROUP BY dm";
$sql.= " ORDER BY dm";
2011-05-28 14:19:49 +02:00
dol_syslog("get vat received back sql=".$sql);
$result=$db->query($sql);
if ($result)
{
2011-05-28 14:19:49 +02:00
$num = $db->num_rows($result);
$var=true;
$i = 0;
if ($num)
{
while ($i < $num)
{
2011-05-28 14:19:49 +02:00
$obj = $db->fetch_object($result);
2012-08-03 20:09:37 +02:00
$amount += $obj->amount;
//$total_ht += $obj->amount;
$total_ttc += $obj->amount;
//$subtotal_ht += $obj->amount;
$subtotal_ttc += $obj->amount;
2011-05-28 14:19:49 +02:00
$i++;
}
}
} else {
dol_print_error($db);
}
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print "<td>".$langs->trans("VATToCollect")."</td>\n";
print "<td align=\"right\">&nbsp;</td>\n";
2011-05-28 14:19:49 +02:00
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
}
2005-08-16 01:39:04 +02:00
else
{
// VAT really already paid
2011-05-28 14:19:49 +02:00
$amount=0;
$sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
$sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
$sql.= " WHERE amount > 0";
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
2011-05-28 14:19:49 +02:00
$sql.= " AND t.entity = ".$conf->entity;
$sql.= " GROUP BY dm";
2012-02-20 23:51:00 +01:00
$sql.= " ORDER BY dm";
2011-05-28 14:19:49 +02:00
dol_syslog("get vat really paid sql=".$sql);
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=false;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
2012-08-03 20:09:37 +02:00
$amount -= $obj->amount;
$total_ht -= $obj->amount;
$total_ttc -= $obj->amount;
$subtotal_ht -= $obj->amount;
$subtotal_ttc -= $obj->amount;
2011-05-28 14:19:49 +02:00
$i++;
}
}
$db->free($result);
} else {
dol_print_error($db);
}
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print "<td>".$langs->trans("VATPaid")."</td>\n";
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print "<td align=\"right\">".price($amount)."</td>\n";
2011-05-28 14:19:49 +02:00
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
// VAT really received
2011-05-28 14:19:49 +02:00
$amount=0;
$sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
$sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
$sql.= " WHERE amount < 0";
2012-08-03 20:09:37 +02:00
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
2011-05-28 14:19:49 +02:00
$sql.= " AND t.entity = ".$conf->entity;
$sql.= " GROUP BY dm";
2012-02-20 23:51:00 +01:00
$sql.= " ORDER BY dm";
2011-05-28 14:19:49 +02:00
dol_syslog("get vat really received back sql=".$sql);
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=true;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
2012-08-03 20:09:37 +02:00
$amount += $obj->amount;
$total_ht += $obj->amount;
$total_ttc += $obj->amount;
$subtotal_ht += $obj->amount;
$subtotal_ttc += $obj->amount;
2011-05-28 14:19:49 +02:00
$i++;
}
}
$db->free($result);
}
else
{
2011-05-28 14:19:49 +02:00
dol_print_error($db);
}
2013-06-20 09:18:12 +02:00
print "<tr ".$bc[$var]."><td>&nbsp;</td>";
2011-05-28 14:19:49 +02:00
print "<td>".$langs->trans("VATCollected")."</td>\n";
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print "<td align=\"right\">".price($amount)."</td>\n";
2011-05-28 14:19:49 +02:00
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
}
2009-02-11 22:15:47 +01:00
if ($mysoc->tva_assuj != 'franchise') // Assujeti
{
2011-05-28 14:19:49 +02:00
print '<tr class="liste_total">';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print '<td colspan="3" align="right">&nbsp;</td>';
print '<td colspan="3" align="right">'.price(price2num($subtotal_ttc,'MT')).'</td>';
2011-05-28 14:19:49 +02:00
print '</tr>';
}
2009-02-11 22:15:47 +01:00
if ($mysoc->tva_assuj != 'franchise') // Assujeti
{
2011-05-28 14:19:49 +02:00
// Total
print '<tr>';
print '<td colspan="4">&nbsp;</td>';
print '</tr>';
print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>';
2012-08-03 20:09:37 +02:00
if ($modecompta == 'CREANCES-DETTES')
print '<td class="liste_total" align="right">'.price(price2num($total_ht,'MT')).'</td>';
print '<td class="liste_total" align="right">'.price(price2num($total_ttc,'MT')).'</td>';
2011-05-28 14:19:49 +02:00
print '</tr>';
2009-02-11 22:15:47 +01:00
}
print "</table>";
print '<br>';
llxFooter();
$db->close();
?>