dolibarr  16.0.5
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 <https://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 (isModEnabled('facture')) {
34  $langs->load("bills");
35 }
36 
37 $id = GETPOST('id') ?GETPOST('id', 'int') : GETPOST('socid', 'int');
38 
39 // Security check
40 if ($user->socid) {
41  $id = $user->socid;
42 }
43 $result = restrictedArea($user, 'societe', $id, '&societe');
44 
45 $object = new Societe($db);
46 if ($id > 0) {
47  $object->fetch($id);
48 }
49 
50 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
51 $hookmanager->initHooks(array('recapcomptacard', 'globalcard'));
52 
53 // Load variable for pagination
54 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
55 $sortfield = GETPOST('sortfield', 'aZ09comma');
56 $sortorder = GETPOST('sortorder', 'aZ09comma');
57 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
58 if (empty($page) || $page == -1) {
59  $page = 0;
60 } // If $page is not defined, or '' or -1
61 $offset = $limit * $page;
62 $pageprev = $page - 1;
63 $pagenext = $page + 1;
64 if (!$sortfield) {
65  $sortfield = "f.datef,f.rowid"; // Set here default search field
66 }
67 if (!$sortorder) {
68  $sortorder = "DESC";
69 }
70 
71 
72 $arrayfields = array(
73  'f.datef'=>array('label'=>"Date", 'checked'=>1),
74  //...
75 );
76 
77 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
78 $hookmanager->initHooks(array('supplierbalencelist', 'globalcard'));
79 
80 /*
81  * Actions
82  */
83 $parameters = array('socid' => $id);
84 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
85 if ($reshook < 0) {
86  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
87 }
88 
89 // None
90 
91 
92 /*
93  * View
94  */
95 
96 $form = new Form($db);
97 $userstatic = new User($db);
98 
99 $title = $langs->trans("ThirdParty").' - '.$langs->trans("Summary");
100 if (!empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/', $conf->global->MAIN_HTML_TITLE) && $object->name) {
101  $title = $object->name.' - '.$langs->trans("Summary");
102 }
103 $help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
104 
105 llxHeader('', $title, $help_url);
106 
107 if ($id > 0) {
108  $param = '';
109  if ($id > 0) {
110  $param .= '&socid='.$id;
111  }
112 
113  $head = societe_prepare_head($object);
114 
115  print dol_get_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
116  dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom', '', '', 0, '', '', 1);
117  print dol_get_fiche_end();
118 
119  if (isModEnabled('facture') && $user->rights->facture->lire) {
120  // Invoice list
121  print load_fiche_titre($langs->trans("CustomerPreview"));
122 
123  print '<table class="noborder tagtable liste centpercent">';
124  print '<tr class="liste_titre">';
125  if (!empty($arrayfields['f.datef']['checked'])) {
126  print_liste_field_titre($arrayfields['f.datef']['label'], $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
127  }
128  print '<td>'.$langs->trans("Element").'</td>';
129  print '<td>'.$langs->trans("Status").'</td>';
130  print '<td class="right">'.$langs->trans("Debit").'</td>';
131  print '<td class="right">'.$langs->trans("Credit").'</td>';
132  print '<td class="right">'.$langs->trans("Balance").'</td>';
133  print '<td class="right">'.$langs->trans("Author").'</td>';
134  print '</tr>';
135 
136  $TData = array();
137 
138  $sql = "SELECT s.nom, s.rowid as socid, f.ref, f.total_ttc, f.datef as df,";
139  $sql .= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
140  $sql .= " u.login, u.rowid as userid";
141  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f,".MAIN_DB_PREFIX."user as u";
142  $sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = ".((int) $object->id);
143  $sql .= " AND f.entity IN (".getEntity('invoice').")";
144  $sql .= " AND f.fk_user_valid = u.rowid";
145  $sql .= $db->order($sortfield, $sortorder);
146 
147  $resql = $db->query($sql);
148  if ($resql) {
149  $num = $db->num_rows($resql);
150 
151  // Boucle sur chaque facture
152  for ($i = 0; $i < $num; $i++) {
153  $objf = $db->fetch_object($resql);
154 
155  $fac = new Facture($db);
156  $ret = $fac->fetch($objf->facid);
157  if ($ret < 0) {
158  print $fac->error."<br>";
159  continue;
160  }
161  $totalpaid = $fac->getSommePaiement();
162 
163  $userstatic->id = $objf->userid;
164  $userstatic->login = $objf->login;
165 
166  $values = array(
167  'fk_facture' => $objf->facid,
168  'date' => $fac->date,
169  'datefieldforsort' => $fac->date.'-'.$fac->ref,
170  'link' => $fac->getNomUrl(1),
171  'status' => $fac->getLibStatut(2, $totalpaid),
172  'amount' => $fac->total_ttc,
173  'author' => $userstatic->getLoginUrl(1)
174  );
175 
176  $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic);
177  $reshook = $hookmanager->executeHooks('facdao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
178  if ($reshook < 0) {
179  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
180  }
181 
182  $TData[] = $values;
183 
184  // Paiements
185  $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
186  $sql .= " p.fk_user_creat, u.login, u.rowid as userid";
187  $sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf,";
188  $sql .= " ".MAIN_DB_PREFIX."paiement as p";
189  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_creat = u.rowid";
190  $sql .= " WHERE pf.fk_paiement = p.rowid";
191  $sql .= " AND p.entity = ".$conf->entity;
192  $sql .= " AND pf.fk_facture = ".((int) $fac->id);
193  $sql .= " ORDER BY p.datep ASC, p.rowid ASC";
194 
195  $resqlp = $db->query($sql);
196  if ($resqlp) {
197  $nump = $db->num_rows($resqlp);
198  $j = 0;
199 
200  while ($j < $nump) {
201  $objp = $db->fetch_object($resqlp);
202 
203  $paymentstatic = new Paiement($db);
204  $paymentstatic->id = $objp->rowid;
205 
206  $userstatic->id = $objp->userid;
207  $userstatic->login = $objp->login;
208 
209  $values = array(
210  'fk_paiement' => $objp->rowid,
211  'date' => $db->jdate($objp->dp),
212  'datefieldforsort' => $db->jdate($objp->dp).'-'.$fac->ref,
213  'link' => $langs->trans("Payment").' '.$paymentstatic->getNomUrl(1),
214  'status' => '',
215  'amount' => -$objp->amount,
216  'author' => $userstatic->getLoginUrl(1)
217  );
218 
219  $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic, 'paymentstatic' => $paymentstatic);
220  $reshook = $hookmanager->executeHooks('paydao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
221  if ($reshook < 0) {
222  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
223  }
224 
225  $TData[] = $values;
226 
227  $j++;
228  }
229 
230  $db->free($resqlp);
231  } else {
232  dol_print_error($db);
233  }
234  }
235  } else {
236  dol_print_error($db);
237  }
238 
239  if (empty($TData)) {
240  print '<tr class="oddeven"><td colspan="7">'.$langs->trans("NoInvoice").'</td></tr>';
241  } else {
242  // Sort array by date ASC to calucalte balance
243  $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
244 
245  // Balance calculation
246  $balance = 0;
247  foreach ($TData as &$data1) {
248  $balance += $data1['amount'];
249  $data1['balance'] += $balance;
250  }
251 
252  // Resorte array to have elements on the required $sortorder
253  $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
254 
255  $totalDebit = 0;
256  $totalCredit = 0;
257 
258  // Display array
259  foreach ($TData as $data) {
260  $html_class = '';
261  if (!empty($data['fk_facture'])) {
262  $html_class = 'facid-'.$data['fk_facture'];
263  } elseif (!empty($data['fk_paiement'])) {
264  $html_class = 'payid-'.$data['fk_paiement'];
265  }
266 
267  print '<tr class="oddeven '.$html_class.'">';
268 
269  $datedetail = dol_print_date($data['date'], 'dayhour');
270  if (!empty($data['fk_facture'])) {
271  $datedetail = dol_print_date($data['date'], 'day');
272  }
273  print '<td class="center" title="'.dol_escape_htmltag($datedetail).'">';
274  print dol_print_date($data['date'], 'day');
275  print "</td>\n";
276 
277  print '<td>'.$data['link']."</td>\n";
278 
279  print '<td class="left">'.$data['status'].'</td>';
280 
281  print '<td class="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
282 
283  $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
284 
285  print '<td class="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
286  $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
287 
288  // Balance
289  print '<td class="right"><span class="amount">'.price($data['balance'])."</span></td>\n";
290 
291  // Author
292  print '<td class="nowrap right">';
293  print $data['author'];
294  print '</td>';
295 
296  print "</tr>\n";
297  }
298 
299  print '<tr class="liste_total">';
300  print '<td colspan="3">&nbsp;</td>';
301  print '<td class="right">'.price($totalDebit).'</td>';
302  print '<td class="right">'.price($totalCredit).'</td>';
303  print '<td class="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
304  print '<td></td>';
305  print "</tr>\n";
306  }
307 
308  print "</table>";
309  }
310 } else {
311  dol_print_error($db);
312 }
313 
314 llxFooter();
315 
316 $db->close();
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
restrictedArea
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
dol_sort_array
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...
Definition: functions.lib.php:8385
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
Facture
Class to manage invoices.
Definition: facture.class.php:60
dol_banner_tab
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.
Definition: functions.lib.php:2046
$help_url
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:116
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
societe_prepare_head
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
Definition: company.lib.php:42
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Paiement
Class to manage payments of customer invoices.
Definition: paiement.class.php:41
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
print_liste_field_titre
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
Definition: functions.lib.php:5026
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
price
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.
Definition: functions.lib.php:5541
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59