html.form.class.php 174 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136
  1. <?php
  2. /* Copyright (c) 2002-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  5. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  6. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  7. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  8. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  9. * Copyright (C) 2006 Marc Barilley/Ocebo <marc@ocebo.com>
  10. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerker@telenet.be>
  11. * Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
  12. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  13. * Copyright (C) 2010 Philippe Grand <philippe.grand@atoo-net.com>
  14. * Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
  15. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  16. * Copyright (C) 2013 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. */
  31. /**
  32. * \file htdocs/core/class/html.form.class.php
  33. * \ingroup core
  34. * \brief File of class with all html predefined components
  35. */
  36. /**
  37. * Class to manage generation of HTML components
  38. * Only common components must be here.
  39. */
  40. class Form
  41. {
  42. var $db;
  43. var $error;
  44. var $num;
  45. // Cache arrays
  46. var $cache_types_paiements=array();
  47. var $cache_conditions_paiements=array();
  48. var $cache_availability=array();
  49. var $cache_demand_reason=array();
  50. var $cache_types_fees=array();
  51. var $cache_vatrates=array();
  52. var $tva_taux_value;
  53. var $tva_taux_libelle;
  54. /**
  55. * Constructor
  56. *
  57. * @param DoliDB $db Database handler
  58. */
  59. public function __construct($db)
  60. {
  61. $this->db = $db;
  62. }
  63. /**
  64. * Output key field for an editable field
  65. *
  66. * @param string $text Text of label or key to translate
  67. * @param string $htmlname Name of select field
  68. * @param string $preselected Name of Value to show/edit (not used in this function)
  69. * @param object $object Object
  70. * @param boolean $perm Permission to allow button to edit parameter
  71. * @param string $typeofdata Type of data ('string' by default, 'email', 'numeric:99', 'text' or 'textarea:rows:cols', 'day' or 'datepicker', 'ckeditor:dolibarr_zzz:width:height:savemethod:1:rows:cols', 'select;xxx[:class]'...)
  72. * @param string $moreparam More param to add on a href URL
  73. * @return string HTML edit field
  74. */
  75. function editfieldkey($text, $htmlname, $preselected, $object, $perm, $typeofdata='string', $moreparam='')
  76. {
  77. global $conf,$langs;
  78. $ret='';
  79. // TODO change for compatibility
  80. if (! empty($conf->global->MAIN_USE_JQUERY_JEDITABLE) && ! preg_match('/^select;/',$typeofdata))
  81. {
  82. if (! empty($perm))
  83. {
  84. $tmp=explode(':',$typeofdata);
  85. $ret.= '<div class="editkey_'.$tmp[0].(! empty($tmp[1]) ? ' '.$tmp[1] : '').'" id="'.$htmlname.'">';
  86. $ret.= $langs->trans($text);
  87. $ret.= '</div>'."\n";
  88. }
  89. else
  90. {
  91. $ret.= $langs->trans($text);
  92. }
  93. }
  94. else
  95. {
  96. $ret.='<table class="nobordernopadding" width="100%"><tr><td class="nowrap">';
  97. $ret.=$langs->trans($text);
  98. $ret.='</td>';
  99. if (GETPOST('action') != 'edit'.$htmlname && $perm) $ret.='<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=edit'.$htmlname.'&amp;id='.$object->id.$moreparam.'">'.img_edit($langs->trans('Edit'),1).'</a></td>';
  100. $ret.='</tr></table>';
  101. }
  102. return $ret;
  103. }
  104. /**
  105. * Output val field for an editable field
  106. *
  107. * @param string $text Text of label (not used in this function)
  108. * @param string $htmlname Name of select field
  109. * @param string $value Value to show/edit
  110. * @param object $object Object
  111. * @param boolean $perm Permission to allow button to edit parameter
  112. * @param string $typeofdata Type of data ('string' by default, 'email', 'numeric:99', 'text' or 'textarea:rows:cols', 'day' or 'datepicker', 'ckeditor:dolibarr_zzz:width:height:savemethod:toolbarstartexpanded:rows:cols', 'select:xxx'...)
  113. * @param string $editvalue When in edit mode, use this value as $value instead of value
  114. * @param object $extObject External object
  115. * @param string $success Success message
  116. * @param string $moreparam More param to add on a href URL
  117. * @return string HTML edit field
  118. */
  119. function editfieldval($text, $htmlname, $value, $object, $perm, $typeofdata='string', $editvalue='', $extObject=null, $success=null, $moreparam='')
  120. {
  121. global $conf,$langs,$db;
  122. $ret='';
  123. // When option to edit inline is activated
  124. if (! empty($conf->global->MAIN_USE_JQUERY_JEDITABLE) && ! preg_match('/^select;|datehourpicker/',$typeofdata)) // FIXME add jquery timepicker
  125. {
  126. $ret.=$this->editInPlace($object, $value, $htmlname, $perm, $typeofdata, $editvalue, $extObject, $success);
  127. }
  128. else
  129. {
  130. if (GETPOST('action') == 'edit'.$htmlname)
  131. {
  132. $ret.="\n";
  133. $ret.='<form method="post" action="'.$_SERVER["PHP_SELF"].($moreparam?'?'.$moreparam:'').'">';
  134. $ret.='<input type="hidden" name="action" value="set'.$htmlname.'">';
  135. $ret.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  136. $ret.='<input type="hidden" name="id" value="'.$object->id.'">';
  137. $ret.='<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  138. $ret.='<tr><td>';
  139. if (preg_match('/^(string|email|numeric)/',$typeofdata))
  140. {
  141. $tmp=explode(':',$typeofdata);
  142. $ret.='<input type="text" id="'.$htmlname.'" name="'.$htmlname.'" value="'.($editvalue?$editvalue:$value).'"'.($tmp[1]?' size="'.$tmp[1].'"':'').'>';
  143. }
  144. else if (preg_match('/^text/',$typeofdata) || preg_match('/^note/',$typeofdata))
  145. {
  146. $tmp=explode(':',$typeofdata);
  147. $ret.='<textarea id="'.$htmlname.'" name="'.$htmlname.'" wrap="soft" rows="'.($tmp[1]?$tmp[1]:'20').'" cols="'.($tmp[2]?$tmp[2]:'100').'">'.($editvalue?$editvalue:$value).'</textarea>';
  148. }
  149. else if ($typeofdata == 'day' || $typeofdata == 'datepicker')
  150. {
  151. $ret.=$this->form_date($_SERVER['PHP_SELF'].'?id='.$object->id,$value,$htmlname);
  152. }
  153. else if ($typeofdata == 'datehourpicker')
  154. {
  155. $ret.=$this->form_date($_SERVER['PHP_SELF'].'?id='.$object->id,$value,$htmlname,1,1);
  156. }
  157. else if (preg_match('/^select;/',$typeofdata))
  158. {
  159. $arraydata=explode(',',preg_replace('/^select;/','',$typeofdata));
  160. foreach($arraydata as $val)
  161. {
  162. $tmp=explode(':',$val);
  163. $arraylist[$tmp[0]]=$tmp[1];
  164. }
  165. $ret.=$this->selectarray($htmlname,$arraylist,$value);
  166. }
  167. else if (preg_match('/^ckeditor/',$typeofdata))
  168. {
  169. $tmp=explode(':',$typeofdata);
  170. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  171. $doleditor=new DolEditor($htmlname, ($editvalue?$editvalue:$value), ($tmp[2]?$tmp[2]:''), ($tmp[3]?$tmp[3]:'100'), ($tmp[1]?$tmp[1]:'dolibarr_notes'), 'In', ($tmp[5]?$tmp[5]:0), true, true, ($tmp[6]?$tmp[6]:'20'), ($tmp[7]?$tmp[7]:'100'));
  172. $ret.=$doleditor->Create(1);
  173. }
  174. $ret.='</td>';
  175. if ($typeofdata != 'day' && $typeofdata != 'datepicker' && $typeofdata != 'datehourpicker') $ret.='<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  176. $ret.='</tr></table>'."\n";
  177. $ret.='</form>'."\n";
  178. }
  179. else
  180. {
  181. if ($typeofdata == 'email') $ret.=dol_print_email($value,0,0,0,0,1);
  182. elseif (preg_match('/^text/',$typeofdata) || preg_match('/^note/',$typeofdata)) $ret.=dol_htmlentitiesbr($value);
  183. elseif ($typeofdata == 'day' || $typeofdata == 'datepicker') $ret.=dol_print_date($value,'day');
  184. elseif ($typeofdata == 'datehourpicker') $ret.=dol_print_date($value,'dayhour');
  185. else if (preg_match('/^select;/',$typeofdata))
  186. {
  187. $arraydata=explode(',',preg_replace('/^select;/','',$typeofdata));
  188. foreach($arraydata as $val)
  189. {
  190. $tmp=explode(':',$val);
  191. $arraylist[$tmp[0]]=$tmp[1];
  192. }
  193. $ret.=$arraylist[$value];
  194. }
  195. else if (preg_match('/^ckeditor/',$typeofdata))
  196. {
  197. $tmpcontent=dol_htmlentitiesbr($value);
  198. if (! empty($conf->global->MAIN_DISABLE_NOTES_TAB))
  199. {
  200. $firstline=preg_replace('/<br>.*/','',$tmpcontent);
  201. $firstline=preg_replace('/[\n\r].*/','',$firstline);
  202. $tmpcontent=$firstline.((strlen($firstline) != strlen($tmpcontent))?'...':'');
  203. }
  204. $ret.=$tmpcontent;
  205. }
  206. else $ret.=$value;
  207. }
  208. }
  209. return $ret;
  210. }
  211. /**
  212. * Output edit in place form
  213. *
  214. * @param object $object Object
  215. * @param string $value Value to show/edit
  216. * @param string $htmlname DIV ID (field name)
  217. * @param int $condition Condition to edit
  218. * @param string $inputType Type of input ('numeric', 'datepicker', 'textarea:rows:cols', 'ckeditor:dolibarr_zzz:width:height:?:1:rows:cols', 'select:xxx')
  219. * @param string $editvalue When in edit mode, use this value as $value instead of value
  220. * @param object $extObject External object
  221. * @param string $success Success message
  222. * @return string HTML edit in place
  223. */
  224. private function editInPlace($object, $value, $htmlname, $condition, $inputType='textarea', $editvalue=null, $extObject=null, $success=null)
  225. {
  226. global $conf;
  227. $out='';
  228. // Check parameters
  229. if ($inputType == 'textarea') $value = dol_nl2br($value);
  230. else if (preg_match('/^numeric/',$inputType)) $value = price($value);
  231. else if ($inputType == 'datepicker') $value = dol_print_date($value, 'day');
  232. if ($condition)
  233. {
  234. $element = false;
  235. $table_element = false;
  236. $fk_element = false;
  237. $loadmethod = false;
  238. $savemethod = false;
  239. $ext_element = false;
  240. $button_only = false;
  241. if (is_object($object))
  242. {
  243. $element = $object->element;
  244. $table_element = $object->table_element;
  245. $fk_element = $object->id;
  246. }
  247. if (is_object($extObject))
  248. {
  249. $ext_element = $extObject->element;
  250. }
  251. if (preg_match('/^(string|email|numeric)/',$inputType))
  252. {
  253. $tmp=explode(':',$inputType);
  254. $inputType=$tmp[0];
  255. if (! empty($tmp[1])) $inputOption=$tmp[1];
  256. if (! empty($tmp[2])) $savemethod=$tmp[2];
  257. }
  258. else if ((preg_match('/^datepicker/',$inputType)) || (preg_match('/^datehourpicker/',$inputType)))
  259. {
  260. $tmp=explode(':',$inputType);
  261. $inputType=$tmp[0];
  262. if (! empty($tmp[1])) $inputOption=$tmp[1];
  263. if (! empty($tmp[2])) $savemethod=$tmp[2];
  264. $out.= '<input id="timestamp" type="hidden"/>'."\n"; // Use for timestamp format
  265. }
  266. else if (preg_match('/^(select|autocomplete)/',$inputType))
  267. {
  268. $tmp=explode(':',$inputType);
  269. $inputType=$tmp[0]; $loadmethod=$tmp[1];
  270. if (! empty($tmp[2])) $savemethod=$tmp[2];
  271. if (! empty($tmp[3])) $button_only=true;
  272. }
  273. else if (preg_match('/^textarea/',$inputType))
  274. {
  275. $tmp=explode(':',$inputType);
  276. $inputType=$tmp[0];
  277. $rows=(empty($tmp[1])?'8':$tmp[1]);
  278. $cols=(empty($tmp[2])?'80':$tmp[2]);
  279. }
  280. else if (preg_match('/^ckeditor/',$inputType))
  281. {
  282. $tmp=explode(':',$inputType);
  283. $inputType=$tmp[0]; $toolbar=$tmp[1];
  284. if (! empty($tmp[2])) $width=$tmp[2];
  285. if (! empty($tmp[3])) $heigth=$tmp[3];
  286. if (! empty($tmp[4])) $savemethod=$tmp[4];
  287. if (! empty($conf->fckeditor->enabled))
  288. {
  289. $out.= '<input id="ckeditor_toolbar" value="'.$toolbar.'" type="hidden"/>'."\n";
  290. }
  291. else
  292. {
  293. $inputType = 'textarea';
  294. }
  295. }
  296. $out.= '<input id="element_'.$htmlname.'" value="'.$element.'" type="hidden"/>'."\n";
  297. $out.= '<input id="table_element_'.$htmlname.'" value="'.$table_element.'" type="hidden"/>'."\n";
  298. $out.= '<input id="fk_element_'.$htmlname.'" value="'.$fk_element.'" type="hidden"/>'."\n";
  299. $out.= '<input id="loadmethod_'.$htmlname.'" value="'.$loadmethod.'" type="hidden"/>'."\n";
  300. if (! empty($savemethod)) $out.= '<input id="savemethod_'.$htmlname.'" value="'.$savemethod.'" type="hidden"/>'."\n";
  301. if (! empty($ext_element)) $out.= '<input id="ext_element_'.$htmlname.'" value="'.$ext_element.'" type="hidden"/>'."\n";
  302. if (! empty($success)) $out.= '<input id="success_'.$htmlname.'" value="'.$success.'" type="hidden"/>'."\n";
  303. if ($inputType == 'textarea') {
  304. $out.= '<input id="textarea_'.$htmlname.'_rows" value="'.$rows.'" type="hidden"/>'."\n";
  305. $out.= '<input id="textarea_'.$htmlname.'_cols" value="'.$cols.'" type="hidden"/>'."\n";
  306. }
  307. $out.= '<span id="viewval_'.$htmlname.'" class="viewval_'.$inputType.($button_only ? ' inactive' : ' active').'">'.$value.'</span>'."\n";
  308. $out.= '<span id="editval_'.$htmlname.'" class="editval_'.$inputType.($button_only ? ' inactive' : ' active').' hideobject">'.(! empty($editvalue) ? $editvalue : $value).'</span>'."\n";
  309. }
  310. else
  311. {
  312. $out = $value;
  313. }
  314. return $out;
  315. }
  316. /**
  317. * Show a text and picto with tooltip on text or picto
  318. *
  319. * @param string $text Text to show
  320. * @param string $htmltext HTML content of tooltip. Must be HTML/UTF8 encoded.
  321. * @param int $tooltipon 1=tooltip sur texte, 2=tooltip sur picto, 3=tooltip sur les 2
  322. * @param int $direction -1=Le picto est avant, 0=pas de picto, 1=le picto est apres
  323. * @param string $img Code img du picto (use img_xxx() function to get it)
  324. * @param string $extracss Add a CSS style to td tags
  325. * @param int $notabs 0=Include table and tr tags, 1=Do not include table and tr tags, 2=use div, 3=use span
  326. * @param string $incbefore Include code before the text
  327. * @param int $noencodehtmltext Do not encode into html entity the htmltext
  328. * @return string Code html du tooltip (texte+picto)
  329. * @see Use function textwithpicto if you can.
  330. */
  331. function textwithtooltip($text, $htmltext, $tooltipon = 1, $direction = 0, $img = '', $extracss = '', $notabs = 0, $incbefore = '', $noencodehtmltext = 0)
  332. {
  333. global $conf;
  334. if ($incbefore) $text = $incbefore.$text;
  335. if (! $htmltext) return $text;
  336. $tag='td';
  337. if ($notabs == 2) $tag='div';
  338. if ($notabs == 3) $tag='span';
  339. // Sanitize tooltip
  340. $htmltext=str_replace("\\","\\\\",$htmltext);
  341. $htmltext=str_replace("\r","",$htmltext);
  342. $htmltext=str_replace("\n","",$htmltext);
  343. $htmltext=str_replace('"',"&quot;",$htmltext);
  344. if ($tooltipon == 2 || $tooltipon == 3) $paramfortooltipimg=' class="classfortooltip'.($extracss?' '.$extracss:'').'" title="'.($noencodehtmltext?$htmltext:dol_escape_htmltag($htmltext,1)).'"'; // Attribut to put on td img tag to store tooltip
  345. else $paramfortooltipimg =($extracss?' class="'.$extracss.'"':''); // Attribut to put on td text tag
  346. if ($tooltipon == 1 || $tooltipon == 3) $paramfortooltiptd=' class="classfortooltip'.($extracss?' '.$extracss:'').'" title="'.($noencodehtmltext?$htmltext:dol_escape_htmltag($htmltext,1)).'"'; // Attribut to put on td tag to store tooltip
  347. else $paramfortooltiptd =($extracss?' class="'.$extracss.'"':''); // Attribut to put on td text tag
  348. $s="";
  349. if (empty($notabs)) $s.='<table class="nobordernopadding" summary=""><tr>';
  350. if ($direction < 0) $s.='<'.$tag.$paramfortooltipimg.' valign="top" width="14">'.$img.'</'.$tag.'>';
  351. // Use another method to help avoid having a space in value in order to use this value with jquery
  352. // TODO add this in css
  353. //if ($text != '') $s.='<'.$tag.$paramfortooltiptd.'>'.(($direction < 0)?'&nbsp;':'').$text.(($direction > 0)?'&nbsp;':'').'</'.$tag.'>';
  354. $paramfortooltiptd.= (($direction < 0)?' style="padding-left: 3px !important;"':'');
  355. $paramfortooltiptd.= (($direction > 0)?' style="padding-right: 3px !important;"':'');
  356. if ((string) $text != '') $s.='<'.$tag.$paramfortooltiptd.'>'.$text.'</'.$tag.'>';
  357. if ($direction > 0) $s.='<'.$tag.$paramfortooltipimg.' valign="top" width="14">'.$img.'</'.$tag.'>';
  358. if (empty($notabs)) $s.='</tr></table>';
  359. return $s;
  360. }
  361. /**
  362. * Show a text with a picto and a tooltip on picto
  363. *
  364. * @param string $text Text to show
  365. * @param string $htmltext Content of tooltip
  366. * @param int $direction 1=Icon is after text, -1=Icon is before text, 0=no icon
  367. * @param string $type Type of picto (info, help, warning, superadmin...)
  368. * @param string $extracss Add a CSS style to td tags
  369. * @param int $noencodehtmltext Do not encode into html entity the htmltext
  370. * @param int $notabs 0=Include table and tr tags, 1=Do not include table and tr tags, 2=use div, 3=use span
  371. * @return string HTML code of text, picto, tooltip
  372. */
  373. function textwithpicto($text, $htmltext, $direction = 1, $type = 'help', $extracss = '', $noencodehtmltext = 0, $notabs = 0)
  374. {
  375. global $conf;
  376. $alt = '';
  377. //For backwards compatibility
  378. if ($type == '0') $type = 'info';
  379. elseif ($type == '1') $type = 'help';
  380. // If info or help with no javascript, show only text
  381. if (empty($conf->use_javascript_ajax))
  382. {
  383. if ($type == 'info' || $type == 'help') return $text;
  384. else
  385. {
  386. $alt = $htmltext;
  387. $htmltext = '';
  388. }
  389. }
  390. // If info or help with smartphone, show only text (tooltip can't works)
  391. if (! empty($conf->dol_no_mouse_hover))
  392. {
  393. if ($type == 'info' || $type == 'help') return $text;
  394. }
  395. if ($type == 'info') $img = img_help(0, $alt);
  396. elseif ($type == 'help') $img = img_help(1, $alt);
  397. elseif ($type == 'superadmin') $img = img_picto($alt, 'redstar');
  398. elseif ($type == 'admin') $img = img_picto($alt, 'star');
  399. elseif ($type == 'warning') $img = img_warning($alt);
  400. return $this->textwithtooltip($text, $htmltext, 2, $direction, $img, $extracss, $notabs, '', $noencodehtmltext);
  401. }
  402. /**
  403. * Return combo list of activated countries, into language of user
  404. *
  405. * @param string $selected Id or Code or Label of preselected country
  406. * @param string $htmlname Name of html select object
  407. * @param string $htmloption Options html on select object
  408. * @return void
  409. */
  410. function select_pays($selected='',$htmlname='country_id',$htmloption='')
  411. {
  412. print $this->select_country($selected,$htmlname,$htmloption);
  413. }
  414. /**
  415. * Return combo list of activated countries, into language of user
  416. *
  417. * @param string $selected Id or Code or Label of preselected country
  418. * @param string $htmlname Name of html select object
  419. * @param string $htmloption Options html on select object
  420. * @param string $maxlength Max length for labels (0=no limit)
  421. * @return string HTML string with select
  422. */
  423. function select_country($selected='',$htmlname='country_id',$htmloption='',$maxlength=0)
  424. {
  425. global $conf,$langs;
  426. $langs->load("dict");
  427. $out='';
  428. $countryArray=array();
  429. $label=array();
  430. $sql = "SELECT rowid, code as code_iso, libelle as label";
  431. $sql.= " FROM ".MAIN_DB_PREFIX."c_pays";
  432. $sql.= " WHERE active = 1";
  433. $sql.= " ORDER BY code ASC";
  434. dol_syslog(get_class($this)."::select_country sql=".$sql);
  435. $resql=$this->db->query($sql);
  436. if ($resql)
  437. {
  438. $out.= '<select id="select'.$htmlname.'" class="flat selectpays" name="'.$htmlname.'" '.$htmloption.'>';
  439. $num = $this->db->num_rows($resql);
  440. $i = 0;
  441. if ($num)
  442. {
  443. $foundselected=false;
  444. while ($i < $num)
  445. {
  446. $obj = $this->db->fetch_object($resql);
  447. $countryArray[$i]['rowid'] = $obj->rowid;
  448. $countryArray[$i]['code_iso'] = $obj->code_iso;
  449. $countryArray[$i]['label'] = ($obj->code_iso && $langs->transnoentitiesnoconv("Country".$obj->code_iso)!="Country".$obj->code_iso?$langs->transnoentitiesnoconv("Country".$obj->code_iso):($obj->label!='-'?$obj->label:''));
  450. $label[$i] = $countryArray[$i]['label'];
  451. $i++;
  452. }
  453. array_multisort($label, SORT_ASC, $countryArray);
  454. foreach ($countryArray as $row)
  455. {
  456. //print 'rr'.$selected.'-'.$row['label'].'-'.$row['code_iso'].'<br>';
  457. if ($selected && $selected != '-1' && ($selected == $row['rowid'] || $selected == $row['code_iso'] || $selected == $row['label']) )
  458. {
  459. $foundselected=true;
  460. $out.= '<option value="'.$row['rowid'].'" selected="selected">';
  461. }
  462. else
  463. {
  464. $out.= '<option value="'.$row['rowid'].'">';
  465. }
  466. $out.= dol_trunc($row['label'],$maxlength,'middle');
  467. if ($row['code_iso']) $out.= ' ('.$row['code_iso'] . ')';
  468. $out.= '</option>';
  469. }
  470. }
  471. $out.= '</select>';
  472. }
  473. else
  474. {
  475. dol_print_error($this->db);
  476. }
  477. return $out;
  478. }
  479. /**
  480. * Return list of types of lines (product or service)
  481. * Example: 0=product, 1=service, 9=other (for external module)
  482. *
  483. * @param string $selected Preselected type
  484. * @param string $htmlname Name of field in html form
  485. * @param int $showempty Add an empty field
  486. * @param int $hidetext Do not show label before combo box
  487. * @param string $forceall Force to show products and services in combo list, whatever are activated modules
  488. * @return void
  489. */
  490. function select_type_of_lines($selected='',$htmlname='type',$showempty=0,$hidetext=0,$forceall=0)
  491. {
  492. global $db,$langs,$user,$conf;
  493. // If product & services are enabled or both disabled.
  494. if ($forceall || (! empty($conf->product->enabled) && ! empty($conf->service->enabled))
  495. || (empty($conf->product->enabled) && empty($conf->service->enabled)))
  496. {
  497. if (empty($hidetext)) print $langs->trans("Type").': ';
  498. print '<select class="flat" id="select_'.$htmlname.'" name="'.$htmlname.'">';
  499. if ($showempty)
  500. {
  501. print '<option value="-1"';
  502. if ($selected == -1) print ' selected="selected"';
  503. print '>&nbsp;</option>';
  504. }
  505. print '<option value="0"';
  506. if (0 == $selected) print ' selected="selected"';
  507. print '>'.$langs->trans("Product");
  508. print '<option value="1"';
  509. if (1 == $selected) print ' selected="selected"';
  510. print '>'.$langs->trans("Service");
  511. print '</select>';
  512. //if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  513. }
  514. if (! $forceall && empty($conf->product->enabled) && ! empty($conf->service->enabled))
  515. {
  516. print '<input type="hidden" name="'.$htmlname.'" value="1">';
  517. }
  518. if (! $forceall && ! empty($conf->product->enabled) && empty($conf->service->enabled))
  519. {
  520. print '<input type="hidden" name="'.$htmlname.'" value="0">';
  521. }
  522. }
  523. /**
  524. * Load into cache cache_types_fees, array of types of fees
  525. *
  526. * @return int Nb of lines loaded, 0 if already loaded, <0 if ko
  527. * TODO move in DAO class
  528. */
  529. function load_cache_types_fees()
  530. {
  531. global $langs;
  532. $langs->load("trips");
  533. if (count($this->cache_types_fees)) return 0; // Cache already load
  534. $sql = "SELECT c.code, c.libelle as label";
  535. $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees as c";
  536. $sql.= " ORDER BY lower(c.libelle) ASC";
  537. dol_syslog(get_class($this).'::load_cache_types_fees sql='.$sql, LOG_DEBUG);
  538. $resql=$this->db->query($sql);
  539. if ($resql)
  540. {
  541. $num = $this->db->num_rows($resql);
  542. $i = 0;
  543. while ($i < $num)
  544. {
  545. $obj = $this->db->fetch_object($resql);
  546. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  547. $label=($obj->code != $langs->trans($obj->code) ? $langs->trans($obj->code) : $langs->trans($obj->label));
  548. $this->cache_types_fees[$obj->code] = $label;
  549. $i++;
  550. }
  551. return $num;
  552. }
  553. else
  554. {
  555. dol_print_error($this->db);
  556. return -1;
  557. }
  558. }
  559. /**
  560. * Return list of types of notes
  561. *
  562. * @param string $selected Preselected type
  563. * @param string $htmlname Name of field in form
  564. * @param int $showempty Add an empty field
  565. * @return void
  566. */
  567. function select_type_fees($selected='',$htmlname='type',$showempty=0)
  568. {
  569. global $user, $langs;
  570. dol_syslog(get_class($this)."::select_type_fees ".$selected.", ".$htmlname, LOG_DEBUG);
  571. $this->load_cache_types_fees();
  572. print '<select class="flat" name="'.$htmlname.'">';
  573. if ($showempty)
  574. {
  575. print '<option value="-1"';
  576. if ($selected == -1) print ' selected="selected"';
  577. print '>&nbsp;</option>';
  578. }
  579. foreach($this->cache_types_fees as $key => $value)
  580. {
  581. print '<option value="'.$key.'"';
  582. if ($key == $selected) print ' selected="selected"';
  583. print '>';
  584. print $value;
  585. print '</option>';
  586. }
  587. print '</select>';
  588. if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  589. }
  590. /**
  591. * Return list of company for customer in Ajax if Ajax activated or go to select_thirparty_list
  592. *
  593. * @param int $selected Preselected products
  594. * @param string $htmlname Name of HTML seletc field (must be unique in page)
  595. * @param int $filter Filter on thirdparty
  596. * @param int $limit Limit on number of returned lines
  597. * @param array $ajaxoptions Options for ajax_autocompleter
  598. * @param int $forcecombo Force to use combo box
  599. * @return void
  600. */
  601. function select_thirdparty($selected='', $htmlname='productid', $filter='', $limit=20, $ajaxoptions=array(), $forcecombo=0)
  602. {
  603. global $langs,$conf;
  604. /* TODO Use ajax autocompletion (not finished)
  605. if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT) && ! $forcecombo)
  606. {
  607. $placeholder='';
  608. if ($selected && empty($selected_input_value))
  609. {
  610. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  611. $societe = new Societe($this->db);
  612. $societe->fetch($selected);
  613. $selected_input_value=$societe->ref;
  614. }
  615. // mode=1 means customers products
  616. $urloption='htmlname='.$htmlname.'&outjson=1&price_level='.$price_level.'&type='.$filtertype.'&mode=1&status='.$status.'&finished='.$finished;
  617. print ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/societe/ajax/company.php', $urloption, $conf->global->COMPANY_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
  618. if (empty($hidelabel)) print $langs->trans("RefOrLabel").' : ';
  619. else if ($hidelabel > 1) {
  620. if (! empty($conf->global->MAIN_HTML5_PLACEHOLDER)) $placeholder=' placeholder="'.$langs->trans("RefOrLabel").'"';
  621. else $placeholder=' title="'.$langs->trans("RefOrLabel").'"';
  622. if ($hidelabel == 2) {
  623. print img_picto($langs->trans("Search"), 'search');
  624. }
  625. }
  626. print '<input type="text" size="20" name="search_'.$htmlname.'" id="search_'.$htmlname.'" value="'.$selected_input_value.'"'.$placeholder.' />';
  627. if ($hidelabel == 3) {
  628. print img_picto($langs->trans("Search"), 'search');
  629. }
  630. }
  631. else
  632. {*/
  633. print $this->select_thirdparty_list($selected,$htmlname,$filter,1,0,$forcecombo,array(),'',0,$limit);
  634. //}
  635. }
  636. /**
  637. * Output html form to select a third party
  638. *
  639. * @param string $selected Preselected type
  640. * @param string $htmlname Name of field in form
  641. * @param string $filter Optionnal filters criteras (example: 's.rowid <> x')
  642. * @param int $showempty Add an empty field
  643. * @param int $showtype Show third party type in combolist (customer, prospect or supplier)
  644. * @param int $forcecombo Force to use combo box
  645. * @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
  646. * @return string HTML string with
  647. * @deprecated Use select_thirdparty instead
  648. */
  649. function select_company($selected='', $htmlname='socid', $filter='', $showempty=0, $showtype=0, $forcecombo=0, $event=array())
  650. {
  651. return $this->select_thirdparty_list($selected,$htmlname, $filter, $showempty, $showtype, $forcecombo, $event);
  652. }
  653. /**
  654. * Output html form to select a third party
  655. *
  656. * @param string $selected Preselected type
  657. * @param string $htmlname Name of field in form
  658. * @param string $filter Optionnal filters criteras (example: 's.rowid <> x')
  659. * @param int $showempty Add an empty field
  660. * @param int $showtype Show third party type in combolist (customer, prospect or supplier)
  661. * @param int $forcecombo Force to use combo box
  662. * @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
  663. * @param string $filterkey Filter on key value
  664. * @param int $outputmode 0=HTML select string, 1=Array
  665. * @param int $limit Limit number of answers
  666. * @return string HTML string with
  667. */
  668. function select_thirdparty_list($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array(), $filterkey='', $outputmode=0, $limit=20)
  669. {
  670. global $conf,$user,$langs;
  671. $out='';
  672. $outarray=array();
  673. // On recherche les societes
  674. $sql = "SELECT s.rowid, s.nom, s.client, s.fournisseur, s.code_client, s.code_fournisseur";
  675. $sql.= " FROM ".MAIN_DB_PREFIX ."societe as s";
  676. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  677. $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
  678. if (! empty($user->societe_id)) $sql.= " AND s.rowid = ".$user->societe_id;
  679. if ($filter) $sql.= " AND (".$filter.")";
  680. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  681. if (! empty($conf->global->COMPANY_HIDE_INACTIVE_IN_COMBOBOX)) $sql.= " AND s.status<>0 ";
  682. // Add criteria
  683. if ($filterkey && $filterkey != '')
  684. {
  685. $sql.=" AND (";
  686. if (! empty($conf->global->COMPANY_DONOTSEARCH_ANYWHERE)) // Can use index
  687. {
  688. $sql.="(s.name LIKE '".$filterkey."%'";
  689. $sql.=")";
  690. }
  691. else
  692. {
  693. // For natural search
  694. $scrit = explode(' ', $filterkey);
  695. foreach ($scrit as $crit) {
  696. $sql.=" AND (s.name LIKE '%".$crit."%'";
  697. $sql.=")";
  698. }
  699. }
  700. if (! empty($conf->barcode->enabled))
  701. {
  702. $sql .= " OR s.barcode LIKE '".$filterkey."'";
  703. }
  704. $sql.=")";
  705. }
  706. $sql.= " ORDER BY nom ASC";
  707. dol_syslog(get_class($this)."::select_thirdparty_list sql=".$sql);
  708. $resql=$this->db->query($sql);
  709. if ($resql)
  710. {
  711. if ($conf->use_javascript_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT && ! $forcecombo)
  712. {
  713. //$minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2);
  714. $out.= ajax_combobox($htmlname, $event, $conf->global->COMPANY_USE_SEARCH_TO_SELECT);
  715. }
  716. // Construct $out and $outarray
  717. $out.= '<select id="'.$htmlname.'" class="flat" name="'.$htmlname.'">'."\n";
  718. if ($showempty) $out.= '<option value="-1"></option>'."\n";
  719. $num = $this->db->num_rows($resql);
  720. $i = 0;
  721. if ($num)
  722. {
  723. while ($i < $num)
  724. {
  725. $obj = $this->db->fetch_object($resql);
  726. $label=$obj->nom;
  727. if ($showtype)
  728. {
  729. if ($obj->client || $obj->fournisseur) $label.=' (';
  730. if ($obj->client == 1 || $obj->client == 3) $label.=$langs->trans("Customer");
  731. if ($obj->client == 2 || $obj->client == 3) $label.=($obj->client==3?', ':'').$langs->trans("Prospect");
  732. if ($obj->fournisseur) $label.=($obj->client?', ':'').$langs->trans("Supplier");
  733. if ($obj->client || $obj->fournisseur) $label.=')';
  734. }
  735. if ($selected > 0 && $selected == $obj->rowid)
  736. {
  737. $out.= '<option value="'.$obj->rowid.'" selected="selected">'.$label.'</option>';
  738. }
  739. else
  740. {
  741. $out.= '<option value="'.$obj->rowid.'">'.$label.'</option>';
  742. }
  743. array_push($outarray, array('key'=>$obj->rowid, 'value'=>$obj->name, 'label'=>$obj->name));
  744. $i++;
  745. if (($i % 10) == 0) $out.="\n";
  746. }
  747. }
  748. $out.= '</select>'."\n";
  749. }
  750. else
  751. {
  752. dol_print_error($this->db);
  753. }
  754. if ($outputmode) return $outarray;
  755. return $out;
  756. }
  757. /**
  758. * Return HTML combo list of absolute discounts
  759. *
  760. * @param string $selected Id remise fixe pre-selectionnee
  761. * @param string $htmlname Nom champ formulaire
  762. * @param string $filter Criteres optionnels de filtre
  763. * @param int $socid Id of thirdparty
  764. * @param int $maxvalue Max value for lines that can be selected
  765. * @return int Return number of qualifed lines in list
  766. */
  767. function select_remises($selected, $htmlname, $filter, $socid, $maxvalue=0)
  768. {
  769. global $langs,$conf;
  770. // On recherche les remises
  771. $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
  772. $sql.= " re.description, re.fk_facture_source";
  773. $sql.= " FROM ".MAIN_DB_PREFIX ."societe_remise_except as re";
  774. $sql.= " WHERE fk_soc = ".$socid;
  775. if ($filter) $sql.= " AND ".$filter;
  776. $sql.= " ORDER BY re.description ASC";
  777. dol_syslog(get_class($this)."::select_remises sql=".$sql);
  778. $resql=$this->db->query($sql);
  779. if ($resql)
  780. {
  781. print '<select class="flat" name="'.$htmlname.'">';
  782. $num = $this->db->num_rows($resql);
  783. $qualifiedlines=$num;
  784. $i = 0;
  785. if ($num)
  786. {
  787. print '<option value="0">&nbsp;</option>';
  788. while ($i < $num)
  789. {
  790. $obj = $this->db->fetch_object($resql);
  791. $desc=dol_trunc($obj->description,40);
  792. if ($desc=='(CREDIT_NOTE)') $desc=$langs->trans("CreditNote");
  793. if ($desc=='(DEPOSIT)') $desc=$langs->trans("Deposit");
  794. $selectstring='';
  795. if ($selected > 0 && $selected == $obj->rowid) $selectstring=' selected="selected"';
  796. $disabled='';
  797. if ($maxvalue > 0 && $obj->amount_ttc > $maxvalue)
  798. {
  799. $qualifiedlines--;
  800. $disabled=' disabled="disabled"';
  801. }
  802. print '<option value="'.$obj->rowid.'"'.$selectstring.$disabled.'>'.$desc.' ('.price($obj->amount_ht).' '.$langs->trans("HT").' - '.price($obj->amount_ttc).' '.$langs->trans("TTC").')</option>';
  803. $i++;
  804. }
  805. }
  806. print '</select>';
  807. return $qualifiedlines;
  808. }
  809. else
  810. {
  811. dol_print_error($this->db);
  812. return -1;
  813. }
  814. }
  815. /**
  816. * Return list of all contacts (for a third party or all)
  817. *
  818. * @param int $socid Id ot third party or 0 for all
  819. * @param string $selected Id contact pre-selectionne
  820. * @param string $htmlname Name of HTML field ('none' for a not editable field)
  821. * @param int $showempty 0=no empty value, 1=add an empty value
  822. * @param string $exclude List of contacts id to exclude
  823. * @param string $limitto Disable answers that are not id in this array list
  824. * @param string $showfunction Add function into label
  825. * @param string $moreclass Add more class to class style
  826. * @param string $showsoc Add company into label
  827. * @param int $forcecombo Force to use combo box
  828. * @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
  829. * @param bool $options_only Return options only (for ajax treatment)
  830. * @return int <0 if KO, Nb of contact in list if OK
  831. */
  832. function select_contacts($socid,$selected='',$htmlname='contactid',$showempty=0,$exclude='',$limitto='',$showfunction=0, $moreclass='', $showsoc=0, $forcecombo=0, $event=array(), $options_only=false)
  833. {
  834. print $this->selectcontacts($socid,$selected,$htmlname,$showempty,$exclude,$limitto,$showfunction, $moreclass, $options_only, $showsoc, $forcecombo, $event);
  835. return $this->num;
  836. }
  837. /**
  838. * Return list of all contacts (for a third party or all)
  839. *
  840. * @param int $socid Id ot third party or 0 for all
  841. * @param string $selected Id contact pre-selectionne
  842. * @param string $htmlname Name of HTML field ('none' for a not editable field)
  843. * @param int $showempty 0=no empty value, 1=add an empty value, 2=add line 'Internal' (used by user edit)
  844. * @param string $exclude List of contacts id to exclude
  845. * @param string $limitto Disable answers that are not id in this array list
  846. * @param string $showfunction Add function into label
  847. * @param string $moreclass Add more class to class style
  848. * @param bool $options_only Return options only (for ajax treatment)
  849. * @param string $showsoc Add company into label
  850. * @param int $forcecombo Force to use combo box
  851. * @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
  852. * @return int <0 if KO, Nb of contact in list if OK
  853. */
  854. function selectcontacts($socid,$selected='',$htmlname='contactid',$showempty=0,$exclude='',$limitto='',$showfunction=0, $moreclass='', $options_only=false, $showsoc=0, $forcecombo=0, $event=array())
  855. {
  856. global $conf,$langs;
  857. $langs->load('companies');
  858. $out='';
  859. // On recherche les societes
  860. $sql = "SELECT sp.rowid, sp.lastname, sp.firstname, sp.poste";
  861. if ($showsoc > 0) {
  862. $sql.= " , s.nom as company";
  863. }
  864. $sql.= " FROM ".MAIN_DB_PREFIX ."socpeople as sp";
  865. if ($showsoc > 0) {
  866. $sql.= " LEFT OUTER JOIN ".MAIN_DB_PREFIX ."societe as s ON s.rowid=sp.fk_soc ";
  867. }
  868. $sql.= " WHERE sp.entity IN (".getEntity('societe', 1).")";
  869. if ($socid > 0) $sql.= " AND sp.fk_soc=".$socid;
  870. $sql.= " ORDER BY sp.lastname ASC";
  871. dol_syslog(get_class($this)."::select_contacts sql=".$sql);
  872. $resql=$this->db->query($sql);
  873. if ($resql)
  874. {
  875. $num=$this->db->num_rows($resql);
  876. if ($conf->use_javascript_ajax && $conf->global->CONTACT_USE_SEARCH_TO_SELECT && ! $forcecombo && ! $options_only)
  877. {
  878. $out.= ajax_combobox($htmlname, $event);
  879. }
  880. if ($htmlname != 'none' || $options_only) $out.= '<select class="flat'.($moreclass?' '.$moreclass:'').'" id="'.$htmlname.'" name="'.$htmlname.'">';
  881. if ($showempty == 1) $out.= '<option value="0"'.($selected=='0'?' selected="selected"':'').'></option>';
  882. if ($showempty == 2) $out.= '<option value="0"'.($selected=='0'?' selected="selected"':'').'>'.$langs->trans("Internal").'</option>';
  883. $num = $this->db->num_rows($resql);
  884. $i = 0;
  885. if ($num)
  886. {
  887. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  888. $contactstatic=new Contact($this->db);
  889. while ($i < $num)
  890. {
  891. $obj = $this->db->fetch_object($resql);
  892. $contactstatic->id=$obj->rowid;
  893. $contactstatic->lastname=$obj->lastname;
  894. $contactstatic->firstname=$obj->firstname;
  895. if ($htmlname != 'none')
  896. {
  897. $disabled=0;
  898. if (is_array($exclude) && count($exclude) && in_array($obj->rowid,$exclude)) $disabled=1;
  899. if (is_array($limitto) && count($limitto) && ! in_array($obj->rowid,$limitto)) $disabled=1;
  900. if ($selected && $selected == $obj->rowid)
  901. {
  902. $out.= '<option value="'.$obj->rowid.'"';
  903. if ($disabled) $out.= ' disabled="disabled"';
  904. $out.= ' selected="selected">';
  905. $out.= $contactstatic->getFullName($langs);
  906. if ($showfunction && $obj->poste) $out.= ' ('.$obj->poste.')';
  907. if (($showsoc > 0) && $obj->company) $out.= ' - ('.$obj->company.')';
  908. $out.= '</option>';
  909. }
  910. else
  911. {
  912. $out.= '<option value="'.$obj->rowid.'"';
  913. if ($disabled) $out.= ' disabled="disabled"';
  914. $out.= '>';
  915. $out.= $contactstatic->getFullName($langs);
  916. if ($showfunction && $obj->poste) $out.= ' ('.$obj->poste.')';
  917. if (($showsoc > 0) && $obj->company) $out.= ' - ('.$obj->company.')';
  918. $out.= '</option>';
  919. }
  920. }
  921. else
  922. {
  923. if ($selected == $obj->rowid)
  924. {
  925. $out.= $contactstatic->getFullName($langs);
  926. if ($showfunction && $obj->poste) $out.= ' ('.$obj->poste.')';
  927. if (($showsoc > 0) && $obj->company) $out.= ' - ('.$obj->company.')';
  928. }
  929. }
  930. $i++;
  931. }
  932. }
  933. else
  934. {
  935. $out.= '<option value="-1"'.($showempty==2?'':' selected="selected"').' disabled="disabled">'.$langs->trans($socid?"NoContactDefinedForThirdParty":"NoContactDefined").'</option>';
  936. }
  937. if ($htmlname != 'none' || $options_only)
  938. {
  939. $out.= '</select>';
  940. }
  941. $this->num = $num;
  942. return $out;
  943. }
  944. else
  945. {
  946. dol_print_error($this->db);
  947. return -1;
  948. }
  949. }
  950. /**
  951. * Return select list of users
  952. *
  953. * @param string $selected Id user preselected
  954. * @param string $htmlname Field name in form
  955. * @param int $show_empty 0=liste sans valeur nulle, 1=ajoute valeur inconnue
  956. * @param array $exclude Array list of users id to exclude
  957. * @param int $disabled If select list must be disabled
  958. * @param array $include Array list of users id to include
  959. * @param int $enableonly Array list of users id to be enabled. All other must be disabled
  960. * @param int $force_entity 0 or Id of environment to force
  961. * @return void
  962. */
  963. function select_users($selected='',$htmlname='userid',$show_empty=0,$exclude='',$disabled=0,$include='',$enableonly='',$force_entity=0)
  964. {
  965. print $this->select_dolusers($selected,$htmlname,$show_empty,$exclude,$disabled,$include,$enableonly,$force_entity);
  966. }
  967. /**
  968. * Return select list of users
  969. *
  970. * @param string $selected User id or user object of user preselected. If -1, we use id of current user.
  971. * @param string $htmlname Field name in form
  972. * @param int $show_empty 0=liste sans valeur nulle, 1=ajoute valeur inconnue
  973. * @param array $exclude Array list of users id to exclude
  974. * @param int $disabled If select list must be disabled
  975. * @param array $include Array list of users id to include
  976. * @param int $enableonly Array list of users id to be enabled. All other must be disabled
  977. * @param int $force_entity 0 or Id of environment to force
  978. * @return string HTML select string
  979. */
  980. function select_dolusers($selected='',$htmlname='userid',$show_empty=0,$exclude='',$disabled=0,$include='',$enableonly='',$force_entity=0)
  981. {
  982. global $conf,$user,$langs;
  983. // If no preselected user defined, we take current user
  984. if ((is_numeric($selected) && ($selected < -1 || empty($selected))) && empty($conf->global->SOCIETE_DISABLE_DEFAULT_SALESREPRESENTATIVE)) $selected=$user->id;
  985. // Permettre l'exclusion d'utilisateurs
  986. if (is_array($exclude)) $excludeUsers = implode("','",$exclude);
  987. // Permettre l'inclusion d'utilisateurs
  988. if (is_array($include)) $includeUsers = implode("','",$include);
  989. $out='';
  990. // On recherche les utilisateurs
  991. $sql = "SELECT DISTINCT u.rowid, u.lastname as lastname, u.firstname, u.login, u.admin, u.entity";
  992. if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
  993. {
  994. $sql.= ", e.label";
  995. }
  996. $sql.= " FROM ".MAIN_DB_PREFIX ."user as u";
  997. if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
  998. {
  999. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX ."entity as e ON e.rowid=u.entity";
  1000. if ($force_entity) $sql.= " WHERE u.entity IN (0,".$force_entity.")";
  1001. else $sql.= " WHERE u.entity IS NOT NULL";
  1002. }
  1003. else
  1004. {
  1005. if (! empty($conf->multicompany->transverse_mode))
  1006. {
  1007. $sql.= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
  1008. $sql.= " WHERE ug.fk_user = u.rowid";
  1009. $sql.= " AND ug.entity = ".$conf->entity;
  1010. }
  1011. else
  1012. {
  1013. $sql.= " WHERE u.entity IN (0,".$conf->entity.")";
  1014. }
  1015. }
  1016. if (! empty($user->societe_id)) $sql.= " AND u.fk_societe = ".$user->societe_id;
  1017. if (is_array($exclude) && $excludeUsers) $sql.= " AND u.rowid NOT IN ('".$excludeUsers."')";
  1018. if (is_array($include) && $includeUsers) $sql.= " AND u.rowid IN ('".$includeUsers."')";
  1019. $sql.= " ORDER BY u.lastname ASC";
  1020. dol_syslog(get_class($this)."::select_dolusers sql=".$sql);
  1021. $resql=$this->db->query($sql);
  1022. if ($resql)
  1023. {
  1024. $num = $this->db->num_rows($resql);
  1025. $i = 0;
  1026. if ($num)
  1027. {
  1028. $out.= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($disabled?' disabled="disabled"':'').'>';
  1029. if ($show_empty) $out.= '<option value="-1"'.((empty($selected) || $selected==-1)?' selected="selected"':'').'>&nbsp;</option>'."\n";
  1030. $userstatic=new User($this->db);
  1031. while ($i < $num)
  1032. {
  1033. $obj = $this->db->fetch_object($resql);
  1034. $userstatic->id=$obj->rowid;
  1035. $userstatic->lastname=$obj->lastname;
  1036. $userstatic->firstname=$obj->firstname;
  1037. $disableline=0;
  1038. if (is_array($enableonly) && count($enableonly) && ! in_array($obj->rowid,$enableonly)) $disableline=1;
  1039. if ((is_object($selected) && $selected->id == $obj->rowid) || (! is_object($selected) && $selected == $obj->rowid))
  1040. {
  1041. $out.= '<option value="'.$obj->rowid.'"';
  1042. if ($disableline) $out.= ' disabled="disabled"';
  1043. $out.= ' selected="selected">';
  1044. }
  1045. else
  1046. {
  1047. $out.= '<option value="'.$obj->rowid.'"';
  1048. if ($disableline) $out.= ' disabled="disabled"';
  1049. $out.= '>';
  1050. }
  1051. $out.= $userstatic->getFullName($langs);
  1052. if (! empty($conf->multicompany->enabled) && empty($conf->multicompany->transverse_mode) && $conf->entity == 1 && $user->admin && ! $user->entity)
  1053. {
  1054. if ($obj->admin && ! $obj->entity) $out.=" (".$langs->trans("AllEntities").")";
  1055. else $out.=" (".$obj->label.")";
  1056. }
  1057. //if ($obj->admin) $out.= ' *';
  1058. if (! empty($conf->global->MAIN_SHOW_LOGIN)) $out.= ' ('.$obj->login.')';
  1059. $out.= '</option>';
  1060. $i++;
  1061. }
  1062. }
  1063. else
  1064. {
  1065. $out.= '<select class="flat" name="'.$htmlname.'" disabled="disabled">';
  1066. $out.= '<option value="">'.$langs->trans("None").'</option>';
  1067. }
  1068. $out.= '</select>';
  1069. }
  1070. else
  1071. {
  1072. dol_print_error($this->db);
  1073. }
  1074. return $out;
  1075. }
  1076. /**
  1077. * Return list of products for customer in Ajax if Ajax activated or go to select_produits_list
  1078. *
  1079. * @param int $selected Preselected products
  1080. * @param string $htmlname Name of HTML seletc field (must be unique in page)
  1081. * @param int $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  1082. * @param int $limit Limit on number of returned lines
  1083. * @param int $price_level Level of price to show
  1084. * @param int $status -1=Return all products, 0=Products not on sell, 1=Products on sell
  1085. * @param int $finished 2=all, 1=finished, 0=raw material
  1086. * @param string $selected_input_value Value of preselected input text (with ajax)
  1087. * @param int $hidelabel Hide label (0=no, 1=yes, 2=show search icon (before) and placeholder, 3 search icon after)
  1088. * @param array $ajaxoptions Options for ajax_autocompleter
  1089. * @return void
  1090. */
  1091. function select_produits($selected='', $htmlname='productid', $filtertype='', $limit=20, $price_level=0, $status=1, $finished=2, $selected_input_value='', $hidelabel=0, $ajaxoptions=array())
  1092. {
  1093. global $langs,$conf;
  1094. $price_level = (! empty($price_level) ? $price_level : 0);
  1095. if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->PRODUIT_USE_SEARCH_TO_SELECT))
  1096. {
  1097. $placeholder='';
  1098. if ($selected && empty($selected_input_value))
  1099. {
  1100. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  1101. $product = new Product($this->db);
  1102. $product->fetch($selected);
  1103. $selected_input_value=$product->ref;
  1104. }
  1105. // mode=1 means customers products
  1106. $urloption='htmlname='.$htmlname.'&outjson=1&price_level='.$price_level.'&type='.$filtertype.'&mode=1&status='.$status.'&finished='.$finished;
  1107. print ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/product/ajax/products.php', $urloption, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
  1108. if (empty($hidelabel)) print $langs->trans("RefOrLabel").' : ';
  1109. else if ($hidelabel > 1) {
  1110. if (! empty($conf->global->MAIN_HTML5_PLACEHOLDER)) $placeholder=' placeholder="'.$langs->trans("RefOrLabel").'"';
  1111. else $placeholder=' title="'.$langs->trans("RefOrLabel").'"';
  1112. if ($hidelabel == 2) {
  1113. print img_picto($langs->trans("Search"), 'search');
  1114. }
  1115. }
  1116. print '<input type="text" size="20" name="search_'.$htmlname.'" id="search_'.$htmlname.'" value="'.$selected_input_value.'"'.$placeholder.' />';
  1117. if ($hidelabel == 3) {
  1118. print img_picto($langs->trans("Search"), 'search');
  1119. }
  1120. }
  1121. else
  1122. {
  1123. print $this->select_produits_list($selected,$htmlname,$filtertype,$limit,$price_level,'',$status,$finished,0);
  1124. }
  1125. }
  1126. /**
  1127. * Return list of products for a customer
  1128. *
  1129. * @param int $selected Preselected product
  1130. * @param string $htmlname Name of select html
  1131. * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  1132. * @param int $limit Limite sur le nombre de lignes retournees
  1133. * @param int $price_level Level of price to show
  1134. * @param string $filterkey Filter on product
  1135. * @param int $status -1=Return all products, 0=Products not on sell, 1=Products on sell
  1136. * @param int $finished Filter on finished field: 2=No filter
  1137. * @param int $outputmode 0=HTML select string, 1=Array
  1138. * @return array Array of keys for json
  1139. */
  1140. function select_produits_list($selected='',$htmlname='productid',$filtertype='',$limit=20,$price_level=0,$filterkey='',$status=1,$finished=2,$outputmode=0)
  1141. {
  1142. global $langs,$conf,$user,$db;
  1143. $out='';
  1144. $outarray=array();
  1145. $sql = "SELECT ";
  1146. $sql.= " p.rowid, p.label, p.ref, p.description, p.fk_product_type, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.duration, p.stock";
  1147. // Multilang : we add translation
  1148. if (! empty($conf->global->MAIN_MULTILANGS))
  1149. {
  1150. $sql.= ", pl.label as label_translated";
  1151. }
  1152. // Price by quantity
  1153. if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY))
  1154. {
  1155. $sql.= ", (SELECT pp.rowid FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = p.rowid";
  1156. if ($price_level >= 1) $sql.= " AND price_level=".$price_level;
  1157. $sql.= " ORDER BY date_price";
  1158. $sql.= " DESC LIMIT 1) as price_rowid";
  1159. $sql.= ", (SELECT pp.price_by_qty FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = p.rowid";
  1160. if ($price_level >= 1) $sql.= " AND price_level=".$price_level;
  1161. $sql.= " ORDER BY date_price";
  1162. $sql.= " DESC LIMIT 1) as price_by_qty";
  1163. }
  1164. $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
  1165. // Multilang : we add translation
  1166. if (! empty($conf->global->MAIN_MULTILANGS))
  1167. {
  1168. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_lang as pl ON pl.fk_product = p.rowid AND pl.lang='". $langs->getDefaultLang() ."'";
  1169. }
  1170. $sql.= ' WHERE p.entity IN ('.getEntity('product', 1).')';
  1171. if ($finished == 0)
  1172. {
  1173. $sql.= " AND p.finished = ".$finished;
  1174. }
  1175. elseif ($finished == 1)
  1176. {
  1177. $sql.= " AND p.finished = ".$finished;
  1178. if ($status >= 0) $sql.= " AND p.tosell = ".$status;
  1179. }
  1180. elseif ($status >= 0)
  1181. {
  1182. $sql.= " AND p.tosell = ".$status;
  1183. }
  1184. if (strval($filtertype) != '') $sql.=" AND p.fk_product_type=".$filtertype;
  1185. // Add criteria on ref/label
  1186. if ($filterkey != '')
  1187. {
  1188. $sql.=' AND (';
  1189. $prefix=empty($conf->global->PRODUCT_DONOTSEARCH_ANYWHERE)?'%':''; // Can use index if PRODUCT_DONOTSEARCH_ANYWHERE is on
  1190. // For natural search
  1191. $scrit = explode(' ', $filterkey);
  1192. $i=0;
  1193. if (count($scrit) > 1) $sql.="(";
  1194. foreach ($scrit as $crit)
  1195. {
  1196. if ($i > 0) $sql.=" AND ";
  1197. $sql.="(p.ref LIKE '".$prefix.$crit."%' OR p.label LIKE '".$prefix.$crit."%'";
  1198. if (! empty($conf->global->MAIN_MULTILANGS)) $sql.=" OR pl.label LIKE '".$prefix.$crit."%'";
  1199. $sql.=")";
  1200. $i++;
  1201. }
  1202. if (count($scrit) > 1) $sql.=")";
  1203. if (! empty($conf->barcode->enabled)) $sql.= " OR p.barcode LIKE '".$prefix.$filterkey."%'";
  1204. $sql.=')';
  1205. }
  1206. $sql.= $db->order("p.ref");
  1207. $sql.= $db->plimit($limit);
  1208. // Build output string
  1209. dol_syslog(get_class($this)."::select_produits_list search product sql=".$sql, LOG_DEBUG);
  1210. $result=$this->db->query($sql);
  1211. if ($result)
  1212. {
  1213. $num = $this->db->num_rows($result);
  1214. $out.='<select class="flat" name="'.$htmlname.'" id="'.$htmlname.'">';
  1215. $out.='<option value="0" selected="selected">&nbsp;</option>';
  1216. $i = 0;
  1217. while ($num && $i < $num)
  1218. {
  1219. $opt = '';
  1220. $optJson = array();
  1221. $objp = $this->db->fetch_object($result);
  1222. if (!empty($objp->price_by_qty) && $objp->price_by_qty == 1)
  1223. { // Price by quantity will return many prices for the same product
  1224. $sql = "SELECT rowid, quantity, price, unitprice, remise_percent, remise";
  1225. $sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
  1226. $sql.= " WHERE fk_product_price=".$objp->price_rowid;
  1227. $sql.= " ORDER BY quantity ASC";
  1228. dol_syslog(get_class($this)."::select_produits_list search price by qty sql=".$sql);
  1229. $result2 = $this->db->query($sql);
  1230. if ($result2)
  1231. {
  1232. $nb_prices = $this->db->num_rows($result2);
  1233. $j = 0;
  1234. while ($nb_prices && $j < $nb_prices) {
  1235. $objp2 = $this->db->fetch_object($result2);
  1236. $objp->quantity = $objp2->quantity;
  1237. $objp->price = $objp2->price;
  1238. $objp->unitprice = $objp2->unitprice;
  1239. $objp->remise_percent = $objp2->remise_percent;
  1240. $objp->remise = $objp2->remise;
  1241. $objp->price_by_qty_rowid = $objp2->rowid;
  1242. $this->_construct_product_list_option($objp, $opt, $optJson, 0, $selected);
  1243. $j++;
  1244. // Add new entry
  1245. // "key" value of json key array is used by jQuery automatically as selected value
  1246. // "label" value of json key array is used by jQuery automatically as text for combo box
  1247. $out.=$opt;
  1248. array_push($outarray, $optJson);
  1249. }
  1250. }
  1251. }
  1252. else
  1253. {
  1254. $this->_construct_product_list_option($objp, $opt, $optJson, $price_level, $selected);
  1255. // Add new entry
  1256. // "key" value of json key array is used by jQuery automatically as selected value
  1257. // "label" value of json key array is used by jQuery automatically as text for combo box
  1258. $out.=$opt;
  1259. array_push($outarray, $optJson);
  1260. }
  1261. $i++;
  1262. }
  1263. $out.='</select>';
  1264. $this->db->free($result);
  1265. if (empty($outputmode)) return $out;
  1266. return $outarray;
  1267. }
  1268. else
  1269. {
  1270. dol_print_error($db);
  1271. }
  1272. }
  1273. /**
  1274. * _construct_product_list_option
  1275. *
  1276. * @param resultset &$objp Resultset of fetch
  1277. * @param string $opt Option
  1278. * @param string $optJson Option
  1279. * @param int $price_level Price level
  1280. * @param string $selected Preselected value
  1281. * @return
  1282. */
  1283. private function _construct_product_list_option(&$objp, &$opt, &$optJson, $price_level, $selected)
  1284. {
  1285. global $langs,$conf,$user,$db;
  1286. $outkey='';
  1287. $outval='';
  1288. $outref='';
  1289. $outlabel='';
  1290. $outdesc='';
  1291. $outtype='';
  1292. $outprice_ht='';
  1293. $outprice_ttc='';
  1294. $outpricebasetype='';
  1295. $outtva_tx='';
  1296. $outqty=1;
  1297. $outdiscount=0;
  1298. $label=$objp->label;
  1299. if (! empty($objp->label_translated)) $label=$objp->label_translated;
  1300. if (! empty($filterkey) && $filterkey != '') $label=preg_replace('/('.preg_quote($filterkey).')/i','<strong>$1</strong>',$label,1);
  1301. $outkey=$objp->rowid;
  1302. $outref=$objp->ref;
  1303. $outlabel=$objp->label;
  1304. $outdesc=$objp->description;
  1305. $outtype=$objp->fk_product_type;
  1306. $opt = '<option value="'.$objp->rowid.'"';
  1307. $opt.= ($objp->rowid == $selected)?' selected="selected"':'';
  1308. $opt.= (!empty($objp->price_by_qty_rowid) && $objp->price_by_qty_rowid > 0)?' pbq="'.$objp->price_by_qty_rowid.'"':'';
  1309. if (! empty($conf->stock->enabled) && $objp->fk_product_type == 0 && isset($objp->stock))
  1310. {
  1311. if ($objp->stock > 0) $opt.= ' class="product_line_stock_ok"';
  1312. else if ($objp->stock <= 0) $opt.= ' class="product_line_stock_too_low"';
  1313. }
  1314. $opt.= '>';
  1315. $opt.= $objp->ref.' - '.dol_trunc($label,32).' - ';
  1316. $objRef = $objp->ref;
  1317. if (! empty($filterkey) && $filterkey != '') $objRef=preg_replace('/('.preg_quote($filterkey).')/i','<strong>$1</strong>',$objRef,1);
  1318. $outval.=$objRef.' - '.dol_trunc($label,32).' - ';
  1319. $found=0;
  1320. $currencytext=$langs->trans("Currency".$conf->currency);
  1321. $currencytextnoent=$langs->transnoentities("Currency".$conf->currency);
  1322. if (dol_strlen($currencytext) > 10) $currencytext=$conf->currency; // If text is too long, we use the short code
  1323. if (dol_strlen($currencytextnoent) > 10) $currencytextnoent=$conf->currency; // If text is too long, we use the short code
  1324. // Multiprice
  1325. if ($price_level >= 1) // If we need a particular price level (from 1 to 6)
  1326. {
  1327. $sql = "SELECT price, price_ttc, price_base_type, tva_tx";
  1328. $sql.= " FROM ".MAIN_DB_PREFIX."product_price";
  1329. $sql.= " WHERE fk_product='".$objp->rowid."'";
  1330. $sql.= " AND price_level=".$price_level;
  1331. $sql.= " ORDER BY date_price";
  1332. $sql.= " DESC LIMIT 1";
  1333. dol_syslog(get_class($this)."::_construct_product_list_option search price for level '.$price_level.' sql=".$sql);
  1334. $result2 = $this->db->query($sql);
  1335. if ($result2)
  1336. {
  1337. $objp2 = $this->db->fetch_object($result2);
  1338. if ($objp2)
  1339. {
  1340. $found=1;
  1341. if ($objp2->price_base_type == 'HT')
  1342. {
  1343. $opt.= price($objp2->price,1).' '.$currencytext.' '.$langs->trans("HT");
  1344. $outval.= price($objp2->price,1).' '.$currencytextnoent.' '.$langs->transnoentities("HT");
  1345. }
  1346. else
  1347. {
  1348. $opt.= price($objp2->price_ttc,1).' '.$currencytext.' '.$langs->trans("TTC");
  1349. $outval.= price($objp2->price_ttc,1).' '.$currencytextnoent.' '.$langs->transnoentities("TTC");
  1350. }
  1351. $outprice_ht=price($objp2->price);
  1352. $outprice_ttc=price($objp2->price_ttc);
  1353. $outpricebasetype=$objp2->price_base_type;
  1354. $outtva_tx=$objp2->tva_tx;
  1355. }
  1356. }
  1357. else
  1358. {
  1359. dol_print_error($this->db);
  1360. }
  1361. }
  1362. // Price by quantity
  1363. if (!empty($objp->quantity) && $objp->quantity >= 1)
  1364. {
  1365. $found = 1;
  1366. $outqty=$objp->quantity;
  1367. $outdiscount=$objp->remise_percent;
  1368. if ($objp->quantity == 1)
  1369. {
  1370. $opt.= price($objp->unitprice).' '.$currencytext."/";
  1371. $outval.= price($objp->unitprice).' '.$currencytextnoent."/";
  1372. $opt.= $langs->trans("Unit"); // Do not use strtolower because it breaks utf8 encoding
  1373. $outval.=$langs->transnoentities("Unit");
  1374. }
  1375. else
  1376. {
  1377. $opt.= price($objp->price).' '.$currencytext."/".$objp->quantity;
  1378. $outval.= price($objp->price).' '.$currencytextnoent."/".$objp->quantity;
  1379. $opt.= $langs->trans("Units"); // Do not use strtolower because it breaks utf8 encoding
  1380. $outval.=$langs->transnoentities("Units");
  1381. }
  1382. $outprice_ht=price($objp->unitprice);
  1383. $outprice_ttc=price($objp->unitprice * (1 + ($objp->tva_tx / 100)));
  1384. $outpricebasetype=$objp->price_base_type;
  1385. $outtva_tx=$objp->tva_tx;
  1386. }
  1387. if (!empty($objp->quantity) && $objp->quantity >= 1)
  1388. {
  1389. $opt.=" (".price($objp->unitprice).' '.$currencytext."/".$langs->trans("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
  1390. $outval.=" (".price($objp->unitprice).' '.$currencytextnoent."/".$langs->transnoentities("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
  1391. }
  1392. if (!empty($objp->remise_percent) && $objp->remise_percent >= 1)
  1393. {
  1394. $opt.=" - ".$langs->trans("Discount")." : ".vatrate($objp->remise_percent).' %';
  1395. $outval.=" - ".$langs->transnoentities("Discount")." : ".vatrate($objp->remise_percent).' %';
  1396. }
  1397. // If level no defined or multiprice not found, we used the default price
  1398. if (! $found)
  1399. {
  1400. if ($objp->price_base_type == 'HT')
  1401. {
  1402. $opt.= price($objp->price,1).' '.$currencytext.' '.$langs->trans("HT");
  1403. $outval.= price($objp->price,1).' '.$currencytextnoent.' '.$langs->transnoentities("HT");
  1404. }
  1405. else
  1406. {
  1407. $opt.= price($objp->price_ttc,1).' '.$currencytext.' '.$langs->trans("TTC");
  1408. $outval.= price($objp->price_ttc,1).' '.$currencytextnoent.' '.$langs->transnoentities("TTC");
  1409. }
  1410. $outprice_ht=price($objp->price);
  1411. $outprice_ttc=price($objp->price_ttc);
  1412. $outpricebasetype=$objp->price_base_type;
  1413. $outtva_tx=$objp->tva_tx;
  1414. }
  1415. if (! empty($conf->stock->enabled) && isset($objp->stock) && $objp->fk_product_type == 0)
  1416. {
  1417. $opt.= ' - '.$langs->trans("Stock").':'.$objp->stock;
  1418. $outval.=' - '.$langs->transnoentities("Stock").':'.$objp->stock;
  1419. }
  1420. if ($objp->duration)
  1421. {
  1422. $duration_value = substr($objp->duration,0,dol_strlen($objp->duration)-1);
  1423. $duration_unit = substr($objp->duration,-1);
  1424. if ($duration_value > 1)
  1425. {
  1426. $dur=array("h"=>$langs->trans("Hours"),"d"=>$langs->trans("Days"),"w"=>$langs->trans("Weeks"),"m"=>$langs->trans("Months"),"y"=>$langs->trans("Years"));
  1427. }
  1428. else
  1429. {
  1430. $dur=array("h"=>$langs->trans("Hour"),"d"=>$langs->trans("Day"),"w"=>$langs->trans("Week"),"m"=>$langs->trans("Month"),"y"=>$langs->trans("Year"));
  1431. }
  1432. $opt.= ' - '.$duration_value.' '.$langs->trans($dur[$duration_unit]);
  1433. $outval.=' - '.$duration_value.' '.$langs->transnoentities($dur[$duration_unit]);
  1434. }
  1435. $opt.= "</option>\n";
  1436. $optJson = array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'label2'=>$outlabel, 'desc'=>$outdesc, 'type'=>$outtype, 'price_ht'=>$outprice_ht, 'price_ttc'=>$outprice_ttc, 'pricebasetype'=>$outpricebasetype, 'tva_tx'=>$outtva_tx, 'qty'=>$outqty, 'discount'=>$outdiscount);
  1437. }
  1438. /**
  1439. * Return list of products for customer (in Ajax if Ajax activated or go to select_produits_fournisseurs_list)
  1440. *
  1441. * @param int $socid Id third party
  1442. * @param string $selected Preselected product
  1443. * @param string $htmlname Name of HTML Select
  1444. * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  1445. * @param string $filtre For a SQL filter
  1446. * @param array $ajaxoptions Options for ajax_autocompleter
  1447. * @return void
  1448. */
  1449. function select_produits_fournisseurs($socid, $selected='', $htmlname='productid', $filtertype='', $filtre='', $ajaxoptions=array())
  1450. {
  1451. global $langs,$conf;
  1452. global $price_level, $status, $finished;
  1453. if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->PRODUIT_USE_SEARCH_TO_SELECT))
  1454. {
  1455. // mode=2 means suppliers products
  1456. $urloption=($socid > 0?'socid='.$socid.'&':'').'htmlname='.$htmlname.'&outjson=1&price_level='.$price_level.'&type='.$filtertype.'&mode=2&status='.$status.'&finished='.$finished;
  1457. print ajax_autocompleter('', $htmlname, DOL_URL_ROOT.'/product/ajax/products.php', $urloption, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
  1458. print $langs->trans("RefOrLabel").' : <input type="text" size="16" name="search_'.$htmlname.'" id="search_'.$htmlname.'">';
  1459. print '<br>';
  1460. }
  1461. else
  1462. {
  1463. print $this->select_produits_fournisseurs_list($socid,$selected,$htmlname,$filtertype,$filtre,'',-1,0);
  1464. }
  1465. }
  1466. /**
  1467. * Return list of suppliers products
  1468. *
  1469. * @param int $socid Id societe fournisseur (0 pour aucun filtre)
  1470. * @param int $selected Produit pre-selectionne
  1471. * @param string $htmlname Nom de la zone select
  1472. * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  1473. * @param string $filtre Pour filtre sql
  1474. * @param string $filterkey Filtre des produits
  1475. * @param int $statut -1=Return all products, 0=Products not on sell, 1=Products on sell
  1476. * @param int $outputmode 0=HTML select string, 1=Array
  1477. * @return array Array of keys for json
  1478. */
  1479. function select_produits_fournisseurs_list($socid,$selected='',$htmlname='productid',$filtertype='',$filtre='',$filterkey='',$statut=-1,$outputmode=0)
  1480. {
  1481. global $langs,$conf;
  1482. $out='';
  1483. $outarray=array();
  1484. $langs->load('stocks');
  1485. $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,";
  1486. $sql.= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.remise_percent, pfp.remise, pfp.unitprice,";
  1487. $sql.= " s.nom";
  1488. $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
  1489. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
  1490. if ($socid) $sql.= " AND pfp.fk_soc = ".$socid;
  1491. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON pfp.fk_soc = s.rowid";
  1492. $sql.= " WHERE p.entity IN (".getEntity('product', 1).")";
  1493. $sql.= " AND p.tobuy = 1";
  1494. if (strval($filtertype) != '') $sql.=" AND p.fk_product_type=".$filtertype;
  1495. if (! empty($filtre)) $sql.=" ".$filtre;
  1496. // Add criteria on ref/label
  1497. if ($filterkey != '')
  1498. {
  1499. $sql.=' AND (';
  1500. $prefix=empty($conf->global->PRODUCT_DONOTSEARCH_ANYWHERE)?'%':''; // Can use index if PRODUCT_DONOTSEARCH_ANYWHERE is on
  1501. // For natural search
  1502. $scrit = explode(' ', $filterkey);
  1503. $i=0;
  1504. if (count($scrit) > 1) $sql.="(";
  1505. foreach ($scrit as $crit)
  1506. {
  1507. if ($i > 0) $sql.=" AND ";
  1508. $sql.="(pfp.ref_fourn LIKE '".$prefix.$crit."%' OR p.ref LIKE '".$prefix.$crit."%' OR p.label LIKE '".$prefix.$crit."%')";
  1509. $i++;
  1510. }
  1511. if (count($scrit) > 1) $sql.=")";
  1512. if (! empty($conf->barcode->enabled)) $sql.= " OR p.barcode LIKE '".$prefix.$filterkey."%'";
  1513. $sql.=')';
  1514. }
  1515. $sql.= " ORDER BY pfp.ref_fourn DESC, pfp.quantity ASC";
  1516. // Build output string
  1517. dol_syslog(get_class($this)."::select_produits_fournisseurs_list sql=".$sql,LOG_DEBUG);
  1518. $result=$this->db->query($sql);
  1519. if ($result)
  1520. {
  1521. $num = $this->db->num_rows($result);
  1522. //$out.='<select class="flat" id="select'.$htmlname.'" name="'.$htmlname.'">'; // remove select to have id same with combo and ajax
  1523. $out.='<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
  1524. if (! $selected) $out.='<option value="0" selected="selected">&nbsp;</option>';
  1525. else $out.='<option value="0">&nbsp;</option>';
  1526. $i = 0;
  1527. while ($i < $num)
  1528. {
  1529. $objp = $this->db->fetch_object($result);
  1530. $outkey=$objp->idprodfournprice;
  1531. $outref=$objp->ref;
  1532. $outval='';
  1533. $outqty=1;
  1534. $outdiscount=0;
  1535. $opt = '<option value="'.$objp->idprodfournprice.'"';
  1536. if ($selected && $selected == $objp->idprodfournprice) $opt.= ' selected="selected"';
  1537. if (empty($objp->idprodfournprice)) $opt.=' disabled="disabled"';
  1538. $opt.= '>';
  1539. $objRef = $objp->ref;
  1540. if ($filterkey && $filterkey != '') $objRef=preg_replace('/('.preg_quote($filterkey).')/i','<strong>$1</strong>',$objRef,1);
  1541. $objRefFourn = $objp->ref_fourn;
  1542. if ($filterkey && $filterkey != '') $objRefFourn=preg_replace('/('.preg_quote($filterkey).')/i','<strong>$1</strong>',$objRefFourn,1);
  1543. $label = $objp->label;
  1544. if ($filterkey && $filterkey != '') $label=preg_replace('/('.preg_quote($filterkey).')/i','<strong>$1</strong>',$label,1);
  1545. $opt.=$objp->ref;
  1546. if (! empty($objp->idprodfournprice)) $opt.=' ('.$objp->ref_fourn.')';
  1547. $opt.=' - ';
  1548. $outval.=$objRef;
  1549. if (! empty($objp->idprodfournprice)) $outval.=' ('.$objRefFourn.')';
  1550. $outval.=' - ';
  1551. $opt.=dol_trunc($objp->label,18).' - ';
  1552. $outval.=dol_trunc($label,18).' - ';
  1553. if (! empty($objp->idprodfournprice))
  1554. {
  1555. $currencytext=$langs->trans("Currency".$conf->currency);
  1556. $currencytextnoent=$langs->transnoentities("Currency".$conf->currency);
  1557. if (dol_strlen($currencytext) > 10) $currencytext=$conf->currency; // If text is too long, we use the short code
  1558. if (dol_strlen($currencytextnoent) > 10) $currencytextnoent=$conf->currency; // If text is too long, we use the short code
  1559. $outqty=$objp->quantity;
  1560. $outdiscount=$objp->remise_percent;
  1561. if ($objp->quantity == 1)
  1562. {
  1563. $opt.= price($objp->fprice).' '.$currencytext."/";
  1564. $outval.= price($objp->fprice).' '.$currencytextnoent."/";
  1565. $opt.= $langs->trans("Unit"); // Do not use strtolower because it breaks utf8 encoding
  1566. $outval.=$langs->transnoentities("Unit");
  1567. }
  1568. else
  1569. {
  1570. $opt.= price($objp->fprice).' '.$currencytext."/".$objp->quantity;
  1571. $outval.= price($objp->fprice).' '.$currencytextnoent."/".$objp->quantity;
  1572. $opt.= $langs->trans("Units"); // Do not use strtolower because it breaks utf8 encoding
  1573. $outval.=$langs->transnoentities("Units");
  1574. }
  1575. if ($objp->quantity >= 1)
  1576. {
  1577. $opt.=" (".price($objp->unitprice).' '.$currencytext."/".$langs->trans("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
  1578. $outval.=" (".price($objp->unitprice).' '.$currencytextnoent."/".$langs->transnoentities("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
  1579. }
  1580. if ($objp->remise_percent >= 1)
  1581. {
  1582. $opt.=" - ".$langs->trans("Discount")." : ".vatrate($objp->remise_percent).' %';
  1583. $outval.=" - ".$langs->transnoentities("Discount")." : ".vatrate($objp->remise_percent).' %';
  1584. }
  1585. if ($objp->duration)
  1586. {
  1587. $opt .= " - ".$objp->duration;
  1588. $outval.=" - ".$objp->duration;
  1589. }
  1590. if (! $socid)
  1591. {
  1592. $opt .= " - ".dol_trunc($objp->nom,8);
  1593. $outval.=" - ".dol_trunc($objp->nom,8);
  1594. }
  1595. }
  1596. else
  1597. {
  1598. $opt.= $langs->trans("NoPriceDefinedForThisSupplier");
  1599. $outval.=$langs->transnoentities("NoPriceDefinedForThisSupplier");
  1600. }
  1601. $opt .= "</option>\n";
  1602. // Add new entry
  1603. // "key" value of json key array is used by jQuery automatically as selected value
  1604. // "label" value of json key array is used by jQuery automatically as text for combo box
  1605. $out.=$opt;
  1606. array_push($outarray, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'discount'=>$outdiscount, 'disabled'=>(empty($objp->idprodfournprice)?true:false)));
  1607. // Exemple of var_dump $outarray
  1608. // array(1) {[0]=>array(6) {[key"]=>string(1) "2" ["value"]=>string(3) "ppp"
  1609. // ["label"]=>string(76) "ppp (<strong>f</strong>ff2) - ppp - 20,00 Euros/1unité (20,00 Euros/unité)"
  1610. // ["qty"]=>string(1) "1" ["discount"]=>string(1) "0" ["disabled"]=>bool(false)
  1611. //}
  1612. //var_dump($outval); var_dump(utf8_check($outval)); var_dump(json_encode($outval));
  1613. //$outval=array('label'=>'ppp (<strong>f</strong>ff2) - ppp - 20,00 Euros/ Unité (20,00 Euros/unité)');
  1614. //var_dump($outval); var_dump(utf8_check($outval)); var_dump(json_encode($outval));
  1615. $i++;
  1616. }
  1617. $out.='</select>';
  1618. $this->db->free($result);
  1619. if (empty($outputmode)) return $out;
  1620. return $outarray;
  1621. }
  1622. else
  1623. {
  1624. dol_print_error($this->db);
  1625. }
  1626. }
  1627. /**
  1628. * Return list of suppliers prices for a product
  1629. *
  1630. * @param int $productid Id of product
  1631. * @param string $htmlname Name of HTML field
  1632. * @return void
  1633. */
  1634. function select_product_fourn_price($productid,$htmlname='productfournpriceid')
  1635. {
  1636. global $langs,$conf;
  1637. $langs->load('stocks');
  1638. $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,";
  1639. $sql.= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.unitprice,";
  1640. $sql.= " s.nom";
  1641. $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
  1642. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
  1643. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON pfp.fk_soc = s.rowid";
  1644. $sql.= " WHERE p.entity IN (".getEntity('product', 1).")";
  1645. $sql.= " AND p.tobuy = 1";
  1646. $sql.= " AND s.fournisseur = 1";
  1647. $sql.= " AND p.rowid = ".$productid;
  1648. $sql.= " ORDER BY s.nom, pfp.ref_fourn DESC";
  1649. dol_syslog(get_class($this)."::select_product_fourn_price sql=".$sql,LOG_DEBUG);
  1650. $result=$this->db->query($sql);
  1651. if ($result)
  1652. {
  1653. $num = $this->db->num_rows($result);
  1654. $form = '<select class="flat" name="'.$htmlname.'">';
  1655. if (! $num)
  1656. {
  1657. $form.= '<option value="0">-- '.$langs->trans("NoSupplierPriceDefinedForThisProduct").' --</option>';
  1658. }
  1659. else
  1660. {
  1661. $form.= '<option value="0">&nbsp;</option>';
  1662. $i = 0;
  1663. while ($i < $num)
  1664. {
  1665. $objp = $this->db->fetch_object($result);
  1666. $opt = '<option value="'.$objp->idprodfournprice.'"';
  1667. $opt.= '>'.$objp->nom.' - '.$objp->ref_fourn.' - ';
  1668. if ($objp->quantity == 1)
  1669. {
  1670. $opt.= price($objp->fprice);
  1671. $opt.= $langs->trans("Currency".$conf->currency)."/";
  1672. }
  1673. $opt.= $objp->quantity.' ';
  1674. if ($objp->quantity == 1)
  1675. {
  1676. $opt.= strtolower($langs->trans("Unit"));
  1677. }
  1678. else
  1679. {
  1680. $opt.= strtolower($langs->trans("Units"));
  1681. }
  1682. if ($objp->quantity > 1)
  1683. {
  1684. $opt.=" - ";
  1685. $opt.= price($objp->unitprice).$langs->trans("Currency".$conf->currency)."/".strtolower($langs->trans("Unit"));
  1686. }
  1687. if ($objp->duration) $opt .= " - ".$objp->duration;
  1688. $opt .= "</option>\n";
  1689. $form.= $opt;
  1690. $i++;
  1691. }
  1692. $form.= '</select>';
  1693. $this->db->free($result);
  1694. }
  1695. return $form;
  1696. }
  1697. else
  1698. {
  1699. dol_print_error($this->db);
  1700. }
  1701. }
  1702. /**
  1703. * Return list of delivery address
  1704. *
  1705. * @param string $selected Id contact pre-selectionn
  1706. * @param int $socid Id of company
  1707. * @param string $htmlname Name of HTML field
  1708. * @param int $showempty Add an empty field
  1709. * @return void
  1710. */
  1711. function select_address($selected, $socid, $htmlname='address_id',$showempty=0)
  1712. {
  1713. // On recherche les utilisateurs
  1714. $sql = "SELECT a.rowid, a.label";
  1715. $sql .= " FROM ".MAIN_DB_PREFIX ."societe_address as a";
  1716. $sql .= " WHERE a.fk_soc = ".$socid;
  1717. $sql .= " ORDER BY a.label ASC";
  1718. dol_syslog(get_class($this)."::select_address sql=".$sql);
  1719. $resql=$this->db->query($sql);
  1720. if ($resql)
  1721. {
  1722. print '<select class="flat" name="'.$htmlname.'">';
  1723. if ($showempty) print '<option value="0">&nbsp;</option>';
  1724. $num = $this->db->num_rows($resql);
  1725. $i = 0;
  1726. if ($num)
  1727. {
  1728. while ($i < $num)
  1729. {
  1730. $obj = $this->db->fetch_object($resql);
  1731. if ($selected && $selected == $obj->rowid)
  1732. {
  1733. print '<option value="'.$obj->rowid.'" selected="selected">'.$obj->label.'</option>';
  1734. }
  1735. else
  1736. {
  1737. print '<option value="'.$obj->rowid.'">'.$obj->label.'</option>';
  1738. }
  1739. $i++;
  1740. }
  1741. }
  1742. print '</select>';
  1743. return $num;
  1744. }
  1745. else
  1746. {
  1747. dol_print_error($this->db);
  1748. }
  1749. }
  1750. /**
  1751. * Charge dans cache la liste des conditions de paiements possibles
  1752. *
  1753. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  1754. */
  1755. function load_cache_conditions_paiements()
  1756. {
  1757. global $langs;
  1758. if (count($this->cache_conditions_paiements)) return 0; // Cache deja charge
  1759. $sql = "SELECT rowid, code, libelle";
  1760. $sql.= " FROM ".MAIN_DB_PREFIX.'c_payment_term';
  1761. $sql.= " WHERE active=1";
  1762. $sql.= " ORDER BY sortorder";
  1763. dol_syslog(get_class($this).'::load_cache_conditions_paiements sql='.$sql,LOG_DEBUG);
  1764. $resql = $this->db->query($sql);
  1765. if ($resql)
  1766. {
  1767. $num = $this->db->num_rows($resql);
  1768. $i = 0;
  1769. while ($i < $num)
  1770. {
  1771. $obj = $this->db->fetch_object($resql);
  1772. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  1773. $libelle=($langs->trans("PaymentConditionShort".$obj->code)!=("PaymentConditionShort".$obj->code)?$langs->trans("PaymentConditionShort".$obj->code):($obj->libelle!='-'?$obj->libelle:''));
  1774. $this->cache_conditions_paiements[$obj->rowid]['code'] =$obj->code;
  1775. $this->cache_conditions_paiements[$obj->rowid]['label']=$libelle;
  1776. $i++;
  1777. }
  1778. return 1;
  1779. }
  1780. else {
  1781. dol_print_error($this->db);
  1782. return -1;
  1783. }
  1784. }
  1785. /**
  1786. * Charge dans cache la liste des délais de livraison possibles
  1787. *
  1788. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  1789. */
  1790. function load_cache_availability()
  1791. {
  1792. global $langs;
  1793. if (count($this->cache_availability)) return 0; // Cache deja charge
  1794. $sql = "SELECT rowid, code, label";
  1795. $sql.= " FROM ".MAIN_DB_PREFIX.'c_availability';
  1796. $sql.= " WHERE active=1";
  1797. $sql.= " ORDER BY rowid";
  1798. dol_syslog(get_class($this).'::load_cache_availability sql='.$sql,LOG_DEBUG);
  1799. $resql = $this->db->query($sql);
  1800. if ($resql)
  1801. {
  1802. $num = $this->db->num_rows($resql);
  1803. $i = 0;
  1804. while ($i < $num)
  1805. {
  1806. $obj = $this->db->fetch_object($resql);
  1807. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  1808. $label=($langs->trans("AvailabilityType".$obj->code)!=("AvailabilityType".$obj->code)?$langs->trans("AvailabilityType".$obj->code):($obj->label!='-'?$obj->label:''));
  1809. $this->cache_availability[$obj->rowid]['code'] =$obj->code;
  1810. $this->cache_availability[$obj->rowid]['label']=$label;
  1811. $i++;
  1812. }
  1813. return 1;
  1814. }
  1815. else {
  1816. dol_print_error($this->db);
  1817. return -1;
  1818. }
  1819. }
  1820. /**
  1821. * Retourne la liste des types de delais de livraison possibles
  1822. *
  1823. * @param int $selected Id du type de delais pre-selectionne
  1824. * @param string $htmlname Nom de la zone select
  1825. * @param string $filtertype To add a filter
  1826. * @param int $addempty Add empty entry
  1827. * @return void
  1828. */
  1829. function select_availability($selected='',$htmlname='availid',$filtertype='',$addempty=0)
  1830. {
  1831. global $langs,$user;
  1832. $this->load_cache_availability();
  1833. print '<select class="flat" name="'.$htmlname.'">';
  1834. if ($addempty) print '<option value="0">&nbsp;</option>';
  1835. foreach($this->cache_availability as $id => $arrayavailability)
  1836. {
  1837. if ($selected == $id)
  1838. {
  1839. print '<option value="'.$id.'" selected="selected">';
  1840. }
  1841. else
  1842. {
  1843. print '<option value="'.$id.'">';
  1844. }
  1845. print $arrayavailability['label'];
  1846. print '</option>';
  1847. }
  1848. print '</select>';
  1849. if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  1850. }
  1851. /**
  1852. * Load into cache cache_demand_reason, array of input reasons
  1853. *
  1854. * @return int Nb of lines loaded, 0 if already loaded, <0 if ko
  1855. */
  1856. function load_cache_demand_reason()
  1857. {
  1858. global $langs;
  1859. if (count($this->cache_demand_reason)) return 0; // Cache already loaded
  1860. $sql = "SELECT rowid, code, label";
  1861. $sql.= " FROM ".MAIN_DB_PREFIX.'c_input_reason';
  1862. $sql.= " WHERE active=1";
  1863. $sql.= " ORDER BY rowid";
  1864. dol_syslog(get_class($this)."::load_cache_demand_reason sql=".$sql,LOG_DEBUG);
  1865. $resql = $this->db->query($sql);
  1866. if ($resql)
  1867. {
  1868. $num = $this->db->num_rows($resql);
  1869. $i = 0;
  1870. $tmparray=array();
  1871. while ($i < $num)
  1872. {
  1873. $obj = $this->db->fetch_object($resql);
  1874. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  1875. $label=($langs->trans("DemandReasonType".$obj->code)!=("DemandReasonType".$obj->code)?$langs->trans("DemandReasonType".$obj->code):($obj->label!='-'?$obj->label:''));
  1876. $tmparray[$obj->rowid]['id'] =$obj->rowid;
  1877. $tmparray[$obj->rowid]['code'] =$obj->code;
  1878. $tmparray[$obj->rowid]['label']=$label;
  1879. $i++;
  1880. }
  1881. $this->cache_demand_reason=dol_sort_array($tmparray, 'label', 'asc');
  1882. unset($tmparray);
  1883. return 1;
  1884. }
  1885. else {
  1886. dol_print_error($this->db);
  1887. return -1;
  1888. }
  1889. }
  1890. /**
  1891. * Return list of events that triggered an object creation
  1892. *
  1893. * @param int $selected Id or code of type origin to select by default
  1894. * @param string $htmlname Nom de la zone select
  1895. * @param string $exclude To exclude a code value (Example: SRC_PROP)
  1896. * @param int $addempty Add an empty entry
  1897. * @return void
  1898. */
  1899. function select_demand_reason($selected='',$htmlname='demandreasonid',$exclude='',$addempty=0)
  1900. {
  1901. global $langs,$user;
  1902. $this->load_cache_demand_reason();
  1903. print '<select class="flat" name="'.$htmlname.'">';
  1904. if ($addempty) print '<option value="0"'.(empty($selected)?' selected="selected"':'').'>&nbsp;</option>';
  1905. foreach($this->cache_demand_reason as $id => $arraydemandreason)
  1906. {
  1907. if ($arraydemandreason['code']==$exclude) continue;
  1908. if ($selected && ($selected == $arraydemandreason['id'] || $selected == $arraydemandreason['code']))
  1909. {
  1910. print '<option value="'.$arraydemandreason['id'].'" selected="selected">';
  1911. }
  1912. else
  1913. {
  1914. print '<option value="'.$arraydemandreason['id'].'">';
  1915. }
  1916. print $arraydemandreason['label'];
  1917. print '</option>';
  1918. }
  1919. print '</select>';
  1920. if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  1921. }
  1922. /**
  1923. * Charge dans cache la liste des types de paiements possibles
  1924. *
  1925. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  1926. */
  1927. function load_cache_types_paiements()
  1928. {
  1929. global $langs;
  1930. if (count($this->cache_types_paiements)) return 0; // Cache deja charge
  1931. $sql = "SELECT id, code, libelle, type";
  1932. $sql.= " FROM ".MAIN_DB_PREFIX."c_paiement";
  1933. $sql.= " WHERE active > 0";
  1934. $sql.= " ORDER BY id";
  1935. dol_syslog(get_class($this)."::load_cache_types_paiements sql=".$sql,LOG_DEBUG);
  1936. $resql = $this->db->query($sql);
  1937. if ($resql)
  1938. {
  1939. $num = $this->db->num_rows($resql);
  1940. $i = 0;
  1941. while ($i < $num)
  1942. {
  1943. $obj = $this->db->fetch_object($resql);
  1944. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  1945. $libelle=($langs->trans("PaymentTypeShort".$obj->code)!=("PaymentTypeShort".$obj->code)?$langs->trans("PaymentTypeShort".$obj->code):($obj->libelle!='-'?$obj->libelle:''));
  1946. $this->cache_types_paiements[$obj->id]['code'] =$obj->code;
  1947. $this->cache_types_paiements[$obj->id]['label']=$libelle;
  1948. $this->cache_types_paiements[$obj->id]['type'] =$obj->type;
  1949. $i++;
  1950. }
  1951. return $num;
  1952. }
  1953. else
  1954. {
  1955. dol_print_error($this->db);
  1956. return -1;
  1957. }
  1958. }
  1959. /**
  1960. * Retourne la liste des types de paiements possibles
  1961. *
  1962. * @param string $selected Id du type de paiement pre-selectionne
  1963. * @param string $htmlname Nom de la zone select
  1964. * @param string $filtertype Pour filtre
  1965. * @param int $addempty Ajoute entree vide
  1966. * @return void
  1967. */
  1968. function select_conditions_paiements($selected='',$htmlname='condid',$filtertype=-1,$addempty=0)
  1969. {
  1970. global $langs,$user;
  1971. $this->load_cache_conditions_paiements();
  1972. print '<select class="flat" name="'.$htmlname.'">';
  1973. if ($addempty) print '<option value="0">&nbsp;</option>';
  1974. foreach($this->cache_conditions_paiements as $id => $arrayconditions)
  1975. {
  1976. if ($selected == $id)
  1977. {
  1978. print '<option value="'.$id.'" selected="selected">';
  1979. }
  1980. else
  1981. {
  1982. print '<option value="'.$id.'">';
  1983. }
  1984. print $arrayconditions['label'];
  1985. print '</option>';
  1986. }
  1987. print '</select>';
  1988. if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  1989. }
  1990. /**
  1991. * Return list of payment methods
  1992. *
  1993. * @param string $selected Id du mode de paiement pre-selectionne
  1994. * @param string $htmlname Nom de la zone select
  1995. * @param string $filtertype To filter on field type in llx_c_paiement (array('code'=>xx,'label'=>zz))
  1996. * @param int $format 0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code
  1997. * @param int $empty 1=peut etre vide, 0 sinon
  1998. * @param int $noadmininfo 0=Add admin info, 1=Disable admin info
  1999. * @param int $maxlength Max length of label
  2000. * @return void
  2001. */
  2002. function select_types_paiements($selected='',$htmlname='paiementtype',$filtertype='',$format=0, $empty=0, $noadmininfo=0,$maxlength=0)
  2003. {
  2004. global $langs,$user;
  2005. dol_syslog(get_class($this)."::select_type_paiements ".$selected.", ".$htmlname.", ".$filtertype.", ".$format,LOG_DEBUG);
  2006. $filterarray=array();
  2007. if ($filtertype == 'CRDT') $filterarray=array(0,2);
  2008. elseif ($filtertype == 'DBIT') $filterarray=array(1,2);
  2009. elseif ($filtertype != '' && $filtertype != '-1') $filterarray=explode(',',$filtertype);
  2010. $this->load_cache_types_paiements();
  2011. print '<select id="select'.$htmlname.'" class="flat selectpaymenttypes" name="'.$htmlname.'">';
  2012. if ($empty) print '<option value="">&nbsp;</option>';
  2013. foreach($this->cache_types_paiements as $id => $arraytypes)
  2014. {
  2015. // On passe si on a demande de filtrer sur des modes de paiments particuliers
  2016. if (count($filterarray) && ! in_array($arraytypes['type'],$filterarray)) continue;
  2017. // We discard empty line if showempty is on because an empty line has already been output.
  2018. if ($empty && empty($arraytypes['code'])) continue;
  2019. if ($format == 0) print '<option value="'.$id.'"';
  2020. if ($format == 1) print '<option value="'.$arraytypes['code'].'"';
  2021. if ($format == 2) print '<option value="'.$arraytypes['code'].'"';
  2022. if ($format == 3) print '<option value="'.$id.'"';
  2023. // Si selected est text, on compare avec code, sinon avec id
  2024. if (preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) print ' selected="selected"';
  2025. elseif ($selected == $id) print ' selected="selected"';
  2026. print '>';
  2027. if ($format == 0) $value=($maxlength?dol_trunc($arraytypes['label'],$maxlength):$arraytypes['label']);
  2028. if ($format == 1) $value=$arraytypes['code'];
  2029. if ($format == 2) $value=($maxlength?dol_trunc($arraytypes['label'],$maxlength):$arraytypes['label']);
  2030. if ($format == 3) $value=$arraytypes['code'];
  2031. print $value?$value:'&nbsp;';
  2032. print '</option>';
  2033. }
  2034. print '</select>';
  2035. if ($user->admin && ! $noadmininfo) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  2036. }
  2037. /**
  2038. * Selection HT or TTC
  2039. *
  2040. * @param string $selected Id pre-selectionne
  2041. * @param string $htmlname Nom de la zone select
  2042. * @return void
  2043. */
  2044. function select_PriceBaseType($selected='',$htmlname='price_base_type')
  2045. {
  2046. print $this->load_PriceBaseType($selected,$htmlname);
  2047. }
  2048. /**
  2049. * Selection HT or TTC
  2050. *
  2051. * @param string $selected Id pre-selectionne
  2052. * @param string $htmlname Nom de la zone select
  2053. * @return void
  2054. */
  2055. function load_PriceBaseType($selected='',$htmlname='price_base_type')
  2056. {
  2057. global $langs;
  2058. $return='';
  2059. $return.= '<select class="flat" name="'.$htmlname.'">';
  2060. $options = array(
  2061. 'HT'=>$langs->trans("HT"),
  2062. 'TTC'=>$langs->trans("TTC")
  2063. );
  2064. foreach($options as $id => $value)
  2065. {
  2066. if ($selected == $id)
  2067. {
  2068. $return.= '<option value="'.$id.'" selected="selected">'.$value;
  2069. }
  2070. else
  2071. {
  2072. $return.= '<option value="'.$id.'">'.$value;
  2073. }
  2074. $return.= '</option>';
  2075. }
  2076. $return.= '</select>';
  2077. return $return;
  2078. }
  2079. /**
  2080. * Return a HTML select list of bank accounts
  2081. *
  2082. * @param string $selected Id account pre-selected
  2083. * @param string $htmlname Name of select zone
  2084. * @param int $statut Status of searched accounts (0=open, 1=closed)
  2085. * @param string $filtre To filter list
  2086. * @param int $useempty 1=Add an empty value in list, 2=Add an empty value in list only if there is more than 2 entries.
  2087. * @param string $moreattrib To add more attribute on select
  2088. * @return void
  2089. */
  2090. function select_comptes($selected='',$htmlname='accountid',$statut=0,$filtre='',$useempty=0,$moreattrib='')
  2091. {
  2092. global $langs, $conf;
  2093. $langs->load("admin");
  2094. $sql = "SELECT rowid, label, bank";
  2095. $sql.= " FROM ".MAIN_DB_PREFIX."bank_account";
  2096. $sql.= " WHERE clos = '".$statut."'";
  2097. $sql.= " AND entity = ".$conf->entity;
  2098. if ($filtre) $sql.=" AND ".$filtre;
  2099. $sql.= " ORDER BY label";
  2100. dol_syslog(get_class($this)."::select_comptes sql=".$sql);
  2101. $result = $this->db->query($sql);
  2102. if ($result)
  2103. {
  2104. $num = $this->db->num_rows($result);
  2105. $i = 0;
  2106. if ($num)
  2107. {
  2108. print '<select id="select'.$htmlname.'" class="flat selectbankaccount" name="'.$htmlname.'"'.($moreattrib?' '.$moreattrib:'').'>';
  2109. if ($useempty == 1 || ($useempty == 2 && $num > 1))
  2110. {
  2111. print '<option value="-1">&nbsp;</option>';
  2112. }
  2113. while ($i < $num)
  2114. {
  2115. $obj = $this->db->fetch_object($result);
  2116. if ($selected == $obj->rowid)
  2117. {
  2118. print '<option value="'.$obj->rowid.'" selected="selected">';
  2119. }
  2120. else
  2121. {
  2122. print '<option value="'.$obj->rowid.'">';
  2123. }
  2124. print $obj->label;
  2125. print '</option>';
  2126. $i++;
  2127. }
  2128. print "</select>";
  2129. }
  2130. else
  2131. {
  2132. print $langs->trans("NoActiveBankAccountDefined");
  2133. }
  2134. }
  2135. else {
  2136. dol_print_error($this->db);
  2137. }
  2138. }
  2139. /**
  2140. * Return list of categories having choosed type
  2141. *
  2142. * @param int $type Type de categories (0=product, 1=supplier, 2=customer, 3=member)
  2143. * @param string $selected Id of category preselected
  2144. * @param string $htmlname HTML field name
  2145. * @param int $maxlength Maximum length for labels
  2146. * @param int $excludeafterid Exclude all categories after this leaf in category tree.
  2147. * @return void
  2148. */
  2149. function select_all_categories($type, $selected='', $htmlname="parent", $maxlength=64, $excludeafterid=0)
  2150. {
  2151. global $langs;
  2152. $langs->load("categories");
  2153. $cat = new Categorie($this->db);
  2154. $cate_arbo = $cat->get_full_arbo($type,$excludeafterid);
  2155. $output = '<select class="flat" name="'.$htmlname.'">';
  2156. if (is_array($cate_arbo))
  2157. {
  2158. if (! count($cate_arbo)) $output.= '<option value="-1" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
  2159. else
  2160. {
  2161. $output.= '<option value="-1">&nbsp;</option>';
  2162. foreach($cate_arbo as $key => $value)
  2163. {
  2164. if ($cate_arbo[$key]['id'] == $selected)
  2165. {
  2166. $add = 'selected="selected" ';
  2167. }
  2168. else
  2169. {
  2170. $add = '';
  2171. }
  2172. $output.= '<option '.$add.'value="'.$cate_arbo[$key]['id'].'">'.dol_trunc($cate_arbo[$key]['fulllabel'],$maxlength,'middle').'</option>';
  2173. }
  2174. }
  2175. }
  2176. $output.= '</select>';
  2177. $output.= "\n";
  2178. return $output;
  2179. }
  2180. /**
  2181. * Show a confirmation HTML form or AJAX popup
  2182. *
  2183. * @param string $page Url of page to call if confirmation is OK
  2184. * @param string $title Title
  2185. * @param string $question Question
  2186. * @param string $action Action
  2187. * @param array $formquestion An array with forms complementary inputs
  2188. * @param string $selectedchoice "" or "no" or "yes"
  2189. * @param int $useajax 0=No, 1=Yes, 2=Yes but submit page with &confirm=no if choice is No
  2190. * @param int $height Force height of box
  2191. * @param int $width Force width of box
  2192. * @return void
  2193. */
  2194. function form_confirm($page, $title, $question, $action, $formquestion='', $selectedchoice="", $useajax=0, $height=170, $width=500)
  2195. {
  2196. print $this->formconfirm($page, $title, $question, $action, $formquestion, $selectedchoice, $useajax, $height, $width);
  2197. }
  2198. /**
  2199. * Show a confirmation HTML form or AJAX popup
  2200. *
  2201. * @param string $page Url of page to call if confirmation is OK
  2202. * @param string $title Title
  2203. * @param string $question Question
  2204. * @param string $action Action
  2205. * @param array $formquestion An array with complementary inputs to add into forms: array(array('label'=> ,'type'=> , ))
  2206. * @param string $selectedchoice "" or "no" or "yes"
  2207. * @param int $useajax 0=No, 1=Yes, 2=Yes but submit page with &confirm=no if choice is No, 'xxx'=preoutput confirm box with div id=dialog-confirm-xxx
  2208. * @param int $height Force height of box
  2209. * @param int $width Force width of bow
  2210. * @return string HTML ajax code if a confirm ajax popup is required, Pure HTML code if it's an html form
  2211. */
  2212. function formconfirm($page, $title, $question, $action, $formquestion='', $selectedchoice="", $useajax=0, $height=170, $width=500)
  2213. {
  2214. global $langs,$conf;
  2215. global $useglobalvars;
  2216. $more='';
  2217. $formconfirm='';
  2218. $inputok=array();
  2219. $inputko=array();
  2220. // Clean parameters
  2221. $newselectedchoice=empty($selectedchoice)?"no":$selectedchoice;
  2222. if (is_array($formquestion) && ! empty($formquestion))
  2223. {
  2224. $more.='<table class="paddingrightonly" width="100%">'."\n";
  2225. $more.='<tr><td colspan="3" valign="top">'.(! empty($formquestion['text'])?$formquestion['text']:'').'</td></tr>'."\n";
  2226. foreach ($formquestion as $key => $input)
  2227. {
  2228. if (is_array($input) && ! empty($input))
  2229. {
  2230. $size=(! empty($input['size'])?' size="'.$input['size'].'"':'');
  2231. if ($input['type'] == 'text')
  2232. {
  2233. $more.='<tr><td valign="top">'.$input['label'].'</td><td valign="top" colspan="2" align="left"><input type="text" class="flat" id="'.$input['name'].'" name="'.$input['name'].'"'.$size.' value="'.$input['value'].'" /></td></tr>'."\n";
  2234. }
  2235. else if ($input['type'] == 'password')
  2236. {
  2237. $more.='<tr><td valign="top">'.$input['label'].'</td><td valign="top" colspan="2" align="left"><input type="password" class="flat" id="'.$input['name'].'" name="'.$input['name'].'"'.$size.' value="'.$input['value'].'" /></td></tr>'."\n";
  2238. }
  2239. else if ($input['type'] == 'select')
  2240. {
  2241. $more.='<tr><td valign="top" style="padding: 4px !important;">';
  2242. if (! empty($input['label'])) $more.=$input['label'].'</td><td valign="top" colspan="2" align="left" style="padding: 4px !important;">';
  2243. $more.=$this->selectarray($input['name'],$input['values'],$input['default'],1);
  2244. $more.='</td></tr>'."\n";
  2245. }
  2246. else if ($input['type'] == 'checkbox')
  2247. {
  2248. $more.='<tr>';
  2249. $more.='<td valign="top">'.$input['label'].' </td><td valign="top" align="left">';
  2250. $more.='<input type="checkbox" class="flat" id="'.$input['name'].'" name="'.$input['name'].'"';
  2251. if (! is_bool($input['value']) && $input['value'] != 'false') $more.=' checked="checked"';
  2252. if (is_bool($input['value']) && $input['value']) $more.=' checked="checked"';
  2253. if (isset($input['disabled'])) $more.=' disabled="disabled"';
  2254. $more.=' /></td>';
  2255. $more.='<td valign="top" align="left">&nbsp;</td>';
  2256. $more.='</tr>'."\n";
  2257. }
  2258. else if ($input['type'] == 'radio')
  2259. {
  2260. $i=0;
  2261. foreach($input['values'] as $selkey => $selval)
  2262. {
  2263. $more.='<tr>';
  2264. if ($i==0) $more.='<td valign="top">'.$input['label'].'</td>';
  2265. else $more.='<td>&nbsp;</td>';
  2266. $more.='<td valign="top" width="20"><input type="radio" class="flat" id="'.$input['name'].'" name="'.$input['name'].'" value="'.$selkey.'"';
  2267. if ($input['disabled']) $more.=' disabled="disabled"';
  2268. $more.=' /></td>';
  2269. $more.='<td valign="top" align="left">';
  2270. $more.=$selval;
  2271. $more.='</td></tr>'."\n";
  2272. $i++;
  2273. }
  2274. }
  2275. else if ($input['type'] == 'other')
  2276. {
  2277. $more.='<tr><td valign="top">';
  2278. if (! empty($input['label'])) $more.=$input['label'].'</td><td valign="top" colspan="2" align="left">';
  2279. $more.=$input['value'];
  2280. $more.='</td></tr>'."\n";
  2281. }
  2282. else if ($input['type'] == 'hidden')
  2283. {
  2284. $more.='<input type="hidden" id="'.$input['name'].'" name="'.$input['name'].'" value="'.$input['value'].'">';
  2285. }
  2286. }
  2287. }
  2288. $more.='</table>'."\n";
  2289. }
  2290. if ($useajax && $conf->use_javascript_ajax)
  2291. {
  2292. $autoOpen=true;
  2293. $dialogconfirm='dialog-confirm';
  2294. $button='';
  2295. if (! is_int($useajax))
  2296. {
  2297. $button=$useajax;
  2298. $useajax=1;
  2299. $autoOpen=false;
  2300. $dialogconfirm.='-'.$button;
  2301. }
  2302. $pageyes=$page.(preg_match('/\?/',$page)?'&':'?').'action='.$action.'&confirm=yes';
  2303. $pageno=($useajax == 2 ? $page.(preg_match('/\?/',$page)?'&':'?').'confirm=no':'');
  2304. // Add input fields into list of fields to read during submit (inputok and inputko)
  2305. if (is_array($formquestion))
  2306. {
  2307. foreach ($formquestion as $key => $input)
  2308. {
  2309. if (isset($input['name'])) array_push($inputok,$input['name']);
  2310. if (isset($input['inputko']) && $input['inputko'] == 1) array_push($inputko,$input['name']);
  2311. }
  2312. }
  2313. // Show JQuery confirm box. Note that global var $useglobalvars is used inside this template
  2314. $formconfirm.= '<div id="'.$dialogconfirm.'" title="'.dol_escape_htmltag($title).'" style="display: none;">';
  2315. if (! empty($more)) {
  2316. $formconfirm.= '<p>'.$more.'</p>';
  2317. }
  2318. $formconfirm.= img_help('','').' '.$question;
  2319. $formconfirm.= '</div>';
  2320. $formconfirm.= '<script type="text/javascript">';
  2321. $formconfirm.='
  2322. $(function() {
  2323. $( "#'.$dialogconfirm.'" ).dialog({
  2324. autoOpen: '.($autoOpen ? "true" : "false").',';
  2325. if ($newselectedchoice == 'no')
  2326. {
  2327. $formconfirm.='
  2328. open: function() {
  2329. $(this).parent().find("button.ui-button:eq(1)").focus();
  2330. },';
  2331. }
  2332. $formconfirm.='
  2333. resizable: false,
  2334. height: "'.$height.'",
  2335. width: "'.$width.'",
  2336. modal: true,
  2337. closeOnEscape: false,
  2338. buttons: {
  2339. "'.dol_escape_js($langs->transnoentities("Yes")).'": function() {
  2340. var options="";
  2341. var inputok = '.json_encode($inputok).';
  2342. var pageyes = "'.dol_escape_js(! empty($pageyes)?$pageyes:'').'";
  2343. if (inputok.length>0) {
  2344. $.each(inputok, function(i, inputname) {
  2345. var more = "";
  2346. if ($("#" + inputname).attr("type") == "checkbox") { more = ":checked"; }
  2347. var inputvalue = $("#" + inputname + more).val();
  2348. if (typeof inputvalue == "undefined") { inputvalue=""; }
  2349. options += "&" + inputname + "=" + inputvalue;
  2350. });
  2351. }
  2352. var urljump = pageyes + (pageyes.indexOf("?") < 0 ? "?" : "") + options;
  2353. //alert(urljump);
  2354. if (pageyes.length > 0) { location.href = urljump; }
  2355. $(this).dialog("close");
  2356. },
  2357. "'.dol_escape_js($langs->transnoentities("No")).'": function() {
  2358. var options = "";
  2359. var inputko = '.json_encode($inputko).';
  2360. var pageno="'.dol_escape_js(! empty($pageno)?$pageno:'').'";
  2361. if (inputko.length>0) {
  2362. $.each(inputko, function(i, inputname) {
  2363. var more = "";
  2364. if ($("#" + inputname).attr("type") == "checkbox") { more = ":checked"; }
  2365. var inputvalue = $("#" + inputname + more).val();
  2366. if (typeof inputvalue == "undefined") { inputvalue=""; }
  2367. options += "&" + inputname + "=" + inputvalue;
  2368. });
  2369. }
  2370. var urljump=pageno + (pageno.indexOf("?") < 0 ? "?" : "") + options;
  2371. //alert(urljump);
  2372. if (pageno.length > 0) { location.href = urljump; }
  2373. $(this).dialog("close");
  2374. }
  2375. }
  2376. });
  2377. var button = "'.$button.'";
  2378. if (button.length > 0) {
  2379. $( "#" + button ).click(function() {
  2380. $("#'.$dialogconfirm.'").dialog("open");
  2381. });
  2382. }
  2383. });
  2384. </script>';
  2385. }
  2386. else
  2387. {
  2388. $formconfirm.= "\n<!-- begin form_confirm page=".$page." -->\n";
  2389. $formconfirm.= '<form method="POST" action="'.$page.'" class="notoptoleftroright">'."\n";
  2390. $formconfirm.= '<input type="hidden" name="action" value="'.$action.'">';
  2391. $formconfirm.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">'."\n";
  2392. $formconfirm.= '<table width="100%" class="valid">'."\n";
  2393. // Line title
  2394. $formconfirm.= '<tr class="validtitre"><td class="validtitre" colspan="3">'.img_picto('','recent').' '.$title.'</td></tr>'."\n";
  2395. // Line form fields
  2396. if ($more)
  2397. {
  2398. $formconfirm.='<tr class="valid"><td class="valid" colspan="3">'."\n";
  2399. $formconfirm.=$more;
  2400. $formconfirm.='</td></tr>'."\n";
  2401. }
  2402. // Line with question
  2403. $formconfirm.= '<tr class="valid">';
  2404. $formconfirm.= '<td class="valid">'.$question.'</td>';
  2405. $formconfirm.= '<td class="valid">';
  2406. $formconfirm.= $this->selectyesno("confirm",$newselectedchoice);
  2407. $formconfirm.= '</td>';
  2408. $formconfirm.= '<td class="valid" align="center"><input class="button" type="submit" value="'.$langs->trans("Validate").'"></td>';
  2409. $formconfirm.= '</tr>'."\n";
  2410. $formconfirm.= '</table>'."\n";
  2411. $formconfirm.= "</form>\n";
  2412. $formconfirm.= '<br>';
  2413. $formconfirm.= "<!-- end form_confirm -->\n";
  2414. }
  2415. return $formconfirm;
  2416. }
  2417. /**
  2418. * Show a form to select a project
  2419. *
  2420. * @param int $page Page
  2421. * @param int $socid Id third party
  2422. * @param int $selected Id pre-selected project
  2423. * @param string $htmlname Name of select field
  2424. * @return void
  2425. */
  2426. function form_project($page, $socid, $selected='', $htmlname='projectid')
  2427. {
  2428. global $langs;
  2429. require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php';
  2430. $langs->load("project");
  2431. if ($htmlname != "none")
  2432. {
  2433. print '<form method="post" action="'.$page.'">';
  2434. print '<input type="hidden" name="action" value="classin">';
  2435. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2436. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2437. print '<tr><td>';
  2438. //print "$socid,$selected,$htmlname";
  2439. select_projects($socid,$selected,$htmlname);
  2440. print '</td>';
  2441. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2442. print '</tr></table></form>';
  2443. }
  2444. else
  2445. {
  2446. if ($selected)
  2447. {
  2448. $projet = new Project($this->db);
  2449. $projet->fetch($selected);
  2450. //print '<a href="'.DOL_URL_ROOT.'/projet/fiche.php?id='.$selected.'">'.$projet->title.'</a>';
  2451. print $projet->getNomUrl(0,'',1);
  2452. }
  2453. else
  2454. {
  2455. print "&nbsp;";
  2456. }
  2457. }
  2458. }
  2459. /**
  2460. * Show a form to select payment conditions
  2461. *
  2462. * @param int $page Page
  2463. * @param string $selected Id condition pre-selectionne
  2464. * @param string $htmlname Name of select html field
  2465. * @param int $addempty Ajoute entree vide
  2466. * @return void
  2467. */
  2468. function form_conditions_reglement($page, $selected='', $htmlname='cond_reglement_id', $addempty=0)
  2469. {
  2470. global $langs;
  2471. if ($htmlname != "none")
  2472. {
  2473. print '<form method="post" action="'.$page.'">';
  2474. print '<input type="hidden" name="action" value="setconditions">';
  2475. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2476. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2477. print '<tr><td>';
  2478. $this->select_conditions_paiements($selected,$htmlname,-1,$addempty);
  2479. print '</td>';
  2480. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2481. print '</tr></table></form>';
  2482. }
  2483. else
  2484. {
  2485. if ($selected)
  2486. {
  2487. $this->load_cache_conditions_paiements();
  2488. print $this->cache_conditions_paiements[$selected]['label'];
  2489. } else {
  2490. print "&nbsp;";
  2491. }
  2492. }
  2493. }
  2494. /**
  2495. * Show a form to select a delivery delay
  2496. *
  2497. * @param int $page Page
  2498. * @param string $selected Id condition pre-selectionne
  2499. * @param string $htmlname Name of select html field
  2500. * @param int $addempty Ajoute entree vide
  2501. * @return void
  2502. */
  2503. function form_availability($page, $selected='', $htmlname='availability', $addempty=0)
  2504. {
  2505. global $langs;
  2506. if ($htmlname != "none")
  2507. {
  2508. print '<form method="post" action="'.$page.'">';
  2509. print '<input type="hidden" name="action" value="setavailability">';
  2510. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2511. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2512. print '<tr><td>';
  2513. $this->select_availability($selected,$htmlname,-1,$addempty);
  2514. print '</td>';
  2515. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2516. print '</tr></table></form>';
  2517. }
  2518. else
  2519. {
  2520. if ($selected)
  2521. {
  2522. $this->load_cache_availability();
  2523. print $this->cache_availability[$selected]['label'];
  2524. } else {
  2525. print "&nbsp;";
  2526. }
  2527. }
  2528. }
  2529. /**
  2530. * Show a select form to select origin
  2531. *
  2532. * @param string $page Page
  2533. * @param string $selected Id condition pre-selectionne
  2534. * @param string $htmlname Name of select html field
  2535. * @param int $addempty Add empty entry
  2536. * @return void
  2537. */
  2538. function form_demand_reason($page, $selected='', $htmlname='demandreason', $addempty=0)
  2539. {
  2540. global $langs;
  2541. if ($htmlname != "none")
  2542. {
  2543. print '<form method="post" action="'.$page.'">';
  2544. print '<input type="hidden" name="action" value="setdemandreason">';
  2545. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2546. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2547. print '<tr><td>';
  2548. $this->select_demand_reason($selected,$htmlname,-1,$addempty);
  2549. print '</td>';
  2550. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2551. print '</tr></table></form>';
  2552. }
  2553. else
  2554. {
  2555. if ($selected)
  2556. {
  2557. $this->load_cache_demand_reason();
  2558. foreach ($this->cache_demand_reason as $key => $val)
  2559. {
  2560. if ($val['id'] == $selected)
  2561. {
  2562. print $val['label'];
  2563. break;
  2564. }
  2565. }
  2566. } else {
  2567. print "&nbsp;";
  2568. }
  2569. }
  2570. }
  2571. /**
  2572. * Show a form to select a date
  2573. *
  2574. * @param string $page Page
  2575. * @param string $selected Date preselected
  2576. * @param string $htmlname Name of input html field
  2577. * @param int $displayhour Display hour selector
  2578. * @param int $displaymin Display minutes selector
  2579. * @return void
  2580. */
  2581. function form_date($page, $selected, $htmlname,$displayhour=0,$displaymin=0)
  2582. {
  2583. global $langs;
  2584. if ($htmlname != "none")
  2585. {
  2586. print '<form method="post" action="'.$page.'" name="form'.$htmlname.'">';
  2587. print '<input type="hidden" name="action" value="set'.$htmlname.'">';
  2588. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2589. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2590. print '<tr><td>';
  2591. print $this->select_date($selected,$htmlname,$displayhour,$displaymin,1,'form'.$htmlname);
  2592. print '</td>';
  2593. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2594. print '</tr></table></form>';
  2595. }
  2596. else
  2597. {
  2598. if ($selected)
  2599. {
  2600. $this->load_cache_types_paiements();
  2601. print $this->cache_types_paiements[$selected]['label'];
  2602. } else {
  2603. print "&nbsp;";
  2604. }
  2605. }
  2606. }
  2607. /**
  2608. * Show a select form to choose a user
  2609. *
  2610. * @param string $page Page
  2611. * @param string $selected Id of user preselected
  2612. * @param string $htmlname Name of input html field
  2613. * @param array $exclude List of users id to exclude
  2614. * @param array $include List of users id to include
  2615. * @return void
  2616. */
  2617. function form_users($page, $selected='', $htmlname='userid', $exclude='', $include='')
  2618. {
  2619. global $langs;
  2620. if ($htmlname != "none")
  2621. {
  2622. print '<form method="POST" action="'.$page.'" name="form'.$htmlname.'">';
  2623. print '<input type="hidden" name="action" value="set'.$htmlname.'">';
  2624. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2625. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2626. print '<tr><td>';
  2627. print $this->select_users($selected,$htmlname,1,$exclude,0,$include);
  2628. print '</td>';
  2629. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2630. print '</tr></table></form>';
  2631. }
  2632. else
  2633. {
  2634. if ($selected)
  2635. {
  2636. require_once DOL_DOCUMENT_ROOT .'/user/class/user.class.php';
  2637. //$this->load_cache_contacts();
  2638. //print $this->cache_contacts[$selected];
  2639. $theuser=new User($this->db);
  2640. $theuser->fetch($selected);
  2641. print $theuser->getNomUrl(1);
  2642. } else {
  2643. print "&nbsp;";
  2644. }
  2645. }
  2646. }
  2647. /**
  2648. * Affiche formulaire de selection des modes de reglement
  2649. *
  2650. * @param string $page Page
  2651. * @param int $selected Id mode pre-selectionne
  2652. * @param string $htmlname Name of select html field
  2653. * @return void
  2654. */
  2655. function form_modes_reglement($page, $selected='', $htmlname='mode_reglement_id')
  2656. {
  2657. global $langs;
  2658. if ($htmlname != "none")
  2659. {
  2660. print '<form method="POST" action="'.$page.'">';
  2661. print '<input type="hidden" name="action" value="setmode">';
  2662. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2663. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2664. print '<tr><td>';
  2665. $this->select_types_paiements($selected,$htmlname);
  2666. print '</td>';
  2667. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2668. print '</tr></table></form>';
  2669. }
  2670. else
  2671. {
  2672. if ($selected)
  2673. {
  2674. $this->load_cache_types_paiements();
  2675. print $this->cache_types_paiements[$selected]['label'];
  2676. } else {
  2677. print "&nbsp;";
  2678. }
  2679. }
  2680. }
  2681. /**
  2682. * Show a select box with available absolute discounts
  2683. *
  2684. * @param string $page Page URL where form is shown
  2685. * @param int $selected Value pre-selected
  2686. * @param string $htmlname Nom du formulaire select. Si 'none', non modifiable. Example 'remise_id'.
  2687. * @param int $socid Third party id
  2688. * @param float $amount Total amount available
  2689. * @param string $filter SQL filter on discounts
  2690. * @param int $maxvalue Max value for lines that can be selected
  2691. * @param string $more More string to add
  2692. * @return void
  2693. */
  2694. function form_remise_dispo($page, $selected, $htmlname, $socid, $amount, $filter='', $maxvalue=0, $more='')
  2695. {
  2696. global $conf,$langs;
  2697. if ($htmlname != "none")
  2698. {
  2699. print '<form method="post" action="'.$page.'">';
  2700. print '<input type="hidden" name="action" value="setabsolutediscount">';
  2701. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2702. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2703. print '<tr><td class="nowrap">';
  2704. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
  2705. {
  2706. if (! $filter || $filter=="fk_facture_source IS NULL") print $langs->trans("CompanyHasAbsoluteDiscount",price($amount),$langs->transnoentities("Currency".$conf->currency)).': '; // If we want deposit to be substracted to payments only and not to total of final invoice
  2707. else print $langs->trans("CompanyHasCreditNote",price($amount),$langs->transnoentities("Currency".$conf->currency)).': ';
  2708. }
  2709. else
  2710. {
  2711. if (! $filter || $filter=="fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND description='(DEPOSIT)')") print $langs->trans("CompanyHasAbsoluteDiscount",price($amount),$langs->transnoentities("Currency".$conf->currency)).': ';
  2712. else print $langs->trans("CompanyHasCreditNote",price($amount),$langs->transnoentities("Currency".$conf->currency)).': ';
  2713. }
  2714. $newfilter='fk_facture IS NULL AND fk_facture_line IS NULL'; // Remises disponibles
  2715. if ($filter) $newfilter.=' AND ('.$filter.')';
  2716. $nbqualifiedlines=$this->select_remises($selected,$htmlname,$newfilter,$socid,$maxvalue);
  2717. print '</td>';
  2718. print '<td class="nowrap">';
  2719. if ($nbqualifiedlines > 0)
  2720. {
  2721. print ' &nbsp; <input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("UseLine")).'"';
  2722. if ($filter && $filter != "fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND description='(DEPOSIT)')") print ' title="'.$langs->trans("UseCreditNoteInInvoicePayment").'"';
  2723. print '>';
  2724. }
  2725. if ($more) print $more;
  2726. print '</td>';
  2727. print '</tr></table></form>';
  2728. }
  2729. else
  2730. {
  2731. if ($selected)
  2732. {
  2733. print $selected;
  2734. }
  2735. else
  2736. {
  2737. print "0";
  2738. }
  2739. }
  2740. }
  2741. /**
  2742. * Affiche formulaire de selection des contacts
  2743. *
  2744. * @param string $page Page
  2745. * @param Societe $societe Third party
  2746. * @param int $selected Id contact pre-selectionne
  2747. * @param string $htmlname Nom du formulaire select
  2748. * @return void
  2749. */
  2750. function form_contacts($page, $societe, $selected='', $htmlname='contactidp')
  2751. {
  2752. global $langs;
  2753. if ($htmlname != "none")
  2754. {
  2755. print '<form method="post" action="'.$page.'">';
  2756. print '<input type="hidden" name="action" value="set_contact">';
  2757. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2758. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2759. print '<tr><td>';
  2760. $num=$this->select_contacts($societe->id, $selected, $htmlname);
  2761. if ($num==0)
  2762. {
  2763. $addcontact = (! empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("AddContact") : $langs->trans("AddContactAddress"));
  2764. print '<font class="error">Cette societe n\'a pas de contact, veuillez en cr�er un avant de faire votre proposition commerciale</font><br>';
  2765. print '<a href="'.DOL_URL_ROOT.'/contact/fiche.php?socid='.$societe->id.'&amp;action=create&amp;backtoreferer=1">'.$addcontact.'</a>';
  2766. }
  2767. print '</td>';
  2768. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2769. print '</tr></table></form>';
  2770. }
  2771. else
  2772. {
  2773. if ($selected)
  2774. {
  2775. require_once DOL_DOCUMENT_ROOT .'/contact/class/contact.class.php';
  2776. //$this->load_cache_contacts();
  2777. //print $this->cache_contacts[$selected];
  2778. $contact=new Contact($this->db);
  2779. $contact->fetch($selected);
  2780. print $contact->getFullName($langs);
  2781. } else {
  2782. print "&nbsp;";
  2783. }
  2784. }
  2785. }
  2786. /**
  2787. * Output html select to select thirdparty
  2788. *
  2789. * @param string $page Page
  2790. * @param string $selected Id preselected
  2791. * @param string $htmlname Name of HTML select
  2792. * @param string $filter Optionnal filters criteras
  2793. * @param int $showempty Add an empty field
  2794. * @param int $showtype Show third party type in combolist (customer, prospect or supplier)
  2795. * @param int $forcecombo Force to use combo box
  2796. * @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
  2797. * @return void
  2798. */
  2799. function form_thirdparty($page, $selected='', $htmlname='socid', $filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array())
  2800. {
  2801. global $langs;
  2802. if ($htmlname != "none")
  2803. {
  2804. print '<form method="post" action="'.$page.'">';
  2805. print '<input type="hidden" name="action" value="set_thirdparty">';
  2806. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2807. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2808. print '<tr><td>';
  2809. print $this->select_company($selected, $htmlname, $filter, $showempty, $showtype, $forcecombo, $event);
  2810. print '</td>';
  2811. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  2812. print '</tr></table></form>';
  2813. }
  2814. else
  2815. {
  2816. if ($selected)
  2817. {
  2818. require_once DOL_DOCUMENT_ROOT .'/societe/class/societe.class.php';
  2819. $soc = new Societe($this->db);
  2820. $soc->fetch($selected);
  2821. print $soc->getNomUrl($langs);
  2822. }
  2823. else
  2824. {
  2825. print "&nbsp;";
  2826. }
  2827. }
  2828. }
  2829. /**
  2830. * Retourne la liste des devises, dans la langue de l'utilisateur
  2831. *
  2832. * @param string $selected preselected currency code
  2833. * @param string $htmlname name of HTML select list
  2834. * @return void
  2835. */
  2836. function select_currency($selected='',$htmlname='currency_id')
  2837. {
  2838. print $this->selectcurrency($selected,$htmlname);
  2839. }
  2840. /**
  2841. * Retourne la liste des devises, dans la langue de l'utilisateur
  2842. *
  2843. * @param string $selected preselected currency code
  2844. * @param string $htmlname name of HTML select list
  2845. * @return void
  2846. */
  2847. function selectcurrency($selected='',$htmlname='currency_id')
  2848. {
  2849. global $conf,$langs,$user;
  2850. $langs->load_cache_currencies('');
  2851. $out='';
  2852. if ($selected=='euro' || $selected=='euros') $selected='EUR'; // Pour compatibilite
  2853. $out.= '<select class="flat" name="'.$htmlname.'">';
  2854. foreach ($langs->cache_currencies as $code_iso => $currency)
  2855. {
  2856. if ($selected && $selected == $code_iso)
  2857. {
  2858. $out.= '<option value="'.$code_iso.'" selected="selected">';
  2859. }
  2860. else
  2861. {
  2862. $out.= '<option value="'.$code_iso.'">';
  2863. }
  2864. $out.= $currency['label'];
  2865. $out.= ' ('.$langs->getCurrencySymbol($code_iso).')';
  2866. $out.= '</option>';
  2867. }
  2868. $out.= '</select>';
  2869. if ($user->admin) $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  2870. return $out;
  2871. }
  2872. /**
  2873. * Load into the cache vat rates of a country
  2874. *
  2875. * @param string $country_code Country code
  2876. * @return int Nb of loaded lines, 0 if already loaded, <0 if KO
  2877. */
  2878. function load_cache_vatrates($country_code)
  2879. {
  2880. global $langs;
  2881. $num = count($this->cache_vatrates);
  2882. if ($num > 0) return $num; // Cache deja charge
  2883. $sql = "SELECT DISTINCT t.taux, t.recuperableonly";
  2884. $sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_pays as p";
  2885. $sql.= " WHERE t.fk_pays = p.rowid";
  2886. $sql.= " AND t.active = 1";
  2887. $sql.= " AND p.code IN (".$country_code.")";
  2888. $sql.= " ORDER BY t.taux ASC, t.recuperableonly ASC";
  2889. $resql=$this->db->query($sql);
  2890. if ($resql)
  2891. {
  2892. $num = $this->db->num_rows($resql);
  2893. if ($num)
  2894. {
  2895. for ($i = 0; $i < $num; $i++)
  2896. {
  2897. $obj = $this->db->fetch_object($resql);
  2898. $this->cache_vatrates[$i]['txtva'] = $obj->taux;
  2899. $this->cache_vatrates[$i]['libtva'] = $obj->taux.'%';
  2900. $this->cache_vatrates[$i]['nprtva'] = $obj->recuperableonly;
  2901. }
  2902. return $num;
  2903. }
  2904. else
  2905. {
  2906. $this->error = '<font class="error">'.$langs->trans("ErrorNoVATRateDefinedForSellerCountry",$country_code).'</font>';
  2907. return -1;
  2908. }
  2909. }
  2910. else
  2911. {
  2912. $this->error = '<font class="error">'.$this->db->error().'</font>';
  2913. return -2;
  2914. }
  2915. }
  2916. /**
  2917. * Output an HTML select vat rate
  2918. *
  2919. * @param string $htmlname Nom champ html
  2920. * @param float $selectedrate Forcage du taux tva pre-selectionne. Mettre '' pour aucun forcage.
  2921. * @param Societe $societe_vendeuse Objet societe vendeuse
  2922. * @param Societe $societe_acheteuse Objet societe acheteuse
  2923. * @param int $idprod Id product
  2924. * @param int $info_bits Miscellaneous information on line (1 for NPR)
  2925. * @param int $type ''=Unknown, 0=Product, 1=Service (Used if idprod not defined)
  2926. * Si vendeur non assujeti a TVA, TVA par defaut=0. Fin de regle.
  2927. * Si le (pays vendeur = pays acheteur) alors la TVA par defaut=TVA du produit vendu. Fin de regle.
  2928. * Si (vendeur et acheteur dans Communaute europeenne) et bien vendu = moyen de transports neuf (auto, bateau, avion), TVA par defaut=0 (La TVA doit etre paye par l'acheteur au centre d'impots de son pays et non au vendeur). Fin de regle.
  2929. * Si (vendeur et acheteur dans Communaute europeenne) et bien vendu autre que transport neuf alors la TVA par defaut=TVA du produit vendu. Fin de regle.
  2930. * Sinon la TVA proposee par defaut=0. Fin de regle.
  2931. * @param bool $options_only Return options only (for ajax treatment)
  2932. * @return void
  2933. */
  2934. function load_tva($htmlname='tauxtva', $selectedrate='', $societe_vendeuse='', $societe_acheteuse='', $idprod=0, $info_bits=0, $type='', $options_only=false)
  2935. {
  2936. global $langs,$conf,$mysoc;
  2937. $return='';
  2938. $txtva=array();
  2939. $libtva=array();
  2940. $nprtva=array();
  2941. // Define defaultnpr and defaultttx
  2942. $defaultnpr=($info_bits & 0x01);
  2943. $defaultnpr=(preg_match('/\*/',$selectedrate) ? 1 : $defaultnpr);
  2944. $defaulttx=str_replace('*','',$selectedrate);
  2945. // Check parameters
  2946. if (is_object($societe_vendeuse) && ! $societe_vendeuse->country_code)
  2947. {
  2948. if ($societe_vendeuse->id == $mysoc->id)
  2949. {
  2950. $return.= '<font class="error">'.$langs->trans("ErrorYourCountryIsNotDefined").'</div>';
  2951. }
  2952. else
  2953. {
  2954. $return.= '<font class="error">'.$langs->trans("ErrorSupplierCountryIsNotDefined").'</div>';
  2955. }
  2956. return $return;
  2957. }
  2958. //var_dump($societe_acheteuse);
  2959. //print "name=$name, selectedrate=$selectedrate, seller=".$societe_vendeuse->country_code." buyer=".$societe_acheteuse->country_code." buyer is company=".$societe_acheteuse->isACompany()." idprod=$idprod, info_bits=$info_bits type=$type";
  2960. //exit;
  2961. // Define list of countries to use to search VAT rates to show
  2962. // First we defined code_pays to use to find list
  2963. if (is_object($societe_vendeuse))
  2964. {
  2965. $code_pays="'".$societe_vendeuse->country_code."'";
  2966. }
  2967. else
  2968. {
  2969. $code_pays="'".$mysoc->country_code."'"; // Pour compatibilite ascendente
  2970. }
  2971. if (! empty($conf->global->SERVICE_ARE_ECOMMERCE_200238EC)) // If option to have vat for end customer for services is on
  2972. {
  2973. if (! $societe_vendeuse->isInEEC() && (! is_object($societe_acheteuse) || ($societe_acheteuse->isInEEC() && ! $societe_acheteuse->isACompany())))
  2974. {
  2975. // We also add the buyer
  2976. if (is_numeric($type))
  2977. {
  2978. if ($type == 1) // We know product is a service
  2979. {
  2980. $code_pays.=",'".$societe_acheteuse->country_code."'";
  2981. }
  2982. }
  2983. else if (! $idprod) // We don't know type of product
  2984. {
  2985. $code_pays.=",'".$societe_acheteuse->country_code."'";
  2986. }
  2987. else
  2988. {
  2989. $prodstatic=new Product($this->db);
  2990. $prodstatic->fetch($idprod);
  2991. if ($prodstatic->type == 1) // We know product is a service
  2992. {
  2993. $code_pays.=",'".$societe_acheteuse->country_code."'";
  2994. }
  2995. }
  2996. }
  2997. }
  2998. // Now we get list
  2999. $num = $this->load_cache_vatrates($code_pays);
  3000. if ($num > 0)
  3001. {
  3002. // Definition du taux a pre-selectionner (si defaulttx non force et donc vaut -1 ou '')
  3003. if ($defaulttx < 0 || dol_strlen($defaulttx) == 0)
  3004. {
  3005. $defaulttx=get_default_tva($societe_vendeuse,$societe_acheteuse,$idprod);
  3006. $defaultnpr=get_default_npr($societe_vendeuse,$societe_acheteuse,$idprod);
  3007. }
  3008. // Si taux par defaut n'a pu etre determine, on prend dernier de la liste.
  3009. // Comme ils sont tries par ordre croissant, dernier = plus eleve = taux courant
  3010. if ($defaulttx < 0 || dol_strlen($defaulttx) == 0)
  3011. {
  3012. $defaulttx = $this->cache_vatrates[$num-1]['txtva'];
  3013. }
  3014. // Disabled if seller is not subject to VAT
  3015. $disabled=false; $title='';
  3016. if (is_object($societe_vendeuse) && $societe_vendeuse->id == $mysoc->id && $societe_vendeuse->tva_assuj == "0") {
  3017. $title=' title="'.$langs->trans('VATIsNotUsed').'"';
  3018. $disabled=true;
  3019. }
  3020. if (! $options_only) $return.= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($disabled?' disabled="disabled"':'').$title.'>';
  3021. foreach ($this->cache_vatrates as $rate)
  3022. {
  3023. // Keep only 0 if seller is not subject to VAT
  3024. if ($disabled && $rate['txtva'] != 0) continue;
  3025. $return.= '<option value="'.$rate['txtva'];
  3026. $return.= $rate['nprtva'] ? '*': '';
  3027. $return.= '"';
  3028. if ($rate['txtva'] == $defaulttx && $rate['nprtva'] == $defaultnpr)
  3029. {
  3030. $return.= ' selected="selected"';
  3031. }
  3032. $return.= '>'.vatrate($rate['libtva']);
  3033. $return.= $rate['nprtva'] ? ' *': '';
  3034. $return.= '</option>';
  3035. $this->tva_taux_value[] = $rate['txtva'];
  3036. $this->tva_taux_libelle[] = $rate['libtva'];
  3037. $this->tva_taux_npr[] = $rate['nprtva'];
  3038. }
  3039. if (! $options_only) $return.= '</select>';
  3040. }
  3041. else
  3042. {
  3043. $return.= $this->error;
  3044. }
  3045. $this->num = $num;
  3046. return $return;
  3047. }
  3048. /**
  3049. * Show a HTML widget to input a date or combo list for day, month, years and optionnaly hours and minutes
  3050. * Fields are preselected with :
  3051. * - set_time date (Local PHP server timestamps or date format YYYY-MM-DD or YYYY-MM-DD HH:MM)
  3052. * - local date of PHP server if set_time is ''
  3053. * - Empty (fields empty) if set_time is -1 (in this case, parameter empty must also have value 1)
  3054. *
  3055. * @param timestamp $set_time Pre-selected date (must be a local PHP server timestamp)
  3056. * @param string $prefix Prefix for fields name
  3057. * @param int $h 1=Show also hours
  3058. * @param int $m 1=Show also minutes
  3059. * @param int $empty 0=Fields required, 1=Empty input is allowed
  3060. * @param string $form_name Not used
  3061. * @param int $d 1=Show days, month, years
  3062. * @param int $addnowbutton Add a button "Now"
  3063. * @param int $nooutput Do not output html string but return it
  3064. * @param int $disabled Disable input fields
  3065. * @param int $fullday When a checkbox with this html name is on, hour and day are set with 00:00 or 23:59
  3066. * @return mixed Nothing or string if nooutput is 1
  3067. */
  3068. function select_date($set_time='', $prefix='re', $h=0, $m=0, $empty=0, $form_name="", $d=1, $addnowbutton=0, $nooutput=0, $disabled=0, $fullday='')
  3069. {
  3070. global $conf,$langs;
  3071. $retstring='';
  3072. if($prefix=='') $prefix='re';
  3073. if($h == '') $h=0;
  3074. if($m == '') $m=0;
  3075. if($empty == '') $empty=0;
  3076. if (! $set_time && $empty == 0) $set_time = dol_now('tzuser');
  3077. // Analysis of the pre-selection date
  3078. if (preg_match('/^([0-9]+)\-([0-9]+)\-([0-9]+)\s?([0-9]+)?:?([0-9]+)?/',$set_time,$reg))
  3079. {
  3080. // Date format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'
  3081. $syear = (! empty($reg[1])?$reg[1]:'');
  3082. $smonth = (! empty($reg[2])?$reg[2]:'');
  3083. $sday = (! empty($reg[3])?$reg[3]:'');
  3084. $shour = (! empty($reg[4])?$reg[4]:'');
  3085. $smin = (! empty($reg[5])?$reg[5]:'');
  3086. }
  3087. elseif (strval($set_time) != '' && $set_time != -1)
  3088. {
  3089. // set_time est un timestamps (0 possible)
  3090. $syear = dol_print_date($set_time, "%Y");
  3091. $smonth = dol_print_date($set_time, "%m");
  3092. $sday = dol_print_date($set_time, "%d");
  3093. $shour = dol_print_date($set_time, "%H");
  3094. $smin = dol_print_date($set_time, "%M");
  3095. }
  3096. else
  3097. {
  3098. // Date est '' ou vaut -1
  3099. $syear = '';
  3100. $smonth = '';
  3101. $sday = '';
  3102. $shour = '';
  3103. $smin = '';
  3104. }
  3105. $usecalendar='combo';
  3106. if (! empty($conf->use_javascript_ajax) && (empty($conf->global->MAIN_POPUP_CALENDAR) || $conf->global->MAIN_POPUP_CALENDAR != "none")) $usecalendar=empty($conf->global->MAIN_POPUP_CALENDAR)?'eldy':$conf->global->MAIN_POPUP_CALENDAR;
  3107. if ($conf->browser->phone) $usecalendar='combo';
  3108. if ($d)
  3109. {
  3110. // Show date with popup
  3111. if ($usecalendar != 'combo')
  3112. {
  3113. $formated_date='';
  3114. //print "e".$set_time." t ".$conf->format_date_short;
  3115. if (strval($set_time) != '' && $set_time != -1)
  3116. {
  3117. //$formated_date=dol_print_date($set_time,$conf->format_date_short);
  3118. $formated_date=dol_print_date($set_time,$langs->trans("FormatDateShortInput")); // FormatDateShortInput for dol_print_date / FormatDateShortJavaInput that is same for javascript
  3119. }
  3120. // Calendrier popup version eldy
  3121. if ($usecalendar == "eldy")
  3122. {
  3123. // Zone de saisie manuelle de la date
  3124. $retstring.='<input id="'.$prefix.'" name="'.$prefix.'" type="text" size="9" maxlength="11" value="'.$formated_date.'"';
  3125. $retstring.=($disabled?' disabled="disabled"':'');
  3126. $retstring.=' onChange="dpChangeDay(\''.$prefix.'\',\''.$langs->trans("FormatDateShortJavaInput").'\'); "'; // FormatDateShortInput for dol_print_date / FormatDateShortJavaInput that is same for javascript
  3127. $retstring.='>';
  3128. // Icone calendrier
  3129. if (! $disabled)
  3130. {
  3131. $retstring.='<button id="'.$prefix.'Button" type="button" class="dpInvisibleButtons"';
  3132. $base=DOL_URL_ROOT.'/core/';
  3133. $retstring.=' onClick="showDP(\''.$base.'\',\''.$prefix.'\',\''.$langs->trans("FormatDateShortJavaInput").'\',\''.$langs->defaultlang.'\');">'.img_object($langs->trans("SelectDate"),'calendarday','class="datecallink"').'</button>';
  3134. }
  3135. else $retstring.='<button id="'.$prefix.'Button" type="button" class="dpInvisibleButtons">'.img_object($langs->trans("Disabled"),'calendarday','class="datecallink"').'</button>';
  3136. $retstring.='<input type="hidden" id="'.$prefix.'day" name="'.$prefix.'day" value="'.$sday.'">'."\n";
  3137. $retstring.='<input type="hidden" id="'.$prefix.'month" name="'.$prefix.'month" value="'.$smonth.'">'."\n";
  3138. $retstring.='<input type="hidden" id="'.$prefix.'year" name="'.$prefix.'year" value="'.$syear.'">'."\n";
  3139. }
  3140. else
  3141. {
  3142. print "Bad value of MAIN_POPUP_CALENDAR";
  3143. }
  3144. }
  3145. // Show date with combo selects
  3146. else
  3147. {
  3148. // Day
  3149. $retstring.='<select'.($disabled?' disabled="disabled"':'').' class="flat" name="'.$prefix.'day">';
  3150. if ($empty || $set_time == -1)
  3151. {
  3152. $retstring.='<option value="0" selected="selected">&nbsp;</option>';
  3153. }
  3154. for ($day = 1 ; $day <= 31; $day++)
  3155. {
  3156. $retstring.='<option value="'.$day.'"'.($day == $sday ? ' selected="selected"':'').'>'.$day.'</option>';
  3157. }
  3158. $retstring.="</select>";
  3159. $retstring.='<select'.($disabled?' disabled="disabled"':'').' class="flat" name="'.$prefix.'month">';
  3160. if ($empty || $set_time == -1)
  3161. {
  3162. $retstring.='<option value="0" selected="selected">&nbsp;</option>';
  3163. }
  3164. // Month
  3165. for ($month = 1 ; $month <= 12 ; $month++)
  3166. {
  3167. $retstring.='<option value="'.$month.'"'.($month == $smonth?' selected="selected"':'').'>';
  3168. $retstring.=dol_print_date(mktime(12,0,0,$month,1,2000),"%b");
  3169. $retstring.="</option>";
  3170. }
  3171. $retstring.="</select>";
  3172. // Year
  3173. if ($empty || $set_time == -1)
  3174. {
  3175. $retstring.='<input'.($disabled?' disabled="disabled"':'').' class="flat" type="text" size="3" maxlength="4" name="'.$prefix.'year" value="'.$syear.'">';
  3176. }
  3177. else
  3178. {
  3179. $retstring.='<select'.($disabled?' disabled="disabled"':'').' class="flat" name="'.$prefix.'year">';
  3180. for ($year = $syear - 5; $year < $syear + 10 ; $year++)
  3181. {
  3182. $retstring.='<option value="'.$year.'"'.($year == $syear ? ' selected="true"':'').'>'.$year.'</option>';
  3183. }
  3184. $retstring.="</select>\n";
  3185. }
  3186. }
  3187. }
  3188. if ($d && $h) $retstring.='&nbsp;';
  3189. if ($h)
  3190. {
  3191. // Show hour
  3192. $retstring.='<select'.($disabled?' disabled="disabled"':'').' class="flat '.($fullday?$fullday.'hour':'').'" name="'.$prefix.'hour">';
  3193. if ($empty) $retstring.='<option value="-1">&nbsp;</option>';
  3194. for ($hour = 0; $hour < 24; $hour++)
  3195. {
  3196. if (strlen($hour) < 2) $hour = "0" . $hour;
  3197. $retstring.='<option value="'.$hour.'"'.(($hour == $shour)?' selected="true"':'').'>'.$hour.(empty($conf->dol_optimize_smallscreen)?'':'H').'</option>';
  3198. }
  3199. $retstring.='</select>';
  3200. if (empty($conf->dol_optimize_smallscreen)) $retstring.=":";
  3201. }
  3202. if ($m)
  3203. {
  3204. // Show minutes
  3205. $retstring.='<select'.($disabled?' disabled="disabled"':'').' class="flat '.($fullday?$fullday.'min':'').'" name="'.$prefix.'min">';
  3206. if ($empty) $retstring.='<option value="-1">&nbsp;</option>';
  3207. for ($min = 0; $min < 60 ; $min++)
  3208. {
  3209. if (strlen($min) < 2) $min = "0" . $min;
  3210. $retstring.='<option value="'.$min.'"'.(($min == $smin)?' selected="true"':'').'>'.$min.(empty($conf->dol_optimize_smallscreen)?'':'').'</option>';
  3211. }
  3212. $retstring.='</select>';
  3213. }
  3214. // Add a "Now" button
  3215. if ($conf->use_javascript_ajax && $addnowbutton)
  3216. {
  3217. // Script which will be inserted in the OnClick of the "Now" button
  3218. $reset_scripts = "";
  3219. // Generate the date part, depending on the use or not of the javascript calendar
  3220. if ($usecalendar == "eldy")
  3221. {
  3222. $base=DOL_URL_ROOT.'/core/';
  3223. $reset_scripts .= 'resetDP(\''.$base.'\',\''.$prefix.'\',\''.$langs->trans("FormatDateShortJavaInput").'\',\''.$langs->defaultlang.'\');';
  3224. }
  3225. else
  3226. {
  3227. $reset_scripts .= 'this.form.elements[\''.$prefix.'day\'].value=formatDate(new Date(), \'d\'); ';
  3228. $reset_scripts .= 'this.form.elements[\''.$prefix.'month\'].value=formatDate(new Date(), \'M\'); ';
  3229. $reset_scripts .= 'this.form.elements[\''.$prefix.'year\'].value=formatDate(new Date(), \'yyyy\'); ';
  3230. }
  3231. // Generate the hour part
  3232. if ($h)
  3233. {
  3234. if ($fullday) $reset_scripts .= " if (jQuery('#fullday:checked').val() == null) {";
  3235. $reset_scripts .= 'this.form.elements[\''.$prefix.'hour\'].value=formatDate(new Date(), \'HH\'); ';
  3236. if ($fullday) $reset_scripts .= ' } ';
  3237. }
  3238. // Generate the minute part
  3239. if ($m)
  3240. {
  3241. if ($fullday) $reset_scripts .= " if (jQuery('#fullday:checked').val() == null) {";
  3242. $reset_scripts .= 'this.form.elements[\''.$prefix.'min\'].value=formatDate(new Date(), \'mm\'); ';
  3243. if ($fullday) $reset_scripts .= ' } ';
  3244. }
  3245. // If reset_scripts is not empty, print the button with the reset_scripts in OnClick
  3246. if ($reset_scripts && empty($conf->dol_optimize_smallscreen))
  3247. {
  3248. $retstring.=' <button class="dpInvisibleButtons datenowlink" id="'.$prefix.'ButtonNow" type="button" name="_useless" value="Now" onClick="'.$reset_scripts.'">';
  3249. $retstring.=$langs->trans("Now");
  3250. $retstring.='</button> ';
  3251. }
  3252. }
  3253. if (! empty($nooutput)) return $retstring;
  3254. print $retstring;
  3255. return;
  3256. }
  3257. /**
  3258. * Function to show a form to select a duration on a page
  3259. *
  3260. * @param string $prefix prefix
  3261. * @param int $iSecond Default preselected duration (number of seconds)
  3262. * @param int $disabled Disable the combo box
  3263. * @param string $typehour if select then hour in select if text input in text
  3264. * @return void
  3265. */
  3266. function select_duration($prefix,$iSecond='',$disabled=0,$typehour='select')
  3267. {
  3268. global $langs;
  3269. if ($iSecond)
  3270. {
  3271. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  3272. $hourSelected = convertSecondToTime($iSecond,'hour');
  3273. $minSelected = convertSecondToTime($iSecond,'min');
  3274. }
  3275. if ($typehour=='select')
  3276. {
  3277. print '<select class="flat" name="'.$prefix.'hour"'.($disabled?' disabled="disabled"':'').'>';
  3278. for ($hour = 0; $hour < 24; $hour++)
  3279. {
  3280. print '<option value="'.$hour.'"';
  3281. if ($hourSelected == $hour)
  3282. {
  3283. print " selected=\"true\"";
  3284. }
  3285. print ">".$hour."</option>";
  3286. }
  3287. print "</select>";
  3288. }
  3289. elseif ($typehour=='text')
  3290. {
  3291. $fullhours=convertSecondToTime($iSecond,'fullhour');
  3292. print '<input type="text" size="3" name="'.$prefix.'hour" class="flat" value="'.$fullhours.'">';
  3293. }
  3294. print $langs->trans('Hours'). "&nbsp;";
  3295. print '<select class="flat" name="'.$prefix.'min"'.($disabled?' disabled="disabled"':'').'>';
  3296. for ($min = 0; $min <= 55; $min=$min+5)
  3297. {
  3298. print '<option value="'.$min.'"';
  3299. if ($minSelected == $min) print ' selected="selected"';
  3300. print '>'.$min.'</option>';
  3301. }
  3302. print "</select>";
  3303. print $langs->trans('Minutes'). "&nbsp;";
  3304. }
  3305. /**
  3306. * Show a select form from an array
  3307. *
  3308. * @param string $htmlname Name of html select area
  3309. * @param array $array Array with key+value
  3310. * @param int $id Preselected key
  3311. * @param int $show_empty 1 si il faut ajouter une valeur vide dans la liste, 0 sinon
  3312. * @param int $key_in_label 1 pour afficher la key dans la valeur "[key] value"
  3313. * @param int $value_as_key 1 to use value as key
  3314. * @param string $option Valeur de l'option en fonction du type choisi
  3315. * @param int $translate Translate and encode value
  3316. * @param int $maxlen Length maximum for labels
  3317. * @param int $disabled Html select box is disabled
  3318. * @return string HTML select string
  3319. */
  3320. function selectarray($htmlname, $array, $id='', $show_empty=0, $key_in_label=0, $value_as_key=0, $option='', $translate=0, $maxlen=0, $disabled=0)
  3321. {
  3322. global $langs;
  3323. if ($value_as_key) $array=array_combine($array, $array);
  3324. $out='<select id="'.$htmlname.'" '.($disabled?'disabled="disabled" ':'').'class="flat" name="'.$htmlname.'" '.($option != ''?$option:'').'>';
  3325. if ($show_empty)
  3326. {
  3327. $out.='<option value="-1"'.($id==-1?' selected="selected"':'').'>&nbsp;</option>'."\n";
  3328. }
  3329. if (is_array($array))
  3330. {
  3331. foreach($array as $key => $value)
  3332. {
  3333. $out.='<option value="'.$key.'"';
  3334. // Si il faut pre-selectionner une valeur
  3335. if ($id != '' && $id == $key)
  3336. {
  3337. $out.=' selected="selected"';
  3338. }
  3339. $out.='>';
  3340. $newval=($translate?$langs->trans(ucfirst($value)):$value);
  3341. if ($key_in_label)
  3342. {
  3343. $selectOptionValue = dol_htmlentitiesbr($key.' - '.($maxlen?dol_trunc($newval,$maxlen):$newval));
  3344. }
  3345. else
  3346. {
  3347. $selectOptionValue = dol_htmlentitiesbr($maxlen?dol_trunc($newval,$maxlen):$newval);
  3348. if ($value == '' || $value == '-') {
  3349. $selectOptionValue='&nbsp;';
  3350. }
  3351. }
  3352. $out.=$selectOptionValue;
  3353. $out.="</option>\n";
  3354. }
  3355. }
  3356. $out.="</select>";
  3357. return $out;
  3358. }
  3359. /**
  3360. * Show a multiselect form from an array.
  3361. *
  3362. * @param string $htmlname Name of select
  3363. * @param array $array Array with key+value
  3364. * @param array $selected Preselected keys
  3365. * @param int $key_in_label 1 pour afficher la key dans la valeur "[key] value"
  3366. * @param int $value_as_key 1 to use value as key
  3367. * @param string $option Valeur de l'option en fonction du type choisi
  3368. * @param int $translate Translate and encode value
  3369. * @return string HTML multiselect string
  3370. */
  3371. function multiselectarray($htmlname, $array, $selected=array(), $key_in_label=0, $value_as_key=0, $option='', $translate=0)
  3372. {
  3373. global $conf, $langs;
  3374. $out = '<select id="'.$htmlname.'" class="multiselect" multiple="multiple" name="'.$htmlname.'[]"'.$option.'>'."\n";
  3375. if (is_array($array) && ! empty($array))
  3376. {
  3377. if ($value_as_key) $array=array_combine($array, $array);
  3378. if (! empty($conf->global->MAIN_USE_JQUERY_MULTISELECT) && is_array($selected) && ! empty($selected))
  3379. {
  3380. foreach ($selected as $selected_value)
  3381. {
  3382. foreach($array as $key => $value)
  3383. {
  3384. if ($selected_value == $key)
  3385. {
  3386. $value=$array[$selected_value];
  3387. $out.= '<option value="'.$key.'" selected="selected">';
  3388. $newval = ($translate ? $langs->trans(ucfirst($value)) : $value);
  3389. $newval = ($key_in_label ? $key.' - '.$newval : $newval);
  3390. $out.= dol_htmlentitiesbr($newval);
  3391. $out.= '</option>'."\n";
  3392. unset($array[$key]);
  3393. }
  3394. }
  3395. }
  3396. if (! empty($array))
  3397. {
  3398. foreach ($array as $key => $value)
  3399. {
  3400. $out.= '<option value="'.$key.'">';
  3401. $newval = ($translate ? $langs->trans(ucfirst($value)) : $value);
  3402. $newval = ($key_in_label ? $key.' - '.$newval : $newval);
  3403. $out.= dol_htmlentitiesbr($newval);
  3404. $out.= '</option>'."\n";
  3405. }
  3406. }
  3407. }
  3408. else
  3409. {
  3410. foreach ($array as $key => $value)
  3411. {
  3412. $out.= '<option value="'.$key.'"';
  3413. if (is_array($selected) && ! empty($selected) && in_array($key, $selected))
  3414. {
  3415. $out.= ' selected="selected"';
  3416. }
  3417. $out.= '>';
  3418. $newval = ($translate ? $langs->trans(ucfirst($value)) : $value);
  3419. $newval = ($key_in_label ? $key.' - '.$newval : $newval);
  3420. $out.= dol_htmlentitiesbr($newval);
  3421. $out.= '</option>'."\n";
  3422. }
  3423. }
  3424. }
  3425. $out.= '</select>'."\n";
  3426. return $out;
  3427. }
  3428. /**
  3429. * Return an html string with a select combo box to choose yes or no
  3430. *
  3431. * @param string $htmlname Name of html select field
  3432. * @param string $value Pre-selected value
  3433. * @param int $option 0 return yes/no, 1 return 1/0
  3434. * @param bool $disabled true or false
  3435. * @return mixed See option
  3436. */
  3437. function selectyesno($htmlname,$value='',$option=0,$disabled=false)
  3438. {
  3439. global $langs;
  3440. $yes="yes"; $no="no";
  3441. if ($option)
  3442. {
  3443. $yes="1";
  3444. $no="0";
  3445. }
  3446. $disabled = ($disabled ? ' disabled="disabled"' : '');
  3447. $resultyesno = '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.$disabled.'>'."\n";
  3448. if (("$value" == 'yes') || ($value == 1))
  3449. {
  3450. $resultyesno .= '<option value="'.$yes.'" selected="selected">'.$langs->trans("Yes").'</option>'."\n";
  3451. $resultyesno .= '<option value="'.$no.'">'.$langs->trans("No").'</option>'."\n";
  3452. }
  3453. else
  3454. {
  3455. $resultyesno .= '<option value="'.$yes.'">'.$langs->trans("Yes").'</option>'."\n";
  3456. $resultyesno .= '<option value="'.$no.'" selected="selected">'.$langs->trans("No").'</option>'."\n";
  3457. }
  3458. $resultyesno .= '</select>'."\n";
  3459. return $resultyesno;
  3460. }
  3461. /**
  3462. * Return list of export templates
  3463. *
  3464. * @param string $selected Id modele pre-selectionne
  3465. * @param string $htmlname Name of HTML select
  3466. * @param string $type Type of searched templates
  3467. * @param int $useempty Affiche valeur vide dans liste
  3468. * @return void
  3469. */
  3470. function select_export_model($selected='',$htmlname='exportmodelid',$type='',$useempty=0)
  3471. {
  3472. $sql = "SELECT rowid, label";
  3473. $sql.= " FROM ".MAIN_DB_PREFIX."export_model";
  3474. $sql.= " WHERE type = '".$type."'";
  3475. $sql.= " ORDER BY rowid";
  3476. $result = $this->db->query($sql);
  3477. if ($result)
  3478. {
  3479. print '<select class="flat" name="'.$htmlname.'">';
  3480. if ($useempty)
  3481. {
  3482. print '<option value="-1">&nbsp;</option>';
  3483. }
  3484. $num = $this->db->num_rows($result);
  3485. $i = 0;
  3486. while ($i < $num)
  3487. {
  3488. $obj = $this->db->fetch_object($result);
  3489. if ($selected == $obj->rowid)
  3490. {
  3491. print '<option value="'.$obj->rowid.'" selected="selected">';
  3492. }
  3493. else
  3494. {
  3495. print '<option value="'.$obj->rowid.'">';
  3496. }
  3497. print $obj->label;
  3498. print '</option>';
  3499. $i++;
  3500. }
  3501. print "</select>";
  3502. }
  3503. else {
  3504. dol_print_error($this->db);
  3505. }
  3506. }
  3507. /**
  3508. * Return a HTML area with the reference of object and a navigation bar for a business object
  3509. * To add a particular filter on select, you must set $object->next_prev_filter to SQL criteria.
  3510. *
  3511. * @param Object $object Object to show
  3512. * @param string $paramid Name of parameter to use to name the id into the URL link
  3513. * @param string $morehtml More html content to output just before the nav bar
  3514. * @param int $shownav Show Condition (navigation is shown if value is 1)
  3515. * @param string $fieldid Nom du champ en base a utiliser pour select next et previous
  3516. * @param string $fieldref Nom du champ objet ref (object->ref) a utiliser pour select next et previous
  3517. * @param string $morehtmlref Code html supplementaire a afficher apres ref
  3518. * @param string $moreparam More param to add in nav link url.
  3519. * @return tring Portion HTML avec ref + boutons nav
  3520. */
  3521. function showrefnav($object,$paramid,$morehtml='',$shownav=1,$fieldid='rowid',$fieldref='ref',$morehtmlref='',$moreparam='')
  3522. {
  3523. global $langs,$conf;
  3524. $ret='';
  3525. if (empty($fieldid)) $fieldid='rowid';
  3526. if (empty($fieldref)) $fieldref='ref';
  3527. //print "paramid=$paramid,morehtml=$morehtml,shownav=$shownav,$fieldid,$fieldref,$morehtmlref,$moreparam";
  3528. $object->load_previous_next_ref((isset($object->next_prev_filter)?$object->next_prev_filter:''),$fieldid);
  3529. $previous_ref = $object->ref_previous?'<a data-role="button" data-icon="arrow-l" data-iconpos="left" href="'.$_SERVER["PHP_SELF"].'?'.$paramid.'='.urlencode($object->ref_previous).$moreparam.'">'.(empty($conf->dol_use_jmobile)?img_picto($langs->trans("Previous"),'previous.png'):'&nbsp;').'</a>':'';
  3530. $next_ref = $object->ref_next?'<a data-role="button" data-icon="arrow-r" data-iconpos="right" href="'.$_SERVER["PHP_SELF"].'?'.$paramid.'='.urlencode($object->ref_next).$moreparam.'">'.(empty($conf->dol_use_jmobile)?img_picto($langs->trans("Next"),'next.png'):'&nbsp;').'</a>':'';
  3531. //print "xx".$previous_ref."x".$next_ref;
  3532. if ($previous_ref || $next_ref || $morehtml) {
  3533. $ret.='<table class="nobordernopadding" width="100%"><tr class="nobordernopadding"><td class="nobordernopadding">';
  3534. }
  3535. $ret.=$object->$fieldref;
  3536. if ($morehtmlref)
  3537. {
  3538. $ret.=' '.$morehtmlref;
  3539. }
  3540. if ($morehtml)
  3541. {
  3542. $ret.='</td><td class="nobordernopadding" align="right">'.$morehtml;
  3543. }
  3544. if ($shownav && ($previous_ref || $next_ref))
  3545. {
  3546. $ret.='</td><td class="nobordernopadding" align="center" width="20">'.$previous_ref.'</td>';
  3547. $ret.='<td class="nobordernopadding" align="center" width="20">'.$next_ref;
  3548. }
  3549. if ($previous_ref || $next_ref || $morehtml)
  3550. {
  3551. $ret.='</td></tr></table>';
  3552. }
  3553. return $ret;
  3554. }
  3555. /**
  3556. * Return HTML code to output a barcode
  3557. *
  3558. * @param Object &$object Object containing data to retrieve file name
  3559. * @param int $width Width of photo
  3560. * @return string HTML code to output barcode
  3561. */
  3562. function showbarcode(&$object,$width=100)
  3563. {
  3564. global $conf;
  3565. //Check if barcode is filled in the card
  3566. if (empty($object->barcode)) return '';
  3567. // Complete object if not complete
  3568. if (empty($object->barcode_type_code) || empty($object->barcode_type_coder))
  3569. {
  3570. $result = $object->fetch_barcode();
  3571. //Check if fetch_barcode() failed
  3572. if ($result < 1) return '<!-- ErrorFetchBarcode -->';
  3573. }
  3574. // Barcode image
  3575. $url=DOL_URL_ROOT.'/viewimage.php?modulepart=barcode&generator='.urlencode($object->barcode_type_coder).'&code='.urlencode($object->barcode).'&encoding='.urlencode($object->barcode_type_code);
  3576. $out ='<!-- url barcode = '.$url.' -->';
  3577. $out.='<img src="'.$url.'">';
  3578. return $out;
  3579. }
  3580. /**
  3581. * Return HTML code to output a photo
  3582. *
  3583. * @param string $modulepart Key to define module concerned ('societe', 'userphoto', 'memberphoto')
  3584. * @param Object $object Object containing data to retrieve file name
  3585. * @param int $width Width of photo
  3586. * @return string HTML code to output photo
  3587. */
  3588. function showphoto($modulepart,$object,$width=100)
  3589. {
  3590. global $conf;
  3591. $entity = (! empty($object->entity) ? $object->entity : $conf->entity);
  3592. $id = (! empty($object->id) ? $object->id : $object->rowid);
  3593. $ret='';$dir='';$file='';$altfile='';$email='';
  3594. if ($modulepart=='societe')
  3595. {
  3596. $dir=$conf->societe->multidir_output[$entity];
  3597. $smallfile=$object->logo;
  3598. $smallfile=preg_replace('/(\.png|\.gif|\.jpg|\.jpeg|\.bmp)/i','_small\\1',$smallfile);
  3599. if ($object->logo) $file=$id.'/logos/thumbs/'.$smallfile;
  3600. }
  3601. else if ($modulepart=='userphoto')
  3602. {
  3603. $dir=$conf->user->dir_output;
  3604. if ($object->photo) $file=get_exdir($id, 2).$object->photo;
  3605. if (! empty($conf->global->MAIN_OLD_IMAGE_LINKS)) $altfile=$object->id.".jpg"; // For backward compatibility
  3606. $email=$object->email;
  3607. }
  3608. else if ($modulepart=='memberphoto')
  3609. {
  3610. $dir=$conf->adherent->dir_output;
  3611. if ($object->photo) $file=get_exdir($id, 2).'photos/'.$object->photo;
  3612. if (! empty($conf->global->MAIN_OLD_IMAGE_LINKS)) $altfile=$object->id.".jpg"; // For backward compatibility
  3613. $email=$object->email;
  3614. }else {
  3615. $dir=$conf->$modulepart->dir_output;
  3616. if ($object->photo) $file=get_exdir($id, 2).'photos/'.$object->photo;
  3617. if (! empty($conf->global->MAIN_OLD_IMAGE_LINKS)) $altfile=$object->id.".jpg"; // For backward compatibility
  3618. $email=$object->email;
  3619. }
  3620. if ($dir)
  3621. {
  3622. $cache='0';
  3623. if ($file && file_exists($dir."/".$file))
  3624. {
  3625. // TODO Link to large image
  3626. $ret.='<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$entity.'&file='.urlencode($file).'&cache='.$cache.'">';
  3627. $ret.='<img alt="Photo" id="photologo'.(preg_replace('/[^a-z]/i','_',$file)).'" class="photologo" border="0" width="'.$width.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$entity.'&file='.urlencode($file).'&cache='.$cache.'">';
  3628. $ret.='</a>';
  3629. }
  3630. else if ($altfile && file_exists($dir."/".$altfile))
  3631. {
  3632. $ret.='<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$entity.'&file='.urlencode($file).'&cache='.$cache.'">';
  3633. $ret.='<img alt="Photo alt" id="photologo'.(preg_replace('/[^a-z]/i','_',$file)).'" class="photologo" border="0" width="'.$width.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$entity.'&file='.urlencode($altfile).'&cache='.$cache.'">';
  3634. $ret.='</a>';
  3635. }
  3636. else
  3637. {
  3638. if (! empty($conf->gravatar->enabled) && $email)
  3639. {
  3640. global $dolibarr_main_url_root;
  3641. $ret.='<!-- Put link to gravatar -->';
  3642. $ret.='<img alt="Photo found on Gravatar" title="Photo Gravatar.com - email '.$email.'" border="0" width="'.$width.'" src="http://www.gravatar.com/avatar/'.dol_hash($email).'?s='.$width.'&d='.urlencode(dol_buildpath('/theme/common/nophoto.jpg',2)).'">';
  3643. }
  3644. else
  3645. {
  3646. $ret.='<img alt="No photo" border="0" width="'.$width.'" src="'.DOL_URL_ROOT.'/theme/common/nophoto.jpg">';
  3647. }
  3648. }
  3649. }
  3650. else dol_print_error('','Call of showphoto with wrong parameters');
  3651. return $ret;
  3652. }
  3653. /**
  3654. * Return select list of groups
  3655. *
  3656. * @param string $selected Id group preselected
  3657. * @param string $htmlname Field name in form
  3658. * @param int $show_empty 0=liste sans valeur nulle, 1=ajoute valeur inconnue
  3659. * @param string $exclude Array list of groups id to exclude
  3660. * @param int $disabled If select list must be disabled
  3661. * @param string $include Array list of groups id to include
  3662. * @param int $enableonly Array list of groups id to be enabled. All other must be disabled
  3663. * @param int $force_entity 0 or Id of environment to force
  3664. * @return void
  3665. */
  3666. function select_dolgroups($selected='', $htmlname='groupid', $show_empty=0, $exclude='', $disabled=0, $include='', $enableonly='', $force_entity=0)
  3667. {
  3668. global $conf,$user,$langs;
  3669. // Permettre l'exclusion de groupes
  3670. if (is_array($exclude)) $excludeGroups = implode("','",$exclude);
  3671. // Permettre l'inclusion de groupes
  3672. if (is_array($include)) $includeGroups = implode("','",$include);
  3673. $out='';
  3674. // On recherche les groupes
  3675. $sql = "SELECT ug.rowid, ug.nom ";
  3676. if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
  3677. {
  3678. $sql.= ", e.label";
  3679. }
  3680. $sql.= " FROM ".MAIN_DB_PREFIX."usergroup as ug ";
  3681. if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
  3682. {
  3683. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."entity as e ON e.rowid=ug.entity";
  3684. if ($force_entity) $sql.= " WHERE ug.entity IN (0,".$force_entity.")";
  3685. else $sql.= " WHERE ug.entity IS NOT NULL";
  3686. }
  3687. else
  3688. {
  3689. $sql.= " WHERE ug.entity IN (0,".$conf->entity.")";
  3690. }
  3691. if (is_array($exclude) && $excludeGroups) $sql.= " AND ug.rowid NOT IN ('".$excludeGroups."')";
  3692. if (is_array($include) && $includeGroups) $sql.= " AND ug.rowid IN ('".$includeGroups."')";
  3693. $sql.= " ORDER BY ug.nom ASC";
  3694. dol_syslog(get_class($this)."::select_dolgroups sql=".$sql);
  3695. $resql=$this->db->query($sql);
  3696. if ($resql)
  3697. {
  3698. $num = $this->db->num_rows($resql);
  3699. $i = 0;
  3700. if ($num)
  3701. {
  3702. $out.= '<select class="flat" name="'.$htmlname.'"'.($disabled?' disabled="disabled"':'').'>';
  3703. if ($show_empty) $out.= '<option value="-1"'.($selected==-1?' selected="selected"':'').'>&nbsp;</option>'."\n";
  3704. while ($i < $num)
  3705. {
  3706. $obj = $this->db->fetch_object($resql);
  3707. $disableline=0;
  3708. if (is_array($enableonly) && count($enableonly) && ! in_array($obj->rowid,$enableonly)) $disableline=1;
  3709. $out.= '<option value="'.$obj->rowid.'"';
  3710. if ($disableline) $out.= ' disabled="disabled"';
  3711. if ((is_object($selected) && $selected->id == $obj->rowid) || (! is_object($selected) && $selected == $obj->rowid))
  3712. {
  3713. $out.= ' selected="selected"';
  3714. }
  3715. $out.= '>';
  3716. $out.= $obj->nom;
  3717. if (! empty($conf->multicompany->enabled) && empty($conf->multicompany->transverse_mode) && $conf->entity == 1)
  3718. {
  3719. $out.= " (".$obj->label.")";
  3720. }
  3721. $out.= '</option>';
  3722. $i++;
  3723. }
  3724. }
  3725. else
  3726. {
  3727. $out.= '<select class="flat" name="'.$htmlname.'" disabled="disabled">';
  3728. $out.= '<option value="">'.$langs->trans("None").'</option>';
  3729. }
  3730. $out.= '</select>';
  3731. }
  3732. else
  3733. {
  3734. dol_print_error($this->db);
  3735. }
  3736. return $out;
  3737. }
  3738. }
  3739. ?>