recap-compta.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2017 Pierre-Henry Favre <support@atm-consulting.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/recap-compta.php
  21. * \ingroup compta
  22. * \brief Page de fiche recap customer
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  28. // Load translation files required by the page
  29. $langs->load("companies");
  30. if (! empty($conf->facture->enabled)) $langs->load("bills");
  31. $id = GETPOST('id')?GETPOST('id','int'):GETPOST('socid','int');
  32. // Security check
  33. if ($user->societe_id) $id=$user->societe_id;
  34. $result = restrictedArea($user, 'societe', $id, '&societe');
  35. $object = new Societe($db);
  36. if ($id > 0) $object->fetch($id);
  37. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  38. $hookmanager->initHooks(array('recapcomptacard','globalcard'));
  39. // Load variable for pagination
  40. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  41. $sortfield = GETPOST('sortfield','alpha');
  42. $sortorder = GETPOST('sortorder','alpha');
  43. $page = GETPOST('page','int');
  44. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  45. $offset = $limit * $page;
  46. $pageprev = $page - 1;
  47. $pagenext = $page + 1;
  48. if (! $sortfield) $sortfield="f.datef,f.rowid"; // Set here default search field
  49. if (! $sortorder) $sortorder="DESC";
  50. $arrayfields=array(
  51. 'f.datef'=>array('label'=>"Date", 'checked'=>1),
  52. //...
  53. );
  54. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  55. $hookmanager->initHooks(array('supplierbalencelist','globalcard'));
  56. /*
  57. * Actions
  58. */
  59. $parameters = array('socid' => $id);
  60. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
  61. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  62. // None
  63. /*
  64. * View
  65. */
  66. $form = new Form($db);
  67. $userstatic=new User($db);
  68. $title=$langs->trans("ThirdParty").' - '.$langs->trans("Summary");
  69. if (! empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/',$conf->global->MAIN_HTML_TITLE) && $object->name) $title=$object->name.' - '.$langs->trans("Symmary");
  70. $help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
  71. llxHeader('',$title,$help_url);
  72. if ($id > 0)
  73. {
  74. $param='';
  75. if ($id > 0) $param.='&socid='.$id;
  76. $head = societe_prepare_head($object);
  77. dol_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
  78. dol_banner_tab($object, 'socid', '', ($user->societe_id?0:1), 'rowid', 'nom', '', '', 0, '', '', 1);
  79. dol_fiche_end();
  80. if (! empty($conf->facture->enabled) && $user->rights->facture->lire)
  81. {
  82. // Invoice list
  83. print load_fiche_titre($langs->trans("CustomerPreview"));
  84. print '<table class="noborder tagtable liste" width="100%">';
  85. print '<tr class="liste_titre">';
  86. if (! empty($arrayfields['f.datef']['checked'])) print_liste_field_titre($arrayfields['f.datef']['label'],$_SERVER["PHP_SELF"],"f.datef","",$param,'align="center" class="nowrap"',$sortfield,$sortorder);
  87. print '<td>'.$langs->trans("Element").'</td>';
  88. print '<td>'.$langs->trans("Status").'</td>';
  89. print '<td align="right">'.$langs->trans("Debit").'</td>';
  90. print '<td align="right">'.$langs->trans("Credit").'</td>';
  91. print '<td align="right">'.$langs->trans("Balance").'</td>';
  92. print '<td align="right">'.$langs->trans("Author").'</td>';
  93. print '</tr>';
  94. $TData = array();
  95. $sql = "SELECT s.nom, s.rowid as socid, f.ref, f.amount, f.datef as df,";
  96. $sql.= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
  97. $sql.= " u.login, u.rowid as userid";
  98. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f,".MAIN_DB_PREFIX."user as u";
  99. $sql.= " WHERE f.fk_soc = s.rowid AND s.rowid = ".$object->id;
  100. $sql.= " AND f.entity = ".$conf->entity;
  101. $sql.= " AND f.fk_user_valid = u.rowid";
  102. $sql.= $db->order($sortfield, $sortorder);
  103. $resql=$db->query($sql);
  104. if ($resql)
  105. {
  106. $num = $db->num_rows($resql);
  107. // Boucle sur chaque facture
  108. for ($i = 0 ; $i < $num ; $i++)
  109. {
  110. $objf = $db->fetch_object($resql);
  111. $fac = new Facture($db);
  112. $ret=$fac->fetch($objf->facid);
  113. if ($ret < 0)
  114. {
  115. print $fac->error."<br>";
  116. continue;
  117. }
  118. $totalpaye = $fac->getSommePaiement();
  119. $userstatic->id=$objf->userid;
  120. $userstatic->login=$objf->login;
  121. $values = array(
  122. 'fk_facture' => $objf->facid,
  123. 'date' => $fac->date,
  124. 'datefieldforsort' => $fac->date.'-'.$fac->ref,
  125. 'link' => $fac->getNomUrl(1),
  126. 'status' => $fac->getLibStatut(2,$totalpaye),
  127. 'amount' => $fac->total_ttc,
  128. 'author' => $userstatic->getLoginUrl(1)
  129. );
  130. $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic);
  131. $reshook = $hookmanager->executeHooks('facdao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
  132. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  133. $TData[] = $values;
  134. // Paiements
  135. $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
  136. $sql.= " p.fk_user_creat, u.login, u.rowid as userid";
  137. $sql.= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf,";
  138. $sql.= " ".MAIN_DB_PREFIX."paiement as p";
  139. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_creat = u.rowid";
  140. $sql.= " WHERE pf.fk_paiement = p.rowid";
  141. $sql.= " AND p.entity = ".$conf->entity;
  142. $sql.= " AND pf.fk_facture = ".$fac->id;
  143. $sql.= " ORDER BY p.datep ASC, p.rowid ASC";
  144. $resqlp = $db->query($sql);
  145. if ($resqlp)
  146. {
  147. $nump = $db->num_rows($resqlp);
  148. $j = 0;
  149. while ($j < $nump)
  150. {
  151. $objp = $db->fetch_object($resqlp);
  152. $paymentstatic = new Paiement($db);
  153. $paymentstatic->id = $objp->rowid;
  154. $userstatic->id=$objp->userid;
  155. $userstatic->login=$objp->login;
  156. $values = array(
  157. 'fk_paiement' => $objp->rowid,
  158. 'date' => $db->jdate($objp->dp),
  159. 'datefieldforsort' => $db->jdate($objp->dp).'-'.$fac->ref,
  160. 'link' => $langs->trans("Payment") .' '. $paymentstatic->getNomUrl(1),
  161. 'status' => '',
  162. 'amount' => -$objp->amount,
  163. 'author' => $userstatic->getLoginUrl(1)
  164. );
  165. $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic, 'paymentstatic' => $paymentstatic);
  166. $reshook = $hookmanager->executeHooks('paydao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
  167. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  168. $TData[] = $values;
  169. $j++;
  170. }
  171. $db->free($resqlp);
  172. }
  173. else
  174. {
  175. dol_print_error($db);
  176. }
  177. }
  178. }
  179. else
  180. {
  181. dol_print_error($db);
  182. }
  183. if(empty($TData)) {
  184. print '<tr class="oddeven"><td colspan="7">'.$langs->trans("NoInvoice").'</td></tr>';
  185. } else {
  186. // Sort array by date ASC to calucalte balance
  187. $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
  188. // Balance calculation
  189. $balance = 0;
  190. foreach($TData as &$data1) {
  191. $balance += $data1['amount'];
  192. $data1['balance'] += $balance;
  193. }
  194. // Resorte array to have elements on the required $sortorder
  195. $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
  196. $totalDebit = 0;
  197. $totalCredit = 0;
  198. // Display array
  199. foreach($TData as $data) {
  200. $html_class = '';
  201. if (!empty($data['fk_facture'])) $html_class = 'facid-'.$data['fk_facture'];
  202. elseif (!empty($data['fk_paiement'])) $html_class = 'payid-'.$data['fk_paiement'];
  203. print '<tr class="oddeven '.$html_class.'">';
  204. print "<td align=\"center\">";
  205. if (!empty($data['fk_facture'])) print dol_print_date($data['date'],'day');
  206. elseif (!empty($data['fk_paiement'])) print dol_print_date($data['date'],'dayhour');
  207. print "</td>\n";
  208. print '<td>'.$data['link']."</td>\n";
  209. print '<td aling="left">'.$data['status'].'</td>';
  210. print '<td align="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
  211. $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
  212. print '<td align="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
  213. $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
  214. // Balance
  215. print '<td align="right">'.price($data['balance'])."</td>\n";
  216. // Author
  217. print '<td class="nowrap" align="right">';
  218. print $data['author'];
  219. print '</td>';
  220. print "</tr>\n";
  221. }
  222. print '<tr class="liste_total">';
  223. print '<td colspan="3">&nbsp;</td>';
  224. print '<td align="right">'.price($totalDebit).'</td>';
  225. print '<td align="right">'.price($totalCredit).'</td>';
  226. print '<td align="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
  227. print '<td></td>';
  228. print "</tr>\n";
  229. }
  230. print "</table>";
  231. }
  232. }
  233. else
  234. {
  235. dol_print_error($db);
  236. }
  237. llxFooter();
  238. $db->close();