dolibarr  13.0.2
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 (!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->socid) $id = $user->socid;
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', 'aZ09comma');
50 $sortorder = GETPOST('sortorder', 'aZ09comma');
51 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : 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("Summary");
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  print dol_get_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
99  dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom', '', '', 0, '', '', 1);
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 centpercent">';
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 class="right">'.$langs->trans("Debit").'</td>';
113  print '<td class="right">'.$langs->trans("Credit").'</td>';
114  print '<td class="right">'.$langs->trans("Balance").'</td>';
115  print '<td class="right">'.$langs->trans("Author").'</td>';
116  print '</tr>';
117 
118  $TData = array();
119 
120  $sql = "SELECT s.nom, s.rowid as socid, f.ref, f.total_ttc, 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 IN (".getEntity('invoice').")";
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  } else {
215  dol_print_error($db);
216  }
217  }
218  } else {
219  dol_print_error($db);
220  }
221 
222  if (empty($TData)) {
223  print '<tr class="oddeven"><td colspan="7">'.$langs->trans("NoInvoice").'</td></tr>';
224  } else {
225  // Sort array by date ASC to calucalte balance
226  $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
227 
228  // Balance calculation
229  $balance = 0;
230  foreach ($TData as &$data1) {
231  $balance += $data1['amount'];
232  $data1['balance'] += $balance;
233  }
234 
235  // Resorte array to have elements on the required $sortorder
236  $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
237 
238  $totalDebit = 0;
239  $totalCredit = 0;
240 
241  // Display array
242  foreach ($TData as $data) {
243  $html_class = '';
244  if (!empty($data['fk_facture'])) $html_class = 'facid-'.$data['fk_facture'];
245  elseif (!empty($data['fk_paiement'])) $html_class = 'payid-'.$data['fk_paiement'];
246 
247  print '<tr class="oddeven '.$html_class.'">';
248 
249  print "<td class=\"center\">";
250  if (!empty($data['fk_facture'])) print dol_print_date($data['date'], 'day');
251  elseif (!empty($data['fk_paiement'])) print dol_print_date($data['date'], 'dayhour');
252  print "</td>\n";
253 
254  print '<td>'.$data['link']."</td>\n";
255 
256  print '<td class="left">'.$data['status'].'</td>';
257 
258  print '<td class="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
259 
260  $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
261 
262  print '<td class="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
263  $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
264 
265  // Balance
266  print '<td class="right">'.price($data['balance'])."</td>\n";
267 
268  // Author
269  print '<td class="nowrap right">';
270  print $data['author'];
271  print '</td>';
272 
273  print "</tr>\n";
274  }
275 
276  print '<tr class="liste_total">';
277  print '<td colspan="3">&nbsp;</td>';
278  print '<td class="right">'.price($totalDebit).'</td>';
279  print '<td class="right">'.price($totalCredit).'</td>';
280  print '<td class="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
281  print '<td></td>';
282  print "</tr>\n";
283  }
284 
285  print "</table>";
286  }
287 } else {
288  dol_print_error($db);
289 }
290 
291 llxFooter();
292 
293 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage Dolibarr users.
Definition: user.class.php:44
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...
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
Class to manage third parties objects (customers, suppliers, prospects...)
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Class to manage payments of customer invoices.
if(!GETPOST('transkey', 'alphanohtml')&&!GETPOST('transphrase', 'alphanohtml')) else
View.
Definition: notice.php:44
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.
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
Definition: company.lib.php:42
print $_SERVER["PHP_SELF"]
Edit parameters.
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_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
print $_SERVER["PHP_SELF"] n
Edit parameters.
Definition: categories.php:101
Class to manage invoices.
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.
llxFooter()
Empty footer.
Definition: wrapper.php:59
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.