treso.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2008-2009 Laurent Destailleur (Eldy) <eldy@users.sourceforge.net>
  4. * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
  5. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com
  6. * Copyright (C) 2016 Frédéric France <frederic.france@free.fr>
  7. * Copyright (C) 2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/compta/bank/treso.php
  24. * \ingroup banque
  25. * \brief Page to estimate future balance
  26. */
  27. // Load Dolibarr environment
  28. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  38. // Load translation files required by the page
  39. $langs->loadLangs(array('banks', 'bills', 'categories', 'companies', 'salaries'));
  40. // Security check
  41. if (GETPOSTISSET("account") || GETPOSTISSET("ref")) {
  42. $id = GETPOSTISSET("account") ? GETPOST("account") : (GETPOSTISSET("ref") ? GETPOST("ref") : '');
  43. }
  44. $fieldid = GETPOSTISSET("ref") ? 'ref' : 'rowid';
  45. if ($user->socid) {
  46. $socid = $user->socid;
  47. }
  48. $result = restrictedArea($user, 'banque', $id, 'bank_account&bank_account', '', '', $fieldid);
  49. $vline = GETPOST('vline');
  50. $page = GETPOSTISSET("page") ? GETPOST("page") : 0;
  51. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  52. $hookmanager->initHooks(array('banktreso', 'globalcard'));
  53. /*
  54. * View
  55. */
  56. $societestatic = new Societe($db);
  57. $userstatic = new User($db);
  58. $facturestatic = new Facture($db);
  59. $facturefournstatic = new FactureFournisseur($db);
  60. $socialcontribstatic = new ChargeSociales($db);
  61. $salarystatic = new Salary($db);
  62. $vatstatic = new TVA($db);
  63. $form = new Form($db);
  64. if (GETPOST("account") || GETPOST("ref")) {
  65. if ($vline) {
  66. $viewline = $vline;
  67. } else {
  68. $viewline = 20;
  69. }
  70. $object = new Account($db);
  71. if (GETPOST("account", 'int')) {
  72. $result = $object->fetch(GETPOST("account", 'int'));
  73. }
  74. if (GETPOST("ref")) {
  75. $result = $object->fetch(0, GETPOST("ref"));
  76. $_GET["account"] = $object->id;
  77. }
  78. $title = $object->ref.' - '.$langs->trans("PlannedTransactions");
  79. $helpurl = "";
  80. llxHeader('', $title, $helpurl);
  81. // Onglets
  82. $head = bank_prepare_head($object);
  83. print dol_get_fiche_head($head, 'cash', $langs->trans("FinancialAccount"), 0, 'account');
  84. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  85. $morehtmlref = '';
  86. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
  87. print dol_get_fiche_end();
  88. // Remainder to pay in future
  89. $sqls = array();
  90. // Customer invoices
  91. $sql = "SELECT 'invoice' as family, f.rowid as objid, f.ref as ref, f.total_ttc, f.type, f.date_lim_reglement as dlr,";
  92. $sql .= " s.rowid as socid, s.nom as name, s.fournisseur";
  93. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  94. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
  95. $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
  96. $sql .= " AND f.paye = 0 AND f.fk_statut = 1"; // Not paid
  97. $sql .= " AND (f.fk_account IN (0, ".$object->id.") OR f.fk_account IS NULL)"; // Id bank account of invoice
  98. $sql .= " ORDER BY dlr ASC";
  99. $sqls[] = $sql;
  100. // Supplier invoices
  101. $sql = " SELECT 'invoice_supplier' as family, ff.rowid as objid, ff.ref as ref, ff.ref_supplier as ref_supplier, (-1*ff.total_ttc) as total_ttc, ff.type, ff.date_lim_reglement as dlr,";
  102. $sql .= " s.rowid as socid, s.nom as name, s.fournisseur";
  103. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as ff";
  104. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON ff.fk_soc = s.rowid";
  105. $sql .= " WHERE ff.entity = ".$conf->entity;
  106. $sql .= " AND ff.paye = 0 AND fk_statut = 1"; // Not paid
  107. $sql .= " AND (ff.fk_account IN (0, ".$object->id.") OR ff.fk_account IS NULL)"; // Id bank account of supplier invoice
  108. $sql .= " ORDER BY dlr ASC";
  109. $sqls[] = $sql;
  110. // Social contributions
  111. $sql = " SELECT 'social_contribution' as family, cs.rowid as objid, cs.libelle as ref, (-1*cs.amount) as total_ttc, ccs.libelle as type, cs.date_ech as dlr,";
  112. $sql .= " 0 as socid, 'noname' as name, 0 as fournisseur";
  113. $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
  114. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as ccs ON cs.fk_type = ccs.id";
  115. $sql .= " WHERE cs.entity = ".$conf->entity;
  116. $sql .= " AND cs.paye = 0"; // Not paid
  117. $sql .= " AND (cs.fk_account IN (0, ".$object->id.") OR cs.fk_account IS NULL)"; // Id bank account of social contribution
  118. $sql .= " ORDER BY dlr ASC";
  119. $sqls[] = $sql;
  120. // Salaries
  121. $sql = " SELECT 'salary' as family, sa.rowid as objid, sa.label as ref, (-1*sa.amount) as total_ttc, sa.dateep as dlr,";
  122. $sql .= " s.rowid as socid, CONCAT(s.firstname, ' ', s.lastname) as name, 0 as fournisseur";
  123. $sql .= " FROM ".MAIN_DB_PREFIX."salary as sa";
  124. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as s ON sa.fk_user = s.rowid";
  125. $sql .= " WHERE sa.entity = ".$conf->entity;
  126. $sql .= " AND sa.paye = 0"; // Not paid
  127. $sql .= " AND (sa.fk_account IN (0, ".$object->id.") OR sa.fk_account IS NULL)"; // Id bank account of salary
  128. $sql .= " ORDER BY dlr ASC";
  129. $sqls[] = $sql;
  130. // VAT
  131. $sql = " SELECT 'vat' as family, t.rowid as objid, t.label as ref, (-1*t.amount) as total_ttc, t.datev as dlr,";
  132. $sql .= " 0 as socid, 'noname' as name, 0 as fournisseur";
  133. $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
  134. $sql .= " WHERE t.entity = ".$conf->entity;
  135. $sql .= " AND t.paye = 0"; // Not paid
  136. $sql .= " AND (t.fk_account IN (-1, 0, ".$object->id.") OR t.fk_account IS NULL)"; // Id bank account of vat
  137. $sql .= " ORDER BY dlr ASC";
  138. $sqls[] = $sql;
  139. // others sql
  140. $parameters = array();
  141. $reshook = $hookmanager->executeHooks('addMoreSQL', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  142. if (empty($reshook) and isset($hookmanager->resArray['sql'])) {
  143. $sqls[] = $hookmanager->resArray['sql'];
  144. }
  145. $error = 0;
  146. $tab_sqlobjOrder = array();
  147. $tab_sqlobj = array();
  148. $nbtotalofrecords = 0;
  149. foreach ($sqls as $sql) {
  150. $resql = $db->query($sql);
  151. if ($resql) {
  152. $nbtotalofrecords += $db->num_rows($resql);
  153. while ($sqlobj = $db->fetch_object($resql)) {
  154. $tmpobj = new stdClass();
  155. $tmpobj->family = $sqlobj->family;
  156. $tmpobj->objid = $sqlobj->objid;
  157. $tmpobj->ref = $sqlobj->ref;
  158. $tmpobj->total_ttc = $sqlobj->total_ttc;
  159. $tmpobj->type = $sqlobj->type;
  160. $tmpobj->dlr = $db->jdate($sqlobj->dlr);
  161. $tmpobj->socid = $sqlobj->socid;
  162. $tmpobj->name = $sqlobj->name;
  163. $tmpobj->fournisseur = $sqlobj->fournisseur;
  164. $tab_sqlobj[] = $tmpobj;
  165. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dlr);
  166. }
  167. $db->free($resql);
  168. } else {
  169. $error++;
  170. }
  171. }
  172. $param = '';
  173. $sortfield = '';
  174. $sortorder = '';
  175. $massactionbutton = '';
  176. $num = 0;
  177. $picto = '';
  178. $morehtml = '';
  179. $limit = 0;
  180. print_barre_liste($langs->trans("RemainderToPay"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, $picto, 0, $morehtml, '', $limit, 0, 0, 1);
  181. $solde = $object->solde(0);
  182. if (getDolGlobalInt('MULTICOMPANY_INVOICE_SHARING_ENABLED')) {
  183. $colspan = 6;
  184. } else {
  185. $colspan = 5;
  186. }
  187. // Show next coming entries
  188. print '<div class="div-table-responsive">';
  189. print '<table class="noborder centpercent">';
  190. // Ligne de titre tableau des ecritures
  191. print '<tr class="liste_titre">';
  192. print '<td>'.$langs->trans("DateDue").'</td>';
  193. print '<td>'.$langs->trans("Description").'</td>';
  194. if (getDolGlobalInt('MULTICOMPANY_INVOICE_SHARING_ENABLED')) {
  195. print '<td>'.$langs->trans("Entity").'</td>';
  196. }
  197. print '<td>'.$langs->trans("ThirdParty").'</td>';
  198. print '<td class="right">'.$langs->trans("Debit").'</td>';
  199. print '<td class="right">'.$langs->trans("Credit").'</td>';
  200. print '<td class="right" width="80">'.$langs->trans("BankBalance").'</td>';
  201. print '</tr>';
  202. // Current balance
  203. print '<tr class="liste_total">';
  204. print '<td class="left" colspan="5">'.$langs->trans("CurrentBalance").'</td>';
  205. print '<td class="nowrap right">'.price($solde).'</td>';
  206. print '</tr>';
  207. // Sort array
  208. if (!$error) {
  209. array_multisort($tab_sqlobjOrder, $tab_sqlobj);
  210. $num = count($tab_sqlobj);
  211. $i = 0;
  212. while ($i < $num) {
  213. $ref = '';
  214. $refcomp = '';
  215. $totalpayment = '';
  216. $tmpobj = array_shift($tab_sqlobj);
  217. if ($tmpobj->family == 'invoice_supplier') {
  218. $showline = 1;
  219. // Uncomment this line to avoid to count suppliers credit note (ff.type = 2)
  220. //$showline=(($tmpobj->total_ttc < 0 && $tmpobj->type != 2) || ($tmpobj->total_ttc > 0 && $tmpobj->type == 2))
  221. if ($showline) {
  222. $ref = $tmpobj->ref;
  223. $facturefournstatic->ref = $ref;
  224. $facturefournstatic->id = $tmpobj->objid;
  225. $facturefournstatic->type = $tmpobj->type;
  226. $ref = $facturefournstatic->getNomUrl(1, '');
  227. $societestatic->id = $tmpobj->socid;
  228. $societestatic->name = $tmpobj->name;
  229. $refcomp = $societestatic->getNomUrl(1, '', 24);
  230. $totalpayment = -1 * $facturefournstatic->getSommePaiement(); // Payment already done
  231. }
  232. }
  233. if ($tmpobj->family == 'invoice') {
  234. $facturestatic->ref = $tmpobj->ref;
  235. $facturestatic->id = $tmpobj->objid;
  236. $facturestatic->type = $tmpobj->type;
  237. $ref = $facturestatic->getNomUrl(1, '');
  238. $societestatic->id = $tmpobj->socid;
  239. $societestatic->name = $tmpobj->name;
  240. $refcomp = $societestatic->getNomUrl(1, '', 24);
  241. $totalpayment = $facturestatic->getSommePaiement(); // Payment already done
  242. $totalpayment += $facturestatic->getSumDepositsUsed();
  243. $totalpayment += $facturestatic->getSumCreditNotesUsed();
  244. }
  245. if ($tmpobj->family == 'social_contribution') {
  246. $socialcontribstatic->ref = $tmpobj->ref;
  247. $socialcontribstatic->id = $tmpobj->objid;
  248. $socialcontribstatic->label = $tmpobj->type;
  249. $ref = $socialcontribstatic->getNomUrl(1, 24);
  250. $totalpayment = -1 * $socialcontribstatic->getSommePaiement(); // Payment already done
  251. }
  252. if ($tmpobj->family == 'salary') {
  253. $salarystatic->ref = $tmpobj->ref;
  254. $salarystatic->id = $tmpobj->objid;
  255. $salarystatic->label = $langs->trans("SalaryPayment");
  256. $ref = $salarystatic->getNomUrl(1, '');
  257. $userstatic->id = $tmpobj->socid;
  258. $userstatic->name = $tmpobj->name;
  259. $refcomp = $userstatic->getNomUrl(1);
  260. $totalpayment = -1 * $salarystatic->getSommePaiement(); // Payment already done
  261. }
  262. if ($tmpobj->family == 'vat') {
  263. $vatstatic->ref = $tmpobj->ref;
  264. $vatstatic->id = $tmpobj->objid;
  265. $vatstatic->type = $tmpobj->type;
  266. $ref = $vatstatic->getNomUrl(1, '');
  267. $totalpayment = -1 * $vatstatic->getSommePaiement(); // Payment already done
  268. }
  269. $parameters = array('obj' => $tmpobj, 'ref' => $ref, 'refcomp' => $refcomp, 'totalpayment' => $totalpayment);
  270. $reshook = $hookmanager->executeHooks('moreFamily', $parameters, $tmpobject, $action); // Note that $action and $tmpobject may have been modified by hook
  271. if (empty($reshook)) {
  272. $ref = isset($hookmanager->resArray['ref']) ? $hookmanager->resArray['ref'] : $ref;
  273. $refcomp = isset($hookmanager->resArray['refcomp']) ? $hookmanager->resArray['refcomp'] : $refcomp;
  274. $totalpayment = isset($hookmanager->resArray['totalpayment']) ? $hookmanager->resArray['totalpayment'] : $totalpayment;
  275. }
  276. $total_ttc = $tmpobj->total_ttc;
  277. if ($totalpayment) {
  278. $total_ttc = $tmpobj->total_ttc - $totalpayment;
  279. }
  280. $solde += $total_ttc;
  281. // We discard lines with a remainder to pay to 0
  282. if (price2num($total_ttc) != 0) {
  283. // Show line
  284. print '<tr class="oddeven">';
  285. print '<td>';
  286. if ($tmpobj->dlr) {
  287. print dol_print_date($tmpobj->dlr, "day");
  288. } else {
  289. print $langs->trans("NotDefined");
  290. }
  291. print "</td>";
  292. print "<td>".$ref."</td>";
  293. if (getDolGlobalString("MULTICOMPANY_INVOICE_SHARING_ENABLED")) {
  294. if ($tmpobj->family == 'invoice') {
  295. $mc->getInfo($tmpobj->entity);
  296. print "<td>".$mc->label."</td>";
  297. } else {
  298. print "<td></td>";
  299. }
  300. }
  301. print "<td>".$refcomp."</td>";
  302. if ($tmpobj->total_ttc < 0) {
  303. print '<td class="nowrap right">'.price(abs($total_ttc))."</td><td>&nbsp;</td>";
  304. };
  305. if ($tmpobj->total_ttc >= 0) {
  306. print '<td>&nbsp;</td><td class="nowrap right">'.price($total_ttc)."</td>";
  307. };
  308. print '<td class="nowrap right">'.price($solde).'</td>';
  309. print "</tr>";
  310. }
  311. $i++;
  312. }
  313. } else {
  314. dol_print_error($db);
  315. }
  316. // Other lines
  317. $parameters = array('solde' => $solde);
  318. $reshook = $hookmanager->executeHooks('printObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  319. if (empty($reshook)) {
  320. print $hookmanager->resPrint;
  321. $solde = isset($hookmanager->resArray['solde']) ? $hookmanager->resArray['solde'] : $solde;
  322. }
  323. // solde
  324. print '<tr class="liste_total">';
  325. print '<td class="left" colspan="'.$colspan.'">'.$langs->trans("FutureBalance").' ('.$object->currency_code.')</td>';
  326. print '<td class="nowrap right">'.price($solde, 0, $langs, 0, 0, -1, $object->currency_code).'</td>';
  327. print '</tr>';
  328. print "</table>";
  329. print "</div>";
  330. } else {
  331. print $langs->trans("ErrorBankAccountNotFound");
  332. }
  333. // End of page
  334. llxFooter();
  335. $db->close();