From 45336e50a461b395ddce56f92e4d9fc9e50945eb Mon Sep 17 00:00:00 2001 From: "ChristosK." <99069049+christoskaterini@users.noreply.github.com> Date: Sun, 21 Sep 2025 17:52:43 +0300 Subject: [PATCH] Update recap-fourn.php (#35417) * Update recap-fourn.php The key improvement is that now the balance calculation works correctly with descending order of movements. The balance will show the most recent transaction with the cumulative total of all transactions, and older transactions will show their respective balances as of that time. This implementation now matches the behavior of compta\recap-compta.php where the balance is calculated from bottom to top when the movements are displayed in descending order. * Update recap-fourn.php The key improvement is that now the balance calculation works correctly with descending order of movements. The balance will show the most recent transaction with the cumulative total of all transactions, and older transactions will show their respective balances as of that time. This implementation now matches the behavior of compta\recap-compta.php where the balance is calculated from bottom to top when the movements are displayed in descending order. * Update recap-fourn.php fix reordering by date column * Update recap-fourn.php * Update recap-fourn.php --------- Co-authored-by: Laurent Destailleur --- htdocs/fourn/recap-fourn.php | 209 +++++++++++++++++++++++++---------- 1 file changed, 151 insertions(+), 58 deletions(-) diff --git a/htdocs/fourn/recap-fourn.php b/htdocs/fourn/recap-fourn.php index 92ce582e8dd..3925c288bcd 100644 --- a/htdocs/fourn/recap-fourn.php +++ b/htdocs/fourn/recap-fourn.php @@ -27,8 +27,9 @@ // Load Dolibarr environment require '../main.inc.php'; -require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; -require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; +require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php'; +require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php'; +require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php'; /** * @var Conf $conf @@ -48,10 +49,27 @@ if ($user->socid > 0) { $socid = $user->socid; } - // Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context $hookmanager->initHooks(array('supplierbalencelist', 'globalcard')); +// Load variable for pagination +$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit; +$sortfield = GETPOST('sortfield', 'aZ09comma'); +$sortorder = GETPOST('sortorder', 'aZ09comma'); +$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page"); +if (empty($page) || $page == -1) { + $page = 0; +} // If $page is not defined, or '' or -1 +$offset = $limit * $page; +$pageprev = $page - 1; +$pagenext = $page + 1; +if (!$sortfield) { + $sortfield = "f.datef,f.rowid"; // Set here default search field +} +if (!$sortorder) { + $sortorder = "DESC"; +} + /* * View */ @@ -78,37 +96,39 @@ if ($socid > 0) { // Invoice list print load_fiche_titre($langs->trans("SupplierPreview")); - print ''; + // Add parameter for sorting + $param = ''; + if ($socid > 0) { + $param .= '&socid=' . $socid; + } - $sql = "SELECT s.nom, s.rowid as socid, f.ref_supplier, f.datef as df,"; + print '
'; + print ''; + print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + /** @var array $TData */ + $TData = array(); + + $sql = "SELECT s.nom, s.rowid as socid, f.ref_supplier, f.total_ttc, f.datef as df,"; $sql .= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,"; $sql .= " u.login, u.rowid as userid"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture_fourn as f,".MAIN_DB_PREFIX."user as u"; - $sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = ".((int) $societe->id); - $sql .= " AND f.entity IN (".getEntity("facture_fourn").")"; // Recognition of the entity attributed to this invoice for Multicompany + $sql .= " FROM " . MAIN_DB_PREFIX . "societe as s," . MAIN_DB_PREFIX . "facture_fourn as f," . MAIN_DB_PREFIX . "user as u"; + $sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = " . ((int) $societe->id); + $sql .= " AND f.entity IN (" . getEntity("facture_fourn") . ")"; // Recognition of the entity attributed to this invoice for Multicompany $sql .= " AND f.fk_user_valid = u.rowid"; - $sql .= " ORDER BY f.datef DESC"; + $sql .= $db->order($sortfield, $sortorder); $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - if ($num <= 0) { - print ''; - } - - $solde = 0; - // Boucle sur chaque facture for ($i = 0; $i < $num; $i++) { $objf = $db->fetch_object($resql); @@ -116,36 +136,35 @@ if ($socid > 0) { $fac = new FactureFournisseur($db); $ret = $fac->fetch($objf->facid); if ($ret < 0) { - print $fac->error."
"; + print $fac->error . "
"; continue; } $totalpaid = $fac->getSommePaiement(); - print ''; + $userstatic->id = $objf->userid; + $userstatic->login = $objf->login; - print '\n"; - print "\n"; + $values = array( + 'fk_facture' => $objf->facid, + 'date' => $fac->date, + 'datefieldforsort' => $fac->date . '-' . $fac->ref, + 'link' => $fac->getNomUrl(1), + 'status' => $fac->getLibStatut(2, $totalpaid), + 'amount' => $fac->total_ttc, + 'author' => $userstatic->getLoginUrl(1) + ); - print ''; - print '\n"; - $solde += $fac->total_ttc; - - print ''; - print '\n"; - - // Author - print ''; - - print "\n"; + $TData[] = $values; // Payments $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,"; $sql .= " p.fk_user_author, u.login, u.rowid as userid"; - $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf,"; - $sql .= " ".MAIN_DB_PREFIX."paiementfourn as p"; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_author = u.rowid"; + $sql .= " FROM " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as pf,"; + $sql .= " " . MAIN_DB_PREFIX . "paiementfourn as p"; + $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u ON p.fk_user_author = u.rowid"; $sql .= " WHERE pf.fk_paiementfourn = p.rowid"; - $sql .= " AND pf.fk_facturefourn = ".((int) $fac->id); + $sql .= " AND pf.fk_facturefourn = " . ((int) $fac->id); + $sql .= " ORDER BY p.datep ASC, p.rowid ASC"; $resqlp = $db->query($sql); if ($resqlp) { @@ -154,22 +173,24 @@ if ($socid > 0) { while ($j < $nump) { $objp = $db->fetch_object($resqlp); - // - print ''; - print '\n"; - print ''; - print "\n"; - print "\n"; - print ''; - $solde -= $objp->amount; - print '\n"; - // Auteur - print ''; + $paymentstatic = new PaiementFourn($db); + $paymentstatic->id = $objp->rowid; - print ''; + $userstatic->id = $objp->userid; + $userstatic->login = $objp->login; + + $values = array( + 'fk_paiement' => $objp->rowid, + 'date' => $db->jdate($objp->dp), + 'datefieldforsort' => $db->jdate($objp->dp) . '-' . $fac->ref, + 'link' => $langs->trans("Payment") . ' ' . $paymentstatic->getNomUrl(1), + 'status' => '', + 'amount' => -$objp->amount, + 'author' => $userstatic->getLoginUrl(1) + ); + + $TData[] = $values; $j++; } @@ -183,6 +204,78 @@ if ($socid > 0) { dol_print_error($db); } + if (empty($TData)) { + print ''; + } else { + // Sort array by date ASC to calculate balance + $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC'); + + // Balance calculation + $balance = 0; + foreach (array_keys($TData) as $key) { + $balance += $TData[$key]['amount']; + if (!array_key_exists('balance', $TData[$key])) { + $TData[$key]['balance'] = 0; + } + $TData[$key]['balance'] += $balance; + } + + // Resorte array to have elements on the required $sortorder + $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder); + + $totalDebit = 0; + $totalCredit = 0; + + // Display array + foreach ($TData as $data) { + $html_class = ''; + if (!empty($data['fk_facture'])) { + $html_class = 'facid-' . $data['fk_facture']; + } elseif (!empty($data['fk_paiement'])) { + $html_class = 'payid-' . $data['fk_paiement']; + } + + print ''; + + $datedetail = dol_print_date($data['date'], 'dayhour'); + if (!empty($data['fk_facture'])) { + $datedetail = dol_print_date($data['date'], 'day'); + } + print '\n"; + + print '\n"; + + print ''; + + print '\n"; + + $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0; + + print '\n"; + $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']); + + // Balance + print '\n"; + + // Author + print ''; + + print "\n"; + } + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + } + print "
' . $langs->trans("Element") . '' . $langs->trans("Status") . '' . $langs->trans("Debit") . '' . $langs->trans("Credit") . '' . $langs->trans("Balance") . '' . $langs->trans("Author") . '
'.$langs->trans("Date").' '.$langs->trans("Status").''.$langs->trans("Debit").''.$langs->trans("Credit").''.$langs->trans("Balance").' 
'.$langs->trans("NoInvoice").'
'.dol_print_date($fac->date)."id\">".img_object($langs->trans("ShowBill"), "bill")." ".$fac->ref."'.$fac->getLibStatut(2, $totalpaid).''.price($fac->total_ttc)." '.price($solde)."'.img_object($langs->trans("ShowUser"), 'user').' '.$objf->login.'
'.dol_print_date($db->jdate($objp->dp))."'; - print '      '; // Decalage - print ''.img_object($langs->trans("ShowPayment"), "payment").' '.$langs->trans("Payment").' '.$objp->rowid.'  '.price($objp->amount).''.price($solde)."'.img_object($langs->trans("ShowUser"), 'user').' '.$objp->login.'
' . $langs->trans("NoInvoice") . '
'; + print dol_print_date($data['date'], 'day'); + print "' . $data['link'] . "' . $data['status'] . '' . (($data['amount'] > 0) ? price(abs($data['amount'])) : '') . "' . (($data['amount'] > 0) ? '' : price(abs($data['amount']))) . "' . price($data['balance']) . "'; + print $data['author']; + print '
 ' . price($totalDebit) . '' . price($totalCredit) . '' . price(price2num($totalDebit - $totalCredit, 'MT')) . '
"; } } else {