company.lib.php 80 KB

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