company.lib.php 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. <?php
  2. /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
  5. * Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  7. * Copyright (C) 2013-2014 Juanjo Menent <jmenent@2byte.es>
  8. * Copyright (C) 2013 Christophe Battarel <contact@altairis.fr>
  9. * Copyright (C) 2013-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  10. * Copyright (C) 2015-2021 Frédéric France <frederic.france@netlogic.fr>
  11. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  12. * Copyright (C) 2017 Rui Strecht <rui.strecht@aliartalentos.com>
  13. * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  27. * or see https://www.gnu.org/
  28. */
  29. /**
  30. * \file htdocs/core/lib/company.lib.php
  31. * \brief Ensemble de fonctions de base pour le module societe
  32. * \ingroup societe
  33. */
  34. /**
  35. * Return array of tabs to used on pages for third parties cards.
  36. *
  37. * @param Societe $object Object company shown
  38. * @return array Array of tabs
  39. */
  40. function societe_prepare_head(Societe $object)
  41. {
  42. global $db, $langs, $conf, $user;
  43. global $hookmanager;
  44. $h = 0;
  45. $head = array();
  46. $head[$h][0] = DOL_URL_ROOT.'/societe/card.php?socid='.$object->id;
  47. $head[$h][1] = $langs->trans("ThirdParty");
  48. $head[$h][2] = 'card';
  49. $h++;
  50. if (empty($conf->global->MAIN_SUPPORT_SHARED_CONTACT_BETWEEN_THIRDPARTIES)) {
  51. if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->societe->contact->lire) {
  52. //$nbContact = count($object->liste_contact(-1,'internal')) + count($object->liste_contact(-1,'external'));
  53. $nbContact = 0;
  54. // Enable caching of thirdrparty count Contacts
  55. require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
  56. $cachekey = 'count_contacts_thirdparty_'.$object->id;
  57. $dataretrieved = dol_getcache($cachekey);
  58. if (!is_null($dataretrieved)) {
  59. $nbContact = $dataretrieved;
  60. } else {
  61. $sql = "SELECT COUNT(p.rowid) as nb";
  62. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
  63. // Add table from hooks
  64. $parameters = array('contacttab' => true);
  65. $reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters, $object); // Note that $action and $object may have been modified by hook
  66. $sql .= $hookmanager->resPrint;
  67. $sql .= " WHERE p.fk_soc = ".((int) $object->id);
  68. // Add where from hooks
  69. $parameters = array('contacttab' => true);
  70. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $object may have been modified by hook
  71. $sql .= $hookmanager->resPrint;
  72. $resql = $db->query($sql);
  73. if ($resql) {
  74. $obj = $db->fetch_object($resql);
  75. $nbContact = $obj->nb;
  76. }
  77. dol_setcache($cachekey, $nbContact, 120); // If setting cache fails, this is not a problem, so we do not test result.
  78. }
  79. $head[$h][0] = DOL_URL_ROOT.'/societe/contact.php?socid='.$object->id;
  80. $head[$h][1] = $langs->trans('ContactsAddresses');
  81. if ($nbContact > 0) {
  82. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
  83. }
  84. $head[$h][2] = 'contact';
  85. $h++;
  86. }
  87. } else {
  88. $head[$h][0] = DOL_URL_ROOT.'/societe/societecontact.php?socid='.$object->id;
  89. $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
  90. $head[$h][1] = $langs->trans("ContactsAddresses");
  91. if ($nbContact > 0) {
  92. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
  93. }
  94. $head[$h][2] = 'contact';
  95. $h++;
  96. }
  97. if ($object->client == 1 || $object->client == 2 || $object->client == 3) {
  98. $head[$h][0] = DOL_URL_ROOT.'/comm/card.php?socid='.$object->id;
  99. $head[$h][1] = '';
  100. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && ($object->client == 2 || $object->client == 3)) {
  101. $head[$h][1] .= $langs->trans("Prospect");
  102. }
  103. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && $object->client == 3) {
  104. $head[$h][1] .= ' | ';
  105. }
  106. if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && ($object->client == 1 || $object->client == 3)) {
  107. $head[$h][1] .= $langs->trans("Customer");
  108. }
  109. $head[$h][2] = 'customer';
  110. $h++;
  111. if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
  112. $langs->load("products");
  113. // price
  114. $head[$h][0] = DOL_URL_ROOT.'/societe/price.php?socid='.$object->id;
  115. $head[$h][1] = $langs->trans("CustomerPrices");
  116. $head[$h][2] = 'price';
  117. $h++;
  118. }
  119. }
  120. $supplier_module_enabled = 0;
  121. if ((isModEnabled('fournisseur') && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_proposal->enabled) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) {
  122. $supplier_module_enabled = 1;
  123. }
  124. if ($supplier_module_enabled == 1 && $object->fournisseur && !empty($user->rights->fournisseur->lire)) {
  125. $head[$h][0] = DOL_URL_ROOT.'/fourn/card.php?socid='.$object->id;
  126. $head[$h][1] = $langs->trans("Supplier");
  127. $head[$h][2] = 'supplier';
  128. $h++;
  129. }
  130. if (!empty($conf->project->enabled) && (!empty($user->rights->projet->lire))) {
  131. $nbProject = 0;
  132. // Enable caching of thirdrparty count projects
  133. require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
  134. $cachekey = 'count_projects_thirdparty_'.$object->id;
  135. $dataretrieved = dol_getcache($cachekey);
  136. if (!is_null($dataretrieved)) {
  137. $nbProject = $dataretrieved;
  138. } else {
  139. $sql = "SELECT COUNT(n.rowid) as nb";
  140. $sql .= " FROM ".MAIN_DB_PREFIX."projet as n";
  141. $sql .= " WHERE fk_soc = ".((int) $object->id);
  142. $sql .= " AND entity IN (".getEntity('project').")";
  143. $resql = $db->query($sql);
  144. if ($resql) {
  145. $obj = $db->fetch_object($resql);
  146. $nbProject = $obj->nb;
  147. } else {
  148. dol_print_error($db);
  149. }
  150. dol_setcache($cachekey, $nbProject, 120); // If setting cache fails, this is not a problem, so we do not test result.
  151. }
  152. $head[$h][0] = DOL_URL_ROOT.'/societe/project.php?socid='.$object->id;
  153. $head[$h][1] = $langs->trans("Projects");
  154. if ($nbProject > 0) {
  155. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbProject.'</span>';
  156. }
  157. $head[$h][2] = 'project';
  158. $h++;
  159. }
  160. // Tab to link resources
  161. if (isModEnabled('resource') && !empty($conf->global->RESOURCE_ON_THIRDPARTIES)) {
  162. $head[$h][0] = DOL_URL_ROOT.'/resource/element_resource.php?element=societe&element_id='.$object->id;
  163. $head[$h][1] = $langs->trans("Resources");
  164. $head[$h][2] = 'resources';
  165. $h++;
  166. }
  167. // Related items
  168. if ((isModEnabled('commande') || isModEnabled('propal') || isModEnabled('facture') || isModEnabled('ficheinter') || (isModEnabled('fournisseur') && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled))
  169. && empty($conf->global->THIRDPARTIES_DISABLE_RELATED_OBJECT_TAB)) {
  170. $head[$h][0] = DOL_URL_ROOT.'/societe/consumption.php?socid='.$object->id;
  171. $head[$h][1] = $langs->trans("Referers");
  172. $head[$h][2] = 'consumption';
  173. $h++;
  174. }
  175. // Bank accounts
  176. if (empty($conf->global->SOCIETE_DISABLE_BANKACCOUNT)) {
  177. $nbBankAccount = 0;
  178. $foundonexternalonlinesystem = 0;
  179. $langs->load("bills");
  180. $title = $langs->trans("PaymentModes");
  181. if (isModEnabled('stripe')) {
  182. //$langs->load("stripe");
  183. //$title = $langs->trans("BankAccountsAndGateways");
  184. $servicestatus = 0;
  185. if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) {
  186. $servicestatus = 1;
  187. }
  188. include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
  189. $societeaccount = new SocieteAccount($db);
  190. $stripecu = $societeaccount->getCustomerAccount($object->id, 'stripe', $servicestatus); // Get thirdparty cu_...
  191. if ($stripecu) {
  192. $foundonexternalonlinesystem++;
  193. }
  194. }
  195. $sql = "SELECT COUNT(n.rowid) as nb";
  196. $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib as n";
  197. $sql .= " WHERE n.fk_soc = ".((int) $object->id);
  198. if (!isModEnabled('stripe')) {
  199. $sql .= " AND n.stripe_card_ref IS NULL";
  200. } else {
  201. $sql .= " AND (n.stripe_card_ref IS NULL OR (n.stripe_card_ref IS NOT NULL AND n.status = ".((int) $servicestatus)."))";
  202. }
  203. $resql = $db->query($sql);
  204. if ($resql) {
  205. $obj = $db->fetch_object($resql);
  206. $nbBankAccount = $obj->nb;
  207. } else {
  208. dol_print_error($db);
  209. }
  210. //if (isModEnabled('stripe') && $nbBankAccount > 0) $nbBankAccount = '...'; // No way to know exact number
  211. $head[$h][0] = DOL_URL_ROOT.'/societe/paymentmodes.php?socid='.urlencode($object->id);
  212. $head[$h][1] = $title;
  213. if ($foundonexternalonlinesystem) {
  214. $head[$h][1] .= '<span class="badge marginleftonlyshort">...</span>';
  215. } elseif ($nbBankAccount > 0) {
  216. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbBankAccount.'</span>';
  217. }
  218. $head[$h][2] = 'rib';
  219. $h++;
  220. }
  221. if (isModEnabled('website') && (!empty($conf->global->WEBSITE_USE_WEBSITE_ACCOUNTS)) && (!empty($user->rights->societe->lire))) {
  222. $head[$h][0] = DOL_URL_ROOT.'/societe/website.php?id='.urlencode($object->id);
  223. $head[$h][1] = $langs->trans("WebSiteAccounts");
  224. $nbNote = 0;
  225. $sql = "SELECT COUNT(n.rowid) as nb";
  226. $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as n";
  227. $sql .= " WHERE fk_soc = ".((int) $object->id).' AND fk_website > 0';
  228. $resql = $db->query($sql);
  229. if ($resql) {
  230. $obj = $db->fetch_object($resql);
  231. $nbNote = $obj->nb;
  232. } else {
  233. dol_print_error($db);
  234. }
  235. if ($nbNote > 0) {
  236. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  237. }
  238. $head[$h][2] = 'website';
  239. $h++;
  240. }
  241. if (getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR', 'thirdparty') == 'thirdparty') {
  242. if (!empty($user->rights->partnership->read)) {
  243. $langs->load("partnership");
  244. $nbPartnership = is_array($object->partnerships) ? count($object->partnerships) : 0;
  245. $head[$h][0] = DOL_URL_ROOT.'/societe/partnership.php?socid='.$object->id;
  246. $head[$h][1] = $langs->trans("Partnership");
  247. $head[$h][2] = 'partnership';
  248. if ($nbPartnership > 0) {
  249. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbPartnership.'</span>';
  250. }
  251. $h++;
  252. }
  253. }
  254. // Show more tabs from modules
  255. // Entries must be declared in modules descriptor with line
  256. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  257. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  258. complete_head_from_modules($conf, $langs, $object, $head, $h, 'thirdparty');
  259. if ($user->socid == 0) {
  260. // Notifications
  261. if (isModEnabled('notification')) {
  262. $nbNotif = 0;
  263. // Enable caching of thirdparty count notifications
  264. require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
  265. $cachekey = 'count_notifications_thirdparty_'.$object->id;
  266. $dataretrieved = dol_getcache($cachekey);
  267. if (!is_null($dataretrieved)) {
  268. $nbNotif = $dataretrieved;
  269. } else {
  270. $sql = "SELECT COUNT(n.rowid) as nb";
  271. $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n";
  272. $sql .= " WHERE fk_soc = ".((int) $object->id);
  273. $resql = $db->query($sql);
  274. if ($resql) {
  275. $obj = $db->fetch_object($resql);
  276. $nbNotif = $obj->nb;
  277. } else {
  278. dol_print_error($db);
  279. }
  280. dol_setcache($cachekey, $nbNotif, 120); // If setting cache fails, this is not a problem, so we do not test result.
  281. }
  282. $head[$h][0] = DOL_URL_ROOT.'/societe/notify/card.php?socid='.urlencode($object->id);
  283. $head[$h][1] = $langs->trans("Notifications");
  284. if ($nbNotif > 0) {
  285. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNotif.'</span>';
  286. }
  287. $head[$h][2] = 'notify';
  288. $h++;
  289. }
  290. // Notes
  291. $nbNote = 0;
  292. if (!empty($object->note_private)) {
  293. $nbNote++;
  294. }
  295. if (!empty($object->note_public)) {
  296. $nbNote++;
  297. }
  298. $head[$h][0] = DOL_URL_ROOT.'/societe/note.php?id='.urlencode($object->id);
  299. $head[$h][1] = $langs->trans("Notes");
  300. if ($nbNote > 0) {
  301. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  302. }
  303. $head[$h][2] = 'note';
  304. $h++;
  305. // Attached files and Links
  306. $totalAttached = 0;
  307. // Enable caching of thirdrparty count attached files and links
  308. require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
  309. $cachekey = 'count_attached_thirdparty_'.$object->id;
  310. $dataretrieved = dol_getcache($cachekey);
  311. if (!is_null($dataretrieved)) {
  312. $totalAttached = $dataretrieved;
  313. } else {
  314. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  315. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  316. $upload_dir = $conf->societe->multidir_output[$object->entity]."/".$object->id;
  317. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  318. $nbLinks = Link::count($db, $object->element, $object->id);
  319. $totalAttached = $nbFiles + $nbLinks;
  320. dol_setcache($cachekey, $totalAttached, 120); // If setting cache fails, this is not a problem, so we do not test result.
  321. }
  322. $head[$h][0] = DOL_URL_ROOT.'/societe/document.php?socid='.$object->id;
  323. $head[$h][1] = $langs->trans("Documents");
  324. if (($totalAttached) > 0) {
  325. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($totalAttached).'</span>';
  326. }
  327. $head[$h][2] = 'document';
  328. $h++;
  329. }
  330. $head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id;
  331. $head[$h][1] = $langs->trans("Events");
  332. if (isModEnabled('agenda')&& (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) {
  333. $nbEvent = 0;
  334. // Enable caching of thirdrparty count actioncomm
  335. require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
  336. $cachekey = 'count_events_thirdparty_'.$object->id;
  337. $dataretrieved = dol_getcache($cachekey);
  338. if (!is_null($dataretrieved)) {
  339. $nbEvent = $dataretrieved;
  340. } else {
  341. $sql = "SELECT COUNT(id) as nb";
  342. $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm";
  343. $sql .= " WHERE fk_soc = ".((int) $object->id);
  344. $resql = $db->query($sql);
  345. if ($resql) {
  346. $obj = $db->fetch_object($resql);
  347. $nbEvent = $obj->nb;
  348. } else {
  349. dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR);
  350. }
  351. dol_setcache($cachekey, $nbEvent, 120); // If setting cache fails, this is not a problem, so we do not test result.
  352. }
  353. $head[$h][1] .= '/';
  354. $head[$h][1] .= $langs->trans("Agenda");
  355. if ($nbEvent > 0) {
  356. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbEvent.'</span>';
  357. }
  358. }
  359. $head[$h][2] = 'agenda';
  360. $h++;
  361. // Log
  362. /*$head[$h][0] = DOL_URL_ROOT.'/societe/info.php?socid='.$object->id;
  363. $head[$h][1] = $langs->trans("Info");
  364. $head[$h][2] = 'info';
  365. $h++;*/
  366. complete_head_from_modules($conf, $langs, $object, $head, $h, 'thirdparty', 'remove');
  367. return $head;
  368. }
  369. /**
  370. * Return array of tabs to used on page
  371. *
  372. * @param Object $object Object for tabs
  373. * @return array Array of tabs
  374. */
  375. function societe_prepare_head2($object)
  376. {
  377. global $langs, $conf, $user;
  378. $h = 0;
  379. $head = array();
  380. $head[$h][0] = DOL_URL_ROOT.'/societe/card.php?socid='.$object->id;
  381. $head[$h][1] = $langs->trans("ThirdParty");
  382. $head[$h][2] = 'company';
  383. $h++;
  384. $head[$h][0] = 'commerciaux.php?socid='.$object->id;
  385. $head[$h][1] = $langs->trans("SalesRepresentative");
  386. $head[$h][2] = 'salesrepresentative';
  387. $h++;
  388. return $head;
  389. }
  390. /**
  391. * Return array head with list of tabs to view object informations.
  392. *
  393. * @return array head array with tabs
  394. */
  395. function societe_admin_prepare_head()
  396. {
  397. global $langs, $conf, $user;
  398. $h = 0;
  399. $head = array();
  400. $head[$h][0] = DOL_URL_ROOT.'/societe/admin/societe.php';
  401. $head[$h][1] = $langs->trans("Miscellaneous");
  402. $head[$h][2] = 'general';
  403. $h++;
  404. // Show more tabs from modules
  405. // Entries must be declared in modules descriptor with line
  406. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  407. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  408. complete_head_from_modules($conf, $langs, null, $head, $h, 'company_admin');
  409. $head[$h][0] = DOL_URL_ROOT.'/societe/admin/societe_extrafields.php';
  410. $head[$h][1] = $langs->trans("ExtraFieldsThirdParties");
  411. $head[$h][2] = 'attributes';
  412. $h++;
  413. $head[$h][0] = DOL_URL_ROOT.'/societe/admin/contact_extrafields.php';
  414. $head[$h][1] = $langs->trans("ExtraFieldsContacts");
  415. $head[$h][2] = 'attributes_contacts';
  416. $h++;
  417. complete_head_from_modules($conf, $langs, null, $head, $h, 'company_admin', 'remove');
  418. return $head;
  419. }
  420. /**
  421. * Return country label, code or id from an id, code or label
  422. *
  423. * @param int $searchkey Id or code of country to search
  424. * @param string $withcode '0'=Return label,
  425. * '1'=Return code + label,
  426. * '2'=Return code from id,
  427. * '3'=Return id from code,
  428. * 'all'=Return array('id'=>,'code'=>,'label'=>)
  429. * @param DoliDB $dbtouse Database handler (using in global way may fail because of conflicts with some autoload features)
  430. * @param Translate $outputlangs Langs object for output translation
  431. * @param int $entconv 0=Return value without entities and not converted to output charset, 1=Ready for html output
  432. * @param int $searchlabel Label of country to search (warning: searching on label is not reliable)
  433. * @return mixed Integer with country id or String with country code or translated country name or Array('id','code','label') or 'NotDefined'
  434. */
  435. function getCountry($searchkey, $withcode = '', $dbtouse = 0, $outputlangs = '', $entconv = 1, $searchlabel = '')
  436. {
  437. global $db, $langs;
  438. $result = '';
  439. // Check parameters
  440. if (empty($searchkey) && empty($searchlabel)) {
  441. if ($withcode === 'all') {
  442. return array('id'=>'', 'code'=>'', 'label'=>'');
  443. } else {
  444. return '';
  445. }
  446. }
  447. if (!is_object($dbtouse)) {
  448. $dbtouse = $db;
  449. }
  450. if (!is_object($outputlangs)) {
  451. $outputlangs = $langs;
  452. }
  453. $sql = "SELECT rowid, code, label FROM ".MAIN_DB_PREFIX."c_country";
  454. if (is_numeric($searchkey)) {
  455. $sql .= " WHERE rowid = ".((int) $searchkey);
  456. } elseif (!empty($searchkey)) {
  457. $sql .= " WHERE code = '".$db->escape($searchkey)."'";
  458. } else {
  459. $sql .= " WHERE label = '".$db->escape($searchlabel)."'";
  460. }
  461. $resql = $dbtouse->query($sql);
  462. if ($resql) {
  463. $obj = $dbtouse->fetch_object($resql);
  464. if ($obj) {
  465. $label = ((!empty($obj->label) && $obj->label != '-') ? $obj->label : '');
  466. if (is_object($outputlangs)) {
  467. $outputlangs->load("dict");
  468. if ($entconv) {
  469. $label = ($obj->code && ($outputlangs->trans("Country".$obj->code) != "Country".$obj->code)) ? $outputlangs->trans("Country".$obj->code) : $label;
  470. } else {
  471. $label = ($obj->code && ($outputlangs->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code)) ? $outputlangs->transnoentitiesnoconv("Country".$obj->code) : $label;
  472. }
  473. }
  474. if ($withcode == 1) {
  475. $result = $label ? "$obj->code - $label" : "$obj->code";
  476. } elseif ($withcode == 2) {
  477. $result = $obj->code;
  478. } elseif ($withcode == 3) {
  479. $result = $obj->rowid;
  480. } elseif ($withcode === 'all') {
  481. $result = array('id'=>$obj->rowid, 'code'=>$obj->code, 'label'=>$label);
  482. } else {
  483. $result = $label;
  484. }
  485. } else {
  486. $result = 'NotDefined';
  487. }
  488. $dbtouse->free($resql);
  489. return $result;
  490. } else {
  491. dol_print_error($dbtouse, '');
  492. }
  493. return 'Error';
  494. }
  495. /**
  496. * Return state translated from an id. Return value is always utf8 encoded and without entities.
  497. *
  498. * @param int $id id of state (province/departement)
  499. * @param int $withcode '0'=Return label,
  500. * '1'=Return string code + label,
  501. * '2'=Return code,
  502. * 'all'=return array('id'=>,'code'=>,'label'=>)
  503. * @param DoliDB $dbtouse Database handler (using in global way may fail because of conflicts with some autoload features)
  504. * @param int $withregion '0'=Ignores region,
  505. * '1'=Add region name/code/id as needed to output,
  506. * @param Translate $outputlangs Langs object for output translation, not fully implemented yet
  507. * @param int $entconv 0=Return value without entities and not converted to output charset, 1=Ready for html output
  508. * @return string|array String with state code or state name or Array('id','code','label')/Array('id','code','label','region_code','region')
  509. */
  510. function getState($id, $withcode = '', $dbtouse = 0, $withregion = 0, $outputlangs = '', $entconv = 1)
  511. {
  512. global $db, $langs;
  513. if (!is_object($dbtouse)) {
  514. $dbtouse = $db;
  515. }
  516. $sql = "SELECT d.rowid as id, d.code_departement as code, d.nom as name, d.active, c.label as country, c.code as country_code, r.code_region as region_code, r.nom as region_name FROM";
  517. $sql .= " ".MAIN_DB_PREFIX."c_departements as d, ".MAIN_DB_PREFIX."c_regions as r,".MAIN_DB_PREFIX."c_country as c";
  518. $sql .= " WHERE d.fk_region=r.code_region and r.fk_pays=c.rowid and d.rowid=".((int) $id);
  519. $sql .= " AND d.active = 1 AND r.active = 1 AND c.active = 1";
  520. $sql .= " ORDER BY c.code, d.code_departement";
  521. dol_syslog("Company.lib::getState", LOG_DEBUG);
  522. $resql = $dbtouse->query($sql);
  523. if ($resql) {
  524. $obj = $dbtouse->fetch_object($resql);
  525. if ($obj) {
  526. $label = ((!empty($obj->name) && $obj->name != '-') ? $obj->name : '');
  527. if (is_object($outputlangs)) {
  528. $outputlangs->load("dict");
  529. if ($entconv) {
  530. $label = ($obj->code && ($outputlangs->trans("State".$obj->code) != "State".$obj->code)) ? $outputlangs->trans("State".$obj->code) : $label;
  531. } else {
  532. $label = ($obj->code && ($outputlangs->transnoentitiesnoconv("State".$obj->code) != "State".$obj->code)) ? $outputlangs->transnoentitiesnoconv("State".$obj->code) : $label;
  533. }
  534. }
  535. if ($withcode == 1) {
  536. if ($withregion == 1) {
  537. return $label = $obj->region_name.' - '.$obj->code.' - '.($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
  538. } else {
  539. return $label = $obj->code.' - '.($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
  540. }
  541. } elseif ($withcode == 2) {
  542. if ($withregion == 1) {
  543. return $label = $obj->region_name.' - '.($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
  544. } else {
  545. return $label = ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
  546. }
  547. } elseif ($withcode === 'all') {
  548. if ($withregion == 1) {
  549. return array('id'=>$obj->id, 'code'=>$obj->code, 'label'=>$label, 'region_code'=>$obj->region_code, 'region'=>$obj->region_name);
  550. } else {
  551. return array('id'=>$obj->id, 'code'=>$obj->code, 'label'=>$label);
  552. }
  553. } else {
  554. if ($withregion == 1) {
  555. return $label = $obj->region_name.' - '.$label;
  556. } else {
  557. return $label;
  558. }
  559. }
  560. } else {
  561. return $langs->transnoentitiesnoconv("NotDefined");
  562. }
  563. } else {
  564. dol_print_error($dbtouse, '');
  565. }
  566. }
  567. /**
  568. * Return label of currency or code+label
  569. *
  570. * @param string $code_iso Code iso of currency
  571. * @param int $withcode '1'=show code + label
  572. * @param Translate $outputlangs Output language
  573. * @return string Label translated of currency
  574. */
  575. function currency_name($code_iso, $withcode = '', $outputlangs = null)
  576. {
  577. global $langs, $db;
  578. if (empty($outputlangs)) {
  579. $outputlangs = $langs;
  580. }
  581. $outputlangs->load("dict");
  582. // If there is a translation, we can send immediatly the label
  583. if ($outputlangs->trans("Currency".$code_iso) != "Currency".$code_iso) {
  584. return ($withcode ? $code_iso.' - ' : '').$outputlangs->trans("Currency".$code_iso);
  585. }
  586. // If no translation, we read table to get label by default
  587. $sql = "SELECT label FROM ".MAIN_DB_PREFIX."c_currencies";
  588. $sql .= " WHERE code_iso='".$db->escape($code_iso)."'";
  589. $resql = $db->query($sql);
  590. if ($resql) {
  591. $num = $db->num_rows($resql);
  592. if ($num) {
  593. $obj = $db->fetch_object($resql);
  594. $label = ($obj->label != '-' ? $obj->label : '');
  595. if ($withcode) {
  596. return ($label == $code_iso) ? "$code_iso" : "$code_iso - $label";
  597. } else {
  598. return $label;
  599. }
  600. } else {
  601. return $code_iso;
  602. }
  603. }
  604. return 'ErrorWhenReadingCurrencyLabel';
  605. }
  606. /**
  607. * Retourne le nom traduit de la forme juridique
  608. *
  609. * @param string $code Code de la forme juridique
  610. * @return string Nom traduit du pays
  611. */
  612. function getFormeJuridiqueLabel($code)
  613. {
  614. global $db, $langs;
  615. if (!$code) {
  616. return '';
  617. }
  618. $sql = "SELECT libelle FROM ".MAIN_DB_PREFIX."c_forme_juridique";
  619. $sql .= " WHERE code='".$db->escape($code)."'";
  620. dol_syslog("Company.lib::getFormeJuridiqueLabel", LOG_DEBUG);
  621. $resql = $db->query($sql);
  622. if ($resql) {
  623. $num = $db->num_rows($resql);
  624. if ($num) {
  625. $obj = $db->fetch_object($resql);
  626. $label = ($obj->libelle != '-' ? $obj->libelle : '');
  627. return $label;
  628. } else {
  629. return $langs->trans("NotDefined");
  630. }
  631. }
  632. }
  633. /**
  634. * Return list of countries that are inside the EEC (European Economic Community)
  635. * Note: Try to keep this function as a "memory only" function for performance reasons.
  636. *
  637. * @return array Array of countries code in EEC
  638. */
  639. function getCountriesInEEC()
  640. {
  641. // List of all country codes that are in europe for european vat rules
  642. // List found on https://ec.europa.eu/taxation_customs/territorial-status-eu-countries-and-certain-territories_en
  643. global $conf, $db;
  644. $country_code_in_EEC = array();
  645. if (!empty($conf->cache['country_code_in_EEC'])) {
  646. // Use of cache to reduce number of database requests
  647. $country_code_in_EEC = $conf->cache['country_code_in_EEC'];
  648. } else {
  649. $sql = "SELECT cc.code FROM ".MAIN_DB_PREFIX."c_country as cc";
  650. $sql .= " WHERE cc.eec = 1";
  651. $resql = $db->query($sql);
  652. if ($resql) {
  653. $num = $db->num_rows($resql);
  654. $i = 0;
  655. while ($i < $num) {
  656. $objp = $db->fetch_object($resql);
  657. $country_code_in_EEC[] = $objp->code;
  658. $i++;
  659. }
  660. } else {
  661. dol_print_error($db);
  662. }
  663. $conf->cache['country_code_in_EEC'] = $country_code_in_EEC;
  664. }
  665. return $country_code_in_EEC;
  666. }
  667. /**
  668. * Return if a country of an object is inside the EEC (European Economic Community)
  669. *
  670. * @param Object $object Object
  671. * @return boolean true = country inside EEC, false = country outside EEC
  672. */
  673. function isInEEC($object)
  674. {
  675. if (empty($object->country_code)) {
  676. return false;
  677. }
  678. $country_code_in_EEC = getCountriesInEEC(); // This make a database call but there is a cache done into $conf->cache['country_code_in_EEC']
  679. //print "dd".$object->country_code;
  680. return in_array($object->country_code, $country_code_in_EEC);
  681. }
  682. /**
  683. * Show html area for list of projects
  684. *
  685. * @param Conf $conf Object conf
  686. * @param Translate $langs Object langs
  687. * @param DoliDB $db Database handler
  688. * @param Object $object Third party object
  689. * @param string $backtopage Url to go once contact is created
  690. * @param int $nocreatelink 1=Hide create project link
  691. * @param string $morehtmlright More html on right of title
  692. * @return int
  693. */
  694. function show_projects($conf, $langs, $db, $object, $backtopage = '', $nocreatelink = 0, $morehtmlright = '')
  695. {
  696. global $user, $action, $hookmanager;
  697. $i = -1;
  698. if (!empty($conf->project->enabled) && $user->rights->projet->lire) {
  699. $langs->load("projects");
  700. $newcardbutton = '';
  701. if (!empty($conf->project->enabled) && $user->rights->projet->creer && empty($nocreatelink)) {
  702. $newcardbutton .= dolGetButtonTitle($langs->trans('AddProject'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/projet/card.php?socid='.$object->id.'&amp;action=create&amp;backtopage='.urlencode($backtopage));
  703. }
  704. print "\n";
  705. print load_fiche_titre($langs->trans("ProjectsDedicatedToThisThirdParty"), $newcardbutton.$morehtmlright, '');
  706. print '<div class="div-table-responsive">'."\n";
  707. print '<table class="noborder centpercent">';
  708. $sql = "SELECT p.rowid as id, p.entity, p.title, p.ref, p.public, p.dateo as do, p.datee as de, p.fk_statut as status, p.fk_opp_status, p.opp_amount, p.opp_percent, p.tms as date_update, p.budget_amount";
  709. $sql .= ", cls.code as opp_status_code";
  710. $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
  711. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_lead_status as cls on p.fk_opp_status = cls.rowid";
  712. $sql .= " WHERE p.fk_soc = ".((int) $object->id);
  713. $sql .= " AND p.entity IN (".getEntity('project').")";
  714. $sql .= " ORDER BY p.dateo DESC";
  715. $result = $db->query($sql);
  716. if ($result) {
  717. $num = $db->num_rows($result);
  718. print '<tr class="liste_titre">';
  719. print '<td>'.$langs->trans("Ref").'</td>';
  720. print '<td>'.$langs->trans("Name").'</td>';
  721. print '<td class="center">'.$langs->trans("DateStart").'</td>';
  722. print '<td class="center">'.$langs->trans("DateEnd").'</td>';
  723. print '<td class="right">'.$langs->trans("OpportunityAmountShort").'</td>';
  724. print '<td class="center">'.$langs->trans("OpportunityStatusShort").'</td>';
  725. print '<td class="right">'.$langs->trans("OpportunityProbabilityShort").'</td>';
  726. print '<td class="right">'.$langs->trans("Status").'</td>';
  727. print '</tr>';
  728. if ($num > 0) {
  729. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  730. $projecttmp = new Project($db);
  731. $i = 0;
  732. while ($i < $num) {
  733. $obj = $db->fetch_object($result);
  734. $projecttmp->fetch($obj->id);
  735. // To verify role of users
  736. $userAccess = $projecttmp->restrictedProjectArea($user);
  737. if ($user->rights->projet->lire && $userAccess > 0) {
  738. print '<tr class="oddeven">';
  739. // Ref
  740. print '<td class="nowraponall">';
  741. print $projecttmp->getNomUrl(1);
  742. print '</td>';
  743. // Label
  744. print '<td class="tdoverflowmax200" title="'.dol_escape_htmltag($obj->title).'">'.dol_escape_htmltag($obj->title).'</td>';
  745. // Date start
  746. print '<td class="center">'.dol_print_date($db->jdate($obj->do), "day").'</td>';
  747. // Date end
  748. print '<td class="center">'.dol_print_date($db->jdate($obj->de), "day").'</td>';
  749. // Opp amount
  750. print '<td class="right">';
  751. if ($obj->opp_status_code) {
  752. print price($obj->opp_amount, 1, '', 1, -1, -1, '');
  753. }
  754. print '</td>';
  755. // Opp status
  756. print '<td class="center">';
  757. if ($obj->opp_status_code) {
  758. print $langs->trans("OppStatus".$obj->opp_status_code);
  759. }
  760. print '</td>';
  761. // Opp percent
  762. print '<td class="right">';
  763. if ($obj->opp_percent) {
  764. print price($obj->opp_percent, 1, '', 1, 0).'%';
  765. }
  766. print '</td>';
  767. // Status
  768. print '<td class="right">'.$projecttmp->getLibStatut(5).'</td>';
  769. print '</tr>';
  770. }
  771. $i++;
  772. }
  773. } else {
  774. print '<tr class="oddeven"><td colspan="8"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  775. }
  776. $db->free($result);
  777. } else {
  778. dol_print_error($db);
  779. }
  780. $parameters = array('sql'=>$sql, 'function'=>'show_projects');
  781. $reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  782. print $hookmanager->resPrint;
  783. print "</table>";
  784. print '</div>';
  785. print "<br>\n";
  786. }
  787. return $i;
  788. }
  789. /**
  790. * Show html area for list of contacts
  791. *
  792. * @param Conf $conf Object conf
  793. * @param Translate $langs Object langs
  794. * @param DoliDB $db Database handler
  795. * @param Societe $object Third party object
  796. * @param string $backtopage Url to go once contact is created
  797. * @param int $showuserlogin 1=Show also user login if it exists
  798. * @return int
  799. */
  800. function show_contacts($conf, $langs, $db, $object, $backtopage = '', $showuserlogin = 0)
  801. {
  802. global $user, $conf, $extrafields, $hookmanager;
  803. global $contextpage;
  804. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  805. $formcompany = new FormCompany($db);
  806. $form = new Form($db);
  807. $optioncss = GETPOST('optioncss', 'alpha');
  808. $sortfield = GETPOST('sortfield', 'aZ09comma');
  809. $sortorder = GETPOST('sortorder', 'aZ09comma');
  810. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  811. $search_status = GETPOST("search_status", 'int');
  812. if ($search_status == '') {
  813. $search_status = 1; // always display active customer first
  814. }
  815. $search_name = GETPOST("search_name", 'alpha');
  816. $search_address = GETPOST("search_address", 'alpha');
  817. $search_poste = GETPOST("search_poste", 'alpha');
  818. $search_roles = GETPOST("search_roles", 'array');
  819. $socialnetworks = getArrayOfSocialNetworks();
  820. $searchAddressPhoneDBFields = array(
  821. //Address
  822. 't.address',
  823. 't.zip',
  824. 't.town',
  825. //Phone
  826. 't.phone',
  827. 't.phone_perso',
  828. 't.phone_mobile',
  829. //Fax
  830. 't.fax',
  831. //E-mail
  832. 't.email',
  833. );
  834. //Social media
  835. // foreach ($socialnetworks as $key => $value) {
  836. // if ($value['active']) {
  837. // $searchAddressPhoneDBFields['t.'.$key] = "t.socialnetworks->'$.".$key."'";
  838. // }
  839. // }
  840. if (!$sortorder) {
  841. $sortorder = "ASC";
  842. }
  843. if (!$sortfield) {
  844. $sortfield = "t.lastname";
  845. }
  846. if (!empty($conf->clicktodial->enabled)) {
  847. $user->fetch_clicktodial(); // lecture des infos de clicktodial du user
  848. }
  849. $contactstatic = new Contact($db);
  850. $extrafields->fetch_name_optionals_label($contactstatic->table_element);
  851. $contactstatic->fields = array(
  852. 'name' =>array('type'=>'varchar(128)', 'label'=>'Name', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1),
  853. 'poste' =>array('type'=>'varchar(128)', 'label'=>'PostOrFunction', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>2, 'index'=>1, 'position'=>20),
  854. 'address' =>array('type'=>'varchar(128)', 'label'=>'Address', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>3, 'index'=>1, 'position'=>30),
  855. 'role' =>array('type'=>'checkbox', 'label'=>'Role', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>4, 'index'=>1, 'position'=>40),
  856. 'statut' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'default'=>0, 'index'=>1, 'position'=>50, 'arrayofkeyval'=>array(0=>$contactstatic->LibStatut(0, 1), 1=>$contactstatic->LibStatut(1, 1))),
  857. );
  858. // Definition of fields for list
  859. $arrayfields = array(
  860. 't.rowid'=>array('label'=>"TechnicalID", 'checked'=>(!empty($conf->global->MAIN_SHOW_TECHNICAL_ID) ? 1 : 0), 'enabled'=>(!empty($conf->global->MAIN_SHOW_TECHNICAL_ID) ? 1 : 0), 'position'=>1),
  861. 't.name'=>array('label'=>"Name", 'checked'=>1, 'position'=>10),
  862. 't.poste'=>array('label'=>"PostOrFunction", 'checked'=>1, 'position'=>20),
  863. 't.address'=>array('label'=>(empty($conf->dol_optimize_smallscreen) ? $langs->trans("Address").' / '.$langs->trans("Phone").' / '.$langs->trans("Email") : $langs->trans("Address")), 'checked'=>1, 'position'=>30),
  864. 'sc.role'=>array('label'=>"ContactByDefaultFor", 'checked'=>1, 'position'=>40),
  865. 't.statut'=>array('label'=>"Status", 'checked'=>1, 'position'=>50, 'class'=>'center'),
  866. );
  867. // Extra fields
  868. if (!empty($extrafields->attributes[$contactstatic->table_element]['label']) && is_array($extrafields->attributes[$contactstatic->table_element]['label']) && count($extrafields->attributes[$contactstatic->table_element]['label'])) {
  869. foreach ($extrafields->attributes[$contactstatic->table_element]['label'] as $key => $val) {
  870. if (!empty($extrafields->attributes[$contactstatic->table_element]['list'][$key])) {
  871. $arrayfields["ef.".$key] = array(
  872. 'label'=>$extrafields->attributes[$contactstatic->table_element]['label'][$key],
  873. 'checked'=>(($extrafields->attributes[$contactstatic->table_element]['list'][$key] < 0) ? 0 : 1),
  874. 'position'=>1000 + $extrafields->attributes[$contactstatic->table_element]['pos'][$key],
  875. 'enabled'=>(abs($extrafields->attributes[$contactstatic->table_element]['list'][$key]) != 3 && $extrafields->attributes[$contactstatic->table_element]['perms'][$key]));
  876. }
  877. }
  878. }
  879. // Initialize array of search criterias
  880. $search = array();
  881. foreach ($arrayfields as $key => $val) {
  882. $queryName = 'search_'.substr($key, 2);
  883. if (GETPOST($queryName, 'alpha')) {
  884. $search[substr($key, 2)] = GETPOST($queryName, 'alpha');
  885. }
  886. }
  887. $search_array_options = $extrafields->getOptionalsFromPost($contactstatic->table_element, '', 'search_');
  888. // Purge search criteria
  889. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
  890. $search_status = '';
  891. $search_name = '';
  892. $search_roles = array();
  893. $search_address = '';
  894. $search_poste = '';
  895. $search = array();
  896. $search_array_options = array();
  897. foreach ($contactstatic->fields as $key => $val) {
  898. $search[$key] = '';
  899. }
  900. }
  901. $contactstatic->fields = dol_sort_array($contactstatic->fields, 'position');
  902. $arrayfields = dol_sort_array($arrayfields, 'position');
  903. $newcardbutton = '';
  904. if ($user->rights->societe->contact->creer) {
  905. $addcontact = (!empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("AddContact") : $langs->trans("AddContactAddress"));
  906. $newcardbutton .= dolGetButtonTitle($addcontact, '', 'fa fa-plus-circle', DOL_URL_ROOT.'/contact/card.php?socid='.$object->id.'&amp;action=create&amp;backtopage='.urlencode($backtopage));
  907. }
  908. print "\n";
  909. $title = (!empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("ContactsForCompany") : $langs->trans("ContactsAddressesForCompany"));
  910. print load_fiche_titre($title, $newcardbutton, '');
  911. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'" name="formfilter">';
  912. print '<input type="hidden" name="token" value="'.newToken().'">';
  913. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  914. print '<input type="hidden" name="socid" value="'.$object->id.'">';
  915. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  916. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  917. print '<input type="hidden" name="page" value="'.$page.'">';
  918. $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
  919. $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  920. //if ($massactionbutton) $selectedfields.=$form->showCheckAddButtons('checkforselect', 1);
  921. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  922. print "\n".'<table class="tagtable liste">'."\n";
  923. $param = "socid=".urlencode($object->id);
  924. if ($search_status != '') {
  925. $param .= '&search_status='.urlencode($search_status);
  926. }
  927. if (count($search_roles) > 0) {
  928. $param .= implode('&search_roles[]=', $search_roles);
  929. }
  930. if ($search_name != '') {
  931. $param .= '&search_name='.urlencode($search_name);
  932. }
  933. if ($search_poste != '') {
  934. $param .= '&search_poste='.urlencode($search_poste);
  935. }
  936. if ($search_address != '') {
  937. $param .= '&search_address='.urlencode($search_address);
  938. }
  939. if ($optioncss != '') {
  940. $param .= '&optioncss='.urlencode($optioncss);
  941. }
  942. // Add $param from extra fields
  943. $extrafieldsobjectkey = $contactstatic->table_element;
  944. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php';
  945. $sql = "SELECT t.rowid, t.entity, t.lastname, t.firstname, t.fk_pays as country_id, t.civility, t.poste, t.phone as phone_pro, t.phone_mobile, t.phone_perso, t.fax, t.email, t.socialnetworks, t.statut, t.photo,";
  946. $sql .= " t.civility as civility_id, t.address, t.zip, t.town";
  947. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as t";
  948. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as ef on (t.rowid = ef.fk_object)";
  949. $sql .= " WHERE t.fk_soc = ".((int) $object->id);
  950. if ($search_status != '' && $search_status != '-1') {
  951. $sql .= " AND t.statut = ".((int) $search_status);
  952. }
  953. if ($search_name) {
  954. $sql .= natural_search(array('t.lastname', 't.firstname'), $search_name);
  955. }
  956. if ($search_poste) {
  957. $sql .= natural_search('t.poste', $search_poste);
  958. }
  959. if ($search_address) {
  960. $sql .= natural_search($searchAddressPhoneDBFields, $search_address);
  961. }
  962. if (count($search_roles) > 0) {
  963. $sql .= " AND t.rowid IN (SELECT sc.fk_socpeople FROM ".MAIN_DB_PREFIX."societe_contacts as sc WHERE sc.fk_c_type_contact IN (".$db->sanitize(implode(',', $search_roles))."))";
  964. }
  965. // Add where from extra fields
  966. $extrafieldsobjectkey = $contactstatic->table_element;
  967. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
  968. // Add where from hooks
  969. $parameters = array('socid' => $object->id);
  970. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $object may have been modified by hook
  971. $sql .= $hookmanager->resPrint;
  972. if ($sortfield == "t.name") {
  973. $sql .= " ORDER BY t.lastname $sortorder, t.firstname $sortorder";
  974. } else {
  975. $sql .= " ORDER BY $sortfield $sortorder";
  976. }
  977. dol_syslog('core/lib/company.lib.php :: show_contacts', LOG_DEBUG);
  978. $result = $db->query($sql);
  979. if (!$result) {
  980. dol_print_error($db);
  981. }
  982. $num = $db->num_rows($result);
  983. // Fields title search
  984. // --------------------------------------------------------------------
  985. print '<tr class="liste_titre">';
  986. foreach ($contactstatic->fields as $key => $val) {
  987. $align = '';
  988. if (in_array($val['type'], array('date', 'datetime', 'timestamp'))) {
  989. $align .= ($align ? ' ' : '').'center';
  990. }
  991. if (in_array($val['type'], array('timestamp'))) {
  992. $align .= ($align ? ' ' : '').'nowrap';
  993. }
  994. if ($key == 'status' || $key == 'statut') {
  995. $align .= ($align ? ' ' : '').'center';
  996. }
  997. if (!empty($arrayfields['t.'.$key]['checked']) || !empty($arrayfields['sc.'.$key]['checked'])) {
  998. print '<td class="liste_titre'.($align ? ' '.$align : '').'">';
  999. if (in_array($key, array('statut'))) {
  1000. print $form->selectarray('search_status', array('-1'=>'', '0'=>$contactstatic->LibStatut(0, 1), '1'=>$contactstatic->LibStatut(1, 1)), $search_status);
  1001. } elseif (in_array($key, array('role'))) {
  1002. print $formcompany->showRoles("search_roles", $contactstatic, 'edit', $search_roles, 'minwidth200 maxwidth300');
  1003. } else {
  1004. print '<input type="text" class="flat maxwidth75" name="search_'.$key.'" value="'.(!empty($search[$key]) ? dol_escape_htmltag($search[$key]) : '').'">';
  1005. }
  1006. print '</td>';
  1007. }
  1008. }
  1009. if ($showuserlogin) {
  1010. print '<td></td>';
  1011. }
  1012. // Extra fields
  1013. $extrafieldsobjectkey = $contactstatic->table_element;
  1014. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_input.tpl.php';
  1015. // Fields from hook
  1016. $parameters = array('arrayfields'=>$arrayfields);
  1017. $reshook = $hookmanager->executeHooks('printFieldListOption', $parameters, $contactstatic); // Note that $action and $object may have been modified by hook
  1018. print $hookmanager->resPrint;
  1019. // Action column
  1020. print '<td class="liste_titre" align="right">';
  1021. print $form->showFilterButtons();
  1022. print '</td>';
  1023. print '</tr>'."\n";
  1024. // Fields title label
  1025. // --------------------------------------------------------------------
  1026. print '<tr class="liste_titre">';
  1027. foreach ($contactstatic->fields as $key => $val) {
  1028. $align = '';
  1029. if (in_array($val['type'], array('date', 'datetime', 'timestamp'))) {
  1030. $align .= ($align ? ' ' : '').'center';
  1031. }
  1032. if (in_array($val['type'], array('timestamp'))) {
  1033. $align .= ($align ? ' ' : '').'nowrap';
  1034. }
  1035. if ($key == 'status' || $key == 'statut') {
  1036. $align .= ($align ? ' ' : '').'center';
  1037. }
  1038. if (!empty($arrayfields['t.'.$key]['checked'])) {
  1039. print getTitleFieldOfList($val['label'], 0, $_SERVER['PHP_SELF'], 't.'.$key, '', $param, ($align ? 'class="'.$align.'"' : ''), $sortfield, $sortorder, $align.' ')."\n";
  1040. }
  1041. if ($key == 'role') {
  1042. $align .= ($align ? ' ' : '').'left';
  1043. }
  1044. if (!empty($arrayfields['sc.'.$key]['checked'])) {
  1045. print getTitleFieldOfList($arrayfields['sc.'.$key]['label'], 0, $_SERVER['PHP_SELF'], '', '', $param, ($align ? 'class="'.$align.'"' : ''), $sortfield, $sortorder, $align.' ')."\n";
  1046. }
  1047. }
  1048. if ($showuserlogin) {
  1049. print '<td>'.$langs->trans("DolibarrLogin").'</td>';
  1050. }
  1051. // Extra fields
  1052. $extrafieldsobjectkey = $contactstatic->table_element;
  1053. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_title.tpl.php';
  1054. // Hook fields
  1055. $parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
  1056. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object); // Note that $action and $object may have been modified by hook
  1057. print $hookmanager->resPrint;
  1058. print getTitleFieldOfList($selectedfields, 0, $_SERVER["PHP_SELF"], '', '', '', 'align="center"', $sortfield, $sortorder, 'maxwidthsearch ')."\n";
  1059. print '</tr>'."\n";
  1060. $i = -1;
  1061. if ($num || (GETPOST('button_search') || GETPOST('button_search.x') || GETPOST('button_search_x'))) {
  1062. $i = 0;
  1063. while ($i < $num) {
  1064. $obj = $db->fetch_object($result);
  1065. $contactstatic->id = $obj->rowid;
  1066. $contactstatic->ref = $obj->rowid;
  1067. $contactstatic->statut = $obj->statut;
  1068. $contactstatic->lastname = $obj->lastname;
  1069. $contactstatic->firstname = $obj->firstname;
  1070. $contactstatic->civility_id = $obj->civility_id;
  1071. $contactstatic->civility_code = $obj->civility_id;
  1072. $contactstatic->poste = $obj->poste;
  1073. $contactstatic->address = $obj->address;
  1074. $contactstatic->zip = $obj->zip;
  1075. $contactstatic->town = $obj->town;
  1076. $contactstatic->phone_pro = $obj->phone_pro;
  1077. $contactstatic->phone_mobile = $obj->phone_mobile;
  1078. $contactstatic->phone_perso = $obj->phone_perso;
  1079. $contactstatic->email = $obj->email;
  1080. $contactstatic->socialnetworks = $obj->socialnetworks;
  1081. $contactstatic->photo = $obj->photo;
  1082. $contactstatic->entity = $obj->entity;
  1083. $country_code = getCountry($obj->country_id, 2);
  1084. $contactstatic->country_code = $country_code;
  1085. $contactstatic->setGenderFromCivility();
  1086. $contactstatic->fetch_optionals();
  1087. $resultRole = $contactstatic->fetchRoles();
  1088. if ($resultRole < 0) {
  1089. setEventMessages(null, $contactstatic->errors, 'errors');
  1090. }
  1091. if (is_array($contactstatic->array_options)) {
  1092. foreach ($contactstatic->array_options as $key => $val) {
  1093. $obj->$key = $val;
  1094. }
  1095. }
  1096. print '<tr class="oddeven">';
  1097. // ID
  1098. if (!empty($arrayfields['t.rowid']['checked'])) {
  1099. print '<td>';
  1100. print $contactstatic->id;
  1101. print '</td>';
  1102. }
  1103. // Photo - Name
  1104. if (!empty($arrayfields['t.name']['checked'])) {
  1105. print '<td>';
  1106. print $form->showphoto('contact', $contactstatic, 0, 0, 0, 'photorefnoborder valignmiddle marginrightonly', 'small', 1, 0, 1);
  1107. print $contactstatic->getNomUrl(0, '', 0, '&backtopage='.urlencode($backtopage));
  1108. print '</td>';
  1109. }
  1110. // Job position
  1111. if (!empty($arrayfields['t.poste']['checked'])) {
  1112. print '<td>';
  1113. if ($obj->poste) {
  1114. print $obj->poste;
  1115. }
  1116. print '</td>';
  1117. }
  1118. // Address - Phone - Email
  1119. if (!empty($arrayfields['t.address']['checked'])) {
  1120. print '<td>';
  1121. print $contactstatic->getBannerAddress('contact', $object);
  1122. print '</td>';
  1123. }
  1124. // Role
  1125. if (!empty($arrayfields['sc.role']['checked'])) {
  1126. print '<td>';
  1127. print $formcompany->showRoles("roles", $contactstatic, 'view');
  1128. print '</td>';
  1129. }
  1130. // Status
  1131. if (!empty($arrayfields['t.statut']['checked'])) {
  1132. print '<td class="center">'.$contactstatic->getLibStatut(5).'</td>';
  1133. }
  1134. if ($showuserlogin) {
  1135. print '<td>';
  1136. $tmpuser= new User($db);
  1137. $resfetch = $tmpuser->fetch(0, '', '', 0, -1, '', $contactstatic->id);
  1138. if ($resfetch > 0) {
  1139. print $tmpuser->getNomUrl(1, '', 0, 0, 24, 1);
  1140. }
  1141. print '</td>';
  1142. }
  1143. // Extra fields
  1144. $extrafieldsobjectkey = $contactstatic->table_element;
  1145. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php';
  1146. // Actions
  1147. print '<td align="right">';
  1148. // Add to agenda
  1149. if (isModEnabled('agenda')&& $user->rights->agenda->myactions->create) {
  1150. print '<a href="'.DOL_URL_ROOT.'/comm/action/card.php?action=create&actioncode=&contactid='.$obj->rowid.'&socid='.$object->id.'&backtopage='.urlencode($backtopage).'">';
  1151. print img_object($langs->trans("Event"), "action");
  1152. print '</a> &nbsp; ';
  1153. }
  1154. // Edit
  1155. if ($user->rights->societe->contact->creer) {
  1156. print '<a class="editfielda paddingleft" href="'.DOL_URL_ROOT.'/contact/card.php?action=edit&token='.newToken().'&id='.$obj->rowid.'&backtopage='.urlencode($backtopage).'">';
  1157. print img_edit();
  1158. print '</a>';
  1159. }
  1160. print '</td>';
  1161. print "</tr>\n";
  1162. $i++;
  1163. }
  1164. } else {
  1165. $colspan = 1;
  1166. foreach ($arrayfields as $key => $val) {
  1167. if (!empty($val['checked'])) {
  1168. $colspan++;
  1169. }
  1170. }
  1171. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1172. }
  1173. print "\n</table>\n";
  1174. print '</div>';
  1175. print '</form>'."\n";
  1176. return $i;
  1177. }
  1178. /**
  1179. * Show html area with actions to do
  1180. *
  1181. * @param Conf $conf Object conf
  1182. * @param Translate $langs Object langs
  1183. * @param DoliDB $db Object db
  1184. * @param Adherent|Societe $filterobj Object thirdparty or member
  1185. * @param Contact $objcon Object contact
  1186. * @param int $noprint Return string but does not output it
  1187. * @param int $actioncode Filter on actioncode
  1188. * @return string|void Return html part or void if noprint is 1
  1189. */
  1190. function show_actions_todo($conf, $langs, $db, $filterobj, $objcon = '', $noprint = 0, $actioncode = '')
  1191. {
  1192. global $user, $conf;
  1193. $out = show_actions_done($conf, $langs, $db, $filterobj, $objcon, 1, $actioncode, 'todo');
  1194. if ($noprint) {
  1195. return $out;
  1196. } else {
  1197. print $out;
  1198. }
  1199. }
  1200. /**
  1201. * Show html area with actions (done or not, ignore the name of function).
  1202. * Note: Global parameter $param must be defined.
  1203. *
  1204. * @param Conf $conf Object conf
  1205. * @param Translate $langs Object langs
  1206. * @param DoliDB $db Object db
  1207. * @param mixed $filterobj Filter on object Adherent|Societe|Project|Product|CommandeFournisseur|Dolresource|Ticket... to list events linked to an object
  1208. * @param Contact $objcon Filter on object contact to filter events on a contact
  1209. * @param int $noprint Return string but does not output it
  1210. * @param string $actioncode Filter on actioncode
  1211. * @param string $donetodo Filter on event 'done' or 'todo' or ''=nofilter (all).
  1212. * @param array $filters Filter on other fields
  1213. * @param string $sortfield Sort field
  1214. * @param string $sortorder Sort order
  1215. * @param string $module You can add module name here if elementtype in table llx_actioncomm is objectkey@module
  1216. * @return string|void Return html part or void if noprint is 1
  1217. */
  1218. function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprint = 0, $actioncode = '', $donetodo = 'done', $filters = array(), $sortfield = 'a.datep,a.id', $sortorder = 'DESC', $module = '')
  1219. {
  1220. global $user, $conf;
  1221. global $form;
  1222. global $param, $massactionbutton;
  1223. $start_year = GETPOST('dateevent_startyear', 'int');
  1224. $start_month = GETPOST('dateevent_startmonth', 'int');
  1225. $start_day = GETPOST('dateevent_startday', 'int');
  1226. $end_year = GETPOST('dateevent_endyear', 'int');
  1227. $end_month = GETPOST('dateevent_endmonth', 'int');
  1228. $end_day = GETPOST('dateevent_endday', 'int');
  1229. $tms_start = '';
  1230. $tms_end = '';
  1231. if (!empty($start_year) && !empty($start_month) && !empty($start_day)) {
  1232. $tms_start = dol_mktime(0, 0, 0, $start_month, $start_day, $start_year, 'tzuserrel');
  1233. }
  1234. if (!empty($end_year) && !empty($end_month) && !empty($end_day)) {
  1235. $tms_end = dol_mktime(23, 59, 59, $end_month, $end_day, $end_year, 'tzuserrel');
  1236. }
  1237. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All test are required to be compatible with all browsers
  1238. $tms_start = '';
  1239. $tms_end = '';
  1240. }
  1241. dol_include_once('/comm/action/class/actioncomm.class.php');
  1242. // Check parameters
  1243. if (!is_object($filterobj) && !is_object($objcon)) {
  1244. dol_print_error('', 'BadParameter');
  1245. }
  1246. $out = '';
  1247. $histo = array();
  1248. $numaction = 0;
  1249. $now = dol_now('tzuser');
  1250. // Open DSI -- Fix order by -- Begin
  1251. $sortfield_list = explode(',', $sortfield);
  1252. $sortfield_label_list = array('a.id' => 'id', 'a.datep' => 'dp', 'a.percent' => 'percent');
  1253. $sortfield_new_list = array();
  1254. foreach ($sortfield_list as $sortfield_value) {
  1255. $sortfield_new_list[] = $sortfield_label_list[trim($sortfield_value)];
  1256. }
  1257. $sortfield_new = implode(',', $sortfield_new_list);
  1258. $sql = '';
  1259. if (isModEnabled('agenda')) {
  1260. // Recherche histo sur actioncomm
  1261. if (is_object($objcon) && $objcon->id > 0) {
  1262. $sql = "SELECT DISTINCT a.id, a.label as label,";
  1263. } else {
  1264. $sql = "SELECT a.id, a.label as label,";
  1265. }
  1266. $sql .= " a.datep as dp,";
  1267. $sql .= " a.datep2 as dp2,";
  1268. $sql .= " a.percent as percent, 'action' as type,";
  1269. $sql .= " a.fk_element, a.elementtype,";
  1270. $sql .= " a.fk_contact,";
  1271. $sql .= " c.code as acode, c.libelle as alabel, c.picto as apicto,";
  1272. $sql .= " u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname";
  1273. if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur'))) {
  1274. $sql .= ", sp.lastname, sp.firstname";
  1275. } elseif (is_object($filterobj) && get_class($filterobj) == 'Dolresource') {
  1276. /* Nothing */
  1277. } elseif (is_object($filterobj) && get_class($filterobj) == 'Project') {
  1278. /* Nothing */
  1279. } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') {
  1280. $sql .= ", m.lastname, m.firstname";
  1281. } elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') {
  1282. $sql .= ", o.ref";
  1283. } elseif (is_object($filterobj) && get_class($filterobj) == 'Product') {
  1284. $sql .= ", o.ref";
  1285. } elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') {
  1286. $sql .= ", o.ref";
  1287. } elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') {
  1288. $sql .= ", o.ref";
  1289. } elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') {
  1290. $sql .= ", o.ref";
  1291. } elseif (is_object($filterobj) && is_array($filterobj->fields) && is_array($filterobj->fields['rowid']) && $filterobj->table_element && $filterobj->element) {
  1292. if (!empty($filterobj->fields['ref'])) {
  1293. $sql .= ", o.ref";
  1294. } elseif (!empty($filterobj->fields['label'])) {
  1295. $sql .= ", o.label";
  1296. }
  1297. }
  1298. $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
  1299. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_action";
  1300. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action = c.id";
  1301. $force_filter_contact = false;
  1302. if (is_object($objcon) && $objcon->id > 0) {
  1303. $force_filter_contact = true;
  1304. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."actioncomm_resources as r ON a.id = r.fk_actioncomm";
  1305. $sql .= " AND r.element_type = '".$db->escape($objcon->table_element)."' AND r.fk_element = ".((int) $objcon->id);
  1306. }
  1307. if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur'))) {
  1308. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON a.fk_contact = sp.rowid";
  1309. } elseif (is_object($filterobj) && get_class($filterobj) == 'Dolresource') {
  1310. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."element_resources as er";
  1311. $sql .= " ON er.resource_type = 'dolresource'";
  1312. $sql .= " AND er.element_id = a.id";
  1313. $sql .= " AND er.resource_id = ".((int) $filterobj->id);
  1314. } elseif (is_object($filterobj) && get_class($filterobj) == 'Project') {
  1315. /* Nothing */
  1316. } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') {
  1317. $sql .= ", ".MAIN_DB_PREFIX."adherent as m";
  1318. } elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') {
  1319. $sql .= ", ".MAIN_DB_PREFIX."commande_fournisseur as o";
  1320. } elseif (is_object($filterobj) && get_class($filterobj) == 'Product') {
  1321. $sql .= ", ".MAIN_DB_PREFIX."product as o";
  1322. } elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') {
  1323. $sql .= ", ".MAIN_DB_PREFIX."ticket as o";
  1324. } elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') {
  1325. $sql .= ", ".MAIN_DB_PREFIX."bom_bom as o";
  1326. } elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') {
  1327. $sql .= ", ".MAIN_DB_PREFIX."contrat as o";
  1328. } elseif (is_object($filterobj) && is_array($filterobj->fields) && is_array($filterobj->fields['rowid']) && (is_array($filterobj->fields['ref']) || is_array($filterobj->fields['label'])) && $filterobj->table_element && $filterobj->element) {
  1329. $sql .= ", ".MAIN_DB_PREFIX.$filterobj->table_element." as o";
  1330. }
  1331. $sql .= " WHERE a.entity IN (".getEntity('agenda').")";
  1332. if ($force_filter_contact === false) {
  1333. if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur')) && $filterobj->id) {
  1334. $sql .= " AND a.fk_soc = ".((int) $filterobj->id);
  1335. } elseif (is_object($filterobj) && get_class($filterobj) == 'Dolresource') {
  1336. /* Nothing */
  1337. } elseif (is_object($filterobj) && get_class($filterobj) == 'Project' && $filterobj->id) {
  1338. $sql .= " AND a.fk_project = ".((int) $filterobj->id);
  1339. } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') {
  1340. $sql .= " AND a.fk_element = m.rowid AND a.elementtype = 'member'";
  1341. if ($filterobj->id) {
  1342. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1343. }
  1344. } elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') {
  1345. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'order_supplier'";
  1346. if ($filterobj->id) {
  1347. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1348. }
  1349. } elseif (is_object($filterobj) && get_class($filterobj) == 'Product') {
  1350. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'product'";
  1351. if ($filterobj->id) {
  1352. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1353. }
  1354. } elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') {
  1355. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'ticket'";
  1356. if ($filterobj->id) {
  1357. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1358. }
  1359. } elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') {
  1360. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'bom'";
  1361. if ($filterobj->id) {
  1362. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1363. }
  1364. } elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') {
  1365. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'contract'";
  1366. if ($filterobj->id) {
  1367. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1368. }
  1369. } elseif (is_object($filterobj) && is_array($filterobj->fields) && is_array($filterobj->fields['rowid']) && (is_array($filterobj->fields['ref']) || is_array($filterobj->fields['label'])) && $filterobj->table_element && $filterobj->element) {
  1370. // Generic case
  1371. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = '".$db->escape($filterobj->element).($module ? "@".$module : "")."'";
  1372. if ($filterobj->id) {
  1373. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1374. }
  1375. }
  1376. }
  1377. if (!empty($tms_start) && !empty($tms_end)) {
  1378. $sql .= " AND ((a.datep BETWEEN '".$db->idate($tms_start)."' AND '".$db->idate($tms_end)."') OR (a.datep2 BETWEEN '".$db->idate($tms_start)."' AND '".$db->idate($tms_end)."'))";
  1379. } elseif (empty($tms_start) && !empty($tms_end)) {
  1380. $sql .= " AND ((a.datep <= '".$db->idate($tms_end)."') OR (a.datep2 <= '".$db->idate($tms_end)."'))";
  1381. } elseif (!empty($tms_start) && empty($tms_end)) {
  1382. $sql .= " AND ((a.datep >= '".$db->idate($tms_start)."') OR (a.datep2 >= '".$db->idate($tms_start)."'))";
  1383. }
  1384. if (is_array($actioncode) && !empty($actioncode)) {
  1385. $sql .= ' AND (';
  1386. foreach ($actioncode as $key => $code) {
  1387. if ($key != 0) {
  1388. $sql .= "OR (";
  1389. }
  1390. if (!empty($code)) {
  1391. addEventTypeSQL($sql, $code);
  1392. }
  1393. if ($key != 0) {
  1394. $sql .= ")";
  1395. }
  1396. }
  1397. $sql .= ')';
  1398. } elseif (!empty($actioncode)) {
  1399. addEventTypeSQL($sql, $actioncode);
  1400. }
  1401. addOtherFilterSQL($sql, $donetodo, $now, $filters);
  1402. if (is_array($actioncode)) {
  1403. foreach ($actioncode as $code) {
  1404. $sql2 = addMailingEventTypeSQL($code, $objcon, $filterobj);
  1405. if (!empty($sql2)) {
  1406. if (!empty($sql)) {
  1407. $sql = $sql." UNION ".$sql2;
  1408. } elseif (empty($sql)) {
  1409. $sql = $sql2;
  1410. }
  1411. break;
  1412. }
  1413. }
  1414. } else {
  1415. $sql2 = addMailingEventTypeSQL($actioncode, $objcon, $filterobj);
  1416. if (!empty($sql) && !empty($sql2)) {
  1417. $sql = $sql." UNION ".$sql2;
  1418. } elseif (empty($sql) && !empty($sql2)) {
  1419. $sql = $sql2;
  1420. }
  1421. }
  1422. }
  1423. //TODO Add limit in nb of results
  1424. if ($sql) {
  1425. $sql .= $db->order($sortfield_new, $sortorder);
  1426. dol_syslog("company.lib::show_actions_done", LOG_DEBUG);
  1427. $resql = $db->query($sql);
  1428. if ($resql) {
  1429. $i = 0;
  1430. $num = $db->num_rows($resql);
  1431. while ($i < $num) {
  1432. $obj = $db->fetch_object($resql);
  1433. if ($obj->type == 'action') {
  1434. $contactaction = new ActionComm($db);
  1435. $contactaction->id = $obj->id;
  1436. $result = $contactaction->fetchResources();
  1437. if ($result < 0) {
  1438. dol_print_error($db);
  1439. setEventMessage("company.lib::show_actions_done Error fetch ressource", 'errors');
  1440. }
  1441. //if ($donetodo == 'todo') $sql.= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
  1442. //elseif ($donetodo == 'done') $sql.= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
  1443. $tododone = '';
  1444. if (($obj->percent >= 0 and $obj->percent < 100) || ($obj->percent == -1 && (!empty($obj->datep) && $obj->datep > $now))) {
  1445. $tododone = 'todo';
  1446. }
  1447. $histo[$numaction] = array(
  1448. 'type'=>$obj->type,
  1449. 'tododone'=>$tododone,
  1450. 'id'=>$obj->id,
  1451. 'datestart'=>$db->jdate($obj->dp),
  1452. 'dateend'=>$db->jdate($obj->dp2),
  1453. 'note'=>$obj->label,
  1454. 'percent'=>$obj->percent,
  1455. 'userid'=>$obj->user_id,
  1456. 'login'=>$obj->user_login,
  1457. 'userfirstname'=>$obj->user_firstname,
  1458. 'userlastname'=>$obj->user_lastname,
  1459. 'userphoto'=>$obj->user_photo,
  1460. 'contact_id'=>$obj->fk_contact,
  1461. 'socpeopleassigned' => $contactaction->socpeopleassigned,
  1462. 'lastname' => empty($obj->lastname) ? '' : $obj->lastname,
  1463. 'firstname' => empty($obj->firstname) ? '' : $obj->firstname,
  1464. 'fk_element'=>$obj->fk_element,
  1465. 'elementtype'=>$obj->elementtype,
  1466. // Type of event
  1467. 'acode'=>$obj->acode,
  1468. 'alabel'=>$obj->alabel,
  1469. 'libelle'=>$obj->alabel, // deprecated
  1470. 'apicto'=>$obj->apicto
  1471. );
  1472. } else {
  1473. $histo[$numaction] = array(
  1474. 'type'=>$obj->type,
  1475. 'tododone'=>'done',
  1476. 'id'=>$obj->id,
  1477. 'datestart'=>$db->jdate($obj->dp),
  1478. 'dateend'=>$db->jdate($obj->dp2),
  1479. 'note'=>$obj->label,
  1480. 'percent'=>$obj->percent,
  1481. 'acode'=>$obj->acode,
  1482. 'userid'=>$obj->user_id,
  1483. 'login'=>$obj->user_login,
  1484. 'userfirstname'=>$obj->user_firstname,
  1485. 'userlastname'=>$obj->user_lastname,
  1486. 'userphoto'=>$obj->user_photo
  1487. );
  1488. }
  1489. $numaction++;
  1490. $i++;
  1491. }
  1492. } else {
  1493. dol_print_error($db);
  1494. }
  1495. }
  1496. if (isModEnabled('agenda')|| (isModEnabled('mailing') && !empty($objcon->email))) {
  1497. $delay_warning = $conf->global->MAIN_DELAY_ACTIONS_TODO * 24 * 60 * 60;
  1498. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  1499. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  1500. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  1501. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  1502. $formactions = new FormActions($db);
  1503. $actionstatic = new ActionComm($db);
  1504. $userstatic = new User($db);
  1505. $userlinkcache = array();
  1506. $contactstatic = new Contact($db);
  1507. $elementlinkcache = array();
  1508. $out .= '<form name="listactionsfilter" class="listactionsfilter" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1509. $out .= '<input type="hidden" name="token" value="'.newToken().'">';
  1510. if ($objcon && get_class($objcon) == 'Contact' &&
  1511. (is_null($filterobj) || get_class($filterobj) == 'Societe')) {
  1512. $out .= '<input type="hidden" name="id" value="'.$objcon->id.'" />';
  1513. } else {
  1514. $out .= '<input type="hidden" name="id" value="'.$filterobj->id.'" />';
  1515. }
  1516. if ($filterobj && get_class($filterobj) == 'Societe') {
  1517. $out .= '<input type="hidden" name="socid" value="'.$filterobj->id.'" />';
  1518. }
  1519. $out .= "\n";
  1520. $out .= '<div class="div-table-responsive-no-min">';
  1521. $out .= '<table class="noborder centpercent">';
  1522. $out .= '<tr class="liste_titre">';
  1523. if ($donetodo) {
  1524. $out .= '<td class="liste_titre"></td>';
  1525. }
  1526. $out .= '<td class="liste_titre"></td>';
  1527. $out .= '<td class="liste_titre"></td>';
  1528. $out .= '<td class="liste_titre">';
  1529. $out .= $formactions->select_type_actions($actioncode, "actioncode", '', empty($conf->global->AGENDA_USE_EVENT_TYPE) ? 1 : -1, 0, (empty($conf->global->AGENDA_USE_MULTISELECT_TYPE) ? 0 : 1), 1, 'minwidth200');
  1530. $out .= '</td>';
  1531. $out .= '<td class="liste_titre maxwidth100onsmartphone"><input type="text" class="maxwidth100onsmartphone" name="search_agenda_label" value="'.$filters['search_agenda_label'].'"></td>';
  1532. $out .= '<td class="liste_titre center">';
  1533. $out .= $form->selectDateToDate($tms_start, $tms_end, 'dateevent', 1);
  1534. $out .= '</td>';
  1535. $out .= '<td class="liste_titre"></td>';
  1536. $out .= '<td class="liste_titre"></td>';
  1537. $out .= '<td class="liste_titre"></td>';
  1538. // Action column
  1539. $out .= '<td class="liste_titre" align="middle">';
  1540. $searchpicto = $form->showFilterAndCheckAddButtons($massactionbutton ? 1 : 0, 'checkforselect', 1);
  1541. $out .= $searchpicto;
  1542. $out .= '</td>';
  1543. $out .= '</tr>';
  1544. $out .= '<tr class="liste_titre">';
  1545. if ($donetodo) {
  1546. $tmp = '';
  1547. if (get_class($filterobj) == 'Societe') {
  1548. $tmp .= '<a href="'.DOL_URL_ROOT.'/comm/action/list.php?mode=show_list&socid='.$filterobj->id.'&status=done">';
  1549. }
  1550. $tmp .= ($donetodo != 'done' ? $langs->trans("ActionsToDoShort") : '');
  1551. $tmp .= ($donetodo != 'done' && $donetodo != 'todo' ? ' / ' : '');
  1552. $tmp .= ($donetodo != 'todo' ? $langs->trans("ActionsDoneShort") : '');
  1553. //$out.=$langs->trans("ActionsToDoShort").' / '.$langs->trans("ActionsDoneShort");
  1554. if (get_class($filterobj) == 'Societe') {
  1555. $tmp .= '</a>';
  1556. }
  1557. $out .= getTitleFieldOfList($tmp);
  1558. }
  1559. $out .= getTitleFieldOfList("Ref", 0, $_SERVER["PHP_SELF"], 'a.id', '', $param, '', $sortfield, $sortorder);
  1560. $out .= getTitleFieldOfList("Owner");
  1561. $out .= getTitleFieldOfList("Type");
  1562. $out .= getTitleFieldOfList("Label", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  1563. $out .= getTitleFieldOfList("Date", 0, $_SERVER["PHP_SELF"], 'a.datep,a.id', '', $param, '', $sortfield, $sortorder, 'center ');
  1564. $out .= getTitleFieldOfList("RelatedObjects", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  1565. $out .= getTitleFieldOfList("ActionOnContact", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'tdoverflowmax125 ', 0, '', 0);
  1566. $out .= getTitleFieldOfList("Status", 0, $_SERVER["PHP_SELF"], 'a.percent', '', $param, '', $sortfield, $sortorder, 'center ');
  1567. $out .= getTitleFieldOfList('', 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'maxwidthsearch ');
  1568. $out .= '</tr>';
  1569. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/cactioncomm.class.php';
  1570. $caction = new CActionComm($db);
  1571. $arraylist = $caction->liste_array(1, 'code', '', (empty($conf->global->AGENDA_USE_EVENT_TYPE) ? 1 : 0), '', 1);
  1572. foreach ($histo as $key => $value) {
  1573. $actionstatic->fetch($histo[$key]['id']); // TODO Do we need this, we already have a lot of data of line into $histo
  1574. $actionstatic->type_picto = $histo[$key]['apicto'];
  1575. $actionstatic->type_code = $histo[$key]['acode'];
  1576. $out .= '<tr class="oddeven">';
  1577. // Done or todo
  1578. if ($donetodo) {
  1579. $out .= '<td class="nowrap">';
  1580. $out .= '</td>';
  1581. }
  1582. // Ref
  1583. $out .= '<td class="nowraponall">';
  1584. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'mailing') {
  1585. $out .= '<a href="'.DOL_URL_ROOT.'/comm/mailing/card.php?id='.$histo[$key]['id'].'">'.img_object($langs->trans("ShowEMailing"), "email").' ';
  1586. $out .= $histo[$key]['id'];
  1587. $out .= '</a>';
  1588. } else {
  1589. $out .= $actionstatic->getNomUrl(1, -1);
  1590. }
  1591. $out .= '</td>';
  1592. // Author of event
  1593. $out .= '<td class="tdoverflowmax150">';
  1594. //$userstatic->id=$histo[$key]['userid'];
  1595. //$userstatic->login=$histo[$key]['login'];
  1596. //$out.=$userstatic->getLoginUrl(1);
  1597. if ($histo[$key]['userid'] > 0) {
  1598. if (isset($userlinkcache[$histo[$key]['userid']])) {
  1599. $link = $userlinkcache[$histo[$key]['userid']];
  1600. } else {
  1601. $userstatic->fetch($histo[$key]['userid']);
  1602. $link = $userstatic->getNomUrl(-1, '', 0, 0, 16, 0, 'firstelselast', '');
  1603. $userlinkcache[$histo[$key]['userid']] = $link;
  1604. }
  1605. $out .= $link;
  1606. }
  1607. $out .= '</td>';
  1608. // Type
  1609. $labeltype = $actionstatic->type_code;
  1610. if (empty($conf->global->AGENDA_USE_EVENT_TYPE) && empty($arraylist[$labeltype])) {
  1611. $labeltype = 'AC_OTH';
  1612. }
  1613. if ($actionstatic->type_code == 'AC_OTH' && $actionstatic->code == 'TICKET_MSG') {
  1614. $labeltype = $langs->trans("Message");
  1615. } else {
  1616. if (!empty($arraylist[$labeltype])) {
  1617. $labeltype = $arraylist[$labeltype];
  1618. }
  1619. if ($actionstatic->type_code == 'AC_OTH_AUTO' && ($actionstatic->type_code != $actionstatic->code) && $labeltype && !empty($arraylist[$actionstatic->code])) {
  1620. $labeltype .= ' - '.$arraylist[$actionstatic->code]; // Use code in priority on type_code
  1621. }
  1622. }
  1623. $out .= '<td class="tdoverflowmax150" title="'.$labeltype.'">';
  1624. $out .= $actionstatic->getTypePicto();
  1625. $out .= $labeltype;
  1626. $out .= '</td>';
  1627. // Title/Label of event
  1628. $out .= '<td class="tdoverflowmax300"';
  1629. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'action') {
  1630. $transcode = $langs->trans("Action".$histo[$key]['acode']);
  1631. $libelle = ($transcode != "Action".$histo[$key]['acode'] ? $transcode : $histo[$key]['alabel']);
  1632. //$actionstatic->libelle=$libelle;
  1633. $libelle = $histo[$key]['note'];
  1634. $actionstatic->id = $histo[$key]['id'];
  1635. $out .= ' title="'.dol_escape_htmltag($libelle).'">';
  1636. $out .= dol_trunc($libelle, 120);
  1637. }
  1638. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'mailing') {
  1639. $out .= '<a href="'.DOL_URL_ROOT.'/comm/mailing/card.php?id='.$histo[$key]['id'].'">'.img_object($langs->trans("ShowEMailing"), "email").' ';
  1640. $transcode = $langs->trans("Action".$histo[$key]['acode']);
  1641. $libelle = ($transcode != "Action".$histo[$key]['acode'] ? $transcode : 'Send mass mailing');
  1642. $out .= ' title="'.dol_escape_htmltag($libelle).'">';
  1643. $out .= dol_trunc($libelle, 120);
  1644. }
  1645. $out .= '</td>';
  1646. // Date
  1647. $out .= '<td class="center nowrap">';
  1648. $out .= dol_print_date($histo[$key]['datestart'], 'dayhour', 'tzuserrel');
  1649. if ($histo[$key]['dateend'] && $histo[$key]['dateend'] != $histo[$key]['datestart']) {
  1650. $tmpa = dol_getdate($histo[$key]['datestart'], true);
  1651. $tmpb = dol_getdate($histo[$key]['dateend'], true);
  1652. if ($tmpa['mday'] == $tmpb['mday'] && $tmpa['mon'] == $tmpb['mon'] && $tmpa['year'] == $tmpb['year']) {
  1653. $out .= '-'.dol_print_date($histo[$key]['dateend'], 'hour', 'tzuserrel');
  1654. } else {
  1655. $out .= '-'.dol_print_date($histo[$key]['dateend'], 'dayhour', 'tzuserrel');
  1656. }
  1657. }
  1658. $late = 0;
  1659. if ($histo[$key]['percent'] == 0 && $histo[$key]['datestart'] && $histo[$key]['datestart'] < ($now - $delay_warning)) {
  1660. $late = 1;
  1661. }
  1662. if ($histo[$key]['percent'] == 0 && !$histo[$key]['datestart'] && $histo[$key]['dateend'] && $histo[$key]['datestart'] < ($now - $delay_warning)) {
  1663. $late = 1;
  1664. }
  1665. if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && $histo[$key]['dateend'] && $histo[$key]['dateend'] < ($now - $delay_warning)) {
  1666. $late = 1;
  1667. }
  1668. if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && !$histo[$key]['dateend'] && $histo[$key]['datestart'] && $histo[$key]['datestart'] < ($now - $delay_warning)) {
  1669. $late = 1;
  1670. }
  1671. if ($late) {
  1672. $out .= img_warning($langs->trans("Late")).' ';
  1673. }
  1674. $out .= "</td>\n";
  1675. // Title of event
  1676. //$out.='<td>'.dol_trunc($histo[$key]['note'], 40).'</td>';
  1677. // Linked object
  1678. $out .= '<td class="nowraponall">';
  1679. if (isset($histo[$key]['elementtype']) && !empty($histo[$key]['fk_element'])) {
  1680. if (isset($elementlinkcache[$histo[$key]['elementtype']]) && isset($elementlinkcache[$histo[$key]['elementtype']][$histo[$key]['fk_element']])) {
  1681. $link = $elementlinkcache[$histo[$key]['elementtype']][$histo[$key]['fk_element']];
  1682. } else {
  1683. if (!isset($elementlinkcache[$histo[$key]['elementtype']])) {
  1684. $elementlinkcache[$histo[$key]['elementtype']] = array();
  1685. }
  1686. $link = dolGetElementUrl($histo[$key]['fk_element'], $histo[$key]['elementtype'], 1);
  1687. $elementlinkcache[$histo[$key]['elementtype']][$histo[$key]['fk_element']] = $link;
  1688. }
  1689. $out .= $link;
  1690. } else {
  1691. $out .= '&nbsp;';
  1692. }
  1693. $out .= '</td>';
  1694. // Contact(s) for action
  1695. if (isset($histo[$key]['socpeopleassigned']) && is_array($histo[$key]['socpeopleassigned']) && count($histo[$key]['socpeopleassigned']) > 0) {
  1696. $out .= '<td class="valignmiddle">';
  1697. $contact = new Contact($db);
  1698. foreach ($histo[$key]['socpeopleassigned'] as $cid => $value) {
  1699. $result = $contact->fetch($cid);
  1700. if ($result < 0) {
  1701. dol_print_error($db, $contact->error);
  1702. }
  1703. if ($result > 0) {
  1704. $out .= $contact->getNomUrl(-3, '', 10, '', -1, 0, 'paddingright');
  1705. if (isset($histo[$key]['acode']) && $histo[$key]['acode'] == 'AC_TEL') {
  1706. if (!empty($contact->phone_pro)) {
  1707. $out .= '('.dol_print_phone($contact->phone_pro).')';
  1708. }
  1709. }
  1710. $out .= '<div class="paddingright"></div>';
  1711. }
  1712. }
  1713. $out .= '</td>';
  1714. } elseif (empty($objcon->id) && isset($histo[$key]['contact_id']) && $histo[$key]['contact_id'] > 0) {
  1715. $contactstatic->lastname = $histo[$key]['lastname'];
  1716. $contactstatic->firstname = $histo[$key]['firstname'];
  1717. $contactstatic->id = $histo[$key]['contact_id'];
  1718. $contactstatic->photo = $histo[$key]['contact_photo'];
  1719. $out .= '<td width="120">'.$contactstatic->getNomUrl(-1, '', 10).'</td>';
  1720. } else {
  1721. $out .= '<td>&nbsp;</td>';
  1722. }
  1723. // Status
  1724. $out .= '<td class="nowrap center">'.$actionstatic->LibStatut($histo[$key]['percent'], 2, 0, $histo[$key]['datestart']).'</td>';
  1725. // Actions
  1726. $out .= '<td></td>';
  1727. $out .= "</tr>\n";
  1728. $i++;
  1729. }
  1730. $out .= "</table>\n";
  1731. $out .= "</div>\n";
  1732. $out .= '</form>';
  1733. }
  1734. if ($noprint) {
  1735. return $out;
  1736. } else {
  1737. print $out;
  1738. }
  1739. }
  1740. /**
  1741. * Show html area for list of subsidiaries
  1742. *
  1743. * @param Conf $conf Object conf
  1744. * @param Translate $langs Object langs
  1745. * @param DoliDB $db Database handler
  1746. * @param Societe $object Third party object
  1747. * @return int
  1748. */
  1749. function show_subsidiaries($conf, $langs, $db, $object)
  1750. {
  1751. global $user;
  1752. $i = -1;
  1753. $sql = "SELECT s.rowid, s.client, s.fournisseur, s.nom as name, s.name_alias, s.email, s.address, s.zip, s.town, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur, s.canvas";
  1754. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  1755. $sql .= " WHERE s.parent = ".((int) $object->id);
  1756. $sql .= " AND s.entity IN (".getEntity('societe').")";
  1757. $sql .= " ORDER BY s.nom";
  1758. $result = $db->query($sql);
  1759. $num = $db->num_rows($result);
  1760. if ($num) {
  1761. $socstatic = new Societe($db);
  1762. print load_fiche_titre($langs->trans("Subsidiaries"), '', '');
  1763. print "\n".'<div class="div-table-responsive-no-min">'."\n";
  1764. print '<table class="noborder centpercent">'."\n";
  1765. print '<tr class="liste_titre"><td>'.$langs->trans("Company").'</td>';
  1766. print '<td>'.$langs->trans("Address").'</td><td>'.$langs->trans("Zip").'</td>';
  1767. print '<td>'.$langs->trans("Town").'</td><td>'.$langs->trans("CustomerCode").'</td>';
  1768. print "<td>&nbsp;</td>";
  1769. print "</tr>";
  1770. $i = 0;
  1771. while ($i < $num) {
  1772. $obj = $db->fetch_object($result);
  1773. $socstatic->id = $obj->rowid;
  1774. $socstatic->name = $obj->name;
  1775. $socstatic->name_alias = $obj->name_alias;
  1776. $socstatic->email = $obj->email;
  1777. $socstatic->code_client = $obj->code_client;
  1778. $socstatic->code_fournisseur = $obj->code_client;
  1779. $socstatic->code_compta = $obj->code_compta;
  1780. $socstatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
  1781. $socstatic->email = $obj->email;
  1782. $socstatic->canvas = $obj->canvas;
  1783. $socstatic->client = $obj->client;
  1784. $socstatic->fournisseur = $obj->fournisseur;
  1785. print '<tr class="oddeven">';
  1786. print '<td class="tdoverflowmax150">';
  1787. print $socstatic->getNomUrl(1);
  1788. print '</td>';
  1789. print '<td class="tdoverflowmax400" title="'.dol_escape_htmltag($obj->address).'">'.dol_escape_htmltag($obj->address).'</td>';
  1790. print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($obj->zip).'">'.$obj->zip.'</td>';
  1791. print '<td class="tdoverflowmax200" title="'.dol_escape_htmltag($obj->town).'">'.$obj->town.'</td>';
  1792. print '<td class="tdoverflowmax200" title="'.dol_escape_htmltag($obj->code_client).'">'.$obj->code_client.'</td>';
  1793. print '<td class="center">';
  1794. print '<a class="editfielda" href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $obj->rowid).'&action=edit&token='.newToken().'">';
  1795. print img_edit();
  1796. print '</a></td>';
  1797. print "</tr>\n";
  1798. $i++;
  1799. }
  1800. print "\n</table>\n";
  1801. print '</div>'."\n";
  1802. }
  1803. print "<br>\n";
  1804. return $i;
  1805. }
  1806. /**
  1807. * Add Event Type SQL
  1808. *
  1809. * @param string $sql $sql modified
  1810. * @param string $actioncode Action code
  1811. * @param string $sqlANDOR "AND", "OR" or "" sql condition
  1812. * @return string sql request
  1813. */
  1814. function addEventTypeSQL(&$sql, $actioncode, $sqlANDOR = "AND")
  1815. {
  1816. global $conf, $db;
  1817. // Condition on actioncode
  1818. if (empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
  1819. if ($actioncode == 'AC_NON_AUTO') {
  1820. $sql .= " $sqlANDOR c.type != 'systemauto'";
  1821. } elseif ($actioncode == 'AC_ALL_AUTO') {
  1822. $sql .= " $sqlANDOR c.type = 'systemauto'";
  1823. } else {
  1824. if ($actioncode == 'AC_OTH') {
  1825. $sql .= " $sqlANDOR c.type != 'systemauto'";
  1826. } elseif ($actioncode == 'AC_OTH_AUTO') {
  1827. $sql .= " $sqlANDOR c.type = 'systemauto'";
  1828. }
  1829. }
  1830. } else {
  1831. if ($actioncode == 'AC_NON_AUTO') {
  1832. $sql .= " $sqlANDOR c.type != 'systemauto'";
  1833. } elseif ($actioncode == 'AC_ALL_AUTO') {
  1834. $sql .= " $sqlANDOR c.type = 'systemauto'";
  1835. } else {
  1836. $sql .= " $sqlANDOR c.code = '".$db->escape($actioncode)."'";
  1837. }
  1838. }
  1839. return $sql;
  1840. }
  1841. /**
  1842. * Add Event Type SQL
  1843. *
  1844. * @param string $sql $sql modified
  1845. * @param string $donetodo donetodo
  1846. * @param string $now now
  1847. * @param string $filters array
  1848. * @return string sql request
  1849. */
  1850. function addOtherFilterSQL(&$sql, $donetodo, $now, $filters)
  1851. {
  1852. global $conf, $db;
  1853. // Condition on actioncode
  1854. if ($donetodo == 'todo') {
  1855. $sql .= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
  1856. } elseif ($donetodo == 'done') {
  1857. $sql .= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
  1858. }
  1859. if (is_array($filters) && $filters['search_agenda_label']) {
  1860. $sql .= natural_search('a.label', $filters['search_agenda_label']);
  1861. }
  1862. return $sql;
  1863. }
  1864. /**
  1865. * Add Mailing Event Type SQL
  1866. *
  1867. * @param string $actioncode Action code
  1868. * @param Object $objcon objcon
  1869. * @param Object $filterobj filterobj
  1870. * @return string
  1871. */
  1872. function addMailingEventTypeSQL($actioncode, $objcon, $filterobj)
  1873. {
  1874. global $conf, $langs, $db;
  1875. // Add also event from emailings. TODO This should be replaced by an automatic event ? May be it's too much for very large emailing.
  1876. if (isModEnabled('mailing') && !empty($objcon->email)
  1877. && (empty($actioncode) || $actioncode == 'AC_OTH_AUTO' || $actioncode == 'AC_EMAILING')) {
  1878. $langs->load("mails");
  1879. $sql2 = "SELECT m.rowid as id, m.titre as label, mc.date_envoi as dp, mc.date_envoi as dp2, '100' as percent, 'mailing' as type";
  1880. $sql2 .= ", null as fk_element, '' as elementtype, null as contact_id";
  1881. $sql2 .= ", 'AC_EMAILING' as acode, '' as alabel, '' as apicto";
  1882. $sql2 .= ", u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname"; // User that valid action
  1883. if (is_object($filterobj) && get_class($filterobj) == 'Societe') {
  1884. $sql2 .= ", '' as lastname, '' as firstname";
  1885. } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') {
  1886. $sql2 .= ", '' as lastname, '' as firstname";
  1887. } elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') {
  1888. $sql2 .= ", '' as ref";
  1889. } elseif (is_object($filterobj) && get_class($filterobj) == 'Product') {
  1890. $sql2 .= ", '' as ref";
  1891. } elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') {
  1892. $sql2 .= ", '' as ref";
  1893. }
  1894. $sql2 .= " FROM ".MAIN_DB_PREFIX."mailing as m, ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."user as u";
  1895. $sql2 .= " WHERE mc.email = '".$db->escape($objcon->email)."'"; // Search is done on email.
  1896. $sql2 .= " AND mc.statut = 1";
  1897. $sql2 .= " AND u.rowid = m.fk_user_valid";
  1898. $sql2 .= " AND mc.fk_mailing=m.rowid";
  1899. return $sql2;
  1900. }
  1901. }
  1902. // Added by MMI Mathieu Moulin iProspective
  1903. function tel_international($value) {
  1904. $value = str_replace([' ', '.', '-'], ['', '', ''], $value);
  1905. if (substr($value, 0, 2)=='00')
  1906. $value = '+'.substr($value, 2);
  1907. if (substr($value, 0, 1)=='0')
  1908. $value = '+33'.substr($value, 1);
  1909. return $value;
  1910. }