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