2007-09-12 00:36:57 +02:00
< ? php
2012-12-30 15:13:49 +01:00
/* Copytight ( C ) 2005 - 2009 Regis Houssin < regis . houssin @ capnetworks . com >
2009-08-07 20:39:01 +02:00
* Copyright ( C ) 2008 - 2009 Laurent Destailleur ( Eldy ) < eldy @ users . sourceforge . net >
2008-06-20 20:30:04 +02:00
* Copyright ( C ) 2008 Raphael Bertrand ( Resultic ) < raphael . bertrand @ resultic . fr >
2007-09-12 00:36:57 +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
2007-09-12 00:36:57 +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
2011-08-01 00:21:57 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2007-09-12 00:36:57 +02:00
*/
/**
2009-08-07 20:39:01 +02:00
* \file htdocs / compta / bank / treso . php
* \ingroup banque
2010-03-23 10:20:38 +01:00
* \brief Page de detail du budget de tresorerie
2008-07-31 18:07:40 +02:00
*/
2007-09-12 00:36:57 +02:00
2013-04-15 13:11:29 +02:00
require '../../main.inc.php' ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/compta/sociales/class/chargesociales.class.php' ;
2013-06-05 16:24:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php' ;
$langs -> load ( " banks " );
$langs -> load ( " categories " );
2007-12-08 00:07:43 +01:00
$langs -> load ( " bills " );
2007-09-12 00:36:57 +02:00
2009-05-05 14:43:51 +02:00
// Security check
if ( isset ( $_GET [ " account " ]) || isset ( $_GET [ " ref " ]))
{
$id = isset ( $_GET [ " account " ]) ? $_GET [ " account " ] : ( isset ( $_GET [ " ref " ]) ? $_GET [ " ref " ] : '' );
}
$fieldid = isset ( $_GET [ " ref " ]) ? 'ref' : 'rowid' ;
if ( $user -> societe_id ) $socid = $user -> societe_id ;
2014-03-21 16:18:17 +01:00
$result = restrictedArea ( $user , 'banque' , $id , 'bank_account&bank_account' , '' , '' , $fieldid );
2009-05-05 14:43:51 +02:00
2007-09-12 00:36:57 +02:00
$vline = isset ( $_GET [ " vline " ]) ? $_GET [ " vline " ] : $_POST [ " vline " ];
$page = isset ( $_GET [ " page " ]) ? $_GET [ " page " ] : 0 ;
$mesg = '' ;
/*
2008-07-31 18:07:40 +02:00
* View
*/
2007-09-12 00:36:57 +02:00
llxHeader ();
2007-11-05 23:37:41 +01:00
2007-09-12 00:36:57 +02:00
$societestatic = new Societe ( $db );
$facturestatic = new Facture ( $db );
$facturefournstatic = new FactureFournisseur ( $db );
2008-07-31 18:07:40 +02:00
$socialcontribstatic = new ChargeSociales ( $db );
2007-09-12 00:36:57 +02:00
2011-11-08 10:18:45 +01:00
$form = new Form ( $db );
2007-09-12 00:36:57 +02:00
2007-11-05 23:37:41 +01:00
if ( $_REQUEST [ " account " ] || $_REQUEST [ " ref " ])
2007-09-12 00:36:57 +02:00
{
if ( $vline )
{
$viewline = $vline ;
}
else
{
$viewline = 20 ;
}
2007-11-05 23:37:41 +01:00
2007-09-12 00:36:57 +02:00
$acct = new Account ( $db );
2008-07-31 18:07:40 +02:00
if ( $_GET [ " account " ])
2007-11-05 23:37:41 +01:00
{
$result = $acct -> fetch ( $_GET [ " account " ]);
}
2008-07-31 18:07:40 +02:00
if ( $_GET [ " ref " ])
2007-11-05 23:37:41 +01:00
{
$result = $acct -> fetch ( 0 , $_GET [ " ref " ]);
$_GET [ " account " ] = $acct -> id ;
}
2007-09-12 00:36:57 +02:00
/*
2008-07-31 18:07:40 +02:00
*
*
*/
2007-09-12 00:36:57 +02:00
// Onglets
$head = bank_prepare_head ( $acct );
2009-08-07 20:39:01 +02:00
dol_fiche_head ( $head , 'cash' , $langs -> trans ( " FinancialAccount " ), 0 , 'account' );
2008-07-31 18:07:40 +02:00
2007-11-05 23:37:41 +01:00
print '<table class="border" width="100%">' ;
2012-07-28 21:49:47 +02:00
$linkback = '<a href="' . DOL_URL_ROOT . '/compta/bank/index.php">' . $langs -> trans ( " BackToList " ) . '</a>' ;
2007-11-05 23:37:41 +01:00
// Ref
print '<tr><td valign="top" width="25%">' . $langs -> trans ( " Ref " ) . '</td>' ;
print '<td colspan="3">' ;
2012-07-28 21:49:47 +02:00
print $form -> showrefnav ( $acct , 'ref' , $linkback , 1 , 'ref' );
2007-11-05 23:37:41 +01:00
print '</td></tr>' ;
// Label
print '<tr><td valign="top">' . $langs -> trans ( " Label " ) . '</td>' ;
print '<td colspan="3">' . $acct -> label . '</td></tr>' ;
2007-09-12 00:36:57 +02:00
2007-11-05 23:37:41 +01:00
print '</table>' ;
print '<br>' ;
2007-09-12 00:36:57 +02:00
2008-07-31 18:07:40 +02:00
if ( $mesg ) print '<div class="error">' . $mesg . '</div>' ;
2007-09-12 00:36:57 +02:00
2008-07-31 18:07:40 +02:00
$solde = $acct -> solde ( 0 );
2007-09-12 00:36:57 +02:00
2008-07-31 18:07:40 +02:00
/*
2010-03-23 10:20:38 +01:00
* Affiche tableau des echeances a venir
2008-07-31 18:07:40 +02:00
*/
2007-09-12 00:36:57 +02:00
print '<table class="notopnoleftnoright" width="100% border="1">' ;
// Ligne de titre tableau des ecritures
print '<tr class="liste_titre">' ;
2008-07-31 19:48:14 +02:00
print '<td>' . $langs -> trans ( " DateEcheance " ) . '</td>' ;
print '<td>' . $langs -> trans ( " Description " ) . '</td>' ;
2007-09-12 00:36:57 +02:00
print '<td>' . $langs -> trans ( " ThirdParty " ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( " Debit " ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( " Credit " ) . '</td>' ;
print '<td align="right" width="80">' . $langs -> trans ( " BankBalance " ) . '</td>' ;
print '</tr>' ;
2008-07-31 18:07:40 +02:00
$var = true ;
// Solde actuel
$var =! $var ;
2010-08-29 21:53:37 +02:00
print '<tr class="liste_total">' ;
2008-07-31 18:07:40 +02:00
print '<td align="left" colspan="5">' . $langs -> trans ( " CurrentBalance " ) . '</td>' ;
2007-09-12 00:36:57 +02:00
print '<td align="right" nowrap>' . price ( $solde ) . '</td>' ;
print '</tr>' ;
2008-07-31 18:07:40 +02:00
$var =! $var ;
2010-08-29 21:53:37 +02:00
print '<tr class="liste_total">' ;
print '<td align="left" colspan="5">' . $langs -> trans ( " RemainderToPay " ) . '</td>' ;
2008-07-31 18:07:40 +02:00
print '<td align="right" nowrap> </td>' ;
print '</tr>' ;
2007-09-12 00:36:57 +02:00
2008-07-31 18:07:40 +02:00
// Remainder to pay in future
2007-09-12 00:36:57 +02:00
2008-07-31 18:07:40 +02:00
// Customer invoices
2010-05-08 22:47:12 +02:00
$sql = " SELECT 'invoice' as family, f.rowid as objid, f.facnumber as ref, f.total_ttc, f.type, f.date_lim_reglement as dlr, " ;
2007-09-12 00:36:57 +02:00
$sql .= " s.rowid as socid, s.nom, s.fournisseur " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON f.fk_soc = s.rowid " ;
2009-05-05 14:43:51 +02:00
$sql .= " WHERE f.entity = " . $conf -> entity ;
2009-08-19 19:16:47 +02:00
$sql .= " AND f.paye = 0 AND f.fk_statut = 1 " ; // Not paid
2007-09-12 00:36:57 +02:00
$sql .= " ORDER BY dlr ASC " ;
2008-07-31 18:07:40 +02:00
// Supplier invoices
2013-04-07 15:43:21 +02:00
$sql2 = " SELECT 'invoice_supplier' as family, ff.rowid as objid, ff.ref_supplier as ref, (-1*ff.total_ttc) as total_ttc, ff.type, ff.date_lim_reglement as dlr, " ;
2008-06-20 20:30:04 +02:00
$sql2 .= " s.rowid as socid, s.nom, s.fournisseur " ;
$sql2 .= " FROM " . MAIN_DB_PREFIX . " facture_fourn as ff " ;
$sql2 .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON ff.fk_soc = s.rowid " ;
2009-05-05 14:43:51 +02:00
$sql2 .= " WHERE ff.entity = " . $conf -> entity ;
2009-08-19 19:16:47 +02:00
$sql2 .= " AND ff.paye = 0 AND fk_statut = 1 " ; // Not paid
2008-06-20 20:30:04 +02:00
$sql2 .= " ORDER BY dlr ASC " ;
2007-09-12 00:36:57 +02:00
2008-07-31 18:07:40 +02:00
// Social contributions
2010-05-08 22:47:12 +02:00
$sql3 = " SELECT 'social_contribution' as family, cs.rowid as objid, cs.libelle as ref, (-1*cs.amount) as total_ttc, ccs.libelle as type, cs.date_ech as dlr " ;
2008-07-31 18:07:40 +02:00
$sql3 .= " FROM " . MAIN_DB_PREFIX . " chargesociales as cs " ;
$sql3 .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_chargesociales as ccs ON cs.fk_type = ccs.id " ;
2009-05-05 14:43:51 +02:00
$sql3 .= " WHERE cs.entity = " . $conf -> entity ;
2009-08-19 19:16:47 +02:00
$sql3 .= " AND cs.paye = 0 " ; // Not paid
2008-07-31 18:07:40 +02:00
$sql3 .= " ORDER BY dlr ASC " ;
$error = 0 ;
2009-05-05 17:27:10 +02:00
$tab_sqlobjOrder = array ();
2008-07-31 18:07:40 +02:00
$tab_sqlobj = array ();
2009-05-05 17:27:10 +02:00
2008-07-31 18:07:40 +02:00
// List customer invoices
2007-09-12 00:36:57 +02:00
$result = $db -> query ( $sql );
2008-07-31 18:07:40 +02:00
if ( $result )
2008-06-20 20:30:04 +02:00
{
2007-09-12 00:36:57 +02:00
$num = $db -> num_rows ( $result );
2008-06-20 20:30:04 +02:00
for ( $i = 0 ; $i < $num ; $i ++ )
2008-07-31 18:07:40 +02:00
{
2008-06-20 20:30:04 +02:00
$sqlobj = $db -> fetch_object ( $result );
$tab_sqlobj [] = $sqlobj ;
2010-05-08 22:47:12 +02:00
$tab_sqlobjOrder [] = $db -> jdate ( $sqlobj -> dlr );
2008-07-31 18:07:40 +02:00
}
$db -> free ( $result );
}
else $error ++ ;
2009-05-05 17:27:10 +02:00
2008-07-31 18:07:40 +02:00
// List supplier invoices
$result2 = $db -> query ( $sql2 );
if ( $result2 )
{
2008-06-20 20:30:04 +02:00
$num = $db -> num_rows ( $result2 );
for ( $i = 0 ; $i < $num ; $i ++ )
2008-07-31 18:07:40 +02:00
{
2008-06-20 20:30:04 +02:00
$sqlobj = $db -> fetch_object ( $result2 );
$tab_sqlobj [] = $sqlobj ;
2010-05-08 22:47:12 +02:00
$tab_sqlobjOrder [] = $db -> jdate ( $sqlobj -> dlr );
2008-07-31 18:07:40 +02:00
}
2008-06-20 20:30:04 +02:00
$db -> free ( $result2 );
2008-07-31 18:07:40 +02:00
}
else $error ++ ;
2009-05-05 17:27:10 +02:00
2008-07-31 18:07:40 +02:00
// List social contributions
$result3 = $db -> query ( $sql3 );
if ( $result3 )
{
$num = $db -> num_rows ( $result3 );
2008-06-20 20:30:04 +02:00
2008-07-31 18:07:40 +02:00
for ( $i = 0 ; $i < $num ; $i ++ )
{
$sqlobj = $db -> fetch_object ( $result3 );
$tab_sqlobj [] = $sqlobj ;
2010-05-08 22:47:12 +02:00
$tab_sqlobjOrder [] = $db -> jdate ( $sqlobj -> dlr );
2008-07-31 18:07:40 +02:00
}
$db -> free ( $result3 );
}
else $error ++ ;
2009-05-05 17:27:10 +02:00
2008-07-31 18:07:40 +02:00
// Sort array
if ( ! $error )
{
2012-03-18 19:23:01 +01:00
array_multisort ( $tab_sqlobjOrder , $tab_sqlobj );
2008-07-31 18:07:40 +02:00
2008-06-20 20:30:04 +02:00
//Apply distinct filter
2008-07-31 18:07:40 +02:00
foreach ( $tab_sqlobj as $key => $value ) {
$tab_sqlobj [ $key ] = " ' " . serialize ( $value ) . " ' " ;
}
$tab_sqlobj = array_unique ( $tab_sqlobj );
foreach ( $tab_sqlobj as $key => $value ) {
$tab_sqlobj [ $key ] = unserialize ( trim ( $value , " ' " ));
}
2011-09-20 16:36:57 +02:00
$num = count ( $tab_sqlobj );
2008-07-31 18:07:40 +02:00
2008-06-20 20:30:04 +02:00
//$num = $db->num_rows($result);
2007-09-12 00:36:57 +02:00
$i = 0 ;
while ( $i < $num )
{
2007-12-07 23:24:34 +01:00
$paiement = '' ;
2008-07-31 18:07:40 +02:00
$ref = '' ;
$refcomp = '' ;
2009-05-05 17:27:10 +02:00
2008-06-20 20:30:04 +02:00
//$obj = $db->fetch_object($result);
$obj = array_shift ( $tab_sqlobj );
2009-05-05 17:27:10 +02:00
2010-12-13 14:16:02 +01:00
if ( $obj -> family == 'invoice_supplier' )
2008-07-31 18:07:40 +02:00
{
2010-10-28 00:41:40 +02:00
// TODO This code is to avoid to count suppliers credit note (ff.type = 2)
2009-05-05 17:27:10 +02:00
// Ajouter gestion des avoirs fournisseurs, champ
2008-07-31 18:07:40 +02:00
if (( $obj -> total_ttc < 0 && $obj -> type != 2 )
|| ( $obj -> total_ttc > 0 && $obj -> type == 2 ))
2007-12-07 23:24:34 +01:00
{
2008-07-31 18:07:40 +02:00
$facturefournstatic -> ref = $obj -> ref ;
$facturefournstatic -> id = $obj -> objid ;
$facturefournstatic -> type = $obj -> type ;
$ref = $facturefournstatic -> getNomUrl ( 1 , '' );
$societestatic -> id = $obj -> socid ;
$societestatic -> nom = $obj -> nom ;
2008-07-31 19:48:14 +02:00
$refcomp = $societestatic -> getNomUrl ( 1 , '' , 24 );
2008-07-31 18:07:40 +02:00
$paiement = - 1 * $facturefournstatic -> getSommePaiement (); // Payment already done
2007-12-07 23:24:34 +01:00
}
2007-09-12 00:36:57 +02:00
}
2008-07-31 18:07:40 +02:00
if ( $obj -> family == 'invoice' )
2007-09-12 00:36:57 +02:00
{
2008-07-31 18:07:40 +02:00
$facturestatic -> ref = $obj -> ref ;
$facturestatic -> id = $obj -> objid ;
2007-09-12 00:36:57 +02:00
$facturestatic -> type = $obj -> type ;
2008-07-31 18:07:40 +02:00
$ref = $facturestatic -> getNomUrl ( 1 , '' );
$societestatic -> id = $obj -> socid ;
$societestatic -> nom = $obj -> nom ;
2008-07-31 19:48:14 +02:00
$refcomp = $societestatic -> getNomUrl ( 1 , '' , 24 );
2007-09-12 00:36:57 +02:00
2008-07-31 18:07:40 +02:00
$paiement = $facturestatic -> getSommePaiement (); // Payment already done
}
if ( $obj -> family == 'social_contribution' )
{
$socialcontribstatic -> ref = $obj -> ref ;
$socialcontribstatic -> id = $obj -> objid ;
$socialcontribstatic -> lib = $obj -> type ;
$ref = $socialcontribstatic -> getNomUrl ( 1 , 24 );
2009-05-05 17:27:10 +02:00
2008-07-31 18:30:43 +02:00
$paiement = - 1 * $socialcontribstatic -> getSommePaiement (); // Payment already done
2008-07-31 18:07:40 +02:00
}
2009-05-05 17:27:10 +02:00
2007-12-07 23:24:34 +01:00
$total_ttc = $obj -> total_ttc ;
if ( $paiement ) $total_ttc = $obj -> total_ttc - $paiement ;
$solde += $total_ttc ;
2007-09-12 00:36:57 +02:00
2010-08-29 21:53:37 +02:00
// We discard with a remain to pay to 0
if ( price2num ( $total_ttc ) != 0 )
{
$var =! $var ;
// Show line
print " <tr " . $bc [ $var ] . " > " ;
print '<td>' ;
if ( $obj -> dlr ) print dol_print_date ( $db -> jdate ( $obj -> dlr ), " day " );
else print $langs -> trans ( " NotDefined " );
print " </td> " ;
print " <td> " . $ref . " </td> " ;
print " <td> " . $refcomp . " </td> " ;
if ( $obj -> total_ttc < 0 ) { print " <td align= \" right \" > " . price ( $total_ttc ) . " </td><td> </td> " ; };
if ( $obj -> total_ttc >= 0 ) { print " <td> </td><td align= \" right \" > " . price ( $total_ttc ) . " </td> " ; };
print '<td align="right">' . price ( $solde ) . '</td>' ;
print " </tr> " ;
}
2007-09-12 00:36:57 +02:00
$i ++ ;
}
}
2007-12-07 23:24:34 +01:00
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $db );
2007-12-07 23:24:34 +01:00
}
2007-09-12 00:36:57 +02:00
2008-07-31 19:48:14 +02:00
// Solde actuel
$var =! $var ;
print '<tr class="liste_total">' ;
print '<td align="left" colspan="5">' . $langs -> trans ( " FutureBalance " ) . '</td>' ;
print '<td align="right" nowrap>' . price ( $solde ) . '</td>' ;
print '</tr>' ;
2009-05-05 17:27:10 +02:00
2007-09-12 00:36:57 +02:00
print " </table> " ;
}
else
{
print $langs -> trans ( " ErrorBankAccountNotFound " );
}
$db -> close ();
2011-08-27 16:24:16 +02:00
llxFooter ();
2007-09-12 00:36:57 +02:00
?>