propal.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /* Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/lib/propal.lib.php
  21. * \brief Ensemble de fonctions de base pour le module propal
  22. * \ingroup propal
  23. */
  24. /**
  25. * Prepare array with list of tabs
  26. *
  27. * @param object $object Object related to tabs
  28. * @return array Array of tabs to show
  29. */
  30. function propal_prepare_head($object)
  31. {
  32. global $db, $langs, $conf, $user;
  33. $langs->loadLangs(array('propal', 'compta', 'companies'));
  34. $h = 0;
  35. $head = array();
  36. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/card.php?id='.$object->id;
  37. $head[$h][1] = $langs->trans('Proposal');
  38. $head[$h][2] = 'comm';
  39. $h++;
  40. if ((empty($conf->commande->enabled) && ((isModEnabled("expedition") && getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && $user->hasRight('expedition', 'lire'))
  41. || (isModEnabled("expedition") && getDolGlobalInt('MAIN_SUBMODULE_DELIVERY') && $user->hasRight('expedition', 'delivery', 'lire'))))) {
  42. $langs->load("sendings");
  43. $text = '';
  44. $head[$h][0] = DOL_URL_ROOT.'/expedition/propal.php?id='.$object->id;
  45. if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION')) {
  46. $text = $langs->trans("Shipment");
  47. }
  48. if (getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION') && getDolGlobalInt('MAIN_SUBMODULE_DELIVERY')) {
  49. $text .= '/';
  50. }
  51. if (getDolGlobalInt('MAIN_SUBMODULE_DELIVERY')) {
  52. $text .= $langs->trans("Receivings");
  53. }
  54. $head[$h][1] = $text;
  55. $head[$h][2] = 'shipping';
  56. $h++;
  57. }
  58. if (!getDolGlobalString('MAIN_DISABLE_CONTACTS_TAB')) {
  59. $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
  60. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/contact.php?id='.$object->id;
  61. $head[$h][1] = $langs->trans('ContactsAddresses');
  62. if ($nbContact > 0) {
  63. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
  64. }
  65. $head[$h][2] = 'contact';
  66. $h++;
  67. }
  68. // Show more tabs from modules
  69. // Entries must be declared in modules descriptor with line
  70. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  71. // $this->tabs = array('entity:-tabname); to remove a tab
  72. complete_head_from_modules($conf, $langs, $object, $head, $h, 'propal', 'add', 'core');
  73. if (!getDolGlobalString('MAIN_DISABLE_NOTES_TAB')) {
  74. $nbNote = 0;
  75. if (!empty($object->note_private)) {
  76. $nbNote++;
  77. }
  78. if (!empty($object->note_public)) {
  79. $nbNote++;
  80. }
  81. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/note.php?id='.$object->id;
  82. $head[$h][1] = $langs->trans('Notes');
  83. if ($nbNote > 0) {
  84. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  85. }
  86. $head[$h][2] = 'note';
  87. $h++;
  88. }
  89. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  90. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  91. $upload_dir = $conf->propal->multidir_output[$object->entity]."/".dol_sanitizeFileName($object->ref);
  92. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  93. $nbLinks = Link::count($db, $object->element, $object->id);
  94. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/document.php?id='.$object->id;
  95. $head[$h][1] = $langs->trans('Documents');
  96. if (($nbFiles + $nbLinks) > 0) {
  97. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  98. }
  99. $head[$h][2] = 'document';
  100. $h++;
  101. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/agenda.php?id='.$object->id;
  102. $head[$h][1] = $langs->trans("Events");
  103. if (isModEnabled('agenda')&& ($user->hasRight('agenda', 'myactions', 'read') || $user->hasRight('agenda', 'allactions', 'read'))) {
  104. $nbEvent = 0;
  105. // Enable caching of thirdparty count actioncomm
  106. require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
  107. $cachekey = 'count_events_propal_'.$object->id;
  108. $dataretrieved = dol_getcache($cachekey);
  109. if (!is_null($dataretrieved)) {
  110. $nbEvent = $dataretrieved;
  111. } else {
  112. $sql = "SELECT COUNT(id) as nb";
  113. $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm";
  114. $sql .= " WHERE fk_element = ".((int) $object->id);
  115. $sql .= " AND elementtype = 'propal'";
  116. $resql = $db->query($sql);
  117. if ($resql) {
  118. $obj = $db->fetch_object($resql);
  119. $nbEvent = $obj->nb;
  120. } else {
  121. dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR);
  122. }
  123. dol_setcache($cachekey, $nbEvent, 120); // If setting cache fails, this is not a problem, so we do not test result.
  124. }
  125. $head[$h][1] .= '/';
  126. $head[$h][1] .= $langs->trans("Agenda");
  127. if ($nbEvent > 0) {
  128. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbEvent.'</span>';
  129. }
  130. }
  131. $head[$h][2] = 'agenda';
  132. $h++;
  133. complete_head_from_modules($conf, $langs, $object, $head, $h, 'propal', 'add', 'external');
  134. complete_head_from_modules($conf, $langs, $object, $head, $h, 'propal', 'remove');
  135. return $head;
  136. }
  137. /**
  138. * Return array head with list of tabs to view object informations.
  139. *
  140. * @return array head array with tabs
  141. */
  142. function propal_admin_prepare_head()
  143. {
  144. global $langs, $conf, $user, $db;
  145. $extrafields = new ExtraFields($db);
  146. $extrafields->fetch_name_optionals_label('propal');
  147. $extrafields->fetch_name_optionals_label('propaldet');
  148. $h = 0;
  149. $head = array();
  150. $head[$h][0] = DOL_URL_ROOT.'/admin/propal.php';
  151. $head[$h][1] = $langs->trans("Miscellaneous");
  152. $head[$h][2] = 'general';
  153. $h++;
  154. // Show more tabs from modules
  155. // Entries must be declared in modules descriptor with line
  156. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  157. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  158. complete_head_from_modules($conf, $langs, null, $head, $h, 'propal_admin');
  159. $head[$h][0] = DOL_URL_ROOT.'/comm/admin/propal_extrafields.php';
  160. $head[$h][1] = $langs->trans("ExtraFields");
  161. $nbExtrafields = $extrafields->attributes['propal']['count'];
  162. if ($nbExtrafields > 0) {
  163. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  164. }
  165. $head[$h][2] = 'attributes';
  166. $h++;
  167. $head[$h][0] = DOL_URL_ROOT.'/comm/admin/propaldet_extrafields.php';
  168. $head[$h][1] = $langs->trans("ExtraFieldsLines");
  169. $nbExtrafields = $extrafields->attributes['propaldet']['count'];
  170. if ($nbExtrafields > 0) {
  171. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  172. }
  173. $head[$h][2] = 'attributeslines';
  174. $h++;
  175. complete_head_from_modules($conf, $langs, null, $head, $h, 'propal_admin', 'remove');
  176. return $head;
  177. }
  178. /**
  179. * Return a HTML table that contains a pie chart of customer proposals
  180. *
  181. * @param int $socid (Optional) Show only results from the customer with this id
  182. * @return string A HTML table that contains a pie chart of customer invoices
  183. */
  184. function getCustomerProposalPieChart($socid = 0)
  185. {
  186. global $conf, $db, $langs, $user;
  187. $result= '';
  188. if (!isModEnabled('propal') || !$user->hasRight('propal', 'lire')) {
  189. return '';
  190. }
  191. $listofstatus = array(Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED);
  192. $propalstatic = new Propal($db);
  193. $sql = "SELECT count(p.rowid) as nb, p.fk_statut as status";
  194. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  195. $sql .= ", ".MAIN_DB_PREFIX."propal as p";
  196. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  197. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  198. }
  199. $sql .= " WHERE p.entity IN (".getEntity($propalstatic->element).")";
  200. $sql .= " AND p.fk_soc = s.rowid";
  201. if ($user->socid) {
  202. $sql .= ' AND p.fk_soc = '.((int) $user->socid);
  203. }
  204. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  205. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  206. }
  207. $sql .= " AND p.fk_statut IN (".$db->sanitize(implode(" ,", $listofstatus)).")";
  208. $sql .= " GROUP BY p.fk_statut";
  209. $resql = $db->query($sql);
  210. if ($resql) {
  211. $num = $db->num_rows($resql);
  212. $i = 0;
  213. $total = 0;
  214. $totalinprocess = 0;
  215. $dataseries = array();
  216. $colorseries = array();
  217. $vals = array();
  218. while ($i < $num) {
  219. $obj = $db->fetch_object($resql);
  220. if ($obj) {
  221. $vals[$obj->status] = $obj->nb;
  222. $totalinprocess += $obj->nb;
  223. $total += $obj->nb;
  224. }
  225. $i++;
  226. }
  227. $db->free($resql);
  228. global $badgeStatus0, $badgeStatus1, $badgeStatus4, $badgeStatus6, $badgeStatus9;
  229. include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
  230. $result = '<div class="div-table-responsive-no-min">';
  231. $result .= '<table class="noborder nohover centpercent">';
  232. $result .= '<tr class="liste_titre">';
  233. $result .= '<td colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("Proposals").'</td>';
  234. $result .= '</tr>';
  235. foreach ($listofstatus as $status) {
  236. $dataseries[] = array($propalstatic->LibStatut($status, 1), (isset($vals[$status]) ? (int) $vals[$status] : 0));
  237. if ($status == Propal::STATUS_DRAFT) {
  238. $colorseries[$status] = '-'.$badgeStatus0;
  239. }
  240. if ($status == Propal::STATUS_VALIDATED) {
  241. $colorseries[$status] = $badgeStatus1;
  242. }
  243. if ($status == Propal::STATUS_SIGNED) {
  244. $colorseries[$status] = $badgeStatus4;
  245. }
  246. if ($status == Propal::STATUS_NOTSIGNED) {
  247. $colorseries[$status] = $badgeStatus9;
  248. }
  249. if ($status == Propal::STATUS_BILLED) {
  250. $colorseries[$status] = $badgeStatus6;
  251. }
  252. if (empty($conf->use_javascript_ajax)) {
  253. $result .= '<tr class="oddeven">';
  254. $result .= '<td>'.$propalstatic->LibStatut($status, 0).'</td>';
  255. $result .= '<td class="right"><a href="list.php?statut='.$status.'">'.(isset($vals[$status]) ? $vals[$status] : 0).'</a></td>';
  256. $result .= "</tr>\n";
  257. }
  258. }
  259. if ($conf->use_javascript_ajax) {
  260. $result .= '<tr>';
  261. $result .= '<td align="center" colspan="2">';
  262. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  263. $dolgraph = new DolGraph();
  264. $dolgraph->SetData($dataseries);
  265. $dolgraph->SetDataColor(array_values($colorseries));
  266. $dolgraph->setShowLegend(2);
  267. $dolgraph->setShowPercent(1);
  268. $dolgraph->SetType(array('pie'));
  269. $dolgraph->setHeight('150');
  270. $dolgraph->setWidth('300');
  271. $dolgraph->draw('idgraphthirdparties');
  272. $result .= $dolgraph->show($total ? 0 : 1);
  273. $result .= '</td>';
  274. $result .= '</tr>';
  275. }
  276. //if ($totalinprocess != $total)
  277. //{
  278. // print '<tr class="liste_total">';
  279. // print '<td>'.$langs->trans("Total").' ('.$langs->trans("CustomersOrdersRunning").')</td>';
  280. // print '<td class="right">'.$totalinprocess.'</td>';
  281. // print '</tr>';
  282. //}
  283. $result .= '<tr class="liste_total">';
  284. $result .= '<td>'.$langs->trans("Total").'</td>';
  285. $result .= '<td class="right">'.$total.'</td>';
  286. $result .= '</tr>';
  287. $result .= '</table>';
  288. $result .= '</div>';
  289. $result .= '<br>';
  290. } else {
  291. dol_print_error($db);
  292. }
  293. return $result;
  294. }