company.lib.php 79 KB

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