order.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 (isModEnabled("expedition")) {
  36. $langs->load("sendings");
  37. }
  38. $langs->load("orders");
  39. $h = 0;
  40. $head = array();
  41. if (isModEnabled('commande') && $user->hasRight('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 (!getDolGlobalString('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 ((getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && $user->hasRight('expedition', 'lire'))
  58. || (getDolGlobalInt('MAIN_SUBMODULE_DELIVERY') && $user->hasRight('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 (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION')) {
  64. $text .= $langs->trans("Shipments");
  65. }
  66. if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && getDolGlobalInt('MAIN_SUBMODULE_DELIVERY')) {
  67. $text .= ' - ';
  68. }
  69. if (getDolGlobalInt('MAIN_SUBMODULE_DELIVERY')) {
  70. $text .= $langs->trans("Receivings");
  71. }
  72. if ($nbShipments > 0 || $nbReceiption > 0) {
  73. $text .= '<span class="badge marginleftonlyshort">'.($nbShipments ? $nbShipments : 0);
  74. }
  75. if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && getDolGlobalInt('MAIN_SUBMODULE_DELIVERY') && ($nbShipments > 0 || $nbReceiption > 0)) {
  76. $text .= ' - ';
  77. }
  78. if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && getDolGlobalInt('MAIN_SUBMODULE_DELIVERY') && ($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', 'add', 'core');
  93. if (!getDolGlobalString('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/agenda.php?id='.$object->id;
  122. $head[$h][1] = $langs->trans("Events");
  123. if (isModEnabled('agenda')&& ($user->hasRight('agenda', 'myactions', 'read') || $user->hasRight('agenda', 'allactions', 'read'))) {
  124. $nbEvent = 0;
  125. // Enable caching of thirdparty count actioncomm
  126. require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
  127. $cachekey = 'count_events_propal_'.$object->id;
  128. $dataretrieved = dol_getcache($cachekey);
  129. if (!is_null($dataretrieved)) {
  130. $nbEvent = $dataretrieved;
  131. } else {
  132. $sql = "SELECT COUNT(id) as nb";
  133. $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm";
  134. $sql .= " WHERE fk_element = ".((int) $object->id);
  135. $sql .= " AND elementtype = 'order'";
  136. $resql = $db->query($sql);
  137. if ($resql) {
  138. $obj = $db->fetch_object($resql);
  139. $nbEvent = $obj->nb;
  140. } else {
  141. dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR);
  142. }
  143. dol_setcache($cachekey, $nbEvent, 120); // If setting cache fails, this is not a problem, so we do not test result.
  144. }
  145. $head[$h][1] .= '/';
  146. $head[$h][1] .= $langs->trans("Agenda");
  147. if ($nbEvent > 0) {
  148. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbEvent.'</span>';
  149. }
  150. }
  151. $head[$h][2] = 'agenda';
  152. $h++;
  153. complete_head_from_modules($conf, $langs, $object, $head, $h, 'order', 'add', 'external');
  154. complete_head_from_modules($conf, $langs, $object, $head, $h, 'order', 'remove');
  155. return $head;
  156. }
  157. /**
  158. * Return array head with list of tabs to view object informations.
  159. *
  160. * @return array head array with tabs
  161. */
  162. function order_admin_prepare_head()
  163. {
  164. global $langs, $conf, $user, $db;
  165. $extrafields = new ExtraFields($db);
  166. $extrafields->fetch_name_optionals_label('commande');
  167. $extrafields->fetch_name_optionals_label('commandedet');
  168. $h = 0;
  169. $head = array();
  170. $head[$h][0] = DOL_URL_ROOT.'/admin/commande.php';
  171. $head[$h][1] = $langs->trans("Miscellaneous");
  172. $head[$h][2] = 'general';
  173. $h++;
  174. complete_head_from_modules($conf, $langs, null, $head, $h, 'order_admin');
  175. $head[$h][0] = DOL_URL_ROOT.'/admin/order_extrafields.php';
  176. $head[$h][1] = $langs->trans("ExtraFields");
  177. $nbExtrafields = $extrafields->attributes['commande']['count'];
  178. if ($nbExtrafields > 0) {
  179. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  180. }
  181. $head[$h][2] = 'attributes';
  182. $h++;
  183. $head[$h][0] = DOL_URL_ROOT.'/admin/orderdet_extrafields.php';
  184. $head[$h][1] = $langs->trans("ExtraFieldsLines");
  185. $nbExtrafields = $extrafields->attributes['commandedet']['count'];
  186. if ($nbExtrafields > 0) {
  187. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  188. }
  189. $head[$h][2] = 'attributeslines';
  190. $h++;
  191. complete_head_from_modules($conf, $langs, null, $head, $h, 'order_admin', 'remove');
  192. return $head;
  193. }
  194. /**
  195. * Return a HTML table that contains a pie chart of sales orders
  196. *
  197. * @param int $socid (Optional) Show only results from the customer with this id
  198. * @return string A HTML table that contains a pie chart of customer invoices
  199. */
  200. function getCustomerOrderPieChart($socid = 0)
  201. {
  202. global $conf, $db, $langs, $user;
  203. $result = '';
  204. if (!isModEnabled('commande') || !$user->hasRight('commande', 'lire')) {
  205. return '';
  206. }
  207. $commandestatic = new Commande($db);
  208. /*
  209. * Statistics
  210. */
  211. $sql = "SELECT count(c.rowid) as nb, c.fk_statut as status";
  212. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  213. $sql .= ", ".MAIN_DB_PREFIX."commande as c";
  214. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  215. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  216. }
  217. $sql .= " WHERE c.fk_soc = s.rowid";
  218. $sql .= " AND c.entity IN (".getEntity($commandestatic->element).")";
  219. if ($user->socid) {
  220. $sql .= ' AND c.fk_soc = '.((int) $user->socid);
  221. }
  222. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  223. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  224. }
  225. $sql .= " GROUP BY c.fk_statut";
  226. $resql = $db->query($sql);
  227. if ($resql) {
  228. $num = $db->num_rows($resql);
  229. $i = 0;
  230. $total = 0;
  231. $totalinprocess = 0;
  232. $dataseries = array();
  233. $colorseries = array();
  234. $vals = array();
  235. // -1=Canceled, 0=Draft, 1=Validated, 2=Accepted/On process, 3=Closed (Sent/Received, billed or not)
  236. while ($i < $num) {
  237. $row = $db->fetch_row($resql);
  238. if ($row) {
  239. if (!isset($vals[$row[1]])) {
  240. $vals[$row[1]] = 0;
  241. }
  242. $vals[$row[1]] += $row[0];
  243. $totalinprocess += $row[0];
  244. $total += $row[0];
  245. }
  246. $i++;
  247. }
  248. $db->free($resql);
  249. global $badgeStatus0, $badgeStatus1, $badgeStatus4, $badgeStatus6, $badgeStatus9;
  250. include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
  251. $result = '<div class="div-table-responsive-no-min">';
  252. $result .= '<table class="noborder nohover centpercent">';
  253. $result .= '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("CustomersOrders").'</th></tr>'."\n";
  254. $listofstatus = array(0, 1, 2, 3, -1);
  255. foreach ($listofstatus as $status) {
  256. $dataseries[] = array($commandestatic->LibStatut($status, 0, 1, 1), (isset($vals[$status]) ? (int) $vals[$status] : 0));
  257. if ($status == Commande::STATUS_DRAFT) {
  258. $colorseries[$status] = '-'.$badgeStatus0;
  259. }
  260. if ($status == Commande::STATUS_VALIDATED) {
  261. $colorseries[$status] = $badgeStatus1;
  262. }
  263. if ($status == Commande::STATUS_SHIPMENTONPROCESS) {
  264. $colorseries[$status] = $badgeStatus4;
  265. }
  266. if ($status == Commande::STATUS_CLOSED) {
  267. $colorseries[$status] = $badgeStatus6;
  268. }
  269. if ($status == Commande::STATUS_CANCELED) {
  270. $colorseries[$status] = $badgeStatus9;
  271. }
  272. if (empty($conf->use_javascript_ajax)) {
  273. $result .= '<tr class="oddeven">';
  274. $result .= '<td>'.$commandestatic->LibStatut($status, 0, 0, 1).'</td>';
  275. $result .= '<td class="right"><a href="list.php?statut='.$status.'">'.(isset($vals[$status]) ? $vals[$status] : 0).' ';
  276. $result .= $commandestatic->LibStatut($status, 0, 3, 1);
  277. $result .= '</a></td>';
  278. $result .= "</tr>\n";
  279. }
  280. }
  281. if (!empty($conf->use_javascript_ajax)) {
  282. $result .= '<tr class="impair"><td align="center" colspan="2">';
  283. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  284. $dolgraph = new DolGraph();
  285. $dolgraph->SetData($dataseries);
  286. $dolgraph->SetDataColor(array_values($colorseries));
  287. $dolgraph->setShowLegend(2);
  288. $dolgraph->setShowPercent(1);
  289. $dolgraph->SetType(array('pie'));
  290. $dolgraph->setHeight('150');
  291. $dolgraph->setWidth('300');
  292. $dolgraph->draw('idgraphstatus');
  293. $result .= $dolgraph->show($total ? 0 : 1);
  294. $result .= '</td></tr>';
  295. }
  296. //if ($totalinprocess != $total)
  297. $result .= '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td class="right">'.$total.'</td></tr>';
  298. $result .= "</table></div><br>";
  299. } else {
  300. dol_print_error($db);
  301. }
  302. return $result;
  303. }