upgrade2.php 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084
  1. <?php
  2. /* Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  3. * Copyright (C) 2005-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015-2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  7. * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. * Upgrade2 scripts can be ran from command line with syntax:
  23. *
  24. * cd htdocs/install
  25. * php upgrade.php 3.4.0 3.5.0 [dirmodule|ignoredbversion]
  26. * php upgrade2.php 3.4.0 3.5.0 [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE]
  27. *
  28. * And for final step:
  29. * php step5.php 3.4.0 3.5.0
  30. *
  31. * Return code is 0 if OK, >0 if error
  32. *
  33. * Note: To just enable a module from command line, use this syntax:
  34. * php upgrade2.php 0.0.0 0.0.0 [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE]
  35. */
  36. /**
  37. * \file htdocs/install/upgrade2.php
  38. * \brief Upgrade some data
  39. */
  40. define('ALLOWED_IF_UPGRADE_UNLOCK_FOUND', 1);
  41. include_once 'inc.php';
  42. if (!file_exists($conffile)) {
  43. print 'Error: Dolibarr config file was not found. This may means that Dolibarr is not installed yet. Please call the page "/install/index.php" instead of "/install/upgrade.php").';
  44. }
  45. require_once $conffile;
  46. require_once $dolibarr_main_document_root.'/compta/facture/class/facture.class.php';
  47. require_once $dolibarr_main_document_root.'/comm/propal/class/propal.class.php';
  48. require_once $dolibarr_main_document_root.'/contrat/class/contrat.class.php';
  49. require_once $dolibarr_main_document_root.'/commande/class/commande.class.php';
  50. require_once $dolibarr_main_document_root.'/fourn/class/fournisseur.commande.class.php';
  51. require_once $dolibarr_main_document_root.'/core/lib/price.lib.php';
  52. require_once $dolibarr_main_document_root.'/core/class/menubase.class.php';
  53. require_once $dolibarr_main_document_root.'/core/lib/files.lib.php';
  54. global $langs;
  55. $grant_query = '';
  56. $step = 2;
  57. $error = 0;
  58. // Cette page peut etre longue. On augmente le delai autorise.
  59. // Ne fonctionne que si on est pas en safe_mode.
  60. $err = error_reporting();
  61. error_reporting(0);
  62. if (!empty($conf->global->MAIN_OVERRIDE_TIME_LIMIT)) {
  63. @set_time_limit((int) $conf->global->MAIN_OVERRIDE_TIME_LIMIT);
  64. } else {
  65. @set_time_limit(600);
  66. }
  67. error_reporting($err);
  68. $setuplang = GETPOST("selectlang", 'aZ09', 3) ?GETPOST("selectlang", 'aZ09', 3) : 'auto';
  69. $langs->setDefaultLang($setuplang);
  70. $versionfrom = GETPOST("versionfrom", 'alpha', 3) ?GETPOST("versionfrom", 'alpha', 3) : (empty($argv[1]) ? '' : $argv[1]);
  71. $versionto = GETPOST("versionto", 'alpha', 3) ?GETPOST("versionto", 'alpha', 3) : (empty($argv[2]) ? '' : $argv[2]);
  72. $enablemodules = GETPOST("enablemodules", 'alpha', 3) ?GETPOST("enablemodules", 'alpha', 3) : (empty($argv[3]) ? '' : $argv[3]);
  73. $langs->loadLangs(array("admin", "install", "bills", "suppliers"));
  74. if ($dolibarr_main_db_type == 'mysqli') {
  75. $choix = 1;
  76. }
  77. if ($dolibarr_main_db_type == 'pgsql') {
  78. $choix = 2;
  79. }
  80. if ($dolibarr_main_db_type == 'mssql') {
  81. $choix = 3;
  82. }
  83. dolibarr_install_syslog("--- upgrade2: entering upgrade2.php page ".$versionfrom." ".$versionto." ".$enablemodules);
  84. if (!is_object($conf)) {
  85. dolibarr_install_syslog("upgrade2: conf file not initialized", LOG_ERR);
  86. }
  87. /*
  88. * View
  89. */
  90. if ((!$versionfrom || preg_match('/version/', $versionfrom)) && (!$versionto || preg_match('/version/', $versionto))) {
  91. print 'Error: Parameter versionfrom or versionto missing or having a bad format.'."\n";
  92. print 'Upgrade must be ran from command line with parameters or called from page install/index.php (like a first install)'."\n";
  93. // Test if batch mode
  94. $sapi_type = php_sapi_name();
  95. $script_file = basename(__FILE__);
  96. $path = __DIR__.'/';
  97. if (substr($sapi_type, 0, 3) == 'cli') {
  98. print 'Syntax from command line: '.$script_file." x.y.z a.b.c [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE...]\n";
  99. }
  100. exit;
  101. }
  102. pHeader('', 'step5', GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : 'upgrade', 'versionfrom='.$versionfrom.'&versionto='.$versionto, '', 'main-inside main-inside-borderbottom');
  103. if (!GETPOST('action', 'aZ09') || preg_match('/upgrade/i', GETPOST('action', 'aZ09'))) {
  104. print '<h3><img class="valignmiddle inline-block paddingright" src="../theme/common/octicons/build/svg/database.svg" width="20" alt="Database"> ';
  105. print '<span class="inline-block">'.$langs->trans('DataMigration').'</span></h3>';
  106. print '<table border="0" width="100%">';
  107. // If password is encoded, we decode it
  108. if ((!empty($dolibarr_main_db_pass) && preg_match('/crypted:/i', $dolibarr_main_db_pass)) || !empty($dolibarr_main_db_encrypted_pass)) {
  109. require_once $dolibarr_main_document_root.'/core/lib/security.lib.php';
  110. if (!empty($dolibarr_main_db_pass) && preg_match('/crypted:/i', $dolibarr_main_db_pass)) {
  111. $dolibarr_main_db_pass = preg_replace('/crypted:/i', '', $dolibarr_main_db_pass);
  112. $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_pass);
  113. $dolibarr_main_db_encrypted_pass = $dolibarr_main_db_pass; // We need to set this as it is used to know the password was initially crypted
  114. } else {
  115. $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_encrypted_pass);
  116. }
  117. }
  118. // $conf is already instancied inside inc.php
  119. $conf->db->type = $dolibarr_main_db_type;
  120. $conf->db->host = $dolibarr_main_db_host;
  121. $conf->db->port = $dolibarr_main_db_port;
  122. $conf->db->name = $dolibarr_main_db_name;
  123. $conf->db->user = $dolibarr_main_db_user;
  124. $conf->db->pass = $dolibarr_main_db_pass;
  125. $db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, (int) $conf->db->port);
  126. if (!$db->connected) {
  127. print '<tr><td colspan="4">'.$langs->trans("ErrorFailedToConnectToDatabase", $conf->db->name).'</td><td class="right">'.$langs->trans('Error').'</td></tr>';
  128. dolibarr_install_syslog('upgrade2: failed to connect to database :'.$conf->db->name.' on '.$conf->db->host.' for user '.$conf->db->user, LOG_ERR);
  129. $error++;
  130. }
  131. if (!$error) {
  132. if ($db->database_selected) {
  133. dolibarr_install_syslog('upgrade2: database connection successful :'.$dolibarr_main_db_name);
  134. } else {
  135. $error++;
  136. }
  137. }
  138. if (empty($dolibarr_main_db_encryption)) {
  139. $dolibarr_main_db_encryption = 0;
  140. }
  141. $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
  142. if (empty($dolibarr_main_db_cryptkey)) {
  143. $dolibarr_main_db_cryptkey = '';
  144. }
  145. $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
  146. // Load global conf
  147. $conf->setValues($db);
  148. $listofentities = array(1);
  149. // Create the global $hookmanager object
  150. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  151. $hookmanager = new HookManager($db);
  152. $hookmanager->initHooks(array('upgrade2'));
  153. $parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto);
  154. $object = new stdClass();
  155. $action = "upgrade";
  156. $reshook = $hookmanager->executeHooks('doUpgradeBefore', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  157. if ($reshook >= 0 && is_array($hookmanager->resArray)) {
  158. // Example: $hookmanager->resArray = array(2, 3, 10);
  159. $listofentities = array_unique(array_merge($listofentities, $hookmanager->resArray));
  160. }
  161. /***************************************************************************************
  162. *
  163. * Migration of data
  164. *
  165. ***************************************************************************************/
  166. // Force to execute this at begin to avoid the new core code into Dolibarr to be broken.
  167. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN birth date';
  168. $db->query($sql, 1);
  169. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN dateemployment date';
  170. $db->query($sql, 1);
  171. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN dateemploymentend date';
  172. $db->query($sql, 1);
  173. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN default_range integer';
  174. $db->query($sql, 1);
  175. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN default_c_exp_tax_cat integer';
  176. $db->query($sql, 1);
  177. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN langs varchar(24)';
  178. $db->query($sql, 1);
  179. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN fieldcomputed text';
  180. $db->query($sql, 1);
  181. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN fielddefault varchar(255)';
  182. $db->query($sql, 1);
  183. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX."extrafields ADD COLUMN enabled varchar(255) DEFAULT '1'";
  184. $db->query($sql, 1);
  185. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN help text';
  186. $db->query($sql, 1);
  187. $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user_rights ADD COLUMN entity integer DEFAULT 1 NOT NULL';
  188. $db->query($sql, 1);
  189. $db->begin();
  190. foreach ($listofentities as $entity) {
  191. // Set $conf context for entity
  192. $conf->setEntityValues($db, $entity);
  193. // Reset forced setup after the setValues
  194. if (defined('SYSLOG_FILE')) {
  195. $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
  196. }
  197. $conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
  198. if (!$error) {
  199. if (count($listofentities) > 1) {
  200. print '<tr><td colspan="4">*** '.$langs->trans("Entity").' '.$entity.'</td></tr>'."\n";
  201. }
  202. // Current version is $conf->global->MAIN_VERSION_LAST_UPGRADE
  203. // Version to install is DOL_VERSION
  204. $dolibarrlastupgradeversionarray = preg_split('/[\.-]/', isset($conf->global->MAIN_VERSION_LAST_UPGRADE) ? $conf->global->MAIN_VERSION_LAST_UPGRADE : (isset($conf->global->MAIN_VERSION_LAST_INSTALL) ? $conf->global->MAIN_VERSION_LAST_INSTALL : ''));
  205. // Chaque action de migration doit renvoyer une ligne sur 4 colonnes avec
  206. // dans la 1ere colonne, la description de l'action a faire
  207. // dans la 4eme colonne, le texte 'OK' si fait ou 'AlreadyDone' si rien n'est fait ou 'Error'
  208. $versiontoarray = explode('.', $versionto);
  209. $versionranarray = explode('.', DOL_VERSION);
  210. $afterversionarray = explode('.', '2.0.0');
  211. $beforeversionarray = explode('.', '2.7.9');
  212. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  213. // Script pour V2 -> V2.1
  214. migrate_paiements($db, $langs, $conf);
  215. migrate_contracts_det($db, $langs, $conf);
  216. migrate_contracts_date1($db, $langs, $conf);
  217. migrate_contracts_date2($db, $langs, $conf);
  218. migrate_contracts_date3($db, $langs, $conf);
  219. migrate_contracts_open($db, $langs, $conf);
  220. migrate_modeles($db, $langs, $conf);
  221. migrate_price_propal($db, $langs, $conf);
  222. migrate_price_commande($db, $langs, $conf);
  223. migrate_price_commande_fournisseur($db, $langs, $conf);
  224. migrate_price_contrat($db, $langs, $conf);
  225. migrate_paiementfourn_facturefourn($db, $langs, $conf);
  226. // Script pour V2.1 -> V2.2
  227. migrate_paiements_orphelins_1($db, $langs, $conf);
  228. migrate_paiements_orphelins_2($db, $langs, $conf);
  229. migrate_links_transfert($db, $langs, $conf);
  230. // Script pour V2.2 -> V2.4
  231. migrate_commande_expedition($db, $langs, $conf);
  232. migrate_commande_livraison($db, $langs, $conf);
  233. migrate_detail_livraison($db, $langs, $conf);
  234. // Script pour V2.5 -> V2.6
  235. migrate_stocks($db, $langs, $conf);
  236. // Script pour V2.6 -> V2.7
  237. migrate_menus($db, $langs, $conf);
  238. migrate_commande_deliveryaddress($db, $langs, $conf);
  239. migrate_restore_missing_links($db, $langs, $conf);
  240. migrate_rename_directories($db, $langs, $conf, '/compta', '/banque');
  241. migrate_rename_directories($db, $langs, $conf, '/societe', '/mycompany');
  242. }
  243. // Script for 2.8
  244. $afterversionarray = explode('.', '2.7.9');
  245. $beforeversionarray = explode('.', '2.8.9');
  246. //print $versionto.' '.versioncompare($versiontoarray,$afterversionarray).' '.versioncompare($versiontoarray,$beforeversionarray);
  247. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  248. migrate_price_facture($db, $langs, $conf); // Code of this function works for 2.8+ because need a field tva_tx
  249. migrate_relationship_tables($db, $langs, $conf, 'co_exp', 'fk_commande', 'commande', 'fk_expedition', 'shipping');
  250. migrate_relationship_tables($db, $langs, $conf, 'pr_exp', 'fk_propal', 'propal', 'fk_expedition', 'shipping');
  251. migrate_relationship_tables($db, $langs, $conf, 'pr_liv', 'fk_propal', 'propal', 'fk_livraison', 'delivery');
  252. migrate_relationship_tables($db, $langs, $conf, 'co_liv', 'fk_commande', 'commande', 'fk_livraison', 'delivery');
  253. migrate_relationship_tables($db, $langs, $conf, 'co_pr', 'fk_propale', 'propal', 'fk_commande', 'commande');
  254. migrate_relationship_tables($db, $langs, $conf, 'fa_pr', 'fk_propal', 'propal', 'fk_facture', 'facture');
  255. migrate_relationship_tables($db, $langs, $conf, 'co_fa', 'fk_commande', 'commande', 'fk_facture', 'facture');
  256. migrate_project_user_resp($db, $langs, $conf);
  257. migrate_project_task_actors($db, $langs, $conf);
  258. }
  259. // Script for 2.9
  260. $afterversionarray = explode('.', '2.8.9');
  261. $beforeversionarray = explode('.', '2.9.9');
  262. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  263. migrate_element_time($db, $langs, $conf);
  264. migrate_customerorder_shipping($db, $langs, $conf);
  265. migrate_shipping_delivery($db, $langs, $conf);
  266. migrate_shipping_delivery2($db, $langs, $conf);
  267. }
  268. // Script for 3.0
  269. $afterversionarray = explode('.', '2.9.9');
  270. $beforeversionarray = explode('.', '3.0.9');
  271. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  272. // No particular code
  273. }
  274. // Script for 3.1
  275. $afterversionarray = explode('.', '3.0.9');
  276. $beforeversionarray = explode('.', '3.1.9');
  277. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  278. migrate_rename_directories($db, $langs, $conf, '/rss', '/externalrss');
  279. migrate_actioncomm_element($db, $langs, $conf);
  280. }
  281. // Script for 3.2
  282. $afterversionarray = explode('.', '3.1.9');
  283. $beforeversionarray = explode('.', '3.2.9');
  284. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  285. migrate_price_contrat($db, $langs, $conf);
  286. migrate_mode_reglement($db, $langs, $conf);
  287. migrate_clean_association($db, $langs, $conf);
  288. }
  289. // Script for 3.3
  290. $afterversionarray = explode('.', '3.2.9');
  291. $beforeversionarray = explode('.', '3.3.9');
  292. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  293. migrate_categorie_association($db, $langs, $conf);
  294. }
  295. // Script for 3.4
  296. // No specific scripts
  297. // Tasks to do always and only into last targeted version
  298. $afterversionarray = explode('.', '3.6.9'); // target is after this
  299. $beforeversionarray = explode('.', '3.7.9'); // target is before this
  300. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  301. migrate_event_assignement($db, $langs, $conf);
  302. }
  303. // Scripts for 3.9
  304. $afterversionarray = explode('.', '3.7.9');
  305. $beforeversionarray = explode('.', '3.8.9');
  306. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  307. // No particular code
  308. }
  309. // Scripts for 4.0
  310. $afterversionarray = explode('.', '3.9.9');
  311. $beforeversionarray = explode('.', '4.0.9');
  312. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  313. migrate_rename_directories($db, $langs, $conf, '/fckeditor', '/medias');
  314. }
  315. // Scripts for 5.0
  316. $afterversionarray = explode('.', '4.0.9');
  317. $beforeversionarray = explode('.', '5.0.9');
  318. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  319. // Migrate to add entity value into llx_societe_remise
  320. migrate_remise_entity($db, $langs, $conf);
  321. // Migrate to add entity value into llx_societe_remise_except
  322. migrate_remise_except_entity($db, $langs, $conf);
  323. }
  324. // Scripts for 6.0
  325. $afterversionarray = explode('.', '5.0.9');
  326. $beforeversionarray = explode('.', '6.0.9');
  327. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  328. if (isModEnabled('multicompany')) {
  329. global $multicompany_transverse_mode;
  330. // Only if the transverse mode is not used
  331. if (empty($multicompany_transverse_mode)) {
  332. // Migrate to add entity value into llx_user_rights
  333. migrate_user_rights_entity($db, $langs, $conf);
  334. // Migrate to add entity value into llx_usergroup_rights
  335. migrate_usergroup_rights_entity($db, $langs, $conf);
  336. }
  337. }
  338. }
  339. // Scripts for 7.0
  340. $afterversionarray = explode('.', '6.0.9');
  341. $beforeversionarray = explode('.', '7.0.9');
  342. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  343. // Migrate contact association
  344. migrate_event_assignement_contact($db, $langs, $conf);
  345. migrate_reset_blocked_log($db, $langs, $conf);
  346. }
  347. // Scripts for 8.0
  348. $afterversionarray = explode('.', '7.0.9');
  349. $beforeversionarray = explode('.', '8.0.9');
  350. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  351. migrate_rename_directories($db, $langs, $conf, '/contracts', '/contract');
  352. }
  353. // Scripts for 9.0
  354. $afterversionarray = explode('.', '8.0.9');
  355. $beforeversionarray = explode('.', '9.0.9');
  356. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  357. //migrate_user_photospath();
  358. }
  359. // Scripts for 11.0
  360. $afterversionarray = explode('.', '10.0.9');
  361. $beforeversionarray = explode('.', '11.0.9');
  362. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  363. migrate_users_socialnetworks();
  364. migrate_members_socialnetworks();
  365. migrate_contacts_socialnetworks();
  366. migrate_thirdparties_socialnetworks();
  367. }
  368. // Scripts for 14.0
  369. $afterversionarray = explode('.', '13.0.9');
  370. $beforeversionarray = explode('.', '14.0.9');
  371. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  372. migrate_export_import_profiles('export');
  373. migrate_export_import_profiles('import');
  374. }
  375. // Scripts for 16.0
  376. $afterversionarray = explode('.', '15.0.9');
  377. $beforeversionarray = explode('.', '16.0.9');
  378. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  379. migrate_user_photospath();
  380. migrate_user_photospath2();
  381. }
  382. // Scripts for 17.0
  383. $afterversionarray = explode('.', '16.0.9');
  384. $beforeversionarray = explode('.', '17.0.9');
  385. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  386. migrate_contractdet_rank();
  387. }
  388. // Scripts for 18.0
  389. $afterversionarray = explode('.', '17.0.9');
  390. $beforeversionarray = explode('.', '18.0.9');
  391. if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
  392. migrate_contractdet_rank();
  393. }
  394. }
  395. // Code executed only if migration is LAST ONE. Must always be done.
  396. if (versioncompare($versiontoarray, $versionranarray) >= 0 || versioncompare($versiontoarray, $versionranarray) <= -3) {
  397. // Reload modules (this must be always done and only into last targeted version, because code to reload module may need table structure of last version)
  398. $listofmodule = array(
  399. 'MAIN_MODULE_ACCOUNTING'=>'newboxdefonly',
  400. 'MAIN_MODULE_AGENDA'=>'newboxdefonly',
  401. 'MAIN_MODULE_BOM'=>'menuonly',
  402. 'MAIN_MODULE_BANQUE'=>'menuonly',
  403. 'MAIN_MODULE_BARCODE'=>'newboxdefonly',
  404. 'MAIN_MODULE_CRON'=>'newboxdefonly',
  405. 'MAIN_MODULE_COMMANDE'=>'newboxdefonly',
  406. 'MAIN_MODULE_BLOCKEDLOG'=>'noboxes',
  407. 'MAIN_MODULE_DEPLACEMENT'=>'newboxdefonly',
  408. 'MAIN_MODULE_DON'=>'newboxdefonly',
  409. 'MAIN_MODULE_ECM'=>'newboxdefonly',
  410. 'MAIN_MODULE_EXTERNALSITE'=>'newboxdefonly',
  411. 'MAIN_MODULE_EXPENSEREPORT'=>'newboxdefonly',
  412. 'MAIN_MODULE_FACTURE'=>'newboxdefonly',
  413. 'MAIN_MODULE_FOURNISSEUR'=>'newboxdefonly',
  414. 'MAIN_MODULE_HOLIDAY'=>'newboxdefonly',
  415. 'MAIN_MODULE_MARGIN'=>'menuonly',
  416. 'MAIN_MODULE_MRP'=>'menuonly',
  417. 'MAIN_MODULE_OPENSURVEY'=>'newboxdefonly',
  418. 'MAIN_MODULE_PAYBOX'=>'newboxdefonly',
  419. 'MAIN_MODULE_PRINTING'=>'newboxdefonly',
  420. 'MAIN_MODULE_PRODUIT'=>'newboxdefonly',
  421. 'MAIN_MODULE_RECRUITMENT'=>'menuonly',
  422. 'MAIN_MODULE_RESOURCE'=>'noboxes',
  423. 'MAIN_MODULE_SALARIES'=>'newboxdefonly',
  424. 'MAIN_MODULE_SERVICE'=>'newboxdefonly',
  425. 'MAIN_MODULE_SYSLOG'=>'newboxdefonly',
  426. 'MAIN_MODULE_SOCIETE'=>'newboxdefonly',
  427. 'MAIN_MODULE_STRIPE'=>'menuonly',
  428. 'MAIN_MODULE_TICKET'=>'newboxdefonly',
  429. 'MAIN_MODULE_TAKEPOS'=>'newboxdefonly',
  430. 'MAIN_MODULE_USER'=>'newboxdefonly', //This one must be always done and only into last targeted version)
  431. 'MAIN_MODULE_VARIANTS'=>'newboxdefonly',
  432. 'MAIN_MODULE_WEBSITE'=>'newboxdefonly',
  433. );
  434. $result = migrate_reload_modules($db, $langs, $conf, $listofmodule);
  435. if ($result < 0) {
  436. $error++;
  437. }
  438. // Reload menus (this must be always and only into last targeted version)
  439. $result = migrate_reload_menu($db, $langs, $conf);
  440. if ($result < 0) {
  441. $error++;
  442. }
  443. }
  444. // Can force activation of some module during migration with parameter 'enablemodules=MAIN_MODULE_XXX,MAIN_MODULE_YYY,...'
  445. // In most cases (online install or upgrade) $enablemodules is empty. Can be forced when ran from command line.
  446. if (!$error && $enablemodules) {
  447. // Reload modules (this must be always done and only into last targeted version)
  448. $listofmodules = array();
  449. $enablemodules = preg_replace('/enablemodules=/', '', $enablemodules);
  450. $tmplistofmodules = explode(',', $enablemodules);
  451. foreach ($tmplistofmodules as $value) {
  452. $listofmodules[$value] = 'forceactivate';
  453. }
  454. $resultreloadmodules = migrate_reload_modules($db, $langs, $conf, $listofmodules, 1);
  455. if ($resultreloadmodules < 0) {
  456. $error++;
  457. }
  458. }
  459. // Can call a dedicated external upgrade process with hook doUpgradeAfterDB()
  460. if (!$error) {
  461. $parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto, 'conf'=>$conf);
  462. $object = new stdClass();
  463. $action = "upgrade";
  464. $reshook = $hookmanager->executeHooks('doUpgradeAfterDB', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  465. if ($hookmanager->resNbOfHooks > 0) {
  466. if ($reshook < 0) {
  467. print '<tr><td colspan="4">';
  468. print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: ';
  469. print $hookmanager->error;
  470. print "<!-- (".$reshook.") -->";
  471. print '</td></tr>';
  472. } else {
  473. print '<tr class="trforrunsql"><td colspan="4">';
  474. print '<b>'.$langs->trans('UpgradeExternalModule').' (DB)</b>: <span class="ok">OK</span>';
  475. print "<!-- (".$reshook.") -->";
  476. print '</td></tr>';
  477. }
  478. } else {
  479. //if (!empty($conf->modules))
  480. if (!empty($conf->modules_parts['hooks'])) { // If there is at least one module with one hook, we show message to say nothing was done
  481. print '<tr class="trforrunsql"><td colspan="4">';
  482. print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: '.$langs->trans("NodoUpgradeAfterDB");
  483. print '</td></tr>';
  484. }
  485. }
  486. }
  487. }
  488. print '</table>';
  489. if (!$error) {
  490. // Set constant to ask to remake a new ping to inform about upgrade (if first ping was done and OK)
  491. $sql = 'UPDATE '.MAIN_DB_PREFIX."const SET VALUE = 'torefresh' WHERE name = 'MAIN_FIRST_PING_OK_ID'";
  492. $db->query($sql, 1);
  493. }
  494. // We always commit.
  495. // Process is designed so we can run it several times whatever is situation.
  496. $db->commit();
  497. /***************************************************************************************
  498. *
  499. * Migration of files
  500. *
  501. ***************************************************************************************/
  502. foreach ($listofentities as $entity) {
  503. // Set $conf context for entity
  504. $conf->setEntityValues($db, $entity);
  505. // Reset forced setup after the setValues
  506. if (defined('SYSLOG_FILE')) {
  507. $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
  508. }
  509. $conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
  510. // Copy directory medias
  511. $srcroot = DOL_DOCUMENT_ROOT.'/install/medias';
  512. $destroot = DOL_DATA_ROOT.'/medias';
  513. dolCopyDir($srcroot, $destroot, 0, 0);
  514. // Actions for all versions (no database change but delete some files and directories)
  515. migrate_delete_old_files($db, $langs, $conf);
  516. migrate_delete_old_dir($db, $langs, $conf);
  517. // Actions for all versions (no database change but create some directories)
  518. dol_mkdir(DOL_DATA_ROOT.'/bank');
  519. // Actions for all versions (no database change but rename some directories)
  520. migrate_rename_directories($db, $langs, $conf, '/banque/bordereau', '/bank/checkdeposits');
  521. $parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto, 'conf'=>$conf);
  522. $object = new stdClass();
  523. $action = "upgrade";
  524. $reshook = $hookmanager->executeHooks('doUpgradeAfterFiles', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  525. if ($hookmanager->resNbOfHooks > 0) {
  526. if ($reshook < 0) {
  527. print '<tr><td colspan="4">';
  528. print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: ';
  529. print $hookmanager->error;
  530. print "<!-- (".$reshook.") -->";
  531. print '</td></tr>';
  532. } else {
  533. print '<tr class="trforrunsql"><td colspan="4">';
  534. print '<b>'.$langs->trans('UpgradeExternalModule').' (Files)</b>: <span class="ok">OK</span>';
  535. print "<!-- (".$reshook.") -->";
  536. print '</td></tr>';
  537. }
  538. } else {
  539. //if (!empty($conf->modules))
  540. if (!empty($conf->modules_parts['hooks'])) { // If there is at least one module with one hook, we show message to say nothing was done
  541. print '<tr class="trforrunsql"><td colspan="4">';
  542. print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: '.$langs->trans("NodoUpgradeAfterFiles");
  543. print '</td></tr>';
  544. }
  545. }
  546. }
  547. $db->close();
  548. $silent = 0;
  549. if (!$silent) {
  550. print '<table width="100%">';
  551. print '<tr><td style="width: 30%">'.$langs->trans("MigrationFinished").'</td>';
  552. print '<td class="right">';
  553. if ($error == 0) {
  554. //print '<span class="ok">'.$langs->trans("OK").'</span> - '; // $error = 0 does not mean there is no error (error are not always trapped)
  555. } else {
  556. print '<span class="error">'.$langs->trans("Error").'</span> - ';
  557. }
  558. //if (!empty($conf->use_javascript_ajax)) { // use_javascript_ajax is not defined
  559. print '<script type="text/javascript">
  560. jQuery(document).ready(function() {
  561. function init_trrunsql()
  562. {
  563. console.log("toggle .trforrunsql");
  564. jQuery(".trforrunsql").toggle();
  565. }
  566. init_trrunsql();
  567. jQuery(".trforrunsqlshowhide").click(function() {
  568. init_trrunsql();
  569. });
  570. });
  571. </script>';
  572. print '<a class="trforrunsqlshowhide" href="#">'.$langs->trans("ShowHideDetails").'</a>';
  573. //}
  574. print '</td></tr>'."\n";
  575. print '</table>';
  576. }
  577. //print '<div><br>'.$langs->trans("MigrationFinished").'</div>';
  578. } else {
  579. print '<div class="error">'.$langs->trans('ErrorWrongParameters').'</div>';
  580. $error++;
  581. }
  582. $ret = 0;
  583. if ($error && isset($argv[1])) {
  584. $ret = 1;
  585. }
  586. dolibarr_install_syslog("Exit ".$ret);
  587. dolibarr_install_syslog("--- upgrade2: end");
  588. pFooter($error ? 2 : 0, $setuplang);
  589. if ($db->connected) {
  590. $db->close();
  591. }
  592. // Return code if ran from command line
  593. if ($ret) {
  594. exit($ret);
  595. }
  596. /**
  597. * Reporte liens vers une facture de paiements sur table de jointure (lien n-n paiements factures)
  598. *
  599. * @param DoliDB $db Database handler
  600. * @param Translate $langs Object langs
  601. * @param Conf $conf Object conf
  602. * @return void
  603. */
  604. function migrate_paiements($db, $langs, $conf)
  605. {
  606. print '<tr><td colspan="4">';
  607. print '<br>';
  608. print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
  609. $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
  610. $obj = $db->fetch_object($result);
  611. if ($obj) {
  612. $sql = "SELECT p.rowid, p.fk_facture, p.amount";
  613. $sql .= " FROM ".MAIN_DB_PREFIX."paiement as p";
  614. $sql .= " WHERE p.fk_facture > 0";
  615. $resql = $db->query($sql);
  616. dolibarr_install_syslog("upgrade2::migrate_paiements");
  617. if ($resql) {
  618. $i = 0;
  619. $row = array();
  620. $num = $db->num_rows($resql);
  621. while ($i < $num) {
  622. $obj = $db->fetch_object($resql);
  623. $row[$i][0] = $obj->rowid;
  624. $row[$i][1] = $obj->fk_facture;
  625. $row[$i][2] = $obj->amount;
  626. $i++;
  627. }
  628. } else {
  629. dol_print_error($db);
  630. }
  631. if ($num) {
  632. print $langs->trans('MigrationPaymentsNumberToUpdate', $num)."<br>\n";
  633. if ($db->begin()) {
  634. $res = 0;
  635. $num = count($row);
  636. for ($i = 0; $i < $num; $i++) {
  637. $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
  638. $sql .= " VALUES (".((int) $row[$i][1]).",".((int) $row[$i][0]).",".((float) $row[$i][2]).")";
  639. $res += $db->query($sql);
  640. $sql = "UPDATE ".MAIN_DB_PREFIX."paiement SET fk_facture = 0 WHERE rowid = ".((int) $row[$i][0]);
  641. $res += $db->query($sql);
  642. print $langs->trans('MigrationProcessPaymentUpdate', $row[$i][0])."<br>\n";
  643. }
  644. }
  645. if ($res == (2 * count($row))) {
  646. $db->commit();
  647. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  648. } else {
  649. $db->rollback();
  650. print $langs->trans('MigrationUpdateFailed').'<br>';
  651. }
  652. } else {
  653. print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
  654. }
  655. } else {
  656. print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
  657. }
  658. print '</td></tr>';
  659. }
  660. /**
  661. * Corrige paiement orphelins (liens paumes suite a bugs)
  662. * Pour verifier s'il reste des orphelins:
  663. * select * from llx_paiement as p left join llx_paiement_facture as pf on pf.fk_paiement=p.rowid WHERE pf.rowid IS NULL AND (p.fk_facture = 0 OR p.fk_facture IS NULL)
  664. *
  665. * @param DoliDB $db Database handler
  666. * @param Translate $langs Object langs
  667. * @param Conf $conf Object conf
  668. * @return void
  669. */
  670. function migrate_paiements_orphelins_1($db, $langs, $conf)
  671. {
  672. print '<tr><td colspan="4">';
  673. print '<br>';
  674. print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
  675. $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
  676. $obj = $db->fetch_object($result);
  677. if ($obj) {
  678. // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
  679. $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
  680. $sql .= " bu2.url_id as socid";
  681. $sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
  682. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
  683. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank=bu2.fk_bank AND bu2.type = 'company')";
  684. $sql .= " WHERE pf.rowid IS NULL AND (p.rowid=bu.url_id AND bu.type='payment') AND bu.fk_bank = b.rowid";
  685. $sql .= " AND b.rappro = 1";
  686. $sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
  687. $resql = $db->query($sql);
  688. dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_1");
  689. $row = array();
  690. if ($resql) {
  691. $i = $j = 0;
  692. $num = $db->num_rows($resql);
  693. while ($i < $num) {
  694. $obj = $db->fetch_object($resql);
  695. if ($obj->pamount == $obj->bamount && $obj->socid) { // Pour etre sur d'avoir bon cas
  696. $row[$j]['paymentid'] = $obj->rowid; // paymentid
  697. $row[$j]['pamount'] = $obj->pamount;
  698. $row[$j]['fk_bank'] = $obj->fk_bank;
  699. $row[$j]['bamount'] = $obj->bamount;
  700. $row[$j]['socid'] = $obj->socid;
  701. $row[$j]['datec'] = $obj->datec;
  702. $j++;
  703. }
  704. $i++;
  705. }
  706. } else {
  707. dol_print_error($db);
  708. }
  709. if (count($row)) {
  710. print $langs->trans('OrphelinsPaymentsDetectedByMethod', 1).': '.count($row)."<br>\n";
  711. $db->begin();
  712. $res = 0;
  713. $num = count($row);
  714. for ($i = 0; $i < $num; $i++) {
  715. if ($conf->global->MAIN_FEATURES_LEVEL == 2) {
  716. print '* '.$row[$i]['datec'].' paymentid='.$row[$i]['paymentid'].' pamount='.$row[$i]['pamount'].' fk_bank='.$row[$i]['fk_bank'].' bamount='.$row[$i]['bamount'].' socid='.$row[$i]['socid'].'<br>';
  717. }
  718. // On cherche facture sans lien paiement et du meme montant et pour meme societe.
  719. $sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
  720. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
  721. $sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".((int) $row[$i]['socid'])." AND total_ttc = ".((float) $row[$i]['pamount']);
  722. $sql .= " AND pf.fk_facture IS NULL";
  723. $sql .= " ORDER BY f.fk_statut";
  724. //print $sql.'<br>';
  725. $resql = $db->query($sql);
  726. if ($resql) {
  727. $num = $db->num_rows($resql);
  728. //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
  729. if ($num >= 1) {
  730. $obj = $db->fetch_object($resql);
  731. $facid = $obj->rowid;
  732. $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
  733. $sql .= " VALUES (".((int) $facid).",".((int) $row[$i]['paymentid']).", ".((float) $row[$i]['pamount']).")";
  734. $res += $db->query($sql);
  735. print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
  736. }
  737. } else {
  738. print 'ERROR';
  739. }
  740. }
  741. if ($res > 0) {
  742. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  743. } else {
  744. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  745. }
  746. $db->commit();
  747. } else {
  748. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  749. }
  750. } else {
  751. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  752. }
  753. print '</td></tr>';
  754. }
  755. /**
  756. * Corrige paiement orphelins (liens paumes suite a bugs)
  757. * Pour verifier s'il reste des orphelins:
  758. * select * from llx_paiement as p left join llx_paiement_facture as pf on pf.fk_paiement=p.rowid WHERE pf.rowid IS NULL AND (p.fk_facture = 0 OR p.fk_facture IS NULL)
  759. *
  760. * @param DoliDB $db Database handler
  761. * @param Translate $langs Object langs
  762. * @param Conf $conf Object conf
  763. * @return void
  764. */
  765. function migrate_paiements_orphelins_2($db, $langs, $conf)
  766. {
  767. print '<tr><td colspan="4">';
  768. print '<br>';
  769. print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
  770. $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
  771. $obj = $db->fetch_object($result);
  772. if ($obj) {
  773. // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
  774. $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
  775. $sql .= " bu2.url_id as socid";
  776. $sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
  777. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
  778. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank = bu2.fk_bank AND bu2.type = 'company')";
  779. $sql .= " WHERE pf.rowid IS NULL AND (p.fk_bank = bu.fk_bank AND bu.type = 'payment') AND bu.fk_bank = b.rowid";
  780. $sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
  781. $resql = $db->query($sql);
  782. dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_2");
  783. $row = array();
  784. if ($resql) {
  785. $i = $j = 0;
  786. $num = $db->num_rows($resql);
  787. while ($i < $num) {
  788. $obj = $db->fetch_object($resql);
  789. if ($obj->pamount == $obj->bamount && $obj->socid) { // Pour etre sur d'avoir bon cas
  790. $row[$j]['paymentid'] = $obj->rowid; // paymentid
  791. $row[$j]['pamount'] = $obj->pamount;
  792. $row[$j]['fk_bank'] = $obj->fk_bank;
  793. $row[$j]['bamount'] = $obj->bamount;
  794. $row[$j]['socid'] = $obj->socid;
  795. $row[$j]['datec'] = $obj->datec;
  796. $j++;
  797. }
  798. $i++;
  799. }
  800. } else {
  801. dol_print_error($db);
  802. }
  803. $nberr = 0;
  804. $num = count($row);
  805. if ($num) {
  806. print $langs->trans('OrphelinsPaymentsDetectedByMethod', 2).': '.count($row)."<br>\n";
  807. $db->begin();
  808. $res = 0;
  809. for ($i = 0; $i < $num; $i++) {
  810. if ($conf->global->MAIN_FEATURES_LEVEL == 2) {
  811. print '* '.$row[$i]['datec'].' paymentid='.$row[$i]['paymentid'].' pamount='.$row[$i]['pamount'].' fk_bank='.$row[$i]['fk_bank'].' '.$row[$i]['bamount'].' socid='.$row[$i]['socid'].'<br>';
  812. }
  813. // On cherche facture sans lien paiement et du meme montant et pour meme societe.
  814. $sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
  815. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
  816. $sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".((int) $row[$i]['socid'])." AND total_ttc = ".((float) $row[$i]['pamount']);
  817. $sql .= " AND pf.fk_facture IS NULL";
  818. $sql .= " ORDER BY f.fk_statut";
  819. //print $sql.'<br>';
  820. $resql = $db->query($sql);
  821. if ($resql) {
  822. $num = $db->num_rows($resql);
  823. //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
  824. if ($num >= 1) {
  825. $obj = $db->fetch_object($resql);
  826. $facid = $obj->rowid;
  827. $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
  828. $sql .= " VALUES (".((int) $facid).",".((int) $row[$i]['paymentid']).", ".((float) $row[$i]['pamount']).")";
  829. $res += $db->query($sql);
  830. print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
  831. }
  832. } else {
  833. print 'ERROR';
  834. $nberr++;
  835. }
  836. }
  837. if ($res > 0) {
  838. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  839. } else {
  840. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  841. }
  842. $db->commit();
  843. } else {
  844. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  845. }
  846. // Delete obsolete fields fk_facture
  847. $db->begin();
  848. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiement DROP COLUMN fk_facture";
  849. $db->query($sql);
  850. if (!$nberr) {
  851. $db->commit();
  852. } else {
  853. print 'ERROR';
  854. $db->rollback();
  855. }
  856. } else {
  857. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  858. }
  859. print '</td></tr>';
  860. }
  861. /**
  862. * Mise a jour des contrats (gestion du contrat + detail de contrat)
  863. *
  864. * @param DoliDB $db Database handler
  865. * @param Translate $langs Object langs
  866. * @param Conf $conf Object conf
  867. * @return void
  868. */
  869. function migrate_contracts_det($db, $langs, $conf)
  870. {
  871. print '<tr><td colspan="4">';
  872. $nberr = 0;
  873. print '<br>';
  874. print '<b>'.$langs->trans('MigrationContractsUpdate')."</b><br>\n";
  875. $sql = "SELECT c.rowid as cref, c.date_contrat, c.statut, c.fk_product, c.fk_facture, c.fk_user_author,";
  876. $sql .= " p.ref, p.label, p.description, p.price, p.tva_tx, p.duration, cd.rowid";
  877. $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
  878. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p";
  879. $sql .= " ON c.fk_product = p.rowid";
  880. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd";
  881. $sql .= " ON c.rowid=cd.fk_contrat";
  882. $sql .= " WHERE cd.rowid IS NULL AND p.rowid IS NOT NULL";
  883. $resql = $db->query($sql);
  884. dolibarr_install_syslog("upgrade2::migrate_contracts_det");
  885. if ($resql) {
  886. $i = 0;
  887. $row = array();
  888. $num = $db->num_rows($resql);
  889. if ($num) {
  890. print $langs->trans('MigrationContractsNumberToUpdate', $num)."<br>\n";
  891. $db->begin();
  892. while ($i < $num) {
  893. $obj = $db->fetch_object($resql);
  894. $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet (";
  895. $sql .= "fk_contrat, fk_product, statut, label, description,";
  896. $sql .= "date_ouverture_prevue, date_ouverture, date_fin_validite, tva_tx, qty,";
  897. $sql .= "subprice, price_ht, fk_user_author, fk_user_ouverture)";
  898. $sql .= " VALUES (";
  899. $sql .= ((int) $obj->cref).", ".($obj->fk_product ? ((int) $obj->fk_product) : 0).", ";
  900. $sql .= "0, ";
  901. $sql .= "'".$db->escape($obj->label)."', null, ";
  902. $sql .= ($obj->date_contrat ? "'".$db->idate($db->jdate($obj->date_contrat))."'" : "null").", ";
  903. $sql .= "null, ";
  904. $sql .= "null, ";
  905. $sql .= ((float) $obj->tva_tx).", 1, ";
  906. $sql .= ((float) $obj->price).", ".((float) $obj->price).", ".((int) $obj->fk_user_author).",";
  907. $sql .= "null";
  908. $sql .= ")";
  909. if ($db->query($sql)) {
  910. print $langs->trans('MigrationContractsLineCreation', $obj->cref)."<br>\n";
  911. } else {
  912. dol_print_error($db);
  913. $nberr++;
  914. }
  915. $i++;
  916. }
  917. if (!$nberr) {
  918. // $db->rollback();
  919. $db->commit();
  920. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  921. } else {
  922. $db->rollback();
  923. print $langs->trans('MigrationUpdateFailed').'<br>';
  924. }
  925. } else {
  926. print $langs->trans('MigrationContractsNothingToUpdate')."<br>\n";
  927. }
  928. } else {
  929. print $langs->trans('MigrationContractsFieldDontExist')."<br>\n";
  930. // dol_print_error($db);
  931. }
  932. print '</td></tr>';
  933. }
  934. /**
  935. * Function to migrate links into llx_bank_url
  936. *
  937. * @param DoliDB $db Database handler
  938. * @param Translate $langs Object langs
  939. * @param Conf $conf Object conf
  940. * @return void
  941. */
  942. function migrate_links_transfert($db, $langs, $conf)
  943. {
  944. print '<tr><td colspan="4">';
  945. $nberr = 0;
  946. print '<br>';
  947. print '<b>'.$langs->trans('MigrationBankTransfertsUpdate')."</b><br>\n";
  948. $sql = "SELECT ba.rowid as barowid, bb.rowid as bbrowid";
  949. $sql .= " FROM ".MAIN_DB_PREFIX."bank as bb, ".MAIN_DB_PREFIX."bank as ba";
  950. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = ba.rowid";
  951. $sql .= " WHERE ba.amount = -bb.amount AND ba.fk_account <> bb.fk_account";
  952. $sql .= " AND ba.datev = bb.datev AND ba.datec = bb.datec";
  953. $sql .= " AND bu.fk_bank IS NULL";
  954. $resql = $db->query($sql);
  955. dolibarr_install_syslog("upgrade2::migrate_links_transfert");
  956. if ($resql) {
  957. $i = 0;
  958. $row = array();
  959. $num = $db->num_rows($resql);
  960. if ($num) {
  961. print $langs->trans('MigrationBankTransfertsToUpdate', $num)."<br>\n";
  962. $db->begin();
  963. while ($i < $num) {
  964. $obj = $db->fetch_object($resql);
  965. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
  966. $sql .= "fk_bank, url_id, url, label, type";
  967. $sql .= ")";
  968. $sql .= " VALUES (";
  969. $sql .= $obj->barowid.",".$obj->bbrowid.", '/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'";
  970. $sql .= ")";
  971. //print $sql.'<br>';
  972. dolibarr_install_syslog("migrate_links_transfert");
  973. if (!$db->query($sql)) {
  974. dol_print_error($db);
  975. $nberr++;
  976. }
  977. $i++;
  978. }
  979. if (!$nberr) {
  980. // $db->rollback();
  981. $db->commit();
  982. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  983. } else {
  984. $db->rollback();
  985. print $langs->trans('MigrationUpdateFailed').'<br>';
  986. }
  987. } else {
  988. print $langs->trans('MigrationBankTransfertsNothingToUpdate')."<br>\n";
  989. }
  990. } else {
  991. dol_print_error($db);
  992. }
  993. print '</td></tr>';
  994. }
  995. /**
  996. * Mise a jour des date de contrats non renseignees
  997. *
  998. * @param DoliDB $db Database handler
  999. * @param Translate $langs Object langs
  1000. * @param Conf $conf Object conf
  1001. * @return void
  1002. */
  1003. function migrate_contracts_date1($db, $langs, $conf)
  1004. {
  1005. print '<tr><td colspan="4">';
  1006. print '<br>';
  1007. print '<b>'.$langs->trans('MigrationContractsEmptyDatesUpdate')."</b><br>\n";
  1008. $sql = "update ".MAIN_DB_PREFIX."contrat set date_contrat=tms where date_contrat is null";
  1009. dolibarr_install_syslog("upgrade2::migrate_contracts_date1");
  1010. $resql = $db->query($sql);
  1011. if (!$resql) {
  1012. dol_print_error($db);
  1013. }
  1014. if ($db->affected_rows($resql) > 0) {
  1015. print $langs->trans('MigrationContractsEmptyDatesUpdateSuccess')."<br>\n";
  1016. } else {
  1017. print $langs->trans('MigrationContractsEmptyDatesNothingToUpdate')."<br>\n";
  1018. }
  1019. $sql = "update ".MAIN_DB_PREFIX."contrat set datec=tms where datec is null";
  1020. dolibarr_install_syslog("upgrade2::migrate_contracts_date1");
  1021. $resql = $db->query($sql);
  1022. if (!$resql) {
  1023. dol_print_error($db);
  1024. }
  1025. if ($db->affected_rows($resql) > 0) {
  1026. print $langs->trans('MigrationContractsEmptyCreationDatesUpdateSuccess')."<br>\n";
  1027. } else {
  1028. print $langs->trans('MigrationContractsEmptyCreationDatesNothingToUpdate')."<br>\n";
  1029. }
  1030. print '</td></tr>';
  1031. }
  1032. /**
  1033. * Update contracts with date min real if service date is lower
  1034. *
  1035. * @param DoliDB $db Database handler
  1036. * @param Translate $langs Language
  1037. * @param Conf $conf Conf
  1038. * @return void
  1039. */
  1040. function migrate_contracts_date2($db, $langs, $conf)
  1041. {
  1042. print '<tr><td colspan="4">';
  1043. $nberr = 0;
  1044. print '<br>';
  1045. print '<b>'.$langs->trans('MigrationContractsInvalidDatesUpdate')."</b><br>\n";
  1046. $sql = "SELECT c.rowid as cref, c.datec, c.date_contrat, MIN(cd.date_ouverture) as datemin";
  1047. $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c,";
  1048. $sql .= " ".MAIN_DB_PREFIX."contratdet as cd";
  1049. $sql .= " WHERE c.rowid=cd.fk_contrat AND cd.date_ouverture IS NOT NULL";
  1050. $sql .= " GROUP BY c.rowid, c.date_contrat";
  1051. $resql = $db->query($sql);
  1052. dolibarr_install_syslog("upgrade2::migrate_contracts_date2");
  1053. if ($resql) {
  1054. $i = 0;
  1055. $row = array();
  1056. $num = $db->num_rows($resql);
  1057. if ($num) {
  1058. $nbcontratsmodifie = 0;
  1059. $db->begin();
  1060. while ($i < $num) {
  1061. $obj = $db->fetch_object($resql);
  1062. if ($obj->date_contrat > $obj->datemin) {
  1063. $datemin = $db->jdate($obj->datemin);
  1064. print $langs->trans('MigrationContractsInvalidDateFix', $obj->cref, $obj->date_contrat, $obj->datemin)."<br>\n";
  1065. $sql = "UPDATE ".MAIN_DB_PREFIX."contrat";
  1066. $sql .= " SET date_contrat='".$db->idate($datemin)."'";
  1067. $sql .= " WHERE rowid = ".((int) $obj->cref);
  1068. $resql2 = $db->query($sql);
  1069. if (!$resql2) {
  1070. dol_print_error($db);
  1071. }
  1072. $nbcontratsmodifie++;
  1073. }
  1074. $i++;
  1075. }
  1076. $db->commit();
  1077. if ($nbcontratsmodifie) {
  1078. print $langs->trans('MigrationContractsInvalidDatesNumber', $nbcontratsmodifie)."<br>\n";
  1079. } else {
  1080. print $langs->trans('MigrationContractsInvalidDatesNothingToUpdate')."<br>\n";
  1081. }
  1082. }
  1083. } else {
  1084. dol_print_error($db);
  1085. }
  1086. print '</td></tr>';
  1087. }
  1088. /**
  1089. * Mise a jour des dates de creation de contrat
  1090. *
  1091. * @param DoliDB $db Database handler
  1092. * @param Translate $langs Object langs
  1093. * @param Conf $conf Object conf
  1094. * @return void
  1095. */
  1096. function migrate_contracts_date3($db, $langs, $conf)
  1097. {
  1098. print '<tr><td colspan="4">';
  1099. print '<br>';
  1100. print '<b>'.$langs->trans('MigrationContractsIncoherentCreationDateUpdate')."</b><br>\n";
  1101. $sql = "update ".MAIN_DB_PREFIX."contrat set datec=date_contrat where datec is null or datec > date_contrat";
  1102. dolibarr_install_syslog("upgrade2::migrate_contracts_date3");
  1103. $resql = $db->query($sql);
  1104. if (!$resql) {
  1105. dol_print_error($db);
  1106. }
  1107. if ($db->affected_rows($resql) > 0) {
  1108. print $langs->trans('MigrationContractsIncoherentCreationDateUpdateSuccess')."<br>\n";
  1109. } else {
  1110. print $langs->trans('MigrationContractsIncoherentCreationDateNothingToUpdate')."<br>\n";
  1111. }
  1112. print '</td></tr>';
  1113. }
  1114. /**
  1115. * Reouverture des contrats qui ont au moins une ligne non fermee
  1116. *
  1117. * @param DoliDB $db Database handler
  1118. * @param Translate $langs Object langs
  1119. * @param Conf $conf Object conf
  1120. * @return void
  1121. */
  1122. function migrate_contracts_open($db, $langs, $conf)
  1123. {
  1124. print '<tr><td colspan="4">';
  1125. print '<br>';
  1126. print '<b>'.$langs->trans('MigrationReopeningContracts')."</b><br>\n";
  1127. $sql = "SELECT c.rowid as cref FROM ".MAIN_DB_PREFIX."contrat as c, ".MAIN_DB_PREFIX."contratdet as cd";
  1128. $sql .= " WHERE cd.statut = 4 AND c.statut=2 AND c.rowid=cd.fk_contrat";
  1129. dolibarr_install_syslog("upgrade2::migrate_contracts_open");
  1130. $resql = $db->query($sql);
  1131. if (!$resql) {
  1132. dol_print_error($db);
  1133. }
  1134. if ($db->affected_rows($resql) > 0) {
  1135. $i = 0;
  1136. $row = array();
  1137. $num = $db->num_rows($resql);
  1138. if ($num) {
  1139. $nbcontratsmodifie = 0;
  1140. $db->begin();
  1141. while ($i < $num) {
  1142. $obj = $db->fetch_object($resql);
  1143. print $langs->trans('MigrationReopenThisContract', $obj->cref)."<br>\n";
  1144. $sql = "UPDATE ".MAIN_DB_PREFIX."contrat";
  1145. $sql .= " SET statut = 1";
  1146. $sql .= " WHERE rowid = ".((int) $obj->cref);
  1147. $resql2 = $db->query($sql);
  1148. if (!$resql2) {
  1149. dol_print_error($db);
  1150. }
  1151. $nbcontratsmodifie++;
  1152. $i++;
  1153. }
  1154. $db->commit();
  1155. if ($nbcontratsmodifie) {
  1156. print $langs->trans('MigrationReopenedContractsNumber', $nbcontratsmodifie)."<br>\n";
  1157. } else {
  1158. print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
  1159. }
  1160. }
  1161. } else {
  1162. print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
  1163. }
  1164. print '</td></tr>';
  1165. }
  1166. /**
  1167. * Factures fournisseurs
  1168. *
  1169. * @param DoliDB $db Database handler
  1170. * @param Translate $langs Object langs
  1171. * @param Conf $conf Object conf
  1172. * @return void
  1173. */
  1174. function migrate_paiementfourn_facturefourn($db, $langs, $conf)
  1175. {
  1176. global $bc;
  1177. print '<tr><td colspan="4">';
  1178. print '<br>';
  1179. print '<b>'.$langs->trans('SuppliersInvoices')."</b><br>\n";
  1180. print '</td></tr>';
  1181. $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiementfourn", "fk_facture_fourn");
  1182. $obj = $db->fetch_object($result);
  1183. if ($obj) {
  1184. $error = 0;
  1185. $nb = 0;
  1186. $select_sql = 'SELECT rowid, fk_facture_fourn, amount';
  1187. $select_sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn';
  1188. $select_sql .= ' WHERE fk_facture_fourn IS NOT NULL';
  1189. dolibarr_install_syslog("upgrade2::migrate_paiementfourn_facturefourn");
  1190. $select_resql = $db->query($select_sql);
  1191. if ($select_resql) {
  1192. $select_num = $db->num_rows($select_resql);
  1193. $i = 0;
  1194. // Pour chaque paiement fournisseur, on insere une ligne dans paiementfourn_facturefourn
  1195. while (($i < $select_num) && (!$error)) {
  1196. $select_obj = $db->fetch_object($select_resql);
  1197. // Verifier si la ligne est deja dans la nouvelle table. On ne veut pas inserer de doublons.
  1198. $check_sql = 'SELECT fk_paiementfourn, fk_facturefourn';
  1199. $check_sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn';
  1200. $check_sql .= ' WHERE fk_paiementfourn = '.((int) $select_obj->rowid).' AND fk_facturefourn = '.((int) $select_obj->fk_facture_fourn);
  1201. $check_resql = $db->query($check_sql);
  1202. if ($check_resql) {
  1203. $check_num = $db->num_rows($check_resql);
  1204. if ($check_num == 0) {
  1205. $db->begin();
  1206. if ($nb == 0) {
  1207. print '<tr><td colspan="4" class="nowrap"><b>'.$langs->trans('SuppliersInvoices').'</b></td></tr>';
  1208. print '<tr><td>fk_paiementfourn</td><td>fk_facturefourn</td><td>'.$langs->trans('Amount').'</td><td>&nbsp;</td></tr>';
  1209. }
  1210. print '<tr class="oddeven">';
  1211. print '<td>'.$select_obj->rowid.'</td><td>'.$select_obj->fk_facture_fourn.'</td><td>'.$select_obj->amount.'</td>';
  1212. $insert_sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn SET ';
  1213. $insert_sql .= ' fk_paiementfourn = \''.$select_obj->rowid.'\',';
  1214. $insert_sql .= ' fk_facturefourn = \''.$select_obj->fk_facture_fourn.'\',';
  1215. $insert_sql .= ' amount = \''.$select_obj->amount.'\'';
  1216. $insert_resql = $db->query($insert_sql);
  1217. if ($insert_resql) {
  1218. $nb++;
  1219. print '<td><span class="ok">'.$langs->trans("OK").'</span></td>';
  1220. } else {
  1221. print '<td><span class="error">Error on insert</span></td>';
  1222. $error++;
  1223. }
  1224. print '</tr>';
  1225. }
  1226. } else {
  1227. $error++;
  1228. }
  1229. $i++;
  1230. }
  1231. } else {
  1232. $error++;
  1233. }
  1234. if (!$error) {
  1235. if (!$nb) {
  1236. print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
  1237. }
  1238. $db->commit();
  1239. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiementfourn DROP COLUMN fk_facture_fourn";
  1240. $db->query($sql);
  1241. } else {
  1242. print '<tr><td>'.$langs->trans("Error").'</td></tr>';
  1243. $db->rollback();
  1244. }
  1245. } else {
  1246. print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
  1247. }
  1248. }
  1249. /**
  1250. * Update total of invoice lines
  1251. *
  1252. * @param DoliDB $db Database handler
  1253. * @param Translate $langs Object langs
  1254. * @param Conf $conf Object conf
  1255. * @return void
  1256. */
  1257. function migrate_price_facture($db, $langs, $conf)
  1258. {
  1259. $err = 0;
  1260. $tmpmysoc = new Societe($db);
  1261. $tmpmysoc->setMysoc($conf);
  1262. $db->begin();
  1263. print '<tr><td colspan="4">';
  1264. print '<br>';
  1265. print '<b>'.$langs->trans('MigrationInvoice')."</b><br>\n";
  1266. // List of invoice lines not up to date
  1267. $sql = "SELECT fd.rowid, fd.qty, fd.subprice, fd.remise_percent, fd.tva_tx as vatrate, fd.total_ttc, fd.info_bits,";
  1268. $sql .= " f.rowid as facid, f.remise_percent as remise_percent_global, f.total_ttc as total_ttc_f";
  1269. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd, ".MAIN_DB_PREFIX."facture as f";
  1270. $sql .= " WHERE fd.fk_facture = f.rowid";
  1271. $sql .= " AND (((fd.total_ttc = 0 AND fd.remise_percent != 100) or fd.total_ttc IS NULL) or f.total_ttc IS NULL)";
  1272. //print $sql;
  1273. dolibarr_install_syslog("upgrade2::migrate_price_facture");
  1274. $resql = $db->query($sql);
  1275. if ($resql) {
  1276. $num = $db->num_rows($resql);
  1277. $i = 0;
  1278. if ($num) {
  1279. while ($i < $num) {
  1280. $obj = $db->fetch_object($resql);
  1281. $rowid = $obj->rowid;
  1282. $qty = $obj->qty;
  1283. $pu = $obj->subprice;
  1284. $vatrate = $obj->vatrate;
  1285. $remise_percent = $obj->remise_percent;
  1286. $remise_percent_global = $obj->remise_percent_global;
  1287. $total_ttc_f = $obj->total_ttc_f;
  1288. $info_bits = $obj->info_bits;
  1289. // On met a jour les 3 nouveaux champs
  1290. $facligne = new FactureLigne($db);
  1291. $facligne->fetch($rowid);
  1292. $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $facligne->product_type, $tmpmysoc);
  1293. $total_ht = $result[0];
  1294. $total_tva = $result[1];
  1295. $total_ttc = $result[2];
  1296. $facligne->total_ht = $total_ht;
  1297. $facligne->total_tva = $total_tva;
  1298. $facligne->total_ttc = $total_ttc;
  1299. dolibarr_install_syslog("upgrade2: line ".$rowid.": facid=".$obj->facid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." remise_global=".$remise_percent_global." -> ".$total_ht.", ".$total_tva.", ".$total_ttc);
  1300. print ". ";
  1301. $facligne->update_total();
  1302. /* On touche a facture mere uniquement si total_ttc = 0 */
  1303. if (!$total_ttc_f) {
  1304. $facture = new Facture($db);
  1305. $facture->id = $obj->facid;
  1306. if ($facture->fetch($facture->id) >= 0) {
  1307. if ($facture->update_price() > 0) {
  1308. //print $facture->id;
  1309. } else {
  1310. print "Error id=".$facture->id;
  1311. $err++;
  1312. }
  1313. } else {
  1314. print "Error #3";
  1315. $err++;
  1316. }
  1317. }
  1318. print " ";
  1319. $i++;
  1320. }
  1321. } else {
  1322. print $langs->trans("AlreadyDone");
  1323. }
  1324. $db->free($resql);
  1325. $db->commit();
  1326. } else {
  1327. print "Error #1 ".$db->error();
  1328. $err++;
  1329. $db->rollback();
  1330. }
  1331. print '<br>';
  1332. print '</td></tr>';
  1333. }
  1334. /**
  1335. * Update total of proposal lines
  1336. *
  1337. * @param DoliDB $db Database handler
  1338. * @param Translate $langs Object langs
  1339. * @param Conf $conf Object conf
  1340. * @return void
  1341. */
  1342. function migrate_price_propal($db, $langs, $conf)
  1343. {
  1344. $tmpmysoc = new Societe($db);
  1345. $tmpmysoc->setMysoc($conf);
  1346. $db->begin();
  1347. print '<tr><td colspan="4">';
  1348. print '<br>';
  1349. print '<b>'.$langs->trans('MigrationProposal')."</b><br>\n";
  1350. // List of proposal lines not up to date
  1351. $sql = "SELECT pd.rowid, pd.qty, pd.subprice, pd.remise_percent, pd.tva_tx as vatrate, pd.info_bits,";
  1352. $sql .= " p.rowid as propalid, p.remise_percent as remise_percent_global";
  1353. $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."propal as p";
  1354. $sql .= " WHERE pd.fk_propal = p.rowid";
  1355. $sql .= " AND ((pd.total_ttc = 0 AND pd.remise_percent != 100) or pd.total_ttc IS NULL)";
  1356. dolibarr_install_syslog("upgrade2::migrate_price_propal");
  1357. $resql = $db->query($sql);
  1358. if ($resql) {
  1359. $num = $db->num_rows($resql);
  1360. $i = 0;
  1361. if ($num) {
  1362. while ($i < $num) {
  1363. $obj = $db->fetch_object($resql);
  1364. $rowid = $obj->rowid;
  1365. $qty = $obj->qty;
  1366. $pu = $obj->subprice;
  1367. $vatrate = $obj->vatrate;
  1368. $remise_percent = $obj->remise_percent;
  1369. $remise_percent_global = $obj->remise_percent_global;
  1370. $info_bits = $obj->info_bits;
  1371. // On met a jour les 3 nouveaux champs
  1372. $propalligne = new PropaleLigne($db);
  1373. $propalligne->fetch($rowid);
  1374. $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $propalligne->product_type, $tmpmysoc);
  1375. $total_ht = $result[0];
  1376. $total_tva = $result[1];
  1377. $total_ttc = $result[2];
  1378. $propalligne->total_ht = $total_ht;
  1379. $propalligne->total_tva = $total_tva;
  1380. $propalligne->total_ttc = $total_ttc;
  1381. dolibarr_install_syslog("upgrade2: Line ".$rowid.": propalid=".$obj->rowid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." remise_global=".$remise_percent_global." -> ".$total_ht.", ".$total_tva.", ".$total_ttc);
  1382. print ". ";
  1383. $propalligne->update_total();
  1384. /* On touche pas a propal mere
  1385. $propal = new Propal($db);
  1386. $propal->id=$obj->rowid;
  1387. if ( $propal->fetch($propal->id) >= 0 )
  1388. {
  1389. if ( $propal->update_price() > 0 )
  1390. {
  1391. print ". ";
  1392. }
  1393. else
  1394. {
  1395. print "Error id=".$propal->id;
  1396. }
  1397. }
  1398. else
  1399. {
  1400. print "Error #3";
  1401. }
  1402. */
  1403. $i++;
  1404. }
  1405. } else {
  1406. print $langs->trans("AlreadyDone");
  1407. }
  1408. $db->free($resql);
  1409. $db->commit();
  1410. } else {
  1411. print "Error #1 ".$db->error();
  1412. $db->rollback();
  1413. }
  1414. print '<br>';
  1415. print '</td></tr>';
  1416. }
  1417. /**
  1418. * Update total of contract lines
  1419. *
  1420. * @param DoliDB $db Database handler
  1421. * @param Translate $langs Object langs
  1422. * @param Conf $conf Object conf
  1423. * @return void
  1424. */
  1425. function migrate_price_contrat($db, $langs, $conf)
  1426. {
  1427. $db->begin();
  1428. $tmpmysoc = new Societe($db);
  1429. $tmpmysoc->setMysoc($conf);
  1430. if (empty($tmpmysoc->country_id)) {
  1431. $tmpmysoc->country_id = 0; // Ti not have this set to '' or will make sql syntax error.
  1432. }
  1433. print '<tr><td colspan="4">';
  1434. print '<br>';
  1435. print '<b>'.$langs->trans('MigrationContract')."</b><br>\n";
  1436. // List of contract lines not up to date
  1437. $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
  1438. $sql .= " c.rowid as contratid";
  1439. $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c";
  1440. $sql .= " WHERE cd.fk_contrat = c.rowid";
  1441. $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100 AND cd.subprice > 0) or cd.total_ttc IS NULL)";
  1442. dolibarr_install_syslog("upgrade2::migrate_price_contrat");
  1443. $resql = $db->query($sql);
  1444. if ($resql) {
  1445. $num = $db->num_rows($resql);
  1446. $i = 0;
  1447. if ($num) {
  1448. while ($i < $num) {
  1449. $obj = $db->fetch_object($resql);
  1450. $rowid = $obj->rowid;
  1451. $qty = $obj->qty;
  1452. $pu = $obj->subprice;
  1453. $vatrate = $obj->vatrate;
  1454. $remise_percent = $obj->remise_percent;
  1455. $info_bits = $obj->info_bits;
  1456. // On met a jour les 3 nouveaux champs
  1457. $contratligne = new ContratLigne($db);
  1458. //$contratligne->fetch($rowid); Non requis car le update_total ne met a jour que chp redefinis
  1459. $contratligne->fetch($rowid);
  1460. $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, 0, 'HT', $info_bits, $contratligne->product_type, $tmpmysoc);
  1461. $total_ht = $result[0];
  1462. $total_tva = $result[1];
  1463. $total_ttc = $result[2];
  1464. $contratligne->total_ht = $total_ht;
  1465. $contratligne->total_tva = $total_tva;
  1466. $contratligne->total_ttc = $total_ttc;
  1467. dolibarr_install_syslog("upgrade2: Line ".$rowid.": contratdetid=".$obj->rowid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." -> ".$total_ht.", ".$total_tva." , ".$total_ttc);
  1468. print ". ";
  1469. $contratligne->update_total();
  1470. $i++;
  1471. }
  1472. } else {
  1473. print $langs->trans("AlreadyDone");
  1474. }
  1475. $db->free($resql);
  1476. $db->commit();
  1477. } else {
  1478. print "Error #1 ".$db->error();
  1479. $db->rollback();
  1480. }
  1481. print '<br>';
  1482. print '</td></tr>';
  1483. }
  1484. /**
  1485. * Update total of sales order lines
  1486. *
  1487. * @param DoliDB $db Database handler
  1488. * @param Translate $langs Object langs
  1489. * @param Conf $conf Object conf
  1490. * @return void
  1491. */
  1492. function migrate_price_commande($db, $langs, $conf)
  1493. {
  1494. $db->begin();
  1495. $tmpmysoc = new Societe($db);
  1496. $tmpmysoc->setMysoc($conf);
  1497. print '<tr><td colspan="4">';
  1498. print '<br>';
  1499. print '<b>'.$langs->trans('MigrationOrder')."</b><br>\n";
  1500. // List of sales orders lines not up to date
  1501. $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
  1502. $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
  1503. $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."commande as c";
  1504. $sql .= " WHERE cd.fk_commande = c.rowid";
  1505. $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
  1506. dolibarr_install_syslog("upgrade2::migrate_price_commande");
  1507. $resql = $db->query($sql);
  1508. if ($resql) {
  1509. $num = $db->num_rows($resql);
  1510. $i = 0;
  1511. if ($num) {
  1512. while ($i < $num) {
  1513. $obj = $db->fetch_object($resql);
  1514. $rowid = $obj->rowid;
  1515. $qty = $obj->qty;
  1516. $pu = $obj->subprice;
  1517. $vatrate = $obj->vatrate;
  1518. $remise_percent = $obj->remise_percent;
  1519. $remise_percent_global = $obj->remise_percent_global;
  1520. $info_bits = $obj->info_bits;
  1521. // On met a jour les 3 nouveaux champs
  1522. $commandeligne = new OrderLine($db);
  1523. $commandeligne->fetch($rowid);
  1524. $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $commandeligne->product_type, $tmpmysoc);
  1525. $total_ht = $result[0];
  1526. $total_tva = $result[1];
  1527. $total_ttc = $result[2];
  1528. $commandeligne->total_ht = $total_ht;
  1529. $commandeligne->total_tva = $total_tva;
  1530. $commandeligne->total_ttc = $total_ttc;
  1531. dolibarr_install_syslog("upgrade2: Line ".$rowid." : commandeid=".$obj->rowid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." remise_global=".$remise_percent_global." -> ".$total_ht.", ".$total_tva.", ".$total_ttc);
  1532. print ". ";
  1533. $commandeligne->update_total();
  1534. /* On touche pas a facture mere
  1535. $commande = new Commande($db);
  1536. $commande->id = $obj->rowid;
  1537. if ( $commande->fetch($commande->id) >= 0 )
  1538. {
  1539. if ( $commande->update_price() > 0 )
  1540. {
  1541. print ". ";
  1542. }
  1543. else
  1544. {
  1545. print "Error id=".$commande->id;
  1546. }
  1547. }
  1548. else
  1549. {
  1550. print "Error #3";
  1551. }
  1552. */
  1553. $i++;
  1554. }
  1555. } else {
  1556. print $langs->trans("AlreadyDone");
  1557. }
  1558. $db->free($resql);
  1559. /*
  1560. $sql = "DELETE FROM ".MAIN_DB_PREFIX."commandedet";
  1561. $sql.= " WHERE price = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0 AND remise_percent = 0";
  1562. $resql=$db->query($sql);
  1563. if (! $resql)
  1564. {
  1565. dol_print_error($db);
  1566. }
  1567. */
  1568. $db->commit();
  1569. } else {
  1570. print "Error #1 ".$db->error();
  1571. $db->rollback();
  1572. }
  1573. print '<br>';
  1574. print '</td></tr>';
  1575. }
  1576. /**
  1577. * Update total of purchase order lines
  1578. *
  1579. * @param DoliDB $db Database handler
  1580. * @param Translate $langs Object langs
  1581. * @param Conf $conf Object conf
  1582. * @return void
  1583. */
  1584. function migrate_price_commande_fournisseur($db, $langs, $conf)
  1585. {
  1586. $db->begin();
  1587. $tmpmysoc = new Societe($db);
  1588. $tmpmysoc->setMysoc($conf);
  1589. print '<tr><td colspan="4">';
  1590. print '<br>';
  1591. print '<b>'.$langs->trans('MigrationSupplierOrder')."</b><br>\n";
  1592. // List of purchase order lines not up to date
  1593. $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
  1594. $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
  1595. $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd, ".MAIN_DB_PREFIX."commande_fournisseur as c";
  1596. $sql .= " WHERE cd.fk_commande = c.rowid";
  1597. $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
  1598. dolibarr_install_syslog("upgrade2::migrate_price_commande_fournisseur");
  1599. $resql = $db->query($sql);
  1600. if ($resql) {
  1601. $num = $db->num_rows($resql);
  1602. $i = 0;
  1603. if ($num) {
  1604. while ($i < $num) {
  1605. $obj = $db->fetch_object($resql);
  1606. $rowid = $obj->rowid;
  1607. $qty = $obj->qty;
  1608. $pu = $obj->subprice;
  1609. $vatrate = $obj->vatrate;
  1610. $remise_percent = $obj->remise_percent;
  1611. $remise_percent_global = $obj->remise_percent_global;
  1612. $info_bits = $obj->info_bits;
  1613. // On met a jour les 3 nouveaux champs
  1614. $commandeligne = new CommandeFournisseurLigne($db);
  1615. $commandeligne->fetch($rowid);
  1616. $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $commandeligne->product_type, $tmpsoc);
  1617. $total_ht = $result[0];
  1618. $total_tva = $result[1];
  1619. $total_ttc = $result[2];
  1620. $commandeligne->total_ht = $total_ht;
  1621. $commandeligne->total_tva = $total_tva;
  1622. $commandeligne->total_ttc = $total_ttc;
  1623. dolibarr_install_syslog("upgrade2: Line ".$rowid.": commandeid=".$obj->rowid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." remise_global=".$remise_percent_global." -> ".$total_ht.", ".$total_tva.", ".$total_ttc);
  1624. print ". ";
  1625. $commandeligne->update_total();
  1626. /* On touche pas a facture mere
  1627. $commande = new Commande($db);
  1628. $commande->id = $obj->rowid;
  1629. if ( $commande->fetch($commande->id) >= 0 )
  1630. {
  1631. if ( $commande->update_price() > 0 )
  1632. {
  1633. print ". ";
  1634. }
  1635. else
  1636. {
  1637. print "Error id=".$commande->id;
  1638. }
  1639. }
  1640. else
  1641. {
  1642. print "Error #3";
  1643. }
  1644. */
  1645. $i++;
  1646. }
  1647. } else {
  1648. print $langs->trans("AlreadyDone");
  1649. }
  1650. $db->free($resql);
  1651. /*
  1652. $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet";
  1653. $sql.= " WHERE subprice = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0";
  1654. $resql=$db->query($sql);
  1655. if (! $resql)
  1656. {
  1657. dol_print_error($db);
  1658. }
  1659. */
  1660. $db->commit();
  1661. } else {
  1662. print "Error #1 ".$db->error();
  1663. $db->rollback();
  1664. }
  1665. print '<br>';
  1666. print '</td></tr>';
  1667. }
  1668. /**
  1669. * Mise a jour des modeles selectionnes
  1670. *
  1671. * @param DoliDB $db Database handler
  1672. * @param Translate $langs Object langs
  1673. * @param Conf $conf Object conf
  1674. * @return void
  1675. */
  1676. function migrate_modeles($db, $langs, $conf)
  1677. {
  1678. //print '<br>';
  1679. //print '<b>'.$langs->trans('UpdateModelsTable')."</b><br>\n";
  1680. dolibarr_install_syslog("upgrade2::migrate_modeles");
  1681. if (isModEnabled('facture')) {
  1682. include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  1683. $modellist = ModelePDFFactures::liste_modeles($db);
  1684. if (count($modellist) == 0) {
  1685. // Aucun model par defaut.
  1686. $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('crabe','invoice')";
  1687. $resql = $db->query($sql);
  1688. if (!$resql) {
  1689. dol_print_error($db);
  1690. }
  1691. }
  1692. }
  1693. if (isModEnabled('commande')) {
  1694. include_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
  1695. $modellist = ModelePDFCommandes::liste_modeles($db);
  1696. if (count($modellist) == 0) {
  1697. // Aucun model par defaut.
  1698. $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('einstein','order')";
  1699. $resql = $db->query($sql);
  1700. if (!$resql) {
  1701. dol_print_error($db);
  1702. }
  1703. }
  1704. }
  1705. if (isModEnabled("expedition")) {
  1706. include_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
  1707. $modellist = ModelePDFExpedition::liste_modeles($db);
  1708. if (count($modellist) == 0) {
  1709. // Aucun model par defaut.
  1710. $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('rouget','shipping')";
  1711. $resql = $db->query($sql);
  1712. if (!$resql) {
  1713. dol_print_error($db);
  1714. }
  1715. }
  1716. }
  1717. //print $langs->trans("AlreadyDone");
  1718. }
  1719. /**
  1720. * Correspondance des expeditions et des commandes clients dans la table llx_co_exp
  1721. *
  1722. * @param DoliDB $db Database handler
  1723. * @param Translate $langs Object langs
  1724. * @param Conf $conf Object conf
  1725. * @return void
  1726. */
  1727. function migrate_commande_expedition($db, $langs, $conf)
  1728. {
  1729. dolibarr_install_syslog("upgrade2::migrate_commande_expedition");
  1730. print '<tr><td colspan="4">';
  1731. print '<br>';
  1732. print '<b>'.$langs->trans('MigrationShipmentOrderMatching')."</b><br>\n";
  1733. $result = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "fk_commande");
  1734. $obj = $db->fetch_object($result);
  1735. if ($obj) {
  1736. $error = 0;
  1737. $db->begin();
  1738. $sql = "SELECT e.rowid, e.fk_commande FROM ".MAIN_DB_PREFIX."expedition as e";
  1739. $resql = $db->query($sql);
  1740. if ($resql) {
  1741. $i = 0;
  1742. $num = $db->num_rows($resql);
  1743. if ($num) {
  1744. while ($i < $num) {
  1745. $obj = $db->fetch_object($resql);
  1746. $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_exp (fk_expedition,fk_commande)";
  1747. $sql .= " VALUES (".((int) $obj->rowid).", ".((int) $obj->fk_commande).")";
  1748. $resql2 = $db->query($sql);
  1749. if (!$resql2) {
  1750. $error++;
  1751. dol_print_error($db);
  1752. }
  1753. print ". ";
  1754. $i++;
  1755. }
  1756. }
  1757. if ($error == 0) {
  1758. $db->commit();
  1759. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."expedition DROP COLUMN fk_commande";
  1760. print $langs->trans('FieldRenamed')."<br>\n";
  1761. $db->query($sql);
  1762. } else {
  1763. $db->rollback();
  1764. }
  1765. } else {
  1766. dol_print_error($db);
  1767. $db->rollback();
  1768. }
  1769. } else {
  1770. print $langs->trans('AlreadyDone')."<br>\n";
  1771. }
  1772. print '</td></tr>';
  1773. }
  1774. /**
  1775. * Correspondance des livraisons et des commandes clients dans la table llx_co_liv
  1776. *
  1777. * @param DoliDB $db Database handler
  1778. * @param Translate $langs Object langs
  1779. * @param Conf $conf Object conf
  1780. * @return void
  1781. */
  1782. function migrate_commande_livraison($db, $langs, $conf)
  1783. {
  1784. dolibarr_install_syslog("upgrade2::migrate_commande_livraison");
  1785. print '<tr><td colspan="4">';
  1786. print '<br>';
  1787. print '<b>'.$langs->trans('MigrationDeliveryOrderMatching')."</b><br>\n";
  1788. $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison", "fk_commande");
  1789. $obj = $db->fetch_object($result);
  1790. if ($obj) {
  1791. $error = 0;
  1792. $db->begin();
  1793. $sql = "SELECT l.rowid, l.fk_commande,";
  1794. $sql .= " c.ref_client, c.date_livraison as delivery_date";
  1795. $sql .= " FROM ".MAIN_DB_PREFIX."livraison as l, ".MAIN_DB_PREFIX."commande as c";
  1796. $sql .= " WHERE c.rowid = l.fk_commande";
  1797. $resql = $db->query($sql);
  1798. if ($resql) {
  1799. $i = 0;
  1800. $num = $db->num_rows($resql);
  1801. if ($num) {
  1802. while ($i < $num) {
  1803. $obj = $db->fetch_object($resql);
  1804. $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_liv (fk_livraison,fk_commande)";
  1805. $sql .= " VALUES (".((int) $obj->rowid).", ".((int) $obj->fk_commande).")";
  1806. $resql2 = $db->query($sql);
  1807. if ($resql2) {
  1808. $delivery_date = $db->jdate($obj->delivery_date);
  1809. $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
  1810. $sqlu .= " ref_client = '".$db->escape($obj->ref_client)."'";
  1811. $sqlu .= ", date_livraison = '".$db->idate($delivery_date)."'";
  1812. $sqlu .= " WHERE rowid = ".((int) $obj->rowid);
  1813. $resql3 = $db->query($sqlu);
  1814. if (!$resql3) {
  1815. $error++;
  1816. dol_print_error($db);
  1817. }
  1818. } else {
  1819. $error++;
  1820. dol_print_error($db);
  1821. }
  1822. print ". ";
  1823. $i++;
  1824. }
  1825. }
  1826. if ($error == 0) {
  1827. $db->commit();
  1828. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_commande";
  1829. print $langs->trans('FieldRenamed')."<br>\n";
  1830. $db->query($sql);
  1831. } else {
  1832. $db->rollback();
  1833. }
  1834. } else {
  1835. dol_print_error($db);
  1836. $db->rollback();
  1837. }
  1838. } else {
  1839. print $langs->trans('AlreadyDone')."<br>\n";
  1840. }
  1841. print '</td></tr>';
  1842. }
  1843. /**
  1844. * Migration des details commandes dans les details livraisons
  1845. *
  1846. * @param DoliDB $db Database handler
  1847. * @param Translate $langs Object langs
  1848. * @param Conf $conf Object conf
  1849. * @return void
  1850. */
  1851. function migrate_detail_livraison($db, $langs, $conf)
  1852. {
  1853. dolibarr_install_syslog("upgrade2::migrate_detail_livraison");
  1854. print '<tr><td colspan="4">';
  1855. print '<br>';
  1856. print '<b>'.$langs->trans('MigrationDeliveryDetail')."</b><br>\n";
  1857. // This is done if field fk_commande_ligne exists.
  1858. // If not this means migration was already done.
  1859. $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet", "fk_commande_ligne");
  1860. $obj = $db->fetch_object($result);
  1861. if ($obj) {
  1862. $error = 0;
  1863. $db->begin();
  1864. $sql = "SELECT cd.rowid, cd.fk_product, cd.description, cd.subprice, cd.total_ht";
  1865. $sql .= ", ld.fk_livraison";
  1866. $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."livraisondet as ld";
  1867. $sql .= " WHERE ld.fk_commande_ligne = cd.rowid";
  1868. $resql = $db->query($sql);
  1869. if ($resql) {
  1870. $i = 0;
  1871. $num = $db->num_rows($resql);
  1872. if ($num) {
  1873. while ($i < $num) {
  1874. $obj = $db->fetch_object($resql);
  1875. $sql = "UPDATE ".MAIN_DB_PREFIX."livraisondet SET";
  1876. $sql .= " fk_product = ".((int) $obj->fk_product);
  1877. $sql .= ",description = '".$db->escape($obj->description)."'";
  1878. $sql .= ",subprice = ".price2num($obj->subprice);
  1879. $sql .= ",total_ht = ".price2num($obj->total_ht);
  1880. $sql .= " WHERE fk_commande_ligne = ".((int) $obj->rowid);
  1881. $resql2 = $db->query($sql);
  1882. if ($resql2) {
  1883. $sql = "SELECT total_ht";
  1884. $sql .= " FROM ".MAIN_DB_PREFIX."livraison";
  1885. $sql .= " WHERE rowid = ".((int) $obj->fk_livraison);
  1886. $resql3 = $db->query($sql);
  1887. if ($resql3) {
  1888. $obju = $db->fetch_object($resql3);
  1889. $total_ht = $obju->total_ht + $obj->total_ht;
  1890. $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
  1891. $sqlu .= " total_ht = ".price2num($total_ht, 'MT');
  1892. $sqlu .= " WHERE rowid = ".((int) $obj->fk_livraison);
  1893. $resql4 = $db->query($sqlu);
  1894. if (!$resql4) {
  1895. $error++;
  1896. dol_print_error($db);
  1897. }
  1898. } else {
  1899. $error++;
  1900. dol_print_error($db);
  1901. }
  1902. } else {
  1903. $error++;
  1904. dol_print_error($db);
  1905. }
  1906. print ". ";
  1907. $i++;
  1908. }
  1909. }
  1910. if ($error == 0) {
  1911. $db->commit();
  1912. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet CHANGE fk_commande_ligne fk_origin_line integer";
  1913. print $langs->trans('FieldRenamed')."<br>\n";
  1914. $db->query($sql);
  1915. } else {
  1916. $db->rollback();
  1917. }
  1918. } else {
  1919. dol_print_error($db);
  1920. $db->rollback();
  1921. }
  1922. } else {
  1923. $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet", "fk_origin_line");
  1924. $obj = $db->fetch_object($result);
  1925. if (!$obj) {
  1926. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet ADD COLUMN fk_origin_line integer after fk_livraison";
  1927. $db->query($sql);
  1928. }
  1929. print $langs->trans('AlreadyDone')."<br>\n";
  1930. }
  1931. print '</td></tr>';
  1932. }
  1933. /**
  1934. * Migration du champ stock dans produits
  1935. *
  1936. * @param DoliDB $db Database handler
  1937. * @param Translate $langs Object langs
  1938. * @param Conf $conf Object conf
  1939. * @return void
  1940. */
  1941. function migrate_stocks($db, $langs, $conf)
  1942. {
  1943. dolibarr_install_syslog("upgrade2::migrate_stocks");
  1944. print '<tr><td colspan="4">';
  1945. print '<br>';
  1946. print '<b>'.$langs->trans('MigrationStockDetail')."</b><br>\n";
  1947. $error = 0;
  1948. $db->begin();
  1949. $sql = "SELECT SUM(reel) as total, fk_product";
  1950. $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps";
  1951. $sql .= " GROUP BY fk_product";
  1952. $resql = $db->query($sql);
  1953. if ($resql) {
  1954. $i = 0;
  1955. $num = $db->num_rows($resql);
  1956. if ($num) {
  1957. while ($i < $num) {
  1958. $obj = $db->fetch_object($resql);
  1959. $sql = "UPDATE ".MAIN_DB_PREFIX."product SET";
  1960. $sql .= " stock = ".price2num($obj->total, 'MS');
  1961. $sql .= " WHERE rowid = ".((int) $obj->fk_product);
  1962. $resql2 = $db->query($sql);
  1963. if ($resql2) {
  1964. } else {
  1965. $error++;
  1966. dol_print_error($db);
  1967. }
  1968. print ". ";
  1969. $i++;
  1970. }
  1971. }
  1972. if ($error == 0) {
  1973. $db->commit();
  1974. } else {
  1975. $db->rollback();
  1976. }
  1977. } else {
  1978. dol_print_error($db);
  1979. $db->rollback();
  1980. }
  1981. print '</td></tr>';
  1982. }
  1983. /**
  1984. * Migration of menus (use only 1 table instead of 3)
  1985. * 2.6 -> 2.7
  1986. *
  1987. * @param DoliDB $db Database handler
  1988. * @param Translate $langs Object langs
  1989. * @param Conf $conf Object conf
  1990. * @return void
  1991. */
  1992. function migrate_menus($db, $langs, $conf)
  1993. {
  1994. dolibarr_install_syslog("upgrade2::migrate_menus");
  1995. print '<tr><td colspan="4">';
  1996. print '<br>';
  1997. print '<b>'.$langs->trans('MigrationMenusDetail')."</b><br>\n";
  1998. $error = 0;
  1999. if ($db->DDLInfoTable(MAIN_DB_PREFIX."menu_constraint")) {
  2000. $db->begin();
  2001. $sql = "SELECT m.rowid, mc.action";
  2002. $sql .= " FROM ".MAIN_DB_PREFIX."menu_constraint as mc, ".MAIN_DB_PREFIX."menu_const as md, ".MAIN_DB_PREFIX."menu as m";
  2003. $sql .= " WHERE md.fk_menu = m.rowid AND md.fk_constraint = mc.rowid";
  2004. $sql .= " AND m.enabled = '1'";
  2005. $resql = $db->query($sql);
  2006. if ($resql) {
  2007. $i = 0;
  2008. $num = $db->num_rows($resql);
  2009. if ($num) {
  2010. while ($i < $num) {
  2011. $obj = $db->fetch_object($resql);
  2012. $sql = "UPDATE ".MAIN_DB_PREFIX."menu SET";
  2013. $sql .= " enabled = '".$db->escape($obj->action)."'";
  2014. $sql .= " WHERE rowid = ".((int) $obj->rowid);
  2015. $sql .= " AND enabled = '1'";
  2016. $resql2 = $db->query($sql);
  2017. if ($resql2) {
  2018. } else {
  2019. $error++;
  2020. dol_print_error($db);
  2021. }
  2022. print ". ";
  2023. $i++;
  2024. }
  2025. }
  2026. if ($error == 0) {
  2027. $db->commit();
  2028. } else {
  2029. $db->rollback();
  2030. }
  2031. } else {
  2032. dol_print_error($db);
  2033. $db->rollback();
  2034. }
  2035. } else {
  2036. print $langs->trans('AlreadyDone')."<br>\n";
  2037. }
  2038. print '</td></tr>';
  2039. }
  2040. /**
  2041. * Migration du champ fk_adresse_livraison dans expedition
  2042. * 2.6 -> 2.7
  2043. *
  2044. * @param DoliDB $db Database handler
  2045. * @param Translate $langs Object langs
  2046. * @param Conf $conf Object conf
  2047. * @return void
  2048. */
  2049. function migrate_commande_deliveryaddress($db, $langs, $conf)
  2050. {
  2051. dolibarr_install_syslog("upgrade2::migrate_commande_deliveryaddress");
  2052. print '<tr><td colspan="4">';
  2053. print '<br>';
  2054. print '<b>'.$langs->trans('MigrationDeliveryAddress')."</b><br>\n";
  2055. $error = 0;
  2056. if ($db->DDLInfoTable(MAIN_DB_PREFIX."co_exp")) {
  2057. $db->begin();
  2058. $sql = "SELECT c.fk_adresse_livraison, ce.fk_expedition";
  2059. $sql .= " FROM ".MAIN_DB_PREFIX."commande as c";
  2060. $sql .= ", ".MAIN_DB_PREFIX."co_exp as ce";
  2061. $sql .= " WHERE c.rowid = ce.fk_commande";
  2062. $sql .= " AND c.fk_adresse_livraison IS NOT NULL AND c.fk_adresse_livraison != 0";
  2063. $resql = $db->query($sql);
  2064. if ($resql) {
  2065. $i = 0;
  2066. $num = $db->num_rows($resql);
  2067. if ($num) {
  2068. while ($i < $num) {
  2069. $obj = $db->fetch_object($resql);
  2070. $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
  2071. $sql .= " fk_adresse_livraison = '".$db->escape($obj->fk_adresse_livraison)."'";
  2072. $sql .= " WHERE rowid = ".((int) $obj->fk_expedition);
  2073. $resql2 = $db->query($sql);
  2074. if (!$resql2) {
  2075. $error++;
  2076. dol_print_error($db);
  2077. }
  2078. print ". ";
  2079. $i++;
  2080. }
  2081. } else {
  2082. print $langs->trans('AlreadyDone')."<br>\n";
  2083. }
  2084. if ($error == 0) {
  2085. $db->commit();
  2086. } else {
  2087. $db->rollback();
  2088. }
  2089. } else {
  2090. dol_print_error($db);
  2091. $db->rollback();
  2092. }
  2093. } else {
  2094. print $langs->trans('AlreadyDone')."<br>\n";
  2095. }
  2096. print '</td></tr>';
  2097. }
  2098. /**
  2099. * Migration du champ fk_remise_except dans llx_facturedet doit correspondre a
  2100. * lien dans llx_societe_remise_except vers llx_facturedet
  2101. *
  2102. * @param DoliDB $db Database handler
  2103. * @param Translate $langs Object langs
  2104. * @param Conf $conf Object conf
  2105. * @return integer <0 if KO, 0=Bad version, >0 if OK
  2106. */
  2107. function migrate_restore_missing_links($db, $langs, $conf)
  2108. {
  2109. dolibarr_install_syslog("upgrade2::migrate_restore_missing_links");
  2110. if (($db->type == 'mysql' || $db->type == 'mysqli')) {
  2111. if (versioncompare($db->getVersionArray(), array(4, 0)) < 0) {
  2112. dolibarr_install_syslog("upgrade2::migrate_restore_missing_links Version of database too old to make this migrate action");
  2113. return 0;
  2114. }
  2115. }
  2116. print '<tr><td colspan="4">';
  2117. print '<br>';
  2118. print '<b>'.$langs->trans('MigrationFixData')."</b> (1)<br>\n";
  2119. $error = 0;
  2120. // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 1.
  2121. $table1 = 'facturedet'; $field1 = 'fk_remise_except';
  2122. $table2 = 'societe_remise_except'; $field2 = 'fk_facture_line';
  2123. $db->begin();
  2124. $sql = "SELECT t1.rowid, t1.".$field1." as field";
  2125. $sql .= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
  2126. $sql .= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
  2127. $sql .= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
  2128. $sql .= " WHERE t1.rowid = t2.".$field2.")";
  2129. dolibarr_install_syslog("upgrade2::migrate_restore_missing_links DIRECTION 1");
  2130. $resql = $db->query($sql);
  2131. if ($resql) {
  2132. $i = 0;
  2133. $num = $db->num_rows($resql);
  2134. if ($num) {
  2135. while ($i < $num) {
  2136. $obj = $db->fetch_object($resql);
  2137. print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
  2138. $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
  2139. $sql .= " ".$field2." = '".$db->escape($obj->rowid)."'";
  2140. $sql .= " WHERE rowid = ".((int) $obj->field);
  2141. $resql2 = $db->query($sql);
  2142. if (!$resql2) {
  2143. $error++;
  2144. dol_print_error($db);
  2145. }
  2146. //print ". ";
  2147. $i++;
  2148. }
  2149. } else {
  2150. print $langs->trans('AlreadyDone')."<br>\n";
  2151. }
  2152. if ($error == 0) {
  2153. $db->commit();
  2154. } else {
  2155. $db->rollback();
  2156. }
  2157. } else {
  2158. dol_print_error($db);
  2159. $db->rollback();
  2160. }
  2161. print '</td></tr>';
  2162. print '<tr><td colspan="4">';
  2163. print '<br>';
  2164. print '<b>'.$langs->trans('MigrationFixData')."</b> (2)<br>\n";
  2165. // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 2.
  2166. $table2 = 'facturedet'; $field2 = 'fk_remise_except';
  2167. $table1 = 'societe_remise_except'; $field1 = 'fk_facture_line';
  2168. $db->begin();
  2169. $sql = "SELECT t1.rowid, t1.".$field1." as field";
  2170. $sql .= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
  2171. $sql .= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
  2172. $sql .= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
  2173. $sql .= " WHERE t1.rowid = t2.".$field2.")";
  2174. dolibarr_install_syslog("upgrade2::migrate_restore_missing_links DIRECTION 2");
  2175. $resql = $db->query($sql);
  2176. if ($resql) {
  2177. $i = 0;
  2178. $num = $db->num_rows($resql);
  2179. if ($num) {
  2180. while ($i < $num) {
  2181. $obj = $db->fetch_object($resql);
  2182. print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
  2183. $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
  2184. $sql .= " ".$field2." = '".$db->escape($obj->rowid)."'";
  2185. $sql .= " WHERE rowid = ".((int) $obj->field);
  2186. $resql2 = $db->query($sql);
  2187. if (!$resql2) {
  2188. $error++;
  2189. dol_print_error($db);
  2190. }
  2191. //print ". ";
  2192. $i++;
  2193. }
  2194. } else {
  2195. print $langs->trans('AlreadyDone')."<br>\n";
  2196. }
  2197. if ($error == 0) {
  2198. $db->commit();
  2199. } else {
  2200. $db->rollback();
  2201. }
  2202. } else {
  2203. dol_print_error($db);
  2204. $db->rollback();
  2205. }
  2206. print '</td></tr>';
  2207. return ($error ? -1 : 1);
  2208. }
  2209. /**
  2210. * Migration du champ fk_user_resp de llx_projet vers llx_element_contact
  2211. *
  2212. * @param DoliDB $db Database handler
  2213. * @param Translate $langs Object langs
  2214. * @param Conf $conf Object conf
  2215. * @return void
  2216. */
  2217. function migrate_project_user_resp($db, $langs, $conf)
  2218. {
  2219. dolibarr_install_syslog("upgrade2::migrate_project_user_resp");
  2220. print '<tr><td colspan="4">';
  2221. print '<br>';
  2222. print '<b>'.$langs->trans('MigrationProjectUserResp')."</b><br>\n";
  2223. $result = $db->DDLDescTable(MAIN_DB_PREFIX."projet", "fk_user_resp");
  2224. $obj = $db->fetch_object($result);
  2225. if ($obj) {
  2226. $error = 0;
  2227. $db->begin();
  2228. $sql = "SELECT rowid, fk_user_resp FROM ".MAIN_DB_PREFIX."projet";
  2229. $resql = $db->query($sql);
  2230. if ($resql) {
  2231. $i = 0;
  2232. $num = $db->num_rows($resql);
  2233. if ($num) {
  2234. while ($i < $num) {
  2235. $obj = $db->fetch_object($resql);
  2236. $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
  2237. $sql2 .= "datecreate";
  2238. $sql2 .= ", statut";
  2239. $sql2 .= ", element_id";
  2240. $sql2 .= ", fk_c_type_contact";
  2241. $sql2 .= ", fk_socpeople";
  2242. $sql2 .= ") VALUES (";
  2243. $sql2 .= "'".$db->idate(dol_now())."'";
  2244. $sql2 .= ", '4'";
  2245. $sql2 .= ", ".$obj->rowid;
  2246. $sql2 .= ", '160'";
  2247. $sql2 .= ", ".$obj->fk_user_resp;
  2248. $sql2 .= ")";
  2249. if ($obj->fk_user_resp > 0) {
  2250. $resql2 = $db->query($sql2);
  2251. if (!$resql2) {
  2252. $error++;
  2253. dol_print_error($db);
  2254. }
  2255. }
  2256. print ". ";
  2257. $i++;
  2258. }
  2259. }
  2260. if ($error == 0) {
  2261. $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."projet DROP COLUMN fk_user_resp";
  2262. if ($db->query($sqlDrop)) {
  2263. $db->commit();
  2264. } else {
  2265. $db->rollback();
  2266. }
  2267. } else {
  2268. $db->rollback();
  2269. }
  2270. } else {
  2271. dol_print_error($db);
  2272. $db->rollback();
  2273. }
  2274. } else {
  2275. print $langs->trans('AlreadyDone')."<br>\n";
  2276. }
  2277. print '</td></tr>';
  2278. }
  2279. /**
  2280. * Migration de la table llx_projet_task_actors vers llx_element_contact
  2281. *
  2282. * @param DoliDB $db Database handler
  2283. * @param Translate $langs Object langs
  2284. * @param Conf $conf Object conf
  2285. * @return void
  2286. */
  2287. function migrate_project_task_actors($db, $langs, $conf)
  2288. {
  2289. dolibarr_install_syslog("upgrade2::migrate_project_task_actors");
  2290. print '<tr><td colspan="4">';
  2291. print '<br>';
  2292. print '<b>'.$langs->trans('MigrationProjectTaskActors')."</b><br>\n";
  2293. if ($db->DDLInfoTable(MAIN_DB_PREFIX."projet_task_actors")) {
  2294. $error = 0;
  2295. $db->begin();
  2296. $sql = "SELECT fk_projet_task as fk_project_task, fk_user FROM ".MAIN_DB_PREFIX."projet_task_actors";
  2297. $resql = $db->query($sql);
  2298. if ($resql) {
  2299. $i = 0;
  2300. $num = $db->num_rows($resql);
  2301. if ($num) {
  2302. while ($i < $num) {
  2303. $obj = $db->fetch_object($resql);
  2304. $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
  2305. $sql2 .= "datecreate";
  2306. $sql2 .= ", statut";
  2307. $sql2 .= ", element_id";
  2308. $sql2 .= ", fk_c_type_contact";
  2309. $sql2 .= ", fk_socpeople";
  2310. $sql2 .= ") VALUES (";
  2311. $sql2 .= "'".$db->idate(dol_now())."'";
  2312. $sql2 .= ", '4'";
  2313. $sql2 .= ", ".$obj->fk_project_task;
  2314. $sql2 .= ", '180'";
  2315. $sql2 .= ", ".$obj->fk_user;
  2316. $sql2 .= ")";
  2317. $resql2 = $db->query($sql2);
  2318. if (!$resql2) {
  2319. $error++;
  2320. dol_print_error($db);
  2321. }
  2322. print ". ";
  2323. $i++;
  2324. }
  2325. }
  2326. if ($error == 0) {
  2327. $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX."projet_task_actors";
  2328. if ($db->query($sqlDrop)) {
  2329. $db->commit();
  2330. } else {
  2331. $db->rollback();
  2332. }
  2333. } else {
  2334. $db->rollback();
  2335. }
  2336. } else {
  2337. dol_print_error($db);
  2338. $db->rollback();
  2339. }
  2340. } else {
  2341. print $langs->trans('AlreadyDone')."<br>\n";
  2342. }
  2343. print '</td></tr>';
  2344. }
  2345. /**
  2346. * Migration des tables de relation
  2347. *
  2348. * @param DoliDB $db Database handler
  2349. * @param Translate $langs Object langs
  2350. * @param Conf $conf Object conf
  2351. * @param string $table Table name
  2352. * @param int $fk_source Id of element source
  2353. * @param string $sourcetype Type of element source
  2354. * @param int $fk_target Id of element target
  2355. * @param string $targettype Type of element target
  2356. * @return void
  2357. */
  2358. function migrate_relationship_tables($db, $langs, $conf, $table, $fk_source, $sourcetype, $fk_target, $targettype)
  2359. {
  2360. print '<tr><td colspan="4">';
  2361. print '<br>';
  2362. print '<b>'.$langs->trans('MigrationRelationshipTables', MAIN_DB_PREFIX.$table)."</b><br>\n";
  2363. $error = 0;
  2364. if ($db->DDLInfoTable(MAIN_DB_PREFIX.$table)) {
  2365. dolibarr_install_syslog("upgrade2::migrate_relationship_tables table = ".MAIN_DB_PREFIX.$table);
  2366. $db->begin();
  2367. $sqlSelect = "SELECT ".$fk_source.", ".$fk_target;
  2368. $sqlSelect .= " FROM ".MAIN_DB_PREFIX.$table;
  2369. $resql = $db->query($sqlSelect);
  2370. if ($resql) {
  2371. $i = 0;
  2372. $num = $db->num_rows($resql);
  2373. if ($num) {
  2374. while ($i < $num) {
  2375. $obj = $db->fetch_object($resql);
  2376. $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
  2377. $sqlInsert .= "fk_source";
  2378. $sqlInsert .= ", sourcetype";
  2379. $sqlInsert .= ", fk_target";
  2380. $sqlInsert .= ", targettype";
  2381. $sqlInsert .= ") VALUES (";
  2382. $sqlInsert .= $obj->$fk_source;
  2383. $sqlInsert .= ", '".$db->escape($sourcetype)."'";
  2384. $sqlInsert .= ", ".$obj->$fk_target;
  2385. $sqlInsert .= ", '".$db->escape($targettype)."'";
  2386. $sqlInsert .= ")";
  2387. $result = $db->query($sqlInsert);
  2388. if (!$result) {
  2389. $error++;
  2390. dol_print_error($db);
  2391. }
  2392. print ". ";
  2393. $i++;
  2394. }
  2395. } else {
  2396. print $langs->trans('AlreadyDone')."<br>\n";
  2397. }
  2398. if ($error == 0) {
  2399. $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX.$table;
  2400. if ($db->query($sqlDrop)) {
  2401. $db->commit();
  2402. } else {
  2403. $db->rollback();
  2404. }
  2405. } else {
  2406. $db->rollback();
  2407. }
  2408. } else {
  2409. dol_print_error($db);
  2410. $db->rollback();
  2411. }
  2412. } else {
  2413. print $langs->trans('AlreadyDone')."<br>\n";
  2414. }
  2415. print '</td></tr>';
  2416. }
  2417. /**
  2418. * Migrate duration in seconds
  2419. *
  2420. * @param DoliDB $db Database handler
  2421. * @param Translate $langs Object langs
  2422. * @param Conf $conf Object conf
  2423. * @return void
  2424. */
  2425. function migrate_element_time($db, $langs, $conf)
  2426. {
  2427. dolibarr_install_syslog("upgrade2::migrate_element_time");
  2428. print '<tr><td colspan="4">';
  2429. print '<br>';
  2430. print '<b>'.$langs->trans('MigrationProjectTaskTime')."</b><br>\n";
  2431. $error = 0;
  2432. $db->begin();
  2433. $sql = "SELECT rowid, fk_element, element_duration";
  2434. $sql .= " FROM ".MAIN_DB_PREFIX."element_time";
  2435. $resql = $db->query($sql);
  2436. if ($resql) {
  2437. $i = 0;
  2438. $num = $db->num_rows($resql);
  2439. if ($num) {
  2440. $totaltime = array();
  2441. $oldtime = 0;
  2442. while ($i < $num) {
  2443. $obj = $db->fetch_object($resql);
  2444. if ($obj->element_duration > 0) {
  2445. // convert to second
  2446. // only for int time and float time ex: 1,75 for 1h45
  2447. list($hour, $min) = explode('.', $obj->element_duration);
  2448. $hour = $hour * 60 * 60;
  2449. $min = ($min / 100) * 60 * 60;
  2450. $newtime = $hour + $min;
  2451. $sql2 = "UPDATE ".MAIN_DB_PREFIX."element_time SET";
  2452. $sql2 .= " element_duration = ".((int) $newtime);
  2453. $sql2 .= " WHERE rowid = ".((int) $obj->rowid);
  2454. $resql2 = $db->query($sql2);
  2455. if (!$resql2) {
  2456. $error++;
  2457. dol_print_error($db);
  2458. }
  2459. print ". ";
  2460. $oldtime++;
  2461. if (!empty($totaltime[$obj->fk_element])) {
  2462. $totaltime[$obj->fk_element] += $newtime;
  2463. } else {
  2464. $totaltime[$obj->fk_element] = $newtime;
  2465. }
  2466. } else {
  2467. if (!empty($totaltime[$obj->fk_element])) {
  2468. $totaltime[$obj->fk_element] += $obj->element_duration;
  2469. } else {
  2470. $totaltime[$obj->fk_element] = $obj->element_duration;
  2471. }
  2472. }
  2473. $i++;
  2474. }
  2475. if ($error == 0) {
  2476. if ($oldtime > 0) {
  2477. foreach ($totaltime as $taskid => $total_duration) {
  2478. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
  2479. $sql .= " duration_effective = ".((int) $total_duration);
  2480. $sql .= " WHERE rowid = ".((int) $taskid);
  2481. $resql = $db->query($sql);
  2482. if (!$resql) {
  2483. $error++;
  2484. dol_print_error($db);
  2485. }
  2486. }
  2487. } else {
  2488. print $langs->trans('AlreadyDone')."<br>\n";
  2489. }
  2490. } else {
  2491. dol_print_error($db);
  2492. }
  2493. } else {
  2494. print $langs->trans('AlreadyDone')."<br>\n";
  2495. }
  2496. } else {
  2497. dol_print_error($db);
  2498. }
  2499. if ($error == 0) {
  2500. $db->commit();
  2501. } else {
  2502. $db->rollback();
  2503. }
  2504. print '</td></tr>';
  2505. }
  2506. /**
  2507. * Migrate order ref_customer and date_delivery fields to llx_expedition
  2508. *
  2509. * @param DoliDB $db Database handler
  2510. * @param Translate $langs Object langs
  2511. * @param Conf $conf Object conf
  2512. * @return void
  2513. */
  2514. function migrate_customerorder_shipping($db, $langs, $conf)
  2515. {
  2516. print '<tr><td colspan="4">';
  2517. print '<br>';
  2518. print '<b>'.$langs->trans('MigrationCustomerOrderShipping')."</b><br>\n";
  2519. $error = 0;
  2520. $result1 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "ref_customer");
  2521. $result2 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "date_delivery");
  2522. $obj1 = $db->fetch_object($result1);
  2523. $obj2 = $db->fetch_object($result2);
  2524. if (!$obj1 && !$obj2) {
  2525. dolibarr_install_syslog("upgrade2::migrate_customerorder_shipping");
  2526. $db->begin();
  2527. $sqlAdd1 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN ref_customer varchar(30) AFTER entity";
  2528. $sqlAdd2 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN date_delivery date DEFAULT NULL AFTER date_expedition";
  2529. if ($db->query($sqlAdd1) && $db->query($sqlAdd2)) {
  2530. $sqlSelect = "SELECT e.rowid as shipping_id, c.ref_client, c.date_livraison as delivery_date";
  2531. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."expedition as e";
  2532. $sqlSelect .= ", ".MAIN_DB_PREFIX."element_element as el";
  2533. $sqlSelect .= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON c.rowid = el.fk_source AND el.sourcetype = 'commande'";
  2534. $sqlSelect .= " WHERE e.rowid = el.fk_target";
  2535. $sqlSelect .= " AND el.targettype = 'shipping'";
  2536. $resql = $db->query($sqlSelect);
  2537. if ($resql) {
  2538. $i = 0;
  2539. $num = $db->num_rows($resql);
  2540. if ($num) {
  2541. while ($i < $num) {
  2542. $obj = $db->fetch_object($resql);
  2543. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
  2544. $sqlUpdate .= " ref_customer = '".$db->escape($obj->ref_client)."'";
  2545. $sqlUpdate .= ", date_delivery = '".$db->escape($obj->delivery_date ? $obj->delivery_date : 'null')."'";
  2546. $sqlUpdate .= " WHERE rowid = ".((int) $obj->shipping_id);
  2547. $result = $db->query($sqlUpdate);
  2548. if (!$result) {
  2549. $error++;
  2550. dol_print_error($db);
  2551. }
  2552. print ". ";
  2553. $i++;
  2554. }
  2555. } else {
  2556. print $langs->trans('AlreadyDone')."<br>\n";
  2557. }
  2558. if ($error == 0) {
  2559. $db->commit();
  2560. } else {
  2561. dol_print_error($db);
  2562. $db->rollback();
  2563. }
  2564. } else {
  2565. dol_print_error($db);
  2566. $db->rollback();
  2567. }
  2568. } else {
  2569. dol_print_error($db);
  2570. $db->rollback();
  2571. }
  2572. } else {
  2573. print $langs->trans('AlreadyDone')."<br>\n";
  2574. }
  2575. print '</td></tr>';
  2576. }
  2577. /**
  2578. * Migrate link stored into fk_expedition into llx_element_element
  2579. *
  2580. * @param DoliDB $db Database handler
  2581. * @param Translate $langs Object langs
  2582. * @param Conf $conf Object conf
  2583. * @return void
  2584. */
  2585. function migrate_shipping_delivery($db, $langs, $conf)
  2586. {
  2587. print '<tr><td colspan="4">';
  2588. print '<br>';
  2589. print '<b>'.$langs->trans('MigrationShippingDelivery')."</b><br>\n";
  2590. $error = 0;
  2591. $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison", "fk_expedition");
  2592. $obj = $db->fetch_object($result);
  2593. if ($obj) {
  2594. dolibarr_install_syslog("upgrade2::migrate_shipping_delivery");
  2595. $db->begin();
  2596. $sqlSelect = "SELECT rowid, fk_expedition";
  2597. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."livraison";
  2598. $sqlSelect .= " WHERE fk_expedition is not null";
  2599. $resql = $db->query($sqlSelect);
  2600. if ($resql) {
  2601. $i = 0;
  2602. $num = $db->num_rows($resql);
  2603. if ($num) {
  2604. while ($i < $num) {
  2605. $obj = $db->fetch_object($resql);
  2606. $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
  2607. $sqlInsert .= "fk_source";
  2608. $sqlInsert .= ", sourcetype";
  2609. $sqlInsert .= ", fk_target";
  2610. $sqlInsert .= ", targettype";
  2611. $sqlInsert .= ") VALUES (";
  2612. $sqlInsert .= $obj->fk_expedition;
  2613. $sqlInsert .= ", 'shipping'";
  2614. $sqlInsert .= ", ".$obj->rowid;
  2615. $sqlInsert .= ", 'delivery'";
  2616. $sqlInsert .= ")";
  2617. $result = $db->query($sqlInsert);
  2618. if ($result) {
  2619. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET fk_expedition = NULL";
  2620. $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
  2621. $result = $db->query($sqlUpdate);
  2622. if (!$result) {
  2623. $error++;
  2624. dol_print_error($db);
  2625. }
  2626. print ". ";
  2627. } else {
  2628. $error++;
  2629. dol_print_error($db);
  2630. }
  2631. $i++;
  2632. }
  2633. } else {
  2634. print $langs->trans('AlreadyDone')."<br>\n";
  2635. }
  2636. if ($error == 0) {
  2637. $sqlDelete = "DELETE FROM ".MAIN_DB_PREFIX."element_element WHERE sourcetype = 'commande' AND targettype = 'delivery'";
  2638. $db->query($sqlDelete);
  2639. $db->commit();
  2640. // DDL commands must not be inside a transaction
  2641. $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_expedition";
  2642. $db->query($sqlDrop);
  2643. } else {
  2644. dol_print_error($db);
  2645. $db->rollback();
  2646. }
  2647. } else {
  2648. dol_print_error($db);
  2649. $db->rollback();
  2650. }
  2651. } else {
  2652. print $langs->trans('AlreadyDone')."<br>\n";
  2653. }
  2654. print '</td></tr>';
  2655. }
  2656. /**
  2657. * We try to complete field ref_customer and date_delivery that are empty into llx_livraison.
  2658. * We set them with value from llx_expedition.
  2659. *
  2660. * @param DoliDB $db Database handler
  2661. * @param Translate $langs Object langs
  2662. * @param Conf $conf Object conf
  2663. * @return void
  2664. */
  2665. function migrate_shipping_delivery2($db, $langs, $conf)
  2666. {
  2667. print '<tr><td colspan="4">';
  2668. print '<br>';
  2669. print '<b>'.$langs->trans('MigrationShippingDelivery2')."</b><br>\n";
  2670. $error = 0;
  2671. dolibarr_install_syslog("upgrade2::migrate_shipping_delivery2");
  2672. $db->begin();
  2673. $sqlSelect = "SELECT l.rowid as delivery_id, e.ref_customer, e.date_delivery";
  2674. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."livraison as l,";
  2675. $sqlSelect .= " ".MAIN_DB_PREFIX."element_element as el,";
  2676. $sqlSelect .= " ".MAIN_DB_PREFIX."expedition as e";
  2677. $sqlSelect .= " WHERE l.rowid = el.fk_target";
  2678. $sqlSelect .= " AND el.targettype = 'delivery'";
  2679. $sqlSelect .= " AND e.rowid = el.fk_source AND el.sourcetype = 'shipping'";
  2680. $sqlSelect .= " AND (e.ref_customer IS NOT NULL OR e.date_delivery IS NOT NULL)"; // Useless to process this record if both are null
  2681. // Add condition to know if we never migrate this record
  2682. $sqlSelect .= " AND (l.ref_customer IS NULL".($db->type != 'pgsql' ? " or l.ref_customer = ''" : "").")";
  2683. $sqlSelect .= " AND (l.date_delivery IS NULL".($db->type != 'pgsql' ? " or l.date_delivery = ''" : "").")";
  2684. $resql = $db->query($sqlSelect);
  2685. if ($resql) {
  2686. $i = 0;
  2687. $num = $db->num_rows($resql);
  2688. if ($num) {
  2689. while ($i < $num) {
  2690. $obj = $db->fetch_object($resql);
  2691. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
  2692. $sqlUpdate .= " ref_customer = '".$db->escape($obj->ref_customer)."',";
  2693. $sqlUpdate .= " date_delivery = ".($obj->date_delivery ? "'".$db->escape($obj->date_delivery)."'" : 'null');
  2694. $sqlUpdate .= " WHERE rowid = ".((int) $obj->delivery_id);
  2695. $result = $db->query($sqlUpdate);
  2696. if (!$result) {
  2697. $error++;
  2698. dol_print_error($db);
  2699. }
  2700. print ". ";
  2701. $i++;
  2702. }
  2703. } else {
  2704. print $langs->trans('AlreadyDone')."<br>\n";
  2705. }
  2706. if ($error == 0) {
  2707. $db->commit();
  2708. } else {
  2709. dol_print_error($db);
  2710. $db->rollback();
  2711. }
  2712. } else {
  2713. dol_print_error($db);
  2714. $db->rollback();
  2715. }
  2716. print '</td></tr>';
  2717. }
  2718. /**
  2719. * Migrate link stored into fk_xxxx into fk_element and elementtype
  2720. *
  2721. * @param DoliDB $db Database handler
  2722. * @param Translate $langs Object langs
  2723. * @param Conf $conf Object conf
  2724. * @return void
  2725. */
  2726. function migrate_actioncomm_element($db, $langs, $conf)
  2727. {
  2728. print '<tr><td colspan="4">';
  2729. print '<br>';
  2730. print '<b>'.$langs->trans('MigrationActioncommElement')."</b><br>\n";
  2731. $elements = array(
  2732. 'propal' => 'propalrowid',
  2733. 'order' => 'fk_commande',
  2734. 'invoice' => 'fk_facture',
  2735. 'contract' => 'fk_contract',
  2736. 'order_supplier' => 'fk_supplier_order',
  2737. 'invoice_supplier' => 'fk_supplier_invoice'
  2738. );
  2739. foreach ($elements as $type => $field) {
  2740. $result = $db->DDLDescTable(MAIN_DB_PREFIX."actioncomm", $field);
  2741. $obj = $db->fetch_object($result);
  2742. if ($obj) {
  2743. dolibarr_install_syslog("upgrade2::migrate_actioncomm_element field=".$field);
  2744. $db->begin();
  2745. $sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm SET ";
  2746. $sql .= "fk_element = ".$field.", elementtype = '".$db->escape($type)."'";
  2747. $sql .= " WHERE ".$field." IS NOT NULL";
  2748. $sql .= " AND fk_element IS NULL";
  2749. $sql .= " AND elementtype IS NULL";
  2750. $resql = $db->query($sql);
  2751. if ($resql) {
  2752. $db->commit();
  2753. // DDL commands must not be inside a transaction
  2754. // We will drop at next version because a migrate should be runnable several times if it fails.
  2755. //$sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."actioncomm DROP COLUMN ".$field;
  2756. //$db->query($sqlDrop);
  2757. //print ". ";
  2758. } else {
  2759. dol_print_error($db);
  2760. $db->rollback();
  2761. }
  2762. } else {
  2763. print $langs->trans('AlreadyDone')."<br>\n";
  2764. }
  2765. }
  2766. print '</td></tr>';
  2767. }
  2768. /**
  2769. * Migrate link stored into fk_mode_reglement
  2770. *
  2771. * @param DoliDB $db Database handler
  2772. * @param Translate $langs Object langs
  2773. * @param Conf $conf Object conf
  2774. * @return void
  2775. */
  2776. function migrate_mode_reglement($db, $langs, $conf)
  2777. {
  2778. print '<tr><td colspan="4">';
  2779. print '<br>';
  2780. print '<b>'.$langs->trans('MigrationPaymentMode')."</b><br>\n";
  2781. $elements = array(
  2782. 'old_id' => array(5, 8, 9, 10, 11),
  2783. 'new_id' => array(50, 51, 52, 53, 54),
  2784. 'code' => array('VAD', 'TRA', 'LCR', 'FAC', 'PRO'),
  2785. 'tables' => array('commande_fournisseur', 'commande', 'facture_rec', 'facture', 'propal')
  2786. );
  2787. $count = 0;
  2788. foreach ($elements['old_id'] as $key => $old_id) {
  2789. $error = 0;
  2790. dolibarr_install_syslog("upgrade2::migrate_mode_reglement code=".$elements['code'][$key]);
  2791. $sqlSelect = "SELECT id";
  2792. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."c_paiement";
  2793. $sqlSelect .= " WHERE id = ".((int) $old_id);
  2794. $sqlSelect .= " AND code = '".$db->escape($elements['code'][$key])."'";
  2795. $resql = $db->query($sqlSelect);
  2796. if ($resql) {
  2797. $num = $db->num_rows($resql);
  2798. if ($num) {
  2799. $count++;
  2800. $db->begin();
  2801. $sqla = "UPDATE ".MAIN_DB_PREFIX."paiement SET";
  2802. $sqla .= " fk_paiement = ".((int) $elements['new_id'][$key]);
  2803. $sqla .= " WHERE fk_paiement = ".((int) $old_id);
  2804. $sqla .= " AND fk_paiement IN (SELECT id FROM ".MAIN_DB_PREFIX."c_paiement WHERE id = ".((int) $old_id)." AND code = '".$db->escape($elements['code'][$key])."')";
  2805. $resqla = $db->query($sqla);
  2806. $sql = "UPDATE ".MAIN_DB_PREFIX."c_paiement SET";
  2807. $sql .= " id = ".((int) $elements['new_id'][$key]);
  2808. $sql .= " WHERE id = ".((int) $old_id);
  2809. $sql .= " AND code = '".$db->escape($elements['code'][$key])."'";
  2810. $resql = $db->query($sql);
  2811. if ($resqla && $resql) {
  2812. foreach ($elements['tables'] as $table) {
  2813. $sql = "UPDATE ".MAIN_DB_PREFIX.$table." SET ";
  2814. $sql .= "fk_mode_reglement = ".((int) $elements['new_id'][$key]);
  2815. $sql .= " WHERE fk_mode_reglement = ".((int) $old_id);
  2816. $resql = $db->query($sql);
  2817. if (!$resql) {
  2818. dol_print_error($db);
  2819. $error++;
  2820. }
  2821. print ". ";
  2822. }
  2823. if (!$error) {
  2824. $db->commit();
  2825. } else {
  2826. dol_print_error($db);
  2827. $db->rollback();
  2828. }
  2829. } else {
  2830. dol_print_error($db);
  2831. $db->rollback();
  2832. }
  2833. }
  2834. }
  2835. }
  2836. if ($count == 0) {
  2837. print $langs->trans('AlreadyDone')."<br>\n";
  2838. }
  2839. print '</td></tr>';
  2840. }
  2841. /**
  2842. * Delete duplicates in table categorie_association
  2843. *
  2844. * @param DoliDB $db Database handler
  2845. * @param Translate $langs Object langs
  2846. * @param Conf $conf Object conf
  2847. * @return void
  2848. */
  2849. function migrate_clean_association($db, $langs, $conf)
  2850. {
  2851. $result = $db->DDLDescTable(MAIN_DB_PREFIX."categorie_association");
  2852. if ($result) { // result defined for version 3.2 or -
  2853. $obj = $db->fetch_object($result);
  2854. if ($obj) { // It table categorie_association exists
  2855. $couples = array();
  2856. $filles = array();
  2857. $sql = "SELECT fk_categorie_mere, fk_categorie_fille";
  2858. $sql .= " FROM ".MAIN_DB_PREFIX."categorie_association";
  2859. dolibarr_install_syslog("upgrade: search duplicate");
  2860. $resql = $db->query($sql);
  2861. if ($resql) {
  2862. $num = $db->num_rows($resql);
  2863. while ($obj = $db->fetch_object($resql)) {
  2864. if (!isset($filles[$obj->fk_categorie_fille])) { // Only one record as child (a child has only on parent).
  2865. if ($obj->fk_categorie_mere != $obj->fk_categorie_fille) {
  2866. $filles[$obj->fk_categorie_fille] = 1; // Set record for this child
  2867. $couples[$obj->fk_categorie_mere.'_'.$obj->fk_categorie_fille] = array('mere'=>$obj->fk_categorie_mere, 'fille'=>$obj->fk_categorie_fille);
  2868. }
  2869. }
  2870. }
  2871. dolibarr_install_syslog("upgrade: result is num=".$num." count(couples)=".count($couples));
  2872. // If there is duplicates couples or child with two parents
  2873. if (count($couples) > 0 && $num > count($couples)) {
  2874. $error = 0;
  2875. $db->begin();
  2876. // We delete all
  2877. $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association";
  2878. dolibarr_install_syslog("upgrade: delete association");
  2879. $resqld = $db->query($sql);
  2880. if ($resqld) {
  2881. // And we insert only each record once
  2882. foreach ($couples as $key => $val) {
  2883. $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association(fk_categorie_mere,fk_categorie_fille)";
  2884. $sql .= " VALUES(".((int) $val['mere']).", ".((int) $val['fille']).")";
  2885. dolibarr_install_syslog("upgrade: insert association");
  2886. $resqli = $db->query($sql);
  2887. if (!$resqli) {
  2888. $error++;
  2889. }
  2890. }
  2891. }
  2892. if (!$error) {
  2893. print '<tr><td>'.$langs->trans("MigrationCategorieAssociation").'</td>';
  2894. print '<td class="right">'.$langs->trans("RemoveDuplicates").' '.$langs->trans("Success").' ('.$num.'=>'.count($couples).')</td></tr>';
  2895. $db->commit();
  2896. } else {
  2897. print '<tr><td>'.$langs->trans("MigrationCategorieAssociation").'</td>';
  2898. print '<td class="right">'.$langs->trans("RemoveDuplicates").' '.$langs->trans("Failed").'</td></tr>';
  2899. $db->rollback();
  2900. }
  2901. }
  2902. } else {
  2903. print '<tr><td>'.$langs->trans("Error").'</td>';
  2904. print '<td class="right"><div class="error">'.$db->lasterror().'</div></td></tr>';
  2905. }
  2906. }
  2907. }
  2908. }
  2909. /**
  2910. * Migrate categorie association
  2911. *
  2912. * @param DoliDB $db Database handler
  2913. * @param Translate $langs Object langs
  2914. * @param Conf $conf Object conf
  2915. * @return void
  2916. */
  2917. function migrate_categorie_association($db, $langs, $conf)
  2918. {
  2919. print '<tr><td colspan="4">';
  2920. print '<br>';
  2921. print '<b>'.$langs->trans('MigrationCategorieAssociation')."</b><br>\n";
  2922. $error = 0;
  2923. if ($db->DDLInfoTable(MAIN_DB_PREFIX."categorie_association")) {
  2924. dolibarr_install_syslog("upgrade2::migrate_categorie_association");
  2925. $db->begin();
  2926. $sqlSelect = "SELECT fk_categorie_mere, fk_categorie_fille";
  2927. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."categorie_association";
  2928. $resql = $db->query($sqlSelect);
  2929. if ($resql) {
  2930. $i = 0;
  2931. $num = $db->num_rows($resql);
  2932. if ($num) {
  2933. while ($i < $num) {
  2934. $obj = $db->fetch_object($resql);
  2935. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."categorie SET ";
  2936. $sqlUpdate .= "fk_parent = ".((int) $obj->fk_categorie_mere);
  2937. $sqlUpdate .= " WHERE rowid = ".((int) $obj->fk_categorie_fille);
  2938. $result = $db->query($sqlUpdate);
  2939. if (!$result) {
  2940. $error++;
  2941. dol_print_error($db);
  2942. }
  2943. print ". ";
  2944. $i++;
  2945. }
  2946. } else {
  2947. print $langs->trans('AlreadyDone')."<br>\n";
  2948. }
  2949. if (!$error) {
  2950. $db->commit();
  2951. } else {
  2952. $db->rollback();
  2953. }
  2954. } else {
  2955. dol_print_error($db);
  2956. $db->rollback();
  2957. }
  2958. } else {
  2959. print $langs->trans('AlreadyDone')."<br>\n";
  2960. }
  2961. print '</td></tr>';
  2962. }
  2963. /**
  2964. * Migrate event assignement to owner
  2965. *
  2966. * @param DoliDB $db Database handler
  2967. * @param Translate $langs Object langs
  2968. * @param Conf $conf Object conf
  2969. * @return void
  2970. */
  2971. function migrate_event_assignement($db, $langs, $conf)
  2972. {
  2973. print '<tr><td colspan="4">';
  2974. print '<br>';
  2975. print '<b>'.$langs->trans('MigrationEvents')."</b><br>\n";
  2976. $error = 0;
  2977. dolibarr_install_syslog("upgrade2::migrate_event_assignement");
  2978. $db->begin();
  2979. $sqlSelect = "SELECT a.id, a.fk_user_action";
  2980. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
  2981. $sqlSelect .= " LEFT JOIN ".MAIN_DB_PREFIX."actioncomm_resources as ar ON ar.fk_actioncomm = a.id AND ar.element_type = 'user' AND ar.fk_element = a.fk_user_action";
  2982. $sqlSelect .= " WHERE fk_user_action > 0 AND fk_user_action NOT IN (SELECT fk_element FROM ".MAIN_DB_PREFIX."actioncomm_resources as ar WHERE ar.fk_actioncomm = a.id AND ar.element_type = 'user')";
  2983. $sqlSelect .= " ORDER BY a.id";
  2984. //print $sqlSelect;
  2985. $resql = $db->query($sqlSelect);
  2986. if ($resql) {
  2987. $i = 0;
  2988. $num = $db->num_rows($resql);
  2989. if ($num) {
  2990. while ($i < $num) {
  2991. $obj = $db->fetch_object($resql);
  2992. $sqlUpdate = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element) ";
  2993. $sqlUpdate .= "VALUES(".((int) $obj->id).", 'user', ".((int) $obj->fk_user_action).")";
  2994. $result = $db->query($sqlUpdate);
  2995. if (!$result) {
  2996. $error++;
  2997. dol_print_error($db);
  2998. }
  2999. print ". ";
  3000. $i++;
  3001. }
  3002. } else {
  3003. print $langs->trans('AlreadyDone')."<br>\n";
  3004. }
  3005. if (!$error) {
  3006. $db->commit();
  3007. } else {
  3008. $db->rollback();
  3009. }
  3010. } else {
  3011. dol_print_error($db);
  3012. $db->rollback();
  3013. }
  3014. print '</td></tr>';
  3015. }
  3016. /**
  3017. * Migrate event assignement to owner
  3018. *
  3019. * @param DoliDB $db Database handler
  3020. * @param Translate $langs Object langs
  3021. * @param Conf $conf Object conf
  3022. * @return void
  3023. */
  3024. function migrate_event_assignement_contact($db, $langs, $conf)
  3025. {
  3026. print '<tr><td colspan="4">';
  3027. print '<br>';
  3028. print '<b>'.$langs->trans('MigrationEventsContact')."</b><br>\n";
  3029. $error = 0;
  3030. dolibarr_install_syslog("upgrade2::migrate_event_assignement");
  3031. $db->begin();
  3032. $sqlSelect = "SELECT a.id, a.fk_contact";
  3033. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
  3034. $sqlSelect .= " LEFT JOIN ".MAIN_DB_PREFIX."actioncomm_resources as ar ON ar.fk_actioncomm = a.id AND ar.element_type = 'socpeople' AND ar.fk_element = a.fk_contact";
  3035. $sqlSelect .= " WHERE fk_contact > 0 AND fk_contact NOT IN (SELECT fk_element FROM ".MAIN_DB_PREFIX."actioncomm_resources as ar WHERE ar.fk_actioncomm = a.id AND ar.element_type = 'socpeople')";
  3036. $sqlSelect .= " ORDER BY a.id";
  3037. //print $sqlSelect;
  3038. $resql = $db->query($sqlSelect);
  3039. if ($resql) {
  3040. $i = 0;
  3041. $num = $db->num_rows($resql);
  3042. if ($num) {
  3043. while ($i < $num) {
  3044. $obj = $db->fetch_object($resql);
  3045. $sqlUpdate = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element) ";
  3046. $sqlUpdate .= "VALUES(".((int) $obj->id).", 'socpeople', ".((int) $obj->fk_contact).")";
  3047. $result = $db->query($sqlUpdate);
  3048. if (!$result) {
  3049. $error++;
  3050. dol_print_error($db);
  3051. }
  3052. print ". ";
  3053. $i++;
  3054. }
  3055. } else {
  3056. print $langs->trans('AlreadyDone')."<br>\n";
  3057. }
  3058. if (!$error) {
  3059. $db->commit();
  3060. } else {
  3061. $db->rollback();
  3062. }
  3063. } else {
  3064. dol_print_error($db);
  3065. $db->rollback();
  3066. }
  3067. print '</td></tr>';
  3068. }
  3069. /**
  3070. * Migrate to reset the blocked log for V7+ algorithm
  3071. *
  3072. * @param DoliDB $db Database handler
  3073. * @param Translate $langs Object langs
  3074. * @param Conf $conf Object conf
  3075. * @return void
  3076. */
  3077. function migrate_reset_blocked_log($db, $langs, $conf)
  3078. {
  3079. global $user;
  3080. require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
  3081. print '<tr><td colspan="4">';
  3082. print '<br>';
  3083. print '<b>'.$langs->trans('MigrationResetBlockedLog')."</b><br>\n";
  3084. $error = 0;
  3085. dolibarr_install_syslog("upgrade2::migrate_reset_blocked_log");
  3086. $db->begin();
  3087. $sqlSelect = "SELECT DISTINCT entity";
  3088. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."blockedlog";
  3089. //print $sqlSelect;
  3090. $resql = $db->query($sqlSelect);
  3091. if ($resql) {
  3092. $i = 0;
  3093. $num = $db->num_rows($resql);
  3094. if ($num) {
  3095. while ($i < $num) {
  3096. $obj = $db->fetch_object($resql);
  3097. print 'Process entity '.$obj->entity;
  3098. $sqlSearch = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."blockedlog WHERE action = 'MODULE_SET' and entity = ".((int) $obj->entity);
  3099. $resqlSearch = $db->query($sqlSearch);
  3100. if ($resqlSearch) {
  3101. $objSearch = $db->fetch_object($resqlSearch);
  3102. //var_dump($objSearch);
  3103. if ($objSearch && $objSearch->nb == 0) {
  3104. print ' - Record for entity must be reset...';
  3105. $sqlUpdate = "DELETE FROM ".MAIN_DB_PREFIX."blockedlog";
  3106. $sqlUpdate .= " WHERE entity = ".((int) $obj->entity);
  3107. $resqlUpdate = $db->query($sqlUpdate);
  3108. if (!$resqlUpdate) {
  3109. $error++;
  3110. dol_print_error($db);
  3111. } else {
  3112. // Add set line
  3113. $object = new stdClass();
  3114. $object->id = 1;
  3115. $object->element = 'module';
  3116. $object->ref = 'systemevent';
  3117. $object->entity = $obj->entity;
  3118. $object->date = dol_now();
  3119. $b = new BlockedLog($db);
  3120. $b->setObjectData($object, 'MODULE_SET', 0);
  3121. $res = $b->create($user);
  3122. if ($res <= 0) {
  3123. $error++;
  3124. }
  3125. }
  3126. } else {
  3127. print ' - '.$langs->trans('AlreadyInV7').'<br>';
  3128. }
  3129. } else {
  3130. dol_print_error($db);
  3131. }
  3132. $i++;
  3133. }
  3134. } else {
  3135. print $langs->trans('NothingToDo')."<br>\n";
  3136. }
  3137. if (!$error) {
  3138. $db->commit();
  3139. } else {
  3140. $db->rollback();
  3141. }
  3142. } else {
  3143. dol_print_error($db);
  3144. $db->rollback();
  3145. }
  3146. print '</td></tr>';
  3147. }
  3148. /**
  3149. * Migrate to add entity value into llx_societe_remise
  3150. *
  3151. * @param DoliDB $db Database handler
  3152. * @param Translate $langs Object langs
  3153. * @param Conf $conf Object conf
  3154. * @return void
  3155. */
  3156. function migrate_remise_entity($db, $langs, $conf)
  3157. {
  3158. print '<tr><td colspan="4">';
  3159. print '<br>';
  3160. print '<b>'.$langs->trans('MigrationRemiseEntity')."</b><br>\n";
  3161. $error = 0;
  3162. dolibarr_install_syslog("upgrade2::migrate_remise_entity");
  3163. $db->begin();
  3164. $sqlSelect = "SELECT sr.rowid, s.entity";
  3165. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."societe_remise as sr, ".MAIN_DB_PREFIX."societe as s";
  3166. $sqlSelect .= " WHERE sr.fk_soc = s.rowid and sr.entity != s.entity";
  3167. //print $sqlSelect;
  3168. $resql = $db->query($sqlSelect);
  3169. if ($resql) {
  3170. $i = 0;
  3171. $num = $db->num_rows($resql);
  3172. if ($num) {
  3173. while ($i < $num) {
  3174. $obj = $db->fetch_object($resql);
  3175. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."societe_remise SET";
  3176. $sqlUpdate .= " entity = ".$obj->entity;
  3177. $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
  3178. $result = $db->query($sqlUpdate);
  3179. if (!$result) {
  3180. $error++;
  3181. dol_print_error($db);
  3182. }
  3183. print ". ";
  3184. $i++;
  3185. }
  3186. } else {
  3187. print $langs->trans('AlreadyDone')."<br>\n";
  3188. }
  3189. if (!$error) {
  3190. $db->commit();
  3191. } else {
  3192. $db->rollback();
  3193. }
  3194. } else {
  3195. dol_print_error($db);
  3196. $db->rollback();
  3197. }
  3198. print '</td></tr>';
  3199. }
  3200. /**
  3201. * Migrate to add entity value into llx_societe_remise_except
  3202. *
  3203. * @param DoliDB $db Database handler
  3204. * @param Translate $langs Object langs
  3205. * @param Conf $conf Object conf
  3206. * @return void
  3207. */
  3208. function migrate_remise_except_entity($db, $langs, $conf)
  3209. {
  3210. print '<tr><td colspan="4">';
  3211. print '<br>';
  3212. print '<b>'.$langs->trans('MigrationRemiseExceptEntity')."</b><br>\n";
  3213. $error = 0;
  3214. dolibarr_install_syslog("upgrade2::migrate_remise_except_entity");
  3215. $db->begin();
  3216. $sqlSelect = "SELECT sr.rowid, sr.fk_soc, sr.fk_facture_source, sr.fk_facture, sr.fk_facture_line";
  3217. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as sr";
  3218. //print $sqlSelect;
  3219. $resql = $db->query($sqlSelect);
  3220. if ($resql) {
  3221. $i = 0;
  3222. $num = $db->num_rows($resql);
  3223. if ($num) {
  3224. while ($i < $num) {
  3225. $obj = $db->fetch_object($resql);
  3226. if (!empty($obj->fk_facture_source) || !empty($obj->fk_facture)) {
  3227. $fk_facture = (!empty($obj->fk_facture_source) ? $obj->fk_facture_source : $obj->fk_facture);
  3228. $sqlSelect2 = "SELECT f.entity";
  3229. $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."facture as f";
  3230. $sqlSelect2 .= " WHERE f.rowid = ".((int) $fk_facture);
  3231. } elseif (!empty($obj->fk_facture_line)) {
  3232. $sqlSelect2 = "SELECT f.entity";
  3233. $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."facturedet as fd";
  3234. $sqlSelect2 .= " WHERE fd.rowid = ".((int) $obj->fk_facture_line);
  3235. $sqlSelect2 .= " AND fd.fk_facture = f.rowid";
  3236. } else {
  3237. $sqlSelect2 = "SELECT s.entity";
  3238. $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."societe as s";
  3239. $sqlSelect2 .= " WHERE s.rowid = ".((int) $obj->fk_soc);
  3240. }
  3241. $resql2 = $db->query($sqlSelect2);
  3242. if ($resql2) {
  3243. if ($db->num_rows($resql2) > 0) {
  3244. $obj2 = $db->fetch_object($resql2);
  3245. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."societe_remise_except SET";
  3246. $sqlUpdate .= " entity = ".((int) $obj2->entity);
  3247. $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
  3248. $result = $db->query($sqlUpdate);
  3249. if (!$result) {
  3250. $error++;
  3251. dol_print_error($db);
  3252. }
  3253. }
  3254. } else {
  3255. $error++;
  3256. dol_print_error($db);
  3257. }
  3258. print ". ";
  3259. $i++;
  3260. }
  3261. } else {
  3262. print $langs->trans('AlreadyDone')."<br>\n";
  3263. }
  3264. if (!$error) {
  3265. $db->commit();
  3266. } else {
  3267. $db->rollback();
  3268. }
  3269. } else {
  3270. dol_print_error($db);
  3271. $db->rollback();
  3272. }
  3273. print '</td></tr>';
  3274. }
  3275. /**
  3276. * Migrate to add entity value into llx_user_rights
  3277. *
  3278. * @param DoliDB $db Database handler
  3279. * @param Translate $langs Object langs
  3280. * @param Conf $conf Object conf
  3281. * @return void
  3282. */
  3283. function migrate_user_rights_entity($db, $langs, $conf)
  3284. {
  3285. print '<tr><td colspan="4">';
  3286. print '<b>'.$langs->trans('MigrationUserRightsEntity')."</b><br>\n";
  3287. $error = 0;
  3288. dolibarr_install_syslog("upgrade2::migrate_user_rights_entity");
  3289. $db->begin();
  3290. $sqlSelect = "SELECT u.rowid, u.entity";
  3291. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."user as u";
  3292. $sqlSelect .= " WHERE u.entity > 1";
  3293. //print $sqlSelect;
  3294. $resql = $db->query($sqlSelect);
  3295. if ($resql) {
  3296. $i = 0;
  3297. $num = $db->num_rows($resql);
  3298. if ($num) {
  3299. while ($i < $num) {
  3300. $obj = $db->fetch_object($resql);
  3301. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."user_rights SET";
  3302. $sqlUpdate .= " entity = ".((int) $obj->entity);
  3303. $sqlUpdate .= " WHERE fk_user = ".((int) $obj->rowid);
  3304. $result = $db->query($sqlUpdate);
  3305. if (!$result) {
  3306. $error++;
  3307. dol_print_error($db);
  3308. }
  3309. print ". ";
  3310. $i++;
  3311. }
  3312. } else {
  3313. print $langs->trans('AlreadyDone')."<br>\n";
  3314. }
  3315. if (!$error) {
  3316. $db->commit();
  3317. } else {
  3318. $db->rollback();
  3319. }
  3320. } else {
  3321. dol_print_error($db);
  3322. $db->rollback();
  3323. }
  3324. print '</td></tr>';
  3325. }
  3326. /**
  3327. * Migrate to add entity value into llx_usergroup_rights
  3328. *
  3329. * @param DoliDB $db Database handler
  3330. * @param Translate $langs Object langs
  3331. * @param Conf $conf Object conf
  3332. * @return void
  3333. */
  3334. function migrate_usergroup_rights_entity($db, $langs, $conf)
  3335. {
  3336. print '<tr><td colspan="4">';
  3337. print '<b>'.$langs->trans('MigrationUserGroupRightsEntity')."</b><br>\n";
  3338. $error = 0;
  3339. dolibarr_install_syslog("upgrade2::migrate_usergroup_rights_entity");
  3340. $db->begin();
  3341. $sqlSelect = "SELECT u.rowid, u.entity";
  3342. $sqlSelect .= " FROM ".MAIN_DB_PREFIX."usergroup as u";
  3343. $sqlSelect .= " WHERE u.entity > 1";
  3344. //print $sqlSelect;
  3345. $resql = $db->query($sqlSelect);
  3346. if ($resql) {
  3347. $i = 0;
  3348. $num = $db->num_rows($resql);
  3349. if ($num) {
  3350. while ($i < $num) {
  3351. $obj = $db->fetch_object($resql);
  3352. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."usergroup_rights SET";
  3353. $sqlUpdate .= " entity = ".((int) $obj->entity);
  3354. $sqlUpdate .= " WHERE fk_usergroup = ".((int) $obj->rowid);
  3355. $result = $db->query($sqlUpdate);
  3356. if (!$result) {
  3357. $error++;
  3358. dol_print_error($db);
  3359. }
  3360. print ". ";
  3361. $i++;
  3362. }
  3363. } else {
  3364. print $langs->trans('AlreadyDone')."<br>\n";
  3365. }
  3366. if (!$error) {
  3367. $db->commit();
  3368. } else {
  3369. $db->rollback();
  3370. }
  3371. } else {
  3372. dol_print_error($db);
  3373. $db->rollback();
  3374. }
  3375. print '</td></tr>';
  3376. }
  3377. /**
  3378. * Migration directory
  3379. *
  3380. * @param DoliDB $db Database handler
  3381. * @param Translate $langs Object langs
  3382. * @param Conf $conf Object conf
  3383. * @param string $oldname Old name (relative to DOL_DATA_ROOT)
  3384. * @param string $newname New name (relative to DOL_DATA_ROOT)
  3385. * @return void
  3386. */
  3387. function migrate_rename_directories($db, $langs, $conf, $oldname, $newname)
  3388. {
  3389. dolibarr_install_syslog("upgrade2::migrate_rename_directories");
  3390. if (is_dir(DOL_DATA_ROOT.$oldname) && !file_exists(DOL_DATA_ROOT.$newname)) {
  3391. dolibarr_install_syslog("upgrade2::migrate_rename_directories move ".DOL_DATA_ROOT.$oldname.' into '.DOL_DATA_ROOT.$newname);
  3392. @rename(DOL_DATA_ROOT.$oldname, DOL_DATA_ROOT.$newname);
  3393. }
  3394. }
  3395. /**
  3396. * Delete deprecated files
  3397. *
  3398. * @param DoliDB $db Database handler
  3399. * @param Translate $langs Object langs
  3400. * @param Conf $conf Object conf
  3401. * @return boolean
  3402. */
  3403. function migrate_delete_old_files($db, $langs, $conf)
  3404. {
  3405. $ret = true;
  3406. dolibarr_install_syslog("upgrade2::migrate_delete_old_files");
  3407. // List of files to delete
  3408. $filetodeletearray = array(
  3409. '/core/triggers/interface_demo.class.php',
  3410. '/core/menus/barre_left/default.php',
  3411. '/core/menus/barre_top/default.php',
  3412. '/core/modules/modComptabiliteExpert.class.php',
  3413. '/core/modules/modCommercial.class.php',
  3414. '/core/modules/modProduit.class.php',
  3415. '/core/modules/modSkype.class.php',
  3416. '/phenix/inc/triggers/interface_modPhenix_Phenixsynchro.class.php',
  3417. '/webcalendar/inc/triggers/interface_modWebcalendar_webcalsynchro.class.php',
  3418. '/core/triggers/interface_modWebcalendar_Webcalsynchro.class.php',
  3419. '/core/triggers/interface_modCommande_Ecotax.class.php',
  3420. '/core/triggers/interface_modCommande_fraisport.class.php',
  3421. '/core/triggers/interface_modPropale_PropalWorkflow.class.php',
  3422. '/core/triggers/interface_99_modWebhook_WebhookTriggers.class.php',
  3423. '/core/triggers/interface_99_modZapier_ZapierTriggers.class.php',
  3424. '/core/menus/smartphone/iphone.lib.php',
  3425. '/core/menus/smartphone/iphone_backoffice.php',
  3426. '/core/menus/smartphone/iphone_frontoffice.php',
  3427. '/core/menus/standard/auguria_backoffice.php',
  3428. '/core/menus/standard/auguria_frontoffice.php',
  3429. '/core/menus/standard/eldy_backoffice.php',
  3430. '/core/menus/standard/eldy_frontoffice.php',
  3431. '/core/modules/mailings/contacts2.modules.php',
  3432. '/core/modules/mailings/contacts3.modules.php',
  3433. '/core/modules/mailings/contacts4.modules.php',
  3434. '/core/modules/mailings/framboise.modules.php',
  3435. '/core/modules/mailings/dolibarr_services_expired.modules.php',
  3436. '/core/modules/mailings/peche.modules.php',
  3437. '/core/modules/mailings/poire.modules.php',
  3438. '/core/modules/mailings/kiwi.modules.php',
  3439. '/core/modules/facture/pdf_crabe.modules.php',
  3440. '/core/modules/facture/pdf_oursin.modules.php',
  3441. '/core/modules/export/export_excel.modules.php',
  3442. '/core/modules/export/export_excel2007new.modules.php',
  3443. '/core/boxes/box_members.php',
  3444. '/api/class/api_generic.class.php',
  3445. '/asterisk/cidlookup.php',
  3446. '/categories/class/api_category.class.php',
  3447. '/categories/class/api_deprecated_category.class.php',
  3448. '/compta/facture/class/api_invoice.class.php',
  3449. '/commande/class/api_commande.class.php',
  3450. '/user/class/api_user.class.php',
  3451. '/product/class/api_product.class.php',
  3452. '/societe/class/api_contact.class.php',
  3453. '/societe/class/api_thirdparty.class.php',
  3454. '/support/online.php',
  3455. '/takepos/class/actions_takepos.class.php',
  3456. '/install/mysql/tables/llx_c_ticketsup_category.key.sql',
  3457. '/install/mysql/tables/llx_c_ticketsup_category.sql',
  3458. '/install/mysql/tables/llx_c_ticketsup_severity.key.sql',
  3459. '/install/mysql/tables/llx_c_ticketsup_severity.sql',
  3460. '/install/mysql/tables/llx_c_ticketsup_type.key.sql',
  3461. '/install/mysql/tables/llx_c_ticketsup_type.sql'
  3462. );
  3463. foreach ($filetodeletearray as $filetodelete) {
  3464. //print '<b>'DOL_DOCUMENT_ROOT.$filetodelete."</b><br>\n";
  3465. if (file_exists(DOL_DOCUMENT_ROOT.$filetodelete)) {
  3466. $result = dol_delete_file(DOL_DOCUMENT_ROOT.$filetodelete, 0, 0, 0, null, true, false);
  3467. if (!$result) {
  3468. $langs->load("errors");
  3469. print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteFile", DOL_DOCUMENT_ROOT.$filetodelete);
  3470. print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
  3471. } else {
  3472. //print $langs->trans("FileWasRemoved", $filetodelete).'<br>';
  3473. }
  3474. }
  3475. }
  3476. return $ret;
  3477. }
  3478. /**
  3479. * Remove deprecated directories
  3480. *
  3481. * @param DoliDB $db Database handler
  3482. * @param Translate $langs Object langs
  3483. * @param Conf $conf Object conf
  3484. * @return boolean
  3485. */
  3486. function migrate_delete_old_dir($db, $langs, $conf)
  3487. {
  3488. $ret = true;
  3489. dolibarr_install_syslog("upgrade2::migrate_delete_old_dir");
  3490. // List of files to delete
  3491. $filetodeletearray = array(
  3492. DOL_DOCUMENT_ROOT.'/core/modules/facture/terre',
  3493. DOL_DOCUMENT_ROOT.'/core/modules/facture/mercure',
  3494. );
  3495. // On linux, we can also removed old directory with a different case than new directory.
  3496. if (!empty($_SERVER["WINDIR"])) {
  3497. $filetodeletearray[] = DOL_DOCUMENT_ROOT.'/includes/phpoffice/PhpSpreadsheet';
  3498. }
  3499. foreach ($filetodeletearray as $filetodelete) {
  3500. $result = 1;
  3501. if (file_exists($filetodelete)) {
  3502. $result = dol_delete_dir_recursive($filetodelete);
  3503. }
  3504. if (!$result) {
  3505. $langs->load("errors");
  3506. print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteDir", $filetodelete);
  3507. print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
  3508. }
  3509. }
  3510. return $ret;
  3511. }
  3512. /**
  3513. * Disable/Reenable features modules.
  3514. * We must do this when internal menu of module or permissions has changed
  3515. * or when triggers have moved.
  3516. *
  3517. * @param DoliDB $db Database handler
  3518. * @param Translate $langs Object langs
  3519. * @param Conf $conf Object conf
  3520. * @param array $listofmodule List of modules, like array('MODULE_KEY_NAME'=>', $reloadmode)
  3521. * @param int $force 1=Reload module even if not already loaded
  3522. * @return int <0 if KO, >0 if OK
  3523. */
  3524. function migrate_reload_modules($db, $langs, $conf, $listofmodule = array(), $force = 0)
  3525. {
  3526. global $user;
  3527. if (count($listofmodule) == 0) {
  3528. return 0;
  3529. }
  3530. if (!is_object($user)) {
  3531. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  3532. $user = new User($db); // To avoid error during migration
  3533. }
  3534. dolibarr_install_syslog("upgrade2::migrate_reload_modules force=".$force.", listofmodule=".join(',', array_keys($listofmodule)));
  3535. $reloadactionformodules = array(
  3536. 'MAIN_MODULE_AGENDA' => array('class' => 'modAgenda', 'remove'=> 1),
  3537. 'MAIN_MODULE_API' => array('class' => 'modApi'),
  3538. 'MAIN_MODULE_BARCODE' => array('class' => 'modBarcode', 'remove'=> 1),
  3539. 'MAIN_MODULE_BLOCKEDLOG' => array('class' => 'modBlockedLog', 'deleteinsertmenus'=> 1),
  3540. 'MAIN_MODULE_CRON' => array('class' => 'modCron', 'remove'=> 1),
  3541. 'MAIN_MODULE_EXTERNALSITE' => array('class' => 'modExternalSite', 'remove'=> 1),
  3542. 'MAIN_MODULE_SOCIETE' => array('class' => 'modSociete', 'remove'=> 1),
  3543. 'MAIN_MODULE_PRODUIT' => array('class' => 'modProduct'),
  3544. 'MAIN_MODULE_SERVICE' => array('class' => 'modService'),
  3545. 'MAIN_MODULE_COMMANDE' => array('class' => 'modCommande'),
  3546. 'MAIN_MODULE_FACTURE' => array('class' => 'modFacture'),
  3547. 'MAIN_MODULE_FOURNISSEUR' => array('class' => 'modFournisseur'),
  3548. 'MAIN_MODULE_HOLIDAY' => array('class' => 'modHoliday', 'remove'=>1),
  3549. 'MAIN_MODULE_EXPENSEREPORT' => array('class' => 'modExpenseReport'),
  3550. 'MAIN_MODULE_DON' => array('class' => 'modDon'),
  3551. 'MAIN_MODULE_ECM' => array('class' => 'modECM', 'remove'=>1),
  3552. 'MAIN_MODULE_KNOWLEDGEMANAGEMENT' => array('class' => 'modKnowledgeManagement', 'remove'=>1),
  3553. 'MAIN_MODULE_EVENTORGANIZATION' => array('class' => 'modEventOrganization', 'remove'=>1),
  3554. 'MAIN_MODULE_PAYBOX' => array('class' => 'modPaybox', 'remove'=>1),
  3555. 'MAIN_MODULE_SUPPLIERPROPOSAL' => array('class' => 'modSupplierProposal', 'remove'=>1),
  3556. 'MAIN_MODULE_OPENSURVEY' => array('class' => 'modOpenSurvey', 'remove'=>1),
  3557. 'MAIN_MODULE_PRODUCTBATCH' => array('class' => 'modProductBatch', 'remove'=>1),
  3558. 'MAIN_MODULE_TAKEPOS' => array('class' => 'modTakePos', 'remove'=>1),
  3559. 'MAIN_MODULE_EMAILCOLLECTOR' => array('class' => 'modEmailCollector', 'remove'=>1),
  3560. );
  3561. foreach ($listofmodule as $moduletoreload => $reloadmode) { // reloadmodule can be 'noboxes', 'newboxdefonly', 'forceactivate'
  3562. if (empty($moduletoreload) || (empty($conf->global->$moduletoreload) && !$force)) {
  3563. continue; // Discard reload if module not enabled
  3564. }
  3565. $mod = null;
  3566. if (!empty($reloadactionformodules[$moduletoreload])) {
  3567. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module ".$moduletoreload." with mode ".$reloadmode);
  3568. $val = $reloadactionformodules[$moduletoreload];
  3569. $classformodule = $val['class'];
  3570. $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/'.$classformodule.'.class.php';
  3571. if ($res) {
  3572. $mod = new $classformodule($db);
  3573. if (!empty($val['remove'])) {
  3574. $mod->remove('noboxes');
  3575. }
  3576. if (!empty($val['deleteinsertmenus'])) {
  3577. // We only reload menus
  3578. $mod->delete_menus();
  3579. $mod->insert_menus();
  3580. } else {
  3581. $mod->init($reloadmode);
  3582. }
  3583. }
  3584. } else { // Other generic cases/modules
  3585. $reg = array();
  3586. $tmp = preg_match('/MAIN_MODULE_([a-zA-Z0-9]+)/', $moduletoreload, $reg);
  3587. if (!empty($reg[1])) {
  3588. if (strtoupper($moduletoreload) == $moduletoreload) { // If key is un uppercase
  3589. $moduletoreloadshort = ucfirst(strtolower($reg[1]));
  3590. } else // If key is a mix of up and low case
  3591. {
  3592. $moduletoreloadshort = $reg[1];
  3593. }
  3594. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module ".$moduletoreloadshort." with mode ".$reloadmode." (generic code)");
  3595. $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/mod'.$moduletoreloadshort.'.class.php';
  3596. if ($res) {
  3597. $classname = 'mod'.$moduletoreloadshort;
  3598. $mod = new $classname($db);
  3599. //$mod->remove('noboxes');
  3600. $mod->delete_menus(); // We must delete to be sure it is inserted with new values
  3601. $mod->init($reloadmode);
  3602. } else {
  3603. dolibarr_install_syslog('Failed to include '.DOL_DOCUMENT_ROOT.'/core/modules/mod'.$moduletoreloadshort.'.class.php');
  3604. $res = @dol_include_once(strtolower($moduletoreloadshort).'/core/modules/mod'.$moduletoreloadshort.'.class.php');
  3605. if ($res) {
  3606. $classname = 'mod'.$moduletoreloadshort;
  3607. $mod = new $classname($db);
  3608. $mod->init($reloadmode);
  3609. } else {
  3610. dolibarr_install_syslog('Failed to include '.strtolower($moduletoreloadshort).'/core/modules/mod'.$moduletoreloadshort.'.class.php', LOG_ERR);
  3611. print "Error, can't find module with name ".$moduletoreload."\n";
  3612. return -1;
  3613. }
  3614. }
  3615. } else {
  3616. dolibarr_install_syslog("Error, can't find module with name ".$moduletoreload, LOG_ERR);
  3617. print "Error, can't find module with name ".$moduletoreload."\n";
  3618. return -1;
  3619. }
  3620. }
  3621. if (!empty($mod) && is_object($mod)) {
  3622. print '<tr class="trforrunsql"><td colspan="4">';
  3623. print '<b>'.$langs->trans('Upgrade').'</b>: ';
  3624. print $langs->trans('MigrationReloadModule').' '.$mod->getName(); // We keep getName outside of trans because getName is already encoded/translated
  3625. print "<!-- (".$reloadmode.") -->";
  3626. print "<br>\n";
  3627. print '</td></tr>';
  3628. }
  3629. }
  3630. return 1;
  3631. }
  3632. /**
  3633. * Reload SQL menu file (if dynamic menus, if modified by version)
  3634. *
  3635. * @param DoliDB $db Database handler
  3636. * @param Translate $langs Object langs
  3637. * @param Conf $conf Object conf
  3638. * @return int <0 if KO, >0 if OK
  3639. */
  3640. function migrate_reload_menu($db, $langs, $conf)
  3641. {
  3642. global $conf;
  3643. dolibarr_install_syslog("upgrade2::migrate_reload_menu");
  3644. // Define list of menu handlers to initialize
  3645. $listofmenuhandler = array();
  3646. if ($conf->global->MAIN_MENU_STANDARD == 'auguria_menu' || $conf->global->MAIN_MENU_SMARTPHONE == 'auguria_menu'
  3647. || $conf->global->MAIN_MENUFRONT_STANDARD == 'auguria_menu' || $conf->global->MAIN_MENUFRONT_SMARTPHONE == 'auguria_menu') {
  3648. $listofmenuhandler['auguria'] = 1; // We set here only dynamic menu handlers
  3649. }
  3650. foreach ($listofmenuhandler as $key => $val) {
  3651. print '<tr class="trforrunsql"><td colspan="4">';
  3652. //print "x".$key;
  3653. print '<br>';
  3654. print '<b>'.$langs->trans('Upgrade').'</b>: '.$langs->trans('MenuHandler')." ".$key."<br>\n";
  3655. // Load sql ini_menu_handler.sql file
  3656. $dir = DOL_DOCUMENT_ROOT."/core/menus/";
  3657. $file = 'init_menu_'.$key.'.sql';
  3658. if (file_exists($dir.$file)) {
  3659. $result = run_sql($dir.$file, 1, '', 1, $key);
  3660. }
  3661. print '</td></tr>';
  3662. }
  3663. return 1;
  3664. }
  3665. /**
  3666. * Migrate file from old path to new one for users
  3667. *
  3668. * @return void
  3669. */
  3670. function migrate_user_photospath()
  3671. {
  3672. global $conf, $db, $langs, $user;
  3673. print '<tr><td colspan="4">';
  3674. print '<b>'.$langs->trans('MigrationUserPhotoPath')."</b><br>\n";
  3675. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  3676. $fuser = new User($db);
  3677. if (!is_object($user)) {
  3678. $user = $fuser; // To avoid error during migration
  3679. }
  3680. $sql = "SELECT rowid as uid, entity from ".MAIN_DB_PREFIX."user"; // Get list of all users
  3681. $resql = $db->query($sql);
  3682. if ($resql) {
  3683. while ($obj = $db->fetch_object($resql)) {
  3684. //$fuser->fetch($obj->uid);
  3685. $fuser->id = $obj->uid;
  3686. $fuser->entity = $obj->entity;
  3687. //echo '<hr>'.$fuser->id.' -> '.$fuser->entity;
  3688. $entity = (empty($fuser->entity) ? 1 : $fuser->entity);
  3689. if ($entity > 1) {
  3690. $dir = DOL_DATA_ROOT.'/'.$entity.'/users';
  3691. } else {
  3692. $dir = $conf->user->multidir_output[$entity]; // $conf->user->multidir_output[] for each entity is construct by the multicompany module
  3693. }
  3694. if ($dir) {
  3695. //print "Process user id ".$fuser->id."<br>\n";
  3696. $origin = $dir.'/'.get_exdir($fuser->id, 2, 0, 1, $fuser, 'user'); // Use old behaviour to get x/y path
  3697. $destin = $dir.'/'.$fuser->id;
  3698. $origin_osencoded = dol_osencode($origin);
  3699. dol_mkdir($destin);
  3700. //echo '<hr>'.$origin.' -> '.$destin;
  3701. if (dol_is_dir($origin)) {
  3702. $handle = opendir($origin_osencoded);
  3703. if (is_resource($handle)) {
  3704. while (($file = readdir($handle)) !== false) {
  3705. if ($file == '.' || $file == '..') {
  3706. continue;
  3707. }
  3708. if (dol_is_dir($origin.'/'.$file)) { // it is a dir (like 'thumbs')
  3709. $thumbs = opendir($origin_osencoded.'/'.$file);
  3710. if (is_resource($thumbs)) {
  3711. dol_mkdir($destin.'/'.$file);
  3712. while (($thumb = readdir($thumbs)) !== false) {
  3713. if (!dol_is_file($destin.'/'.$file.'/'.$thumb)) {
  3714. if ($thumb == '.' || $thumb == '..') {
  3715. continue;
  3716. }
  3717. //print $origin.'/'.$file.'/'.$thumb.' -> '.$destin.'/'.$file.'/'.$thumb.'<br>'."\n";
  3718. print '.';
  3719. dol_copy($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb, 0, 0);
  3720. //var_dump('aaa');exit;
  3721. }
  3722. }
  3723. // dol_delete_dir($origin.'/'.$file);
  3724. }
  3725. } else { // it is a file
  3726. if (!dol_is_file($destin.'/'.$file)) {
  3727. //print $origin.'/'.$file.' -> '.$destin.'/'.$file.'<br>'."\n";
  3728. print '.';
  3729. dol_copy($origin.'/'.$file, $destin.'/'.$file, 0, 0);
  3730. //var_dump('eee');exit;
  3731. }
  3732. }
  3733. }
  3734. }
  3735. }
  3736. }
  3737. }
  3738. }
  3739. print '</td></tr>';
  3740. }
  3741. /**
  3742. * Migrate file from old path users/99/file.jpg into users/99/photos/file.jpg
  3743. *
  3744. * @return void
  3745. */
  3746. function migrate_user_photospath2()
  3747. {
  3748. global $db, $langs, $user;
  3749. print '<tr><td colspan="4">';
  3750. print '<b>'.$langs->trans('MigrationUserPhotoPath')."</b><br>\n";
  3751. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  3752. $fuser = new User($db);
  3753. if (!is_object($user)) {
  3754. $user = $fuser; // To avoid error during migration
  3755. }
  3756. $sql = "SELECT rowid as uid, entity, photo from ".MAIN_DB_PREFIX."user"; // Get list of all users
  3757. $resql = $db->query($sql);
  3758. if ($resql) {
  3759. while ($obj = $db->fetch_object($resql)) {
  3760. //$fuser->fetch($obj->uid);
  3761. $fuser->id = $obj->uid;
  3762. $fuser->entity = $obj->entity;
  3763. $fuser->photo = $obj->photo;
  3764. //echo '<hr>'.$fuser->id.' -> '.$fuser->entity;
  3765. $entity = (empty($fuser->entity) ? 1 : $fuser->entity);
  3766. if ($entity > 1) {
  3767. $dir = DOL_DATA_ROOT.'/'.$entity.'/users';
  3768. } else {
  3769. $dir = DOL_DATA_ROOT.'/users';
  3770. }
  3771. if ($dir) {
  3772. //print "Process user id ".$fuser->id."<br>\n";
  3773. $origin = $dir.'/'.$fuser->id;
  3774. $destin = $dir.'/'.$fuser->id.'/photos';
  3775. $origin_osencoded = dol_osencode($origin);
  3776. dol_mkdir($destin);
  3777. //echo '<hr>'.$origin.' -> '.$destin;
  3778. if (dol_is_dir($origin)) {
  3779. $handle = opendir($origin_osencoded);
  3780. if (is_resource($handle)) {
  3781. while (($file = readdir($handle)) !== false) {
  3782. if ($file == '.' || $file == '..' || $file == 'photos') {
  3783. continue;
  3784. }
  3785. if (!empty($fuser->photo) && ($file != $fuser->photo && $file != 'thumbs')) {
  3786. continue;
  3787. }
  3788. if (dol_is_dir($origin.'/'.$file)) { // it is a dir (like 'thumbs')
  3789. $thumbs = opendir($origin_osencoded.'/'.$file);
  3790. if (is_resource($thumbs)) {
  3791. dol_mkdir($destin.'/'.$file);
  3792. while (($thumb = readdir($thumbs)) !== false) {
  3793. if (!dol_is_file($destin.'/'.$file.'/'.$thumb)) {
  3794. if ($thumb == '.' || $thumb == '..') {
  3795. continue;
  3796. }
  3797. //print $origin.'/'.$file.'/'.$thumb.' -> '.$destin.'/'.$file.'/'.$thumb.'<br>'."\n";
  3798. print '.';
  3799. dol_copy($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb, 0, 0);
  3800. }
  3801. }
  3802. // dol_delete_dir($origin.'/'.$file);
  3803. }
  3804. } else { // it is a file
  3805. if (!dol_is_file($destin.'/'.$file)) {
  3806. //print $origin.'/'.$file.' -> '.$destin.'/'.$file.'<br>'."\n";
  3807. print '.';
  3808. dol_copy($origin.'/'.$file, $destin.'/'.$file, 0, 0);
  3809. }
  3810. }
  3811. }
  3812. }
  3813. }
  3814. }
  3815. }
  3816. }
  3817. print '</td></tr>';
  3818. }
  3819. /* A faire egalement: Modif statut paye et fk_facture des factures payes completement
  3820. On recherche facture incorrecte:
  3821. select f.rowid, f.total_ttc as t1, sum(pf.amount) as t2 from llx_facture as f, llx_paiement_facture as pf where pf.fk_facture=f.rowid and f.fk_statut in(2,3) and paye=0 and close_code is null group by f.rowid
  3822. having f.total_ttc = sum(pf.amount)
  3823. On les corrige:
  3824. update llx_facture set paye=1, fk_statut=2 where close_code is null
  3825. and rowid in (...)
  3826. */
  3827. /**
  3828. * Migrate users fields facebook and co to socialnetworks.
  3829. * Can be called only when version is 10.0.* or lower. Fields does not exists after.
  3830. *
  3831. * @return void
  3832. */
  3833. function migrate_users_socialnetworks()
  3834. {
  3835. global $db, $langs;
  3836. // skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
  3837. $error = 0;
  3838. $db->begin();
  3839. print '<tr><td colspan="4">';
  3840. $sql = 'SELECT rowid, socialnetworks';
  3841. $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'user WHERE';
  3842. $sql .= " skype IS NOT NULL OR skype <> ''";
  3843. $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
  3844. $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
  3845. $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
  3846. $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
  3847. $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
  3848. $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
  3849. $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
  3850. $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
  3851. //print $sql;
  3852. $resql = $db->query($sql);
  3853. if ($resql) {
  3854. while ($obj = $db->fetch_object($resql)) {
  3855. $arraysocialnetworks = array();
  3856. if (!empty($obj->skype)) {
  3857. $arraysocialnetworks['skype'] = $obj->skype;
  3858. }
  3859. if (!empty($obj->twitter)) {
  3860. $arraysocialnetworks['twitter'] = $obj->twitter;
  3861. }
  3862. if (!empty($obj->facebook)) {
  3863. $arraysocialnetworks['facebook'] = $obj->facebook;
  3864. }
  3865. if (!empty($obj->linkedin)) {
  3866. $arraysocialnetworks['linkedin'] = $obj->linkedin;
  3867. }
  3868. if (!empty($obj->instagram)) {
  3869. $arraysocialnetworks['instagram'] = $obj->instagram;
  3870. }
  3871. if (!empty($obj->snapchat)) {
  3872. $arraysocialnetworks['snapchat'] = $obj->snapchat;
  3873. }
  3874. if (!empty($obj->googleplus)) {
  3875. $arraysocialnetworks['googleplus'] = $obj->googleplus;
  3876. }
  3877. if (!empty($obj->youtube)) {
  3878. $arraysocialnetworks['youtube'] = $obj->youtube;
  3879. }
  3880. if (!empty($obj->whatsapp)) {
  3881. $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
  3882. }
  3883. if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
  3884. $obj->socialnetworks = '[]';
  3885. }
  3886. $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
  3887. $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."user SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
  3888. $sqlupd .= ', skype=null';
  3889. $sqlupd .= ', twitter=null';
  3890. $sqlupd .= ', facebook=null';
  3891. $sqlupd .= ', linkedin=null';
  3892. $sqlupd .= ', instagram=null';
  3893. $sqlupd .= ', snapchat=null';
  3894. $sqlupd .= ', googleplus=null';
  3895. $sqlupd .= ', youtube=null';
  3896. $sqlupd .= ', whatsapp=null';
  3897. $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
  3898. //print $sqlupd."<br>";
  3899. $resqlupd = $db->query($sqlupd);
  3900. if (!$resqlupd) {
  3901. dol_print_error($db);
  3902. $error++;
  3903. }
  3904. }
  3905. } else {
  3906. $error++;
  3907. }
  3908. if (!$error) {
  3909. $db->commit();
  3910. } else {
  3911. dol_print_error($db);
  3912. $db->rollback();
  3913. }
  3914. print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Users')."</b><br>\n";
  3915. print '</td></tr>';
  3916. }
  3917. /**
  3918. * Migrate members fields facebook and co to socialnetworks
  3919. * Can be called only when version is 10.0.* or lower. Fields does not exists after.
  3920. *
  3921. * @return void
  3922. */
  3923. function migrate_members_socialnetworks()
  3924. {
  3925. global $db, $langs;
  3926. print '<tr><td colspan="4">';
  3927. $error = 0;
  3928. $db->begin();
  3929. print '<tr><td colspan="4">';
  3930. $sql = 'SELECT rowid, socialnetworks';
  3931. $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'adherent WHERE ';
  3932. $sql .= " skype IS NOT NULL OR skype <> ''";
  3933. $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
  3934. $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
  3935. $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
  3936. $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
  3937. $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
  3938. $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
  3939. $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
  3940. $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
  3941. //print $sql;
  3942. $resql = $db->query($sql);
  3943. if ($resql) {
  3944. while ($obj = $db->fetch_object($resql)) {
  3945. $arraysocialnetworks = array();
  3946. if (!empty($obj->skype)) {
  3947. $arraysocialnetworks['skype'] = $obj->skype;
  3948. }
  3949. if (!empty($obj->twitter)) {
  3950. $arraysocialnetworks['twitter'] = $obj->twitter;
  3951. }
  3952. if (!empty($obj->facebook)) {
  3953. $arraysocialnetworks['facebook'] = $obj->facebook;
  3954. }
  3955. if (!empty($obj->linkedin)) {
  3956. $arraysocialnetworks['linkedin'] = $obj->linkedin;
  3957. }
  3958. if (!empty($obj->instagram)) {
  3959. $arraysocialnetworks['instagram'] = $obj->instagram;
  3960. }
  3961. if (!empty($obj->snapchat)) {
  3962. $arraysocialnetworks['snapchat'] = $obj->snapchat;
  3963. }
  3964. if (!empty($obj->googleplus)) {
  3965. $arraysocialnetworks['googleplus'] = $obj->googleplus;
  3966. }
  3967. if (!empty($obj->youtube)) {
  3968. $arraysocialnetworks['youtube'] = $obj->youtube;
  3969. }
  3970. if (!empty($obj->whatsapp)) {
  3971. $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
  3972. }
  3973. if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
  3974. $obj->socialnetworks = '[]';
  3975. }
  3976. $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
  3977. $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."adherent SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
  3978. $sqlupd .= ', skype=null';
  3979. $sqlupd .= ', twitter=null';
  3980. $sqlupd .= ', facebook=null';
  3981. $sqlupd .= ', linkedin=null';
  3982. $sqlupd .= ', instagram=null';
  3983. $sqlupd .= ', snapchat=null';
  3984. $sqlupd .= ', googleplus=null';
  3985. $sqlupd .= ', youtube=null';
  3986. $sqlupd .= ', whatsapp=null';
  3987. $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
  3988. //print $sqlupd."<br>";
  3989. $resqlupd = $db->query($sqlupd);
  3990. if (!$resqlupd) {
  3991. dol_print_error($db);
  3992. $error++;
  3993. }
  3994. }
  3995. } else {
  3996. $error++;
  3997. }
  3998. if (!$error) {
  3999. $db->commit();
  4000. } else {
  4001. dol_print_error($db);
  4002. $db->rollback();
  4003. }
  4004. print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Members')."</b><br>\n";
  4005. print '</td></tr>';
  4006. }
  4007. /**
  4008. * Migrate contacts fields facebook and co to socialnetworks
  4009. * Can be called only when version is 10.0.* or lower. Fields does not exists after.
  4010. *
  4011. * @return void
  4012. */
  4013. function migrate_contacts_socialnetworks()
  4014. {
  4015. global $db, $langs;
  4016. // jabberid,skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
  4017. $error = 0;
  4018. $db->begin();
  4019. print '<tr><td colspan="4">';
  4020. $sql = 'SELECT rowid, socialnetworks';
  4021. $sql .= ', jabberid, skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'socpeople WHERE';
  4022. $sql .= " jabberid IS NOT NULL OR jabberid <> ''";
  4023. $sql .= " OR skype IS NOT NULL OR skype <> ''";
  4024. $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
  4025. $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
  4026. $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
  4027. $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
  4028. $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
  4029. $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
  4030. $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
  4031. $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
  4032. //print $sql;
  4033. $resql = $db->query($sql);
  4034. if ($resql) {
  4035. while ($obj = $db->fetch_object($resql)) {
  4036. $arraysocialnetworks = array();
  4037. if (!empty($obj->jabberid)) {
  4038. $arraysocialnetworks['jabber'] = $obj->jabberid;
  4039. }
  4040. if (!empty($obj->skype)) {
  4041. $arraysocialnetworks['skype'] = $obj->skype;
  4042. }
  4043. if (!empty($obj->twitter)) {
  4044. $arraysocialnetworks['twitter'] = $obj->twitter;
  4045. }
  4046. if (!empty($obj->facebook)) {
  4047. $arraysocialnetworks['facebook'] = $obj->facebook;
  4048. }
  4049. if (!empty($obj->linkedin)) {
  4050. $arraysocialnetworks['linkedin'] = $obj->linkedin;
  4051. }
  4052. if (!empty($obj->instagram)) {
  4053. $arraysocialnetworks['instagram'] = $obj->instagram;
  4054. }
  4055. if (!empty($obj->snapchat)) {
  4056. $arraysocialnetworks['snapchat'] = $obj->snapchat;
  4057. }
  4058. if (!empty($obj->googleplus)) {
  4059. $arraysocialnetworks['googleplus'] = $obj->googleplus;
  4060. }
  4061. if (!empty($obj->youtube)) {
  4062. $arraysocialnetworks['youtube'] = $obj->youtube;
  4063. }
  4064. if (!empty($obj->whatsapp)) {
  4065. $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
  4066. }
  4067. if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
  4068. $obj->socialnetworks = '[]';
  4069. }
  4070. $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
  4071. $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."socpeople SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
  4072. $sqlupd .= ', jabberid=null';
  4073. $sqlupd .= ', skype=null';
  4074. $sqlupd .= ', twitter=null';
  4075. $sqlupd .= ', facebook=null';
  4076. $sqlupd .= ', linkedin=null';
  4077. $sqlupd .= ', instagram=null';
  4078. $sqlupd .= ', snapchat=null';
  4079. $sqlupd .= ', googleplus=null';
  4080. $sqlupd .= ', youtube=null';
  4081. $sqlupd .= ', whatsapp=null';
  4082. $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
  4083. //print $sqlupd."<br>";
  4084. $resqlupd = $db->query($sqlupd);
  4085. if (!$resqlupd) {
  4086. dol_print_error($db);
  4087. $error++;
  4088. }
  4089. }
  4090. } else {
  4091. $error++;
  4092. }
  4093. if (!$error) {
  4094. $db->commit();
  4095. } else {
  4096. dol_print_error($db);
  4097. $db->rollback();
  4098. }
  4099. print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Contacts')."</b><br>\n";
  4100. print '</td></tr>';
  4101. }
  4102. /**
  4103. * Migrate thirdparties fields facebook and co to socialnetworks
  4104. * Can be called only when version is 10.0.* or lower. Fields does not exists after.
  4105. *
  4106. * @return void
  4107. */
  4108. function migrate_thirdparties_socialnetworks()
  4109. {
  4110. global $db, $langs;
  4111. // skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
  4112. $error = 0;
  4113. $db->begin();
  4114. print '<tr><td colspan="4">';
  4115. $sql = 'SELECT rowid, socialnetworks';
  4116. $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'societe WHERE ';
  4117. $sql .= " skype IS NOT NULL OR skype <> ''";
  4118. $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
  4119. $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
  4120. $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
  4121. $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
  4122. $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
  4123. $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
  4124. $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
  4125. $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
  4126. //print $sql;
  4127. $resql = $db->query($sql);
  4128. if ($resql) {
  4129. while ($obj = $db->fetch_object($resql)) {
  4130. $arraysocialnetworks = array();
  4131. if (!empty($obj->skype)) {
  4132. $arraysocialnetworks['skype'] = $obj->skype;
  4133. }
  4134. if (!empty($obj->twitter)) {
  4135. $arraysocialnetworks['twitter'] = $obj->twitter;
  4136. }
  4137. if (!empty($obj->facebook)) {
  4138. $arraysocialnetworks['facebook'] = $obj->facebook;
  4139. }
  4140. if (!empty($obj->linkedin)) {
  4141. $arraysocialnetworks['linkedin'] = $obj->linkedin;
  4142. }
  4143. if (!empty($obj->instagram)) {
  4144. $arraysocialnetworks['instagram'] = $obj->instagram;
  4145. }
  4146. if (!empty($obj->snapchat)) {
  4147. $arraysocialnetworks['snapchat'] = $obj->snapchat;
  4148. }
  4149. if (!empty($obj->googleplus)) {
  4150. $arraysocialnetworks['googleplus'] = $obj->googleplus;
  4151. }
  4152. if (!empty($obj->youtube)) {
  4153. $arraysocialnetworks['youtube'] = $obj->youtube;
  4154. }
  4155. if (!empty($obj->whatsapp)) {
  4156. $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
  4157. }
  4158. if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
  4159. $obj->socialnetworks = '[]';
  4160. }
  4161. $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
  4162. $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."societe SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
  4163. $sqlupd .= ', skype=null';
  4164. $sqlupd .= ', twitter=null';
  4165. $sqlupd .= ', facebook=null';
  4166. $sqlupd .= ', linkedin=null';
  4167. $sqlupd .= ', instagram=null';
  4168. $sqlupd .= ', snapchat=null';
  4169. $sqlupd .= ', googleplus=null';
  4170. $sqlupd .= ', youtube=null';
  4171. $sqlupd .= ', whatsapp=null';
  4172. $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
  4173. //print $sqlupd."<br>";
  4174. $resqlupd = $db->query($sqlupd);
  4175. if (!$resqlupd) {
  4176. dol_print_error($db);
  4177. $error++;
  4178. }
  4179. }
  4180. } else {
  4181. $error++;
  4182. }
  4183. if (!$error) {
  4184. $db->commit();
  4185. } else {
  4186. dol_print_error($db);
  4187. $db->rollback();
  4188. }
  4189. print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Thirdparties')."</b><br>\n";
  4190. print '</td></tr>';
  4191. }
  4192. /**
  4193. * Migrate export and import profiles to fix field name that was renamed
  4194. *
  4195. * @param string $mode 'export' or 'import'
  4196. * @return void
  4197. */
  4198. function migrate_export_import_profiles($mode = 'export')
  4199. {
  4200. global $db, $langs;
  4201. $error = 0;
  4202. $resultstring = '';
  4203. $db->begin();
  4204. print '<tr class="trforrunsql"><td colspan="4">';
  4205. $sql = 'SELECT rowid, field';
  4206. if ($mode == 'export') {
  4207. $sql .= ', filter';
  4208. }
  4209. $sql .= ' FROM '.MAIN_DB_PREFIX.$mode.'_model WHERE';
  4210. $sql .= " type LIKE 'propale_%' OR type LIKE 'commande_%' OR type LIKE 'facture_%'";
  4211. //print $sql;
  4212. $resql = $db->query($sql);
  4213. if ($resql) {
  4214. while ($obj = $db->fetch_object($resql)) {
  4215. $oldfield = $obj->field;
  4216. $newfield = str_replace(array(',f.facnumber', 'f.facnumber,', 'f.total,', 'f.tva,'), array(',f.ref', 'f.ref,', 'f.total_ht,', 'f.total_tva,'), $oldfield);
  4217. if ($mode == 'export') {
  4218. $oldfilter = $obj->filter;
  4219. $newfilter = str_replace(array('f.facnumber=', 'f.total=', 'f.tva='), array('f.ref=', 'f.total_ht=', 'f.total_tva='), $oldfilter);
  4220. } else {
  4221. $oldfilter = '';
  4222. $newfilter = '';
  4223. }
  4224. if ($oldfield != $newfield || $oldfilter != $newfilter) {
  4225. $sqlupd = 'UPDATE '.MAIN_DB_PREFIX.$mode."_model SET field = '".$db->escape($newfield)."'";
  4226. if ($mode == 'export') {
  4227. $sqlupd .= ", filter = '".$db->escape($newfilter)."'";
  4228. }
  4229. $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
  4230. $resultstring .= '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$sqlupd."</td></tr>\n";
  4231. $resqlupd = $db->query($sqlupd);
  4232. if (!$resqlupd) {
  4233. dol_print_error($db);
  4234. $error++;
  4235. }
  4236. }
  4237. }
  4238. } else {
  4239. $error++;
  4240. }
  4241. if (!$error) {
  4242. $db->commit();
  4243. } else {
  4244. dol_print_error($db);
  4245. $db->rollback();
  4246. }
  4247. print '<b>'.$langs->trans('MigrationImportOrExportProfiles', $mode)."</b><br>\n";
  4248. print '</td></tr>';
  4249. if ($resultstring) {
  4250. print $resultstring;
  4251. } else {
  4252. print '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$langs->trans("NothingToDo")."</td></tr>\n";
  4253. }
  4254. }
  4255. /**
  4256. * Migrate Rank into contract line
  4257. *
  4258. * @return void
  4259. */
  4260. function migrate_contractdet_rank()
  4261. {
  4262. global $db, $langs;
  4263. $error = 0;
  4264. $resultstring = '';
  4265. $db->begin();
  4266. print '<tr class="trforrunsql"><td colspan="4">';
  4267. print '<b>'.$langs->trans('MigrationContractLineRank')."</b><br>\n";
  4268. $sql = "SELECT c.rowid as cid ,cd.rowid as cdid,cd.rang FROM ".$db->prefix()."contratdet as cd INNER JOIN ".$db->prefix()."contrat as c ON c.rowid=cd.fk_contrat AND cd.rang=0";
  4269. $sql .=" ORDER BY c.rowid,cd.rowid";
  4270. $resql = $db->query($sql);
  4271. if ($resql) {
  4272. $currentRank=0;
  4273. $current_contract=0;
  4274. while ($obj = $db->fetch_object($resql)) {
  4275. if (empty($current_contract) || $current_contract==$obj->cid) {
  4276. $currentRank++;
  4277. } else {
  4278. $currentRank=1;
  4279. }
  4280. $sqlUpd = "UPDATE ".$db->prefix()."contratdet SET rang=".(int) $currentRank." WHERE rowid=".(int) $obj->cdid;
  4281. $resultstring = '.';
  4282. print $resultstring;
  4283. $resqlUpd = $db->query($sqlUpd);
  4284. if (!$resqlUpd) {
  4285. dol_print_error($db);
  4286. $error++;
  4287. }
  4288. $current_contract = $obj->cid;
  4289. }
  4290. } else {
  4291. $error++;
  4292. }
  4293. if (!$error) {
  4294. $db->commit();
  4295. } else {
  4296. $db->rollback();
  4297. }
  4298. print '</td></tr>';
  4299. if (!$resultstring) {
  4300. print '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$langs->trans("NothingToDo")."</td></tr>\n";
  4301. }
  4302. }