upgrade2.php 150 KB

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