dolibarr  7.0.0-beta
treso.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
3  * Copyright (C) 2008-2009 Laurent Destailleur (Eldy) <eldy@users.sourceforge.net>
4  * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
5  * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com
6  * Copyright (C) 2016 Frédéric France <frederic.france@free.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
28 require '../../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
35 
36 $langs->load("banks");
37 $langs->load("categories");
38 $langs->load("bills");
39 $langs->load("companies");
40 
41 // Security check
42 if (isset($_GET["account"]) || isset($_GET["ref"]))
43 {
44  $id = isset($_GET["account"])?$_GET["account"]:(isset($_GET["ref"])?$_GET["ref"]:'');
45 }
46 $fieldid = isset($_GET["ref"])?'ref':'rowid';
47 if ($user->societe_id) $socid=$user->societe_id;
48 $result=restrictedArea($user,'banque',$id,'bank_account&bank_account','','',$fieldid);
49 
50 
51 $vline=isset($_GET["vline"])?$_GET["vline"]:$_POST["vline"];
52 $page=isset($_GET["page"])?$_GET["page"]:0;
53 
54 /*
55  * View
56  */
57 
58 $title = $langs->trans("FinancialAccount").' - '.$langs->trans("PlannedTransactions");
59 $helpurl = "";
60 llxHeader('',$title,$helpurl);
61 
62 $societestatic = new Societe($db);
63 $facturestatic=new Facture($db);
64 $facturefournstatic=new FactureFournisseur($db);
65 $socialcontribstatic=new ChargeSociales($db);
66 
67 $form = new Form($db);
68 
69 if ($_REQUEST["account"] || $_REQUEST["ref"])
70 {
71  if ($vline)
72  {
73  $viewline = $vline;
74  }
75  else
76  {
77  $viewline = 20;
78  }
79 
80  $object = new Account($db);
81  if ($_GET["account"])
82  {
83  $result=$object->fetch($_GET["account"]);
84  }
85  if ($_GET["ref"])
86  {
87  $result=$object->fetch(0,$_GET["ref"]);
88  $_GET["account"]=$object->id;
89  }
90 
91 
92  // Onglets
93  $head=bank_prepare_head($object);
94  dol_fiche_head($head, 'cash', $langs->trans("FinancialAccount"), 0, 'account');
95 
96  $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
97 
98  dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
99 
100  dol_fiche_end();
101 
102  print '<br>';
103 
104  $solde = $object->solde(0);
105 
106  // Show next coming entries
107  print '<div class="div-table-responsive">';
108  print '<table class="noborder centpercent">';
109 
110  // Ligne de titre tableau des ecritures
111  print '<tr class="liste_titre">';
112  print '<td>'.$langs->trans("DateDue").'</td>';
113  print '<td>'.$langs->trans("Description").'</td>';
114  print '<td>'.$langs->trans("ThirdParty").'</td>';
115  print '<td align="right">'.$langs->trans("Debit").'</td>';
116  print '<td align="right">'.$langs->trans("Credit").'</td>';
117  print '<td align="right" width="80">'.$langs->trans("BankBalance").'</td>';
118  print '</tr>';
119 
120  $var=true;
121 
122  // Current balance
123 
124  print '<tr class="liste_total">';
125  print '<td align="left" colspan="5">'.$langs->trans("CurrentBalance").'</td>';
126  print '<td align="right" class="nowrap">'.price($solde).'</td>';
127  print '</tr>';
128 
129 
130  print '<tr class="liste_titre">';
131  print '<td align="left" colspan="5">'.$langs->trans("RemainderToPay").'</td>';
132  print '<td align="right" class="nowrap">&nbsp;</td>';
133  print '</tr>';
134 
135 
136  // Remainder to pay in future
137 
138  // Customer invoices
139  $sql = "SELECT 'invoice' as family, f.rowid as objid, f.facnumber as ref, f.total_ttc, f.type, f.date_lim_reglement as dlr,";
140  $sql.= " s.rowid as socid, s.nom as name, s.fournisseur";
141  $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
142  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
143  $sql.= " WHERE f.entity = ".$conf->entity;
144  $sql.= " AND f.paye = 0 AND f.fk_statut = 1"; // Not paid
145  $sql.= " AND (f.fk_account IN (0, ".$object->id.") OR f.fk_account IS NULL)"; // Id bank account of invoice
146  $sql.= " ORDER BY dlr ASC";
147 
148  // Supplier invoices
149  $sql2= " SELECT 'invoice_supplier' as family, ff.rowid as objid, ff.ref as ref, ff.ref_supplier as ref_supplier, (-1*ff.total_ttc) as total_ttc, ff.type, ff.date_lim_reglement as dlr,";
150  $sql2.= " s.rowid as socid, s.nom as name, s.fournisseur";
151  $sql2.= " FROM ".MAIN_DB_PREFIX."facture_fourn as ff";
152  $sql2.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON ff.fk_soc = s.rowid";
153  $sql2.= " WHERE ff.entity = ".$conf->entity;
154  $sql2.= " AND ff.paye = 0 AND fk_statut = 1"; // Not paid
155  $sql2.= " AND (ff.fk_account IN (0, ".$object->id.") OR ff.fk_account IS NULL)"; // Id bank account of supplier invoice
156  $sql2.= " ORDER BY dlr ASC";
157 
158  // Social contributions
159  $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";
160  $sql3.= ", cs.fk_account";
161  $sql3.= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
162  $sql3.= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as ccs ON cs.fk_type = ccs.id";
163  $sql3.= " WHERE cs.entity = ".$conf->entity;
164  $sql3.= " AND cs.paye = 0"; // Not paid
165  $sql3.= " AND (cs.fk_account IN (0, ".$object->id.") OR cs.fk_account IS NULL)"; // Id bank account of social contribution
166  $sql3.= " ORDER BY dlr ASC";
167 
168  $error=0;
169  $tab_sqlobjOrder=array();
170  $tab_sqlobj=array();
171 
172  // List customer invoices
173  $result = $db->query($sql);
174  if ($result)
175  {
176  $num = $db->num_rows($result);
177  for ($i = 0;$i < $num;$i++)
178  {
179  $sqlobj = $db->fetch_object($result);
180  $tab_sqlobj[] = $sqlobj;
181  $tab_sqlobjOrder[]= $db->jdate($sqlobj->dlr);
182  }
183  $db->free($result);
184  }
185  else $error++;
186 
187  // List supplier invoices
188  $result2=$db->query($sql2);
189  if ($result2)
190  {
191  $num = $db->num_rows($result2);
192  for ($i = 0;$i < $num;$i++)
193  {
194  $sqlobj = $db->fetch_object($result2);
195  $tab_sqlobj[] = $sqlobj;
196  $tab_sqlobjOrder[]= $db->jdate($sqlobj->dlr);
197  }
198  $db->free($result2);
199  }
200  else $error++;
201 
202  // List social contributions
203  $result3=$db->query($sql3);
204  if ($result3)
205  {
206  $num = $db->num_rows($result3);
207 
208  for ($i = 0;$i < $num;$i++)
209  {
210  $sqlobj = $db->fetch_object($result3);
211  $tab_sqlobj[] = $sqlobj;
212  $tab_sqlobjOrder[]= $db->jdate($sqlobj->dlr);
213  }
214  $db->free($result3);
215  }
216  else $error++;
217 
218 
219  // Sort array
220  if (! $error)
221  {
222  array_multisort($tab_sqlobjOrder,$tab_sqlobj);
223 
224  //Apply distinct filter
225  foreach ($tab_sqlobj as $key=>$value) {
226  $tab_sqlobj[$key] = "'" . serialize($value) . "'";
227  }
228  $tab_sqlobj = array_unique($tab_sqlobj);
229  foreach ($tab_sqlobj as $key=>$value) {
230  $tab_sqlobj[$key] = unserialize(trim($value, "'"));
231  }
232 
233  $num = count($tab_sqlobj);
234 
235  //$num = $db->num_rows($result);
236  $i = 0;
237  while ($i < $num)
238  {
239  $paiement = '';
240  $ref = '';
241  $refcomp = '';
242 
243  //$obj = $db->fetch_object($result);
244  $obj = array_shift($tab_sqlobj);
245 
246  if ($obj->family == 'invoice_supplier')
247  {
248  $showline=1;
249  // Uncomment this line to avoid to count suppliers credit note (ff.type = 2)
250  //$showline=(($obj->total_ttc < 0 && $obj->type != 2) || ($obj->total_ttc > 0 && $obj->type == 2))
251  if ($showline)
252  {
253  $ref=$obj->ref;
254  $facturefournstatic->ref=$ref;
255  $facturefournstatic->id=$obj->objid;
256  $facturefournstatic->type=$obj->type;
257  $ref = $facturefournstatic->getNomUrl(1,'');
258 
259  $societestatic->id = $obj->socid;
260  $societestatic->name = $obj->name;
261  $refcomp=$societestatic->getNomUrl(1,'',24);
262 
263  $paiement = -1*$facturefournstatic->getSommePaiement(); // Payment already done
264  }
265  }
266  if ($obj->family == 'invoice')
267  {
268  $facturestatic->ref=$obj->ref;
269  $facturestatic->id=$obj->objid;
270  $facturestatic->type=$obj->type;
271  $ref = $facturestatic->getNomUrl(1,'');
272 
273  $societestatic->id = $obj->socid;
274  $societestatic->name = $obj->name;
275  $refcomp=$societestatic->getNomUrl(1,'',24);
276 
277  $paiement = $facturestatic->getSommePaiement(); // Payment already done
278  $paiement+= $facturestatic->getSumDepositsUsed();
279  $paiement+= $facturestatic->getSumCreditNotesUsed();
280  }
281  if ($obj->family == 'social_contribution')
282  {
283  $socialcontribstatic->ref=$obj->ref;
284  $socialcontribstatic->id=$obj->objid;
285  $socialcontribstatic->lib=$obj->type;
286  $ref = $socialcontribstatic->getNomUrl(1,24);
287 
288  $paiement = -1*$socialcontribstatic->getSommePaiement(); // Payment already done
289  }
290 
291  $total_ttc = $obj->total_ttc;
292  if ($paiement) $total_ttc = $obj->total_ttc - $paiement;
293  $solde += $total_ttc;
294 
295  // We discard lines with a remainder to pay to 0
296  if (price2num($total_ttc) != 0)
297  {
298 
299 
300  // Show line
301  print '<tr class="oddeven">';
302  print '<td>';
303  if ($obj->dlr) print dol_print_date($db->jdate($obj->dlr),"day");
304  else print $langs->trans("NotDefined");
305  print "</td>";
306  print "<td>".$ref."</td>";
307  print "<td>".$refcomp."</td>";
308  if ($obj->total_ttc < 0) { print "<td align=\"right\">".price(abs($total_ttc))."</td><td>&nbsp;</td>"; };
309  if ($obj->total_ttc >= 0) { print "<td>&nbsp;</td><td align=\"right\">".price($total_ttc)."</td>"; };
310  print '<td align="right">'.price($solde).'</td>';
311  print "</tr>";
312  }
313 
314  $i++;
315  }
316  }
317  else
318  {
319  dol_print_error($db);
320  }
321 
322  // Solde actuel
323 
324  print '<tr class="liste_total">';
325  print '<td align="left" colspan="5">'.$langs->trans("FutureBalance").' ('.$object->currency_code.')</td>';
326  print '<td align="right" class="nowrap">'.price($solde, 0, $langs, 0, 0, -1, $object->currency_code).'</td>';
327  print '</tr>';
328 
329  print "</table>";
330  print "</div>";
331 }
332 else
333 {
334  print $langs->trans("ErrorBankAccountNotFound");
335 }
336 
337 llxFooter();
338 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
bank_prepare_head(Account $object)
Prepare array with list of tabs.
Definition: bank.lib.php:34
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to manage suppliers invoices.
if(empty($reshook)) $form
View.
Definition: perms.php:103
Class to manage bank accounts.
Class to manage generation of HTML components Only common components must be here.
Class to manage third parties objects (customers, suppliers, prospects...)
if($_POST["cancel"]==$langs->trans("Cancel")&&!$id) if($action== 'setdatev'&&$user->rights->tax->charges->creer) if($action== 'add'&&$_POST["cancel"]<> $langs->trans("Cancel")) if($action== 'delete') $title
Actions.
Definition: card.php:183
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:46
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print
Draft customers invoices.
Definition: index.php:91
Class to manage invoices.
Classe permettant la gestion des paiements des charges La tva collectee n'est calculee que sur les fa...
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $objcanvas=null)
Check permissions of a user to show a page and an object.