propal.lib.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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") && !empty($conf->expedition_bon->enabled) && $user->rights->expedition->lire)
  41. || (isModEnabled("expedition") && !empty($conf->delivery_note->enabled) && $user->rights->expedition->delivery->lire)))) {
  42. $langs->load("sendings");
  43. $text = '';
  44. $head[$h][0] = DOL_URL_ROOT.'/expedition/propal.php?id='.$object->id;
  45. if ($conf->expedition_bon->enabled) {
  46. $text = $langs->trans("Shipment");
  47. }
  48. if ($conf->delivery_note->enabled) {
  49. $text .= '/'.$langs->trans("Receivings");
  50. }
  51. $head[$h][1] = $text;
  52. $head[$h][2] = 'shipping';
  53. $h++;
  54. }
  55. if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB)) {
  56. $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
  57. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/contact.php?id='.$object->id;
  58. $head[$h][1] = $langs->trans('ContactsAddresses');
  59. if ($nbContact > 0) {
  60. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
  61. }
  62. $head[$h][2] = 'contact';
  63. $h++;
  64. }
  65. // Show more tabs from modules
  66. // Entries must be declared in modules descriptor with line
  67. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  68. // $this->tabs = array('entity:-tabname); to remove a tab
  69. complete_head_from_modules($conf, $langs, $object, $head, $h, 'propal', 'add', 'core');
  70. if (empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
  71. $nbNote = 0;
  72. if (!empty($object->note_private)) {
  73. $nbNote++;
  74. }
  75. if (!empty($object->note_public)) {
  76. $nbNote++;
  77. }
  78. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/note.php?id='.$object->id;
  79. $head[$h][1] = $langs->trans('Notes');
  80. if ($nbNote > 0) {
  81. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  82. }
  83. $head[$h][2] = 'note';
  84. $h++;
  85. }
  86. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  87. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  88. $upload_dir = $conf->propal->multidir_output[$object->entity]."/".dol_sanitizeFileName($object->ref);
  89. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  90. $nbLinks = Link::count($db, $object->element, $object->id);
  91. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/document.php?id='.$object->id;
  92. $head[$h][1] = $langs->trans('Documents');
  93. if (($nbFiles + $nbLinks) > 0) {
  94. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  95. }
  96. $head[$h][2] = 'document';
  97. $h++;
  98. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/info.php?id='.$object->id;
  99. $head[$h][1] = $langs->trans('Info');
  100. $head[$h][2] = 'info';
  101. $h++;
  102. complete_head_from_modules($conf, $langs, $object, $head, $h, 'propal', 'add', 'external');
  103. complete_head_from_modules($conf, $langs, $object, $head, $h, 'propal', 'remove');
  104. return $head;
  105. }
  106. /**
  107. * Return array head with list of tabs to view object informations.
  108. *
  109. * @return array head array with tabs
  110. */
  111. function propal_admin_prepare_head()
  112. {
  113. global $langs, $conf, $user, $db;
  114. $extrafields = new ExtraFields($db);
  115. $extrafields->fetch_name_optionals_label('propal');
  116. $extrafields->fetch_name_optionals_label('propaldet');
  117. $h = 0;
  118. $head = array();
  119. $head[$h][0] = DOL_URL_ROOT.'/admin/propal.php';
  120. $head[$h][1] = $langs->trans("Miscellaneous");
  121. $head[$h][2] = 'general';
  122. $h++;
  123. // Show more tabs from modules
  124. // Entries must be declared in modules descriptor with line
  125. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  126. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  127. complete_head_from_modules($conf, $langs, null, $head, $h, 'propal_admin');
  128. $head[$h][0] = DOL_URL_ROOT.'/comm/admin/propal_extrafields.php';
  129. $head[$h][1] = $langs->trans("ExtraFields");
  130. $nbExtrafields = $extrafields->attributes['propal']['count'];
  131. if ($nbExtrafields > 0) {
  132. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  133. }
  134. $head[$h][2] = 'attributes';
  135. $h++;
  136. $head[$h][0] = DOL_URL_ROOT.'/comm/admin/propaldet_extrafields.php';
  137. $head[$h][1] = $langs->trans("ExtraFieldsLines");
  138. $nbExtrafields = $extrafields->attributes['propaldet']['count'];
  139. if ($nbExtrafields > 0) {
  140. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  141. }
  142. $head[$h][2] = 'attributeslines';
  143. $h++;
  144. complete_head_from_modules($conf, $langs, null, $head, $h, 'propal_admin', 'remove');
  145. return $head;
  146. }
  147. /**
  148. * Return a HTML table that contains a pie chart of customer proposals
  149. *
  150. * @param int $socid (Optional) Show only results from the customer with this id
  151. * @return string A HTML table that contains a pie chart of customer invoices
  152. */
  153. function getCustomerProposalPieChart($socid = 0)
  154. {
  155. global $conf, $db, $langs, $user;
  156. $result= '';
  157. if (!isModEnabled('propal') || empty($user->rights->propal->lire)) {
  158. return '';
  159. }
  160. $listofstatus = array(Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED);
  161. $propalstatic = new Propal($db);
  162. $sql = "SELECT count(p.rowid) as nb, p.fk_statut as status";
  163. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  164. $sql .= ", ".MAIN_DB_PREFIX."propal as p";
  165. if (empty($user->rights->societe->client->voir) && !$socid) {
  166. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  167. }
  168. $sql .= " WHERE p.entity IN (".getEntity($propalstatic->element).")";
  169. $sql .= " AND p.fk_soc = s.rowid";
  170. if ($user->socid) {
  171. $sql .= ' AND p.fk_soc = '.((int) $user->socid);
  172. }
  173. if (empty($user->rights->societe->client->voir) && !$socid) {
  174. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  175. }
  176. $sql .= " AND p.fk_statut IN (".$db->sanitize(implode(" ,", $listofstatus)).")";
  177. $sql .= " GROUP BY p.fk_statut";
  178. $resql = $db->query($sql);
  179. if ($resql) {
  180. $num = $db->num_rows($resql);
  181. $i = 0;
  182. $total = 0;
  183. $totalinprocess = 0;
  184. $dataseries = array();
  185. $colorseries = array();
  186. $vals = array();
  187. while ($i < $num) {
  188. $obj = $db->fetch_object($resql);
  189. if ($obj) {
  190. $vals[$obj->status] = $obj->nb;
  191. $totalinprocess += $obj->nb;
  192. $total += $obj->nb;
  193. }
  194. $i++;
  195. }
  196. $db->free($resql);
  197. include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
  198. $result = '<div class="div-table-responsive-no-min">';
  199. $result .= '<table class="noborder nohover centpercent">';
  200. $result .= '<tr class="liste_titre">';
  201. $result .= '<td colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("Proposals").'</td>';
  202. $result .= '</tr>';
  203. foreach ($listofstatus as $status) {
  204. $dataseries[] = array($propalstatic->LibStatut($status, 1), (isset($vals[$status]) ? (int) $vals[$status] : 0));
  205. if ($status == Propal::STATUS_DRAFT) {
  206. $colorseries[$status] = '-'.$badgeStatus0;
  207. }
  208. if ($status == Propal::STATUS_VALIDATED) {
  209. $colorseries[$status] = $badgeStatus1;
  210. }
  211. if ($status == Propal::STATUS_SIGNED) {
  212. $colorseries[$status] = $badgeStatus4;
  213. }
  214. if ($status == Propal::STATUS_NOTSIGNED) {
  215. $colorseries[$status] = $badgeStatus9;
  216. }
  217. if ($status == Propal::STATUS_BILLED) {
  218. $colorseries[$status] = $badgeStatus6;
  219. }
  220. if (empty($conf->use_javascript_ajax)) {
  221. $result .= '<tr class="oddeven">';
  222. $result .= '<td>'.$propalstatic->LibStatut($status, 0).'</td>';
  223. $result .= '<td class="right"><a href="list.php?statut='.$status.'">'.(isset($vals[$status]) ? $vals[$status] : 0).'</a></td>';
  224. $result .= "</tr>\n";
  225. }
  226. }
  227. if ($conf->use_javascript_ajax) {
  228. $result .= '<tr>';
  229. $result .= '<td align="center" colspan="2">';
  230. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  231. $dolgraph = new DolGraph();
  232. $dolgraph->SetData($dataseries);
  233. $dolgraph->SetDataColor(array_values($colorseries));
  234. $dolgraph->setShowLegend(2);
  235. $dolgraph->setShowPercent(1);
  236. $dolgraph->SetType(array('pie'));
  237. $dolgraph->setHeight('150');
  238. $dolgraph->setWidth('300');
  239. $dolgraph->draw('idgraphthirdparties');
  240. $result .= $dolgraph->show($total ? 0 : 1);
  241. $result .= '</td>';
  242. $result .= '</tr>';
  243. }
  244. //if ($totalinprocess != $total)
  245. //{
  246. // print '<tr class="liste_total">';
  247. // print '<td>'.$langs->trans("Total").' ('.$langs->trans("CustomersOrdersRunning").')</td>';
  248. // print '<td class="right">'.$totalinprocess.'</td>';
  249. // print '</tr>';
  250. //}
  251. $result .= '<tr class="liste_total">';
  252. $result .= '<td>'.$langs->trans("Total").'</td>';
  253. $result .= '<td class="right">'.$total.'</td>';
  254. $result .= '</tr>';
  255. $result .= '</table>';
  256. $result .= '</div>';
  257. $result .= '<br>';
  258. } else {
  259. dol_print_error($db);
  260. }
  261. return $result;
  262. }