invoice.lib.php 51 KB

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