order.lib.php 10 KB

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