company.lib.php 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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@capnetworks.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 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  10. * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
  11. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. * or see http://www.gnu.org/
  26. */
  27. /**
  28. * \file htdocs/core/lib/company.lib.php
  29. * \brief Ensemble de fonctions de base pour le module societe
  30. * \ingroup societe
  31. */
  32. /**
  33. * Return array of tabs to used on pages for third parties cards.
  34. *
  35. * @param Societe $object Object company shown
  36. * @return array Array of tabs
  37. */
  38. function societe_prepare_head(Societe $object)
  39. {
  40. global $db, $langs, $conf, $user;
  41. $h = 0;
  42. $head = array();
  43. $head[$h][0] = DOL_URL_ROOT.'/societe/card.php?socid='.$object->id;
  44. $head[$h][1] = $langs->trans("Card");
  45. $head[$h][2] = 'card';
  46. $h++;
  47. if ($object->client==1 || $object->client==2 || $object->client==3)
  48. {
  49. $head[$h][0] = DOL_URL_ROOT.'/comm/card.php?socid='.$object->id;
  50. $head[$h][1] = '';
  51. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && ($object->client==2 || $object->client==3)) $head[$h][1] .= $langs->trans("Prospect");
  52. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && $object->client==3) $head[$h][1] .= '/';
  53. if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && ($object->client==1 || $object->client==3)) $head[$h][1] .= $langs->trans("Customer");
  54. $head[$h][2] = 'customer';
  55. $h++;
  56. if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
  57. {
  58. $langs->load("products");
  59. // price
  60. $head[$h][0] = DOL_URL_ROOT.'/societe/price.php?socid='.$object->id;
  61. $head[$h][1] = $langs->trans("CustomerPrices");
  62. $head[$h][2] = 'price';
  63. $h++;
  64. }
  65. }
  66. if (! empty($conf->fournisseur->enabled) && $object->fournisseur && ! empty($user->rights->fournisseur->lire))
  67. {
  68. $head[$h][0] = DOL_URL_ROOT.'/fourn/card.php?socid='.$object->id;
  69. $head[$h][1] = $langs->trans("Supplier");
  70. $head[$h][2] = 'supplier';
  71. $h++;
  72. }
  73. if (! empty($conf->global->MAIN_SUPPORT_SHARED_CONTACT_BETWEEN_THIRDPARTIES))
  74. {
  75. $head[$h][0] = DOL_URL_ROOT.'/societe/societecontact.php?socid='.$object->id;
  76. $nbContact = count($object->liste_contact(-1,'internal')) + count($object->liste_contact(-1,'external'));
  77. $head[$h][1] = $langs->trans("ContactsAddresses");
  78. if ($nbContact > 0) $head[$h][1].= ' <span class="badge">'.$nbContact.'</span>';
  79. $head[$h][2] = 'contact';
  80. $h++;
  81. }
  82. if (! empty($conf->projet->enabled) && (!empty($user->rights->projet->lire) ))
  83. {
  84. $head[$h][0] = DOL_URL_ROOT.'/societe/project.php?socid='.$object->id;
  85. $head[$h][1] = $langs->trans("Projects");
  86. $nbNote = 0;
  87. $sql = "SELECT COUNT(n.rowid) as nb";
  88. $sql.= " FROM ".MAIN_DB_PREFIX."projet as n";
  89. $sql.= " WHERE fk_soc = ".$object->id;
  90. $resql=$db->query($sql);
  91. if ($resql)
  92. {
  93. $num = $db->num_rows($resql);
  94. $i = 0;
  95. while ($i < $num)
  96. {
  97. $obj = $db->fetch_object($resql);
  98. $nbNote=$obj->nb;
  99. $i++;
  100. }
  101. }
  102. else {
  103. dol_print_error($db);
  104. }
  105. if ($nbNote > 0) $head[$h][1].= ' <span class="badge">'.$nbNote.'</span>';
  106. $head[$h][2] = 'project';
  107. $h++;
  108. }
  109. // Tab to link resources
  110. if (! empty($conf->resource->enabled) && ! empty($conf->global->RESOURCE_ON_THIRDPARTIES))
  111. {
  112. $head[$h][0] = DOL_URL_ROOT.'/resource/element_resource.php?element=societe&element_id='.$object->id;
  113. $head[$h][1] = $langs->trans("Resources");
  114. $head[$h][2] = 'resources';
  115. $h++;
  116. }
  117. // Related items
  118. if (! empty($conf->commande->enabled) || ! empty($conf->propal->enabled) || ! empty($conf->facture->enabled) || ! empty($conf->fichinter->enabled) || ! empty($conf->fournisseur->enabled))
  119. {
  120. $head[$h][0] = DOL_URL_ROOT.'/societe/consumption.php?socid='.$object->id;
  121. $head[$h][1] = $langs->trans("Referers");
  122. $head[$h][2] = 'consumption';
  123. $h++;
  124. }
  125. // Bank accounrs
  126. if (empty($conf->global->SOCIETE_DISABLE_BANKACCOUNT))
  127. {
  128. $langs->load("banks");
  129. $nbBankAccount=0;
  130. $head[$h][0] = DOL_URL_ROOT .'/societe/rib.php?socid='.$object->id;
  131. $head[$h][1] = $langs->trans("BankAccounts");
  132. $sql = "SELECT COUNT(n.rowid) as nb";
  133. $sql.= " FROM ".MAIN_DB_PREFIX."societe_rib as n";
  134. $sql.= " WHERE fk_soc = ".$object->id;
  135. $resql=$db->query($sql);
  136. if ($resql)
  137. {
  138. $num = $db->num_rows($resql);
  139. $i = 0;
  140. while ($i < $num)
  141. {
  142. $obj = $db->fetch_object($resql);
  143. $nbBankAccount=$obj->nb;
  144. $i++;
  145. }
  146. }
  147. else {
  148. dol_print_error($db);
  149. }
  150. if ($nbBankAccount > 0) $head[$h][1].= ' <span class="badge">'.$nbBankAccount.'</span>';
  151. $head[$h][2] = 'rib';
  152. $h++;
  153. }
  154. // Show more tabs from modules
  155. // Entries must be declared in modules descriptor with line
  156. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  157. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  158. complete_head_from_modules($conf,$langs,$object,$head,$h,'thirdparty');
  159. if ($user->societe_id == 0)
  160. {
  161. // Notifications
  162. if (! empty($conf->notification->enabled))
  163. {
  164. $nbNote = 0;
  165. $sql = "SELECT COUNT(n.rowid) as nb";
  166. $sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n";
  167. $sql.= " WHERE fk_soc = ".$object->id;
  168. $resql=$db->query($sql);
  169. if ($resql)
  170. {
  171. $num = $db->num_rows($resql);
  172. $i = 0;
  173. while ($i < $num)
  174. {
  175. $obj = $db->fetch_object($resql);
  176. $nbNote=$obj->nb;
  177. $i++;
  178. }
  179. }
  180. else {
  181. dol_print_error($db);
  182. }
  183. $head[$h][0] = DOL_URL_ROOT.'/societe/notify/card.php?socid='.$object->id;
  184. $head[$h][1] = $langs->trans("Notifications");
  185. if ($nbNote > 0) $head[$h][1].= ' <span class="badge">'.$nbNote.'</span>';
  186. $head[$h][2] = 'notify';
  187. $h++;
  188. }
  189. // Notes
  190. $nbNote = 0;
  191. if(!empty($object->note_private)) $nbNote++;
  192. if(!empty($object->note_public)) $nbNote++;
  193. $head[$h][0] = DOL_URL_ROOT.'/societe/note.php?id='.$object->id;
  194. $head[$h][1] = $langs->trans("Notes");
  195. if ($nbNote > 0) $head[$h][1].= ' <span class="badge">'.$nbNote.'</span>';
  196. $head[$h][2] = 'note';
  197. $h++;
  198. // Attached files
  199. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  200. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  201. $upload_dir = $conf->societe->multidir_output[$object->entity] . "/" . $object->id ;
  202. $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview.*\.png)$'));
  203. $nbLinks=Link::count($db, $object->element, $object->id);
  204. $head[$h][0] = DOL_URL_ROOT.'/societe/document.php?socid='.$object->id;
  205. $head[$h][1] = $langs->trans("Documents");
  206. if (($nbFiles+$nbLinks) > 0) $head[$h][1].= ' <span class="badge">'.($nbFiles+$nbLinks).'</span>';
  207. $head[$h][2] = 'document';
  208. $h++;
  209. }
  210. $head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id;
  211. $head[$h][1].= $langs->trans("Events");
  212. if (! empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read) ))
  213. {
  214. $head[$h][1].= '/';
  215. $head[$h][1].= $langs->trans("Agenda");
  216. }
  217. $head[$h][2] = 'agenda';
  218. $h++;
  219. // Log
  220. /*$head[$h][0] = DOL_URL_ROOT.'/societe/info.php?socid='.$object->id;
  221. $head[$h][1] = $langs->trans("Info");
  222. $head[$h][2] = 'info';
  223. $h++;*/
  224. complete_head_from_modules($conf,$langs,$object,$head,$h,'thirdparty','remove');
  225. return $head;
  226. }
  227. /**
  228. * Return array of tabs to used on page
  229. *
  230. * @param Object $object Object for tabs
  231. * @return array Array of tabs
  232. */
  233. function societe_prepare_head2($object)
  234. {
  235. global $langs, $conf, $user;
  236. $h = 0;
  237. $head = array();
  238. $head[$h][0] = DOL_URL_ROOT.'/societe/card.php?socid='.$object->id;
  239. $head[$h][1] = $langs->trans("Card");
  240. $head[$h][2] = 'company';
  241. $h++;
  242. $head[$h][0] = 'commerciaux.php?socid='.$object->id;
  243. $head[$h][1] = $langs->trans("SalesRepresentative");
  244. $head[$h][2] = 'salesrepresentative';
  245. $h++;
  246. return $head;
  247. }
  248. /**
  249. * Return array head with list of tabs to view object informations.
  250. *
  251. * @return array head array with tabs
  252. */
  253. function societe_admin_prepare_head()
  254. {
  255. global $langs, $conf, $user;
  256. $h = 0;
  257. $head = array();
  258. $head[$h][0] = DOL_URL_ROOT.'/societe/admin/societe.php';
  259. $head[$h][1] = $langs->trans("Miscellaneous");
  260. $head[$h][2] = 'general';
  261. $h++;
  262. // Show more tabs from modules
  263. // Entries must be declared in modules descriptor with line
  264. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  265. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  266. complete_head_from_modules($conf,$langs,null,$head,$h,'company_admin');
  267. $head[$h][0] = DOL_URL_ROOT.'/societe/admin/societe_extrafields.php';
  268. $head[$h][1] = $langs->trans("ExtraFieldsThirdParties");
  269. $head[$h][2] = 'attributes';
  270. $h++;
  271. $head[$h][0] = DOL_URL_ROOT.'/societe/admin/contact_extrafields.php';
  272. $head[$h][1] = $langs->trans("ExtraFieldsContacts");
  273. $head[$h][2] = 'attributes_contacts';
  274. $h++;
  275. complete_head_from_modules($conf,$langs,null,$head,$h,'company_admin','remove');
  276. return $head;
  277. }
  278. /**
  279. * Return country label, code or id from an id, code or label
  280. *
  281. * @param int $searchkey Id or code of country to search
  282. * @param int $withcode '0'=Return label,
  283. * '1'=Return code + label,
  284. * '2'=Return code from id,
  285. * '3'=Return id from code,
  286. * 'all'=Return array('id'=>,'code'=>,'label'=>)
  287. * @param DoliDB $dbtouse Database handler (using in global way may fail because of conflicts with some autoload features)
  288. * @param Translate $outputlangs Langs object for output translation
  289. * @param int $entconv 0=Return value without entities and not converted to output charset, 1=Ready for html output
  290. * @param int $searchlabel Label of country to search (warning: searching on label is not reliable)
  291. * @return mixed String with country code or translated country name or Array('id','code','label')
  292. */
  293. function getCountry($searchkey,$withcode='',$dbtouse=0,$outputlangs='',$entconv=1,$searchlabel='')
  294. {
  295. global $db,$langs;
  296. $result='';
  297. // Check parameters
  298. if (empty($searchkey) && empty($searchlabel))
  299. {
  300. if ($withcode === 'all') return array('id'=>'','code'=>'','label'=>'');
  301. else return '';
  302. }
  303. if (! is_object($dbtouse)) $dbtouse=$db;
  304. if (! is_object($outputlangs)) $outputlangs=$langs;
  305. $sql = "SELECT rowid, code, label FROM ".MAIN_DB_PREFIX."c_country";
  306. if (is_numeric($searchkey)) $sql.= " WHERE rowid=".$searchkey;
  307. elseif (! empty($searchkey)) $sql.= " WHERE code='".$db->escape($searchkey)."'";
  308. else $sql.= " WHERE label='".$db->escape($searchlabel)."'";
  309. $resql=$dbtouse->query($sql);
  310. if ($resql)
  311. {
  312. $obj = $dbtouse->fetch_object($resql);
  313. if ($obj)
  314. {
  315. $label=((! empty($obj->label) && $obj->label!='-')?$obj->label:'');
  316. if (is_object($outputlangs))
  317. {
  318. $outputlangs->load("dict");
  319. if ($entconv) $label=($obj->code && ($outputlangs->trans("Country".$obj->code)!="Country".$obj->code))?$outputlangs->trans("Country".$obj->code):$label;
  320. else $label=($obj->code && ($outputlangs->transnoentitiesnoconv("Country".$obj->code)!="Country".$obj->code))?$outputlangs->transnoentitiesnoconv("Country".$obj->code):$label;
  321. }
  322. if ($withcode == 1) $result=$label?"$obj->code - $label":"$obj->code";
  323. else if ($withcode == 2) $result=$obj->code;
  324. else if ($withcode == 3) $result=$obj->rowid;
  325. else if ($withcode === 'all') $result=array('id'=>$obj->rowid,'code'=>$obj->code,'label'=>$label);
  326. else $result=$label;
  327. }
  328. else
  329. {
  330. $result='NotDefined';
  331. }
  332. $dbtouse->free($resql);
  333. return $result;
  334. }
  335. else dol_print_error($dbtouse,'');
  336. return 'Error';
  337. }
  338. /**
  339. * Return state translated from an id. Return value is always utf8 encoded and without entities.
  340. *
  341. * @param int $id id of state (province/departement)
  342. * @param int $withcode '0'=Return label,
  343. * '1'=Return string code + label,
  344. * '2'=Return code,
  345. * 'all'=return array('id'=>,'code'=>,'label'=>)
  346. * @param DoliDB $dbtouse Database handler (using in global way may fail because of conflicts with some autoload features)
  347. * @return string String with state code or state name (Return value is always utf8 encoded and without entities)
  348. */
  349. function getState($id,$withcode='',$dbtouse=0)
  350. {
  351. global $db,$langs;
  352. if (! is_object($dbtouse)) $dbtouse=$db;
  353. $sql = "SELECT rowid, code_departement as code, nom as label FROM ".MAIN_DB_PREFIX."c_departements";
  354. $sql.= " WHERE rowid=".$id;
  355. dol_syslog("Company.lib::getState", LOG_DEBUG);
  356. $resql=$dbtouse->query($sql);
  357. if ($resql)
  358. {
  359. $obj = $dbtouse->fetch_object($resql);
  360. if ($obj)
  361. {
  362. $label=$obj->label;
  363. if ($withcode == '1') return $label=$obj->code?"$obj->code":"$obj->code - $label";
  364. else if ($withcode == '2') return $label=$obj->code;
  365. else if ($withcode == 'all') return array('id'=>$obj->rowid,'code'=>$obj->code,'label'=>$label);
  366. else return $label;
  367. }
  368. else
  369. {
  370. return $langs->transnoentitiesnoconv("NotDefined");
  371. }
  372. }
  373. else dol_print_error($dbtouse,'');
  374. }
  375. /**
  376. * Retourne le nom traduit ou code+nom d'une devise
  377. *
  378. * @param string $code_iso Code iso de la devise
  379. * @param int $withcode '1'=affiche code + nom
  380. * @return string Nom traduit de la devise
  381. */
  382. function currency_name($code_iso,$withcode='')
  383. {
  384. global $langs,$db;
  385. // Si il existe une traduction, on peut renvoyer de suite le libelle
  386. if ($langs->trans("Currency".$code_iso)!="Currency".$code_iso)
  387. {
  388. return $langs->trans("Currency".$code_iso);
  389. }
  390. // Si pas de traduction, on consulte le libelle par defaut en table
  391. $sql = "SELECT label FROM ".MAIN_DB_PREFIX."c_currencies";
  392. $sql.= " WHERE code_iso='".$code_iso."'";
  393. $resql=$db->query($sql);
  394. if ($resql)
  395. {
  396. $num = $db->num_rows($resql);
  397. if ($num)
  398. {
  399. $obj = $db->fetch_object($resql);
  400. $label=($obj->label!='-'?$obj->label:'');
  401. if ($withcode) return ($label==$code_iso)?"$code_iso":"$code_iso - $label";
  402. else return $label;
  403. }
  404. else
  405. {
  406. return $code_iso;
  407. }
  408. }
  409. }
  410. /**
  411. * Retourne le nom traduit de la forme juridique
  412. *
  413. * @param string $code Code de la forme juridique
  414. * @return string Nom traduit du pays
  415. */
  416. function getFormeJuridiqueLabel($code)
  417. {
  418. global $db,$langs;
  419. if (! $code) return '';
  420. $sql = "SELECT libelle FROM ".MAIN_DB_PREFIX."c_forme_juridique";
  421. $sql.= " WHERE code='$code'";
  422. dol_syslog("Company.lib::getFormeJuridiqueLabel", LOG_DEBUG);
  423. $resql=$db->query($sql);
  424. if ($resql)
  425. {
  426. $num = $db->num_rows($resql);
  427. if ($num)
  428. {
  429. $obj = $db->fetch_object($resql);
  430. $label=($obj->libelle!='-' ? $obj->libelle : '');
  431. return $label;
  432. }
  433. else
  434. {
  435. return $langs->trans("NotDefined");
  436. }
  437. }
  438. }
  439. /**
  440. * Return if a country is inside the EEC (European Economic Community)
  441. * TODO Add a field into country dictionary.
  442. *
  443. * @param Object $object Object
  444. * @return boolean true = country inside EEC, false = country outside EEC
  445. */
  446. function isInEEC($object)
  447. {
  448. // List of all country codes that are in europe for european vat rules
  449. // List found on http://ec.europa.eu/taxation_customs/common/faq/faq_1179_en.htm#9
  450. $country_code_in_EEC=array(
  451. 'AT', // Austria
  452. 'BE', // Belgium
  453. 'BG', // Bulgaria
  454. 'CY', // Cyprus
  455. 'CZ', // Czech republic
  456. 'DE', // Germany
  457. 'DK', // Danemark
  458. 'EE', // Estonia
  459. 'ES', // Spain
  460. 'FI', // Finland
  461. 'FR', // France
  462. 'GB', // United Kingdom
  463. 'GR', // Greece
  464. 'HR', // Croatia
  465. 'NL', // Holland
  466. 'HU', // Hungary
  467. 'IE', // Ireland
  468. 'IM', // Isle of Man - Included in UK
  469. 'IT', // Italy
  470. 'LT', // Lithuania
  471. 'LU', // Luxembourg
  472. 'LV', // Latvia
  473. 'MC', // Monaco - Included in France
  474. 'MT', // Malta
  475. //'NO', // Norway
  476. 'PL', // Poland
  477. 'PT', // Portugal
  478. 'RO', // Romania
  479. 'SE', // Sweden
  480. 'SK', // Slovakia
  481. 'SI', // Slovenia
  482. 'UK', // United Kingdom
  483. //'CH', // Switzerland - No. Swizerland in not in EEC
  484. );
  485. //print "dd".$this->country_code;
  486. return in_array($object->country_code, $country_code_in_EEC);
  487. }
  488. /**
  489. * Show html area for list of projects
  490. *
  491. * @param Conf $conf Object conf
  492. * @param Translate $langs Object langs
  493. * @param DoliDB $db Database handler
  494. * @param Object $object Third party object
  495. * @param string $backtopage Url to go once contact is created
  496. * @param int $nocreatelink 1=Hide create project link
  497. * @return void
  498. */
  499. function show_projects($conf, $langs, $db, $object, $backtopage='', $nocreatelink=0)
  500. {
  501. global $user;
  502. global $bc;
  503. $i = -1 ;
  504. if (! empty($conf->projet->enabled) && $user->rights->projet->lire)
  505. {
  506. $langs->load("projects");
  507. $buttoncreate='';
  508. if (! empty($conf->projet->enabled) && $user->rights->projet->creer && empty($nocreatelink))
  509. {
  510. //$buttoncreate='<a class="butAction" href="'.DOL_URL_ROOT.'/projet/card.php?socid='.$object->id.'&action=create&amp;backtopage='.urlencode($backtopage).'">'.$langs->trans("AddProject").'</a>';
  511. $buttoncreate='<a class="addnewrecord" href="'.DOL_URL_ROOT.'/projet/card.php?socid='.$object->id.'&amp;action=create&amp;backtopage='.urlencode($backtopage).'">'.$langs->trans("AddProject");
  512. if (empty($conf->dol_optimize_smallscreen)) $buttoncreate.=' '.img_picto($langs->trans("AddProject"),'filenew');
  513. $buttoncreate.='</a>'."\n";
  514. }
  515. print "\n";
  516. print load_fiche_titre($langs->trans("ProjectsDedicatedToThisThirdParty"),$buttoncreate,'');
  517. print '<div class="div-table-responsive">';
  518. print "\n".'<table class="noborder" width=100%>';
  519. $sql = "SELECT p.rowid as id, 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";
  520. $sql .= ", cls.code as opp_status_code";
  521. $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
  522. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_lead_status as cls on p.fk_opp_status = cls.rowid";
  523. $sql .= " WHERE p.fk_soc = ".$object->id;
  524. $sql .= " ORDER BY p.dateo DESC";
  525. $result=$db->query($sql);
  526. if ($result)
  527. {
  528. $num = $db->num_rows($result);
  529. print '<tr class="liste_titre">';
  530. print '<td>'.$langs->trans("Ref").'</td>';
  531. print '<td>'.$langs->trans("Name").'</td>';
  532. print '<td class="center">'.$langs->trans("DateStart").'</td>';
  533. print '<td class="center">'.$langs->trans("DateEnd").'</td>';
  534. print '<td class="right">'.$langs->trans("OpportunityAmountShort").'</td>';
  535. print '<td class="center">'.$langs->trans("OpportunityStatusShort").'</td>';
  536. print '<td class="right">'.$langs->trans("OpportunityProbabilityShort").'</td>';
  537. print '<td class="right">'.$langs->trans("Status").'</td>';
  538. print '</tr>';
  539. if ($num > 0)
  540. {
  541. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  542. $projecttmp = new Project($db);
  543. $i=0;
  544. $var=true;
  545. while ($i < $num)
  546. {
  547. $obj = $db->fetch_object($result);
  548. $projecttmp->fetch($obj->id);
  549. // To verify role of users
  550. $userAccess = $projecttmp->restrictedProjectArea($user);
  551. if ($user->rights->projet->lire && $userAccess > 0)
  552. {
  553. $var = !$var;
  554. print '<tr class="oddeven">';
  555. // Ref
  556. print '<td><a href="'.DOL_URL_ROOT.'/projet/card.php?id='.$projecttmp->id.'">'.img_object($langs->trans("ShowProject"),($obj->public?'projectpub':'project'))." ".$obj->ref.'</a></td>';
  557. // Label
  558. print '<td>'.$obj->title.'</td>';
  559. // Date start
  560. print '<td class="center">'.dol_print_date($db->jdate($obj->do),"day").'</td>';
  561. // Date end
  562. print '<td class="center">'.dol_print_date($db->jdate($obj->de),"day").'</td>';
  563. // Opp amount
  564. print '<td class="right">';
  565. if ($obj->opp_status_code)
  566. {
  567. print price($obj->opp_amount, 1, '', 1, -1, -1, '');
  568. }
  569. print '</td>';
  570. // Opp status
  571. print '<td align="center">';
  572. if ($obj->opp_status_code) print $langs->trans("OppStatusShort".$obj->opp_status_code);
  573. print '</td>';
  574. // Opp percent
  575. print '<td align="right">';
  576. if ($obj->opp_percent) print price($obj->opp_percent, 1, '', 1, 0).'%';
  577. print '</td>';
  578. // Status
  579. print '<td align="right">'.$projecttmp->getLibStatut(5).'</td>';
  580. print '</tr>';
  581. }
  582. $i++;
  583. }
  584. }
  585. else
  586. {
  587. $var = false;
  588. print '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  589. }
  590. $db->free($result);
  591. }
  592. else
  593. {
  594. dol_print_error($db);
  595. }
  596. print "</table>";
  597. print '</div>';
  598. print "<br>\n";
  599. }
  600. return $i;
  601. }
  602. /**
  603. * Show html area for list of contacts
  604. *
  605. * @param Conf $conf Object conf
  606. * @param Translate $langs Object langs
  607. * @param DoliDB $db Database handler
  608. * @param Societe $object Third party object
  609. * @param string $backtopage Url to go once contact is created
  610. * @return void
  611. */
  612. function show_contacts($conf,$langs,$db,$object,$backtopage='')
  613. {
  614. global $user,$conf;
  615. global $bc;
  616. $form= new Form($db);
  617. $sortfield = GETPOST("sortfield",'alpha');
  618. $sortorder = GETPOST("sortorder",'alpha');
  619. $page = GETPOST('page','int');
  620. $search_status = GETPOST("search_status",'int');
  621. if ($search_status=='') $search_status=1; // always display activ customer first
  622. $search_name = GETPOST("search_name",'alpha');
  623. $search_addressphone = GETPOST("search_addressphone",'alpha');
  624. if (! $sortorder) $sortorder="ASC";
  625. if (! $sortfield) $sortfield="p.lastname";
  626. $i=-1;
  627. $contactstatic = new Contact($db);
  628. if (! empty($conf->clicktodial->enabled))
  629. {
  630. $user->fetch_clicktodial(); // lecture des infos de clicktodial
  631. }
  632. $buttoncreate='';
  633. if ($user->rights->societe->contact->creer)
  634. {
  635. $addcontact = (! empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("AddContact") : $langs->trans("AddContactAddress"));
  636. $buttoncreate='<a class="addnewrecord" href="'.DOL_URL_ROOT.'/contact/card.php?socid='.$object->id.'&amp;action=create&amp;backtopage='.urlencode($backtopage).'">'.$addcontact;
  637. if (empty($conf->dol_optimize_smallscreen)) $buttoncreate.=' '.img_picto($addcontact,'filenew');
  638. $buttoncreate.='</a>'."\n";
  639. }
  640. print "\n";
  641. $title = (! empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("ContactsForCompany") : $langs->trans("ContactsAddressesForCompany"));
  642. print load_fiche_titre($title,$buttoncreate,'');
  643. print '<form method="GET" action="'.$_SERVER["PHP_SELF"].'" name="formfilter">';
  644. print '<input type="hidden" name="socid" value="'.$object->id.'">';
  645. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  646. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  647. print '<input type="hidden" name="page" value="'.$page.'">';
  648. print "\n".'<table class="noborder" width="100%">'."\n";
  649. $param="socid=".$object->id;
  650. if ($search_status != '') $param.='&amp;search_status='.$search_status;
  651. if ($search_name != '') $param.='&amp;search_name='.urlencode($search_name);
  652. $colspan=9;
  653. print '<tr class="liste_titre">';
  654. print_liste_field_titre($langs->trans("Name"),$_SERVER["PHP_SELF"],"p.lastname","",$param,'',$sortfield,$sortorder);
  655. print_liste_field_titre($langs->trans("Poste"),$_SERVER["PHP_SELF"],"p.poste","",$param,'',$sortfield,$sortorder);
  656. print_liste_field_titre($langs->trans("Address").' / '.$langs->trans("Phone").' / '.$langs->trans("Email"),$_SERVER["PHP_SELF"],"","",$param,'',$sortfield,$sortorder);
  657. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"p.statut","",$param,'',$sortfield,$sortorder);
  658. // Add to agenda
  659. if (! empty($conf->agenda->enabled) && ! empty($user->rights->agenda->myactions->create))
  660. {
  661. $colspan++;
  662. print_liste_field_titre('');
  663. }
  664. // Edit
  665. print_liste_field_titre('');
  666. print "</tr>\n";
  667. $sql = "SELECT p.rowid, p.lastname, p.firstname, p.fk_pays as country_id, p.civility, p.poste, p.phone as phone_pro, p.phone_mobile, p.phone_perso, p.fax, p.email, p.skype, p.statut, p.photo,";
  668. $sql .= " p.civility as civility_id, p.address, p.zip, p.town";
  669. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
  670. $sql .= " WHERE p.fk_soc = ".$object->id;
  671. if ($search_status!='' && $search_status != '-1') $sql .= " AND p.statut = ".$db->escape($search_status);
  672. if ($search_name) $sql .= " AND (p.lastname LIKE '%".$db->escape($search_name)."%' OR p.firstname LIKE '%".$db->escape($search_name)."%')";
  673. $sql.= " ORDER BY $sortfield $sortorder";
  674. dol_syslog('core/lib/company.lib.php :: show_contacts', LOG_DEBUG);
  675. $result = $db->query($sql);
  676. if (! $result) dol_print_error($db);
  677. $num = $db->num_rows($result);
  678. $var=true;
  679. if ($num || (GETPOST('button_search') || GETPOST('button_search.x') || GETPOST('button_search_x')))
  680. {
  681. print '<tr class="liste_titre">';
  682. // Photo - Name
  683. print '<td class="liste_titre">';
  684. print '<input type="text" class="flat" name="search_name" size="20" value="'.$search_name.'">';
  685. print '</td>';
  686. // Position
  687. print '<td class="liste_titre">';
  688. print '</td>';
  689. // Address - Phone - Email
  690. print '<td class="liste_titre">&nbsp;</td>';
  691. // Status
  692. print '<td class="liste_titre maxwidthonsmartphone">';
  693. print $form->selectarray('search_status', array('-1'=>'','0'=>$contactstatic->LibStatut(0,1),'1'=>$contactstatic->LibStatut(1,1)),$search_status);
  694. print '</td>';
  695. // Add to agenda
  696. if (! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create)
  697. {
  698. $colspan++;
  699. print '<td class="liste_titre">&nbsp;</td>';
  700. }
  701. // Edit
  702. print '<td class="liste_titre" align="right">';
  703. print '<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"),'search.png','','',1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  704. print '</td>';
  705. print "</tr>";
  706. $i=0;
  707. while ($i < $num)
  708. {
  709. $obj = $db->fetch_object($result);
  710. $contactstatic->id = $obj->rowid;
  711. $contactstatic->ref = $obj->ref;
  712. $contactstatic->statut = $obj->statut;
  713. $contactstatic->lastname = $obj->lastname;
  714. $contactstatic->firstname = $obj->firstname;
  715. $contactstatic->civility_id = $obj->civility_id;
  716. $contactstatic->civility_code = $obj->civility_id;
  717. $contactstatic->poste = $obj->poste;
  718. $contactstatic->address = $obj->address;
  719. $contactstatic->zip = $obj->zip;
  720. $contactstatic->town = $obj->town;
  721. $contactstatic->phone_pro = $obj->phone_pro;
  722. $contactstatic->phone_mobile = $obj->phone_mobile;
  723. $contactstatic->phone_perso = $obj->phone_perso;
  724. $contactstatic->email = $obj->email;
  725. $contactstatic->web = $obj->web;
  726. $contactstatic->skype = $obj->skype;
  727. $contactstatic->photo = $obj->photo;
  728. $country_code = getCountry($obj->country_id, 2);
  729. $contactstatic->country_code = $country_code;
  730. $contactstatic->setGenderFromCivility();
  731. print "<tr>";
  732. // Photo - Name
  733. print '<td>';
  734. print $form->showphoto('contact',$contactstatic,0,0,0,'photorefnoborder valignmiddle marginrightonly','small',1,0,1);
  735. print $contactstatic->getNomUrl(0,'',0,'&backtopage='.urlencode($backtopage));
  736. print '</td>';
  737. // Job position
  738. print '<td>';
  739. if ($obj->poste) print $obj->poste;
  740. print '</td>';
  741. // Address - Phone - Email
  742. print '<td>';
  743. print $contactstatic->getBannerAddress('contact', $object);
  744. print '</td>';
  745. // Status
  746. print '<td>'.$contactstatic->getLibStatut(5).'</td>';
  747. // Add to agenda
  748. if (! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create)
  749. {
  750. print '<td align="center">';
  751. print '<a href="'.DOL_URL_ROOT.'/comm/action/card.php?action=create&actioncode=&contactid='.$obj->rowid.'&socid='.$object->id.'&backtopage='.urlencode($backtopage).'">';
  752. print img_object($langs->trans("Event"),"action");
  753. print '</a></td>';
  754. }
  755. // Edit
  756. if ($user->rights->societe->contact->creer)
  757. {
  758. print '<td align="right">';
  759. print '<a href="'.DOL_URL_ROOT.'/contact/card.php?action=edit&amp;id='.$obj->rowid.'&amp;backtopage='.urlencode($backtopage).'">';
  760. print img_edit();
  761. print '</a></td>';
  762. }
  763. else print '<td>&nbsp;</td>';
  764. print "</tr>\n";
  765. $i++;
  766. }
  767. }
  768. else
  769. {
  770. print "<tr ".$bc[! $var].">";
  771. print '<td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td>';
  772. print "</tr>\n";
  773. }
  774. print "\n</table>\n";
  775. print '</form>'."\n";
  776. return $i;
  777. }
  778. /**
  779. * Show html area for list of addresses
  780. *
  781. * @param Conf $conf Object conf
  782. * @param Translate $langs Object langs
  783. * @param DoliDB $db Database handler
  784. * @param Societe $object Third party object
  785. * @param string $backtopage Url to go once address is created
  786. * @return void
  787. */
  788. function show_addresses($conf,$langs,$db,$object,$backtopage='')
  789. {
  790. global $user;
  791. global $bc;
  792. require_once DOL_DOCUMENT_ROOT.'/societe/class/address.class.php';
  793. $addressstatic = new Address($db);
  794. $num = $addressstatic->fetch_lines($object->id);
  795. $buttoncreate='';
  796. if ($user->rights->societe->creer)
  797. {
  798. $buttoncreate='<a class="addnewrecord" href="'.DOL_URL_ROOT.'/comm/address.php?socid='.$object->id.'&amp;action=create&amp;backtopage='.urlencode($backtopage).'">'.$langs->trans("AddAddress").' '.img_picto($langs->trans("AddAddress"),'filenew').'</a>'."\n";
  799. }
  800. print "\n";
  801. print load_fiche_titre($langs->trans("AddressesForCompany"),$buttoncreate,'');
  802. print "\n".'<table class="noborder" width="100%">'."\n";
  803. print '<tr class="liste_titre"><td>'.$langs->trans("Label").'</td>';
  804. print '<td>'.$langs->trans("CompanyName").'</td>';
  805. print '<td>'.$langs->trans("Town").'</td>';
  806. print '<td>'.$langs->trans("Country").'</td>';
  807. print '<td>'.$langs->trans("Phone").'</td>';
  808. print '<td>'.$langs->trans("Fax").'</td>';
  809. print "<td>&nbsp;</td>";
  810. print "</tr>";
  811. if ($num > 0)
  812. {
  813. $var=true;
  814. foreach ($addressstatic->lines as $address)
  815. {
  816. $var = !$var;
  817. print '<tr class="oddeven">';
  818. print '<td>';
  819. $addressstatic->id = $address->id;
  820. $addressstatic->label = $address->label;
  821. print $addressstatic->getNomUrl(1);
  822. print '</td>';
  823. print '<td>'.$address->name.'</td>';
  824. print '<td>'.$address->town.'</td>';
  825. $img=picto_from_langcode($address->country_code);
  826. print '<td>'.($img?$img.' ':'').$address->country.'</td>';
  827. // Lien click to dial
  828. print '<td>';
  829. print dol_print_phone($address->phone,$address->country_code,$address->id,$object->id,'AC_TEL');
  830. print '</td>';
  831. print '<td>';
  832. print dol_print_phone($address->fax,$address->country_code,$address->id,$object->id,'AC_FAX');
  833. print '</td>';
  834. if ($user->rights->societe->creer)
  835. {
  836. print '<td align="right">';
  837. print '<a href="'.DOL_URL_ROOT.'/comm/address.php?action=edit&amp;id='.$address->id.'&amp;socid='.$object->id.'&amp;backtopage='.urlencode($backtopage).'">';
  838. print img_edit();
  839. print '</a></td>';
  840. }
  841. print "</tr>\n";
  842. }
  843. }
  844. else
  845. {
  846. //print '<tr class="oddeven">';
  847. //print '<td>'.$langs->trans("NoAddressYetDefined").'</td>';
  848. //print "</tr>\n";
  849. }
  850. print "\n</table>\n";
  851. print "<br>\n";
  852. return $num;
  853. }
  854. /**
  855. * Show html area with actions to do
  856. *
  857. * @param Conf $conf Object conf
  858. * @param Translate $langs Object langs
  859. * @param DoliDB $db Object db
  860. * @param Adherent|Societe $filterobj Object third party or member
  861. * @param Contact $objcon Object contact
  862. * @param int $noprint Return string but does not output it
  863. * @param int $actioncode Filter on actioncode
  864. * @return mixed Return html part or void if noprint is 1
  865. */
  866. function show_actions_todo($conf,$langs,$db,$filterobj,$objcon='',$noprint=0,$actioncode='')
  867. {
  868. global $bc,$user,$conf;
  869. $out = show_actions_done($conf,$langs,$db,$filterobj,$objcon,1,$actioncode, 'todo');
  870. if ($noprint) return $out;
  871. else print $out;
  872. }
  873. /**
  874. * Show html area with actions (done or not, ignore the name of function)
  875. *
  876. * @param Conf $conf Object conf
  877. * @param Translate $langs Object langs
  878. * @param DoliDB $db Object db
  879. * @param Adherent|Societe|Project $filterobj Object third party or member or project
  880. * @param Contact $objcon Object contact
  881. * @param int $noprint Return string but does not output it
  882. * @param string $actioncode Filter on actioncode
  883. * @param string $donetodo Filter on event 'done' or 'todo' or ''=nofilter (all).
  884. * @param array $filters Filter on other fields
  885. * @param string $sortfield Sort field
  886. * @param string $sortorder Sort order
  887. * @return mixed Return html part or void if noprint is 1
  888. * TODO change function to be able to list event linked to an object.
  889. */
  890. function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=0, $actioncode='', $donetodo='done', $filters=array(), $sortfield='a.datep,a.id', $sortorder='DESC')
  891. {
  892. global $bc,$user,$conf;
  893. global $form;
  894. global $param;
  895. // Check parameters
  896. if (! is_object($filterobj)) dol_print_error('','BadParameter');
  897. $out='';
  898. $histo=array();
  899. $numaction = 0 ;
  900. $now=dol_now('tzuser');
  901. if (! empty($conf->agenda->enabled))
  902. {
  903. // Recherche histo sur actioncomm
  904. $sql = "SELECT a.id, a.label,";
  905. $sql.= " a.datep as dp,";
  906. $sql.= " a.datep2 as dp2,";
  907. $sql.= " a.note, a.percent,";
  908. $sql.= " a.fk_element, a.elementtype,";
  909. $sql.= " a.fk_user_author, a.fk_contact,";
  910. $sql.= " c.code as acode, c.libelle as alabel, c.picto as apicto,";
  911. $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";
  912. if (get_class($filterobj) == 'Societe') $sql.= ", sp.lastname, sp.firstname";
  913. if (get_class($filterobj) == 'Adherent') $sql.= ", m.lastname, m.firstname";
  914. if (get_class($filterobj) == 'CommandeFournisseur') $sql.= ", o.ref";
  915. $sql.= " FROM ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
  916. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action = c.id";
  917. if (get_class($filterobj) == 'Societe') $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON a.fk_contact = sp.rowid";
  918. if (get_class($filterobj) == 'Adherent') $sql.= ", ".MAIN_DB_PREFIX."adherent as m";
  919. if (get_class($filterobj) == 'CommandeFournisseur') $sql.= ", ".MAIN_DB_PREFIX."commande_fournisseur as o";
  920. $sql.= " WHERE u.rowid = a.fk_user_action";
  921. $sql.= " AND a.entity IN (".getEntity('agenda', 1).")";
  922. if (get_class($filterobj) == 'Societe' && $filterobj->id) $sql.= " AND a.fk_soc = ".$filterobj->id;
  923. if (get_class($filterobj) == 'Project' && $filterobj->id) $sql.= " AND a.fk_project = ".$filterobj->id;
  924. if (get_class($filterobj) == 'Adherent')
  925. {
  926. $sql.= " AND a.fk_element = m.rowid AND a.elementtype = 'member'";
  927. if ($filterobj->id) $sql.= " AND a.fk_element = ".$filterobj->id;
  928. }
  929. if (get_class($filterobj) == 'CommandeFournisseur')
  930. {
  931. $sql.= " AND a.fk_element = o.rowid AND a.elementtype = 'order_supplier'";
  932. if ($filterobj->id) $sql.= " AND a.fk_element = ".$filterobj->id;
  933. }
  934. if (is_object($objcon) && $objcon->id) $sql.= " AND a.fk_contact = ".$objcon->id;
  935. // Condition on actioncode
  936. if (! empty($actioncode))
  937. {
  938. if (empty($conf->global->AGENDA_USE_EVENT_TYPE))
  939. {
  940. if ($actioncode == 'AC_NON_AUTO') $sql.= " AND c.type != 'systemauto'";
  941. elseif ($actioncode == 'AC_ALL_AUTO') $sql.= " AND c.type = 'systemauto'";
  942. else
  943. {
  944. if ($actioncode == 'AC_OTH') $sql.= " AND c.type != 'systemauto'";
  945. if ($actioncode == 'AC_OTH_AUTO') $sql.= " AND c.type = 'systemauto'";
  946. }
  947. }
  948. else
  949. {
  950. if ($actioncode == 'AC_NON_AUTO') $sql.= " AND c.type != 'systemauto'";
  951. elseif ($actioncode == 'AC_ALL_AUTO') $sql.= " AND c.type = 'systemauto'";
  952. else $sql.= " AND c.code = '".$db->escape($actioncode)."'";
  953. }
  954. }
  955. if ($donetodo == 'todo') $sql.= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
  956. if ($donetodo == 'done') $sql.= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
  957. if (is_array($filters) && $filters['search_agenda_label']) $sql.= natural_search('a.label', $filters['search_agenda_label']);
  958. $sql.= $db->order($sortfield, $sortorder);
  959. dol_syslog("company.lib::show_actions_done", LOG_DEBUG);
  960. $resql=$db->query($sql);
  961. if ($resql)
  962. {
  963. $i = 0 ;
  964. $num = $db->num_rows($resql);
  965. $var=true;
  966. while ($i < $num)
  967. {
  968. $obj = $db->fetch_object($resql);
  969. //if ($donetodo == 'todo') $sql.= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
  970. //if ($donetodo == 'done') $sql.= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
  971. $tododone='';
  972. if (($obj->percent >= 0 and $obj->percent < 100) || ($obj->percent == -1 && $obj->datep > $now)) $tododone='todo';
  973. $histo[$numaction]=array(
  974. 'type'=>'action',
  975. 'tododone'=>$tododone,
  976. 'id'=>$obj->id,
  977. 'datestart'=>$db->jdate($obj->dp),
  978. 'dateend'=>$db->jdate($obj->dp2),
  979. 'note'=>$obj->label,
  980. 'percent'=>$obj->percent,
  981. 'userid'=>$obj->user_id,
  982. 'login'=>$obj->user_login,
  983. 'userfirstname'=>$obj->user_firstname,
  984. 'userlastname'=>$obj->user_lastname,
  985. 'userphoto'=>$obj->user_photo,
  986. 'contact_id'=>$obj->fk_contact,
  987. 'lastname'=>$obj->lastname,
  988. 'firstname'=>$obj->firstname,
  989. 'fk_element'=>$obj->fk_element,
  990. 'elementtype'=>$obj->elementtype,
  991. // Type of event
  992. 'acode'=>$obj->acode,
  993. 'alabel'=>$obj->alabel,
  994. 'libelle'=>$obj->alabel, // deprecated
  995. 'apicto'=>$obj->apicto
  996. );
  997. $numaction++;
  998. $i++;
  999. }
  1000. }
  1001. else
  1002. {
  1003. dol_print_error($db);
  1004. }
  1005. }
  1006. // Add also event from emailings. FIXME This should be replaced by an automatic event
  1007. if (! empty($conf->mailing->enabled) && ! empty($objcon->email))
  1008. {
  1009. $langs->load("mails");
  1010. $sql = "SELECT m.rowid as id, mc.date_envoi as da, m.titre as note, '100' as percentage,";
  1011. $sql.= " 'AC_EMAILING' as acode,";
  1012. $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"; // User that valid action
  1013. $sql.= " FROM ".MAIN_DB_PREFIX."mailing as m, ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."user as u";
  1014. $sql.= " WHERE mc.email = '".$db->escape($objcon->email)."'"; // Search is done on email.
  1015. $sql.= " AND mc.statut = 1";
  1016. $sql.= " AND u.rowid = m.fk_user_valid";
  1017. $sql.= " AND mc.fk_mailing=m.rowid";
  1018. $sql.= " ORDER BY mc.date_envoi DESC, m.rowid DESC";
  1019. dol_syslog("company.lib::show_actions_done", LOG_DEBUG);
  1020. $resql=$db->query($sql);
  1021. if ($resql)
  1022. {
  1023. $i = 0 ;
  1024. $num = $db->num_rows($resql);
  1025. $var=true;
  1026. while ($i < $num)
  1027. {
  1028. $obj = $db->fetch_object($resql);
  1029. $histo[$numaction]=array(
  1030. 'type'=>'mailing',
  1031. 'tododone'=>'done',
  1032. 'id'=>$obj->id,
  1033. 'datestart'=>$db->jdate($obj->da),
  1034. 'dateend'=>$db->jdate($obj->da),
  1035. 'note'=>$obj->note,
  1036. 'percent'=>$obj->percentage,
  1037. 'acode'=>$obj->acode,
  1038. 'userid'=>$obj->user_id,
  1039. 'login'=>$obj->user_login,
  1040. 'userfirstname'=>$obj->user_firstname,
  1041. 'userlastname'=>$obj->user_lastname,
  1042. 'userphoto'=>$obj->user_photo
  1043. );
  1044. $numaction++;
  1045. $i++;
  1046. }
  1047. $db->free($resql);
  1048. }
  1049. else
  1050. {
  1051. dol_print_error($db);
  1052. }
  1053. }
  1054. if (! empty($conf->agenda->enabled) || (! empty($conf->mailing->enabled) && ! empty($objcon->email)))
  1055. {
  1056. $delay_warning=$conf->global->MAIN_DELAY_ACTIONS_TODO*24*60*60;
  1057. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  1058. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  1059. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  1060. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  1061. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  1062. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  1063. $formactions=new FormActions($db);
  1064. $actionstatic=new ActionComm($db);
  1065. $userstatic=new User($db);
  1066. $contactstatic = new Contact($db);
  1067. // TODO mutualize/uniformize
  1068. $propalstatic=new Propal($db);
  1069. $orderstatic=new Commande($db);
  1070. $supplierorderstatic=new CommandeFournisseur($db);
  1071. $facturestatic=new Facture($db);
  1072. $out.='<form name="listactionsfilter" class="listactionsfilter" action="' . $_SERVER["PHP_SELF"] . '" method="POST">';
  1073. if ($objcon && get_class($objcon) == 'Contact' && get_class($filterobj) == 'Societe')
  1074. {
  1075. $out.='<input type="hidden" name="id" value="'.$objcon->id.'" />';
  1076. }
  1077. else
  1078. {
  1079. $out.='<input type="hidden" name="id" value="'.$filterobj->id.'" />';
  1080. }
  1081. if (get_class($filterobj) == 'Societe') $out.='<input type="hidden" name="socid" value="'.$filterobj->id.'" />';
  1082. $out.="\n";
  1083. $out.='<div class="div-table-responsive-no-min">';
  1084. $out.='<table class="noborder" width="100%">';
  1085. $out.='<tr class="liste_titre">';
  1086. if ($donetodo)
  1087. {
  1088. $out.='<td class="liste_titre"></td>';
  1089. }
  1090. $out.='<td class="liste_titre"></td>';
  1091. $out.='<td class="liste_titre"></td>';
  1092. $out.='<td class="liste_titre maxwidth100onsmartphone"><input type="text" class="maxwidth100onsmartphone" name="search_agenda_label" value="'.$filters['search_agenda_label'].'"></td>';
  1093. $out.='<td class="liste_titre"></td>';
  1094. $out.='<td class="liste_titre">';
  1095. $out.=$formactions->select_type_actions($actioncode, "actioncode", '', empty($conf->global->AGENDA_USE_EVENT_TYPE)?1:-1, 0, 0, 1);
  1096. $out.='</td>';
  1097. $out.='<td class="liste_titre"></td>';
  1098. $out.='<td class="liste_titre"></td>';
  1099. $out.='<td class="liste_titre"></td>';
  1100. // Action column
  1101. $out.='<td class="liste_titre" align="middle">';
  1102. $searchpicto=$form->showFilterAndCheckAddButtons($massactionbutton?1:0, 'checkforselect', 1);
  1103. $out.=$searchpicto;
  1104. $out.='</td>';
  1105. $out.='</tr>';
  1106. $out.='<tr class="liste_titre">';
  1107. if ($donetodo)
  1108. {
  1109. $tmp='';
  1110. if (get_class($filterobj) == 'Societe') $tmp.='<a href="'.DOL_URL_ROOT.'/comm/action/listactions.php?socid='.$filterobj->id.'&amp;status=done">';
  1111. $tmp.=($donetodo != 'done' ? $langs->trans("ActionsToDoShort") : '');
  1112. $tmp.=($donetodo != 'done' && $donetodo != 'todo' ? ' / ' : '');
  1113. $tmp.=($donetodo != 'todo' ? $langs->trans("ActionsDoneShort") : '');
  1114. //$out.=$langs->trans("ActionsToDoShort").' / '.$langs->trans("ActionsDoneShort");
  1115. if (get_class($filterobj) == 'Societe') $tmp.='</a>';
  1116. $out.=getTitleFieldOfList($tmp);
  1117. }
  1118. $out.=getTitleFieldOfList($langs->trans("Ref"), 0, $_SERVER["PHP_SELF"], 'a.id', '', $param, '', $sortfield, $sortorder);
  1119. $out.=getTitleFieldOfList($langs->trans("Owner"));
  1120. $out.=getTitleFieldOfList($langs->trans("Label"), 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  1121. $out.=getTitleFieldOfList($langs->trans("Date"), 0, $_SERVER["PHP_SELF"], 'a.datep,a.id', '', $param, 'align="center"', $sortfield, $sortorder);
  1122. $out.=getTitleFieldOfList($langs->trans("Type"));
  1123. $out.=getTitleFieldOfList('');
  1124. $out.=getTitleFieldOfList('');
  1125. $out.=getTitleFieldOfList($langs->trans("Status"), 0, $_SERVER["PHP_SELF"], 'a.percent', '', $param, 'align="center"', $sortfield, $sortorder);
  1126. $out.=getTitleFieldOfList('', 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'maxwidthsearch ');
  1127. $out.='</tr>';
  1128. foreach ($histo as $key=>$value)
  1129. {
  1130. $actionstatic->fetch($histo[$key]['id']); // TODO Do we need this, we already have a lot of data of line into $histo
  1131. $out.="<tr ".$bc[$var].">";
  1132. // Done or todo
  1133. if ($donetodo)
  1134. {
  1135. $out.='<td class="nowrap">';
  1136. $out.='</td>';
  1137. }
  1138. // Ref
  1139. $out.='<td class="nowrap">';
  1140. $out.=$actionstatic->getNomUrl(1, -1);
  1141. $out.='</td>';
  1142. // Author of event
  1143. $out.='<td>';
  1144. //$userstatic->id=$histo[$key]['userid'];
  1145. //$userstatic->login=$histo[$key]['login'];
  1146. //$out.=$userstatic->getLoginUrl(1);
  1147. $userstatic->fetch($histo[$key]['userid']);
  1148. $out.=$userstatic->getNomUrl(-1);
  1149. $out.='</td>';
  1150. // Title
  1151. $out.='<td>';
  1152. if (isset($histo[$key]['type']) && $histo[$key]['type']=='action')
  1153. {
  1154. $actionstatic->type_code=$histo[$key]['acode'];
  1155. $transcode=$langs->trans("Action".$histo[$key]['acode']);
  1156. $libelle=($transcode!="Action".$histo[$key]['acode']?$transcode:$histo[$key]['alabel']);
  1157. //$actionstatic->libelle=$libelle;
  1158. $libelle=$histo[$key]['note'];
  1159. $actionstatic->id=$histo[$key]['id'];
  1160. $out.=dol_trunc($libelle,120);
  1161. }
  1162. if (isset($histo[$key]['type']) && $histo[$key]['type']=='mailing')
  1163. {
  1164. $out.='<a href="'.DOL_URL_ROOT.'/comm/mailing/card.php?id='.$histo[$key]['id'].'">'.img_object($langs->trans("ShowEMailing"),"email").' ';
  1165. $transcode=$langs->trans("Action".$histo[$key]['acode']);
  1166. $libelle=($transcode!="Action".$histo[$key]['acode']?$transcode:'Send mass mailing');
  1167. $out.=dol_trunc($libelle,120);
  1168. }
  1169. $out.='</td>';
  1170. // Date
  1171. $out.='<td class="center nowrap">';
  1172. $out.=dol_print_date($histo[$key]['datestart'],'dayhour');
  1173. if ($histo[$key]['dateend'] && $histo[$key]['dateend'] != $histo[$key]['datestart'])
  1174. {
  1175. $tmpa=dol_getdate($histo[$key]['datestart'],true);
  1176. $tmpb=dol_getdate($histo[$key]['dateend'],true);
  1177. if ($tmpa['mday'] == $tmpb['mday'] && $tmpa['mon'] == $tmpb['mon'] && $tmpa['year'] == $tmpb['year']) $out.='-'.dol_print_date($histo[$key]['dateend'],'hour');
  1178. else $out.='-'.dol_print_date($histo[$key]['dateend'],'dayhour');
  1179. }
  1180. $late=0;
  1181. if ($histo[$key]['percent'] == 0 && $histo[$key]['datestart'] && $db->jdate($histo[$key]['datestart']) < ($now - $delay_warning)) $late=1;
  1182. if ($histo[$key]['percent'] == 0 && ! $histo[$key]['datestart'] && $histo[$key]['dateend'] && $db->jdate($histo[$key]['datestart']) < ($now - $delay_warning)) $late=1;
  1183. if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && $histo[$key]['dateend'] && $db->jdate($histo[$key]['dateend']) < ($now - $delay_warning)) $late=1;
  1184. if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && ! $histo[$key]['dateend'] && $histo[$key]['datestart'] && $db->jdate($histo[$key]['datestart']) < ($now - $delay_warning)) $late=1;
  1185. if ($late) $out.=img_warning($langs->trans("Late")).' ';
  1186. $out.="</td>\n";
  1187. // Type
  1188. $out.='<td>';
  1189. if (! empty($conf->global->AGENDA_USE_EVENT_TYPE))
  1190. {
  1191. if ($histo[$key]['apicto']) $out.=img_picto('', $histo[$key]['apicto']);
  1192. else {
  1193. if ($histo[$key]['acode'] == 'AC_TEL') $out.=img_picto('', 'object_phoning').' ';
  1194. if ($histo[$key]['acode'] == 'AC_FAX') $out.=img_picto('', 'object_phoning_fax').' ';
  1195. if ($histo[$key]['acode'] == 'AC_EMAIL') $out.=img_picto('', 'object_email').' ';
  1196. }
  1197. $out.=$actionstatic->type;
  1198. }
  1199. else {
  1200. $typelabel = $actionstatic->type;
  1201. if ($histo[$key]['acode'] != 'AC_OTH_AUTO') $typelabel = $langs->trans("ActionAC_MANUAL");
  1202. $out.=$typelabel;
  1203. }
  1204. $out.='</td>';
  1205. // Title of event
  1206. //$out.='<td>'.dol_trunc($histo[$key]['note'], 40).'</td>';
  1207. // Objet lie
  1208. // TODO mutualize/uniformize
  1209. $out.='<td>';
  1210. //var_dump($histo[$key]['elementtype']);
  1211. if (isset($histo[$key]['elementtype']))
  1212. {
  1213. if ($histo[$key]['elementtype'] == 'propal' && ! empty($conf->propal->enabled))
  1214. {
  1215. //$propalstatic->ref=$langs->trans("ProposalShort");
  1216. //$propalstatic->id=$histo[$key]['fk_element'];
  1217. if ($propalstatic->fetch($histo[$key]['fk_element'])>0) {
  1218. $propalstatic->type=$histo[$key]['ftype'];
  1219. $out.=$propalstatic->getNomUrl(1);
  1220. } else {
  1221. $out.= $langs->trans("ProposalDeleted");
  1222. }
  1223. }
  1224. elseif (($histo[$key]['elementtype'] == 'order' || $histo[$key]['elementtype'] == 'commande') && ! empty($conf->commande->enabled))
  1225. {
  1226. //$orderstatic->ref=$langs->trans("Order");
  1227. //$orderstatic->id=$histo[$key]['fk_element'];
  1228. if ($orderstatic->fetch($histo[$key]['fk_element'])>0) {
  1229. $orderstatic->type=$histo[$key]['ftype'];
  1230. $out.=$orderstatic->getNomUrl(1);
  1231. } else {
  1232. $out.= $langs->trans("OrderDeleted");
  1233. }
  1234. }
  1235. elseif (($histo[$key]['elementtype'] == 'invoice' || $histo[$key]['elementtype'] == 'facture') && ! empty($conf->facture->enabled))
  1236. {
  1237. //$facturestatic->ref=$langs->trans("Invoice");
  1238. //$facturestatic->id=$histo[$key]['fk_element'];
  1239. if ($facturestatic->fetch($histo[$key]['fk_element'])>0) {
  1240. $facturestatic->type=$histo[$key]['ftype'];
  1241. $out.=$facturestatic->getNomUrl(1,'compta');
  1242. } else {
  1243. $out.= $langs->trans("InvoiceDeleted");
  1244. }
  1245. }
  1246. else $out.='&nbsp;';
  1247. }
  1248. else $out.='&nbsp;';
  1249. $out.='</td>';
  1250. // Contact pour cette action
  1251. if (! empty($objcon->id) && isset($histo[$key]['contact_id']) && $histo[$key]['contact_id'] > 0)
  1252. {
  1253. $contactstatic->lastname=$histo[$key]['lastname'];
  1254. $contactstatic->firstname=$histo[$key]['firstname'];
  1255. $contactstatic->id=$histo[$key]['contact_id'];
  1256. $out.='<td width="120">'.$contactstatic->getNomUrl(1,'',10).'</td>';
  1257. }
  1258. else
  1259. {
  1260. $out.='<td>&nbsp;</td>';
  1261. }
  1262. // Status
  1263. $out.='<td class="nowrap" align="center">'.$actionstatic->LibStatut($histo[$key]['percent'],3,1,$histo[$key]['datestart']).'</td>';
  1264. // Actions
  1265. $out.='<td></td>';
  1266. $out.="</tr>\n";
  1267. $i++;
  1268. }
  1269. $out.="</table>\n";
  1270. $out.="</div>\n";
  1271. }
  1272. $out.='</form>';
  1273. if ($noprint) return $out;
  1274. else print $out;
  1275. }
  1276. /**
  1277. * Show html area for list of subsidiaries
  1278. *
  1279. * @param Conf $conf Object conf
  1280. * @param Translate $langs Object langs
  1281. * @param DoliDB $db Database handler
  1282. * @param Societe $object Third party object
  1283. * @return void
  1284. */
  1285. function show_subsidiaries($conf,$langs,$db,$object)
  1286. {
  1287. global $user;
  1288. global $bc;
  1289. $i=-1;
  1290. $sql = "SELECT s.rowid, s.nom as name, s.address, s.zip, s.town, s.code_client, s.canvas";
  1291. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  1292. $sql.= " WHERE s.parent = ".$object->id;
  1293. $sql.= " AND s.entity IN (".getEntity('societe', 1).")";
  1294. $sql.= " ORDER BY s.nom";
  1295. $result = $db->query($sql);
  1296. $num = $db->num_rows($result);
  1297. if ($num)
  1298. {
  1299. $socstatic = new Societe($db);
  1300. print load_fiche_titre($langs->trans("Subsidiaries"), '', '');
  1301. print "\n".'<table class="noborder" width="100%">'."\n";
  1302. print '<tr class="liste_titre"><td>'.$langs->trans("Company").'</td>';
  1303. print '<td>'.$langs->trans("Address").'</td><td>'.$langs->trans("Zip").'</td>';
  1304. print '<td>'.$langs->trans("Town").'</td><td>'.$langs->trans("CustomerCode").'</td>';
  1305. print "<td>&nbsp;</td>";
  1306. print "</tr>";
  1307. $i=0;
  1308. $var=true;
  1309. while ($i < $num)
  1310. {
  1311. $obj = $db->fetch_object($result);
  1312. $var = !$var;
  1313. print '<tr class="oddeven">';
  1314. print '<td>';
  1315. $socstatic->id = $obj->rowid;
  1316. $socstatic->name = $obj->name;
  1317. $socstatic->canvas = $obj->canvas;
  1318. print $socstatic->getNomUrl(1);
  1319. print '</td>';
  1320. print '<td>'.$obj->address.'</td>';
  1321. print '<td>'.$obj->zip.'</td>';
  1322. print '<td>'.$obj->town.'</td>';
  1323. print '<td>'.$obj->code_client.'</td>';
  1324. print '<td align="center">';
  1325. print '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$obj->rowid.'&amp;action=edit">';
  1326. print img_edit();
  1327. print '</a></td>';
  1328. print "</tr>\n";
  1329. $i++;
  1330. }
  1331. print "\n</table>\n";
  1332. }
  1333. print "<br>\n";
  1334. return $i;
  1335. }