company.lib.php 85 KB

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