upgrade2.php 164 KB

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