dolibarr 25.0.0-alpha
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-2026 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';
37require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
38require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
39require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php';
40
41
42// Load translation files required by the page
43$langs->loadLangs(array('bills', 'companies'));
44
45// Security check
46$socid = GETPOSTINT("socid");
47if ($user->isExternalUser()) {
48 $action = '';
49 $socid = $user->isExternalUser();
50}
51$result = restrictedArea($user, 'societe', $socid, '&societe');
52
53// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
54$hookmanager->initHooks(array('supplierbalencelist', 'globalcard'));
55
56// Load variable for pagination
57$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
58$sortfield = GETPOST('sortfield', 'aZ09comma');
59$sortorder = GETPOST('sortorder', 'aZ09comma');
60$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
61if (empty($page) || $page == -1) {
62 $page = 0;
63} // If $page is not defined, or '' or -1
64$offset = $limit * $page;
65$pageprev = $page - 1;
66$pagenext = $page + 1;
67if (!$sortfield) {
68 $sortfield = "f.datef,f.rowid"; // Set here default search field
69}
70if (!$sortorder) {
71 $sortorder = "DESC";
72}
73
74/*
75 * View
76 */
77
78$form = new Form($db);
79$userstatic = new User($db);
80
81llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-fourn page-recap-fourn');
82
83if ($socid > 0) {
84 $societe = new Societe($db);
85 $societe->fetch($socid);
86
87 /*
88 * Show tabs
89 */
90 $head = societe_prepare_head($societe);
91
92 print dol_get_fiche_head($head, 'supplier', $langs->trans("ThirdParty"), 0, 'company');
93 dol_banner_tab($societe, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom');
94 print dol_get_fiche_end();
95
96 if ((isModEnabled("fournisseur") && $user->hasRight("fournisseur", "facture", "lire") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) || (isModEnabled("supplier_invoice") && $user->hasRight("supplier_invoice", "lire"))) {
97 // Invoice list
98 print load_fiche_titre($langs->trans("SupplierPreview"));
99
100 // Add parameter for sorting
101 $param = '';
102 if ($socid > 0) {
103 $param .= '&socid=' . $socid;
104 }
105
106 print '<table class="noborder tagtable liste centpercent">';
107 print '<tr class="liste_titre">';
108 print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
109 print '<td>' . $langs->trans("Element") . '</td>';
110 print '<td>' . $langs->trans("Status") . '</td>';
111 print '<td class="right">' . $langs->trans("Debit") . '</td>';
112 print '<td class="right">' . $langs->trans("Credit") . '</td>';
113 print '<td class="right">' . $langs->trans("Balance") . '</td>';
114 print '<td class="right">' . $langs->trans("Author") . '</td>';
115 print '</tr>';
116
118 $TData = array();
119
120 $sql = "SELECT s.nom, s.rowid as socid, f.ref_supplier, 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_fourn as f," . MAIN_DB_PREFIX . "user as u";
124 $sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = " . ((int) $societe->id);
125 $sql .= " AND f.entity IN (" . getEntity("facture_fourn") . ")"; // Recognition of the entity attributed to this invoice for Multicompany
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 $num = $db->num_rows($resql);
132
133 // Loop over each invoice
134 for ($i = 0; $i < $num; $i++) {
135 $objf = $db->fetch_object($resql);
136
137 $fac = new FactureFournisseur($db);
138 $ret = $fac->fetch($objf->facid);
139 if ($ret < 0) {
140 print $fac->error . "<br>";
141 continue;
142 }
143 $totalpaid = $fac->getSommePaiement();
144
145 $userstatic->id = $objf->userid;
146 $userstatic->login = $objf->login;
147
148 $values = array(
149 'fk_facture' => $objf->facid,
150 'date' => $fac->date,
151 'datefieldforsort' => $fac->date . '-' . $fac->ref,
152 'link' => $fac->getNomUrl(1),
153 'status' => $fac->getLibStatut(2, $totalpaid),
154 'amount' => $fac->total_ttc,
155 'author' => $userstatic->getLoginUrl(1)
156 );
157
158 $TData[] = $values;
159
160 // Payments
161 $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
162 $sql .= " p.fk_user_author, u.login, u.rowid as userid";
163 $sql .= " FROM " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as pf,";
164 $sql .= " " . MAIN_DB_PREFIX . "paiementfourn as p";
165 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u ON p.fk_user_author = u.rowid";
166 $sql .= " WHERE pf.fk_paiementfourn = p.rowid";
167 $sql .= " AND pf.fk_facturefourn = " . ((int) $fac->id);
168 $sql .= " ORDER BY p.datep ASC, p.rowid ASC";
169
170 $resqlp = $db->query($sql);
171 if ($resqlp) {
172 $nump = $db->num_rows($resqlp);
173 $j = 0;
174
175 while ($j < $nump) {
176 $objp = $db->fetch_object($resqlp);
177
178 $paymentstatic = new PaiementFourn($db);
179 $paymentstatic->id = $objp->rowid;
180
181 $userstatic->id = $objp->userid;
182 $userstatic->login = $objp->login;
183
184 $values = array(
185 'fk_paiement' => $objp->rowid,
186 'date' => $db->jdate($objp->dp),
187 'datefieldforsort' => $db->jdate($objp->dp) . '-' . $fac->ref,
188 'link' => $langs->trans("Payment") . ' ' . $paymentstatic->getNomUrl(1),
189 'status' => '',
190 'amount' => -$objp->amount,
191 'author' => $userstatic->getLoginUrl(1)
192 );
193
194 $TData[] = $values;
195
196 $j++;
197 }
198
199 $db->free($resqlp);
200 } else {
201 dol_print_error($db);
202 }
203 }
204 } else {
205 dol_print_error($db);
206 }
207
208 if (empty($TData)) {
209 print '<tr class="oddeven"><td colspan="7"><span class="opacitymedium">' . $langs->trans("NoInvoice") . '</span></td></tr>';
210 } else {
211 // Sort array by date ASC to calculate balance
212 $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
213
214 // Balance calculation
215 $balance = 0;
216 foreach (array_keys($TData) as $key) {
217 $balance += $TData[$key]['amount'];
218 if (!array_key_exists('balance', $TData[$key])) {
219 $TData[$key]['balance'] = 0;
220 }
221 $TData[$key]['balance'] += $balance;
222 }
223
224 // Resorte array to have elements on the required $sortorder
225 $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
226
227 $totalDebit = 0;
228 $totalCredit = 0;
229
230 // Display array
231 foreach ($TData as $data) {
232 $html_class = '';
233 if (!empty($data['fk_facture'])) {
234 $html_class = 'facid-' . $data['fk_facture'];
235 } elseif (!empty($data['fk_paiement'])) {
236 $html_class = 'payid-' . $data['fk_paiement'];
237 }
238
239 print '<tr class="oddeven ' . $html_class . '">';
240
241 $datedetail = dol_print_date($data['date'], 'dayhour');
242 if (!empty($data['fk_facture'])) {
243 $datedetail = dol_print_date($data['date'], 'day');
244 }
245 print '<td class="center" title="' . dol_escape_htmltag($datedetail) . '">';
246 print dol_print_date($data['date'], 'day');
247 print "</td>\n";
248
249 print '<td>' . $data['link'] . "</td>\n";
250
251 print '<td class="left">' . $data['status'] . '</td>';
252
253 print '<td class="right">' . (($data['amount'] > 0) ? price(abs($data['amount'])) : '') . "</td>\n";
254
255 $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
256
257 print '<td class="right">' . (($data['amount'] > 0) ? '' : price(abs($data['amount']))) . "</td>\n";
258 $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
259
260 // Balance
261 print '<td class="right"><span class="amount">' . price($data['balance']) . "</span></td>\n";
262
263 // Author
264 print '<td class="nowrap right">';
265 print $data['author'];
266 print '</td>';
267
268 print "</tr>\n";
269 }
270
271 print '<tr class="liste_total">';
272 print '<td colspan="3">&nbsp;</td>';
273 print '<td class="right">' . price($totalDebit) . '</td>';
274 print '<td class="right">' . price($totalCredit) . '</td>';
275 print '<td class="right">' . price(price2num($totalDebit - $totalCredit, 'MT')) . '</td>';
276 print '<td></td>';
277 print "</tr>\n";
278 }
279
280 print "</table>";
281 }
282} else {
283 dol_print_error($db);
284}
285
286// End of page
287llxFooter();
288$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, $subtabs='')
Return array of tabs to used on pages for third parties cards.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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).
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 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.
print_liste_field_titre($name, $file="", $field="", $begin="", $param="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
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.
Definition html.lib.php:540
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:738
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:181
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.