invoice.lib.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. <?php
  2. /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2017 Charlie Benke <charlie@patas-monkey.com>
  7. * Copyright (C) 2017 ATM-CONSULTING <contact@atm-consulting.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. * or see https://www.gnu.org/
  22. */
  23. /**
  24. * \file htdocs/core/lib/invoice.lib.php
  25. * \brief Functions used by invoice module
  26. * \ingroup invoice
  27. */
  28. /**
  29. * Initialize the array of tabs for customer invoice
  30. *
  31. * @param Facture $object Invoice object
  32. * @return array Array of head tabs
  33. */
  34. function facture_prepare_head($object)
  35. {
  36. global $db, $langs, $conf;
  37. $h = 0;
  38. $head = array();
  39. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/card.php?facid='.$object->id;
  40. $head[$h][1] = $langs->trans('CustomerInvoice');
  41. $head[$h][2] = 'compta';
  42. $h++;
  43. if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB)) {
  44. $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
  45. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/contact.php?facid='.urlencode($object->id);
  46. $head[$h][1] = $langs->trans('ContactsAddresses');
  47. if ($nbContact > 0) {
  48. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
  49. }
  50. $head[$h][2] = 'contact';
  51. $h++;
  52. }
  53. if (!empty($conf->prelevement->enabled)) {
  54. $nbStandingOrders = 0;
  55. $sql = "SELECT COUNT(pfd.rowid) as nb";
  56. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
  57. $sql .= " WHERE pfd.fk_facture = ".((int) $object->id);
  58. $sql .= " AND pfd.ext_payment_id IS NULL";
  59. $resql = $db->query($sql);
  60. if ($resql) {
  61. $obj = $db->fetch_object($resql);
  62. if ($obj) {
  63. $nbStandingOrders = $obj->nb;
  64. }
  65. } else {
  66. dol_print_error($db);
  67. }
  68. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/prelevement.php?facid='.urlencode($object->id);
  69. $head[$h][1] = $langs->trans('StandingOrders');
  70. if ($nbStandingOrders > 0) {
  71. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbStandingOrders.'</span>';
  72. }
  73. $head[$h][2] = 'standingorders';
  74. $h++;
  75. }
  76. // Show more tabs from modules
  77. // Entries must be declared in modules descriptor with line
  78. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  79. // $this->tabs = array('entity:-tabname); to remove a tab
  80. complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice');
  81. if (empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
  82. $nbNote = 0;
  83. if (!empty($object->note_private)) {
  84. $nbNote++;
  85. }
  86. if (!empty($object->note_public)) {
  87. $nbNote++;
  88. }
  89. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/note.php?facid='.$object->id;
  90. $head[$h][1] = $langs->trans('Notes');
  91. if ($nbNote > 0) {
  92. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  93. }
  94. $head[$h][2] = 'note';
  95. $h++;
  96. }
  97. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  98. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  99. $upload_dir = $conf->facture->dir_output."/".dol_sanitizeFileName($object->ref);
  100. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  101. $nbLinks = Link::count($db, $object->element, $object->id);
  102. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/document.php?facid='.$object->id;
  103. $head[$h][1] = $langs->trans('Documents');
  104. if (($nbFiles + $nbLinks) > 0) {
  105. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  106. }
  107. $head[$h][2] = 'documents';
  108. $h++;
  109. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/info.php?facid='.$object->id;
  110. $head[$h][1] = $langs->trans('Info');
  111. $head[$h][2] = 'info';
  112. $h++;
  113. complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice', 'remove');
  114. return $head;
  115. }
  116. /**
  117. * Return array head with list of tabs to view object informations.
  118. *
  119. * @return array head array with tabs
  120. */
  121. function invoice_admin_prepare_head()
  122. {
  123. global $langs, $conf, $user;
  124. $h = 0;
  125. $head = array();
  126. $head[$h][0] = DOL_URL_ROOT.'/admin/facture.php';
  127. $head[$h][1] = $langs->trans("Miscellaneous");
  128. $head[$h][2] = 'general';
  129. $h++;
  130. $head[$h][0] = DOL_URL_ROOT.'/admin/payment.php';
  131. $head[$h][1] = $langs->trans("Payments");
  132. $head[$h][2] = 'payment';
  133. $h++;
  134. // Show more tabs from modules
  135. // Entries must be declared in modules descriptor with line
  136. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  137. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  138. complete_head_from_modules($conf, $langs, null, $head, $h, 'invoice_admin');
  139. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/admin/facture_cust_extrafields.php';
  140. $head[$h][1] = $langs->trans("ExtraFieldsCustomerInvoices");
  141. $head[$h][2] = 'attributes';
  142. $h++;
  143. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/admin/facturedet_cust_extrafields.php';
  144. $head[$h][1] = $langs->trans("ExtraFieldsLines");
  145. $head[$h][2] = 'attributeslines';
  146. $h++;
  147. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/admin/facture_rec_cust_extrafields.php';
  148. $head[$h][1] = $langs->trans("ExtraFieldsCustomerInvoicesRec");
  149. $head[$h][2] = 'attributesrec';
  150. $h++;
  151. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/admin/facturedet_rec_cust_extrafields.php';
  152. $head[$h][1] = $langs->trans("ExtraFieldsLinesRec");
  153. $head[$h][2] = 'attributeslinesrec';
  154. $h++;
  155. if ($conf->global->INVOICE_USE_SITUATION) { // Warning, implementation is seriously bugged and a new one not compatible is expected to become stable
  156. $head[$h][0] = DOL_URL_ROOT.'/admin/facture_situation.php';
  157. $head[$h][1] = $langs->trans("InvoiceSituation");
  158. $head[$h][2] = 'situation';
  159. $h++;
  160. }
  161. complete_head_from_modules($conf, $langs, null, $head, $h, 'invoice_admin', 'remove');
  162. return $head;
  163. }
  164. /**
  165. * Return array head with list of tabs to view object informations.
  166. *
  167. * @param Facture $object Invoice object
  168. * @return array head array with tabs
  169. */
  170. function invoice_rec_prepare_head($object)
  171. {
  172. global $db, $langs, $conf;
  173. $h = 0;
  174. $head = array();
  175. $head[$h][0] = DOL_URL_ROOT . '/compta/facture/card-rec.php?id=' . $object->id;
  176. $head[$h][1] = $langs->trans("RepeatableInvoice");
  177. $head[$h][2] = 'card';
  178. $h++;
  179. // Show more tabs from modules
  180. // Entries must be declared in modules descriptor with line
  181. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  182. // $this->tabs = array('entity:-tabname); to remove a tab
  183. complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice-rec');
  184. complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice-rec', 'remove');
  185. return $head;
  186. }
  187. /**
  188. * Return array head with list of tabs to view object informations.
  189. *
  190. * @param Facture $object Invoice object
  191. * @return array head array with tabs
  192. */
  193. function supplier_invoice_rec_prepare_head($object)
  194. {
  195. global $db, $langs, $conf;
  196. $h = 0;
  197. $head = array();
  198. $head[$h][0] = DOL_URL_ROOT . '/fourn/facture/card-rec.php?id=' . $object->id;
  199. $head[$h][1] = $langs->trans("RepeatableSupplierInvoice");
  200. $head[$h][2] = 'card';
  201. $h++;
  202. // Show more tabs from modules
  203. // Entries must be declared in modules descriptor with line
  204. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  205. // $this->tabs = array('entity:-tabname); to remove a tab
  206. complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice_supplier_rec');
  207. complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice_supplier_rec', 'remove');
  208. return $head;
  209. }
  210. /**
  211. * Return an HTML table that contains a pie chart of the number of customers or supplier invoices
  212. *
  213. * @param string $mode Can be 'customers' or 'suppliers'
  214. * @return string A HTML table that contains a pie chart of customers or supplier invoices
  215. */
  216. function getNumberInvoicesPieChart($mode)
  217. {
  218. global $conf, $db, $langs, $user;
  219. if (!empty($conf->facture->enabled) && !empty($user->rights->facture->lire)) {
  220. include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
  221. $now = date_create(date('Y-m-d', dol_now()));
  222. $datenowsub30 = date_create(date('Y-m-d', dol_now()));
  223. $datenowsub15 = date_create(date('Y-m-d', dol_now()));
  224. $datenowadd30 = date_create(date('Y-m-d', dol_now()));
  225. $datenowadd15 = date_create(date('Y-m-d', dol_now()));
  226. $interval30days = date_interval_create_from_date_string('30 days');
  227. $interval15days = date_interval_create_from_date_string('15 days');
  228. date_sub($datenowsub30, $interval30days);
  229. date_sub($datenowsub15, $interval15days);
  230. date_add($datenowadd30, $interval30days);
  231. date_add($datenowadd15, $interval15days);
  232. $sql = "SELECT";
  233. $sql .= " sum(".$db->ifsql("f.date_lim_reglement < '".date_format($datenowsub30, 'Y-m-d')."'", 1, 0).") as nblate30";
  234. $sql .= ", sum(".$db->ifsql("f.date_lim_reglement < '".date_format($datenowsub15, 'Y-m-d')."'", 1, 0).") as nblate15";
  235. $sql .= ", sum(".$db->ifsql("f.date_lim_reglement < '".date_format($now, 'Y-m-d')."'", 1, 0).") as nblatenow";
  236. $sql .= ", sum(".$db->ifsql("f.date_lim_reglement >= '".date_format($now, 'Y-m-d')."' OR f.date_lim_reglement IS NULL", 1, 0).") as nbnotlatenow";
  237. $sql .= ", sum(".$db->ifsql("f.date_lim_reglement > '".date_format($datenowadd15, 'Y-m-d')."'", 1, 0).") as nbnotlate15";
  238. $sql .= ", sum(".$db->ifsql("f.date_lim_reglement > '".date_format($datenowadd30, 'Y-m-d')."'", 1, 0).") as nbnotlate30";
  239. if ($mode == 'customers') {
  240. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  241. } elseif ($mode == 'fourn' || $mode == 'suppliers') {
  242. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  243. } else {
  244. return '';
  245. }
  246. $sql .= " WHERE f.type <> 2";
  247. $sql .= " AND f.fk_statut = 1";
  248. if (isset($user->socid) && $user->socid > 0) {
  249. $sql .= " AND f.fk_soc = ".((int) $user->socid);
  250. }
  251. $resql = $db->query($sql);
  252. if ($resql) {
  253. $num = $db->num_rows($resql);
  254. $i = 0;
  255. $total = 0;
  256. $dataseries = array();
  257. while ($i < $num) {
  258. $obj = $db->fetch_object($resql);
  259. /*
  260. $dataseries = array(array($langs->trans('InvoiceLate30Days'), $obj->nblate30)
  261. ,array($langs->trans('InvoiceLate15Days'), $obj->nblate15 - $obj->nblate30)
  262. ,array($langs->trans('InvoiceLateMinus15Days'), $obj->nblatenow - $obj->nblate15)
  263. ,array($langs->trans('InvoiceNotLate'), $obj->nbnotlatenow - $obj->nbnotlate15)
  264. ,array($langs->trans('InvoiceNotLate15Days'), $obj->nbnotlate15 - $obj->nbnotlate30)
  265. ,array($langs->trans('InvoiceNotLate30Days'), $obj->nbnotlate30));
  266. */
  267. $dataseries[$i]=array($langs->transnoentitiesnoconv('NbOfOpenInvoices'), $obj->nblate30, $obj->nblate15 - $obj->nblate30, $obj->nblatenow - $obj->nblate15, $obj->nbnotlatenow - $obj->nbnotlate15, $obj->nbnotlate15 - $obj->nbnotlate30, $obj->nbnotlate30);
  268. $i++;
  269. }
  270. if (!empty($dataseries[0])) {
  271. foreach ($dataseries[0] as $key => $value) {
  272. if (is_numeric($value)) {
  273. $total += $value;
  274. }
  275. }
  276. }
  277. $legend = array(
  278. $langs->trans('InvoiceLate30Days'),
  279. $langs->trans('InvoiceLate15Days'),
  280. $langs->trans('InvoiceLateMinus15Days'),
  281. $mode == 'customers' ? $langs->trans('InvoiceNotLate') : $langs->trans("InvoiceToPay"),
  282. $mode == 'customers' ? $langs->trans('InvoiceNotLate15Days') : $langs->trans("InvoiceToPay15Days"),
  283. $mode == 'customers' ? $langs->trans('InvoiceNotLate30Days') : $langs->trans("InvoiceToPay30Days"),
  284. );
  285. $colorseries = array($badgeStatus8, $badgeStatus1, $badgeStatus3, $badgeStatus4, $badgeStatus11, '-'.$badgeStatus11);
  286. $result = '<div class="div-table-responsive-no-min">';
  287. $result .= '<table class="noborder nohover centpercent">';
  288. $result .= '<tr class="liste_titre">';
  289. $result .= '<td>'.$langs->trans("NbOfOpenInvoices").' - ';
  290. if ($mode == 'customers') {
  291. $result .= $langs->trans("CustomerInvoice");
  292. } elseif ($mode == 'fourn' || $mode == 'suppliers') {
  293. $result .= $langs->trans("SupplierInvoice");
  294. } else {
  295. return '';
  296. }
  297. $result .= '</td>';
  298. $result .= '</tr>';
  299. if ($conf->use_javascript_ajax) {
  300. //var_dump($dataseries);
  301. $dolgraph = new DolGraph();
  302. $dolgraph->SetData($dataseries);
  303. $dolgraph->setLegend($legend);
  304. $dolgraph->SetDataColor(array_values($colorseries));
  305. $dolgraph->setShowLegend(2);
  306. $dolgraph->setShowPercent(1);
  307. $dolgraph->SetType(array('bars', 'bars', 'bars', 'bars', 'bars', 'bars'));
  308. //$dolgraph->SetType(array('pie'));
  309. $dolgraph->setHeight('160'); /* 160 min is required to show the 6 lines of legend */
  310. $dolgraph->setWidth('450');
  311. $dolgraph->setHideXValues(true);
  312. if ($mode == 'customers') {
  313. $dolgraph->draw('idgraphcustomerinvoices');
  314. } elseif ($mode == 'fourn' || $mode == 'suppliers') {
  315. $dolgraph->draw('idgraphfourninvoices');
  316. } else {
  317. return '';
  318. }
  319. $result .= '<tr maxwidth="255">';
  320. $result .= '<td class="center">'.$dolgraph->show($total ? 0 : $langs->trans("NoOpenInvoice")).'</td>';
  321. $result .= '</tr>';
  322. } else {
  323. // Print text lines
  324. }
  325. $result .= '</table>';
  326. $result .= '</div>';
  327. return $result;
  328. } else {
  329. dol_print_error($db);
  330. }
  331. }
  332. }
  333. /**
  334. * Return a HTML table that contains a list with customer invoice drafts
  335. *
  336. * @param int $maxCount (Optional) The maximum count of elements inside the table
  337. * @param int $socid (Optional) Show only results from the customer with this id
  338. * @return string A HTML table that contains a list with customer invoice drafts
  339. */
  340. function getCustomerInvoiceDraftTable($maxCount = 500, $socid = 0)
  341. {
  342. global $conf, $db, $langs, $user, $hookmanager;
  343. $maxofloop = (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD);
  344. $result = '';
  345. $tmpinvoice = new Facture($db);
  346. $sql = "SELECT f.rowid, f.ref, f.datef as date, f.total_ht, f.total_tva, f.total_ttc, f.ref_client";
  347. $sql .= ", f.type, f.fk_statut as status, f.paye";
  348. $sql .= ", s.nom as name";
  349. $sql .= ", s.rowid as socid, s.email";
  350. $sql .= ", s.code_client, s.code_compta, s.code_fournisseur, s.code_compta_fournisseur";
  351. $sql .= ", cc.rowid as country_id, cc.code as country_code";
  352. if (empty($user->rights->societe->client->voir) && !$socid) {
  353. $sql .= ", sc.fk_soc, sc.fk_user ";
  354. }
  355. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays";
  356. if (empty($user->rights->societe->client->voir) && !$socid) {
  357. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  358. }
  359. $sql .= " WHERE s.rowid = f.fk_soc AND f.fk_statut = ".Facture::STATUS_DRAFT;
  360. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  361. if (empty($user->rights->societe->client->voir) && !$socid) {
  362. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  363. }
  364. if ($socid) {
  365. $sql .= " AND f.fk_soc = ".((int) $socid);
  366. }
  367. // Add where from hooks
  368. $parameters = array();
  369. $reshook = $hookmanager->executeHooks('printFieldListWhereCustomerDraft', $parameters);
  370. $sql .= $hookmanager->resPrint;
  371. $sql .= " GROUP BY f.rowid, f.ref, f.datef, f.total_ht, f.total_tva, f.total_ttc, f.ref_client, f.type, f.fk_statut, f.paye,";
  372. $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_compta, s.code_fournisseur, s.code_compta_fournisseur,";
  373. $sql .= " cc.rowid, cc.code";
  374. if (empty($user->rights->societe->client->voir) && !$socid) {
  375. $sql .= ", sc.fk_soc, sc.fk_user";
  376. }
  377. // Add Group from hooks
  378. $parameters = array();
  379. $reshook = $hookmanager->executeHooks('printFieldListGroupByCustomerDraft', $parameters);
  380. $sql .= $hookmanager->resPrint;
  381. $resql = $db->query($sql);
  382. if ($resql) {
  383. $num = $db->num_rows($resql);
  384. $nbofloop = min($num, $maxofloop);
  385. $result .= '<div class="div-table-responsive-no-min">';
  386. $result .= '<table class="noborder centpercent">';
  387. $result .= '<tr class="liste_titre">';
  388. $result .= '<th colspan="3">';
  389. $result .= $langs->trans("CustomersDraftInvoices").' ';
  390. $result .= '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?search_status='.Facture::STATUS_DRAFT.'">';
  391. $result .= '<span class="badge marginleftonlyshort">'.$num.'</span>';
  392. $result .= '</a>';
  393. $result .= '</th>';
  394. $result .= '</tr>';
  395. if ($num) {
  396. $companystatic = new Societe($db);
  397. $i = 0;
  398. $othernb = 0;
  399. $tot_ttc = 0;
  400. while ($i < $nbofloop) {
  401. $obj = $db->fetch_object($resql);
  402. if ($i >= $maxCount) {
  403. $othernb += 1;
  404. $i++;
  405. $tot_ttc += $obj->total_ttc;
  406. continue;
  407. }
  408. $tmpinvoice->id = $obj->rowid;
  409. $tmpinvoice->ref = $obj->ref;
  410. $tmpinvoice->date = $db->jdate($obj->date);
  411. $tmpinvoice->type = $obj->type;
  412. $tmpinvoice->total_ht = $obj->total_ht;
  413. $tmpinvoice->total_tva = $obj->total_tva;
  414. $tmpinvoice->total_ttc = $obj->total_ttc;
  415. $tmpinvoice->ref_client = $obj->ref_client;
  416. $tmpinvoice->statut = $obj->status;
  417. $tmpinvoice->paye = $obj->paye;
  418. $companystatic->id = $obj->socid;
  419. $companystatic->name = $obj->name;
  420. $companystatic->email = $obj->email;
  421. $companystatic->country_id = $obj->country_id;
  422. $companystatic->country_code = $obj->country_code;
  423. $companystatic->client = 1;
  424. $companystatic->code_client = $obj->code_client;
  425. $companystatic->code_fournisseur = $obj->code_fournisseur;
  426. $companystatic->code_compta = $obj->code_compta;
  427. $companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
  428. $result .= '<tr class="oddeven">';
  429. $result .= '<td class="nowrap tdoverflowmax100">';
  430. $result .= $tmpinvoice->getNomUrl(1, '');
  431. $result .= '</td>';
  432. $result .= '<td class="nowrap tdoverflowmax100">';
  433. $result .= $companystatic->getNomUrl(1, 'customer');
  434. $result .= '</td>';
  435. $result .= '<td class="nowrap right"><span class="amount">'.price($obj->total_ttc).'</span></td>';
  436. $result .= '</tr>';
  437. $tot_ttc += $obj->total_ttc;
  438. $i++;
  439. }
  440. if ($othernb) {
  441. $result .= '<tr class="oddeven">';
  442. $result .= '<td class="nowrap" colspan="3">';
  443. $result .= '<span class="opacitymedium">'.$langs->trans("More").'...'.($othernb < $maxofloop ? ' ('.$othernb.')' : '').'</span>';
  444. $result .= '</td>';
  445. $result .= "</tr>\n";
  446. }
  447. $result .= '<tr class="liste_total"><td class="left">'.$langs->trans("Total").'</td>';
  448. $result .= '<td colspan="2" class="right">'.price($tot_ttc).'</td>';
  449. $result .= '</tr>';
  450. } else {
  451. $result .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("NoInvoice").'</td></tr>';
  452. }
  453. $result .= "</table></div>";
  454. $db->free($resql);
  455. } else {
  456. dol_print_error($db);
  457. }
  458. return $result;
  459. }
  460. /**
  461. * Return a HTML table that contains a list with customer invoice drafts
  462. *
  463. * @param int $maxCount (Optional) The maximum count of elements inside the table
  464. * @param int $socid (Optional) Show only results from the customer with this id
  465. * @return string A HTML table that contains a list with customer invoice drafts
  466. */
  467. function getDraftSupplierTable($maxCount = 500, $socid = 0)
  468. {
  469. global $conf, $db, $langs, $user, $hookmanager;
  470. $maxofloop = (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD);
  471. $result = '';
  472. $facturesupplierstatic = new FactureFournisseur($db);
  473. $sql = "SELECT f.ref, f.rowid, f.total_ht, f.total_tva, f.total_ttc, f.type, f.ref_supplier, f.fk_statut as status, f.paye";
  474. $sql .= ", s.nom as name";
  475. $sql .= ", s.rowid as socid, s.email";
  476. $sql .= ", s.code_client, s.code_compta";
  477. $sql .= ", s.code_fournisseur, s.code_compta_fournisseur";
  478. $sql .= ", cc.rowid as country_id, cc.code as country_code";
  479. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f, ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays";
  480. if (empty($user->rights->societe->client->voir) && !$socid) {
  481. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  482. }
  483. $sql .= " WHERE s.rowid = f.fk_soc AND f.fk_statut = ".FactureFournisseur::STATUS_DRAFT;
  484. $sql .= " AND f.entity IN (".getEntity('invoice').')';
  485. if (empty($user->rights->societe->client->voir) && !$socid) {
  486. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  487. }
  488. if ($socid) {
  489. $sql .= " AND f.fk_soc = ".((int) $socid);
  490. }
  491. // Add where from hooks
  492. $parameters = array();
  493. $reshook = $hookmanager->executeHooks('printFieldListWhereSupplierDraft', $parameters);
  494. $sql .= $hookmanager->resPrint;
  495. $resql = $db->query($sql);
  496. if ($resql) {
  497. $num = $db->num_rows($resql);
  498. $nbofloop = min($num, $maxofloop);
  499. $result .= '<div class="div-table-responsive-no-min">';
  500. $result .= '<table class="noborder centpercent">';
  501. $result .= '<tr class="liste_titre">';
  502. $result .= '<th colspan="3">';
  503. $result .= $langs->trans("SuppliersDraftInvoices").' ';
  504. $result .= '<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php?search_status='.FactureFournisseur::STATUS_DRAFT.'">';
  505. $result .= '<span class="badge marginleftonlyshort">'.$num.'</span>';
  506. $result .= '</a>';
  507. $result .= '</th>';
  508. $result .= '</tr>';
  509. if ($num) {
  510. $companystatic = new Societe($db);
  511. $i = 0;
  512. $othernb = 0;
  513. $tot_ttc = 0;
  514. while ($i < $nbofloop) {
  515. $obj = $db->fetch_object($resql);
  516. if ($i >= $maxCount) {
  517. $othernb += 1;
  518. $i++;
  519. $tot_ttc += $obj->total_ttc;
  520. continue;
  521. }
  522. $facturesupplierstatic->ref = $obj->ref;
  523. $facturesupplierstatic->id = $obj->rowid;
  524. $facturesupplierstatic->total_ht = $obj->total_ht;
  525. $facturesupplierstatic->total_tva = $obj->total_tva;
  526. $facturesupplierstatic->total_ttc = $obj->total_ttc;
  527. $facturesupplierstatic->ref_supplier = $obj->ref_supplier;
  528. $facturesupplierstatic->type = $obj->type;
  529. $facturesupplierstatic->statut = $obj->status;
  530. $facturesupplierstatic->paye = $obj->paye;
  531. $companystatic->id = $obj->socid;
  532. $companystatic->name = $obj->name;
  533. $companystatic->email = $obj->email;
  534. $companystatic->country_id = $obj->country_id;
  535. $companystatic->country_code = $obj->country_code;
  536. $companystatic->fournisseur = 1;
  537. $companystatic->code_client = $obj->code_client;
  538. $companystatic->code_fournisseur = $obj->code_fournisseur;
  539. $companystatic->code_compta = $obj->code_compta;
  540. $companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
  541. $result .= '<tr class="oddeven">';
  542. $result .= '<td class="nowrap tdoverflowmax100">';
  543. $result .= $facturesupplierstatic->getNomUrl(1, '');
  544. $result .= '</td>';
  545. $result .= '<td class="nowrap tdoverflowmax100">';
  546. $result .= $companystatic->getNomUrl(1, 'supplier');
  547. $result .= '</td>';
  548. $result .= '<td class="right"><span class="amount">'.price($obj->total_ttc).'</span></td>';
  549. $result .= '</tr>';
  550. $tot_ttc += $obj->total_ttc;
  551. $i++;
  552. }
  553. if ($othernb) {
  554. $result .= '<tr class="oddeven">';
  555. $result .= '<td class="nowrap" colspan="3">';
  556. $result .= '<span class="opacitymedium">'.$langs->trans("More").'...'.($othernb < $maxofloop ? ' ('.$othernb.')' : '').'</span>';
  557. $result .= '</td>';
  558. $result .= "</tr>\n";
  559. }
  560. $result .= '<tr class="liste_total"><td class="left">'.$langs->trans("Total").'</td>';
  561. $result .= '<td colspan="2" class="right">'.price($tot_ttc).'</td>';
  562. $result .= '</tr>';
  563. } else {
  564. $result .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("NoInvoice").'</td></tr>';
  565. }
  566. $result .= "</table></div>";
  567. $db->free($resql);
  568. } else {
  569. dol_print_error($db);
  570. }
  571. return $result;
  572. }
  573. /**
  574. * Return a HTML table that contains a list with latest edited customer invoices
  575. *
  576. * @param int $maxCount (Optional) The maximum count of elements inside the table
  577. * @param int $socid (Optional) Show only results from the customer with this id
  578. * @return string A HTML table that contains a list with latest edited customer invoices
  579. */
  580. function getCustomerInvoiceLatestEditTable($maxCount = 5, $socid = 0)
  581. {
  582. global $conf, $db, $langs, $user;
  583. $sql = "SELECT f.rowid, f.entity, f.ref, f.fk_statut as status, f.paye, f.type, f.total_ht, f.total_tva, f.total_ttc, f.datec,";
  584. $sql .= " s.nom as socname, s.rowid as socid, s.canvas, s.client";
  585. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  586. $sql .= ", ".MAIN_DB_PREFIX."societe as s";
  587. if (empty($user->rights->societe->client->voir) && !$socid) {
  588. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  589. }
  590. $sql .= " WHERE f.fk_soc = s.rowid";
  591. $sql .= " AND f.entity IN (".getEntity('facture').")";
  592. if ($socid) {
  593. $sql .= " AND f.fk_soc = ".((int) $socid);
  594. }
  595. if (empty($user->rights->societe->client->voir) && !$socid) {
  596. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  597. }
  598. $sql .= " ORDER BY f.tms DESC";
  599. $sql .= $db->plimit($maxCount, 0);
  600. $resql = $db->query($sql);
  601. if (!$resql) {
  602. dol_print_error($db);
  603. }
  604. $num = $db->num_rows($resql);
  605. $result = '<div class="div-table-responsive-no-min">';
  606. $result .= '<table class="noborder centpercent">';
  607. $result .= '<tr class="liste_titre">';
  608. $result .= '<th colspan="3">'.$langs->trans("LastCustomersBills", $maxCount).'</th>';
  609. $result .= '<th class="right">'.$langs->trans("AmountTTC").'</th>';
  610. $result .= '<th class="right"></th>';
  611. $result .= '</tr>';
  612. if ($num < 1) {
  613. $result .= '</table>';
  614. $result .= '</div>';
  615. return $result;
  616. }
  617. $formfile = new FormFile($db);
  618. $objectstatic = new Facture($db);
  619. $companystatic = new Societe($db);
  620. $i = 0;
  621. while ($i < $num) {
  622. $obj = $db->fetch_object($resql);
  623. $objectstatic->id = $obj->rowid;
  624. $objectstatic->ref = $obj->ref;
  625. $objectstatic->paye = $obj->paye;
  626. $objectstatic->statut = $obj->status;
  627. $objectstatic->total_ht = $obj->total_ht;
  628. $objectstatic->total_tva = $obj->total_tva;
  629. $objectstatic->total_ttc = $obj->total_ttc;
  630. $objectstatic->type = $obj->type;
  631. $companystatic->id = $obj->socid;
  632. $companystatic->name = $obj->socname;
  633. $companystatic->client = $obj->client;
  634. $companystatic->canvas = $obj->canvas;
  635. $filename = dol_sanitizeFileName($obj->ref);
  636. $filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename;
  637. $result .= '<tr class="nowrap">';
  638. $result .= '<td class="oddeven">';
  639. $result .= '<table class="nobordernopadding">';
  640. $result .= '<tr class="nocellnopadd">';
  641. $result .= '<td width="96" class="nobordernopadding nowrap">'.$objectstatic->getNomUrl(1).'</td>';
  642. $result .= '<td width="16" class="nobordernopadding nowrap">&nbsp;</td>';
  643. $result .= '<td width="16" class="nobordernopadding right">'.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'</td>';
  644. $result .= '</tr>';
  645. $result .= '</table>';
  646. $result .= '</td>';
  647. $result .= '<td class="tdoverflowmax150">'.$companystatic->getNomUrl(1, 'customer').'</td>';
  648. $result .= '<td>'.dol_print_date($db->jdate($obj->datec), 'day').'</td>';
  649. $result .= '<td class="right amount">'.price($obj->total_ttc).'</td>';
  650. $result .= '<td class="right">'.$objectstatic->getLibStatut(5).'</td>';
  651. $result .= '</tr>';
  652. $i++;
  653. }
  654. $result .= '</table>';
  655. $result .= '</div>';
  656. return $result;
  657. }
  658. /**
  659. * Return a HTML table that contains a list with latest edited supplier invoices
  660. *
  661. * @param int $maxCount (Optional) The maximum count of elements inside the table
  662. * @param int $socid (Optional) Show only results from the supplier with this id
  663. * @return string A HTML table that contains a list with latest edited supplier invoices
  664. */
  665. function getPurchaseInvoiceLatestEditTable($maxCount = 5, $socid = 0)
  666. {
  667. global $conf, $db, $langs, $user;
  668. $sql = "SELECT f.rowid, f.entity, f.ref, f.fk_statut as status, f.paye, f.total_ht, f.total_tva, f.total_ttc, f.type, f.ref_supplier, f.datec,";
  669. $sql .= " s.nom as socname, s.rowid as socid, s.canvas, s.client";
  670. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  671. $sql .= ", ".MAIN_DB_PREFIX."societe as s";
  672. if (empty($user->rights->societe->client->voir) && !$socid) {
  673. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  674. }
  675. $sql .= " WHERE f.fk_soc = s.rowid";
  676. $sql .= " AND f.entity IN (".getEntity('facture_fourn').")";
  677. if ($socid) {
  678. $sql .= " AND f.fk_soc = ".((int) $socid);
  679. }
  680. if (empty($user->rights->societe->client->voir) && !$socid) {
  681. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  682. }
  683. $sql .= " ORDER BY f.tms DESC";
  684. $sql .= $db->plimit($maxCount, 0);
  685. $resql = $db->query($sql);
  686. if (!$resql) {
  687. dol_print_error($db);
  688. return '';
  689. }
  690. $num = $db->num_rows($resql);
  691. $result = '<div class="div-table-responsive-no-min">';
  692. $result .= '<table class="noborder centpercent">';
  693. $result .= '<tr class="liste_titre">';
  694. $result .= '<th colspan="3">'.$langs->trans("BoxTitleLastSupplierBills", $maxCount).'</th>';
  695. $result .= '<th class="right">'.$langs->trans("AmountTTC").'</th>';
  696. $result .= '<th class="right"></th>';
  697. $result .= '</tr>';
  698. if ($num < 1) {
  699. $result .= '</table>';
  700. $result .= '</div>';
  701. return $result;
  702. }
  703. $objectstatic = new FactureFournisseur($db);
  704. $companystatic = new Societe($db);
  705. $formfile = new FormFile($db);
  706. $i = 0;
  707. while ($i < $num) {
  708. $obj = $db->fetch_object($resql);
  709. $objectstatic->id = $obj->rowid;
  710. $objectstatic->ref = $obj->ref;
  711. $objectstatic->paye = $obj->paye;
  712. $objectstatic->statut = $obj->status;
  713. $objectstatic->total_ht = $obj->total_ht;
  714. $objectstatic->total_tva = $obj->total_tva;
  715. $objectstatic->total_ttc = $obj->total_ttc;
  716. $objectstatic->type = $obj->type;
  717. $companystatic->id = $obj->socid;
  718. $companystatic->name = $obj->socname;
  719. $companystatic->client = $obj->client;
  720. $companystatic->canvas = $obj->canvas;
  721. $filename = dol_sanitizeFileName($obj->ref);
  722. $filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename;
  723. $result .= '<tr class="nowrap">';
  724. $result .= '<td class="oddeven">';
  725. $result .= '<table class="nobordernopadding">';
  726. $result .= '<tr class="nocellnopadd">';
  727. $result .= '<td width="96" class="nobordernopadding nowrap">'.$objectstatic->getNomUrl(1).'</td>';
  728. $result .= '<td width="16" class="nobordernopadding nowrap">&nbsp;</td>';
  729. $result .= '<td width="16" class="nobordernopadding right">'.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'</td>';
  730. $result .= '</tr>';
  731. $result .= '</table>';
  732. $result .= '</td>';
  733. $result .= '<td class="tdoverflowmax150">'.$companystatic->getNomUrl(1, 'supplier').'</td>';
  734. $result .= '<td>'.dol_print_date($db->jdate($obj->datec), 'day').'</td>';
  735. $result .= '<td class="amount right">'.price($obj->total_ttc).'</td>';
  736. $result .= '<td class="right">'.$objectstatic->getLibStatut(5).'</td>';
  737. $result .= '</tr>';
  738. $i++;
  739. }
  740. $result .= '</table>';
  741. $result .= '</div>';
  742. return $result;
  743. }
  744. /**
  745. * Return a HTML table that contains of unpaid customers invoices
  746. *
  747. * @param int $maxCount (Optional) The maximum count of elements inside the table
  748. * @param int $socid (Optional) Show only results from the supplier with this id
  749. * @return string A HTML table that conatins a list with open (unpaid) supplier invoices
  750. */
  751. function getCustomerInvoiceUnpaidOpenTable($maxCount = 500, $socid = 0)
  752. {
  753. global $conf, $db, $langs, $user, $hookmanager;
  754. $result = '';
  755. if (!empty($conf->facture->enabled) && !empty($user->rights->facture->lire)) {
  756. $tmpinvoice = new Facture($db);
  757. $sql = "SELECT f.rowid, f.ref, f.fk_statut as status, f.datef, f.type, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.tms";
  758. $sql .= ", f.date_lim_reglement as datelimite";
  759. $sql .= ", s.nom as name";
  760. $sql .= ", s.rowid as socid, s.email";
  761. $sql .= ", s.code_client, s.code_compta";
  762. $sql .= ", s.code_fournisseur, s.code_compta_fournisseur";
  763. $sql .= ", cc.rowid as country_id, cc.code as country_code";
  764. $sql .= ", sum(pf.amount) as am";
  765. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays,".MAIN_DB_PREFIX."facture as f";
  766. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf on f.rowid=pf.fk_facture";
  767. if (empty($user->rights->societe->client->voir) && !$socid) {
  768. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  769. }
  770. $sql .= " WHERE s.rowid = f.fk_soc AND f.paye = 0 AND f.fk_statut = ".Facture::STATUS_VALIDATED;
  771. $sql .= " AND f.entity IN (".getEntity('invoice').')';
  772. if (empty($user->rights->societe->client->voir) && !$socid) {
  773. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  774. }
  775. if ($socid) {
  776. $sql .= " AND f.fk_soc = ".((int) $socid);
  777. }
  778. // Add where from hooks
  779. $parameters = array();
  780. $reshook = $hookmanager->executeHooks('printFieldListWhereCustomerUnpaid', $parameters);
  781. $sql .= $hookmanager->resPrint;
  782. $sql .= " GROUP BY f.rowid, f.ref, f.fk_statut, f.datef, f.type, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.tms, f.date_lim_reglement,";
  783. $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_compta, cc.rowid, cc.code";
  784. $sql .= ", s.code_fournisseur, s.code_compta_fournisseur";
  785. $sql .= " ORDER BY f.datef ASC, f.ref ASC";
  786. $resql = $db->query($sql);
  787. if ($resql) {
  788. $num = $db->num_rows($resql);
  789. $i = 0;
  790. $othernb = 0;
  791. $formfile = new FormFile($db);
  792. print '<div class="div-table-responsive-no-min">';
  793. print '<table class="noborder centpercent">';
  794. print '<tr class="liste_titre">';
  795. print '<th colspan="2">';
  796. print $langs->trans("BillsCustomersUnpaid", $num).' ';
  797. print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?search_status='.Facture::STATUS_VALIDATED.'">';
  798. print '<span class="badge">'.$num.'</span>';
  799. print '</a>';
  800. print '</th>';
  801. print '<th class="right">'.$langs->trans("DateDue").'</th>';
  802. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  803. print '<th class="right">'.$langs->trans("AmountHT").'</th>';
  804. }
  805. print '<th class="right">'.$langs->trans("AmountTTC").'</th>';
  806. print '<th class="right">'.$langs->trans("Received").'</th>';
  807. print '<th width="16">&nbsp;</th>';
  808. print '</tr>';
  809. if ($num) {
  810. $societestatic = new Societe($db);
  811. $total_ttc = $totalam = $total = 0;
  812. while ($i < $num) {
  813. $obj = $db->fetch_object($resql);
  814. if ($i >= $maxCount) {
  815. $othernb += 1;
  816. $i++;
  817. $total += $obj->total_ht;
  818. $total_ttc += $obj->total_ttc;
  819. continue;
  820. }
  821. $tmpinvoice->ref = $obj->ref;
  822. $tmpinvoice->id = $obj->rowid;
  823. $tmpinvoice->total_ht = $obj->total_ht;
  824. $tmpinvoice->total_tva = $obj->total_tva;
  825. $tmpinvoice->total_ttc = $obj->total_ttc;
  826. $tmpinvoice->type = $obj->type;
  827. $tmpinvoice->statut = $obj->status;
  828. $tmpinvoice->paye = $obj->paye;
  829. $tmpinvoice->date_lim_reglement = $db->jdate($obj->datelimite);
  830. $societestatic->id = $obj->socid;
  831. $societestatic->name = $obj->name;
  832. $societestatic->email = $obj->email;
  833. $societestatic->country_id = $obj->country_id;
  834. $societestatic->country_code = $obj->country_code;
  835. $societestatic->client = 1;
  836. $societestatic->code_client = $obj->code_client;
  837. $societestatic->code_fournisseur = $obj->code_fournisseur;
  838. $societestatic->code_compta = $obj->code_compta;
  839. $societestatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
  840. print '<tr class="oddeven">';
  841. print '<td class="nowrap">';
  842. print '<table class="nobordernopadding"><tr class="nocellnopadd">';
  843. print '<td class="nobordernopadding nowrap">';
  844. print $tmpinvoice->getNomUrl(1, '');
  845. print '</td>';
  846. print '<td width="16" class="nobordernopadding hideonsmartphone right">';
  847. $filename = dol_sanitizeFileName($obj->ref);
  848. $filedir = $conf->facture->dir_output.'/'.dol_sanitizeFileName($obj->ref);
  849. $urlsource = $_SERVER['PHP_SELF'].'?facid='.$obj->rowid;
  850. print $formfile->getDocumentsLink($tmpinvoice->element, $filename, $filedir);
  851. print '</td></tr></table>';
  852. print '</td>';
  853. print '<td class="nowrap tdoverflowmax100">';
  854. print $societestatic->getNomUrl(1, 'customer');
  855. print '</td>';
  856. print '<td class="right">';
  857. print dol_print_date($db->jdate($obj->datelimite), 'day');
  858. if ($tmpinvoice->hasDelay()) {
  859. print img_warning($langs->trans("Late"));
  860. }
  861. print '</td>';
  862. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  863. print '<td class="right"><span class="amount">'.price($obj->total_ht).'</span></td>';
  864. }
  865. print '<td class="nowrap right"><span class="amount">'.price($obj->total_ttc).'</span></td>';
  866. print '<td class="nowrap right"><span class="amount">'.price($obj->am).'</span></td>';
  867. print '<td>'.$tmpinvoice->getLibStatut(3, $obj->am).'</td>';
  868. print '</tr>';
  869. $total_ttc += $obj->total_ttc;
  870. $total += $obj->total_ht;
  871. $totalam += $obj->am;
  872. $i++;
  873. }
  874. if ($othernb) {
  875. $colspan = 6;
  876. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  877. $colspan++;
  878. }
  879. print '<tr class="oddeven">';
  880. print '<td class="nowrap" colspan="'.$colspan.'">';
  881. print '<span class="opacitymedium">'.$langs->trans("More").'... ('.$othernb.')</span>';
  882. print '</td>';
  883. print "</tr>\n";
  884. }
  885. print '<tr class="liste_total"><td colspan="2">'.$langs->trans("Total").' &nbsp; <span style="font-weight: normal">('.$langs->trans("RemainderToTake").': '.price($total_ttc - $totalam).')</span> </td>';
  886. print '<td>&nbsp;</td>';
  887. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  888. print '<td class="right"><span class="amount">'.price($total).'</span></td>';
  889. }
  890. print '<td class="nowrap right"><span class="amount">'.price($total_ttc).'</span></td>';
  891. print '<td class="nowrap right"><span class="amount">'.price($totalam).'</span></td>';
  892. print '<td>&nbsp;</td>';
  893. print '</tr>';
  894. } else {
  895. $colspan = 6;
  896. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  897. $colspan++;
  898. }
  899. print '<tr class="oddeven"><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoInvoice").'</td></tr>';
  900. }
  901. print '</table></div><br>';
  902. $db->free($resql);
  903. } else {
  904. dol_print_error($db);
  905. }
  906. }
  907. return $result;
  908. }
  909. /**
  910. * Return a HTML table that contains of unpaid purchase invoices
  911. *
  912. * @param int $maxCount (Optional) The maximum count of elements inside the table
  913. * @param int $socid (Optional) Show only results from the supplier with this id
  914. * @return string A HTML table that conatins a list with open (unpaid) supplier invoices
  915. */
  916. function getPurchaseInvoiceUnpaidOpenTable($maxCount = 500, $socid = 0)
  917. {
  918. global $conf, $db, $langs, $user, $hookmanager;
  919. $result = '';
  920. if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire) || (!empty($conf->supplier_invoice->enabled) && $user->rights->supplier_invoice->lire)) {
  921. $facstatic = new FactureFournisseur($db);
  922. $sql = "SELECT ff.rowid, ff.ref, ff.fk_statut as status, ff.type, ff.libelle as label, ff.total_ht, ff.total_tva, ff.total_ttc, ff.paye";
  923. $sql .= ", ff.date_lim_reglement";
  924. $sql .= ", s.nom as name";
  925. $sql .= ", s.rowid as socid, s.email";
  926. $sql .= ", s.code_client, s.code_compta";
  927. $sql .= ", s.code_fournisseur, s.code_compta_fournisseur";
  928. $sql .= ", sum(pf.amount) as am";
  929. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture_fourn as ff";
  930. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf on ff.rowid=pf.fk_facturefourn";
  931. if (empty($user->rights->societe->client->voir) && !$socid) {
  932. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  933. }
  934. $sql .= " WHERE s.rowid = ff.fk_soc";
  935. $sql .= " AND ff.entity = ".$conf->entity;
  936. $sql .= " AND ff.paye = 0";
  937. $sql .= " AND ff.fk_statut = ".FactureFournisseur::STATUS_VALIDATED;
  938. if (empty($user->rights->societe->client->voir) && !$socid) {
  939. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  940. }
  941. if ($socid) {
  942. $sql .= " AND ff.fk_soc = ".((int) $socid);
  943. }
  944. // Add where from hooks
  945. $parameters = array();
  946. $reshook = $hookmanager->executeHooks('printFieldListWhereSupplierUnpaid', $parameters);
  947. $sql .= $hookmanager->resPrint;
  948. $sql .= " GROUP BY ff.rowid, ff.ref, ff.fk_statut, ff.type, ff.libelle, ff.total_ht, ff.total_tva, ff.total_ttc, ff.paye, ff.date_lim_reglement,";
  949. $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur";
  950. $sql .= " ORDER BY ff.date_lim_reglement ASC";
  951. $resql = $db->query($sql);
  952. if ($resql) {
  953. $num = $db->num_rows($resql);
  954. $othernb = 0;
  955. $formfile = new FormFile($db);
  956. print '<div class="div-table-responsive-no-min">';
  957. print '<table class="noborder centpercent">';
  958. print '<tr class="liste_titre">';
  959. print '<th colspan="2">';
  960. print $langs->trans("BillsSuppliersUnpaid", $num).' ';
  961. print '<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php?search_status='.FactureFournisseur::STATUS_VALIDATED.'">';
  962. print '<span class="badge">'.$num.'</span>';
  963. print '</a>';
  964. print '</th>';
  965. print '<th class="right">'.$langs->trans("DateDue").'</th>';
  966. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  967. print '<th class="right">'.$langs->trans("AmountHT").'</th>';
  968. }
  969. print '<th class="right">'.$langs->trans("AmountTTC").'</th>';
  970. print '<th class="right">'.$langs->trans("Paid").'</th>';
  971. print '<th width="16">&nbsp;</th>';
  972. print "</tr>\n";
  973. $societestatic = new Societe($db);
  974. if ($num) {
  975. $i = 0;
  976. $total = $total_ttc = $totalam = 0;
  977. while ($i < $num) {
  978. $obj = $db->fetch_object($resql);
  979. if ($i >= $maxCount) {
  980. $othernb += 1;
  981. $i++;
  982. $total += $obj->total_ht;
  983. $total_ttc += $obj->total_ttc;
  984. continue;
  985. }
  986. $facstatic->ref = $obj->ref;
  987. $facstatic->id = $obj->rowid;
  988. $facstatic->type = $obj->type;
  989. $facstatic->total_ht = $obj->total_ht;
  990. $facstatic->total_tva = $obj->total_tva;
  991. $facstatic->total_ttc = $obj->total_ttc;
  992. $facstatic->statut = $obj->status;
  993. $facstatic->paye = $obj->paye;
  994. $societestatic->id = $obj->socid;
  995. $societestatic->name = $obj->name;
  996. $societestatic->email = $obj->email;
  997. $societestatic->client = 0;
  998. $societestatic->fournisseur = 1;
  999. $societestatic->code_client = $obj->code_client;
  1000. $societestatic->code_fournisseur = $obj->code_fournisseur;
  1001. $societestatic->code_compta = $obj->code_compta;
  1002. $societestatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
  1003. print '<tr class="oddeven">';
  1004. print '<td class="nowrap tdoverflowmax100">';
  1005. print $facstatic->getNomUrl(1, '');
  1006. print '</td>';
  1007. print '<td class="nowrap tdoverflowmax100">'.$societestatic->getNomUrl(1, 'supplier').'</td>';
  1008. print '<td class="right">'.dol_print_date($db->jdate($obj->date_lim_reglement), 'day').'</td>';
  1009. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  1010. print '<td class="right"><span class="amount">'.price($obj->total_ht).'</span></td>';
  1011. }
  1012. print '<td class="nowrap right"><span class="amount">'.price($obj->total_ttc).'</span></td>';
  1013. print '<td class="nowrap right"><span class="amount">'.price($obj->am).'</span></td>';
  1014. print '<td>'.$facstatic->getLibStatut(3, $obj->am).'</td>';
  1015. print '</tr>';
  1016. $total += $obj->total_ht;
  1017. $total_ttc += $obj->total_ttc;
  1018. $totalam += $obj->am;
  1019. $i++;
  1020. }
  1021. if ($othernb) {
  1022. $colspan = 6;
  1023. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  1024. $colspan++;
  1025. }
  1026. print '<tr class="oddeven">';
  1027. print '<td class="nowrap" colspan="'.$colspan.'">';
  1028. print '<span class="opacitymedium">'.$langs->trans("More").'... ('.$othernb.')</span>';
  1029. print '</td>';
  1030. print "</tr>\n";
  1031. }
  1032. print '<tr class="liste_total"><td colspan="2">'.$langs->trans("Total").' &nbsp; <span style="font-weight: normal">('.$langs->trans("RemainderToPay").': '.price($total_ttc - $totalam).')</span> </td>';
  1033. print '<td>&nbsp;</td>';
  1034. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  1035. print '<td class="right">'.price($total).'</td>';
  1036. }
  1037. print '<td class="nowrap right">'.price($total_ttc).'</td>';
  1038. print '<td class="nowrap right">'.price($totalam).'</td>';
  1039. print '<td>&nbsp;</td>';
  1040. print '</tr>';
  1041. } else {
  1042. $colspan = 6;
  1043. if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
  1044. $colspan++;
  1045. }
  1046. print '<tr class="oddeven"><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoInvoice").'</td></tr>';
  1047. }
  1048. print '</table></div><br>';
  1049. } else {
  1050. dol_print_error($db);
  1051. }
  1052. }
  1053. return $result;
  1054. }