recap-compta.php 7.8 KB

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