invoice.lib.php 46 KB

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