order.lib.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. * or see https://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/lib/order.lib.php
  23. * \brief Ensemble de fonctions de base pour le module commande
  24. * \ingroup commande
  25. */
  26. /**
  27. * Prepare array with list of tabs
  28. *
  29. * @param Commande $object Object related to tabs
  30. * @return array Array of tabs to show
  31. */
  32. function commande_prepare_head(Commande $object)
  33. {
  34. global $db, $langs, $conf, $user;
  35. if (!empty($conf->expedition->enabled)) {
  36. $langs->load("sendings");
  37. }
  38. $langs->load("orders");
  39. $h = 0;
  40. $head = array();
  41. if (!empty($conf->commande->enabled) && $user->rights->commande->lire) {
  42. $head[$h][0] = DOL_URL_ROOT.'/commande/card.php?id='.$object->id;
  43. $head[$h][1] = $langs->trans("CustomerOrder");
  44. $head[$h][2] = 'order';
  45. $h++;
  46. }
  47. if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB)) {
  48. $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
  49. $head[$h][0] = DOL_URL_ROOT.'/commande/contact.php?id='.$object->id;
  50. $head[$h][1] = $langs->trans('ContactsAddresses');
  51. if ($nbContact > 0) {
  52. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
  53. }
  54. $head[$h][2] = 'contact';
  55. $h++;
  56. }
  57. if (($conf->expedition_bon->enabled && $user->rights->expedition->lire)
  58. || ($conf->delivery_note->enabled && $user->rights->expedition->delivery->lire)) {
  59. $nbShipments = $object->getNbOfShipments();
  60. $nbReceiption = 0;
  61. $head[$h][0] = DOL_URL_ROOT.'/expedition/shipment.php?id='.$object->id;
  62. $text = '';
  63. if ($conf->expedition_bon->enabled) {
  64. $text .= $langs->trans("Shipments");
  65. }
  66. if ($conf->expedition_bon->enabled && $conf->delivery_note->enabled) {
  67. $text .= ' - ';
  68. }
  69. if ($conf->delivery_note->enabled) {
  70. $text .= $langs->trans("Receivings");
  71. }
  72. if ($nbShipments > 0 || $nbReceiption > 0) {
  73. $text .= '<span class="badge marginleftonlyshort">'.($nbShipments ? $nbShipments : 0);
  74. }
  75. if ($conf->expedition_bon->enabled && $conf->delivery_note->enabled && ($nbShipments > 0 || $nbReceiption > 0)) {
  76. $text .= ' - ';
  77. }
  78. if ($conf->expedition_bon->enabled && $conf->delivery_note->enabled && ($nbShipments > 0 || $nbReceiption > 0)) {
  79. $text .= ($nbReceiption ? $nbReceiption : 0);
  80. }
  81. if ($nbShipments > 0 || $nbReceiption > 0) {
  82. $text .= '</span>';
  83. }
  84. $head[$h][1] = $text;
  85. $head[$h][2] = 'shipping';
  86. $h++;
  87. }
  88. // Show more tabs from modules
  89. // Entries must be declared in modules descriptor with line
  90. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  91. // $this->tabs = array('entity:-tabname); to remove a tab
  92. complete_head_from_modules($conf, $langs, $object, $head, $h, 'order');
  93. if (empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
  94. $nbNote = 0;
  95. if (!empty($object->note_private)) {
  96. $nbNote++;
  97. }
  98. if (!empty($object->note_public)) {
  99. $nbNote++;
  100. }
  101. $head[$h][0] = DOL_URL_ROOT.'/commande/note.php?id='.$object->id;
  102. $head[$h][1] = $langs->trans('Notes');
  103. if ($nbNote > 0) {
  104. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  105. }
  106. $head[$h][2] = 'note';
  107. $h++;
  108. }
  109. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  110. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  111. $upload_dir = $conf->commande->multidir_output[$object->entity]."/".dol_sanitizeFileName($object->ref);
  112. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  113. $nbLinks = Link::count($db, $object->element, $object->id);
  114. $head[$h][0] = DOL_URL_ROOT.'/commande/document.php?id='.$object->id;
  115. $head[$h][1] = $langs->trans('Documents');
  116. if (($nbFiles + $nbLinks) > 0) {
  117. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  118. }
  119. $head[$h][2] = 'documents';
  120. $h++;
  121. $head[$h][0] = DOL_URL_ROOT.'/commande/info.php?id='.$object->id;
  122. $head[$h][1] = $langs->trans("Info");
  123. $head[$h][2] = 'info';
  124. $h++;
  125. complete_head_from_modules($conf, $langs, $object, $head, $h, 'order', 'remove');
  126. return $head;
  127. }
  128. /**
  129. * Return array head with list of tabs to view object informations.
  130. *
  131. * @return array head array with tabs
  132. */
  133. function order_admin_prepare_head()
  134. {
  135. global $langs, $conf, $user;
  136. $h = 0;
  137. $head = array();
  138. $head[$h][0] = DOL_URL_ROOT.'/admin/commande.php';
  139. $head[$h][1] = $langs->trans("Miscellaneous");
  140. $head[$h][2] = 'general';
  141. $h++;
  142. complete_head_from_modules($conf, $langs, null, $head, $h, 'order_admin');
  143. $head[$h][0] = DOL_URL_ROOT.'/admin/order_extrafields.php';
  144. $head[$h][1] = $langs->trans("ExtraFields");
  145. $head[$h][2] = 'attributes';
  146. $h++;
  147. $head[$h][0] = DOL_URL_ROOT.'/admin/orderdet_extrafields.php';
  148. $head[$h][1] = $langs->trans("ExtraFieldsLines");
  149. $head[$h][2] = 'attributeslines';
  150. $h++;
  151. complete_head_from_modules($conf, $langs, null, $head, $h, 'order_admin', 'remove');
  152. return $head;
  153. }
  154. /**
  155. * Return a HTML table that contains a pie chart of customer orders
  156. *
  157. * @param int $socid (Optional) Show only results from the customer with this id
  158. * @return string A HTML table that contains a pie chart of customer invoices
  159. */
  160. function getCustomerOrderPieChart($socid = 0)
  161. {
  162. global $conf, $db, $langs, $user;
  163. $result = '';
  164. if (empty($conf->commande->enabled) || empty($user->rights->commande->lire)) {
  165. return '';
  166. }
  167. $commandestatic = new Commande($db);
  168. /*
  169. * Statistics
  170. */
  171. $sql = "SELECT count(c.rowid) as nb, c.fk_statut as status";
  172. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  173. $sql .= ", ".MAIN_DB_PREFIX."commande as c";
  174. if (!$user->rights->societe->client->voir && !$socid) {
  175. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  176. }
  177. $sql .= " WHERE c.fk_soc = s.rowid";
  178. $sql .= " AND c.entity IN (".getEntity('societe').")";
  179. if ($user->socid) {
  180. $sql .= ' AND c.fk_soc = '.((int) $user->socid);
  181. }
  182. if (!$user->rights->societe->client->voir && !$socid) {
  183. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  184. }
  185. $sql .= " GROUP BY c.fk_statut";
  186. $resql = $db->query($sql);
  187. if ($resql) {
  188. $num = $db->num_rows($resql);
  189. $i = 0;
  190. $total = 0;
  191. $totalinprocess = 0;
  192. $dataseries = array();
  193. $colorseries = array();
  194. $vals = array();
  195. // -1=Canceled, 0=Draft, 1=Validated, 2=Accepted/On process, 3=Closed (Sent/Received, billed or not)
  196. while ($i < $num) {
  197. $row = $db->fetch_row($resql);
  198. if ($row) {
  199. //if ($row[1]!=-1 && ($row[1]!=3 || $row[2]!=1))
  200. {
  201. if (!isset($vals[$row[1]])) {
  202. $vals[$row[1]] = 0;
  203. }
  204. $vals[$row[1]] += $row[0];
  205. $totalinprocess += $row[0];
  206. }
  207. $total += $row[0];
  208. }
  209. $i++;
  210. }
  211. $db->free($resql);
  212. include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
  213. $result = '<div class="div-table-responsive-no-min">';
  214. $result .= '<table class="noborder nohover centpercent">';
  215. $result .= '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("CustomersOrders").'</th></tr>'."\n";
  216. $listofstatus = array(0, 1, 2, 3, -1);
  217. foreach ($listofstatus as $status) {
  218. $dataseries[] = array($commandestatic->LibStatut($status, 0, 1, 1), (isset($vals[$status]) ? (int) $vals[$status] : 0));
  219. if ($status == Commande::STATUS_DRAFT) {
  220. $colorseries[$status] = '-'.$badgeStatus0;
  221. }
  222. if ($status == Commande::STATUS_VALIDATED) {
  223. $colorseries[$status] = $badgeStatus1;
  224. }
  225. if ($status == Commande::STATUS_SHIPMENTONPROCESS) {
  226. $colorseries[$status] = $badgeStatus4;
  227. }
  228. if ($status == Commande::STATUS_CLOSED && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT)) {
  229. $colorseries[$status] = $badgeStatus6;
  230. }
  231. if ($status == Commande::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
  232. $colorseries[$status] = $badgeStatus6;
  233. }
  234. if ($status == Commande::STATUS_CANCELED) {
  235. $colorseries[$status] = $badgeStatus9;
  236. }
  237. if (empty($conf->use_javascript_ajax)) {
  238. $result .= '<tr class="oddeven">';
  239. $result .= '<td>'.$commandestatic->LibStatut($status, 0, 0, 1).'</td>';
  240. $result .= '<td class="right"><a href="list.php?statut='.$status.'">'.(isset($vals[$status]) ? $vals[$status] : 0).' ';
  241. $result .= $commandestatic->LibStatut($status, 0, 3, 1);
  242. $result .= '</a></td>';
  243. $result .= "</tr>\n";
  244. }
  245. }
  246. if ($conf->use_javascript_ajax) {
  247. $result .= '<tr class="impair"><td align="center" colspan="2">';
  248. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  249. $dolgraph = new DolGraph();
  250. $dolgraph->SetData($dataseries);
  251. $dolgraph->SetDataColor(array_values($colorseries));
  252. $dolgraph->setShowLegend(2);
  253. $dolgraph->setShowPercent(1);
  254. $dolgraph->SetType(array('pie'));
  255. $dolgraph->setHeight('150');
  256. $dolgraph->setWidth('300');
  257. $dolgraph->draw('idgraphstatus');
  258. $result .= $dolgraph->show($total ? 0 : 1);
  259. $result .= '</td></tr>';
  260. }
  261. //if ($totalinprocess != $total)
  262. $result .= '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td class="right">'.$total.'</td></tr>';
  263. $result .= "</table></div><br>";
  264. } else {
  265. dol_print_error($db);
  266. }
  267. return $result;
  268. }