invoice.lib.php 49 KB

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