box_factures_imp.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $sql = "SELECT s.rowid as socid, s.nom as name, s.name_alias";
  73. $sql .= ", s.code_client, s.code_compta, s.client";
  74. $sql .= ", s.logo, s.email, s.entity";
  75. $sql .= ", s.tva_intra, s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6";
  76. $sql .= ", f.ref, f.date_lim_reglement as datelimite";
  77. $sql .= ", f.type";
  78. $sql .= ", f.datef as df";
  79. $sql .= ", f.total_ht";
  80. $sql .= ", f.total_tva";
  81. $sql .= ", f.total_ttc";
  82. $sql .= ", f.paye, f.fk_statut as status, 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) {
  86. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  87. }
  88. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  89. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid=pf.fk_facture ";
  90. $sql .= " WHERE f.fk_soc = s.rowid";
  91. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  92. $sql .= " AND f.paye = 0";
  93. $sql .= " AND fk_statut = 1";
  94. if (!$user->rights->societe->client->voir && !$user->socid) {
  95. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  96. }
  97. if ($user->socid) {
  98. $sql .= " AND s.rowid = ".((int) $user->socid);
  99. }
  100. $sql .= " GROUP BY s.rowid, s.nom, s.name_alias, s.code_client, s.code_compta, s.client, s.logo, s.email, s.entity, s.tva_intra, s.siren, s.siret, s.ape, s.idprof4, s.idprof5, s.idprof6,";
  101. $sql .= " f.ref, f.date_lim_reglement,";
  102. $sql .= " f.type, f.datef, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.fk_statut, f.rowid";
  103. //$sql.= " ORDER BY f.datef DESC, f.ref DESC ";
  104. $sql .= " ORDER BY datelimite ASC, f.ref ASC ";
  105. $sql .= $this->db->plimit($max, 0);
  106. $result = $this->db->query($sql);
  107. if ($result) {
  108. $num = $this->db->num_rows($result);
  109. $now = dol_now();
  110. $line = 0;
  111. $l_due_date = $langs->trans('Late').' ('.strtolower($langs->trans('DateDue')).': %s)';
  112. while ($line < $num) {
  113. $objp = $this->db->fetch_object($result);
  114. $datelimite = $this->db->jdate($objp->datelimite);
  115. $facturestatic->id = $objp->facid;
  116. $facturestatic->ref = $objp->ref;
  117. $facturestatic->type = $objp->type;
  118. $facturestatic->total_ht = $objp->total_ht;
  119. $facturestatic->total_tva = $objp->total_tva;
  120. $facturestatic->total_ttc = $objp->total_ttc;
  121. $facturestatic->statut = $objp->status;
  122. $facturestatic->status = $objp->status;
  123. $facturestatic->date_lim_reglement = $this->db->jdate($objp->datelimite);
  124. $facturestatic->alreadypaid = $objp->paye;
  125. $societestatic->id = $objp->socid;
  126. $societestatic->name = $objp->name;
  127. //$societestatic->name_alias = $objp->name_alias;
  128. $societestatic->code_client = $objp->code_client;
  129. $societestatic->code_compta = $objp->code_compta;
  130. $societestatic->client = $objp->client;
  131. $societestatic->logo = $objp->logo;
  132. $societestatic->email = $objp->email;
  133. $societestatic->entity = $objp->entity;
  134. $societestatic->tva_intra = $objp->tva_intra;
  135. $societestatic->idprof1 = $objp->idprof1;
  136. $societestatic->idprof2 = $objp->idprof2;
  137. $societestatic->idprof3 = $objp->idprof3;
  138. $societestatic->idprof4 = $objp->idprof4;
  139. $societestatic->idprof5 = $objp->idprof5;
  140. $societestatic->idprof6 = $objp->idprof6;
  141. $late = '';
  142. if ($facturestatic->hasDelay()) {
  143. $late = img_warning(sprintf($l_due_date, dol_print_date($datelimite, 'day', 'tzuserrel')));
  144. }
  145. $this->info_box_contents[$line][] = array(
  146. 'td' => 'class="nowraponall"',
  147. 'text' => $facturestatic->getNomUrl(1),
  148. 'text2'=> $late,
  149. 'asis' => 1,
  150. );
  151. $this->info_box_contents[$line][] = array(
  152. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  153. 'text' => $societestatic->getNomUrl(1, '', 44),
  154. 'asis' => 1,
  155. );
  156. $this->info_box_contents[$line][] = array(
  157. 'td' => 'class="nowraponall right amount"',
  158. 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
  159. );
  160. $this->info_box_contents[$line][] = array(
  161. 'td' => 'class="right"',
  162. 'text' => dol_print_date($datelimite, 'day', 'tzuserrel'),
  163. );
  164. $this->info_box_contents[$line][] = array(
  165. 'td' => 'class="right" width="18"',
  166. 'text' => $facturestatic->LibStatut($objp->paye, $objp->status, 3, $objp->am),
  167. );
  168. $line++;
  169. }
  170. if ($num == 0) {
  171. $this->info_box_contents[$line][0] = array(
  172. 'td' => 'class="center opacitymedium"',
  173. 'text'=>$langs->trans("NoUnpaidCustomerBills")
  174. );
  175. }
  176. $this->db->free($result);
  177. } else {
  178. $this->info_box_contents[0][0] = array(
  179. 'td' => '',
  180. 'maxlength'=>500,
  181. 'text' => ($this->db->error().' sql='.$sql),
  182. );
  183. }
  184. } else {
  185. $this->info_box_contents[0][0] = array(
  186. 'td' => 'class="nohover opacitymedium left"',
  187. 'text' => $langs->trans("ReadPermissionNotAllowed")
  188. );
  189. }
  190. }
  191. /**
  192. * Method to show box
  193. *
  194. * @param array $head Array with properties of box title
  195. * @param array $contents Array with properties of box lines
  196. * @param int $nooutput No print, only return string
  197. * @return string
  198. */
  199. public function showBox($head = null, $contents = null, $nooutput = 0)
  200. {
  201. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  202. }
  203. }