list.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. <?php
  2. /* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2020 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
  6. * Copyright (C) 2014-2019 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2015 Claudio Aschieri <c.aschieri@19.coop>
  8. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  9. * Copyright (C) 2016-2018 Ferran Marcet <fmarcet@2byte.es>
  10. * Copyright (C) 2019 Nicolas Zabouri <info@inovea-conseil.com>
  11. * Copyright (C) 2021 Alexandre Spangaro <aspangaro@open-dsi.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 <https://www.gnu.org/licenses/>.
  25. */
  26. /**
  27. * \file htdocs/contrat/list.php
  28. * \ingroup contrat
  29. * \brief Page to list contracts
  30. */
  31. // Load Dolibarr environment
  32. require '../main.inc.php';
  33. require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  39. if (!empty($conf->categorie->enabled)) {
  40. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  41. }
  42. // Load translation files required by the page
  43. $langs->loadLangs(array('contracts', 'products', 'companies', 'compta'));
  44. $action = GETPOST('action', 'aZ09');
  45. $massaction = GETPOST('massaction', 'alpha');
  46. $show_files = GETPOST('show_files', 'int');
  47. $confirm = GETPOST('confirm', 'alpha');
  48. $toselect = GETPOST('toselect', 'array');
  49. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'contractlist'; // To manage different context of search
  50. $search_name = GETPOST('search_name', 'alpha');
  51. $search_email = GETPOST('search_email', 'alpha');
  52. $search_town = GETPOST('search_town', 'alpha');
  53. $search_zip = GETPOST('search_zip', 'alpha');
  54. $search_state = GETPOST("search_state", 'alpha');
  55. $search_country = GETPOST("search_country", 'int');
  56. $search_type_thirdparty = GETPOST("search_type_thirdparty", 'int');
  57. $search_contract = GETPOST('search_contract', 'alpha');
  58. $search_ref_customer = GETPOST('search_ref_customer', 'alpha');
  59. $search_ref_supplier = GETPOST('search_ref_supplier', 'alpha');
  60. $sall = (GETPOST('search_all', 'alphanohtml') != '') ? GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml');
  61. $search_status = GETPOST('search_status', 'alpha');
  62. $socid = GETPOST('socid', 'int');
  63. $search_user = GETPOST('search_user', 'int');
  64. $search_sale = GETPOST('search_sale', 'int');
  65. $search_product_category = GETPOST('search_product_category', 'int');
  66. $search_dfmonth = GETPOST('search_dfmonth', 'int');
  67. $search_dfyear = GETPOST('search_dfyear', 'int');
  68. $search_op2df = GETPOST('search_op2df', 'alpha');
  69. $search_date_startday = GETPOST('search_date_startday', 'int');
  70. $search_date_startmonth = GETPOST('search_date_startmonth', 'int');
  71. $search_date_startyear = GETPOST('search_date_startyear', 'int');
  72. $search_date_endday = GETPOST('search_date_endday', 'int');
  73. $search_date_endmonth = GETPOST('search_date_endmonth', 'int');
  74. $search_date_endyear = GETPOST('search_date_endyear', 'int');
  75. $search_date_start = dol_mktime(0, 0, 0, $search_date_startmonth, $search_date_startday, $search_date_startyear); // Use tzserver
  76. $search_date_end = dol_mktime(23, 59, 59, $search_date_endmonth, $search_date_endday, $search_date_endyear);
  77. $searchCategoryCustomerOperator = 0;
  78. if (GETPOSTISSET('formfilteraction')) {
  79. $searchCategoryCustomerOperator = GETPOST('search_category_customer_operator', 'int');
  80. } elseif (!empty($conf->global->MAIN_SEARCH_CAT_OR_BY_DEFAULT)) {
  81. $searchCategoryCustomerOperator = $conf->global->MAIN_SEARCH_CAT_OR_BY_DEFAULT;
  82. }
  83. $searchCategoryCustomerList = GETPOST('search_category_customer_list', 'array');
  84. $optioncss = GETPOST('optioncss', 'alpha');
  85. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  86. $sortfield = GETPOST('sortfield', 'aZ09comma');
  87. $sortorder = GETPOST('sortorder', 'aZ09comma');
  88. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  89. if (empty($page) || $page == -1) {
  90. $page = 0;
  91. } // If $page is not defined, or '' or -1
  92. $offset = $limit * $page;
  93. $pageprev = $page - 1;
  94. $pagenext = $page + 1;
  95. if (!$sortfield) {
  96. $sortfield = 'c.ref';
  97. }
  98. if (!$sortorder) {
  99. $sortorder = 'DESC';
  100. }
  101. // Security check
  102. $id = GETPOST('id', 'int');
  103. if ($user->socid) {
  104. $socid = $user->socid;
  105. }
  106. $result = restrictedArea($user, 'contrat', $id);
  107. $diroutputmassaction = $conf->contrat->dir_output.'/temp/massgeneration/'.$user->id;
  108. $staticcontrat = new Contrat($db);
  109. $staticcontratligne = new ContratLigne($db);
  110. if ($search_status == '') {
  111. $search_status = 1;
  112. }
  113. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  114. $object = new Contrat($db);
  115. $hookmanager->initHooks(array('contractlist'));
  116. $extrafields = new ExtraFields($db);
  117. // fetch optionals attributes and labels
  118. $extrafields->fetch_name_optionals_label($object->table_element);
  119. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  120. // List of fields to search into when doing a "search in all"
  121. $fieldstosearchall = array(
  122. 'c.ref'=>'Ref',
  123. 'c.ref_customer'=>'RefCustomer',
  124. 'c.ref_supplier'=>'RefSupplier',
  125. 's.nom'=>"ThirdParty",
  126. 'c.note_public'=>'NotePublic',
  127. );
  128. if (empty($user->socid)) {
  129. $fieldstosearchall["c.note_private"] = "NotePrivate";
  130. }
  131. $arrayfields = array(
  132. 'c.ref'=>array('label'=>$langs->trans("Ref"), 'checked'=>1, 'position'=>10),
  133. 'c.ref_customer'=>array('label'=>$langs->trans("RefCustomer"), 'checked'=>1, 'position'=>12),
  134. 'c.ref_supplier'=>array('label'=>$langs->trans("RefSupplier"), 'checked'=>1, 'position'=>14),
  135. 's.nom'=>array('label'=>$langs->trans("ThirdParty"), 'checked'=>1, 'position'=>30),
  136. 's.email'=>array('label'=>$langs->trans("ThirdPartyEmail"), 'checked'=>0, 'position'=>30),
  137. 's.town'=>array('label'=>$langs->trans("Town"), 'checked'=>0, 'position'=>31),
  138. 's.zip'=>array('label'=>$langs->trans("Zip"), 'checked'=>1, 'position'=>32),
  139. 'state.nom'=>array('label'=>$langs->trans("StateShort"), 'checked'=>0, 'position'=>33),
  140. 'country.code_iso'=>array('label'=>$langs->trans("Country"), 'checked'=>0, 'position'=>34),
  141. 'sale_representative'=>array('label'=>$langs->trans("SaleRepresentativesOfThirdParty"), 'checked'=>-1, 'position'=>80),
  142. 'c.date_contrat'=>array('label'=>$langs->trans("DateContract"), 'checked'=>1, 'position'=>45),
  143. 'c.datec'=>array('label'=>$langs->trans("DateCreation"), 'checked'=>0, 'position'=>500),
  144. 'c.tms'=>array('label'=>$langs->trans("DateModificationShort"), 'checked'=>0, 'position'=>500),
  145. 'lower_planned_end_date'=>array('label'=>$langs->trans("LowerDateEndPlannedShort"), 'checked'=>1, 'position'=>900, 'help'=>$langs->trans("LowerDateEndPlannedShort")),
  146. 'status'=>array('label'=>$langs->trans("Status"), 'checked'=>1, 'position'=>1000),
  147. );
  148. // Extra fields
  149. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php';
  150. $object->fields = dol_sort_array($object->fields, 'position');
  151. $arrayfields = dol_sort_array($arrayfields, 'position');
  152. /*
  153. * Action
  154. */
  155. if (GETPOST('cancel', 'alpha')) {
  156. $action = 'list'; $massaction = '';
  157. }
  158. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
  159. $massaction = '';
  160. }
  161. $parameters = array('socid'=>$socid);
  162. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  163. if ($reshook < 0) {
  164. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  165. }
  166. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  167. // Purge search criteria
  168. 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
  169. $search_dfmonth = '';
  170. $search_dfyear = '';
  171. $search_op2df = '';
  172. $search_name = "";
  173. $search_email = "";
  174. $search_town = '';
  175. $search_zip = "";
  176. $search_state = "";
  177. $search_type = '';
  178. $search_country = '';
  179. $search_contract = "";
  180. $search_ref_customer = "";
  181. $search_ref_supplier = "";
  182. $search_user = '';
  183. $search_sale = '';
  184. $search_product_category = '';
  185. $search_date_startday = '';
  186. $search_date_startmonth = '';
  187. $search_date_startyear = '';
  188. $search_date_endday = '';
  189. $search_date_endmonth = '';
  190. $search_date_endyear = '';
  191. $search_date_start = '';
  192. $search_date_end = '';
  193. $sall = "";
  194. $search_status = "";
  195. $toselect = array();
  196. $search_type_thirdparty = '';
  197. $searchCategoryCustomerList = array();
  198. $search_array_options = array();
  199. }
  200. if (empty($reshook)) {
  201. $objectclass = 'Contrat';
  202. $objectlabel = 'Contracts';
  203. $permissiontoread = $user->rights->contrat->lire;
  204. $permissiontodelete = $user->rights->contrat->supprimer;
  205. $uploaddir = $conf->contrat->dir_output;
  206. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  207. }
  208. /*
  209. * View
  210. */
  211. $now = dol_now();
  212. $form = new Form($db);
  213. $formfile = new FormFile($db);
  214. $formother = new FormOther($db);
  215. $socstatic = new Societe($db);
  216. $formcompany = new FormCompany($db);
  217. $contracttmp = new Contrat($db);
  218. $title = "";
  219. $sql = 'SELECT';
  220. $sql .= " c.rowid, c.ref, c.datec as date_creation, c.tms as date_update, c.date_contrat, c.statut, c.ref_customer, c.ref_supplier, c.note_private, c.note_public, c.entity,";
  221. $sql .= ' s.rowid as socid, s.nom as name, s.name_alias, s.email, s.town, s.zip, s.fk_pays as country_id, s.client, s.code_client, s.status as company_status, s.logo as company_logo,';
  222. $sql .= " typent.code as typent_code,";
  223. $sql .= " state.code_departement as state_code, state.nom as state_name,";
  224. $sql .= " MIN(".$db->ifsql("cd.statut=4", "cd.date_fin_validite", "null").") as lower_planned_end_date,";
  225. $sql .= " SUM(".$db->ifsql("cd.statut=0", 1, 0).') as nb_initial,';
  226. $sql .= " SUM(".$db->ifsql("cd.statut=4 AND (cd.date_fin_validite IS NULL OR cd.date_fin_validite >= '".$db->idate($now)."')", 1, 0).') as nb_running,';
  227. $sql .= " SUM(".$db->ifsql("cd.statut=4 AND (cd.date_fin_validite IS NOT NULL AND cd.date_fin_validite < '".$db->idate($now)."')", 1, 0).') as nb_expired,';
  228. $sql .= " SUM(".$db->ifsql("cd.statut=4 AND (cd.date_fin_validite IS NOT NULL AND cd.date_fin_validite < '".$db->idate($now - $conf->contrat->services->expires->warning_delay)."')", 1, 0).') as nb_late,';
  229. $sql .= " SUM(".$db->ifsql("cd.statut=5", 1, 0).') as nb_closed';
  230. // Add fields from extrafields
  231. if (!empty($extrafields->attributes[$object->table_element]['label'])) {
  232. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
  233. $sql .= ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? ", ef.".$key." as options_".$key : '');
  234. }
  235. }
  236. // Add fields from hooks
  237. $parameters = array();
  238. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  239. $sql .= $hookmanager->resPrint;
  240. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  241. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as country on (country.rowid = s.fk_pays)";
  242. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_typent as typent on (typent.id = s.fk_typent)";
  243. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as state on (state.rowid = s.fk_departement)";
  244. if ($search_sale > 0 || (empty($user->rights->societe->client->voir) && !$socid)) {
  245. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  246. }
  247. $sql .= ", ".MAIN_DB_PREFIX."contrat as c";
  248. if (!empty($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
  249. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as ef on (c.rowid = ef.fk_object)";
  250. }
  251. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd ON c.rowid = cd.fk_contrat";
  252. if ($search_user > 0) {
  253. $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
  254. $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as tc";
  255. }
  256. $sql .= " WHERE c.fk_soc = s.rowid ";
  257. $sql .= ' AND c.entity IN ('.getEntity('contract').')';
  258. if ($search_type_thirdparty != '' && $search_type_thirdparty > 0) {
  259. $sql .= " AND s.fk_typent IN (".$db->sanitize($db->escape($search_type_thirdparty)).')';
  260. }
  261. if ($socid) {
  262. $sql .= " AND s.rowid = ".((int) $socid);
  263. }
  264. if (empty($user->rights->societe->client->voir) && !$socid) {
  265. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  266. }
  267. if ($search_date_start) {
  268. $sql .= " AND c.date_contrat >= '".$db->idate($search_date_start)."'";
  269. }
  270. if ($search_date_end) {
  271. $sql .= " AND c.date_contrat <= '".$db->idate($search_date_end)."'";
  272. }
  273. if ($search_name) {
  274. $sql .= natural_search('s.nom', $search_name);
  275. }
  276. if ($search_email) {
  277. $sql .= natural_search('s.email', $search_email);
  278. }
  279. if ($search_contract) {
  280. $sql .= natural_search(array('c.rowid', 'c.ref'), $search_contract);
  281. }
  282. if (!empty($search_ref_customer)) {
  283. $sql .= natural_search(array('c.ref_customer'), $search_ref_customer);
  284. }
  285. if (!empty($search_ref_supplier)) {
  286. $sql .= natural_search(array('c.ref_supplier'), $search_ref_supplier);
  287. }
  288. if ($search_zip) {
  289. $sql .= natural_search(array('s.zip'), $search_zip);
  290. }
  291. if ($search_town) {
  292. $sql .= natural_search(array('s.town'), $search_town);
  293. }
  294. if ($search_country && $search_country != '-1') {
  295. $sql .= " AND s.fk_pays IN (".$db->sanitize($search_country).')';
  296. }
  297. if ($search_sale > 0) {
  298. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $search_sale);
  299. }
  300. if ($sall) {
  301. $sql .= natural_search(array_keys($fieldstosearchall), $sall);
  302. }
  303. if ($search_user > 0) {
  304. $sql .= " AND ec.fk_c_type_contact = tc.rowid AND tc.element='contrat' AND tc.source='internal' AND ec.element_id = c.rowid AND ec.fk_socpeople = ".((int) $search_user);
  305. }
  306. // Search for tag/category ($searchCategoryProductList is an array of ID)
  307. $searchCategoryProductOperator = -1;
  308. $searchCategoryProductList = array($search_product_category);
  309. if (!empty($searchCategoryProductList)) {
  310. $searchCategoryProductSqlList = array();
  311. $listofcategoryid = '';
  312. foreach ($searchCategoryProductList as $searchCategoryProduct) {
  313. if (intval($searchCategoryProduct) == -2) {
  314. $searchCategoryProductSqlList[] = "NOT EXISTS (SELECT ck.fk_product FROM ".MAIN_DB_PREFIX."categorie_product as ck, ".MAIN_DB_PREFIX."contratdet as cd WHERE cd.fk_contrat = c.rowid AND cd.fk_product = ck.fk_product)";
  315. } elseif (intval($searchCategoryProduct) > 0) {
  316. if ($searchCategoryProductOperator == 0) {
  317. $searchCategoryProductSqlList[] = " EXISTS (SELECT ck.fk_product FROM ".MAIN_DB_PREFIX."categorie_product as ck, ".MAIN_DB_PREFIX."contratdet as cd WHERE cd.fk_contrat = c.rowid AND cd.fk_product = ck.fk_product AND ck.fk_categorie = ".((int) $searchCategoryProduct).")";
  318. } else {
  319. $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryProduct);
  320. }
  321. }
  322. }
  323. if ($listofcategoryid) {
  324. $searchCategoryProductSqlList[] = " EXISTS (SELECT ck.fk_product FROM ".MAIN_DB_PREFIX."categorie_product as ck, ".MAIN_DB_PREFIX."contratdet as cd WHERE cd.fk_contrat = c.rowid AND cd.fk_product = ck.fk_product AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))";
  325. }
  326. if ($searchCategoryProductOperator == 1) {
  327. if (!empty($searchCategoryProductSqlList)) {
  328. $sql .= " AND (".implode(' OR ', $searchCategoryProductSqlList).")";
  329. }
  330. } else {
  331. if (!empty($searchCategoryProductSqlList)) {
  332. $sql .= " AND (".implode(' AND ', $searchCategoryProductSqlList).")";
  333. }
  334. }
  335. }
  336. $searchCategoryCustomerSqlList = array();
  337. if ($searchCategoryCustomerOperator == 1) {
  338. $existsCategoryCustomerList = array();
  339. foreach ($searchCategoryCustomerList as $searchCategoryCustomer) {
  340. if (intval($searchCategoryCustomer) == -2) {
  341. $sqlCategoryCustomerNotExists = " NOT EXISTS (";
  342. $sqlCategoryCustomerNotExists .= " SELECT cat_cus.fk_soc";
  343. $sqlCategoryCustomerNotExists .= " FROM ".$db->prefix()."categorie_societe AS cat_cus";
  344. $sqlCategoryCustomerNotExists .= " WHERE cat_cus.fk_soc = s.rowid";
  345. $sqlCategoryCustomerNotExists .= " )";
  346. $searchCategoryCustomerSqlList[] = $sqlCategoryCustomerNotExists;
  347. } elseif (intval($searchCategoryCustomer) > 0) {
  348. $existsCategoryCustomerList[] = $db->escape($searchCategoryCustomer);
  349. }
  350. }
  351. if (!empty($existsCategoryCustomerList)) {
  352. $sqlCategoryCustomerExists = " EXISTS (";
  353. $sqlCategoryCustomerExists .= " SELECT cat_cus.fk_soc";
  354. $sqlCategoryCustomerExists .= " FROM ".$db->prefix()."categorie_societe AS cat_cus";
  355. $sqlCategoryCustomerExists .= " WHERE cat_cus.fk_soc = s.rowid";
  356. $sqlCategoryCustomerExists .= " AND cat_cus.fk_categorie IN (".$db->sanitize(implode(',', $existsCategoryCustomerList)).")";
  357. $sqlCategoryCustomerExists .= " )";
  358. $searchCategoryCustomerSqlList[] = $sqlCategoryCustomerExists;
  359. }
  360. if (!empty($searchCategoryCustomerSqlList)) {
  361. $sql .= " AND (".implode(' OR ', $searchCategoryCustomerSqlList).")";
  362. }
  363. } else {
  364. foreach ($searchCategoryCustomerList as $searchCategoryCustomer) {
  365. if (intval($searchCategoryCustomer) == -2) {
  366. $sqlCategoryCustomerNotExists = " NOT EXISTS (";
  367. $sqlCategoryCustomerNotExists .= " SELECT cat_cus.fk_soc";
  368. $sqlCategoryCustomerNotExists .= " FROM ".$db->prefix()."categorie_societe AS cat_cus";
  369. $sqlCategoryCustomerNotExists .= " WHERE cat_cus.fk_soc = s.rowid";
  370. $sqlCategoryCustomerNotExists .= " )";
  371. $searchCategoryCustomerSqlList[] = $sqlCategoryCustomerNotExists;
  372. } elseif (intval($searchCategoryCustomer) > 0) {
  373. $searchCategoryCustomerSqlList[] = "s.rowid IN (SELECT fk_soc FROM ".$db->prefix()."categorie_societe WHERE fk_categorie = ".((int) $searchCategoryCustomer).")";
  374. }
  375. }
  376. if (!empty($searchCategoryCustomerSqlList)) {
  377. $sql .= " AND (".implode(' AND ', $searchCategoryCustomerSqlList).")";
  378. }
  379. }
  380. // Add where from extra fields
  381. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
  382. // Add where from hooks
  383. $parameters = array();
  384. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  385. $sql .= $hookmanager->resPrint;
  386. $sql .= " GROUP BY c.rowid, c.ref, c.datec, c.tms, c.date_contrat, c.statut, c.ref_customer, c.ref_supplier, c.note_private, c.note_public, c.entity,";
  387. $sql .= ' s.rowid, s.nom, s.name_alias, s.email, s.town, s.zip, s.fk_pays, s.client, s.code_client, s.status, s.logo,';
  388. $sql .= " typent.code,";
  389. $sql .= " state.code_departement, state.nom";
  390. // Add fields from extrafields
  391. if (!empty($extrafields->attributes[$object->table_element]['label'])) {
  392. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
  393. $sql .= ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? ", ef.".$key : '');
  394. }
  395. }
  396. // Add where from hooks
  397. $parameters = array('search_dfyear' => $search_dfyear, 'search_op2df'=>$search_op2df);
  398. $reshook = $hookmanager->executeHooks('printFieldListGroupBy', $parameters, $object); // Note that $action and $object may have been modified by hook
  399. $sql .= $hookmanager->resPrint;
  400. // Add HAVING from hooks
  401. $parameters = array('search_dfyear' => $search_dfyear, 'search_op2df'=>$search_op2df);
  402. $reshook = $hookmanager->executeHooks('printFieldListHaving', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  403. if (empty($reshook)) {
  404. if ($search_dfyear > 0 && $search_op2df) {
  405. if ($search_op2df == '<=') {
  406. $sql .= " HAVING MIN(".$db->ifsql("cd.statut=4", "cd.date_fin_validite", "null").") <= '".$db->idate(dol_get_last_day($search_dfyear, $search_dfmonth, false))."'";
  407. } elseif ($search_op2df == '>=') {
  408. $sql .= " HAVING MIN(".$db->ifsql("cd.statut=4", "cd.date_fin_validite", "null").") >= '".$db->idate(dol_get_first_day($search_dfyear, $search_dfmonth, false))."'";
  409. } else {
  410. $sql .= " HAVING MIN(".$db->ifsql("cd.statut=4", "cd.date_fin_validite", "null").") <= '".$db->idate(dol_get_last_day($search_dfyear, $search_dfmonth, false))."' AND MIN(".$db->ifsql("cd.statut=4", "cd.date_fin_validite", "null").") >= '".$db->idate(dol_get_first_day($search_dfyear, $search_dfmonth, false))."'";
  411. }
  412. }
  413. }
  414. $sql .= $hookmanager->resPrint;
  415. $nbtotalofrecords = '';
  416. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  417. //$result = $db->query($sql);
  418. //$nbtotalofrecords = $db->num_rows($result);
  419. if ($search_dfyear > 0 && $search_op2df) {
  420. $resql = $db->query($sql, 0, 'auto', 1);
  421. while ($db->fetch_object($resql)) {
  422. if (empty($nbtotalofrecords)) {
  423. $nbtotalofrecords = 1; // We can't make +1 because init value is ''
  424. } else {
  425. $nbtotalofrecords++;
  426. }
  427. }
  428. } else {
  429. $sqlforcount = preg_replace('/^SELECT[a-zA-Z0-9\._\s\(\),=<>\:\-\']+\sFROM/Ui', 'SELECT COUNT(*) as nbtotalofrecords FROM', $sql);
  430. $sqlforcount = preg_replace('/LEFT JOIN '.MAIN_DB_PREFIX.'contratdet as cd ON c.rowid = cd.fk_contrat/', '', $sqlforcount);
  431. $sqlforcount = preg_replace('/GROUP BY.*$/', '', $sqlforcount);
  432. $resql = $db->query($sqlforcount);
  433. $objforcount = $db->fetch_object($resql);
  434. $nbtotalofrecords = $objforcount->nbtotalofrecords;
  435. }
  436. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  437. $page = 0;
  438. $offset = 0;
  439. }
  440. }
  441. $sql .= $db->order($sortfield, $sortorder);
  442. if ($limit) {
  443. $sql .= $db->plimit($limit + 1, $offset);
  444. }
  445. $resql = $db->query($sql);
  446. if (!$resql) {
  447. dol_print_error($db);
  448. exit;
  449. }
  450. $num = $db->num_rows($resql);
  451. // Direct jump if only one record found
  452. if ($num == 1 && !empty($conf->global->MAIN_SEARCH_DIRECT_OPEN_IF_ONLY_ONE) && $sall && !$page) {
  453. $obj = $db->fetch_object($resql);
  454. $id = $obj->rowid;
  455. header("Location: ".DOL_URL_ROOT.'/contrat/card.php?id='.$id);
  456. exit;
  457. }
  458. // Output page
  459. // --------------------------------------------------------------------
  460. llxHeader('', $langs->trans("Contracts"));
  461. $i = 0;
  462. $arrayofselected = is_array($toselect) ? $toselect : array();
  463. if ($socid > 0) {
  464. $soc = new Societe($db);
  465. $soc->fetch($socid);
  466. if (empty($search_name)) {
  467. $search_name = $soc->name;
  468. }
  469. }
  470. $param = '';
  471. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  472. $param .= '&contextpage='.urlencode($contextpage);
  473. }
  474. if ($limit > 0 && $limit != $conf->liste_limit) {
  475. $param .= '&limit='.urlencode($limit);
  476. }
  477. if ($sall != '') {
  478. $param .= '&sall='.urlencode($sall);
  479. }
  480. if ($search_contract != '') {
  481. $param .= '&search_contract='.urlencode($search_contract);
  482. }
  483. if ($search_name != '') {
  484. $param .= '&search_name='.urlencode($search_name);
  485. }
  486. if ($search_email != '') {
  487. $param .= '&search_email='.urlencode($search_email);
  488. }
  489. if ($search_ref_customer != '') {
  490. $param .= '&search_ref_customer='.urlencode($search_ref_customer);
  491. }
  492. if ($search_ref_supplier != '') {
  493. $param .= '&search_ref_supplier='.urlencode($search_ref_supplier);
  494. }
  495. if ($search_op2df != '') {
  496. $param .= '&search_op2df='.urlencode($search_op2df);
  497. }
  498. if ($search_date_startday > 0) {
  499. $param .= '&search_date_startday='.urlencode($search_date_startday);
  500. }
  501. if ($search_date_startmonth > 0) {
  502. $param .= '&search_date_startmonth='.urlencode($search_date_startmonth);
  503. }
  504. if ($search_date_startyear > 0) {
  505. $param .= '&search_date_startyear='.urlencode($search_date_startyear);
  506. }
  507. if ($search_date_endday > 0) {
  508. $param .= '&search_date_endday='.urlencode($search_date_endday);
  509. }
  510. if ($search_date_endmonth > 0) {
  511. $param .= '&search_date_endmonth='.urlencode($search_date_endmonth);
  512. }
  513. if ($search_date_endyear > 0) {
  514. $param .= '&search_date_endyear='.urlencode($search_date_endyear);
  515. }
  516. if ($search_dfyear > 0) {
  517. $param .= '&search_dfyear='.urlencode($search_dfyear);
  518. }
  519. if ($search_dfmonth > 0) {
  520. $param .= '&search_dfmonth='.urlencode($search_dfmonth);
  521. }
  522. if ($search_sale > 0) {
  523. $param .= '&search_sale='.urlencode($search_sale);
  524. }
  525. if ($search_user > 0) {
  526. $param .= '&search_user='.urlencode($search_user);
  527. }
  528. if ($search_type_thirdparty > 0) {
  529. $param .= '&search_type_thirdparty='.urlencode($search_type_thirdparty);
  530. }
  531. if ($search_country != '') {
  532. $param .= "&search_country=".urlencode($search_country);
  533. }
  534. if ($search_product_category > 0) {
  535. $param .= '&search_product_category='.urlencode($search_product_category);
  536. }
  537. if ($show_files) {
  538. $param .= '&show_files='.urlencode($show_files);
  539. }
  540. if ($optioncss != '') {
  541. $param .= '&optioncss='.urlencode($optioncss);
  542. }
  543. foreach ($searchCategoryCustomerList as $searchCategoryCustomer) {
  544. $param .= "&search_category_customer_list[]=".urlencode($searchCategoryCustomer);
  545. }
  546. // Add $param from extra fields
  547. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php';
  548. // List of mass actions available
  549. $arrayofmassactions = array(
  550. 'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
  551. 'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
  552. 'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
  553. );
  554. if ($user->rights->contrat->supprimer) {
  555. $arrayofmassactions['predelete'] = img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete");
  556. }
  557. if (in_array($massaction, array('presend', 'predelete'))) {
  558. $arrayofmassactions = array();
  559. }
  560. $massactionbutton = $form->selectMassAction('', $arrayofmassactions);
  561. $url = DOL_URL_ROOT.'/contrat/card.php?action=create';
  562. if (!empty($socid)) {
  563. $url .= '&socid='.((int) $socid);
  564. }
  565. $newcardbutton = dolGetButtonTitle($langs->trans('NewContractSubscription'), '', 'fa fa-plus-circle', $url, '', $user->rights->contrat->creer);
  566. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  567. if ($optioncss != '') {
  568. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  569. }
  570. print '<input type="hidden" name="token" value="'.newToken().'">';
  571. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  572. print '<input type="hidden" name="action" value="list">';
  573. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  574. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  575. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  576. print_barre_liste($langs->trans("Contracts"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'contract', 0, $newcardbutton, '', $limit, 0, 0, 1);
  577. $topicmail = "SendContractRef";
  578. $modelmail = "contract";
  579. $objecttmp = new Contrat($db);
  580. $trackid = 'con'.$object->id;
  581. include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
  582. if ($sall) {
  583. foreach ($fieldstosearchall as $key => $val) {
  584. $fieldstosearchall[$key] = $langs->trans($val);
  585. }
  586. print '<div class="divsearchfieldfilter">'.$langs->trans("FilterOnInto", $sall).join(', ', $fieldstosearchall).'</div>';
  587. }
  588. $moreforfilter = '';
  589. // If the user can view prospects other than his'
  590. if ($user->rights->user->user->lire) {
  591. $langs->load("commercial");
  592. $moreforfilter .= '<div class="divsearchfield">';
  593. $tmptitle = $langs->trans('ThirdPartiesOfSaleRepresentative');
  594. $moreforfilter .= img_picto($tmptitle, 'user', 'class="pictofixedwidth"').$formother->select_salesrepresentatives($search_sale, 'search_sale', $user, 0, $tmptitle, 'widthcentpercentminusx maxwidth300');
  595. $moreforfilter .= '</div>';
  596. }
  597. // If the user can view other users
  598. if ($user->rights->user->user->lire) {
  599. $moreforfilter .= '<div class="divsearchfield">';
  600. $tmptitle = $langs->trans('LinkedToSpecificUsers');
  601. $moreforfilter .= img_picto($tmptitle, 'user', 'class="pictofixedwidth"').$form->select_dolusers($search_user, 'search_user', $tmptitle, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  602. $moreforfilter .= '</div>';
  603. }
  604. // If the user can view categories of products
  605. if (isModEnabled('categorie') && $user->rights->categorie->lire && ($user->rights->produit->lire || $user->rights->service->lire)) {
  606. include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  607. $moreforfilter .= '<div class="divsearchfield">';
  608. $tmptitle = $langs->trans('IncludingProductWithTag');
  609. $cate_arbo = $form->select_all_categories(Categorie::TYPE_PRODUCT, null, 'parent', null, null, 1);
  610. $moreforfilter .= img_picto($tmptitle, 'category', 'class="pictofixedwidth"').$form->selectarray('search_product_category', $cate_arbo, $search_product_category, $tmptitle, 0, 0, '', 0, 0, 0, 0, 'widthcentpercentminusx maxwidth300', 1);
  611. $moreforfilter .= '</div>';
  612. }
  613. // Filter on customer categories
  614. if (!empty($conf->global->MAIN_SEARCH_CATEGORY_CUSTOMER_ON_CONTRACT_LIST) && !empty($conf->categorie->enabled) && $user->rights->categorie->lire) {
  615. $moreforfilter .= '<div class="divsearchfield">';
  616. $tmptitle = $langs->transnoentities('CustomersProspectsCategoriesShort');
  617. $moreforfilter .= img_picto($tmptitle, 'category', 'class="pictofixedwidth"');
  618. $categoriesArr = $form->select_all_categories(Categorie::TYPE_CUSTOMER, '', '', 64, 0, 1);
  619. $categoriesArr[-2] = '- '.$langs->trans('NotCategorized').' -';
  620. $moreforfilter .= Form::multiselectarray('search_category_customer_list', $categoriesArr, $searchCategoryCustomerList, 0, 0, 'minwidth300', 0, 0, '', 'category', $tmptitle);
  621. $moreforfilter .= ' <input type="checkbox" class="valignmiddle" id="search_category_customer_operator" name="search_category_customer_operator" value="1"'.($searchCategoryCustomerOperator == 1 ? ' checked="checked"' : '').'/>';
  622. $moreforfilter .= $form->textwithpicto('', $langs->trans('UseOrOperatorForCategories') . ' : ' . $tmptitle, 1, 'help', '', 0, 2, 'tooltip_cat_cus'); // Tooltip on click
  623. $moreforfilter .= '</div>';
  624. }
  625. $parameters = array();
  626. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  627. if (empty($reshook)) {
  628. $moreforfilter .= $hookmanager->resPrint;
  629. } else {
  630. $moreforfilter = $hookmanager->resPrint;
  631. }
  632. if (!empty($moreforfilter)) {
  633. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  634. print $moreforfilter;
  635. print '</div>';
  636. }
  637. $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
  638. $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage, getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')); // This also change content of $arrayfields
  639. if ($massactionbutton) {
  640. $selectedfields .= $form->showCheckAddButtons('checkforselect', 1);
  641. }
  642. print '<div class="div-table-responsive">';
  643. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  644. print '<tr class="liste_titre_filter">';
  645. // Action column
  646. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  647. print '<td class="liste_titre center">';
  648. $searchpicto = $form->showFilterButtons('left');
  649. print $searchpicto;
  650. print '</td>';
  651. }
  652. if (!empty($arrayfields['c.ref']['checked'])) {
  653. print '<td class="liste_titre">';
  654. print '<input type="text" class="flat" size="3" name="search_contract" value="'.dol_escape_htmltag($search_contract).'">';
  655. print '</td>';
  656. }
  657. if (!empty($arrayfields['c.ref_customer']['checked'])) {
  658. print '<td class="liste_titre">';
  659. print '<input type="text" class="flat" size="6" name="search_ref_customer" value="'.dol_escape_htmltag($search_ref_customer).'">';
  660. print '</td>';
  661. }
  662. if (!empty($arrayfields['c.ref_supplier']['checked'])) {
  663. print '<td class="liste_titre">';
  664. print '<input type="text" class="flat" size="6" name="search_ref_supplier" value="'.dol_escape_htmltag($search_ref_supplier).'">';
  665. print '</td>';
  666. }
  667. if (!empty($arrayfields['s.nom']['checked'])) {
  668. print '<td class="liste_titre">';
  669. print '<input type="text" class="flat" size="8" name="search_name" value="'.dol_escape_htmltag($search_name).'">';
  670. print '</td>';
  671. }
  672. if (!empty($arrayfields['s.email']['checked'])) {
  673. print '<td class="liste_titre">';
  674. print '<input type="text" class="flat" size="6" name="search_email" value="'.dol_escape_htmltag($search_email).'">';
  675. print '</td>';
  676. }
  677. // Town
  678. if (!empty($arrayfields['s.town']['checked'])) {
  679. print '<td class="liste_titre"><input class="flat" type="text" size="6" name="search_town" value="'.dol_escape_htmltag($search_town).'"></td>';
  680. }
  681. // Zip
  682. if (!empty($arrayfields['s.zip']['checked'])) {
  683. print '<td class="liste_titre"><input class="flat" type="text" size="6" name="search_zip" value="'.dol_escape_htmltag($search_zip).'"></td>';
  684. }
  685. // State
  686. if (!empty($arrayfields['state.nom']['checked'])) {
  687. print '<td class="liste_titre">';
  688. print '<input class="flat" size="4" type="text" name="search_state" value="'.dol_escape_htmltag($search_state).'">';
  689. print '</td>';
  690. }
  691. // Country
  692. if (!empty($arrayfields['country.code_iso']['checked'])) {
  693. print '<td class="liste_titre center">';
  694. print $form->select_country($search_country, 'search_country', '', 0, 'minwidth100imp maxwidth100');
  695. print '</td>';
  696. }
  697. // Company type
  698. if (!empty($arrayfields['typent.code']['checked'])) {
  699. print '<td class="liste_titre maxwidthonsmartphone center">';
  700. print $form->selectarray("search_type_thirdparty", $formcompany->typent_array(0), $search_type_thirdparty, 1, 0, 0, '', 0, 0, 0, (empty($conf->global->SOCIETE_SORT_ON_TYPEENT) ? 'ASC' : $conf->global->SOCIETE_SORT_ON_TYPEENT), '', 1);
  701. print '</td>';
  702. }
  703. if (!empty($arrayfields['sale_representative']['checked'])) {
  704. print '<td class="liste_titre"></td>';
  705. }
  706. if (!empty($arrayfields['c.date_contrat']['checked'])) {
  707. print '<td class="liste_titre center">';
  708. print '<div class="nowrap">';
  709. print $form->selectDate($search_date_start ? $search_date_start : -1, 'search_date_start', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('From'));
  710. print '</div>';
  711. print '<div class="nowrap">';
  712. print $form->selectDate($search_date_end ? $search_date_end : -1, 'search_date_end', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('to'));
  713. print '</div>';
  714. print '</td>';
  715. }
  716. // Extra fields
  717. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_input.tpl.php';
  718. // Fields from hook
  719. $parameters = array('arrayfields'=>$arrayfields);
  720. $reshook = $hookmanager->executeHooks('printFieldListOption', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  721. print $hookmanager->resPrint;
  722. // Date creation
  723. if (!empty($arrayfields['c.datec']['checked'])) {
  724. print '<td class="liste_titre">';
  725. print '</td>';
  726. }
  727. // Date modification
  728. if (!empty($arrayfields['c.tms']['checked'])) {
  729. print '<td class="liste_titre">';
  730. print '</td>';
  731. }
  732. // First end date
  733. if (!empty($arrayfields['lower_planned_end_date']['checked'])) {
  734. print '<td class="liste_titre nowraponall center">';
  735. $arrayofoperators = array('0'=>'', '='=>'=', '<='=>'<=', '>='=>'>=');
  736. print $form->selectarray('search_op2df', $arrayofoperators, $search_op2df, 0, 0, 0, '', 0, 0, 0, '', 'maxwidth50imp');
  737. print '</br>';
  738. print $formother->select_month($search_dfmonth, 'search_dfmonth', 1, 0);
  739. print ' ';
  740. print $formother->selectyear($search_dfyear, 'search_dfyear', 1, 20, 5, 0, 0, '');
  741. print '</td>';
  742. }
  743. // Status
  744. if (!empty($arrayfields['status']['checked'])) {
  745. print '<td class="liste_titre right" colspan="4"></td>';
  746. }
  747. // Action column
  748. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  749. print '<td class="liste_titre center">';
  750. $searchpicto = $form->showFilterButtons();
  751. print $searchpicto;
  752. print '</td>';
  753. }
  754. print "</tr>\n";
  755. print '<tr class="liste_titre">';
  756. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  757. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
  758. }
  759. if (!empty($arrayfields['c.ref']['checked'])) {
  760. print_liste_field_titre($arrayfields['c.ref']['label'], $_SERVER["PHP_SELF"], "c.ref", "", $param, '', $sortfield, $sortorder);
  761. }
  762. if (!empty($arrayfields['c.ref_customer']['checked'])) {
  763. print_liste_field_titre($arrayfields['c.ref_customer']['label'], $_SERVER["PHP_SELF"], "c.ref_customer", "", $param, '', $sortfield, $sortorder);
  764. }
  765. if (!empty($arrayfields['c.ref_supplier']['checked'])) {
  766. print_liste_field_titre($arrayfields['c.ref_supplier']['label'], $_SERVER["PHP_SELF"], "c.ref_supplier", "", $param, '', $sortfield, $sortorder);
  767. }
  768. if (!empty($arrayfields['s.nom']['checked'])) {
  769. print_liste_field_titre($arrayfields['s.nom']['label'], $_SERVER["PHP_SELF"], "s.nom", "", $param, '', $sortfield, $sortorder);
  770. }
  771. if (!empty($arrayfields['s.email']['checked'])) {
  772. print_liste_field_titre($arrayfields['s.email']['label'], $_SERVER["PHP_SELF"], "s.email", "", $param, '', $sortfield, $sortorder);
  773. }
  774. if (!empty($arrayfields['s.town']['checked'])) {
  775. print_liste_field_titre($arrayfields['s.town']['label'], $_SERVER["PHP_SELF"], 's.town', '', $param, '', $sortfield, $sortorder);
  776. }
  777. if (!empty($arrayfields['s.zip']['checked'])) {
  778. print_liste_field_titre($arrayfields['s.zip']['label'], $_SERVER["PHP_SELF"], 's.zip', '', $param, '', $sortfield, $sortorder);
  779. }
  780. if (!empty($arrayfields['state.nom']['checked'])) {
  781. print_liste_field_titre($arrayfields['state.nom']['label'], $_SERVER["PHP_SELF"], "state.nom", "", $param, '', $sortfield, $sortorder);
  782. }
  783. if (!empty($arrayfields['country.code_iso']['checked'])) {
  784. print_liste_field_titre($arrayfields['country.code_iso']['label'], $_SERVER["PHP_SELF"], "country.code_iso", "", $param, '', $sortfield, $sortorder, 'center ');
  785. }
  786. if (!empty($arrayfields['typent.code']['checked'])) {
  787. print_liste_field_titre($arrayfields['typent.code']['label'], $_SERVER["PHP_SELF"], "typent.code", "", $param, '', $sortfield, $sortorder, 'center ');
  788. }
  789. if (!empty($arrayfields['sale_representative']['checked'])) {
  790. print_liste_field_titre($arrayfields['sale_representative']['label'], $_SERVER["PHP_SELF"], "", "", $param, '', $sortfield, $sortorder);
  791. }
  792. if (!empty($arrayfields['c.date_contrat']['checked'])) {
  793. print_liste_field_titre($arrayfields['c.date_contrat']['label'], $_SERVER["PHP_SELF"], "c.date_contrat", "", $param, '', $sortfield, $sortorder, 'center ');
  794. }
  795. // Extra fields
  796. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_title.tpl.php';
  797. // Hook fields
  798. $parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
  799. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  800. print $hookmanager->resPrint;
  801. if (!empty($arrayfields['c.datec']['checked'])) {
  802. print_liste_field_titre($arrayfields['c.datec']['label'], $_SERVER["PHP_SELF"], "c.datec", "", $param, '', $sortfield, $sortorder, 'center nowrap ');
  803. }
  804. if (!empty($arrayfields['c.tms']['checked'])) {
  805. print_liste_field_titre($arrayfields['c.tms']['label'], $_SERVER["PHP_SELF"], "c.tms", "", $param, '', $sortfield, $sortorder, 'center nowrap ');
  806. }
  807. if (!empty($arrayfields['lower_planned_end_date']['checked'])) {
  808. print_liste_field_titre($arrayfields['lower_planned_end_date']['label'], $_SERVER["PHP_SELF"], "lower_planned_end_date", "", $param, '', $sortfield, $sortorder, 'center ');
  809. }
  810. if (!empty($arrayfields['status']['checked'])) {
  811. print_liste_field_titre($staticcontratligne->LibStatut(0, 3, -1, 'class="nochangebackground"'), '', '', '', '', 'width="16"');
  812. print_liste_field_titre($staticcontratligne->LibStatut(4, 3, 0, 'class="nochangebackground"'), '', '', '', '', 'width="16"');
  813. print_liste_field_titre($staticcontratligne->LibStatut(4, 3, 1, 'class="nochangebackground"'), '', '', '', '', 'width="16"');
  814. print_liste_field_titre($staticcontratligne->LibStatut(5, 3, -1, 'class="nochangebackground"'), '', '', '', '', 'width="16"');
  815. }
  816. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  817. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
  818. }
  819. print "</tr>\n";
  820. $totalarray = array();
  821. $totalarray['nbfield'] = 0;
  822. $typenArray = array();
  823. $cacheCountryIDCode = array();
  824. while ($i < min($num, $limit)) {
  825. $obj = $db->fetch_object($resql);
  826. $contracttmp->ref = $obj->ref;
  827. $contracttmp->id = $obj->rowid;
  828. $contracttmp->ref_customer = $obj->ref_customer;
  829. $contracttmp->ref_supplier = $obj->ref_supplier;
  830. if ($obj->socid > 0) {
  831. $result = $socstatic->fetch($obj->socid);
  832. }
  833. /*$socstatic->id = $obj->socid;
  834. $socstatic->name = $obj->name;
  835. $socstatic->name_alias = $obj->name_alias;
  836. $socstatic->email = $obj->email;
  837. $socstatic->status = $obj->company_status;
  838. $socstatic->logo = $obj->logo;
  839. $socstatic->country_id = $obj->country_id;
  840. $socstatic->country_code = '';
  841. $socstatic->country = '';*/
  842. if ($obj->country_id > 0) {
  843. if (!isset($cacheCountryIDCode[$obj->country_id]['code'])) {
  844. $tmparray = getCountry($obj->country_id, 'all');
  845. $cacheCountryIDCode[$obj->country_id] = array('code'=> empty($tmparray['code']) ? '' : $tmparray['code'], 'label' => empty($tmparray['label']) ? '' : $tmparray['label']);
  846. }
  847. $socstatic->country_code = $cacheCountryIDCode[$obj->country_id]['code'];
  848. $socstatic->country = $cacheCountryIDCode[$obj->country_id]['label'];
  849. }
  850. print '<tr class="oddeven">';
  851. // Action column
  852. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  853. print '<td class="nowrap center">';
  854. if ($massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
  855. $selected = 0;
  856. if (in_array($obj->rowid, $arrayofselected)) {
  857. $selected = 1;
  858. }
  859. print '<input id="cb'.$obj->rowid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->rowid.'"'.($selected ? ' checked="checked"' : '').'>';
  860. }
  861. print '</td>';
  862. }
  863. // Ref
  864. if (!empty($arrayfields['c.ref']['checked'])) {
  865. print '<td class="nowraponall">';
  866. print $contracttmp->getNomUrl(1);
  867. if ($obj->nb_late) {
  868. print img_warning($langs->trans("Late"));
  869. }
  870. if (!empty($obj->note_private) || !empty($obj->note_public)) {
  871. print ' <span class="note">';
  872. print '<a href="'.DOL_URL_ROOT.'/contrat/note.php?id='.$obj->rowid.'&save_lastsearch_values=1">'.img_picto($langs->trans("ViewPrivateNote"), 'note').'</a>';
  873. print '</span>';
  874. }
  875. $filename = dol_sanitizeFileName($obj->ref);
  876. $filedir = $conf->contrat->multidir_output[$obj->entity].'/'.dol_sanitizeFileName($obj->ref);
  877. $urlsource = $_SERVER['PHP_SELF'].'?id='.$obj->rowid;
  878. print $formfile->getDocumentsLink($contracttmp->element, $filename, $filedir);
  879. print '</td>';
  880. print '</td>';
  881. }
  882. // Ref thirdparty
  883. if (!empty($arrayfields['c.ref_customer']['checked'])) {
  884. print '<td class="tdoverflowmax200" title="'.dol_escape_htmltag(dol_string_nohtmltag($contracttmp->getFormatedCustomerRef($obj->ref_customer))).'">'.$contracttmp->getFormatedCustomerRef($obj->ref_customer).'</td>';
  885. }
  886. if (!empty($arrayfields['c.ref_supplier']['checked'])) {
  887. print '<td class="tdoverflowmax200" title="'.dol_escape_htmltag($obj->ref_supplier).'">'.dol_escape_htmltag($obj->ref_supplier).'</td>';
  888. }
  889. if (!empty($arrayfields['s.nom']['checked'])) {
  890. print '<td class="tdoverflowmax150">';
  891. if ($obj->socid > 0) {
  892. // TODO Use a cache for this string
  893. print $socstatic->getNomUrl(1, '');
  894. }
  895. print '</td>';
  896. }
  897. // Email
  898. if (!empty($arrayfields['s.email']['checked'])) {
  899. print '<td class="tdoverflowmax200" title="'.dol_escape_htmltag($obj->email).'">'.dol_print_email($obj->email, 0, $obj->socid, 0, 0, 1, 1).'</td>';
  900. }
  901. // Town
  902. if (!empty($arrayfields['s.town']['checked'])) {
  903. print '<td class="nocellnopadd">';
  904. print $obj->town;
  905. print '</td>';
  906. if (!$i) {
  907. $totalarray['nbfield']++;
  908. }
  909. }
  910. // Zip
  911. if (!empty($arrayfields['s.zip']['checked'])) {
  912. print '<td class="nocellnopadd">';
  913. print $obj->zip;
  914. print '</td>';
  915. if (!$i) {
  916. $totalarray['nbfield']++;
  917. }
  918. }
  919. // State
  920. if (!empty($arrayfields['state.nom']['checked'])) {
  921. print "<td>".$obj->state_name."</td>\n";
  922. if (!$i) {
  923. $totalarray['nbfield']++;
  924. }
  925. }
  926. // Country
  927. if (!empty($arrayfields['country.code_iso']['checked'])) {
  928. print '<td class="center tdoverflowmax100" title="'.dol_escape_htmltag($socstatic->country).'">';
  929. print dol_escape_htmltag($socstatic->country);
  930. print '</td>';
  931. if (!$i) {
  932. $totalarray['nbfield']++;
  933. }
  934. }
  935. // Type ent
  936. if (!empty($arrayfields['typent.code']['checked'])) {
  937. print '<td class="center">';
  938. if (count($typenArray) == 0) {
  939. $typenArray = $formcompany->typent_array(1);
  940. }
  941. print $typenArray[$obj->typent_code];
  942. print '</td>';
  943. if (!$i) {
  944. $totalarray['nbfield']++;
  945. }
  946. }
  947. if (!empty($arrayfields['sale_representative']['checked'])) {
  948. // Sales representatives
  949. print '<td>';
  950. if ($obj->socid > 0) {
  951. $listsalesrepresentatives = $socstatic->getSalesRepresentatives($user);
  952. if ($listsalesrepresentatives < 0) {
  953. dol_print_error($db);
  954. }
  955. $nbofsalesrepresentative = count($listsalesrepresentatives);
  956. if ($nbofsalesrepresentative > 6) {
  957. // We print only number
  958. print $nbofsalesrepresentative;
  959. } elseif ($nbofsalesrepresentative > 0) {
  960. $userstatic = new User($db);
  961. $j = 0;
  962. foreach ($listsalesrepresentatives as $val) {
  963. $userstatic->id = $val['id'];
  964. $userstatic->lastname = $val['lastname'];
  965. $userstatic->firstname = $val['firstname'];
  966. $userstatic->email = $val['email'];
  967. $userstatic->statut = $val['statut'];
  968. $userstatic->entity = $val['entity'];
  969. $userstatic->photo = $val['photo'];
  970. $userstatic->login = $val['login'];
  971. $userstatic->phone = $val['phone'];
  972. $userstatic->job = $val['job'];
  973. $userstatic->gender = $val['gender'];
  974. //print '<div class="float">':
  975. print ($nbofsalesrepresentative < 2) ? $userstatic->getNomUrl(-1, '', 0, 0, 12) : $userstatic->getNomUrl(-2);
  976. $j++;
  977. if ($j < $nbofsalesrepresentative) {
  978. print ' ';
  979. }
  980. //print '</div>';
  981. }
  982. }
  983. //else print $langs->trans("NoSalesRepresentativeAffected");
  984. } else {
  985. print '&nbsp;';
  986. }
  987. print '</td>';
  988. }
  989. // Date
  990. if (!empty($arrayfields['c.date_contrat']['checked'])) {
  991. print '<td class="center">'.dol_print_date($db->jdate($obj->date_contrat), 'day', 'tzserver').'</td>';
  992. }
  993. // Extra fields
  994. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php';
  995. // Fields from hook
  996. $parameters = array('arrayfields'=>$arrayfields, 'obj'=>$obj, 'i'=>$i, 'totalarray'=>&$totalarray);
  997. $reshook = $hookmanager->executeHooks('printFieldListValue', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  998. print $hookmanager->resPrint;
  999. // Date creation
  1000. if (!empty($arrayfields['c.datec']['checked'])) {
  1001. print '<td class="center nowrap">';
  1002. print dol_print_date($db->jdate($obj->date_creation), 'dayhour', 'tzuser');
  1003. print '</td>';
  1004. if (!$i) {
  1005. $totalarray['nbfield']++;
  1006. }
  1007. }
  1008. // Date modification
  1009. if (!empty($arrayfields['c.tms']['checked'])) {
  1010. print '<td class="center nowrap">';
  1011. print dol_print_date($db->jdate($obj->date_update), 'dayhour', 'tzuser');
  1012. print '</td>';
  1013. if (!$i) {
  1014. $totalarray['nbfield']++;
  1015. }
  1016. }
  1017. // Date lower end date
  1018. if (!empty($arrayfields['lower_planned_end_date']['checked'])) {
  1019. print '<td class="center nowrapforall">';
  1020. print dol_print_date($db->jdate($obj->lower_planned_end_date), 'day', 'tzuser');
  1021. print '</td>';
  1022. if (!$i) {
  1023. $totalarray['nbfield']++;
  1024. }
  1025. }
  1026. // Status
  1027. if (!empty($arrayfields['status']['checked'])) {
  1028. print '<td class="center">'.($obj->nb_initial > 0 ? $obj->nb_initial : '').'</td>';
  1029. print '<td class="center">'.($obj->nb_running > 0 ? $obj->nb_running : '').'</td>';
  1030. print '<td class="center">'.($obj->nb_expired > 0 ? $obj->nb_expired : '').'</td>';
  1031. print '<td class="center">'.($obj->nb_closed > 0 ? $obj->nb_closed : '').'</td>';
  1032. }
  1033. // Action column
  1034. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  1035. print '<td class="nowrap center">';
  1036. if ($massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
  1037. $selected = 0;
  1038. if (in_array($obj->rowid, $arrayofselected)) {
  1039. $selected = 1;
  1040. }
  1041. print '<input id="cb'.$obj->rowid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->rowid.'"'.($selected ? ' checked="checked"' : '').'>';
  1042. }
  1043. print '</td>';
  1044. }
  1045. if (!$i) {
  1046. $totalarray['nbfield']++;
  1047. }
  1048. print "</tr>\n";
  1049. $i++;
  1050. }
  1051. // If no record found
  1052. if ($num == 0) {
  1053. $colspan = 4; // Include the 4 columns of status
  1054. foreach ($arrayfields as $key => $val) {
  1055. if (!empty($val['checked'])) {
  1056. $colspan++;
  1057. }
  1058. }
  1059. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
  1060. }
  1061. $db->free($resql);
  1062. $parameters = array('arrayfields'=>$arrayfields, 'sql'=>$sql);
  1063. $reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1064. print $hookmanager->resPrint;
  1065. print '</table>';
  1066. print '</div>';
  1067. print '</form>';
  1068. $hidegeneratedfilelistifempty = 1;
  1069. if ($massaction == 'builddoc' || $action == 'remove_file' || $show_files) {
  1070. $hidegeneratedfilelistifempty = 0;
  1071. }
  1072. // Show list of available documents
  1073. $urlsource = $_SERVER['PHP_SELF'].'?sortfield='.$sortfield.'&sortorder='.$sortorder;
  1074. $urlsource .= str_replace('&amp;', '&', $param);
  1075. $filedir = $diroutputmassaction;
  1076. $genallowed = $user->rights->contrat->lire;
  1077. $delallowed = $user->rights->contrat->lire;
  1078. print $formfile->showdocuments('massfilesarea_contract', '', $filedir, $urlsource, 0, $delallowed, '', 1, 1, 0, 48, 1, $param, $title, '', '', '', null, $hidegeneratedfilelistifempty);
  1079. llxFooter();
  1080. $db->close();