dolibarr 23.0.3
recap-fourn.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2019 Pierre Ardoin <mapiolca@me.com>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Load Dolibarr environment
29require '../main.inc.php';
30require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
31require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
32require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php';
33
42// Load translation files required by the page
43$langs->loadLangs(array('bills', 'companies'));
44
45// Security check
46$socid = GETPOSTINT("socid");
47if ($user->socid > 0) {
48 $action = '';
49 $socid = $user->socid;
50}
51
52// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
53$hookmanager->initHooks(array('supplierbalencelist', 'globalcard'));
54
55// Load variable for pagination
56$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
57$sortfield = GETPOST('sortfield', 'aZ09comma');
58$sortorder = GETPOST('sortorder', 'aZ09comma');
59$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
60if (empty($page) || $page == -1) {
61 $page = 0;
62} // If $page is not defined, or '' or -1
63$offset = $limit * $page;
64$pageprev = $page - 1;
65$pagenext = $page + 1;
66if (!$sortfield) {
67 $sortfield = "f.datef,f.rowid"; // Set here default search field
68}
69if (!$sortorder) {
70 $sortorder = "DESC";
71}
72
73/*
74 * View
75 */
76
77$form = new Form($db);
78$userstatic = new User($db);
79
80llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-fourn page-recap-fourn');
81
82if ($socid > 0) {
83 $societe = new Societe($db);
84 $societe->fetch($socid);
85
86 /*
87 * Show tabs
88 */
89 $head = societe_prepare_head($societe);
90
91 print dol_get_fiche_head($head, 'supplier', $langs->trans("ThirdParty"), 0, 'company');
92 dol_banner_tab($societe, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom');
93 print dol_get_fiche_end();
94
95 if ((isModEnabled("fournisseur") && $user->hasRight("fournisseur", "facture", "lire") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) || (isModEnabled("supplier_invoice") && $user->hasRight("supplier_invoice", "lire"))) {
96 // Invoice list
97 print load_fiche_titre($langs->trans("SupplierPreview"));
98
99 // Add parameter for sorting
100 $param = '';
101 if ($socid > 0) {
102 $param .= '&socid=' . $socid;
103 }
104
105 print '<table class="noborder tagtable liste centpercent">';
106 print '<tr class="liste_titre">';
107 print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
108 print '<td>' . $langs->trans("Element") . '</td>';
109 print '<td>' . $langs->trans("Status") . '</td>';
110 print '<td class="right">' . $langs->trans("Debit") . '</td>';
111 print '<td class="right">' . $langs->trans("Credit") . '</td>';
112 print '<td class="right">' . $langs->trans("Balance") . '</td>';
113 print '<td class="right">' . $langs->trans("Author") . '</td>';
114 print '</tr>';
115
117 $TData = array();
118
119 $sql = "SELECT s.nom, s.rowid as socid, f.ref_supplier, f.total_ttc, f.datef as df,";
120 $sql .= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
121 $sql .= " u.login, u.rowid as userid";
122 $sql .= " FROM " . MAIN_DB_PREFIX . "societe as s," . MAIN_DB_PREFIX . "facture_fourn as f," . MAIN_DB_PREFIX . "user as u";
123 $sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = " . ((int) $societe->id);
124 $sql .= " AND f.entity IN (" . getEntity("facture_fourn") . ")"; // Recognition of the entity attributed to this invoice for Multicompany
125 $sql .= " AND f.fk_user_valid = u.rowid";
126 $sql .= $db->order($sortfield, $sortorder);
127
128 $resql = $db->query($sql);
129 if ($resql) {
130 $num = $db->num_rows($resql);
131
132 // Boucle sur chaque facture
133 for ($i = 0; $i < $num; $i++) {
134 $objf = $db->fetch_object($resql);
135
136 $fac = new FactureFournisseur($db);
137 $ret = $fac->fetch($objf->facid);
138 if ($ret < 0) {
139 print $fac->error . "<br>";
140 continue;
141 }
142 $totalpaid = $fac->getSommePaiement();
143
144 $userstatic->id = $objf->userid;
145 $userstatic->login = $objf->login;
146
147 $values = array(
148 'fk_facture' => $objf->facid,
149 'date' => $fac->date,
150 'datefieldforsort' => $fac->date . '-' . $fac->ref,
151 'link' => $fac->getNomUrl(1),
152 'status' => $fac->getLibStatut(2, $totalpaid),
153 'amount' => $fac->total_ttc,
154 'author' => $userstatic->getLoginUrl(1)
155 );
156
157 $TData[] = $values;
158
159 // Payments
160 $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
161 $sql .= " p.fk_user_author, u.login, u.rowid as userid";
162 $sql .= " FROM " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as pf,";
163 $sql .= " " . MAIN_DB_PREFIX . "paiementfourn as p";
164 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u ON p.fk_user_author = u.rowid";
165 $sql .= " WHERE pf.fk_paiementfourn = p.rowid";
166 $sql .= " AND pf.fk_facturefourn = " . ((int) $fac->id);
167 $sql .= " ORDER BY p.datep ASC, p.rowid ASC";
168
169 $resqlp = $db->query($sql);
170 if ($resqlp) {
171 $nump = $db->num_rows($resqlp);
172 $j = 0;
173
174 while ($j < $nump) {
175 $objp = $db->fetch_object($resqlp);
176
177 $paymentstatic = new PaiementFourn($db);
178 $paymentstatic->id = $objp->rowid;
179
180 $userstatic->id = $objp->userid;
181 $userstatic->login = $objp->login;
182
183 $values = array(
184 'fk_paiement' => $objp->rowid,
185 'date' => $db->jdate($objp->dp),
186 'datefieldforsort' => $db->jdate($objp->dp) . '-' . $fac->ref,
187 'link' => $langs->trans("Payment") . ' ' . $paymentstatic->getNomUrl(1),
188 'status' => '',
189 'amount' => -$objp->amount,
190 'author' => $userstatic->getLoginUrl(1)
191 );
192
193 $TData[] = $values;
194
195 $j++;
196 }
197
198 $db->free($resqlp);
199 } else {
200 dol_print_error($db);
201 }
202 }
203 } else {
204 dol_print_error($db);
205 }
206
207 if (empty($TData)) {
208 print '<tr class="oddeven"><td colspan="7"><span class="opacitymedium">' . $langs->trans("NoInvoice") . '</span></td></tr>';
209 } else {
210 // Sort array by date ASC to calculate balance
211 $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
212
213 // Balance calculation
214 $balance = 0;
215 foreach (array_keys($TData) as $key) {
216 $balance += $TData[$key]['amount'];
217 if (!array_key_exists('balance', $TData[$key])) {
218 $TData[$key]['balance'] = 0;
219 }
220 $TData[$key]['balance'] += $balance;
221 }
222
223 // Resorte array to have elements on the required $sortorder
224 $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
225
226 $totalDebit = 0;
227 $totalCredit = 0;
228
229 // Display array
230 foreach ($TData as $data) {
231 $html_class = '';
232 if (!empty($data['fk_facture'])) {
233 $html_class = 'facid-' . $data['fk_facture'];
234 } elseif (!empty($data['fk_paiement'])) {
235 $html_class = 'payid-' . $data['fk_paiement'];
236 }
237
238 print '<tr class="oddeven ' . $html_class . '">';
239
240 $datedetail = dol_print_date($data['date'], 'dayhour');
241 if (!empty($data['fk_facture'])) {
242 $datedetail = dol_print_date($data['date'], 'day');
243 }
244 print '<td class="center" title="' . dol_escape_htmltag($datedetail) . '">';
245 print dol_print_date($data['date'], 'day');
246 print "</td>\n";
247
248 print '<td>' . $data['link'] . "</td>\n";
249
250 print '<td class="left">' . $data['status'] . '</td>';
251
252 print '<td class="right">' . (($data['amount'] > 0) ? price(abs($data['amount'])) : '') . "</td>\n";
253
254 $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
255
256 print '<td class="right">' . (($data['amount'] > 0) ? '' : price(abs($data['amount']))) . "</td>\n";
257 $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
258
259 // Balance
260 print '<td class="right"><span class="amount">' . price($data['balance']) . "</span></td>\n";
261
262 // Author
263 print '<td class="nowrap right">';
264 print $data['author'];
265 print '</td>';
266
267 print "</tr>\n";
268 }
269
270 print '<tr class="liste_total">';
271 print '<td colspan="3">&nbsp;</td>';
272 print '<td class="right">' . price($totalDebit) . '</td>';
273 print '<td class="right">' . price($totalCredit) . '</td>';
274 print '<td class="right">' . price(price2num($totalDebit - $totalCredit, 'MT')) . '</td>';
275 print '<td></td>';
276 print "</tr>\n";
277 }
278
279 print "</table>";
280 }
281} else {
282 dol_print_error($db);
283}
284
285// End of page
286llxFooter();
287$db->close();
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage suppliers invoices.
Class to manage generation of HTML components Only common components must be here.
Class to manage payments for supplier invoices.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
print_liste_field_titre($name, $file="", $field="", $begin="", $param="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
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, $morecssdiv='')
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_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...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...