dolibarr  9.0.0
recap-compta.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2017 Pierre-Henry Favre <support@atm-consulting.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
30 
31 // Load translation files required by the page
32 $langs->load("companies");
33 if (! empty($conf->facture->enabled)) $langs->load("bills");
34 
35 $id = GETPOST('id')?GETPOST('id','int'):GETPOST('socid','int');
36 
37 // Security check
38 if ($user->societe_id) $id=$user->societe_id;
39 $result = restrictedArea($user, 'societe', $id, '&societe');
40 
41 $object = new Societe($db);
42 if ($id > 0) $object->fetch($id);
43 
44 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
45 $hookmanager->initHooks(array('recapcomptacard','globalcard'));
46 
47 // Load variable for pagination
48 $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
49 $sortfield = GETPOST('sortfield','alpha');
50 $sortorder = GETPOST('sortorder','alpha');
51 $page = GETPOST('page','int');
52 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
53 $offset = $limit * $page;
54 $pageprev = $page - 1;
55 $pagenext = $page + 1;
56 if (! $sortfield) $sortfield="f.datef,f.rowid"; // Set here default search field
57 if (! $sortorder) $sortorder="DESC";
58 
59 
60 $arrayfields=array(
61  'f.datef'=>array('label'=>"Date", 'checked'=>1),
62  //...
63 );
64 
65 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
66 $hookmanager->initHooks(array('supplierbalencelist','globalcard'));
67 
68 /*
69  * Actions
70  */
71 $parameters = array('socid' => $id);
72 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
73 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
74 
75 // None
76 
77 
78 /*
79  * View
80  */
81 
82 $form = new Form($db);
83 $userstatic=new User($db);
84 
85 $title=$langs->trans("ThirdParty").' - '.$langs->trans("Summary");
86 if (! empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/',$conf->global->MAIN_HTML_TITLE) && $object->name) $title=$object->name.' - '.$langs->trans("Symmary");
87 $help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
88 
89 llxHeader('',$title,$help_url);
90 
91 if ($id > 0)
92 {
93  $param='';
94  if ($id > 0) $param.='&socid='.$id;
95 
96  $head = societe_prepare_head($object);
97 
98  dol_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
99  dol_banner_tab($object, 'socid', '', ($user->societe_id?0:1), 'rowid', 'nom', '', '', 0, '', '', 1);
100  dol_fiche_end();
101 
102  if (! empty($conf->facture->enabled) && $user->rights->facture->lire)
103  {
104  // Invoice list
105  print load_fiche_titre($langs->trans("CustomerPreview"));
106 
107  print '<table class="noborder tagtable liste" width="100%">';
108  print '<tr class="liste_titre">';
109  if (! empty($arrayfields['f.datef']['checked'])) print_liste_field_titre($arrayfields['f.datef']['label'],$_SERVER["PHP_SELF"],"f.datef","",$param,'align="center" class="nowrap"',$sortfield,$sortorder);
110  print '<td>'.$langs->trans("Element").'</td>';
111  print '<td>'.$langs->trans("Status").'</td>';
112  print '<td align="right">'.$langs->trans("Debit").'</td>';
113  print '<td align="right">'.$langs->trans("Credit").'</td>';
114  print '<td align="right">'.$langs->trans("Balance").'</td>';
115  print '<td align="right">'.$langs->trans("Author").'</td>';
116  print '</tr>';
117 
118  $TData = array();
119 
120  $sql = "SELECT s.nom, s.rowid as socid, f.facnumber, f.amount, f.datef as df,";
121  $sql.= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
122  $sql.= " u.login, u.rowid as userid";
123  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f,".MAIN_DB_PREFIX."user as u";
124  $sql.= " WHERE f.fk_soc = s.rowid AND s.rowid = ".$object->id;
125  $sql.= " AND f.entity = ".$conf->entity;
126  $sql.= " AND f.fk_user_valid = u.rowid";
127  $sql.= $db->order($sortfield, $sortorder);
128 
129  $resql=$db->query($sql);
130  if ($resql)
131  {
132  $num = $db->num_rows($resql);
133 
134  // Boucle sur chaque facture
135  for ($i = 0 ; $i < $num ; $i++)
136  {
137  $objf = $db->fetch_object($resql);
138 
139  $fac = new Facture($db);
140  $ret=$fac->fetch($objf->facid);
141  if ($ret < 0)
142  {
143  print $fac->error."<br>";
144  continue;
145  }
146  $totalpaye = $fac->getSommePaiement();
147 
148  $userstatic->id=$objf->userid;
149  $userstatic->login=$objf->login;
150 
151  $values = array(
152  'fk_facture' => $objf->facid,
153  'date' => $fac->date,
154  'datefieldforsort' => $fac->date.'-'.$fac->ref,
155  'link' => $fac->getNomUrl(1),
156  'status' => $fac->getLibStatut(2,$totalpaye),
157  'amount' => $fac->total_ttc,
158  'author' => $userstatic->getLoginUrl(1)
159  );
160 
161  $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic);
162  $reshook = $hookmanager->executeHooks('facdao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
163  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
164 
165  $TData[] = $values;
166 
167  // Paiements
168  $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
169  $sql.= " p.fk_user_creat, u.login, u.rowid as userid";
170  $sql.= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf,";
171  $sql.= " ".MAIN_DB_PREFIX."paiement as p";
172  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_creat = u.rowid";
173  $sql.= " WHERE pf.fk_paiement = p.rowid";
174  $sql.= " AND p.entity = ".$conf->entity;
175  $sql.= " AND pf.fk_facture = ".$fac->id;
176  $sql.= " ORDER BY p.datep ASC, p.rowid ASC";
177 
178  $resqlp = $db->query($sql);
179  if ($resqlp)
180  {
181  $nump = $db->num_rows($resqlp);
182  $j = 0;
183 
184  while ($j < $nump)
185  {
186  $objp = $db->fetch_object($resqlp);
187 
188  $paymentstatic = new Paiement($db);
189  $paymentstatic->id = $objp->rowid;
190 
191  $userstatic->id=$objp->userid;
192  $userstatic->login=$objp->login;
193 
194  $values = array(
195  'fk_paiement' => $objp->rowid,
196  'date' => $db->jdate($objp->dp),
197  'datefieldforsort' => $db->jdate($objp->dp).'-'.$fac->ref,
198  'link' => $langs->trans("Payment") .' '. $paymentstatic->getNomUrl(1),
199  'status' => '',
200  'amount' => -$objp->amount,
201  'author' => $userstatic->getLoginUrl(1)
202  );
203 
204  $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic, 'paymentstatic' => $paymentstatic);
205  $reshook = $hookmanager->executeHooks('paydao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
206  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
207 
208  $TData[] = $values;
209 
210  $j++;
211  }
212 
213  $db->free($resqlp);
214  }
215  else
216  {
217  dol_print_error($db);
218  }
219  }
220  }
221  else
222  {
223  dol_print_error($db);
224  }
225 
226  if(empty($TData)) {
227  print '<tr class="oddeven"><td colspan="7">'.$langs->trans("NoInvoice").'</td></tr>';
228  } else {
229 
230  // Sort array by date ASC to calucalte balance
231  $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
232 
233  // Balance calculation
234  $balance = 0;
235  foreach($TData as &$data1) {
236  $balance += $data1['amount'];
237  $data1['balance'] += $balance;
238  }
239 
240  // Resorte array to have elements on the required $sortorder
241  $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
242 
243  $totalDebit = 0;
244  $totalCredit = 0;
245 
246  // Display array
247  foreach($TData as $data) {
248 
249  $html_class = '';
250  if (!empty($data['fk_facture'])) $html_class = 'facid-'.$data['fk_facture'];
251  elseif (!empty($data['fk_paiement'])) $html_class = 'payid-'.$data['fk_paiement'];
252 
253  print '<tr class="oddeven '.$html_class.'">';
254 
255  print "<td align=\"center\">";
256  if (!empty($data['fk_facture'])) print dol_print_date($data['date'],'day');
257  elseif (!empty($data['fk_paiement'])) print dol_print_date($data['date'],'dayhour');
258  print "</td>\n";
259 
260  print '<td>'.$data['link']."</td>\n";
261 
262  print '<td aling="left">'.$data['status'].'</td>';
263 
264  print '<td align="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
265 
266  $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
267 
268  print '<td align="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
269  $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
270 
271  // Balance
272  print '<td align="right">'.price($data['balance'])."</td>\n";
273 
274  // Author
275  print '<td class="nowrap" align="right">';
276  print $data['author'];
277  print '</td>';
278 
279  print "</tr>\n";
280  }
281 
282  print '<tr class="liste_total">';
283  print '<td colspan="3">&nbsp;</td>';
284  print '<td align="right">'.price($totalDebit).'</td>';
285  print '<td align="right">'.price($totalCredit).'</td>';
286  print '<td align="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
287  print '<td></td>';
288  print "</tr>\n";
289  }
290 
291  print "</table>";
292  }
293 }
294 else
295 {
296  dol_print_error($db);
297 }
298 
299 llxFooter();
300 
301 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by second index function, which produces ascending (default) or descending output...
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.
Class to manage Dolibarr users.
Definition: user.class.php:41
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 generation of HTML components Only common components must be here.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_fiche_end($notab=0)
Show tab footer of a card.
Class to manage payments of customer invoices.
llxHeader()
Empty header.
Definition: wrapper.php:44
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
Definition: company.lib.php:42
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="")
Show title line of an array.
Class to manage invoices.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.