box_factures_fourn_imp.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /* Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/boxes/box_factures_fourn_imp.php
  21. * \ingroup fournisseur
  22. * \brief Widget to show remain to get on purchase invoices
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  25. /**
  26. * Class to manage the box to show not paid suppliers invoices
  27. */
  28. class box_factures_fourn_imp extends ModeleBoxes
  29. {
  30. public $boxcode = "oldestunpaidsupplierbills";
  31. public $boximg = "object_bill";
  32. public $boxlabel = "BoxOldestUnpaidSupplierBills";
  33. public $depends = array("facture", "fournisseur");
  34. /**
  35. * @var DoliDB Database handler.
  36. */
  37. public $db;
  38. public $param;
  39. public $info_box_head = array();
  40. public $info_box_contents = array();
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. * @param string $param More parameters
  46. */
  47. public function __construct($db, $param)
  48. {
  49. global $user;
  50. $this->db = $db;
  51. $this->hidden = !($user->hasRight('fournisseur', 'facture', 'lire'));
  52. }
  53. /**
  54. * Load data into info_box_contents array to show array later.
  55. *
  56. * @param int $max Maximum number of records to load
  57. * @return void
  58. */
  59. public function loadBox($max = 5)
  60. {
  61. global $conf, $user, $langs;
  62. $this->max = $max;
  63. //$this->max = 1000;
  64. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  65. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  66. $facturestatic = new FactureFournisseur($this->db);
  67. $thirdpartystatic = new Fournisseur($this->db);
  68. $langs->load("bills");
  69. $this->info_box_head = array('text' => $langs->trans("BoxTitleOldestUnpaidSupplierBills", $this->max));
  70. if ($user->hasRight('fournisseur', 'facture', 'lire')) {
  71. $sql1 = "SELECT s.rowid as socid, s.nom as name, s.name_alias";
  72. $sql1 .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
  73. $sql1 .= ", s.logo, s.email, s.entity, s.tva_intra, s.siren, s.siret, s.ape, s.idprof4, s.idprof5, s.idprof6";
  74. $sql1 .= ", f.rowid as facid, f.ref, f.ref_supplier, f.date_lim_reglement as datelimite";
  75. $sql1 .= ", f.datef as df";
  76. $sql1 .= ", f.total_ht";
  77. $sql1 .= ", f.total_tva";
  78. $sql1 .= ", f.total_ttc";
  79. $sql1 .= ", f.paye, f.fk_statut as status, f.type";
  80. $sql1 .= ", f.tms";
  81. $sql1 .= ", SUM(pf.amount) as am";
  82. $sql2 = " FROM ".MAIN_DB_PREFIX."societe as s";
  83. $sql2 .= ",".MAIN_DB_PREFIX."facture_fourn as f";
  84. $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
  85. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  86. $sql2 .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  87. }
  88. $sql2 .= " WHERE f.fk_soc = s.rowid";
  89. $sql2 .= " AND f.entity IN (".getEntity('supplier_invoice').")";
  90. $sql2 .= " AND f.paye = 0";
  91. $sql2 .= " AND fk_statut = 1";
  92. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  93. $sql2 .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  94. }
  95. if ($user->socid) {
  96. $sql2 .= " AND s.rowid = ".((int) $user->socid);
  97. }
  98. $sql3 = " GROUP BY s.rowid, s.nom, s.name_alias, s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur, s.logo, s.email, s.entity, s.tva_intra, s.siren, s.siret, s.ape, s.idprof4, s.idprof5, s.idprof6,";
  99. $sql3 .= " f.rowid, f.ref, f.ref_supplier, f.date_lim_reglement,";
  100. $sql3 .= " f.type, f.datef, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.fk_statut, f.tms";
  101. $sql3 .= " ORDER BY datelimite DESC, f.ref_supplier DESC ";
  102. $sql3 .= $this->db->plimit($this->max + 1, 0);
  103. $sql = $sql1.$sql2.$sql3;
  104. $result = $this->db->query($sql);
  105. if ($result) {
  106. $num = $this->db->num_rows($result);
  107. $line = 0;
  108. $l_due_date = $langs->trans('Late').' ('.strtolower($langs->trans('DateDue')).': %s)';
  109. while ($line < min($num, $this->max)) {
  110. $objp = $this->db->fetch_object($result);
  111. $datelimite = $this->db->jdate($objp->datelimite);
  112. $date = $this->db->jdate($objp->df);
  113. $datem = $this->db->jdate($objp->tms);
  114. $facturestatic->id = $objp->facid;
  115. $facturestatic->ref = $objp->ref;
  116. $facturestatic->type = $objp->type;
  117. $facturestatic->total_ht = $objp->total_ht;
  118. $facturestatic->total_tva = $objp->total_tva;
  119. $facturestatic->total_ttc = $objp->total_ttc;
  120. $facturestatic->date = $date;
  121. $facturestatic->date_echeance = $datelimite;
  122. $facturestatic->statut = $objp->status;
  123. $facturestatic->status = $objp->status;
  124. //$alreadypaid = $facturestatic->getSommePaiement();
  125. $facturestatic->paye = $objp->paye;
  126. $facturestatic->alreadypaid = $objp->am;
  127. $thirdpartystatic->id = $objp->socid;
  128. $thirdpartystatic->name = $objp->name;
  129. $thirdpartystatic->name_alias = $objp->name_alias;
  130. $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
  131. $thirdpartystatic->code_compta_fournisseur = $objp->code_compta_fournisseur;
  132. $thirdpartystatic->fournisseur = $objp->fournisseur;
  133. $thirdpartystatic->logo = $objp->logo;
  134. $thirdpartystatic->email = $objp->email;
  135. $thirdpartystatic->entity = $objp->entity;
  136. $thirdpartystatic->tva_intra = $objp->tva_intra;
  137. $thirdpartystatic->idprof1 = !empty($objp->idprof1) ? $objp->idprof1 : '';
  138. $thirdpartystatic->idprof2 = !empty($objp->idprof2) ? $objp->idprof2 : '';
  139. $thirdpartystatic->idprof3 = !empty($objp->idprof3) ? $objp->idprof3 : '';
  140. $thirdpartystatic->idprof4 = !empty($objp->idprof4) ? $objp->idprof4 : '';
  141. $thirdpartystatic->idprof5 = !empty($objp->idprof5) ? $objp->idprof5 : '';
  142. $thirdpartystatic->idprof6 = !empty($objp->idprof6) ? $objp->idprof6 : '';
  143. $late = '';
  144. if ($facturestatic->hasDelay()) {
  145. $late = img_warning(sprintf($l_due_date, dol_print_date($datelimite, 'day', 'tzuserrel')));
  146. }
  147. $tooltip = $langs->trans('SupplierInvoice').': '.($objp->ref ? $objp->ref : $objp->facid).'<br>'.$langs->trans('RefSupplier').': '.$objp->ref_supplier;
  148. $this->info_box_contents[$line][] = array(
  149. 'td' => 'class="nowraponall"',
  150. 'text' => $facturestatic->getNomUrl(1),
  151. 'text2'=> $late,
  152. 'asis' => 1,
  153. );
  154. $this->info_box_contents[$line][] = array(
  155. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  156. 'text' => $thirdpartystatic->getNomUrl(1, '', 44),
  157. 'asis' => 1,
  158. );
  159. $this->info_box_contents[$line][] = array(
  160. 'td' => 'class="nowraponall right amount"',
  161. 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
  162. );
  163. $this->info_box_contents[$line][] = array(
  164. 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateDue").': '.dol_print_date($datelimite, 'day', 'tzuserrel')).'"',
  165. 'text' => dol_print_date($datelimite, 'day', 'tzuserrel'),
  166. );
  167. $this->info_box_contents[$line][] = array(
  168. 'td' => 'class="right" width="18"',
  169. 'text' => $facturestatic->LibStatut($objp->paye, $objp->status, 3, $objp->am, $objp->type),
  170. );
  171. $line++;
  172. }
  173. if ($this->max < $num) {
  174. $this->info_box_contents[$line][] = array('td' => 'colspan="6"', 'text' => '...');
  175. $line++;
  176. }
  177. if ($num == 0) {
  178. $this->info_box_contents[$line][0] = array(
  179. 'td' => 'class="center opacitymedium"',
  180. 'text'=>$langs->trans("NoUnpaidSupplierBills"),
  181. );
  182. }
  183. $sql = "SELECT SUM(f.total_ht) as total_ht ".$sql2;
  184. $result = $this->db->query($sql);
  185. $objp = $this->db->fetch_object($result);
  186. $totalamount = $objp->total_ht;
  187. // Add the sum à the bottom of the boxes
  188. $this->info_box_contents[$line][] = array(
  189. 'tr' => 'class="liste_total_wrap"',
  190. 'td' => 'class="liste_total"',
  191. 'text' => $langs->trans("Total"),
  192. );
  193. $this->info_box_contents[$line][] = array(
  194. 'td' => 'class="liste_total"',
  195. 'text' => "&nbsp;",
  196. );
  197. $this->info_box_contents[$line][] = array(
  198. 'td' => 'class="right liste_total" ',
  199. 'text' => price($totalamount, 0, $langs, 0, -1, -1, $conf->currency),
  200. );
  201. $this->info_box_contents[$line][] = array(
  202. 'td' => 'class="liste_total"',
  203. 'text' => "&nbsp;",
  204. );
  205. $this->info_box_contents[$line][] = array(
  206. 'td' => 'class="liste_total"',
  207. 'text' => "&nbsp;",
  208. );
  209. $this->db->free($result);
  210. } else {
  211. $this->info_box_contents[0][0] = array(
  212. 'td' => '',
  213. 'maxlength'=>500,
  214. 'text' => ($this->db->error().' sql='.$sql),
  215. );
  216. }
  217. } else {
  218. $this->info_box_contents[0][0] = array(
  219. 'td' => 'class="nohover opacitymedium left"',
  220. 'text' => $langs->trans("ReadPermissionNotAllowed")
  221. );
  222. }
  223. }
  224. /**
  225. * Method to show box
  226. *
  227. * @param array $head Array with properties of box title
  228. * @param array $contents Array with properties of box lines
  229. * @param int $nooutput No print, only return string
  230. * @return string
  231. */
  232. public function showBox($head = null, $contents = null, $nooutput = 0)
  233. {
  234. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  235. }
  236. }