box_factures_imp.php 8.5 KB

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