company.lib.php 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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->THIRDPARTIES_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("bills");
  187. $title = $langs->trans("PaymentModes");
  188. if (!empty($conf->stripe->enabled)) {
  189. //$langs->load("stripe");
  190. //$title = $langs->trans("BankAccountsAndGateways");
  191. $servicestatus = 0;
  192. if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) {
  193. $servicestatus = 1;
  194. }
  195. include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
  196. $societeaccount = new SocieteAccount($db);
  197. $stripecu = $societeaccount->getCustomerAccount($object->id, 'stripe', $servicestatus); // Get thirdparty cu_...
  198. if ($stripecu) {
  199. $foundonexternalonlinesystem++;
  200. }
  201. }
  202. $sql = "SELECT COUNT(n.rowid) as nb";
  203. $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib as n";
  204. $sql .= " WHERE n.fk_soc = ".((int) $object->id);
  205. if (empty($conf->stripe->enabled)) {
  206. $sql .= " AND n.stripe_card_ref IS NULL";
  207. } else {
  208. $sql .= " AND (n.stripe_card_ref IS NULL OR (n.stripe_card_ref IS NOT NULL AND n.status = ".((int) $servicestatus)."))";
  209. }
  210. $resql = $db->query($sql);
  211. if ($resql) {
  212. $obj = $db->fetch_object($resql);
  213. $nbBankAccount = $obj->nb;
  214. } else {
  215. dol_print_error($db);
  216. }
  217. //if (! empty($conf->stripe->enabled) && $nbBankAccount > 0) $nbBankAccount = '...'; // No way to know exact number
  218. $head[$h][0] = DOL_URL_ROOT.'/societe/paymentmodes.php?socid='.urlencode($object->id);
  219. $head[$h][1] = $title;
  220. if ($foundonexternalonlinesystem) {
  221. $head[$h][1] .= '<span class="badge marginleftonlyshort">...</span>';
  222. } elseif ($nbBankAccount > 0) {
  223. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbBankAccount.'</span>';
  224. }
  225. $head[$h][2] = 'rib';
  226. $h++;
  227. }
  228. if (!empty($conf->website->enabled) && (!empty($conf->global->WEBSITE_USE_WEBSITE_ACCOUNTS)) && (!empty($user->rights->societe->lire))) {
  229. $head[$h][0] = DOL_URL_ROOT.'/societe/website.php?id='.urlencode($object->id);
  230. $head[$h][1] = $langs->trans("WebSiteAccounts");
  231. $nbNote = 0;
  232. $sql = "SELECT COUNT(n.rowid) as nb";
  233. $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as n";
  234. $sql .= " WHERE fk_soc = ".((int) $object->id).' AND fk_website > 0';
  235. $resql = $db->query($sql);
  236. if ($resql) {
  237. $obj = $db->fetch_object($resql);
  238. $nbNote = $obj->nb;
  239. } else {
  240. dol_print_error($db);
  241. }
  242. if ($nbNote > 0) {
  243. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  244. }
  245. $head[$h][2] = 'website';
  246. $h++;
  247. }
  248. if (getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR', 'thirdparty') == 'thirdparty') {
  249. if (!empty($user->rights->partnership->read)) {
  250. $langs->load("partnership");
  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 thirdparty 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 https://ec.europa.eu/taxation_customs/territorial-status-eu-countries-and-certain-territories_en
  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"><span class="opacitymedium">'.$langs->trans("None").'</span></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', 'aZ09comma');
  812. $sortorder = GETPOST('sortorder', 'aZ09comma');
  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&token='.newToken().'&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']) && $filterobj->table_element && $filterobj->element) {
  1275. if (!empty($filterobj->fields['ref'])) {
  1276. $sql .= ", o.ref";
  1277. } elseif (!empty($filterobj->fields['label'])) {
  1278. $sql .= ", o.label";
  1279. }
  1280. }
  1281. $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
  1282. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_action";
  1283. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action = c.id";
  1284. $force_filter_contact = false;
  1285. if (is_object($objcon) && $objcon->id > 0) {
  1286. $force_filter_contact = true;
  1287. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."actioncomm_resources as r ON a.id = r.fk_actioncomm";
  1288. $sql .= " AND r.element_type = '".$db->escape($objcon->table_element)."' AND r.fk_element = ".((int) $objcon->id);
  1289. }
  1290. if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur'))) {
  1291. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON a.fk_contact = sp.rowid";
  1292. } elseif (is_object($filterobj) && get_class($filterobj) == 'Dolresource') {
  1293. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."element_resources as er";
  1294. $sql .= " ON er.resource_type = 'dolresource'";
  1295. $sql .= " AND er.element_id = a.id";
  1296. $sql .= " AND er.resource_id = ".((int) $filterobj->id);
  1297. } elseif (is_object($filterobj) && get_class($filterobj) == 'Project') {
  1298. /* Nothing */
  1299. } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') {
  1300. $sql .= ", ".MAIN_DB_PREFIX."adherent as m";
  1301. } elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') {
  1302. $sql .= ", ".MAIN_DB_PREFIX."commande_fournisseur as o";
  1303. } elseif (is_object($filterobj) && get_class($filterobj) == 'Product') {
  1304. $sql .= ", ".MAIN_DB_PREFIX."product as o";
  1305. } elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') {
  1306. $sql .= ", ".MAIN_DB_PREFIX."ticket as o";
  1307. } elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') {
  1308. $sql .= ", ".MAIN_DB_PREFIX."bom_bom as o";
  1309. } elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') {
  1310. $sql .= ", ".MAIN_DB_PREFIX."contrat as o";
  1311. } elseif (is_object($filterobj) && is_array($filterobj->fields) && is_array($filterobj->fields['rowid']) && (is_array($filterobj->fields['ref']) || is_array($filterobj->fields['label'])) && $filterobj->table_element && $filterobj->element) {
  1312. $sql .= ", ".MAIN_DB_PREFIX.$filterobj->table_element." as o";
  1313. }
  1314. $sql .= " WHERE a.entity IN (".getEntity('agenda').")";
  1315. if ($force_filter_contact === false) {
  1316. if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur')) && $filterobj->id) {
  1317. $sql .= " AND a.fk_soc = ".((int) $filterobj->id);
  1318. } elseif (is_object($filterobj) && get_class($filterobj) == 'Dolresource') {
  1319. /* Nothing */
  1320. } elseif (is_object($filterobj) && get_class($filterobj) == 'Project' && $filterobj->id) {
  1321. $sql .= " AND a.fk_project = ".((int) $filterobj->id);
  1322. } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') {
  1323. $sql .= " AND a.fk_element = m.rowid AND a.elementtype = 'member'";
  1324. if ($filterobj->id) {
  1325. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1326. }
  1327. } elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') {
  1328. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'order_supplier'";
  1329. if ($filterobj->id) {
  1330. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1331. }
  1332. } elseif (is_object($filterobj) && get_class($filterobj) == 'Product') {
  1333. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'product'";
  1334. if ($filterobj->id) {
  1335. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1336. }
  1337. } elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') {
  1338. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'ticket'";
  1339. if ($filterobj->id) {
  1340. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1341. }
  1342. } elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') {
  1343. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'bom'";
  1344. if ($filterobj->id) {
  1345. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1346. }
  1347. } elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') {
  1348. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'contract'";
  1349. if ($filterobj->id) {
  1350. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1351. }
  1352. } elseif (is_object($filterobj) && is_array($filterobj->fields) && is_array($filterobj->fields['rowid']) && (is_array($filterobj->fields['ref']) || is_array($filterobj->fields['label'])) && $filterobj->table_element && $filterobj->element) {
  1353. // Generic case
  1354. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = '".$db->escape($filterobj->element).($module ? "@".$module : "")."'";
  1355. if ($filterobj->id) {
  1356. $sql .= " AND a.fk_element = ".((int) $filterobj->id);
  1357. }
  1358. }
  1359. }
  1360. if (!empty($tms_start) && !empty($tms_end)) {
  1361. $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)."'))";
  1362. } elseif (empty($tms_start) && !empty($tms_end)) {
  1363. $sql .= " AND ((a.datep <= '".$db->idate($tms_end)."') OR (a.datep2 <= '".$db->idate($tms_end)."'))";
  1364. } elseif (!empty($tms_start) && empty($tms_end)) {
  1365. $sql .= " AND ((a.datep >= '".$db->idate($tms_start)."') OR (a.datep2 >= '".$db->idate($tms_start)."'))";
  1366. }
  1367. if (is_array($actioncode) && !empty($actioncode)) {
  1368. $sql .= ' AND (';
  1369. foreach ($actioncode as $key => $code) {
  1370. if ($key != 0) {
  1371. $sql .= "OR (";
  1372. }
  1373. if (!empty($code)) {
  1374. addEventTypeSQL($sql, $code);
  1375. }
  1376. if ($key != 0) {
  1377. $sql .= ")";
  1378. }
  1379. }
  1380. $sql .= ')';
  1381. } elseif (!empty($actioncode)) {
  1382. addEventTypeSQL($sql, $actioncode);
  1383. }
  1384. addOtherFilterSQL($sql, $donetodo, $now, $filters);
  1385. if (is_array($actioncode)) {
  1386. foreach ($actioncode as $code) {
  1387. $sql2 = addMailingEventTypeSQL($code, $objcon, $filterobj);
  1388. if (!empty($sql2)) {
  1389. if (!empty($sql)) {
  1390. $sql = $sql." UNION ".$sql2;
  1391. } elseif (empty($sql)) {
  1392. $sql = $sql2;
  1393. }
  1394. break;
  1395. }
  1396. }
  1397. } else {
  1398. $sql2 = addMailingEventTypeSQL($actioncode, $objcon, $filterobj);
  1399. if (!empty($sql) && !empty($sql2)) {
  1400. $sql = $sql." UNION ".$sql2;
  1401. } elseif (empty($sql) && !empty($sql2)) {
  1402. $sql = $sql2;
  1403. }
  1404. }
  1405. }
  1406. //TODO Add limit in nb of results
  1407. if ($sql) {
  1408. $sql .= $db->order($sortfield_new, $sortorder);
  1409. dol_syslog("company.lib::show_actions_done", LOG_DEBUG);
  1410. $resql = $db->query($sql);
  1411. if ($resql) {
  1412. $i = 0;
  1413. $num = $db->num_rows($resql);
  1414. while ($i < $num) {
  1415. $obj = $db->fetch_object($resql);
  1416. if ($obj->type == 'action') {
  1417. $contactaction = new ActionComm($db);
  1418. $contactaction->id = $obj->id;
  1419. $result = $contactaction->fetchResources();
  1420. if ($result < 0) {
  1421. dol_print_error($db);
  1422. setEventMessage("company.lib::show_actions_done Error fetch ressource", 'errors');
  1423. }
  1424. //if ($donetodo == 'todo') $sql.= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
  1425. //elseif ($donetodo == 'done') $sql.= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
  1426. $tododone = '';
  1427. if (($obj->percent >= 0 and $obj->percent < 100) || ($obj->percent == -1 && (!empty($obj->datep) && $obj->datep > $now))) {
  1428. $tododone = 'todo';
  1429. }
  1430. $histo[$numaction] = array(
  1431. 'type'=>$obj->type,
  1432. 'tododone'=>$tododone,
  1433. 'id'=>$obj->id,
  1434. 'datestart'=>$db->jdate($obj->dp),
  1435. 'dateend'=>$db->jdate($obj->dp2),
  1436. 'note'=>$obj->label,
  1437. 'percent'=>$obj->percent,
  1438. 'userid'=>$obj->user_id,
  1439. 'login'=>$obj->user_login,
  1440. 'userfirstname'=>$obj->user_firstname,
  1441. 'userlastname'=>$obj->user_lastname,
  1442. 'userphoto'=>$obj->user_photo,
  1443. 'contact_id'=>$obj->fk_contact,
  1444. 'socpeopleassigned' => $contactaction->socpeopleassigned,
  1445. 'lastname' => empty($obj->lastname) ? '' : $obj->lastname,
  1446. 'firstname' => empty($obj->firstname) ? '' : $obj->firstname,
  1447. 'fk_element'=>$obj->fk_element,
  1448. 'elementtype'=>$obj->elementtype,
  1449. // Type of event
  1450. 'acode'=>$obj->acode,
  1451. 'alabel'=>$obj->alabel,
  1452. 'libelle'=>$obj->alabel, // deprecated
  1453. 'apicto'=>$obj->apicto
  1454. );
  1455. } else {
  1456. $histo[$numaction] = array(
  1457. 'type'=>$obj->type,
  1458. 'tododone'=>'done',
  1459. 'id'=>$obj->id,
  1460. 'datestart'=>$db->jdate($obj->dp),
  1461. 'dateend'=>$db->jdate($obj->dp2),
  1462. 'note'=>$obj->label,
  1463. 'percent'=>$obj->percent,
  1464. 'acode'=>$obj->acode,
  1465. 'userid'=>$obj->user_id,
  1466. 'login'=>$obj->user_login,
  1467. 'userfirstname'=>$obj->user_firstname,
  1468. 'userlastname'=>$obj->user_lastname,
  1469. 'userphoto'=>$obj->user_photo
  1470. );
  1471. }
  1472. $numaction++;
  1473. $i++;
  1474. }
  1475. } else {
  1476. dol_print_error($db);
  1477. }
  1478. }
  1479. if (!empty($conf->agenda->enabled) || (!empty($conf->mailing->enabled) && !empty($objcon->email))) {
  1480. $delay_warning = $conf->global->MAIN_DELAY_ACTIONS_TODO * 24 * 60 * 60;
  1481. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  1482. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  1483. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  1484. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  1485. $formactions = new FormActions($db);
  1486. $actionstatic = new ActionComm($db);
  1487. $userstatic = new User($db);
  1488. $userlinkcache = array();
  1489. $contactstatic = new Contact($db);
  1490. $elementlinkcache = array();
  1491. $out .= '<form name="listactionsfilter" class="listactionsfilter" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1492. $out .= '<input type="hidden" name="token" value="'.newToken().'">';
  1493. if ($objcon && get_class($objcon) == 'Contact' &&
  1494. (is_null($filterobj) || get_class($filterobj) == 'Societe')) {
  1495. $out .= '<input type="hidden" name="id" value="'.$objcon->id.'" />';
  1496. } else {
  1497. $out .= '<input type="hidden" name="id" value="'.$filterobj->id.'" />';
  1498. }
  1499. if ($filterobj && get_class($filterobj) == 'Societe') {
  1500. $out .= '<input type="hidden" name="socid" value="'.$filterobj->id.'" />';
  1501. }
  1502. $out .= "\n";
  1503. $out .= '<div class="div-table-responsive-no-min">';
  1504. $out .= '<table class="noborder centpercent">';
  1505. $out .= '<tr class="liste_titre">';
  1506. if ($donetodo) {
  1507. $out .= '<td class="liste_titre"></td>';
  1508. }
  1509. $out .= '<td class="liste_titre"></td>';
  1510. $out .= '<td class="liste_titre"></td>';
  1511. $out .= '<td class="liste_titre">';
  1512. $out .= $formactions->select_type_actions($actioncode, "actioncode", '', empty($conf->global->AGENDA_USE_EVENT_TYPE) ? 1 : -1, 0, (empty($conf->global->AGENDA_USE_MULTISELECT_TYPE) ? 0 : 1), 1, 'minwidth200');
  1513. $out .= '</td>';
  1514. $out .= '<td class="liste_titre maxwidth100onsmartphone"><input type="text" class="maxwidth100onsmartphone" name="search_agenda_label" value="'.$filters['search_agenda_label'].'"></td>';
  1515. $out .= '<td class="liste_titre center">';
  1516. $out .= $form->selectDateToDate($tms_start, $tms_end, 'dateevent', 1);
  1517. $out .= '</td>';
  1518. $out .= '<td class="liste_titre"></td>';
  1519. $out .= '<td class="liste_titre"></td>';
  1520. $out .= '<td class="liste_titre"></td>';
  1521. // Action column
  1522. $out .= '<td class="liste_titre" align="middle">';
  1523. $searchpicto = $form->showFilterAndCheckAddButtons($massactionbutton ? 1 : 0, 'checkforselect', 1);
  1524. $out .= $searchpicto;
  1525. $out .= '</td>';
  1526. $out .= '</tr>';
  1527. $out .= '<tr class="liste_titre">';
  1528. if ($donetodo) {
  1529. $tmp = '';
  1530. if (get_class($filterobj) == 'Societe') {
  1531. $tmp .= '<a href="'.DOL_URL_ROOT.'/comm/action/list.php?mode=show_list&socid='.$filterobj->id.'&status=done">';
  1532. }
  1533. $tmp .= ($donetodo != 'done' ? $langs->trans("ActionsToDoShort") : '');
  1534. $tmp .= ($donetodo != 'done' && $donetodo != 'todo' ? ' / ' : '');
  1535. $tmp .= ($donetodo != 'todo' ? $langs->trans("ActionsDoneShort") : '');
  1536. //$out.=$langs->trans("ActionsToDoShort").' / '.$langs->trans("ActionsDoneShort");
  1537. if (get_class($filterobj) == 'Societe') {
  1538. $tmp .= '</a>';
  1539. }
  1540. $out .= getTitleFieldOfList($tmp);
  1541. }
  1542. $out .= getTitleFieldOfList("Ref", 0, $_SERVER["PHP_SELF"], 'a.id', '', $param, '', $sortfield, $sortorder);
  1543. $out .= getTitleFieldOfList("Owner");
  1544. $out .= getTitleFieldOfList("Type");
  1545. $out .= getTitleFieldOfList("Label", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  1546. $out .= getTitleFieldOfList("Date", 0, $_SERVER["PHP_SELF"], 'a.datep,a.id', '', $param, '', $sortfield, $sortorder, 'center ');
  1547. $out .= getTitleFieldOfList("RelatedObjects", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  1548. $out .= getTitleFieldOfList("ActionOnContact", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'tdoverflowmax125 ', 0, '', 0);
  1549. $out .= getTitleFieldOfList("Status", 0, $_SERVER["PHP_SELF"], 'a.percent', '', $param, '', $sortfield, $sortorder, 'center ');
  1550. $out .= getTitleFieldOfList('', 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'maxwidthsearch ');
  1551. $out .= '</tr>';
  1552. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/cactioncomm.class.php';
  1553. $caction = new CActionComm($db);
  1554. $arraylist = $caction->liste_array(1, 'code', '', (empty($conf->global->AGENDA_USE_EVENT_TYPE) ? 1 : 0), '', 1);
  1555. foreach ($histo as $key => $value) {
  1556. $actionstatic->fetch($histo[$key]['id']); // TODO Do we need this, we already have a lot of data of line into $histo
  1557. $actionstatic->type_picto = $histo[$key]['apicto'];
  1558. $actionstatic->type_code = $histo[$key]['acode'];
  1559. $out .= '<tr class="oddeven">';
  1560. // Done or todo
  1561. if ($donetodo) {
  1562. $out .= '<td class="nowrap">';
  1563. $out .= '</td>';
  1564. }
  1565. // Ref
  1566. $out .= '<td class="nowraponall">';
  1567. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'mailing') {
  1568. $out .= '<a href="'.DOL_URL_ROOT.'/comm/mailing/card.php?id='.$histo[$key]['id'].'">'.img_object($langs->trans("ShowEMailing"), "email").' ';
  1569. $out .= $histo[$key]['id'];
  1570. $out .= '</a>';
  1571. } else {
  1572. $out .= $actionstatic->getNomUrl(1, -1);
  1573. }
  1574. $out .= '</td>';
  1575. // Author of event
  1576. $out .= '<td class="tdoverflowmax150">';
  1577. //$userstatic->id=$histo[$key]['userid'];
  1578. //$userstatic->login=$histo[$key]['login'];
  1579. //$out.=$userstatic->getLoginUrl(1);
  1580. if ($histo[$key]['userid'] > 0) {
  1581. if (isset($userlinkcache[$histo[$key]['userid']])) {
  1582. $link = $userlinkcache[$histo[$key]['userid']];
  1583. } else {
  1584. $userstatic->fetch($histo[$key]['userid']);
  1585. $link = $userstatic->getNomUrl(-1, '', 0, 0, 16, 0, 'firstelselast', '');
  1586. $userlinkcache[$histo[$key]['userid']] = $link;
  1587. }
  1588. $out .= $link;
  1589. }
  1590. $out .= '</td>';
  1591. // Type
  1592. $labeltype = $actionstatic->type_code;
  1593. if (empty($conf->global->AGENDA_USE_EVENT_TYPE) && empty($arraylist[$labeltype])) {
  1594. $labeltype = 'AC_OTH';
  1595. }
  1596. if ($actionstatic->type_code == 'AC_OTH' && $actionstatic->code == 'TICKET_MSG') {
  1597. $labeltype = $langs->trans("Message");
  1598. } else {
  1599. if (!empty($arraylist[$labeltype])) {
  1600. $labeltype = $arraylist[$labeltype];
  1601. }
  1602. if ($actionstatic->type_code == 'AC_OTH_AUTO' && ($actionstatic->type_code != $actionstatic->code) && $labeltype && !empty($arraylist[$actionstatic->code])) {
  1603. $labeltype .= ' - '.$arraylist[$actionstatic->code]; // Use code in priority on type_code
  1604. }
  1605. }
  1606. $out .= '<td class="tdoverflowmax150" title="'.$labeltype.'">';
  1607. $out .= $actionstatic->getTypePicto();
  1608. $out .= $labeltype;
  1609. $out .= '</td>';
  1610. // Title/Label of event
  1611. $out .= '<td class="tdoverflowmax300"';
  1612. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'action') {
  1613. $transcode = $langs->trans("Action".$histo[$key]['acode']);
  1614. $libelle = ($transcode != "Action".$histo[$key]['acode'] ? $transcode : $histo[$key]['alabel']);
  1615. //$actionstatic->libelle=$libelle;
  1616. $libelle = $histo[$key]['note'];
  1617. $actionstatic->id = $histo[$key]['id'];
  1618. $out .= ' title="'.dol_escape_htmltag($libelle).'">';
  1619. $out .= dol_trunc($libelle, 120);
  1620. }
  1621. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'mailing') {
  1622. $out .= '<a href="'.DOL_URL_ROOT.'/comm/mailing/card.php?id='.$histo[$key]['id'].'">'.img_object($langs->trans("ShowEMailing"), "email").' ';
  1623. $transcode = $langs->trans("Action".$histo[$key]['acode']);
  1624. $libelle = ($transcode != "Action".$histo[$key]['acode'] ? $transcode : 'Send mass mailing');
  1625. $out .= ' title="'.dol_escape_htmltag($libelle).'">';
  1626. $out .= dol_trunc($libelle, 120);
  1627. }
  1628. $out .= '</td>';
  1629. // Date
  1630. $out .= '<td class="center nowrap">';
  1631. $out .= dol_print_date($histo[$key]['datestart'], 'dayhour', 'tzuserrel');
  1632. if ($histo[$key]['dateend'] && $histo[$key]['dateend'] != $histo[$key]['datestart']) {
  1633. $tmpa = dol_getdate($histo[$key]['datestart'], true);
  1634. $tmpb = dol_getdate($histo[$key]['dateend'], true);
  1635. if ($tmpa['mday'] == $tmpb['mday'] && $tmpa['mon'] == $tmpb['mon'] && $tmpa['year'] == $tmpb['year']) {
  1636. $out .= '-'.dol_print_date($histo[$key]['dateend'], 'hour', 'tzuserrel');
  1637. } else {
  1638. $out .= '-'.dol_print_date($histo[$key]['dateend'], 'dayhour', 'tzuserrel');
  1639. }
  1640. }
  1641. $late = 0;
  1642. if ($histo[$key]['percent'] == 0 && $histo[$key]['datestart'] && $histo[$key]['datestart'] < ($now - $delay_warning)) {
  1643. $late = 1;
  1644. }
  1645. if ($histo[$key]['percent'] == 0 && !$histo[$key]['datestart'] && $histo[$key]['dateend'] && $histo[$key]['datestart'] < ($now - $delay_warning)) {
  1646. $late = 1;
  1647. }
  1648. if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && $histo[$key]['dateend'] && $histo[$key]['dateend'] < ($now - $delay_warning)) {
  1649. $late = 1;
  1650. }
  1651. if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && !$histo[$key]['dateend'] && $histo[$key]['datestart'] && $histo[$key]['datestart'] < ($now - $delay_warning)) {
  1652. $late = 1;
  1653. }
  1654. if ($late) {
  1655. $out .= img_warning($langs->trans("Late")).' ';
  1656. }
  1657. $out .= "</td>\n";
  1658. // Title of event
  1659. //$out.='<td>'.dol_trunc($histo[$key]['note'], 40).'</td>';
  1660. // Linked object
  1661. $out .= '<td class="nowraponall">';
  1662. if (isset($histo[$key]['elementtype']) && !empty($histo[$key]['fk_element'])) {
  1663. if (isset($elementlinkcache[$histo[$key]['elementtype']]) && isset($elementlinkcache[$histo[$key]['elementtype']][$histo[$key]['fk_element']])) {
  1664. $link = $elementlinkcache[$histo[$key]['elementtype']][$histo[$key]['fk_element']];
  1665. } else {
  1666. if (!isset($elementlinkcache[$histo[$key]['elementtype']])) {
  1667. $elementlinkcache[$histo[$key]['elementtype']] = array();
  1668. }
  1669. $link = dolGetElementUrl($histo[$key]['fk_element'], $histo[$key]['elementtype'], 1);
  1670. $elementlinkcache[$histo[$key]['elementtype']][$histo[$key]['fk_element']] = $link;
  1671. }
  1672. $out .= $link;
  1673. } else {
  1674. $out .= '&nbsp;';
  1675. }
  1676. $out .= '</td>';
  1677. // Contact(s) for action
  1678. if (isset($histo[$key]['socpeopleassigned']) && is_array($histo[$key]['socpeopleassigned']) && count($histo[$key]['socpeopleassigned']) > 0) {
  1679. $out .= '<td class="valignmiddle">';
  1680. $contact = new Contact($db);
  1681. foreach ($histo[$key]['socpeopleassigned'] as $cid => $value) {
  1682. $result = $contact->fetch($cid);
  1683. if ($result < 0) {
  1684. dol_print_error($db, $contact->error);
  1685. }
  1686. if ($result > 0) {
  1687. $out .= $contact->getNomUrl(-3, '', 10, '', -1, 0, 'paddingright');
  1688. if (isset($histo[$key]['acode']) && $histo[$key]['acode'] == 'AC_TEL') {
  1689. if (!empty($contact->phone_pro)) {
  1690. $out .= '('.dol_print_phone($contact->phone_pro).')';
  1691. }
  1692. }
  1693. $out .= '<div class="paddingright"></div>';
  1694. }
  1695. }
  1696. $out .= '</td>';
  1697. } elseif (empty($objcon->id) && isset($histo[$key]['contact_id']) && $histo[$key]['contact_id'] > 0) {
  1698. $contactstatic->lastname = $histo[$key]['lastname'];
  1699. $contactstatic->firstname = $histo[$key]['firstname'];
  1700. $contactstatic->id = $histo[$key]['contact_id'];
  1701. $contactstatic->photo = $histo[$key]['contact_photo'];
  1702. $out .= '<td width="120">'.$contactstatic->getNomUrl(-1, '', 10).'</td>';
  1703. } else {
  1704. $out .= '<td>&nbsp;</td>';
  1705. }
  1706. // Status
  1707. $out .= '<td class="nowrap center">'.$actionstatic->LibStatut($histo[$key]['percent'], 2, 0, $histo[$key]['datestart']).'</td>';
  1708. // Actions
  1709. $out .= '<td></td>';
  1710. $out .= "</tr>\n";
  1711. $i++;
  1712. }
  1713. $out .= "</table>\n";
  1714. $out .= "</div>\n";
  1715. $out .= '</form>';
  1716. }
  1717. if ($noprint) {
  1718. return $out;
  1719. } else {
  1720. print $out;
  1721. }
  1722. }
  1723. /**
  1724. * Show html area for list of subsidiaries
  1725. *
  1726. * @param Conf $conf Object conf
  1727. * @param Translate $langs Object langs
  1728. * @param DoliDB $db Database handler
  1729. * @param Societe $object Third party object
  1730. * @return int
  1731. */
  1732. function show_subsidiaries($conf, $langs, $db, $object)
  1733. {
  1734. global $user;
  1735. $i = -1;
  1736. $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";
  1737. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  1738. $sql .= " WHERE s.parent = ".((int) $object->id);
  1739. $sql .= " AND s.entity IN (".getEntity('societe').")";
  1740. $sql .= " ORDER BY s.nom";
  1741. $result = $db->query($sql);
  1742. $num = $db->num_rows($result);
  1743. if ($num) {
  1744. $socstatic = new Societe($db);
  1745. print load_fiche_titre($langs->trans("Subsidiaries"), '', '');
  1746. print "\n".'<div class="div-table-responsive-no-min">'."\n";
  1747. print '<table class="noborder centpercent">'."\n";
  1748. print '<tr class="liste_titre"><td>'.$langs->trans("Company").'</td>';
  1749. print '<td>'.$langs->trans("Address").'</td><td>'.$langs->trans("Zip").'</td>';
  1750. print '<td>'.$langs->trans("Town").'</td><td>'.$langs->trans("CustomerCode").'</td>';
  1751. print "<td>&nbsp;</td>";
  1752. print "</tr>";
  1753. $i = 0;
  1754. while ($i < $num) {
  1755. $obj = $db->fetch_object($result);
  1756. $socstatic->id = $obj->rowid;
  1757. $socstatic->name = $obj->name;
  1758. $socstatic->name_alias = $obj->name_alias;
  1759. $socstatic->email = $obj->email;
  1760. $socstatic->code_client = $obj->code_client;
  1761. $socstatic->code_fournisseur = $obj->code_client;
  1762. $socstatic->code_compta = $obj->code_compta;
  1763. $socstatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
  1764. $socstatic->email = $obj->email;
  1765. $socstatic->canvas = $obj->canvas;
  1766. $socstatic->client = $obj->client;
  1767. $socstatic->fournisseur = $obj->fournisseur;
  1768. print '<tr class="oddeven">';
  1769. print '<td class="tdoverflowmax150">';
  1770. print $socstatic->getNomUrl(1);
  1771. print '</td>';
  1772. print '<td class="tdoverflowmax400" title="'.dol_escape_htmltag($obj->address).'">'.dol_escape_htmltag($obj->address).'</td>';
  1773. print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($obj->zip).'">'.$obj->zip.'</td>';
  1774. print '<td class="tdoverflowmax200" title="'.dol_escape_htmltag($obj->town).'">'.$obj->town.'</td>';
  1775. print '<td class="tdoverflowmax200" title="'.dol_escape_htmltag($obj->code_client).'">'.$obj->code_client.'</td>';
  1776. print '<td class="center">';
  1777. print '<a class="editfielda" href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $obj->rowid).'&action=edit&token='.newToken().'">';
  1778. print img_edit();
  1779. print '</a></td>';
  1780. print "</tr>\n";
  1781. $i++;
  1782. }
  1783. print "\n</table>\n";
  1784. print '</div>'."\n";
  1785. }
  1786. print "<br>\n";
  1787. return $i;
  1788. }
  1789. /**
  1790. * Add Event Type SQL
  1791. *
  1792. * @param string $sql $sql modified
  1793. * @param string $actioncode Action code
  1794. * @param string $sqlANDOR "AND", "OR" or "" sql condition
  1795. * @return string sql request
  1796. */
  1797. function addEventTypeSQL(&$sql, $actioncode, $sqlANDOR = "AND")
  1798. {
  1799. global $conf, $db;
  1800. // Condition on actioncode
  1801. if (empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
  1802. if ($actioncode == 'AC_NON_AUTO') {
  1803. $sql .= " $sqlANDOR c.type != 'systemauto'";
  1804. } elseif ($actioncode == 'AC_ALL_AUTO') {
  1805. $sql .= " $sqlANDOR c.type = 'systemauto'";
  1806. } else {
  1807. if ($actioncode == 'AC_OTH') {
  1808. $sql .= " $sqlANDOR c.type != 'systemauto'";
  1809. } elseif ($actioncode == 'AC_OTH_AUTO') {
  1810. $sql .= " $sqlANDOR c.type = 'systemauto'";
  1811. }
  1812. }
  1813. } else {
  1814. if ($actioncode == 'AC_NON_AUTO') {
  1815. $sql .= " $sqlANDOR c.type != 'systemauto'";
  1816. } elseif ($actioncode == 'AC_ALL_AUTO') {
  1817. $sql .= " $sqlANDOR c.type = 'systemauto'";
  1818. } else {
  1819. $sql .= " $sqlANDOR c.code = '".$db->escape($actioncode)."'";
  1820. }
  1821. }
  1822. return $sql;
  1823. }
  1824. /**
  1825. * Add Event Type SQL
  1826. *
  1827. * @param string $sql $sql modified
  1828. * @param string $donetodo donetodo
  1829. * @param string $now now
  1830. * @param string $filters array
  1831. * @return string sql request
  1832. */
  1833. function addOtherFilterSQL(&$sql, $donetodo, $now, $filters)
  1834. {
  1835. global $conf, $db;
  1836. // Condition on actioncode
  1837. if ($donetodo == 'todo') {
  1838. $sql .= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
  1839. } elseif ($donetodo == 'done') {
  1840. $sql .= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
  1841. }
  1842. if (is_array($filters) && $filters['search_agenda_label']) {
  1843. $sql .= natural_search('a.label', $filters['search_agenda_label']);
  1844. }
  1845. return $sql;
  1846. }
  1847. /**
  1848. * Add Mailing Event Type SQL
  1849. *
  1850. * @param string $actioncode Action code
  1851. * @param Object $objcon objcon
  1852. * @param Object $filterobj filterobj
  1853. * @return string
  1854. */
  1855. function addMailingEventTypeSQL($actioncode, $objcon, $filterobj)
  1856. {
  1857. global $conf, $langs, $db;
  1858. // Add also event from emailings. TODO This should be replaced by an automatic event ? May be it's too much for very large emailing.
  1859. if (!empty($conf->mailing->enabled) && !empty($objcon->email)
  1860. && (empty($actioncode) || $actioncode == 'AC_OTH_AUTO' || $actioncode == 'AC_EMAILING')) {
  1861. $langs->load("mails");
  1862. $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";
  1863. $sql2 .= ", null as fk_element, '' as elementtype, null as contact_id";
  1864. $sql2 .= ", 'AC_EMAILING' as acode, '' as alabel, '' as apicto";
  1865. $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
  1866. if (is_object($filterobj) && get_class($filterobj) == 'Societe') {
  1867. $sql2 .= ", '' as lastname, '' as firstname";
  1868. } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') {
  1869. $sql2 .= ", '' as lastname, '' as firstname";
  1870. } elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') {
  1871. $sql2 .= ", '' as ref";
  1872. } elseif (is_object($filterobj) && get_class($filterobj) == 'Product') {
  1873. $sql2 .= ", '' as ref";
  1874. } elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') {
  1875. $sql2 .= ", '' as ref";
  1876. }
  1877. $sql2 .= " FROM ".MAIN_DB_PREFIX."mailing as m, ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."user as u";
  1878. $sql2 .= " WHERE mc.email = '".$db->escape($objcon->email)."'"; // Search is done on email.
  1879. $sql2 .= " AND mc.statut = 1";
  1880. $sql2 .= " AND u.rowid = m.fk_user_valid";
  1881. $sql2 .= " AND mc.fk_mailing=m.rowid";
  1882. return $sql2;
  1883. }
  1884. }