dolibarr  7.0.0-beta
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 $langs->load("companies");
32 if (! empty($conf->facture->enabled)) $langs->load("bills");
33 
34 $id = GETPOST('id')?GETPOST('id','int'):GETPOST('socid','int');
35 
36 // Security check
37 if ($user->societe_id) $id=$user->societe_id;
38 $result = restrictedArea($user, 'societe', $id, '&societe');
39 
40 $object = new Societe($db);
41 if ($id > 0) $object->fetch($id);
42 
43 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
44 $hookmanager->initHooks(array('recapcomptacard','globalcard'));
45 
46 // Load variable for pagination
47 $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
48 $sortfield = GETPOST('sortfield','alpha');
49 $sortorder = GETPOST('sortorder','alpha');
50 $page = GETPOST('page','int');
51 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
52 $offset = $limit * $page;
53 $pageprev = $page - 1;
54 $pagenext = $page + 1;
55 if (! $sortfield) $sortfield="f.datef"; // Set here default search field
56 if (! $sortorder) $sortorder="DESC";
57 
58 
59 $arrayfields=array(
60  'f.datef'=>array('label'=>"Date", 'checked'=>1),
61  //...
62 );
63 
64 /*
65  * Actions
66  */
67 $parameters = array('socid' => $id);
68 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
69 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
70 
71 // None
72 
73 
74 /*
75  * View
76  */
77 
78 $form = new Form($db);
79 $userstatic=new User($db);
80 
81 $title=$langs->trans("ThirdParty").' - '.$langs->trans("Summary");
82 if (! empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/',$conf->global->MAIN_HTML_TITLE) && $object->name) $title=$object->name.' - '.$langs->trans("Symmary");
83 $help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
84 
85 llxHeader('',$title,$help_url);
86 
87 if ($id > 0)
88 {
89  $param='';
90  if ($id > 0) $param.='&socid='.$id;
91 
92  $head = societe_prepare_head($object);
93 
94  dol_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
95  dol_banner_tab($object, 'socid', '', ($user->societe_id?0:1), 'rowid', 'nom', '', '', 0, '', '', 1);
96  dol_fiche_end();
97 
98  if (! empty($conf->facture->enabled) && $user->rights->facture->lire)
99  {
100  // Invoice list
101  print load_fiche_titre($langs->trans("CustomerPreview"));
102 
103  print '<table class="noborder" width="100%">';
104  print '<tr class="liste_titre">';
105  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);
106  print '<td>'.$langs->trans("Element").'</td>';
107  print '<td>'.$langs->trans("Status").'</td>';
108  print '<td align="right">'.$langs->trans("Debit").'</td>';
109  print '<td align="right">'.$langs->trans("Credit").'</td>';
110  print '<td align="right">'.$langs->trans("Balance").'</td>';
111  print '<td align="right">'.$langs->trans("Author").'</td>';
112  print '</tr>';
113 
114  $TData = array();
115  $TDataSort = array();
116 
117  $sql = "SELECT s.nom, s.rowid as socid, f.facnumber, f.amount, f.datef as df,";
118  $sql.= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
119  $sql.= " u.login, u.rowid as userid";
120  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f,".MAIN_DB_PREFIX."user as u";
121  $sql.= " WHERE f.fk_soc = s.rowid AND s.rowid = ".$object->id;
122  $sql.= " AND f.entity = ".$conf->entity;
123  $sql.= " AND f.fk_user_valid = u.rowid";
124  $sql.= $db->order($sortfield, $sortorder);
125 
126  $resql=$db->query($sql);
127  if ($resql)
128  {
129  $var=true;
130  $num = $db->num_rows($resql);
131 
132  // Boucle sur chaque facture
133  for ($i = 0 ; $i < $num ; $i++)
134  {
135  $objf = $db->fetch_object($resql);
136 
137  $fac = new Facture($db);
138  $ret=$fac->fetch($objf->facid);
139  if ($ret < 0)
140  {
141  print $fac->error."<br>";
142  continue;
143  }
144  $totalpaye = $fac->getSommePaiement();
145 
146  $userstatic->id=$objf->userid;
147  $userstatic->login=$objf->login;
148 
149  $values = array(
150  'fk_facture' => $objf->facid,
151  'date' => $fac->date,
152  'link' => $fac->getNomUrl(1),
153  'status' => $fac->getLibStatut(2,$totalpaye),
154  'amount' => $fac->total_ttc,
155  'author' => $userstatic->getLoginUrl(1)
156  );
157 
158  $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic);
159  $reshook = $hookmanager->executeHooks('facdao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
160  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
161 
162  $TData[] = $values;
163  $TDataSort[] = $fac->date;
164 
165  // Paiements
166  $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
167  $sql.= " p.fk_user_creat, u.login, u.rowid as userid";
168  $sql.= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf,";
169  $sql.= " ".MAIN_DB_PREFIX."paiement as p";
170  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_creat = u.rowid";
171  $sql.= " WHERE pf.fk_paiement = p.rowid";
172  $sql.= " AND p.entity = ".$conf->entity;
173  $sql.= " AND pf.fk_facture = ".$fac->id;
174  $sql.= " ORDER BY p.datep ASC";
175 
176  $resqlp = $db->query($sql);
177  if ($resqlp)
178  {
179  $nump = $db->num_rows($resqlp);
180  $j = 0;
181 
182  while ($j < $nump)
183  {
184  $objp = $db->fetch_object($resqlp);
185 
186  $paymentstatic = new Paiement($db);
187  $paymentstatic->id = $objp->rowid;
188 
189  $userstatic->id=$objp->userid;
190  $userstatic->login=$objp->login;
191 
192  $values = array(
193  'fk_paiement' => $objp->rowid,
194  'date' => $db->jdate($objp->dp),
195  'link' => $langs->trans("Payment") .' '. $paymentstatic->getNomUrl(1),
196  'status' => '',
197  'amount' => -$objp->amount,
198  'author' => $userstatic->getLoginUrl(1)
199  );
200 
201  $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic, 'paymentstatic' => $paymentstatic);
202  $reshook = $hookmanager->executeHooks('paydao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
203  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
204 
205  $TData[] = $values;
206  $TDataSort[] = $db->jdate($objp->dp);
207 
208  $j++;
209  }
210 
211  $db->free($resqlp);
212  }
213  else
214  {
215  dol_print_error($db);
216  }
217  }
218  }
219  else
220  {
221  dol_print_error($db);
222  }
223 
224  if(empty($TData)) {
225  print '<tr class="oddeven"><td colspan="7">'.$langs->trans("NoInvoice").'</td></tr>';
226  } else {
227 
228  // Sort array by date
229  asort($TDataSort);
230  array_multisort($TData,$TDataSort);
231 
232  // Balance calculation
233  foreach($TData as &$data1) {
234  $balance += $data1['amount'];
235  $data1['balance'] += $balance;
236  }
237 
238  // Reverse array to have last elements on top
239  $TData = dol_sort_array($TData, 'date', $sortorder);
240 
241 
242  $totalDebit = 0;
243  $totalCredit = 0;
244 
245  // Display array
246  foreach($TData as $data) {
247 
248  $html_class = '';
249  if (!empty($data['fk_facture'])) $html_class = 'facid-'.$data['fk_facture'];
250  elseif (!empty($data['fk_paiement'])) $html_class = 'payid-'.$data['fk_paiement'];
251 
252  print '<tr class="oddeven '.$html_class.'">';
253 
254  print "<td align=\"center\">".dol_print_date($data['date'],'day')."</td>\n";
255  print '<td>'.$data['link']."</td>\n";
256 
257  print '<td aling="left">'.$data['status'].'</td>';
258  print '<td align="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
259  $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
260  print '<td align="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
261  $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
262  print '<td align="right">'.price($data['balance'])."</td>\n";
263 
264  // Author
265  print '<td class="nowrap" align="right">';
266  print $data['author'];
267  print '</td>';
268 
269  print "</tr>\n";
270  }
271 
272  print '<tr class="liste_total">';
273  print '<td colspan="3">&nbsp;</td>';
274  print '<td align="right">'.price($totalDebit).'</td>';
275  print '<td align="right">'.price($totalCredit).'</td>';
276  print '<td align="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
277  print '<td></td>';
278  print "</tr>\n";
279  }
280 
281  print "</table>";
282  }
283 }
284 else
285 {
286  dol_print_error($db);
287 }
288 
289 llxFooter();
290 
291 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
if(!GETPOST('transkey')&&!GETPOST('transphrase')) else
View.
Definition: notice.php:43
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_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.
Class to manage Dolibarr users.
Definition: user.class.php:39
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
Class to manage payments of customer invoices.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
Definition: company.lib.php:40
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).
print
Draft customers invoices.
Definition: index.php:91
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:1013
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 '...
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.