box_factures_imp.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 Widget to show remain to get on sale invoices
  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 not paid sales 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->hasRight('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. //$this->max = 1000;
  66. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  67. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  68. $facturestatic = new Facture($this->db);
  69. $societestatic = new Societe($this->db);
  70. $langs->load("bills");
  71. $textHead = $langs->trans("BoxTitleOldestUnpaidCustomerBills");
  72. $this->info_box_head = array('text' => $langs->trans("BoxTitleOldestUnpaidCustomerBills", $this->max), 'limit'=> dol_strlen($textHead));
  73. if ($user->hasRight('facture', 'lire')) {
  74. $sql1 = "SELECT s.rowid as socid, s.nom as name, s.name_alias, s.code_client, s.client";
  75. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  76. $sql1 .= ", spe.accountancy_code_customer as code_compta";
  77. } else {
  78. $sql1 .= ", s.code_compta";
  79. }
  80. $sql1 .= ", s.logo, s.email, s.entity";
  81. $sql1 .= ", s.tva_intra, s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6";
  82. $sql1 .= ", f.ref, f.date_lim_reglement as datelimite";
  83. $sql1 .= ", f.type";
  84. $sql1 .= ", f.datef as date";
  85. $sql1 .= ", f.total_ht";
  86. $sql1 .= ", f.total_tva";
  87. $sql1 .= ", f.total_ttc";
  88. $sql1 .= ", f.paye, f.fk_statut as status, f.rowid as facid";
  89. $sql1 .= ", SUM(pf.amount) as am";
  90. $sql2 = " FROM ".MAIN_DB_PREFIX."societe as s";
  91. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  92. $sql2 .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
  93. }
  94. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  95. $sql2 .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  96. }
  97. $sql2 .= ", ".MAIN_DB_PREFIX."facture as f";
  98. $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
  99. $sql2 .= " WHERE f.fk_soc = s.rowid";
  100. $sql2 .= " AND f.entity IN (".getEntity('invoice').")";
  101. $sql2 .= " AND f.paye = 0";
  102. $sql2 .= " AND fk_statut = 1";
  103. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  104. $sql2 .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  105. }
  106. if ($user->socid) {
  107. $sql2 .= " AND s.rowid = ".((int) $user->socid);
  108. }
  109. $sql3 = " 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,";
  110. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  111. $sql3 .= " spe.accountancy_code_customer as code_compta,";
  112. } else {
  113. $sql3 .= " s.code_compta,";
  114. }
  115. $sql3 .= " f.rowid, f.ref, f.date_lim_reglement,";
  116. $sql3 .= " f.type, f.datef, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.fk_statut";
  117. $sql3 .= " ORDER BY datelimite ASC, f.ref ASC ";
  118. $sql3 .= $this->db->plimit($this->max + 1, 0);
  119. $sql = $sql1.$sql2.$sql3;
  120. $result = $this->db->query($sql);
  121. if ($result) {
  122. $num = $this->db->num_rows($result);
  123. $line = 0;
  124. $l_due_date = $langs->trans('Late').' ('.strtolower($langs->trans('DateDue')).': %s)';
  125. while ($line < min($num, $this->max)) {
  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->paye = $objp->paye;
  139. $facturestatic->alreadypaid = $objp->am;
  140. $societestatic->id = $objp->socid;
  141. $societestatic->name = $objp->name;
  142. //$societestatic->name_alias = $objp->name_alias;
  143. $societestatic->code_client = $objp->code_client;
  144. $societestatic->code_compta = $objp->code_compta;
  145. $societestatic->client = $objp->client;
  146. $societestatic->logo = $objp->logo;
  147. $societestatic->email = $objp->email;
  148. $societestatic->entity = $objp->entity;
  149. $societestatic->tva_intra = $objp->tva_intra;
  150. $societestatic->idprof1 = $objp->idprof1;
  151. $societestatic->idprof2 = $objp->idprof2;
  152. $societestatic->idprof3 = $objp->idprof3;
  153. $societestatic->idprof4 = $objp->idprof4;
  154. $societestatic->idprof5 = $objp->idprof5;
  155. $societestatic->idprof6 = $objp->idprof6;
  156. $late = '';
  157. if ($facturestatic->hasDelay()) {
  158. $late = img_warning(sprintf($l_due_date, dol_print_date($datelimite, 'day', 'tzuserrel')));
  159. }
  160. $this->info_box_contents[$line][] = array(
  161. 'td' => 'class="nowraponall"',
  162. 'text' => $facturestatic->getNomUrl(1),
  163. 'text2'=> $late,
  164. 'asis' => 1,
  165. );
  166. $this->info_box_contents[$line][] = array(
  167. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  168. 'text' => $societestatic->getNomUrl(1, '', 44),
  169. 'asis' => 1,
  170. );
  171. $this->info_box_contents[$line][] = array(
  172. 'td' => 'class="nowraponall right amount"',
  173. 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
  174. );
  175. $this->info_box_contents[$line][] = array(
  176. 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateDue").': '.dol_print_date($datelimite, 'day', 'tzuserrel')).'"',
  177. 'text' => dol_print_date($datelimite, 'day', 'tzuserrel'),
  178. );
  179. $this->info_box_contents[$line][] = array(
  180. 'td' => 'class="right" width="18"',
  181. 'text' => $facturestatic->LibStatut($objp->paye, $objp->status, 3, $objp->am),
  182. );
  183. $line++;
  184. }
  185. if ($this->max < $num) {
  186. $this->info_box_contents[$line][] = array('td' => 'colspan="6"', 'text' => '...');
  187. $line++;
  188. }
  189. if ($num == 0) {
  190. $this->info_box_contents[$line][0] = array(
  191. 'td' => 'class="center opacitymedium"',
  192. 'text'=>$langs->trans("NoUnpaidCustomerBills")
  193. );
  194. }
  195. $sql = "SELECT SUM(f.total_ht) as total_ht ".$sql2;
  196. $result = $this->db->query($sql);
  197. $objp = $this->db->fetch_object($result);
  198. $totalamount = $objp->total_ht;
  199. // Add the sum à the bottom of the boxes
  200. $this->info_box_contents[$line][] = array(
  201. 'tr' => 'class="liste_total_wrap"',
  202. 'td' => 'class="liste_total"',
  203. 'text' => $langs->trans("Total"),
  204. );
  205. $this->info_box_contents[$line][] = array(
  206. 'td' => 'class="liste_total"',
  207. 'text' => "&nbsp;",
  208. );
  209. $this->info_box_contents[$line][] = array(
  210. 'td' => 'class="right liste_total" ',
  211. 'text' => price($totalamount, 0, $langs, 0, -1, -1, $conf->currency),
  212. );
  213. $this->info_box_contents[$line][] = array(
  214. 'td' => 'class="liste_total"',
  215. 'text' => "&nbsp;",
  216. );
  217. $this->info_box_contents[$line][] = array(
  218. 'td' => 'class="liste_total"',
  219. 'text' => "&nbsp;",
  220. );
  221. $this->db->free($result);
  222. } else {
  223. $this->info_box_contents[0][0] = array(
  224. 'td' => '',
  225. 'maxlength'=>500,
  226. 'text' => ($this->db->error().' sql='.$sql),
  227. );
  228. }
  229. } else {
  230. $this->info_box_contents[0][0] = array(
  231. 'td' => 'class="nohover opacitymedium left"',
  232. 'text' => $langs->trans("ReadPermissionNotAllowed")
  233. );
  234. }
  235. }
  236. /**
  237. * Method to show box
  238. *
  239. * @param array $head Array with properties of box title
  240. * @param array $contents Array with properties of box lines
  241. * @param int $nooutput No print, only return string
  242. * @return string
  243. */
  244. public function showBox($head = null, $contents = null, $nooutput = 0)
  245. {
  246. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  247. }
  248. }