12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033 |
- <?php
- /* Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
- * Copyright (C) 2005-2018 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
- * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
- * Copyright (C) 2015-2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- * Upgrade2 scripts can be ran from command line with syntax:
- *
- * cd htdocs/install
- * php upgrade.php 3.4.0 3.5.0 [dirmodule|ignoredbversion]
- * php upgrade2.php 3.4.0 3.5.0 [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE]
- *
- * And for final step:
- * php step5.php 3.4.0 3.5.0
- *
- * Return code is 0 if OK, >0 if error
- *
- * Note: To just enable a module from command line, use this syntax:
- * php upgrade2.php 0.0.0 0.0.0 [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE]
- */
- /**
- * \file htdocs/install/upgrade2.php
- * \brief Upgrade some data
- */
- include_once 'inc.php';
- if (!file_exists($conffile))
- {
- 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").';
- }
- require_once $conffile;
- require_once $dolibarr_main_document_root.'/compta/facture/class/facture.class.php';
- require_once $dolibarr_main_document_root.'/comm/propal/class/propal.class.php';
- require_once $dolibarr_main_document_root.'/contrat/class/contrat.class.php';
- require_once $dolibarr_main_document_root.'/commande/class/commande.class.php';
- require_once $dolibarr_main_document_root.'/fourn/class/fournisseur.commande.class.php';
- require_once $dolibarr_main_document_root.'/core/lib/price.lib.php';
- require_once $dolibarr_main_document_root.'/core/class/menubase.class.php';
- require_once $dolibarr_main_document_root.'/core/lib/files.lib.php';
- global $langs;
- $grant_query = '';
- $step = 2;
- $error = 0;
- // Cette page peut etre longue. On augmente le delai autorise.
- // Ne fonctionne que si on est pas en safe_mode.
- $err = error_reporting();
- error_reporting(0);
- if (!empty($conf->global->MAIN_OVERRIDE_TIME_LIMIT))
- @set_time_limit((int) $conf->global->MAIN_OVERRIDE_TIME_LIMIT);
- else @set_time_limit(600);
- error_reporting($err);
- $setuplang = GETPOST("selectlang", 'aZ09', 3) ?GETPOST("selectlang", 'aZ09', 3) : 'auto';
- $langs->setDefaultLang($setuplang);
- $versionfrom = GETPOST("versionfrom", 'alpha', 3) ?GETPOST("versionfrom", 'alpha', 3) : (empty($argv[1]) ? '' : $argv[1]);
- $versionto = GETPOST("versionto", 'alpha', 3) ?GETPOST("versionto", 'alpha', 3) : (empty($argv[2]) ? '' : $argv[2]);
- $enablemodules = GETPOST("enablemodules", 'alpha', 3) ?GETPOST("enablemodules", 'alpha', 3) : (empty($argv[3]) ? '' : $argv[3]);
- $langs->loadLangs(array("admin", "install", "bills", "suppliers"));
- if ($dolibarr_main_db_type == 'mysqli') $choix = 1;
- if ($dolibarr_main_db_type == 'pgsql') $choix = 2;
- if ($dolibarr_main_db_type == 'mssql') $choix = 3;
- dolibarr_install_syslog("--- upgrade2: entering upgrade2.php page ".$versionfrom." ".$versionto." ".$enablemodules);
- if (!is_object($conf)) dolibarr_install_syslog("upgrade2: conf file not initialized", LOG_ERR);
- /*
- * View
- */
- if ((!$versionfrom || preg_match('/version/', $versionfrom)) && (!$versionto || preg_match('/version/', $versionto)))
- {
- print 'Error: Parameter versionfrom or versionto missing or having a bad format.'."\n";
- print 'Upgrade must be ran from command line with parameters or called from page install/index.php (like a first install)'."\n";
- // Test if batch mode
- $sapi_type = php_sapi_name();
- $script_file = basename(__FILE__);
- $path = __DIR__.'/';
- if (substr($sapi_type, 0, 3) == 'cli')
- {
- print 'Syntax from command line: '.$script_file." x.y.z a.b.c [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE...]\n";
- }
- exit;
- }
- pHeader('', 'step5', GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : 'upgrade', 'versionfrom='.$versionfrom.'&versionto='.$versionto);
- if (!GETPOST('action', 'aZ09') || preg_match('/upgrade/i', GETPOST('action', 'aZ09')))
- {
- print '<h3><img class="valigntextbottom" src="../theme/common/octicons/build/svg/database.svg" width="20" alt="Database"> '.$langs->trans('DataMigration').'</h3>';
- print '<table cellspacing="0" cellpadding="1" border="0" width="100%">';
- // If password is encoded, we decode it
- if (preg_match('/crypted:/i', $dolibarr_main_db_pass) || !empty($dolibarr_main_db_encrypted_pass))
- {
- require_once $dolibarr_main_document_root.'/core/lib/security.lib.php';
- if (preg_match('/crypted:/i', $dolibarr_main_db_pass))
- {
- $dolibarr_main_db_pass = preg_replace('/crypted:/i', '', $dolibarr_main_db_pass);
- $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_pass);
- $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
- } else $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_encrypted_pass);
- }
- // $conf is already instancied inside inc.php
- $conf->db->type = $dolibarr_main_db_type;
- $conf->db->host = $dolibarr_main_db_host;
- $conf->db->port = $dolibarr_main_db_port;
- $conf->db->name = $dolibarr_main_db_name;
- $conf->db->user = $dolibarr_main_db_user;
- $conf->db->pass = $dolibarr_main_db_pass;
- $db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, $conf->db->port);
- // Create the global $hookmanager object
- include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
- $hookmanager = new HookManager($db);
- $hookmanager->initHooks(array('upgrade'));
- if (!$db->connected)
- {
- print '<tr><td colspan="4">'.$langs->trans("ErrorFailedToConnectToDatabase", $conf->db->name).'</td><td class="right">'.$langs->trans('Error').'</td></tr>';
- dolibarr_install_syslog('upgrade2: failed to connect to database :'.$conf->db->name.' on '.$conf->db->host.' for user '.$conf->db->user, LOG_ERR);
- $error++;
- }
- if (!$error)
- {
- if ($db->database_selected)
- {
- dolibarr_install_syslog('upgrade2: database connection successful :'.$dolibarr_main_db_name);
- } else {
- $error++;
- }
- }
- if (empty($dolibarr_main_db_encryption)) $dolibarr_main_db_encryption = 0;
- $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
- if (empty($dolibarr_main_db_cryptkey)) $dolibarr_main_db_cryptkey = '';
- $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
- // Chargement config
- if (!$error)
- {
- $conf->setValues($db);
- // Reset forced setup after the setValues
- if (defined('SYSLOG_FILE')) $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
- $conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
- }
- /***************************************************************************************
- *
- * Migration of data
- *
- ***************************************************************************************/
- $db->begin();
- if (!$error)
- {
- // Current version is $conf->global->MAIN_VERSION_LAST_UPGRADE
- // Version to install is DOL_VERSION
- $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 : ''));
- // Chaque action de migration doit renvoyer une ligne sur 4 colonnes avec
- // dans la 1ere colonne, la description de l'action a faire
- // dans la 4eme colonne, le texte 'OK' si fait ou 'AlreadyDone' si rien n'est fait ou 'Error'
- $versiontoarray = explode('.', $versionto);
- $versionranarray = explode('.', DOL_VERSION);
- // Force to execute this at begin to avoid the new core code into Dolibarr to be broken.
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN birth date';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN dateemployment date';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN dateemploymentend date';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN default_range integer';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN default_c_exp_tax_cat integer';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN langs varchar(24)';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN fieldcomputed text';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN fielddefault varchar(255)';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX."extrafields ADD COLUMN enabled varchar(255) DEFAULT '1'";
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN help text';
- $db->query($sql, 1);
- $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user_rights ADD COLUMN entity integer DEFAULT 1 NOT NULL';
- $db->query($sql, 1);
- $afterversionarray = explode('.', '2.0.0');
- $beforeversionarray = explode('.', '2.7.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- // Script pour V2 -> V2.1
- migrate_paiements($db, $langs, $conf);
- migrate_contracts_det($db, $langs, $conf);
- migrate_contracts_date1($db, $langs, $conf);
- migrate_contracts_date2($db, $langs, $conf);
- migrate_contracts_date3($db, $langs, $conf);
- migrate_contracts_open($db, $langs, $conf);
- migrate_modeles($db, $langs, $conf);
- migrate_price_propal($db, $langs, $conf);
- migrate_price_commande($db, $langs, $conf);
- migrate_price_commande_fournisseur($db, $langs, $conf);
- migrate_price_contrat($db, $langs, $conf);
- migrate_paiementfourn_facturefourn($db, $langs, $conf);
- // Script pour V2.1 -> V2.2
- migrate_paiements_orphelins_1($db, $langs, $conf);
- migrate_paiements_orphelins_2($db, $langs, $conf);
- migrate_links_transfert($db, $langs, $conf);
- // Script pour V2.2 -> V2.4
- migrate_commande_expedition($db, $langs, $conf);
- migrate_commande_livraison($db, $langs, $conf);
- migrate_detail_livraison($db, $langs, $conf);
- // Script pour V2.5 -> V2.6
- migrate_stocks($db, $langs, $conf);
- // Script pour V2.6 -> V2.7
- migrate_menus($db, $langs, $conf);
- migrate_commande_deliveryaddress($db, $langs, $conf);
- migrate_restore_missing_links($db, $langs, $conf);
- migrate_rename_directories($db, $langs, $conf, '/compta', '/banque');
- migrate_rename_directories($db, $langs, $conf, '/societe', '/mycompany');
- }
- // Script for 2.8
- $afterversionarray = explode('.', '2.7.9');
- $beforeversionarray = explode('.', '2.8.9');
- //print $versionto.' '.versioncompare($versiontoarray,$afterversionarray).' '.versioncompare($versiontoarray,$beforeversionarray);
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_price_facture($db, $langs, $conf); // Code of this function works for 2.8+ because need a field tva_tx
- migrate_relationship_tables($db, $langs, $conf, 'co_exp', 'fk_commande', 'commande', 'fk_expedition', 'shipping');
- migrate_relationship_tables($db, $langs, $conf, 'pr_exp', 'fk_propal', 'propal', 'fk_expedition', 'shipping');
- migrate_relationship_tables($db, $langs, $conf, 'pr_liv', 'fk_propal', 'propal', 'fk_livraison', 'delivery');
- migrate_relationship_tables($db, $langs, $conf, 'co_liv', 'fk_commande', 'commande', 'fk_livraison', 'delivery');
- migrate_relationship_tables($db, $langs, $conf, 'co_pr', 'fk_propale', 'propal', 'fk_commande', 'commande');
- migrate_relationship_tables($db, $langs, $conf, 'fa_pr', 'fk_propal', 'propal', 'fk_facture', 'facture');
- migrate_relationship_tables($db, $langs, $conf, 'co_fa', 'fk_commande', 'commande', 'fk_facture', 'facture');
- migrate_project_user_resp($db, $langs, $conf);
- migrate_project_task_actors($db, $langs, $conf);
- }
- // Script for 2.9
- $afterversionarray = explode('.', '2.8.9');
- $beforeversionarray = explode('.', '2.9.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_project_task_time($db, $langs, $conf);
- migrate_customerorder_shipping($db, $langs, $conf);
- migrate_shipping_delivery($db, $langs, $conf);
- migrate_shipping_delivery2($db, $langs, $conf);
- }
- // Script for 3.0
- $afterversionarray = explode('.', '2.9.9');
- $beforeversionarray = explode('.', '3.0.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- // No particular code
- }
- // Script for 3.1
- $afterversionarray = explode('.', '3.0.9');
- $beforeversionarray = explode('.', '3.1.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_rename_directories($db, $langs, $conf, '/rss', '/externalrss');
- migrate_actioncomm_element($db, $langs, $conf);
- }
- // Script for 3.2
- $afterversionarray = explode('.', '3.1.9');
- $beforeversionarray = explode('.', '3.2.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_price_contrat($db, $langs, $conf);
- migrate_mode_reglement($db, $langs, $conf);
- migrate_clean_association($db, $langs, $conf);
- }
- // Script for 3.3
- $afterversionarray = explode('.', '3.2.9');
- $beforeversionarray = explode('.', '3.3.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_categorie_association($db, $langs, $conf);
- }
- // Script for 3.4
- // No specific scripts
- // Tasks to do always and only into last targeted version
- $afterversionarray = explode('.', '3.6.9'); // target is after this
- $beforeversionarray = explode('.', '3.7.9'); // target is before this
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_event_assignement($db, $langs, $conf);
- }
- // Scripts for 3.9
- $afterversionarray = explode('.', '3.7.9');
- $beforeversionarray = explode('.', '3.8.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- // No particular code
- }
- // Scripts for 4.0
- $afterversionarray = explode('.', '3.9.9');
- $beforeversionarray = explode('.', '4.0.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_rename_directories($db, $langs, $conf, '/fckeditor', '/medias');
- }
- // Scripts for 5.0
- $afterversionarray = explode('.', '4.0.9');
- $beforeversionarray = explode('.', '5.0.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- // Migrate to add entity value into llx_societe_remise
- migrate_remise_entity($db, $langs, $conf);
- // Migrate to add entity value into llx_societe_remise_except
- migrate_remise_except_entity($db, $langs, $conf);
- }
- // Scripts for 6.0
- $afterversionarray = explode('.', '5.0.9');
- $beforeversionarray = explode('.', '6.0.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- if (!empty($conf->multicompany->enabled))
- {
- global $multicompany_transverse_mode;
- // Only if the transverse mode is not used
- if (empty($multicompany_transverse_mode))
- {
- // Migrate to add entity value into llx_user_rights
- migrate_user_rights_entity($db, $langs, $conf);
- // Migrate to add entity value into llx_usergroup_rights
- migrate_usergroup_rights_entity($db, $langs, $conf);
- }
- }
- }
- // Scripts for 7.0
- $afterversionarray = explode('.', '6.0.9');
- $beforeversionarray = explode('.', '7.0.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- // Migrate contact association
- migrate_event_assignement_contact($db, $langs, $conf);
- migrate_reset_blocked_log($db, $langs, $conf);
- }
- // Scripts for 8.0
- $afterversionarray = explode('.', '7.0.9');
- $beforeversionarray = explode('.', '8.0.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_rename_directories($db, $langs, $conf, '/contracts', '/contract');
- }
- // Scripts for 9.0
- $afterversionarray = explode('.', '8.0.9');
- $beforeversionarray = explode('.', '9.0.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0)
- {
- migrate_user_photospath();
- }
- // Scripts for 11.0
- $afterversionarray = explode('.', '10.0.9');
- $beforeversionarray = explode('.', '11.0.9');
- if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
- migrate_users_socialnetworks();
- migrate_members_socialnetworks();
- migrate_contacts_socialnetworks();
- migrate_thirdparties_socialnetworks();
- }
- }
- // Code executed only if migration is LAST ONE. Must always be done.
- if (versioncompare($versiontoarray, $versionranarray) >= 0 || versioncompare($versiontoarray, $versionranarray) <= -3)
- {
- // 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)
- $listofmodule = array(
- 'MAIN_MODULE_ACCOUNTING'=>'newboxdefonly',
- 'MAIN_MODULE_AGENDA'=>'newboxdefonly',
- 'MAIN_MODULE_BARCODE'=>'newboxdefonly',
- 'MAIN_MODULE_CRON'=>'newboxdefonly',
- 'MAIN_MODULE_COMMANDE'=>'newboxdefonly',
- 'MAIN_MODULE_DEPLACEMENT'=>'newboxdefonly',
- 'MAIN_MODULE_DON'=>'newboxdefonly',
- 'MAIN_MODULE_ECM'=>'newboxdefonly',
- 'MAIN_MODULE_EXTERNALSITE'=>'newboxdefonly',
- 'MAIN_MODULE_EXPENSEREPORT'=>'newboxdefonly',
- 'MAIN_MODULE_FACTURE'=>'newboxdefonly',
- 'MAIN_MODULE_FOURNISSEUR'=>'newboxdefonly',
- 'MAIN_MODULE_HOLIDAY'=>'newboxdefonly',
- 'MAIN_MODULE_OPENSURVEY'=>'newboxdefonly',
- 'MAIN_MODULE_PAYBOX'=>'newboxdefonly',
- 'MAIN_MODULE_PRINTING'=>'newboxdefonly',
- 'MAIN_MODULE_PRODUIT'=>'newboxdefonly',
- 'MAIN_MODULE_RESOURCE'=>'newboxdefonly',
- 'MAIN_MODULE_SALARIES'=>'newboxdefonly',
- 'MAIN_MODULE_SYSLOG'=>'newboxdefonly',
- 'MAIN_MODULE_SOCIETE'=>'newboxdefonly',
- 'MAIN_MODULE_SERVICE'=>'newboxdefonly',
- 'MAIN_MODULE_TAKEPOS'=>'newboxdefonly',
- 'MAIN_MODULE_USER'=>'newboxdefonly', //This one must be always done and only into last targeted version)
- 'MAIN_MODULE_VARIANTS'=>'newboxdefonly',
- 'MAIN_MODULE_WEBSITE'=>'newboxdefonly',
- );
- migrate_reload_modules($db, $langs, $conf, $listofmodule);
- // Reload menus (this must be always and only into last targeted version)
- migrate_reload_menu($db, $langs, $conf);
- }
- // Can force activation of some module during migration with parameter 'enablemodules=MAIN_MODULE_XXX,MAIN_MODULE_YYY,...'
- // In most cases (online install or upgrade) $enablemodules is empty. Can be forced when ran from command line.
- if (!$error && $enablemodules)
- {
- // Reload modules (this must be always done and only into last targeted version)
- $listofmodules = array();
- $enablemodules = preg_replace('/enablemodules=/', '', $enablemodules);
- $tmplistofmodules = explode(',', $enablemodules);
- foreach ($tmplistofmodules as $value)
- {
- $listofmodules[$value] = 'forceactivate';
- }
- migrate_reload_modules($db, $langs, $conf, $listofmodules, 1);
- }
- // Can call a dedicated external upgrade process
- if (!$error)
- {
- $parameters = array('versionfrom'=>$versionfrom, 'versionto='.$versionto);
- $object = new stdClass();
- $action = "upgrade";
- $reshook = $hookmanager->executeHooks('doUpgrade2', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
- if ($hookmanager->resNbOfHooks > 0)
- {
- if ($reshook < 0)
- {
- print '<tr><td colspan="4">';
- print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: ';
- print $hookmanager->error;
- print "<!-- (".$reshook.") -->";
- print '</td></tr>';
- } else {
- print '<tr><td colspan="4">';
- print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: OK';
- print "<!-- (".$reshook.") -->";
- print '</td></tr>';
- }
- } else {
- //if (! empty($conf->modules))
- if (!empty($conf->modules_parts['hooks'])) // If there is at least one module with one hook, we show message to say nothing was done
- {
- print '<tr><td colspan="4">';
- print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: '.$langs->trans("None");
- print '</td></tr>';
- }
- }
- }
- print '</table>';
- $sql = 'UPDATE '.MAIN_DB_PREFIX."const SET VALUE = 'torefresh' WHERE name = 'MAIN_FIRST_PING_OK_ID'";
- $db->query($sql, 1);
- // We always commit.
- // Process is designed so we can run it several times whatever is situation.
- $db->commit();
- $db->close();
- // Copy directory medias
- $srcroot = DOL_DOCUMENT_ROOT.'/install/medias';
- $destroot = DOL_DATA_ROOT.'/medias';
- dolCopyDir($srcroot, $destroot, 0, 0);
- // Actions for all versions (no database change but delete some files and directories)
- migrate_delete_old_files($db, $langs, $conf);
- migrate_delete_old_dir($db, $langs, $conf);
- // Actions for all versions (no database change but create some directories)
- dol_mkdir(DOL_DATA_ROOT.'/bank');
- // Actions for all versions (no database change but rename some directories)
- migrate_rename_directories($db, $langs, $conf, '/banque/bordereau', '/bank/checkdeposits');
- print '<div><br>'.$langs->trans("MigrationFinished").'</div>';
- } else {
- print '<div class="error">'.$langs->trans('ErrorWrongParameters').'</div>';
- $error++;
- }
- $ret = 0;
- if ($error && isset($argv[1])) $ret = 1;
- dolibarr_install_syslog("Exit ".$ret);
- dolibarr_install_syslog("--- upgrade2: end");
- pFooter($error ? 2 : 0, $setuplang);
- if ($db->connected) $db->close();
- // Return code if ran from command line
- if ($ret) exit($ret);
- /**
- * Reporte liens vers une facture de paiements sur table de jointure (lien n-n paiements factures)
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_paiements($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- $sql = "SELECT p.rowid, p.fk_facture, p.amount";
- $sql .= " FROM ".MAIN_DB_PREFIX."paiement as p";
- $sql .= " WHERE p.fk_facture > 0";
- $resql = $db->query($sql);
- dolibarr_install_syslog("upgrade2::migrate_paiements");
- if ($resql)
- {
- $i = 0;
- $row = array();
- $num = $db->num_rows($resql);
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $row[$i][0] = $obj->rowid;
- $row[$i][1] = $obj->fk_facture;
- $row[$i][2] = $obj->amount;
- $i++;
- }
- } else {
- dol_print_error($db);
- }
- if ($num)
- {
- print $langs->trans('MigrationPaymentsNumberToUpdate', $num)."<br>\n";
- if ($db->begin())
- {
- $res = 0;
- $num = count($row);
- for ($i = 0; $i < $num; $i++)
- {
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
- $sql .= " VALUES (".$row[$i][1].",".$row[$i][0].",".$row[$i][2].")";
- $res += $db->query($sql);
- $sql = "UPDATE ".MAIN_DB_PREFIX."paiement SET fk_facture = 0 WHERE rowid = ".$row[$i][0];
- $res += $db->query($sql);
- print $langs->trans('MigrationProcessPaymentUpdate', $row[$i][0])."<br>\n";
- }
- }
- if ($res == (2 * count($row)))
- {
- $db->commit();
- print $langs->trans('MigrationSuccessfullUpdate')."<br>";
- } else {
- $db->rollback();
- print $langs->trans('MigrationUpdateFailed').'<br>';
- }
- } else {
- print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
- }
- } else {
- print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Corrige paiement orphelins (liens paumes suite a bugs)
- * Pour verifier s'il reste des orphelins:
- * 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)
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_paiements_orphelins_1($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
- $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
- $sql .= " bu2.url_id as socid";
- $sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank=bu2.fk_bank AND bu2.type = 'company')";
- $sql .= " WHERE pf.rowid IS NULL AND (p.rowid=bu.url_id AND bu.type='payment') AND bu.fk_bank = b.rowid";
- $sql .= " AND b.rappro = 1";
- $sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
- $resql = $db->query($sql);
- dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_1");
- $row = array();
- if ($resql)
- {
- $i = $j = 0;
- $num = $db->num_rows($resql);
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- if ($obj->pamount == $obj->bamount && $obj->socid) // Pour etre sur d'avoir bon cas
- {
- $row[$j]['paymentid'] = $obj->rowid; // paymentid
- $row[$j]['pamount'] = $obj->pamount;
- $row[$j]['fk_bank'] = $obj->fk_bank;
- $row[$j]['bamount'] = $obj->bamount;
- $row[$j]['socid'] = $obj->socid;
- $row[$j]['datec'] = $obj->datec;
- $j++;
- }
- $i++;
- }
- } else {
- dol_print_error($db);
- }
- if (count($row))
- {
- print $langs->trans('OrphelinsPaymentsDetectedByMethod', 1).': '.count($row)."<br>\n";
- $db->begin();
- $res = 0;
- $num = count($row);
- for ($i = 0; $i < $num; $i++)
- {
- 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>';
- // On cherche facture sans lien paiement et du meme montant et pour meme societe.
- $sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
- $sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".$row[$i]['socid']." AND total_ttc = ".$row[$i]['pamount'];
- $sql .= " AND pf.fk_facture IS NULL";
- $sql .= " ORDER BY f.fk_statut";
- //print $sql.'<br>';
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
- if ($num >= 1)
- {
- $obj = $db->fetch_object($resql);
- $facid = $obj->rowid;
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
- $sql .= " VALUES (".$facid.",".$row[$i]['paymentid'].",".$row[$i]['pamount'].")";
- $res += $db->query($sql);
- print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
- }
- } else {
- print 'ERROR';
- }
- }
- if ($res > 0)
- {
- print $langs->trans('MigrationSuccessfullUpdate')."<br>";
- } else {
- print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
- }
- $db->commit();
- } else {
- print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
- }
- } else {
- print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Corrige paiement orphelins (liens paumes suite a bugs)
- * Pour verifier s'il reste des orphelins:
- * 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)
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_paiements_orphelins_2($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
- $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
- $sql .= " bu2.url_id as socid";
- $sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank = bu2.fk_bank AND bu2.type = 'company')";
- $sql .= " WHERE pf.rowid IS NULL AND (p.fk_bank = bu.fk_bank AND bu.type = 'payment') AND bu.fk_bank = b.rowid";
- $sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
- $resql = $db->query($sql);
- dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_2");
- $row = array();
- if ($resql)
- {
- $i = $j = 0;
- $num = $db->num_rows($resql);
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- if ($obj->pamount == $obj->bamount && $obj->socid) // Pour etre sur d'avoir bon cas
- {
- $row[$j]['paymentid'] = $obj->rowid; // paymentid
- $row[$j]['pamount'] = $obj->pamount;
- $row[$j]['fk_bank'] = $obj->fk_bank;
- $row[$j]['bamount'] = $obj->bamount;
- $row[$j]['socid'] = $obj->socid;
- $row[$j]['datec'] = $obj->datec;
- $j++;
- }
- $i++;
- }
- } else {
- dol_print_error($db);
- }
- $nberr = 0;
- $num = count($row);
- if ($num)
- {
- print $langs->trans('OrphelinsPaymentsDetectedByMethod', 2).': '.count($row)."<br>\n";
- $db->begin();
- $res = 0;
- for ($i = 0; $i < $num; $i++)
- {
- 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>';
- // On cherche facture sans lien paiement et du meme montant et pour meme societe.
- $sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
- $sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".$row[$i]['socid']." AND total_ttc = ".$row[$i]['pamount'];
- $sql .= " AND pf.fk_facture IS NULL";
- $sql .= " ORDER BY f.fk_statut";
- //print $sql.'<br>';
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
- if ($num >= 1)
- {
- $obj = $db->fetch_object($resql);
- $facid = $obj->rowid;
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
- $sql .= " VALUES (".$facid.",".$row[$i]['paymentid'].",".$row[$i]['pamount'].")";
- $res += $db->query($sql);
- print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
- }
- } else {
- print 'ERROR';
- $nberr++;
- }
- }
- if ($res > 0)
- {
- print $langs->trans('MigrationSuccessfullUpdate')."<br>";
- } else {
- print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
- }
- $db->commit();
- } else {
- print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
- }
- // Delete obsolete fields fk_facture
- $db->begin();
- $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiement DROP COLUMN fk_facture";
- $db->query($sql);
- if (!$nberr)
- {
- $db->commit();
- } else {
- print 'ERROR';
- $db->rollback();
- }
- } else {
- print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Mise a jour des contrats (gestion du contrat + detail de contrat)
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_contracts_det($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- $nberr = 0;
- print '<br>';
- print '<b>'.$langs->trans('MigrationContractsUpdate')."</b><br>\n";
- $sql = "SELECT c.rowid as cref, c.date_contrat, c.statut, c.fk_product, c.fk_facture, c.fk_user_author,";
- $sql .= " p.ref, p.label, p.description, p.price, p.tva_tx, p.duration, cd.rowid";
- $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p";
- $sql .= " ON c.fk_product = p.rowid";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd";
- $sql .= " ON c.rowid=cd.fk_contrat";
- $sql .= " WHERE cd.rowid IS NULL AND p.rowid IS NOT NULL";
- $resql = $db->query($sql);
- dolibarr_install_syslog("upgrade2::migrate_contracts_det");
- if ($resql)
- {
- $i = 0;
- $row = array();
- $num = $db->num_rows($resql);
- if ($num)
- {
- print $langs->trans('MigrationContractsNumberToUpdate', $num)."<br>\n";
- $db->begin();
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet (";
- $sql .= "fk_contrat, fk_product, statut, label, description,";
- $sql .= "date_ouverture_prevue, date_ouverture, date_fin_validite, tva_tx, qty,";
- $sql .= "subprice, price_ht, fk_user_author, fk_user_ouverture)";
- $sql .= " VALUES (";
- $sql .= $obj->cref.", ".($obj->fk_product ? $obj->fk_product : 0).", ";
- $sql .= "0, ";
- $sql .= "'".$db->escape($obj->label)."', null, ";
- $sql .= ($obj->date_contrat ? "'".$db->escape($obj->date_contrat)."'" : "null").", ";
- $sql .= "null, ";
- $sql .= "null, ";
- $sql .= "'".$db->escape($obj->tva_tx)."' , 1, ";
- $sql .= "'".$db->escape($obj->price)."', '".$db->escape($obj->price)."', ".$obj->fk_user_author.",";
- $sql .= "null";
- $sql .= ")";
- if ($db->query($sql))
- {
- print $langs->trans('MigrationContractsLineCreation', $obj->cref)."<br>\n";
- } else {
- dol_print_error($db);
- $nberr++;
- }
- $i++;
- }
- if (!$nberr)
- {
- // $db->rollback();
- $db->commit();
- print $langs->trans('MigrationSuccessfullUpdate')."<br>";
- } else {
- $db->rollback();
- print $langs->trans('MigrationUpdateFailed').'<br>';
- }
- } else {
- print $langs->trans('MigrationContractsNothingToUpdate')."<br>\n";
- }
- } else {
- print $langs->trans('MigrationContractsFieldDontExist')."<br>\n";
- // dol_print_error($db);
- }
- print '</td></tr>';
- }
- /**
- * Function to migrate links into llx_bank_url
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_links_transfert($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- $nberr = 0;
- print '<br>';
- print '<b>'.$langs->trans('MigrationBankTransfertsUpdate')."</b><br>\n";
- $sql = "SELECT ba.rowid as barowid, bb.rowid as bbrowid";
- $sql .= " FROM ".MAIN_DB_PREFIX."bank as bb, ".MAIN_DB_PREFIX."bank as ba";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = ba.rowid";
- $sql .= " WHERE ba.amount = -bb.amount AND ba.fk_account <> bb.fk_account";
- $sql .= " AND ba.datev = bb.datev AND ba.datec = bb.datec";
- $sql .= " AND bu.fk_bank IS NULL";
- $resql = $db->query($sql);
- dolibarr_install_syslog("upgrade2::migrate_links_transfert");
- if ($resql)
- {
- $i = 0;
- $row = array();
- $num = $db->num_rows($resql);
- if ($num)
- {
- print $langs->trans('MigrationBankTransfertsToUpdate', $num)."<br>\n";
- $db->begin();
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
- $sql .= "fk_bank, url_id, url, label, type";
- $sql .= ")";
- $sql .= " VALUES (";
- $sql .= $obj->barowid.",".$obj->bbrowid.", '/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'";
- $sql .= ")";
- print $sql.'<br>';
- dolibarr_install_syslog("migrate_links_transfert");
- if (!$db->query($sql))
- {
- dol_print_error($db);
- $nberr++;
- }
- $i++;
- }
- if (!$nberr)
- {
- // $db->rollback();
- $db->commit();
- print $langs->trans('MigrationSuccessfullUpdate')."<br>";
- } else {
- $db->rollback();
- print $langs->trans('MigrationUpdateFailed').'<br>';
- }
- } else {
- print $langs->trans('MigrationBankTransfertsNothingToUpdate')."<br>\n";
- }
- } else {
- dol_print_error($db);
- }
- print '</td></tr>';
- }
- /**
- * Mise a jour des date de contrats non renseignees
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_contracts_date1($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationContractsEmptyDatesUpdate')."</b><br>\n";
- $sql = "update ".MAIN_DB_PREFIX."contrat set date_contrat=tms where date_contrat is null";
- dolibarr_install_syslog("upgrade2::migrate_contracts_date1");
- $resql = $db->query($sql);
- if (!$resql) dol_print_error($db);
- if ($db->affected_rows($resql) > 0)
- print $langs->trans('MigrationContractsEmptyDatesUpdateSuccess')."<br>\n";
- else print $langs->trans('MigrationContractsEmptyDatesNothingToUpdate')."<br>\n";
- $sql = "update ".MAIN_DB_PREFIX."contrat set datec=tms where datec is null";
- dolibarr_install_syslog("upgrade2::migrate_contracts_date1");
- $resql = $db->query($sql);
- if (!$resql) dol_print_error($db);
- if ($db->affected_rows($resql) > 0)
- print $langs->trans('MigrationContractsEmptyCreationDatesUpdateSuccess')."<br>\n";
- else print $langs->trans('MigrationContractsEmptyCreationDatesNothingToUpdate')."<br>\n";
- print '</td></tr>';
- }
- /**
- * Update contracts with date min real if service date is lower
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Language
- * @param Conf $conf Conf
- * @return void
- */
- function migrate_contracts_date2($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- $nberr = 0;
- print '<br>';
- print '<b>'.$langs->trans('MigrationContractsInvalidDatesUpdate')."</b><br>\n";
- $sql = "SELECT c.rowid as cref, c.datec, c.date_contrat, MIN(cd.date_ouverture) as datemin";
- $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c,";
- $sql .= " ".MAIN_DB_PREFIX."contratdet as cd";
- $sql .= " WHERE c.rowid=cd.fk_contrat AND cd.date_ouverture IS NOT NULL";
- $sql .= " GROUP BY c.rowid, c.date_contrat";
- $resql = $db->query($sql);
- dolibarr_install_syslog("upgrade2::migrate_contracts_date2");
- if ($resql)
- {
- $i = 0;
- $row = array();
- $num = $db->num_rows($resql);
- if ($num)
- {
- $nbcontratsmodifie = 0;
- $db->begin();
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- if ($obj->date_contrat > $obj->datemin)
- {
- $datemin = $db->jdate($obj->datemin);
- print $langs->trans('MigrationContractsInvalidDateFix', $obj->cref, $obj->date_contrat, $obj->datemin)."<br>\n";
- $sql = "UPDATE ".MAIN_DB_PREFIX."contrat";
- $sql .= " SET date_contrat='".$db->idate($datemin)."'";
- $sql .= " WHERE rowid=".$obj->cref;
- $resql2 = $db->query($sql);
- if (!$resql2) dol_print_error($db);
- $nbcontratsmodifie++;
- }
- $i++;
- }
- $db->commit();
- if ($nbcontratsmodifie)
- print $langs->trans('MigrationContractsInvalidDatesNumber', $nbcontratsmodifie)."<br>\n";
- else print $langs->trans('MigrationContractsInvalidDatesNothingToUpdate')."<br>\n";
- }
- } else {
- dol_print_error($db);
- }
- print '</td></tr>';
- }
- /**
- * Mise a jour des dates de creation de contrat
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_contracts_date3($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationContractsIncoherentCreationDateUpdate')."</b><br>\n";
- $sql = "update ".MAIN_DB_PREFIX."contrat set datec=date_contrat where datec is null or datec > date_contrat";
- dolibarr_install_syslog("upgrade2::migrate_contracts_date3");
- $resql = $db->query($sql);
- if (!$resql) dol_print_error($db);
- if ($db->affected_rows($resql) > 0)
- print $langs->trans('MigrationContractsIncoherentCreationDateUpdateSuccess')."<br>\n";
- else print $langs->trans('MigrationContractsIncoherentCreationDateNothingToUpdate')."<br>\n";
- print '</td></tr>';
- }
- /**
- * Reouverture des contrats qui ont au moins une ligne non fermee
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_contracts_open($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationReopeningContracts')."</b><br>\n";
- $sql = "SELECT c.rowid as cref FROM ".MAIN_DB_PREFIX."contrat as c, ".MAIN_DB_PREFIX."contratdet as cd";
- $sql .= " WHERE cd.statut = 4 AND c.statut=2 AND c.rowid=cd.fk_contrat";
- dolibarr_install_syslog("upgrade2::migrate_contracts_open");
- $resql = $db->query($sql);
- if (!$resql) dol_print_error($db);
- if ($db->affected_rows($resql) > 0) {
- $i = 0;
- $row = array();
- $num = $db->num_rows($resql);
- if ($num)
- {
- $nbcontratsmodifie = 0;
- $db->begin();
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- print $langs->trans('MigrationReopenThisContract', $obj->cref)."<br>\n";
- $sql = "UPDATE ".MAIN_DB_PREFIX."contrat";
- $sql .= " SET statut=1";
- $sql .= " WHERE rowid=".$obj->cref;
- $resql2 = $db->query($sql);
- if (!$resql2) dol_print_error($db);
- $nbcontratsmodifie++;
- $i++;
- }
- $db->commit();
- if ($nbcontratsmodifie)
- print $langs->trans('MigrationReopenedContractsNumber', $nbcontratsmodifie)."<br>\n";
- else print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
- }
- } else print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
- print '</td></tr>';
- }
- /**
- * Factures fournisseurs
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_paiementfourn_facturefourn($db, $langs, $conf)
- {
- global $bc;
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('SuppliersInvoices')."</b><br>\n";
- print '</td></tr>';
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiementfourn", "fk_facture_fourn");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- $error = 0;
- $nb = 0;
- $select_sql = 'SELECT rowid, fk_facture_fourn, amount';
- $select_sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn';
- $select_sql .= ' WHERE fk_facture_fourn IS NOT NULL';
- dolibarr_install_syslog("upgrade2::migrate_paiementfourn_facturefourn");
- $select_resql = $db->query($select_sql);
- if ($select_resql)
- {
- $select_num = $db->num_rows($select_resql);
- $i = 0;
- $var = true;
- // Pour chaque paiement fournisseur, on insere une ligne dans paiementfourn_facturefourn
- while (($i < $select_num) && (!$error))
- {
- $var = !$var;
- $select_obj = $db->fetch_object($select_resql);
- // Verifier si la ligne est deja dans la nouvelle table. On ne veut pas inserer de doublons.
- $check_sql = 'SELECT fk_paiementfourn, fk_facturefourn';
- $check_sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn';
- $check_sql .= ' WHERE fk_paiementfourn = '.$select_obj->rowid.' AND fk_facturefourn = '.$select_obj->fk_facture_fourn;
- $check_resql = $db->query($check_sql);
- if ($check_resql)
- {
- $check_num = $db->num_rows($check_resql);
- if ($check_num == 0)
- {
- $db->begin();
- if ($nb == 0)
- {
- print '<tr><td colspan="4" class="nowrap"><b>'.$langs->trans('SuppliersInvoices').'</b></td></tr>';
- print '<tr><td>fk_paiementfourn</td><td>fk_facturefourn</td><td>'.$langs->trans('Amount').'</td><td> </td></tr>';
- }
- print '<tr class="oddeven">';
- print '<td>'.$select_obj->rowid.'</td><td>'.$select_obj->fk_facture_fourn.'</td><td>'.$select_obj->amount.'</td>';
- $insert_sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn SET ';
- $insert_sql .= ' fk_paiementfourn = \''.$select_obj->rowid.'\',';
- $insert_sql .= ' fk_facturefourn = \''.$select_obj->fk_facture_fourn.'\',';
- $insert_sql .= ' amount = \''.$select_obj->amount.'\'';
- $insert_resql = $db->query($insert_sql);
- if ($insert_resql)
- {
- $nb++;
- print '<td><span style="color:green">'.$langs->trans("OK").'</span></td>';
- } else {
- print '<td><span style="color:red">Error on insert</span></td>';
- $error++;
- }
- print '</tr>';
- }
- } else {
- $error++;
- }
- $i++;
- }
- } else {
- $error++;
- }
- if (!$error)
- {
- if (!$nb)
- {
- print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
- }
- $db->commit();
- $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiementfourn DROP COLUMN fk_facture_fourn";
- $db->query($sql);
- } else {
- print '<tr><td>'.$langs->trans("Error").'</td></tr>';
- $db->rollback();
- }
- } else {
- print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
- }
- }
- /**
- * Update total of invoice lines
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_price_facture($db, $langs, $conf)
- {
- $err = 0;
- $tmpmysoc = new Societe($db);
- $tmpmysoc->setMysoc($conf);
- $db->begin();
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationInvoice')."</b><br>\n";
- // List of invoice lines not up to date
- $sql = "SELECT fd.rowid, fd.qty, fd.subprice, fd.remise_percent, fd.tva_tx as vatrate, fd.total_ttc, fd.info_bits,";
- $sql .= " f.rowid as facid, f.remise_percent as remise_percent_global, f.total_ttc as total_ttc_f";
- $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd, ".MAIN_DB_PREFIX."facture as f";
- $sql .= " WHERE fd.fk_facture = f.rowid";
- $sql .= " AND (((fd.total_ttc = 0 AND fd.remise_percent != 100) or fd.total_ttc IS NULL) or f.total_ttc IS NULL)";
- //print $sql;
- dolibarr_install_syslog("upgrade2::migrate_price_facture");
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- $i = 0;
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $rowid = $obj->rowid;
- $qty = $obj->qty;
- $pu = $obj->subprice;
- $vatrate = $obj->vatrate;
- $remise_percent = $obj->remise_percent;
- $remise_percent_global = $obj->remise_percent_global;
- $total_ttc_f = $obj->total_ttc_f;
- $info_bits = $obj->info_bits;
- // On met a jour les 3 nouveaux champs
- $facligne = new FactureLigne($db);
- $facligne->fetch($rowid);
- $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $facligne->product_type, $tmpmysoc);
- $total_ht = $result[0];
- $total_tva = $result[1];
- $total_ttc = $result[2];
- $facligne->total_ht = $total_ht;
- $facligne->total_tva = $total_tva;
- $facligne->total_ttc = $total_ttc;
- 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);
- print ". ";
- $facligne->update_total();
- /* On touche a facture mere uniquement si total_ttc = 0 */
- if (!$total_ttc_f)
- {
- $facture = new Facture($db);
- $facture->id = $obj->facid;
- if ($facture->fetch($facture->id) >= 0)
- {
- if ($facture->update_price() > 0)
- {
- //print $facture->id;
- } else {
- print "Error id=".$facture->id;
- $err++;
- }
- } else {
- print "Error #3";
- $err++;
- }
- }
- print " ";
- $i++;
- }
- } else {
- print $langs->trans("AlreadyDone");
- }
- $db->free($resql);
- $db->commit();
- } else {
- print "Error #1 ".$db->error();
- $err++;
- $db->rollback();
- }
- print '<br>';
- print '</td></tr>';
- }
- /**
- * Update total of proposal lines
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_price_propal($db, $langs, $conf)
- {
- $tmpmysoc = new Societe($db);
- $tmpmysoc->setMysoc($conf);
- $db->begin();
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationProposal')."</b><br>\n";
- // List of proposal lines not up to date
- $sql = "SELECT pd.rowid, pd.qty, pd.subprice, pd.remise_percent, pd.tva_tx as vatrate, pd.info_bits,";
- $sql .= " p.rowid as propalid, p.remise_percent as remise_percent_global";
- $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."propal as p";
- $sql .= " WHERE pd.fk_propal = p.rowid";
- $sql .= " AND ((pd.total_ttc = 0 AND pd.remise_percent != 100) or pd.total_ttc IS NULL)";
- dolibarr_install_syslog("upgrade2::migrate_price_propal");
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- $i = 0;
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $rowid = $obj->rowid;
- $qty = $obj->qty;
- $pu = $obj->subprice;
- $vatrate = $obj->vatrate;
- $remise_percent = $obj->remise_percent;
- $remise_percent_global = $obj->remise_percent_global;
- $info_bits = $obj->info_bits;
- // On met a jour les 3 nouveaux champs
- $propalligne = new PropaleLigne($db);
- $propalligne->fetch($rowid);
- $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $propalligne->product_type, $tmpmysoc);
- $total_ht = $result[0];
- $total_tva = $result[1];
- $total_ttc = $result[2];
- $propalligne->total_ht = $total_ht;
- $propalligne->total_tva = $total_tva;
- $propalligne->total_ttc = $total_ttc;
- 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);
- print ". ";
- $propalligne->update_total();
- /* On touche pas a propal mere
- $propal = new Propal($db);
- $propal->id=$obj->rowid;
- if ( $propal->fetch($propal->id) >= 0 )
- {
- if ( $propal->update_price() > 0 )
- {
- print ". ";
- }
- else
- {
- print "Error id=".$propal->id;
- }
- }
- else
- {
- print "Error #3";
- }
- */
- $i++;
- }
- } else {
- print $langs->trans("AlreadyDone");
- }
- $db->free($resql);
- $db->commit();
- } else {
- print "Error #1 ".$db->error();
- $db->rollback();
- }
- print '<br>';
- print '</td></tr>';
- }
- /**
- * Update total of contract lines
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_price_contrat($db, $langs, $conf)
- {
- $db->begin();
- $tmpmysoc = new Societe($db);
- $tmpmysoc->setMysoc($conf);
- if (empty($tmpmysoc->country_id)) $tmpmysoc->country_id = 0; // Ti not have this set to '' or will make sql syntax error.
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationContract')."</b><br>\n";
- // List of contract lines not up to date
- $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
- $sql .= " c.rowid as contratid";
- $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c";
- $sql .= " WHERE cd.fk_contrat = c.rowid";
- $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100 AND cd.subprice > 0) or cd.total_ttc IS NULL)";
- dolibarr_install_syslog("upgrade2::migrate_price_contrat");
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- $i = 0;
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $rowid = $obj->rowid;
- $qty = $obj->qty;
- $pu = $obj->subprice;
- $vatrate = $obj->vatrate;
- $remise_percent = $obj->remise_percent;
- $info_bits = $obj->info_bits;
- // On met a jour les 3 nouveaux champs
- $contratligne = new ContratLigne($db);
- //$contratligne->fetch($rowid); Non requis car le update_total ne met a jour que chp redefinis
- $contratligne->fetch($rowid);
- $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, 0, 'HT', $info_bits, $contratligne->product_type, $tmpmysoc);
- $total_ht = $result[0];
- $total_tva = $result[1];
- $total_ttc = $result[2];
- $contratligne->total_ht = $total_ht;
- $contratligne->total_tva = $total_tva;
- $contratligne->total_ttc = $total_ttc;
- 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);
- print ". ";
- $contratligne->update_total();
- $i++;
- }
- } else {
- print $langs->trans("AlreadyDone");
- }
- $db->free($resql);
- $db->commit();
- } else {
- print "Error #1 ".$db->error();
- $db->rollback();
- }
- print '<br>';
- print '</td></tr>';
- }
- /**
- * Update total of sales order lines
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_price_commande($db, $langs, $conf)
- {
- $db->begin();
- $tmpmysoc = new Societe($db);
- $tmpmysoc->setMysoc($conf);
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationOrder')."</b><br>\n";
- // List of sales orders lines not up to date
- $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
- $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
- $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."commande as c";
- $sql .= " WHERE cd.fk_commande = c.rowid";
- $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
- dolibarr_install_syslog("upgrade2::migrate_price_commande");
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- $i = 0;
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $rowid = $obj->rowid;
- $qty = $obj->qty;
- $pu = $obj->subprice;
- $vatrate = $obj->vatrate;
- $remise_percent = $obj->remise_percent;
- $remise_percent_global = $obj->remise_percent_global;
- $info_bits = $obj->info_bits;
- // On met a jour les 3 nouveaux champs
- $commandeligne = new OrderLine($db);
- $commandeligne->fetch($rowid);
- $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $commandeligne->product_type, $tmpmysoc);
- $total_ht = $result[0];
- $total_tva = $result[1];
- $total_ttc = $result[2];
- $commandeligne->total_ht = $total_ht;
- $commandeligne->total_tva = $total_tva;
- $commandeligne->total_ttc = $total_ttc;
- 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);
- print ". ";
- $commandeligne->update_total();
- /* On touche pas a facture mere
- $commande = new Commande($db);
- $commande->id = $obj->rowid;
- if ( $commande->fetch($commande->id) >= 0 )
- {
- if ( $commande->update_price() > 0 )
- {
- print ". ";
- }
- else
- {
- print "Error id=".$commande->id;
- }
- }
- else
- {
- print "Error #3";
- }
- */
- $i++;
- }
- } else {
- print $langs->trans("AlreadyDone");
- }
- $db->free($resql);
- /*
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."commandedet";
- $sql.= " WHERE price = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0 AND remise_percent = 0";
- $resql=$db->query($sql);
- if (! $resql)
- {
- dol_print_error($db);
- }
- */
- $db->commit();
- } else {
- print "Error #1 ".$db->error();
- $db->rollback();
- }
- print '<br>';
- print '</td></tr>';
- }
- /**
- * Update total of purchase order lines
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_price_commande_fournisseur($db, $langs, $conf)
- {
- $db->begin();
- $tmpmysoc = new Societe($db);
- $tmpmysoc->setMysoc($conf);
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationSupplierOrder')."</b><br>\n";
- // List of purchase order lines not up to date
- $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
- $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
- $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd, ".MAIN_DB_PREFIX."commande_fournisseur as c";
- $sql .= " WHERE cd.fk_commande = c.rowid";
- $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
- dolibarr_install_syslog("upgrade2::migrate_price_commande_fournisseur");
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- $i = 0;
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $rowid = $obj->rowid;
- $qty = $obj->qty;
- $pu = $obj->subprice;
- $vatrate = $obj->vatrate;
- $remise_percent = $obj->remise_percent;
- $remise_percent_global = $obj->remise_percent_global;
- $info_bits = $obj->info_bits;
- // On met a jour les 3 nouveaux champs
- $commandeligne = new CommandeFournisseurLigne($db);
- $commandeligne->fetch($rowid);
- $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $commandeligne->product_type, $tmpsoc);
- $total_ht = $result[0];
- $total_tva = $result[1];
- $total_ttc = $result[2];
- $commandeligne->total_ht = $total_ht;
- $commandeligne->total_tva = $total_tva;
- $commandeligne->total_ttc = $total_ttc;
- 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);
- print ". ";
- $commandeligne->update_total();
- /* On touche pas a facture mere
- $commande = new Commande($db);
- $commande->id = $obj->rowid;
- if ( $commande->fetch($commande->id) >= 0 )
- {
- if ( $commande->update_price() > 0 )
- {
- print ". ";
- }
- else
- {
- print "Error id=".$commande->id;
- }
- }
- else
- {
- print "Error #3";
- }
- */
- $i++;
- }
- } else {
- print $langs->trans("AlreadyDone");
- }
- $db->free($resql);
- /*
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet";
- $sql.= " WHERE subprice = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0";
- $resql=$db->query($sql);
- if (! $resql)
- {
- dol_print_error($db);
- }
- */
- $db->commit();
- } else {
- print "Error #1 ".$db->error();
- $db->rollback();
- }
- print '<br>';
- print '</td></tr>';
- }
- /**
- * Mise a jour des modeles selectionnes
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_modeles($db, $langs, $conf)
- {
- //print '<br>';
- //print '<b>'.$langs->trans('UpdateModelsTable')."</b><br>\n";
- dolibarr_install_syslog("upgrade2::migrate_modeles");
- if (!empty($conf->facture->enabled))
- {
- include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
- $modellist = ModelePDFFactures::liste_modeles($db);
- if (count($modellist) == 0)
- {
- // Aucun model par defaut.
- $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('crabe','invoice')";
- $resql = $db->query($sql);
- if (!$resql) dol_print_error($db);
- }
- }
- if (!empty($conf->commande->enabled))
- {
- include_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
- $modellist = ModelePDFCommandes::liste_modeles($db);
- if (count($modellist) == 0)
- {
- // Aucun model par defaut.
- $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('einstein','order')";
- $resql = $db->query($sql);
- if (!$resql) dol_print_error($db);
- }
- }
- if (!empty($conf->expedition->enabled))
- {
- include_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
- $modellist = ModelePDFExpedition::liste_modeles($db);
- if (count($modellist) == 0)
- {
- // Aucun model par defaut.
- $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('rouget','shipping')";
- $resql = $db->query($sql);
- if (!$resql) dol_print_error($db);
- }
- }
- //print $langs->trans("AlreadyDone");
- }
- /**
- * Correspondance des expeditions et des commandes clients dans la table llx_co_exp
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_commande_expedition($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_commande_expedition");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationShipmentOrderMatching')."</b><br>\n";
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "fk_commande");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- $error = 0;
- $db->begin();
- $sql = "SELECT e.rowid, e.fk_commande FROM ".MAIN_DB_PREFIX."expedition as e";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_exp (fk_expedition,fk_commande)";
- $sql .= " VALUES (".$obj->rowid.",".$obj->fk_commande.")";
- $resql2 = $db->query($sql);
- if (!$resql2)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- }
- if ($error == 0)
- {
- $db->commit();
- $sql = "ALTER TABLE ".MAIN_DB_PREFIX."expedition DROP COLUMN fk_commande";
- print $langs->trans('FieldRenamed')."<br>\n";
- $db->query($sql);
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Correspondance des livraisons et des commandes clients dans la table llx_co_liv
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_commande_livraison($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_commande_livraison");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationDeliveryOrderMatching')."</b><br>\n";
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison", "fk_commande");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- $error = 0;
- $db->begin();
- $sql = "SELECT l.rowid, l.fk_commande";
- $sql .= ", c.ref_client, c.date_livraison";
- $sql .= " FROM ".MAIN_DB_PREFIX."livraison as l, ".MAIN_DB_PREFIX."commande as c";
- $sql .= " WHERE c.rowid = l.fk_commande";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_liv (fk_livraison,fk_commande)";
- $sql .= " VALUES (".$obj->rowid.",".$obj->fk_commande.")";
- $resql2 = $db->query($sql);
- if ($resql2)
- {
- $delivery_date = $db->jdate($obj->date_livraison);
- $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
- $sqlu .= " ref_client='".$db->escape($obj->ref_client)."'";
- $sqlu .= ", date_livraison='".$db->idate($delivery_date)."'";
- $sqlu .= " WHERE rowid = ".$obj->rowid;
- $resql3 = $db->query($sqlu);
- if (!$resql3)
- {
- $error++;
- dol_print_error($db);
- }
- } else {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- }
- if ($error == 0)
- {
- $db->commit();
- $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_commande";
- print $langs->trans('FieldRenamed')."<br>\n";
- $db->query($sql);
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migration des details commandes dans les details livraisons
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_detail_livraison($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_detail_livraison");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationDeliveryDetail')."</b><br>\n";
- // This is done if field fk_commande_ligne exists.
- // If not this means migration was already done.
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet", "fk_commande_ligne");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- $error = 0;
- $db->begin();
- $sql = "SELECT cd.rowid, cd.fk_product, cd.description, cd.subprice, cd.total_ht";
- $sql .= ", ld.fk_livraison";
- $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."livraisondet as ld";
- $sql .= " WHERE ld.fk_commande_ligne = cd.rowid";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql = "UPDATE ".MAIN_DB_PREFIX."livraisondet SET";
- $sql .= " fk_product=".$obj->fk_product;
- $sql .= ",description='".$db->escape($obj->description)."'";
- $sql .= ",subprice='".$db->escape($obj->subprice)."'";
- $sql .= ",total_ht='".$db->escape($obj->total_ht)."'";
- $sql .= " WHERE fk_commande_ligne = ".$obj->rowid;
- $resql2 = $db->query($sql);
- if ($resql2)
- {
- $sql = "SELECT total_ht";
- $sql .= " FROM ".MAIN_DB_PREFIX."livraison";
- $sql .= " WHERE rowid = ".$obj->fk_livraison;
- $resql3 = $db->query($sql);
- if ($resql3)
- {
- $obju = $db->fetch_object($resql3);
- $total_ht = $obju->total_ht + $obj->total_ht;
- $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
- $sqlu .= " total_ht='".$db->escape($total_ht)."'";
- $sqlu .= " WHERE rowid=".$obj->fk_livraison;
- $resql4 = $db->query($sqlu);
- if (!$resql4)
- {
- $error++;
- dol_print_error($db);
- }
- } else {
- $error++;
- dol_print_error($db);
- }
- } else {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- }
- if ($error == 0)
- {
- $db->commit();
- $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet CHANGE fk_commande_ligne fk_origin_line integer";
- print $langs->trans('FieldRenamed')."<br>\n";
- $db->query($sql);
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet", "fk_origin_line");
- $obj = $db->fetch_object($result);
- if (!$obj)
- {
- $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet ADD COLUMN fk_origin_line integer after fk_livraison";
- $db->query($sql);
- }
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migration du champ stock dans produits
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_stocks($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_stocks");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationStockDetail')."</b><br>\n";
- $error = 0;
- $db->begin();
- $sql = "SELECT SUM(reel) as total, fk_product";
- $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps";
- $sql .= " GROUP BY fk_product";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql = "UPDATE ".MAIN_DB_PREFIX."product SET";
- $sql .= " stock = '".$db->escape($obj->total)."'";
- $sql .= " WHERE rowid=".$obj->fk_product;
- $resql2 = $db->query($sql);
- if ($resql2)
- {
- } else {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- }
- if ($error == 0)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migration of menus (use only 1 table instead of 3)
- * 2.6 -> 2.7
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_menus($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_menus");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationMenusDetail')."</b><br>\n";
- $error = 0;
- if ($db->DDLInfoTable(MAIN_DB_PREFIX."menu_constraint"))
- {
- $db->begin();
- $sql = "SELECT m.rowid, mc.action";
- $sql .= " FROM ".MAIN_DB_PREFIX."menu_constraint as mc, ".MAIN_DB_PREFIX."menu_const as md, ".MAIN_DB_PREFIX."menu as m";
- $sql .= " WHERE md.fk_menu = m.rowid AND md.fk_constraint = mc.rowid";
- $sql .= " AND m.enabled = '1'";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql = "UPDATE ".MAIN_DB_PREFIX."menu SET";
- $sql .= " enabled = '".$db->escape($obj->action)."'";
- $sql .= " WHERE rowid=".$obj->rowid;
- $sql .= " AND enabled = '1'";
- $resql2 = $db->query($sql);
- if ($resql2)
- {
- } else {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- }
- if ($error == 0)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migration du champ fk_adresse_livraison dans expedition
- * 2.6 -> 2.7
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_commande_deliveryaddress($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_commande_deliveryaddress");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationDeliveryAddress')."</b><br>\n";
- $error = 0;
- if ($db->DDLInfoTable(MAIN_DB_PREFIX."co_exp"))
- {
- $db->begin();
- $sql = "SELECT c.fk_adresse_livraison, ce.fk_expedition";
- $sql .= " FROM ".MAIN_DB_PREFIX."commande as c";
- $sql .= ", ".MAIN_DB_PREFIX."co_exp as ce";
- $sql .= " WHERE c.rowid = ce.fk_commande";
- $sql .= " AND c.fk_adresse_livraison IS NOT NULL AND c.fk_adresse_livraison != 0";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
- $sql .= " fk_adresse_livraison = '".$db->escape($obj->fk_adresse_livraison)."'";
- $sql .= " WHERE rowid=".$obj->fk_expedition;
- $resql2 = $db->query($sql);
- if (!$resql2)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if ($error == 0)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migration du champ fk_remise_except dans llx_facturedet doit correspondre a
- * lien dans llx_societe_remise_except vers llx_facturedet
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return integer|null
- */
- function migrate_restore_missing_links($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_restore_missing_links");
- if (($db->type == 'mysql' || $db->type == 'mysqli'))
- {
- if (versioncompare($db->getVersionArray(), array(4, 0)) < 0)
- {
- dolibarr_install_syslog("upgrade2::migrate_restore_missing_links Version of database too old to make this migrate action");
- return 0;
- }
- }
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationFixData')."</b> (1)<br>\n";
- $error = 0;
- // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 1.
- $table1 = 'facturedet'; $field1 = 'fk_remise_except';
- $table2 = 'societe_remise_except'; $field2 = 'fk_facture_line';
- $db->begin();
- $sql = "SELECT t1.rowid, t1.".$field1." as field";
- $sql .= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
- $sql .= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
- $sql .= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
- $sql .= " WHERE t1.rowid = t2.".$field2.")";
- dolibarr_install_syslog("upgrade2::migrate_restore_missing_links DIRECTION 1");
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
- $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
- $sql .= " ".$field2." = '".$db->escape($obj->rowid)."'";
- $sql .= " WHERE rowid=".$obj->field;
- $resql2 = $db->query($sql);
- if (!$resql2)
- {
- $error++;
- dol_print_error($db);
- }
- //print ". ";
- $i++;
- }
- } else print $langs->trans('AlreadyDone')."<br>\n";
- if ($error == 0)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationFixData')."</b> (2)<br>\n";
- // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 2.
- $table2 = 'facturedet'; $field2 = 'fk_remise_except';
- $table1 = 'societe_remise_except'; $field1 = 'fk_facture_line';
- $db->begin();
- $sql = "SELECT t1.rowid, t1.".$field1." as field";
- $sql .= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
- $sql .= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
- $sql .= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
- $sql .= " WHERE t1.rowid = t2.".$field2.")";
- dolibarr_install_syslog("upgrade2::migrate_restore_missing_links DIRECTION 2");
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
- $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
- $sql .= " ".$field2." = '".$db->escape($obj->rowid)."'";
- $sql .= " WHERE rowid=".$obj->field;
- $resql2 = $db->query($sql);
- if (!$resql2)
- {
- $error++;
- dol_print_error($db);
- }
- //print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if ($error == 0)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migration du champ fk_user_resp de llx_projet vers llx_element_contact
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_project_user_resp($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_project_user_resp");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationProjectUserResp')."</b><br>\n";
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."projet", "fk_user_resp");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- $error = 0;
- $db->begin();
- $sql = "SELECT rowid, fk_user_resp FROM ".MAIN_DB_PREFIX."projet";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
- $sql2 .= "datecreate";
- $sql2 .= ", statut";
- $sql2 .= ", element_id";
- $sql2 .= ", fk_c_type_contact";
- $sql2 .= ", fk_socpeople";
- $sql2 .= ") VALUES (";
- $sql2 .= "'".$db->idate(dol_now())."'";
- $sql2 .= ", '4'";
- $sql2 .= ", ".$obj->rowid;
- $sql2 .= ", '160'";
- $sql2 .= ", ".$obj->fk_user_resp;
- $sql2 .= ")";
- if ($obj->fk_user_resp > 0)
- {
- $resql2 = $db->query($sql2);
- if (!$resql2)
- {
- $error++;
- dol_print_error($db);
- }
- }
- print ". ";
- $i++;
- }
- }
- if ($error == 0)
- {
- $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."projet DROP COLUMN fk_user_resp";
- if ($db->query($sqlDrop))
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migration de la table llx_projet_task_actors vers llx_element_contact
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_project_task_actors($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_project_task_actors");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationProjectTaskActors')."</b><br>\n";
- if ($db->DDLInfoTable(MAIN_DB_PREFIX."projet_task_actors"))
- {
- $error = 0;
- $db->begin();
- $sql = "SELECT fk_projet_task as fk_project_task, fk_user FROM ".MAIN_DB_PREFIX."projet_task_actors";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
- $sql2 .= "datecreate";
- $sql2 .= ", statut";
- $sql2 .= ", element_id";
- $sql2 .= ", fk_c_type_contact";
- $sql2 .= ", fk_socpeople";
- $sql2 .= ") VALUES (";
- $sql2 .= "'".$db->idate(dol_now())."'";
- $sql2 .= ", '4'";
- $sql2 .= ", ".$obj->fk_project_task;
- $sql2 .= ", '180'";
- $sql2 .= ", ".$obj->fk_user;
- $sql2 .= ")";
- $resql2 = $db->query($sql2);
- if (!$resql2)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- }
- if ($error == 0)
- {
- $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX."projet_task_actors";
- if ($db->query($sqlDrop))
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migration des tables de relation
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @param string $table Table name
- * @param int $fk_source Id of element source
- * @param string $sourcetype Type of element source
- * @param int $fk_target Id of element target
- * @param string $targettype Type of element target
- * @return void
- */
- function migrate_relationship_tables($db, $langs, $conf, $table, $fk_source, $sourcetype, $fk_target, $targettype)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationRelationshipTables', MAIN_DB_PREFIX.$table)."</b><br>\n";
- $error = 0;
- if ($db->DDLInfoTable(MAIN_DB_PREFIX.$table))
- {
- dolibarr_install_syslog("upgrade2::migrate_relationship_tables table = ".MAIN_DB_PREFIX.$table);
- $db->begin();
- $sqlSelect = "SELECT ".$fk_source.", ".$fk_target;
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX.$table;
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
- $sqlInsert .= "fk_source";
- $sqlInsert .= ", sourcetype";
- $sqlInsert .= ", fk_target";
- $sqlInsert .= ", targettype";
- $sqlInsert .= ") VALUES (";
- $sqlInsert .= $obj->$fk_source;
- $sqlInsert .= ", '".$db->escape($sourcetype)."'";
- $sqlInsert .= ", ".$obj->$fk_target;
- $sqlInsert .= ", '".$db->escape($targettype)."'";
- $sqlInsert .= ")";
- $result = $db->query($sqlInsert);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if ($error == 0)
- {
- $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX.$table;
- if ($db->query($sqlDrop))
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migrate duration in seconds
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_project_task_time($db, $langs, $conf)
- {
- dolibarr_install_syslog("upgrade2::migrate_project_task_time");
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationProjectTaskTime')."</b><br>\n";
- $error = 0;
- $db->begin();
- $sql = "SELECT rowid, fk_task, task_duration";
- $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time";
- $resql = $db->query($sql);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- $totaltime = array();
- $oldtime = 0;
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- if ($obj->task_duration > 0)
- {
- // convert to second
- // only for int time and float time ex: 1,75 for 1h45
- list($hour, $min) = explode('.', $obj->task_duration);
- $hour = $hour * 60 * 60;
- $min = ($min / 100) * 60 * 60;
- $newtime = $hour + $min;
- $sql2 = "UPDATE ".MAIN_DB_PREFIX."projet_task_time SET";
- $sql2 .= " task_duration = ".$newtime;
- $sql2 .= " WHERE rowid = ".$obj->rowid;
- $resql2 = $db->query($sql2);
- if (!$resql2)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $oldtime++;
- if (!empty($totaltime[$obj->fk_task])) $totaltime[$obj->fk_task] += $newtime;
- else $totaltime[$obj->fk_task] = $newtime;
- } else {
- if (!empty($totaltime[$obj->fk_task])) $totaltime[$obj->fk_task] += $obj->task_duration;
- else $totaltime[$obj->fk_task] = $obj->task_duration;
- }
- $i++;
- }
- if ($error == 0)
- {
- if ($oldtime > 0)
- {
- foreach ($totaltime as $taskid => $total_duration)
- {
- $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
- $sql .= " duration_effective = ".$total_duration;
- $sql .= " WHERE rowid = ".$taskid;
- $resql = $db->query($sql);
- if (!$resql)
- {
- $error++;
- dol_print_error($db);
- }
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- } else {
- dol_print_error($db);
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- } else {
- dol_print_error($db);
- }
- if ($error == 0)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migrate order ref_customer and date_delivery fields to llx_expedition
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_customerorder_shipping($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationCustomerOrderShipping')."</b><br>\n";
- $error = 0;
- $result1 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "ref_customer");
- $result2 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "date_delivery");
- $obj1 = $db->fetch_object($result1);
- $obj2 = $db->fetch_object($result2);
- if (!$obj1 && !$obj2)
- {
- dolibarr_install_syslog("upgrade2::migrate_customerorder_shipping");
- $db->begin();
- $sqlAdd1 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN ref_customer varchar(30) AFTER entity";
- $sqlAdd2 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN date_delivery date DEFAULT NULL AFTER date_expedition";
- if ($db->query($sqlAdd1) && $db->query($sqlAdd2))
- {
- $sqlSelect = "SELECT e.rowid as shipping_id, c.ref_client, c.date_livraison";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."expedition as e";
- $sqlSelect .= ", ".MAIN_DB_PREFIX."element_element as el";
- $sqlSelect .= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON c.rowid = el.fk_source AND el.sourcetype = 'commande'";
- $sqlSelect .= " WHERE e.rowid = el.fk_target";
- $sqlSelect .= " AND el.targettype = 'shipping'";
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
- $sqlUpdate .= " ref_customer = '".$db->escape($obj->ref_client)."'";
- $sqlUpdate .= ", date_delivery = '".$db->escape($obj->date_livraison ? $obj->date_livraison : 'null')."'";
- $sqlUpdate .= " WHERE rowid = ".$obj->shipping_id;
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if ($error == 0)
- {
- $db->commit();
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migrate link stored into fk_expedition into llx_element_element
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_shipping_delivery($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationShippingDelivery')."</b><br>\n";
- $error = 0;
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison", "fk_expedition");
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- dolibarr_install_syslog("upgrade2::migrate_shipping_delivery");
- $db->begin();
- $sqlSelect = "SELECT rowid, fk_expedition";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."livraison";
- $sqlSelect .= " WHERE fk_expedition is not null";
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
- $sqlInsert .= "fk_source";
- $sqlInsert .= ", sourcetype";
- $sqlInsert .= ", fk_target";
- $sqlInsert .= ", targettype";
- $sqlInsert .= ") VALUES (";
- $sqlInsert .= $obj->fk_expedition;
- $sqlInsert .= ", 'shipping'";
- $sqlInsert .= ", ".$obj->rowid;
- $sqlInsert .= ", 'delivery'";
- $sqlInsert .= ")";
- $result = $db->query($sqlInsert);
- if ($result)
- {
- $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET fk_expedition = NULL";
- $sqlUpdate .= " WHERE rowid = ".$obj->rowid;
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- } else {
- $error++;
- dol_print_error($db);
- }
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if ($error == 0)
- {
- $sqlDelete = "DELETE FROM ".MAIN_DB_PREFIX."element_element WHERE sourcetype = 'commande' AND targettype = 'delivery'";
- $db->query($sqlDelete);
- $db->commit();
- // DDL commands must not be inside a transaction
- $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_expedition";
- $db->query($sqlDrop);
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * We try to complete field ref_customer and date_delivery that are empty into llx_livraison.
- * We set them with value from llx_expedition.
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_shipping_delivery2($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationShippingDelivery2')."</b><br>\n";
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_shipping_delivery2");
- $db->begin();
- $sqlSelect = "SELECT l.rowid as delivery_id, e.ref_customer, e.date_delivery";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."livraison as l,";
- $sqlSelect .= " ".MAIN_DB_PREFIX."element_element as el,";
- $sqlSelect .= " ".MAIN_DB_PREFIX."expedition as e";
- $sqlSelect .= " WHERE l.rowid = el.fk_target";
- $sqlSelect .= " AND el.targettype = 'delivery'";
- $sqlSelect .= " AND e.rowid = el.fk_source AND el.sourcetype = 'shipping'";
- $sqlSelect .= " AND (e.ref_customer IS NOT NULL OR e.date_delivery IS NOT NULL)"; // Useless to process this record if both are null
- // Add condition to know if we never migrate this record
- $sqlSelect .= " AND (l.ref_customer IS NULL".($db->type != 'pgsql' ? " or l.ref_customer = ''" : "").")";
- $sqlSelect .= " AND (l.date_delivery IS NULL".($db->type != 'pgsql' ? " or l.date_delivery = ''" : "").")";
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
- $sqlUpdate .= " ref_customer = '".$db->escape($obj->ref_customer)."',";
- $sqlUpdate .= " date_delivery = ".($obj->date_delivery ? "'".$db->escape($obj->date_delivery)."'" : 'null');
- $sqlUpdate .= " WHERE rowid = ".$obj->delivery_id;
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if ($error == 0)
- {
- $db->commit();
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migrate link stored into fk_xxxx into fk_element and elementtype
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_actioncomm_element($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationActioncommElement')."</b><br>\n";
- $elements = array(
- 'propal' => 'propalrowid',
- 'order' => 'fk_commande',
- 'invoice' => 'fk_facture',
- 'contract' => 'fk_contract',
- 'order_supplier' => 'fk_supplier_order',
- 'invoice_supplier' => 'fk_supplier_invoice'
- );
- foreach ($elements as $type => $field)
- {
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."actioncomm", $field);
- $obj = $db->fetch_object($result);
- if ($obj)
- {
- dolibarr_install_syslog("upgrade2::migrate_actioncomm_element field=".$field);
- $db->begin();
- $sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm SET ";
- $sql .= "fk_element = ".$field.", elementtype = '".$db->escape($type)."'";
- $sql .= " WHERE ".$field." IS NOT NULL";
- $sql .= " AND fk_element IS NULL";
- $sql .= " AND elementtype IS NULL";
- $resql = $db->query($sql);
- if ($resql)
- {
- $db->commit();
- // DDL commands must not be inside a transaction
- // We will drop at next version because a migrate should be runnable several times if it fails.
- //$sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."actioncomm DROP COLUMN ".$field;
- //$db->query($sqlDrop);
- //print ". ";
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- }
- print '</td></tr>';
- }
- /**
- * Migrate link stored into fk_mode_reglement
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_mode_reglement($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationPaymentMode')."</b><br>\n";
- $elements = array(
- 'old_id' => array(5, 8, 9, 10, 11),
- 'new_id' => array(50, 51, 52, 53, 54),
- 'code' => array('VAD', 'TRA', 'LCR', 'FAC', 'PRO'),
- 'tables' => array('commande_fournisseur', 'commande', 'facture_rec', 'facture', 'propal')
- );
- $count = 0;
- foreach ($elements['old_id'] as $key => $old_id)
- {
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_mode_reglement code=".$elements['code'][$key]);
- $sqlSelect = "SELECT id";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."c_paiement";
- $sqlSelect .= " WHERE id = ".$old_id;
- $sqlSelect .= " AND code = '".$db->escape($elements['code'][$key])."'";
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- if ($num)
- {
- $count++;
- $db->begin();
- $sqla = "UPDATE ".MAIN_DB_PREFIX."paiement SET ";
- $sqla .= "fk_paiement = ".$elements['new_id'][$key];
- $sqla .= " WHERE fk_paiement = ".$old_id;
- $sqla .= " AND fk_paiement IN (SELECT id FROM ".MAIN_DB_PREFIX."c_paiement WHERE id = ".$old_id." AND code = '".$db->escape($elements['code'][$key])."')";
- $resqla = $db->query($sqla);
- $sql = "UPDATE ".MAIN_DB_PREFIX."c_paiement SET ";
- $sql .= "id = ".$elements['new_id'][$key];
- $sql .= " WHERE id = ".$old_id;
- $sql .= " AND code = '".$db->escape($elements['code'][$key])."'";
- $resql = $db->query($sql);
- if ($resqla && $resql)
- {
- foreach ($elements['tables'] as $table)
- {
- $sql = "UPDATE ".MAIN_DB_PREFIX.$table." SET ";
- $sql .= "fk_mode_reglement = ".$elements['new_id'][$key];
- $sql .= " WHERE fk_mode_reglement = ".$old_id;
- $resql = $db->query($sql);
- if (!$resql)
- {
- dol_print_error($db);
- $error++;
- }
- print ". ";
- }
- if (!$error)
- {
- $db->commit();
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- }
- }
- }
- if ($count == 0) print $langs->trans('AlreadyDone')."<br>\n";
- print '</td></tr>';
- }
- /**
- * Delete duplicates in table categorie_association
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_clean_association($db, $langs, $conf)
- {
- $result = $db->DDLDescTable(MAIN_DB_PREFIX."categorie_association");
- if ($result) // result defined for version 3.2 or -
- {
- $obj = $db->fetch_object($result);
- if ($obj) // It table categorie_association exists
- {
- $couples = array();
- $filles = array();
- $sql = "SELECT fk_categorie_mere, fk_categorie_fille";
- $sql .= " FROM ".MAIN_DB_PREFIX."categorie_association";
- dolibarr_install_syslog("upgrade: search duplicate");
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- while ($obj = $db->fetch_object($resql))
- {
- if (!isset($filles[$obj->fk_categorie_fille])) // Only one record as child (a child has only on parent).
- {
- if ($obj->fk_categorie_mere != $obj->fk_categorie_fille)
- {
- $filles[$obj->fk_categorie_fille] = 1; // Set record for this child
- $couples[$obj->fk_categorie_mere.'_'.$obj->fk_categorie_fille] = array('mere'=>$obj->fk_categorie_mere, 'fille'=>$obj->fk_categorie_fille);
- }
- }
- }
- dolibarr_install_syslog("upgrade: result is num=".$num." count(couples)=".count($couples));
- // If there is duplicates couples or child with two parents
- if (count($couples) > 0 && $num > count($couples))
- {
- $error = 0;
- $db->begin();
- // We delete all
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association";
- dolibarr_install_syslog("upgrade: delete association");
- $resqld = $db->query($sql);
- if ($resqld)
- {
- // And we insert only each record once
- foreach ($couples as $key => $val)
- {
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association(fk_categorie_mere,fk_categorie_fille)";
- $sql .= " VALUES(".$val['mere'].", ".$val['fille'].")";
- dolibarr_install_syslog("upgrade: insert association");
- $resqli = $db->query($sql);
- if (!$resqli) $error++;
- }
- }
- if (!$error)
- {
- print '<tr><td>'.$langs->trans("MigrationCategorieAssociation").'</td>';
- print '<td class="right">'.$langs->trans("RemoveDuplicates").' '.$langs->trans("Success").' ('.$num.'=>'.count($couples).')</td></tr>';
- $db->commit();
- } else {
- print '<tr><td>'.$langs->trans("MigrationCategorieAssociation").'</td>';
- print '<td class="right">'.$langs->trans("RemoveDuplicates").' '.$langs->trans("Failed").'</td></tr>';
- $db->rollback();
- }
- }
- } else {
- print '<tr><td>'.$langs->trans("Error").'</td>';
- print '<td class="right"><div class="error">'.$db->lasterror().'</div></td></tr>';
- }
- }
- }
- }
- /**
- * Migrate categorie association
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_categorie_association($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationCategorieAssociation')."</b><br>\n";
- $error = 0;
- if ($db->DDLInfoTable(MAIN_DB_PREFIX."categorie_association"))
- {
- dolibarr_install_syslog("upgrade2::migrate_categorie_association");
- $db->begin();
- $sqlSelect = "SELECT fk_categorie_mere, fk_categorie_fille";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."categorie_association";
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."categorie SET ";
- $sqlUpdate .= "fk_parent = ".$obj->fk_categorie_mere;
- $sqlUpdate .= " WHERE rowid = ".$obj->fk_categorie_fille;
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if (!$error)
- {
- // TODO DROP table in the next release
- /*
- $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX."categorie_association";
- if ($db->query($sqlDrop))
- {
- $db->commit();
- }
- else
- {
- $db->rollback();
- }
- */
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- print '</td></tr>';
- }
- /**
- * Migrate event assignement to owner
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_event_assignement($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationEvents')."</b><br>\n";
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_event_assignement");
- $db->begin();
- $sqlSelect = "SELECT a.id, a.fk_user_action";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
- $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";
- $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')";
- $sqlSelect .= " ORDER BY a.id";
- //print $sqlSelect;
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlUpdate = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element) ";
- $sqlUpdate .= "VALUES(".$obj->id.", 'user', ".$obj->fk_user_action.")";
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if (!$error)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migrate event assignement to owner
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_event_assignement_contact($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationEventsContact')."</b><br>\n";
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_event_assignement");
- $db->begin();
- $sqlSelect = "SELECT a.id, a.fk_contact";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
- $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";
- $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')";
- $sqlSelect .= " ORDER BY a.id";
- //print $sqlSelect;
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlUpdate = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element) ";
- $sqlUpdate .= "VALUES(".$obj->id.", 'socpeople', ".$obj->fk_contact.")";
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if (!$error)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migrate to reset the blocked log for V7+ algorithm
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_reset_blocked_log($db, $langs, $conf)
- {
- global $user;
- require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationResetBlockedLog')."</b><br>\n";
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_reset_blocked_log");
- $db->begin();
- $sqlSelect = "SELECT DISTINCT entity";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."blockedlog";
- //print $sqlSelect;
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- print 'Process entity '.$obj->entity;
- $sqlSearch = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."blockedlog WHERE action = 'MODULE_SET' and entity = ".$obj->entity;
- $resqlSearch = $db->query($sqlSearch);
- if ($resqlSearch)
- {
- $objSearch = $db->fetch_object($resqlSearch);
- //var_dump($objSearch);
- if ($objSearch && $objSearch->nb == 0)
- {
- print ' - Record for entity must be reset...';
- $sqlUpdate = "DELETE FROM ".MAIN_DB_PREFIX."blockedlog";
- $sqlUpdate .= " WHERE entity = ".$obj->entity;
- $resqlUpdate = $db->query($sqlUpdate);
- if (!$resqlUpdate)
- {
- $error++;
- dol_print_error($db);
- } else {
- // Add set line
- $object = new stdClass();
- $object->id = 1;
- $object->element = 'module';
- $object->ref = 'systemevent';
- $object->entity = $obj->entity;
- $object->date = dol_now();
- $b = new BlockedLog($db);
- $b->setObjectData($object, 'MODULE_SET', 0);
- $res = $b->create($user);
- if ($res <= 0) {
- $error++;
- }
- }
- } else {
- print ' - '.$langs->trans('AlreadyInV7').'<br>';
- }
- } else {
- dol_print_error($db);
- }
- $i++;
- }
- } else {
- print $langs->trans('NothingToDo')."<br>\n";
- }
- if (!$error)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migrate to add entity value into llx_societe_remise
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_remise_entity($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationRemiseEntity')."</b><br>\n";
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_remise_entity");
- $db->begin();
- $sqlSelect = "SELECT sr.rowid, s.entity";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."societe_remise as sr, ".MAIN_DB_PREFIX."societe as s";
- $sqlSelect .= " WHERE sr.fk_soc = s.rowid and sr.entity != s.entity";
- //print $sqlSelect;
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."societe_remise SET";
- $sqlUpdate .= " entity = ".$obj->entity;
- $sqlUpdate .= " WHERE rowid = ".$obj->rowid;
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if (!$error)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migrate to add entity value into llx_societe_remise_except
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_remise_except_entity($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<br>';
- print '<b>'.$langs->trans('MigrationRemiseExceptEntity')."</b><br>\n";
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_remise_except_entity");
- $db->begin();
- $sqlSelect = "SELECT sr.rowid, sr.fk_soc, sr.fk_facture_source, sr.fk_facture, sr.fk_facture_line";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as sr";
- //print $sqlSelect;
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- if (!empty($obj->fk_facture_source) || !empty($obj->fk_facture))
- {
- $fk_facture = (!empty($obj->fk_facture_source) ? $obj->fk_facture_source : $obj->fk_facture);
- $sqlSelect2 = "SELECT f.entity";
- $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."facture as f";
- $sqlSelect2 .= " WHERE f.rowid = ".$fk_facture;
- } elseif (!empty($obj->fk_facture_line))
- {
- $sqlSelect2 = "SELECT f.entity";
- $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."facturedet as fd";
- $sqlSelect2 .= " WHERE fd.rowid = ".$obj->fk_facture_line;
- $sqlSelect2 .= " AND fd.fk_facture = f.rowid";
- } else {
- $sqlSelect2 = "SELECT s.entity";
- $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."societe as s";
- $sqlSelect2 .= " WHERE s.rowid = ".$obj->fk_soc;
- }
- $resql2 = $db->query($sqlSelect2);
- if ($resql2)
- {
- if ($db->num_rows($resql2) > 0)
- {
- $obj2 = $db->fetch_object($resql2);
- $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."societe_remise_except SET";
- $sqlUpdate .= " entity = ".$obj2->entity;
- $sqlUpdate .= " WHERE rowid = ".$obj->rowid;
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- }
- } else {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if (!$error)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migrate to add entity value into llx_user_rights
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_user_rights_entity($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<b>'.$langs->trans('MigrationUserRightsEntity')."</b><br>\n";
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_user_rights_entity");
- $db->begin();
- $sqlSelect = "SELECT u.rowid, u.entity";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."user as u";
- $sqlSelect .= " WHERE u.entity > 1";
- //print $sqlSelect;
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."user_rights SET";
- $sqlUpdate .= " entity = ".$obj->entity;
- $sqlUpdate .= " WHERE fk_user = ".$obj->rowid;
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if (!$error)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migrate to add entity value into llx_usergroup_rights
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_usergroup_rights_entity($db, $langs, $conf)
- {
- print '<tr><td colspan="4">';
- print '<b>'.$langs->trans('MigrationUserGroupRightsEntity')."</b><br>\n";
- $error = 0;
- dolibarr_install_syslog("upgrade2::migrate_usergroup_rights_entity");
- $db->begin();
- $sqlSelect = "SELECT u.rowid, u.entity";
- $sqlSelect .= " FROM ".MAIN_DB_PREFIX."usergroup as u";
- $sqlSelect .= " WHERE u.entity > 1";
- //print $sqlSelect;
- $resql = $db->query($sqlSelect);
- if ($resql)
- {
- $i = 0;
- $num = $db->num_rows($resql);
- if ($num)
- {
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
- $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."usergroup_rights SET";
- $sqlUpdate .= " entity = ".$obj->entity;
- $sqlUpdate .= " WHERE fk_usergroup = ".$obj->rowid;
- $result = $db->query($sqlUpdate);
- if (!$result)
- {
- $error++;
- dol_print_error($db);
- }
- print ". ";
- $i++;
- }
- } else {
- print $langs->trans('AlreadyDone')."<br>\n";
- }
- if (!$error)
- {
- $db->commit();
- } else {
- $db->rollback();
- }
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '</td></tr>';
- }
- /**
- * Migration directory
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @param string $oldname Old name (relative to DOL_DATA_ROOT)
- * @param string $newname New name (relative to DOL_DATA_ROOT)
- * @return void
- */
- function migrate_rename_directories($db, $langs, $conf, $oldname, $newname)
- {
- dolibarr_install_syslog("upgrade2::migrate_rename_directories");
- if (is_dir(DOL_DATA_ROOT.$oldname) && !file_exists(DOL_DATA_ROOT.$newname))
- {
- dolibarr_install_syslog("upgrade2::migrate_rename_directories move ".DOL_DATA_ROOT.$oldname.' into '.DOL_DATA_ROOT.$newname);
- @rename(DOL_DATA_ROOT.$oldname, DOL_DATA_ROOT.$newname);
- }
- }
- /**
- * Delete deprecated files
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_delete_old_files($db, $langs, $conf)
- {
- $result = true;
- dolibarr_install_syslog("upgrade2::migrate_delete_old_files");
- // List of files to delete
- $filetodeletearray = array(
- '/core/triggers/interface_demo.class.php',
- '/core/menus/barre_left/default.php',
- '/core/menus/barre_top/default.php',
- '/core/modules/modComptabiliteExpert.class.php',
- '/core/modules/modCommercial.class.php',
- '/core/modules/modProduit.class.php',
- '/core/modules/modSkype.class.php',
- '/phenix/inc/triggers/interface_modPhenix_Phenixsynchro.class.php',
- '/webcalendar/inc/triggers/interface_modWebcalendar_webcalsynchro.class.php',
- '/core/triggers/interface_modWebcalendar_Webcalsynchro.class.php',
- '/core/triggers/interface_modCommande_Ecotax.class.php',
- '/core/triggers/interface_modCommande_fraisport.class.php',
- '/core/triggers/interface_modPropale_PropalWorkflow.class.php',
- '/core/menus/smartphone/iphone.lib.php',
- '/core/menus/smartphone/iphone_backoffice.php',
- '/core/menus/smartphone/iphone_frontoffice.php',
- '/core/menus/standard/auguria_backoffice.php',
- '/core/menus/standard/auguria_frontoffice.php',
- '/core/menus/standard/eldy_backoffice.php',
- '/core/menus/standard/eldy_frontoffice.php',
- '/core/modules/mailings/contacts2.modules.php',
- '/core/modules/mailings/contacts3.modules.php',
- '/core/modules/mailings/contacts4.modules.php',
- '/core/modules/mailings/framboise.modules.php',
- '/core/modules/mailings/dolibarr_services_expired.modules.php',
- '/core/modules/mailings/peche.modules.php',
- '/core/modules/mailings/poire.modules.php',
- '/core/modules/mailings/kiwi.modules.php',
- '/core/modules/facture/pdf_crabe.modules.php',
- '/core/modules/facture/pdf_oursin.modules.php',
- '/api/class/api_generic.class.php',
- '/categories/class/api_category.class.php',
- '/categories/class/api_deprecated_category.class.php',
- '/compta/facture/class/api_invoice.class.php',
- '/commande/class/api_commande.class.php',
- '/user/class/api_user.class.php',
- '/product/class/api_product.class.php',
- '/societe/class/api_contact.class.php',
- '/societe/class/api_thirdparty.class.php',
- '/support/online.php',
- '/takepos/class/actions_takepos.class.php',
- '/install/mysql/tables/llx_c_ticketsup_category.key.sql',
- '/install/mysql/tables/llx_c_ticketsup_category.sql',
- '/install/mysql/tables/llx_c_ticketsup_severity.key.sql',
- '/install/mysql/tables/llx_c_ticketsup_severity.sql',
- '/install/mysql/tables/llx_c_ticketsup_type.key.sql',
- '/install/mysql/tables/llx_c_ticketsup_type.sql'
- );
- foreach ($filetodeletearray as $filetodelete) {
- //print '<b>'DOL_DOCUMENT_ROOT.$filetodelete."</b><br>\n";
- $result = 1;
- if (file_exists(DOL_DOCUMENT_ROOT.$filetodelete)) {
- $result = dol_delete_file(DOL_DOCUMENT_ROOT.$filetodelete, 0, 0, 0, null, true, false);
- if (!$result) {
- $langs->load("errors");
- print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteFile", DOL_DOCUMENT_ROOT.$filetodelete);
- print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
- } else {
- //print $langs->trans("FileWasRemoved", $filetodelete).'<br>';
- }
- }
- }
- return $result;
- }
- /**
- * Remove deprecated directories
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_delete_old_dir($db, $langs, $conf)
- {
- $result = true;
- dolibarr_install_syslog("upgrade2::migrate_delete_old_dir");
- // List of files to delete
- $filetodeletearray = array(
- DOL_DOCUMENT_ROOT.'/core/modules/facture/terre',
- DOL_DOCUMENT_ROOT.'/core/modules/facture/mercure',
- );
- foreach ($filetodeletearray as $filetodelete) {
- //print '<b>'.$filetodelete."</b><br>\n";
- if (file_exists($filetodelete)) {
- $result = dol_delete_dir_recursive($filetodelete);
- }
- if (!$result) {
- $langs->load("errors");
- print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteDir", $filetodelete);
- print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
- }
- }
- return $result;
- }
- /**
- * Disable/Reenable features modules.
- * We must do this when internal menu of module or permissions has changed
- * or when triggers have moved.
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @param array $listofmodule List of modules
- * @param int $force 1=Reload module even if not already loaded
- * @return void
- */
- function migrate_reload_modules($db, $langs, $conf, $listofmodule = array(), $force = 0)
- {
- if (count($listofmodule) == 0) return;
- dolibarr_install_syslog("upgrade2::migrate_reload_modules force=".$force.", listofmodule=".join(',', array_keys($listofmodule)));
- foreach ($listofmodule as $moduletoreload => $reloadmode) // reloadmodule can be 'noboxes', 'newboxdefonly', 'forceactivate'
- {
- if (empty($moduletoreload) || (empty($conf->global->$moduletoreload) && !$force)) continue; // Discard reload if module not enabled
- $mod = null;
- if ($moduletoreload == 'MAIN_MODULE_AGENDA')
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Agenda module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modAgenda.class.php';
- if ($res) {
- $mod = new modAgenda($db);
- $mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_API')
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Rest API module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modApi.class.php';
- if ($res) {
- $mod = new modApi($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_BARCODE')
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Barcode module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modBarcode.class.php';
- if ($res) {
- $mod = new modBarcode($db);
- $mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_CRON')
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Cron module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modCron.class.php';
- if ($res) {
- $mod = new modCron($db);
- $mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_SOCIETE')
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Societe module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modSociete.class.php';
- if ($res) {
- $mod = new modSociete($db);
- $mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_PRODUIT') // Permission has changed into 2.7
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Produit module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modProduct.class.php';
- if ($res) {
- $mod = new modProduct($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_SERVICE') // Permission has changed into 2.7
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Service module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modService.class.php';
- if ($res) {
- $mod = new modService($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_COMMANDE') // Permission has changed into 2.9
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Commande module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modCommande.class.php';
- if ($res) {
- $mod = new modCommande($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_FACTURE') // Permission has changed into 2.9
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Facture module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modFacture.class.php';
- if ($res) {
- $mod = new modFacture($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_FOURNISSEUR') // Permission has changed into 2.9
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Fournisseur module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modFournisseur.class.php';
- if ($res) {
- $mod = new modFournisseur($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_HOLIDAY') // Permission and tabs has changed into 3.8
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Leave Request module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modHoliday.class.php';
- if ($res) {
- $mod = new modHoliday($db);
- $mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_DEPLACEMENT') // Permission has changed into 3.0
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Deplacement module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modDeplacement.class.php';
- if ($res) {
- $mod = new modDeplacement($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_EXPENSEREPORT')
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Expense Report module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modExpenseReport.class.php';
- if ($res) {
- $mod = new modExpenseReport($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_DON') // Permission has changed into 3.0
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Don module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modDon.class.php';
- if ($res) {
- $mod = new modDon($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_ECM') // Permission has changed into 3.0 and 3.1
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate ECM module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modECM.class.php';
- if ($res) {
- $mod = new modECM($db);
- $mod->remove('noboxes'); // We need to remove because a permission id has been removed
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_PAYBOX') // Permission has changed into 3.0
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Paybox module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modPaybox.class.php';
- if ($res) {
- $mod = new modPaybox($db);
- $mod->remove('noboxes'); // We need to remove because id of module has changed
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_SUPPLIERPROPOSAL') // Module after 3.5
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Supplier Proposal module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modSupplierProposal.class.php';
- if ($res) {
- $mod = new modSupplierProposal($db);
- $mod->remove('noboxes'); // We need to remove because id of module has changed
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_OPENSURVEY') // Permission has changed into 3.0
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Opensurvey module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modOpenSurvey.class.php';
- if ($res) {
- $mod = new modOpenSurvey($db);
- $mod->remove('noboxes'); // We need to remove because menu entries has changed
- $mod->init($reloadmode);
- }
- } elseif ($moduletoreload == 'MAIN_MODULE_TAKEPOS') // Permission has changed into 10.0
- {
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Takepos module");
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modTakePos.class.php';
- if ($res) {
- $mod = new modTakePos($db);
- $mod->remove('noboxes'); // We need to remove because menu entries has changed
- $mod->init($reloadmode);
- }
- } else {
- $reg = array();
- $tmp = preg_match('/MAIN_MODULE_([a-zA-Z0-9]+)/', $moduletoreload, $reg);
- if (!empty($reg[1]))
- {
- if (strtoupper($moduletoreload) == $moduletoreload) // If key is un uppercase
- {
- $moduletoreloadshort = ucfirst(strtolower($reg[1]));
- } else // If key is a mix of up and low case
- {
- $moduletoreloadshort = $reg[1];
- }
- dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module ".$moduletoreloadshort." with mode ".$reloadmode);
- $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/mod'.$moduletoreloadshort.'.class.php';
- if ($res) {
- $classname = 'mod'.$moduletoreloadshort;
- $mod = new $classname($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- } else {
- dolibarr_install_syslog('Failed to include '.DOL_DOCUMENT_ROOT.'/core/modules/mod'.$moduletoreloadshort.'.class.php');
- $res = @dol_include_once(strtolower($moduletoreloadshort).'/core/modules/mod'.$moduletoreloadshort.'.class.php');
- if ($res) {
- $classname = 'mod'.$moduletoreloadshort;
- $mod = new $classname($db);
- //$mod->remove('noboxes');
- $mod->init($reloadmode);
- } else {
- dolibarr_install_syslog('Failed to include '.strtolower($moduletoreloadshort).'/core/modules/mod'.$moduletoreloadshort.'.class.php');
- }
- }
- } else {
- dolibarr_install_syslog("Error, can't find module with name ".$moduletoreload, LOG_WARNING);
- print "Error, can't find module with name ".$moduletoreload;
- }
- }
- if (!empty($mod) && is_object($mod))
- {
- print '<tr><td colspan="4">';
- print '<b>'.$langs->trans('Upgrade').'</b>: ';
- print $langs->trans('MigrationReloadModule').' '.$mod->getName(); // We keep getName outside of trans because getName is already encoded/translated
- print "<!-- (".$reloadmode.") -->";
- print "<br>\n";
- print '</td></tr>';
- }
- }
- }
- /**
- * Reload menu if dynamic menus, if modified by version
- *
- * @param DoliDB $db Database handler
- * @param Translate $langs Object langs
- * @param Conf $conf Object conf
- * @return void
- */
- function migrate_reload_menu($db, $langs, $conf)
- {
- global $conf;
- dolibarr_install_syslog("upgrade2::migrate_reload_menu");
- // Define list of menu handlers to initialize
- $listofmenuhandler = array();
- if ($conf->global->MAIN_MENU_STANDARD == 'auguria_menu' || $conf->global->MAIN_MENU_SMARTPHONE == 'auguria_menu'
- || $conf->global->MAIN_MENUFRONT_STANDARD == 'auguria_menu' || $conf->global->MAIN_MENUFRONT_SMARTPHONE == 'auguria_menu')
- {
- $listofmenuhandler['auguria'] = 1; // We set here only dynamic menu handlers
- }
- foreach ($listofmenuhandler as $key => $val)
- {
- print '<tr><td colspan="4">';
- //print "x".$key;
- print '<br>';
- print '<b>'.$langs->trans('Upgrade').'</b>: '.$langs->trans('MenuHandler')." ".$key."<br>\n";
- // Load sql ini_menu_handler.sql file
- $dir = DOL_DOCUMENT_ROOT."/core/menus/";
- $file = 'init_menu_'.$key.'.sql';
- if (file_exists($dir.$file))
- {
- $result = run_sql($dir.$file, 1, '', 1, $key);
- }
- print '</td></tr>';
- }
- }
- /**
- * Migrate file from old path to new one for users
- *
- * @return void
- */
- function migrate_user_photospath()
- {
- global $conf, $db, $langs, $user;
- print '<tr><td colspan="4">';
- print '<b>'.$langs->trans('MigrationUserPhotoPath')."</b><br>\n";
- include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
- $fuser = new User($db);
- if (!is_object($user)) $user = $fuser; // To avoid error during migration
- $sql = "SELECT rowid as uid from ".MAIN_DB_PREFIX."user"; // Get list of all users
- $resql = $db->query($sql);
- if ($resql)
- {
- while ($obj = $db->fetch_object($resql))
- {
- $fuser->fetch($obj->uid);
- //echo '<hr>'.$fuser->id.' -> '.$fuser->entity;
- $entity = (empty($fuser->entity) ? 1 : $fuser->entity);
- if ($entity > 1) {
- $dir = DOL_DATA_ROOT.'/'.$entity.'/users';
- } else {
- $dir = $conf->user->multidir_output[$entity]; // $conf->user->multidir_output[] for each entity is construct by the multicompany module
- }
- if ($dir)
- {
- //print "Process user id ".$fuser->id."<br>\n";
- $origin = $dir.'/'.get_exdir($fuser->id, 2, 0, 1, $fuser, 'user'); // Use old behaviour to get x/y path
- $destin = $dir.'/'.$fuser->id;
- $origin_osencoded = dol_osencode($origin);
- dol_mkdir($destin);
- //echo '<hr>'.$origin.' -> '.$destin;
- if (dol_is_dir($origin))
- {
- $handle = opendir($origin_osencoded);
- if (is_resource($handle))
- {
- while (($file = readdir($handle)) !== false)
- {
- if ($file == '.' || $file == '..') continue;
- if (dol_is_dir($origin.'/'.$file)) // it is a dir (like 'thumbs')
- {
- $thumbs = opendir($origin_osencoded.'/'.$file);
- if (is_resource($thumbs))
- {
- dol_mkdir($destin.'/'.$file);
- while (($thumb = readdir($thumbs)) !== false)
- {
- if (!dol_is_file($destin.'/'.$file.'/'.$thumb))
- {
- if ($thumb == '.' || $thumb == '..') continue;
- //print $origin.'/'.$file.'/'.$thumb.' -> '.$destin.'/'.$file.'/'.$thumb.'<br>'."\n";
- print '.';
- dol_copy($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb, 0, 0);
- //var_dump('aaa');exit;
- }
- }
- // dol_delete_dir($origin.'/'.$file);
- }
- } else // it is a file
- {
- if (!dol_is_file($destin.'/'.$file))
- {
- //print $origin.'/'.$file.' -> '.$destin.'/'.$file.'<br>'."\n";
- print '.';
- dol_copy($origin.'/'.$file, $destin.'/'.$file, 0, 0);
- //var_dump('eee');exit;
- }
- }
- }
- }
- }
- }
- }
- }
- print '</td></tr>';
- }
- /* A faire egalement: Modif statut paye et fk_facture des factures payes completement
- On recherche facture incorrecte:
- 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
- having f.total_ttc = sum(pf.amount)
- On les corrige:
- update llx_facture set paye=1, fk_statut=2 where close_code is null
- and rowid in (...)
- */
- /**
- * Migrate users fields facebook and co to socialnetworks
- *
- * @return void
- */
- function migrate_users_socialnetworks()
- {
- global $db, $langs;
- // skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
- $error = 0;
- $db->begin();
- print '<tr><td colspan="4">';
- $sql = 'SELECT rowid, socialnetworks';
- $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'user WHERE';
- $sql .= " skype IS NOT NULL OR skype <> ''";
- $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
- $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
- $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
- $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
- $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
- $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
- $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
- $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
- //print $sql;
- $resql = $db->query($sql);
- if ($resql) {
- while ($obj = $db->fetch_object($resql)) {
- $arraysocialnetworks = array();
- if (!empty($obj->skype)) {
- $arraysocialnetworks['skype'] = $obj->skype;
- }
- if (!empty($obj->twitter)) {
- $arraysocialnetworks['twitter'] = $obj->twitter;
- }
- if (!empty($obj->facebook)) {
- $arraysocialnetworks['facebook'] = $obj->facebook;
- }
- if (!empty($obj->linkedin)) {
- $arraysocialnetworks['linkedin'] = $obj->linkedin;
- }
- if (!empty($obj->instagram)) {
- $arraysocialnetworks['instagram'] = $obj->instagram;
- }
- if (!empty($obj->snapchat)) {
- $arraysocialnetworks['snapchat'] = $obj->snapchat;
- }
- if (!empty($obj->googleplus)) {
- $arraysocialnetworks['googleplus'] = $obj->googleplus;
- }
- if (!empty($obj->youtube)) {
- $arraysocialnetworks['youtube'] = $obj->youtube;
- }
- if (!empty($obj->whatsapp)) {
- $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
- }
- if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
- $obj->socialnetworks = '[]';
- }
- $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
- $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."user SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
- $sqlupd .= ', skype=null';
- $sqlupd .= ', twitter=null';
- $sqlupd .= ', facebook=null';
- $sqlupd .= ', linkedin=null';
- $sqlupd .= ', instagram=null';
- $sqlupd .= ', snapchat=null';
- $sqlupd .= ', googleplus=null';
- $sqlupd .= ', youtube=null';
- $sqlupd .= ', whatsapp=null';
- $sqlupd .= ' WHERE rowid='.$obj->rowid;
- //print $sqlupd."<br>";
- $resqlupd = $db->query($sqlupd);
- if (!$resqlupd) {
- dol_print_error($db);
- $error++;
- }
- }
- } else {
- $error++;
- }
- if (!$error) {
- $db->commit();
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Users')."</b><br>\n";
- print '</td></tr>';
- }
- /**
- * Migrate members fields facebook and co to socialnetworks
- *
- * @return void
- */
- function migrate_members_socialnetworks()
- {
- global $db, $langs;
- print '<tr><td colspan="4">';
- $error = 0;
- $db->begin();
- print '<tr><td colspan="4">';
- $sql = 'SELECT rowid, socialnetworks';
- $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'adherent WHERE ';
- $sql .= " skype IS NOT NULL OR skype <> ''";
- $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
- $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
- $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
- $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
- $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
- $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
- $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
- $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
- //print $sql;
- $resql = $db->query($sql);
- if ($resql) {
- while ($obj = $db->fetch_object($resql)) {
- $arraysocialnetworks = array();
- if (!empty($obj->skype)) {
- $arraysocialnetworks['skype'] = $obj->skype;
- }
- if (!empty($obj->twitter)) {
- $arraysocialnetworks['twitter'] = $obj->twitter;
- }
- if (!empty($obj->facebook)) {
- $arraysocialnetworks['facebook'] = $obj->facebook;
- }
- if (!empty($obj->linkedin)) {
- $arraysocialnetworks['linkedin'] = $obj->linkedin;
- }
- if (!empty($obj->instagram)) {
- $arraysocialnetworks['instagram'] = $obj->instagram;
- }
- if (!empty($obj->snapchat)) {
- $arraysocialnetworks['snapchat'] = $obj->snapchat;
- }
- if (!empty($obj->googleplus)) {
- $arraysocialnetworks['googleplus'] = $obj->googleplus;
- }
- if (!empty($obj->youtube)) {
- $arraysocialnetworks['youtube'] = $obj->youtube;
- }
- if (!empty($obj->whatsapp)) {
- $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
- }
- if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
- $obj->socialnetworks = '[]';
- }
- $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
- $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."adherent SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
- $sqlupd .= ', skype=null';
- $sqlupd .= ', twitter=null';
- $sqlupd .= ', facebook=null';
- $sqlupd .= ', linkedin=null';
- $sqlupd .= ', instagram=null';
- $sqlupd .= ', snapchat=null';
- $sqlupd .= ', googleplus=null';
- $sqlupd .= ', youtube=null';
- $sqlupd .= ', whatsapp=null';
- $sqlupd .= ' WHERE rowid='.$obj->rowid;
- //print $sqlupd."<br>";
- $resqlupd = $db->query($sqlupd);
- if (!$resqlupd) {
- dol_print_error($db);
- $error++;
- }
- }
- } else {
- $error++;
- }
- if (!$error) {
- $db->commit();
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Members')."</b><br>\n";
- print '</td></tr>';
- }
- /**
- * Migrate contacts fields facebook and co to socialnetworks
- *
- * @return void
- */
- function migrate_contacts_socialnetworks()
- {
- global $db, $langs;
- // jabberid,skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
- $error = 0;
- $db->begin();
- print '<tr><td colspan="4">';
- $sql = 'SELECT rowid, socialnetworks';
- $sql .= ', jabberid, skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'socpeople WHERE ';
- $sql .= " jabberid IS NOT NULL OR jabberid <> ''";
- $sql .= " OR skype IS NOT NULL OR skype <> ''";
- $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
- $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
- $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
- $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
- $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
- $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
- $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
- $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
- //print $sql;
- $resql = $db->query($sql);
- if ($resql) {
- while ($obj = $db->fetch_object($resql)) {
- $arraysocialnetworks = array();
- if (!empty($obj->jabberid)) {
- $arraysocialnetworks['jabber'] = $obj->jabberid;
- }
- if (!empty($obj->skype)) {
- $arraysocialnetworks['skype'] = $obj->skype;
- }
- if (!empty($obj->twitter)) {
- $arraysocialnetworks['twitter'] = $obj->twitter;
- }
- if (!empty($obj->facebook)) {
- $arraysocialnetworks['facebook'] = $obj->facebook;
- }
- if (!empty($obj->linkedin)) {
- $arraysocialnetworks['linkedin'] = $obj->linkedin;
- }
- if (!empty($obj->instagram)) {
- $arraysocialnetworks['instagram'] = $obj->instagram;
- }
- if (!empty($obj->snapchat)) {
- $arraysocialnetworks['snapchat'] = $obj->snapchat;
- }
- if (!empty($obj->googleplus)) {
- $arraysocialnetworks['googleplus'] = $obj->googleplus;
- }
- if (!empty($obj->youtube)) {
- $arraysocialnetworks['youtube'] = $obj->youtube;
- }
- if (!empty($obj->whatsapp)) {
- $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
- }
- if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
- $obj->socialnetworks = '[]';
- }
- $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
- $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."socpeople SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
- $sqlupd .= ', jabberid=null';
- $sqlupd .= ', skype=null';
- $sqlupd .= ', twitter=null';
- $sqlupd .= ', facebook=null';
- $sqlupd .= ', linkedin=null';
- $sqlupd .= ', instagram=null';
- $sqlupd .= ', snapchat=null';
- $sqlupd .= ', googleplus=null';
- $sqlupd .= ', youtube=null';
- $sqlupd .= ', whatsapp=null';
- $sqlupd .= ' WHERE rowid='.$obj->rowid;
- //print $sqlupd."<br>";
- $resqlupd = $db->query($sqlupd);
- if (!$resqlupd) {
- dol_print_error($db);
- $error++;
- }
- }
- } else {
- $error++;
- }
- if (!$error) {
- $db->commit();
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Contacts')."</b><br>\n";
- print '</td></tr>';
- }
- /**
- * Migrate thirdparties fields facebook and co to socialnetworks
- *
- * @return void
- */
- function migrate_thirdparties_socialnetworks()
- {
- global $db, $langs;
- // skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
- $error = 0;
- $db->begin();
- print '<tr><td colspan="4">';
- $sql = 'SELECT rowid, socialnetworks';
- $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'societe WHERE ';
- $sql .= " skype IS NOT NULL OR skype <> ''";
- $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
- $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
- $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
- $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
- $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
- $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
- $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
- $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
- //print $sql;
- $resql = $db->query($sql);
- if ($resql) {
- while ($obj = $db->fetch_object($resql)) {
- $arraysocialnetworks = array();
- if (!empty($obj->skype)) {
- $arraysocialnetworks['skype'] = $obj->skype;
- }
- if (!empty($obj->twitter)) {
- $arraysocialnetworks['twitter'] = $obj->twitter;
- }
- if (!empty($obj->facebook)) {
- $arraysocialnetworks['facebook'] = $obj->facebook;
- }
- if (!empty($obj->linkedin)) {
- $arraysocialnetworks['linkedin'] = $obj->linkedin;
- }
- if (!empty($obj->instagram)) {
- $arraysocialnetworks['instagram'] = $obj->instagram;
- }
- if (!empty($obj->snapchat)) {
- $arraysocialnetworks['snapchat'] = $obj->snapchat;
- }
- if (!empty($obj->googleplus)) {
- $arraysocialnetworks['googleplus'] = $obj->googleplus;
- }
- if (!empty($obj->youtube)) {
- $arraysocialnetworks['youtube'] = $obj->youtube;
- }
- if (!empty($obj->whatsapp)) {
- $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
- }
- if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
- $obj->socialnetworks = '[]';
- }
- $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
- $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."societe SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
- $sqlupd .= ', skype=null';
- $sqlupd .= ', twitter=null';
- $sqlupd .= ', facebook=null';
- $sqlupd .= ', linkedin=null';
- $sqlupd .= ', instagram=null';
- $sqlupd .= ', snapchat=null';
- $sqlupd .= ', googleplus=null';
- $sqlupd .= ', youtube=null';
- $sqlupd .= ', whatsapp=null';
- $sqlupd .= ' WHERE rowid='.$obj->rowid;
- //print $sqlupd."<br>";
- $resqlupd = $db->query($sqlupd);
- if (!$resqlupd) {
- dol_print_error($db);
- $error++;
- }
- }
- } else {
- $error++;
- }
- if (!$error) {
- $db->commit();
- } else {
- dol_print_error($db);
- $db->rollback();
- }
- print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Thirdparties')."</b><br>\n";
- print '</td></tr>';
- }
|