box_factures_imp.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015-2019 Frederic France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/boxes/box_factures_imp.php
  22. * \ingroup factures
  23. * \brief Module de generation de l'affichage de la box factures impayees
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  27. /**
  28. * Class to manage the box to show last invoices
  29. */
  30. class box_factures_imp extends ModeleBoxes
  31. {
  32. public $boxcode = "oldestunpaidcustomerbills";
  33. public $boximg = "object_bill";
  34. public $boxlabel = "BoxOldestUnpaidCustomerBills";
  35. public $depends = array("facture");
  36. /**
  37. * @var DoliDB Database handler.
  38. */
  39. public $db;
  40. public $param;
  41. public $info_box_head = array();
  42. public $info_box_contents = array();
  43. /**
  44. * Constructor
  45. *
  46. * @param DoliDB $db Database handler
  47. * @param string $param More parameters
  48. */
  49. public function __construct($db, $param)
  50. {
  51. global $user;
  52. $this->db = $db;
  53. $this->hidden = !($user->rights->facture->lire);
  54. }
  55. /**
  56. * Load data into info_box_contents array to show array later.
  57. *
  58. * @param int $max Maximum number of records to load
  59. * @return void
  60. */
  61. public function loadBox($max = 5)
  62. {
  63. global $conf, $user, $langs;
  64. $this->max = $max;
  65. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  66. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  67. $facturestatic = new Facture($this->db);
  68. $societestatic = new Societe($this->db);
  69. $langs->load("bills");
  70. $this->info_box_head = array('text' => $langs->trans("BoxTitleOldestUnpaidCustomerBills", $max));
  71. if ($user->rights->facture->lire)
  72. {
  73. $sql = "SELECT s.nom as name, s.rowid as socid, s.email,";
  74. $sql .= " s.code_client,";
  75. $sql .= " s.logo,";
  76. $sql .= " f.ref, f.date_lim_reglement as datelimite,";
  77. $sql .= " f.type,";
  78. $sql .= " f.datef as df,";
  79. $sql .= " f.total as total_ht,";
  80. $sql .= " f.tva as total_tva,";
  81. $sql .= " f.total_ttc,";
  82. $sql .= " f.paye, f.fk_statut, f.rowid as facid";
  83. $sql .= ", sum(pf.amount) as am";
  84. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  85. if (!$user->rights->societe->client->voir && !$user->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  86. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  87. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid=pf.fk_facture ";
  88. $sql .= " WHERE f.fk_soc = s.rowid";
  89. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  90. $sql .= " AND f.paye = 0";
  91. $sql .= " AND fk_statut = 1";
  92. if (!$user->rights->societe->client->voir && !$user->socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
  93. if ($user->socid) $sql .= " AND s.rowid = ".$user->socid;
  94. $sql .= " GROUP BY s.nom, s.rowid, s.email, s.code_client, s.logo, f.ref, f.date_lim_reglement,";
  95. $sql .= " f.type, f.datef, f.total, f.tva, f.total_ttc, f.paye, f.fk_statut, f.rowid";
  96. //$sql.= " ORDER BY f.datef DESC, f.ref DESC ";
  97. $sql .= " ORDER BY datelimite ASC, f.ref ASC ";
  98. $sql .= $this->db->plimit($max, 0);
  99. $result = $this->db->query($sql);
  100. if ($result)
  101. {
  102. $num = $this->db->num_rows($result);
  103. $now = dol_now();
  104. $line = 0;
  105. $l_due_date = $langs->trans('Late').' ('.strtolower($langs->trans('DateDue')).': %s)';
  106. while ($line < $num)
  107. {
  108. $objp = $this->db->fetch_object($result);
  109. $datelimite = $this->db->jdate($objp->datelimite);
  110. $facturestatic->id = $objp->facid;
  111. $facturestatic->ref = $objp->ref;
  112. $facturestatic->type = $objp->type;
  113. $facturestatic->total_ht = $objp->total_ht;
  114. $facturestatic->total_tva = $objp->total_tva;
  115. $facturestatic->total_ttc = $objp->total_ttc;
  116. $facturestatic->statut = $objp->fk_statut;
  117. $facturestatic->date_lim_reglement = $this->db->jdate($objp->datelimite);
  118. $societestatic->id = $objp->socid;
  119. $societestatic->name = $objp->name;
  120. $societestatic->client = 1;
  121. $societestatic->email = $objp->email;
  122. $societestatic->code_client = $objp->code_client;
  123. $societestatic->logo = $objp->logo;
  124. $late = '';
  125. if ($facturestatic->hasDelay()) {
  126. $late = img_warning(sprintf($l_due_date, dol_print_date($datelimite, 'day')));
  127. }
  128. $this->info_box_contents[$line][] = array(
  129. 'td' => 'class="nowraponall"',
  130. 'text' => $facturestatic->getNomUrl(1),
  131. 'text2'=> $late,
  132. 'asis' => 1,
  133. );
  134. $this->info_box_contents[$line][] = array(
  135. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  136. 'text' => $societestatic->getNomUrl(1, '', 44),
  137. 'asis' => 1,
  138. );
  139. $this->info_box_contents[$line][] = array(
  140. 'td' => 'class="nowraponall right"',
  141. 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
  142. );
  143. $this->info_box_contents[$line][] = array(
  144. 'td' => 'class="right"',
  145. 'text' => dol_print_date($datelimite, 'day'),
  146. );
  147. $this->info_box_contents[$line][] = array(
  148. 'td' => 'class="right" width="18"',
  149. 'text' => $facturestatic->LibStatut($objp->paye, $objp->fk_statut, 3, $objp->am),
  150. );
  151. $line++;
  152. }
  153. if ($num == 0) $this->info_box_contents[$line][0] = array(
  154. 'td' => 'class="center opacitymedium"',
  155. 'text'=>$langs->trans("NoUnpaidCustomerBills")
  156. );
  157. $this->db->free($result);
  158. }
  159. else
  160. {
  161. $this->info_box_contents[0][0] = array(
  162. 'td' => '',
  163. 'maxlength'=>500,
  164. 'text' => ($this->db->error().' sql='.$sql),
  165. );
  166. }
  167. }
  168. else {
  169. $this->info_box_contents[0][0] = array(
  170. 'td' => 'class="nohover opacitymedium left"',
  171. 'text' => $langs->trans("ReadPermissionNotAllowed")
  172. );
  173. }
  174. }
  175. /**
  176. * Method to show box
  177. *
  178. * @param array $head Array with properties of box title
  179. * @param array $contents Array with properties of box lines
  180. * @param int $nooutput No print, only return string
  181. * @return string
  182. */
  183. public function showBox($head = null, $contents = null, $nooutput = 0)
  184. {
  185. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  186. }
  187. }