treso.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. *
  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. /**
  22. * \file htdocs/compta/bank/treso.php
  23. * \ingroup banque
  24. * \brief Page to estimate future balance
  25. */
  26. // Load Dolibarr environment
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array('banks', 'categories', 'bills', 'companies'));
  36. // Security check
  37. if (GETPOSTISSET("account") || GETPOSTISSET("ref")) {
  38. $id = GETPOSTISSET("account") ? GETPOST("account") : (GETPOSTISSET("ref") ? GETPOST("ref") : '');
  39. }
  40. $fieldid = GETPOSTISSET("ref") ? 'ref' : 'rowid';
  41. if ($user->socid) {
  42. $socid = $user->socid;
  43. }
  44. $result = restrictedArea($user, 'banque', $id, 'bank_account&bank_account', '', '', $fieldid);
  45. $vline = GETPOST('vline');
  46. $page = GETPOSTISSET("page") ? GETPOST("page") : 0;
  47. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  48. $hookmanager->initHooks(array('banktreso', 'globalcard'));
  49. /*
  50. * View
  51. */
  52. $societestatic = new Societe($db);
  53. $facturestatic = new Facture($db);
  54. $facturefournstatic = new FactureFournisseur($db);
  55. $socialcontribstatic = new ChargeSociales($db);
  56. $form = new Form($db);
  57. if (GETPOST("account") || GETPOST("ref")) {
  58. if ($vline) {
  59. $viewline = $vline;
  60. } else {
  61. $viewline = 20;
  62. }
  63. $object = new Account($db);
  64. if (GETPOST("account", 'int')) {
  65. $result = $object->fetch(GETPOST("account", 'int'));
  66. }
  67. if (GETPOST("ref")) {
  68. $result = $object->fetch(0, GETPOST("ref"));
  69. $_GET["account"] = $object->id;
  70. }
  71. $title = $object->ref.' - '.$langs->trans("PlannedTransactions");
  72. $helpurl = "";
  73. llxHeader('', $title, $helpurl);
  74. // Onglets
  75. $head = bank_prepare_head($object);
  76. print dol_get_fiche_head($head, 'cash', $langs->trans("FinancialAccount"), 0, 'account');
  77. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  78. $morehtmlref = '';
  79. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
  80. print dol_get_fiche_end();
  81. // Remainder to pay in future
  82. $sqls = array();
  83. // Customer invoices
  84. $sql = "SELECT 'invoice' as family, f.rowid as objid, f.ref as ref, f.total_ttc, f.type, f.date_lim_reglement as dlr,";
  85. $sql .= " s.rowid as socid, s.nom as name, s.fournisseur";
  86. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  87. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
  88. $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
  89. $sql .= " AND f.paye = 0 AND f.fk_statut = 1"; // Not paid
  90. $sql .= " AND (f.fk_account IN (0, ".$object->id.") OR f.fk_account IS NULL)"; // Id bank account of invoice
  91. $sql .= " ORDER BY dlr ASC";
  92. $sqls[] = $sql;
  93. // Supplier invoices
  94. $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,";
  95. $sql .= " s.rowid as socid, s.nom as name, s.fournisseur";
  96. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as ff";
  97. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON ff.fk_soc = s.rowid";
  98. $sql .= " WHERE ff.entity = ".$conf->entity;
  99. $sql .= " AND ff.paye = 0 AND fk_statut = 1"; // Not paid
  100. $sql .= " AND (ff.fk_account IN (0, ".$object->id.") OR ff.fk_account IS NULL)"; // Id bank account of supplier invoice
  101. $sql .= " ORDER BY dlr ASC";
  102. $sqls[] = $sql;
  103. // Social contributions
  104. $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,";
  105. $sql .= " 0 as socid, 'noname' as name, 0 as fournisseur";
  106. $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
  107. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as ccs ON cs.fk_type = ccs.id";
  108. $sql .= " WHERE cs.entity = ".$conf->entity;
  109. $sql .= " AND cs.paye = 0"; // Not paid
  110. $sql .= " AND (cs.fk_account IN (0, ".$object->id.") OR cs.fk_account IS NULL)"; // Id bank account of social contribution
  111. $sql .= " ORDER BY dlr ASC";
  112. $sqls[] = $sql;
  113. // others sql
  114. $parameters = array();
  115. $reshook = $hookmanager->executeHooks('addMoreSQL', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  116. if (empty($reshook) and isset($hookmanager->resArray['sql'])) {
  117. $sqls[] = $hookmanager->resArray['sql'];
  118. }
  119. $error = 0;
  120. $tab_sqlobjOrder = array();
  121. $tab_sqlobj = array();
  122. $nbtotalofrecords = 0;
  123. foreach ($sqls as $sql) {
  124. $resql = $db->query($sql);
  125. if ($resql) {
  126. $nbtotalofrecords += $db->num_rows($resql);
  127. while ($sqlobj = $db->fetch_object($resql)) {
  128. $tmpobj = new stdClass();
  129. $tmpobj->family = $sqlobj->family;
  130. $tmpobj->objid = $sqlobj->objid;
  131. $tmpobj->ref = $sqlobj->ref;
  132. $tmpobj->total_ttc = $sqlobj->total_ttc;
  133. $tmpobj->type = $sqlobj->type;
  134. $tmpobj->dlr = $db->jdate($sqlobj->dlr);
  135. $tmpobj->socid = $sqlobj->socid;
  136. $tmpobj->name = $sqlobj->name;
  137. $tmpobj->fournisseur = $sqlobj->fournisseur;
  138. $tab_sqlobj[] = $tmpobj;
  139. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dlr);
  140. }
  141. $db->free($resql);
  142. } else {
  143. $error++;
  144. }
  145. }
  146. $param = '';
  147. $sortfield = '';
  148. $sortorder = '';
  149. $massactionbutton = '';
  150. $num = 0;
  151. $picto = '';
  152. $morehtml = '';
  153. $limit = 0;
  154. print_barre_liste($langs->trans("RemainderToPay"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, $picto, 0, $morehtml, '', $limit, 0, 0, 1);
  155. $solde = $object->solde(0);
  156. if (getDolGlobalInt('MULTICOMPANY_INVOICE_SHARING_ENABLED')) {
  157. $colspan = 6;
  158. } else {
  159. $colspan = 5;
  160. }
  161. // Show next coming entries
  162. print '<div class="div-table-responsive">';
  163. print '<table class="noborder centpercent">';
  164. // Ligne de titre tableau des ecritures
  165. print '<tr class="liste_titre">';
  166. print '<td>'.$langs->trans("DateDue").'</td>';
  167. print '<td>'.$langs->trans("Description").'</td>';
  168. if (getDolGlobalInt('MULTICOMPANY_INVOICE_SHARING_ENABLED')) {
  169. print '<td>'.$langs->trans("Entity").'</td>';
  170. }
  171. print '<td>'.$langs->trans("ThirdParty").'</td>';
  172. print '<td class="right">'.$langs->trans("Debit").'</td>';
  173. print '<td class="right">'.$langs->trans("Credit").'</td>';
  174. print '<td class="right" width="80">'.$langs->trans("BankBalance").'</td>';
  175. print '</tr>';
  176. // Current balance
  177. print '<tr class="liste_total">';
  178. print '<td class="left" colspan="5">'.$langs->trans("CurrentBalance").'</td>';
  179. print '<td class="nowrap right">'.price($solde).'</td>';
  180. print '</tr>';
  181. // Sort array
  182. if (!$error) {
  183. array_multisort($tab_sqlobjOrder, $tab_sqlobj);
  184. $num = count($tab_sqlobj);
  185. $i = 0;
  186. while ($i < $num) {
  187. $ref = '';
  188. $refcomp = '';
  189. $totalpayment = '';
  190. $tmpobj = array_shift($tab_sqlobj);
  191. if ($tmpobj->family == 'invoice_supplier') {
  192. $showline = 1;
  193. // Uncomment this line to avoid to count suppliers credit note (ff.type = 2)
  194. //$showline=(($tmpobj->total_ttc < 0 && $tmpobj->type != 2) || ($tmpobj->total_ttc > 0 && $tmpobj->type == 2))
  195. if ($showline) {
  196. $ref = $tmpobj->ref;
  197. $facturefournstatic->ref = $ref;
  198. $facturefournstatic->id = $tmpobj->objid;
  199. $facturefournstatic->type = $tmpobj->type;
  200. $ref = $facturefournstatic->getNomUrl(1, '');
  201. $societestatic->id = $tmpobj->socid;
  202. $societestatic->name = $tmpobj->name;
  203. $refcomp = $societestatic->getNomUrl(1, '', 24);
  204. $totalpayment = -1 * $facturefournstatic->getSommePaiement(); // Payment already done
  205. }
  206. }
  207. if ($tmpobj->family == 'invoice') {
  208. $facturestatic->ref = $tmpobj->ref;
  209. $facturestatic->id = $tmpobj->objid;
  210. $facturestatic->type = $tmpobj->type;
  211. $ref = $facturestatic->getNomUrl(1, '');
  212. $societestatic->id = $tmpobj->socid;
  213. $societestatic->name = $tmpobj->name;
  214. $refcomp = $societestatic->getNomUrl(1, '', 24);
  215. $totalpayment = $facturestatic->getSommePaiement(); // Payment already done
  216. $totalpayment += $facturestatic->getSumDepositsUsed();
  217. $totalpayment += $facturestatic->getSumCreditNotesUsed();
  218. }
  219. if ($tmpobj->family == 'social_contribution') {
  220. $socialcontribstatic->ref = $tmpobj->ref;
  221. $socialcontribstatic->id = $tmpobj->objid;
  222. $socialcontribstatic->label = $tmpobj->type;
  223. $ref = $socialcontribstatic->getNomUrl(1, 24);
  224. $totalpayment = -1 * $socialcontribstatic->getSommePaiement(); // Payment already done
  225. }
  226. $parameters = array('obj' => $tmpobj, 'ref' => $ref, 'refcomp' => $refcomp, 'totalpayment' => $totalpayment);
  227. $reshook = $hookmanager->executeHooks('moreFamily', $parameters, $tmpobject, $action); // Note that $action and $tmpobject may have been modified by hook
  228. if (empty($reshook)) {
  229. $ref = isset($hookmanager->resArray['ref']) ? $hookmanager->resArray['ref'] : $ref;
  230. $refcomp = isset($hookmanager->resArray['refcomp']) ? $hookmanager->resArray['refcomp'] : $refcomp;
  231. $totalpayment = isset($hookmanager->resArray['totalpayment']) ? $hookmanager->resArray['totalpayment'] : $totalpayment;
  232. }
  233. $total_ttc = $tmpobj->total_ttc;
  234. if ($totalpayment) {
  235. $total_ttc = $tmpobj->total_ttc - $totalpayment;
  236. }
  237. $solde += $total_ttc;
  238. // We discard lines with a remainder to pay to 0
  239. if (price2num($total_ttc) != 0) {
  240. // Show line
  241. print '<tr class="oddeven">';
  242. print '<td>';
  243. if ($tmpobj->dlr) {
  244. print dol_print_date($tmpobj->dlr, "day");
  245. } else {
  246. print $langs->trans("NotDefined");
  247. }
  248. print "</td>";
  249. print "<td>".$ref."</td>";
  250. if (getDolGlobalString("MULTICOMPANY_INVOICE_SHARING_ENABLED")) {
  251. if ($tmpobj->family == 'invoice') {
  252. $mc->getInfo($tmpobj->entity);
  253. print "<td>".$mc->label."</td>";
  254. } else {
  255. print "<td></td>";
  256. }
  257. }
  258. print "<td>".$refcomp."</td>";
  259. if ($tmpobj->total_ttc < 0) {
  260. print '<td class="nowrap right">'.price(abs($total_ttc))."</td><td>&nbsp;</td>";
  261. };
  262. if ($tmpobj->total_ttc >= 0) {
  263. print '<td>&nbsp;</td><td class="nowrap right">'.price($total_ttc)."</td>";
  264. };
  265. print '<td class="nowrap right">'.price($solde).'</td>';
  266. print "</tr>";
  267. }
  268. $i++;
  269. }
  270. } else {
  271. dol_print_error($db);
  272. }
  273. // Other lines
  274. $parameters = array('solde' => $solde);
  275. $reshook = $hookmanager->executeHooks('printObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  276. if (empty($reshook)) {
  277. print $hookmanager->resPrint;
  278. $solde = isset($hookmanager->resArray['solde']) ? $hookmanager->resArray['solde'] : $solde;
  279. }
  280. // solde
  281. print '<tr class="liste_total">';
  282. print '<td class="left" colspan="'.$colspan.'">'.$langs->trans("FutureBalance").' ('.$object->currency_code.')</td>';
  283. print '<td class="nowrap right">'.price($solde, 0, $langs, 0, 0, -1, $object->currency_code).'</td>';
  284. print '</tr>';
  285. print "</table>";
  286. print "</div>";
  287. } else {
  288. print $langs->trans("ErrorBankAccountNotFound");
  289. }
  290. // End of page
  291. llxFooter();
  292. $db->close();