upgrade2.php 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825
  1. <?php
  2. /* Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  3. * Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Upgrade scripts can be ran from command line with syntax:
  21. *
  22. * cd htdocs/install
  23. * php upgrade.php 3.4.0 3.5.0
  24. * php upgrade2.php 3.4.0 3.5.0
  25. *
  26. * Return code is 0 if OK, >0 if error
  27. */
  28. /**
  29. * \file htdocs/install/upgrade2.php
  30. * \brief Upgrade some data
  31. */
  32. include_once 'inc.php';
  33. if (! file_exists($conffile))
  34. {
  35. 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").';
  36. }
  37. require_once $conffile; if (! isset($dolibarr_main_db_type)) $dolibarr_main_db_type='mysql'; // For backward compatibility
  38. require_once $dolibarr_main_document_root . '/compta/facture/class/facture.class.php';
  39. require_once $dolibarr_main_document_root . '/comm/propal/class/propal.class.php';
  40. require_once $dolibarr_main_document_root . '/contrat/class/contrat.class.php';
  41. require_once $dolibarr_main_document_root . '/commande/class/commande.class.php';
  42. require_once $dolibarr_main_document_root . '/fourn/class/fournisseur.commande.class.php';
  43. require_once $dolibarr_main_document_root . '/core/lib/price.lib.php';
  44. require_once $dolibarr_main_document_root . '/core/class/menubase.class.php';
  45. require_once $dolibarr_main_document_root . '/core/lib/files.lib.php';
  46. $grant_query='';
  47. $etape = 2;
  48. $error = 0;
  49. // Cette page peut etre longue. On augmente le delai autorise.
  50. // Ne fonctionne que si on est pas en safe_mode.
  51. $err=error_reporting();
  52. error_reporting(0);
  53. @set_time_limit(120);
  54. error_reporting($err);
  55. $setuplang=GETPOST("selectlang",'',3)?GETPOST("selectlang",'',3):'auto';
  56. $langs->setDefaultLang($setuplang);
  57. $versionfrom=GETPOST("versionfrom",'',3)?GETPOST("versionfrom",'',3):(empty($argv[1])?'':$argv[1]);
  58. $versionto=GETPOST("versionto",'',3)?GETPOST("versionto",'',3):(empty($argv[2])?'':$argv[2]);
  59. $langs->load('admin');
  60. $langs->load('install');
  61. $langs->load("bills");
  62. $langs->load("suppliers");
  63. if ($dolibarr_main_db_type == 'mysql') $choix=1;
  64. if ($dolibarr_main_db_type == 'mysqli') $choix=1;
  65. if ($dolibarr_main_db_type == 'pgsql') $choix=2;
  66. if ($dolibarr_main_db_type == 'mssql') $choix=3;
  67. dolibarr_install_syslog("upgrade2: Entering upgrade2.php page");
  68. if (! is_object($conf)) dolibarr_install_syslog("upgrade2: conf file not initialized",LOG_ERR);
  69. /*
  70. * View
  71. */
  72. pHeader('','etape5',GETPOST("action")?GETPOST("action"):'upgrade','versionfrom='.$versionfrom.'&versionto='.$versionto);
  73. if (! GETPOST("action") || preg_match('/upgrade/i',GETPOST('action')))
  74. {
  75. print '<h3>'.$langs->trans('DataMigration').'</h3>';
  76. print '<table cellspacing="0" cellpadding="1" border="0" width="100%">';
  77. // If password is encoded, we decode it
  78. if (preg_match('/crypted:/i',$dolibarr_main_db_pass) || ! empty($dolibarr_main_db_encrypted_pass))
  79. {
  80. require_once $dolibarr_main_document_root.'/core/lib/security.lib.php';
  81. if (preg_match('/crypted:/i',$dolibarr_main_db_pass))
  82. {
  83. $dolibarr_main_db_pass = preg_replace('/crypted:/i', '', $dolibarr_main_db_pass);
  84. $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_pass);
  85. $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
  86. }
  87. else $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_encrypted_pass);
  88. }
  89. // $conf is already instancied inside inc.php
  90. $conf->db->type = $dolibarr_main_db_type;
  91. $conf->db->host = $dolibarr_main_db_host;
  92. $conf->db->port = $dolibarr_main_db_port;
  93. $conf->db->name = $dolibarr_main_db_name;
  94. $conf->db->user = $dolibarr_main_db_user;
  95. $conf->db->pass = $dolibarr_main_db_pass;
  96. $db=getDoliDBInstance($conf->db->type,$conf->db->host,$conf->db->user,$conf->db->pass,$conf->db->name,$conf->db->port);
  97. // Create the global $hookmanager object
  98. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  99. $hookmanager=new HookManager($db);
  100. if ($db->connected != 1)
  101. {
  102. print '<tr><td colspan="4">'.$langs->trans("ErrorFailedToConnectToDatabase",$conf->db->name).'</td><td align="right">'.$langs->trans('Error').'</td></tr>';
  103. dolibarr_install_syslog('upgrade2: Failed to connect to database : '.$conf->db->name.' on '.$conf->db->host.' for user '.$conf->db->user, LOG_ERR);
  104. $error++;
  105. }
  106. if (! $error)
  107. {
  108. if($db->database_selected == 1)
  109. {
  110. dolibarr_install_syslog('upgrade2: Database connection successfull : '.$dolibarr_main_db_name);
  111. }
  112. else
  113. {
  114. $error++;
  115. }
  116. }
  117. if (empty($dolibarr_main_db_encryption)) $dolibarr_main_db_encryption=0;
  118. $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
  119. if (empty($dolibarr_main_db_cryptkey)) $dolibarr_main_db_cryptkey='';
  120. $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
  121. // Chargement config
  122. if (! $error) $conf->setValues($db);
  123. /***************************************************************************************
  124. *
  125. * Migration des donnees
  126. *
  127. ***************************************************************************************/
  128. if (! $error)
  129. {
  130. // Current version is $conf->global->MAIN_VERSION_LAST_UPGRADE
  131. // Version to install is DOL_VERSION
  132. $dolibarrlastupgradeversionarray=preg_split('/[\.-]/',isset($conf->global->MAIN_VERSION_LAST_UPGRADE)?$conf->global->MAIN_VERSION_LAST_UPGRADE:$conf->global->MAIN_VERSION_LAST_INSTALL);
  133. $db->begin();
  134. // Chaque action de migration doit renvoyer une ligne sur 4 colonnes avec
  135. // dans la 1ere colonne, la description de l'action a faire
  136. // dans la 4eme colonne, le texte 'OK' si fait ou 'AlreadyDone' si rien n'est fait ou 'Error'
  137. $versiontoarray=explode('.',$versionto);
  138. $afterversionarray=explode('.','2.0.0');
  139. $beforeversionarray=explode('.','2.7.9');
  140. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  141. {
  142. // Script pour V2 -> V2.1
  143. migrate_paiements($db,$langs,$conf);
  144. migrate_contracts_det($db,$langs,$conf);
  145. migrate_contracts_date1($db,$langs,$conf);
  146. migrate_contracts_date2($db,$langs,$conf);
  147. migrate_contracts_date3($db,$langs,$conf);
  148. migrate_contracts_open($db,$langs,$conf);
  149. migrate_modeles($db,$langs,$conf);
  150. migrate_price_propal($db,$langs,$conf);
  151. migrate_price_commande($db,$langs,$conf);
  152. migrate_price_commande_fournisseur($db,$langs,$conf);
  153. migrate_price_contrat($db,$langs,$conf);
  154. migrate_paiementfourn_facturefourn($db,$langs,$conf);
  155. // Script pour V2.1 -> V2.2
  156. migrate_paiements_orphelins_1($db,$langs,$conf);
  157. migrate_paiements_orphelins_2($db,$langs,$conf);
  158. migrate_links_transfert($db,$langs,$conf);
  159. // Script pour V2.2 -> V2.4
  160. migrate_commande_expedition($db,$langs,$conf);
  161. migrate_commande_livraison($db,$langs,$conf);
  162. migrate_detail_livraison($db,$langs,$conf);
  163. // Script pour V2.5 -> V2.6
  164. migrate_stocks($db,$langs,$conf);
  165. // Script pour V2.6 -> V2.7
  166. migrate_menus($db,$langs,$conf);
  167. migrate_commande_deliveryaddress($db,$langs,$conf);
  168. migrate_restore_missing_links($db,$langs,$conf);
  169. migrate_directories($db,$langs,$conf,'/compta','/banque');
  170. migrate_directories($db,$langs,$conf,'/societe','/mycompany');
  171. // Reload modules
  172. migrate_reload_modules($db,$langs,$conf);
  173. // Reload menus
  174. migrate_reload_menu($db,$langs,$conf,$versionto);
  175. }
  176. // Script for VX (X<2.8) -> V2.8
  177. $afterversionarray=explode('.','2.7.9');
  178. $beforeversionarray=explode('.','2.8.9');
  179. //print $versionto.' '.versioncompare($versiontoarray,$afterversionarray).' '.versioncompare($versiontoarray,$beforeversionarray);
  180. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  181. {
  182. migrate_price_facture($db,$langs,$conf); // Code of this function works for 2.8+ because need a field tva_tx
  183. migrate_relationship_tables($db,$langs,$conf,'co_exp','fk_commande','commande','fk_expedition','shipping');
  184. migrate_relationship_tables($db,$langs,$conf,'pr_exp','fk_propal','propal','fk_expedition','shipping');
  185. migrate_relationship_tables($db,$langs,$conf,'pr_liv','fk_propal','propal','fk_livraison','delivery');
  186. migrate_relationship_tables($db,$langs,$conf,'co_liv','fk_commande','commande','fk_livraison','delivery');
  187. migrate_relationship_tables($db,$langs,$conf,'co_pr','fk_propale','propal','fk_commande','commande');
  188. migrate_relationship_tables($db,$langs,$conf,'fa_pr','fk_propal','propal','fk_facture','facture');
  189. migrate_relationship_tables($db,$langs,$conf,'co_fa','fk_commande','commande','fk_facture','facture');
  190. migrate_project_user_resp($db,$langs,$conf);
  191. migrate_project_task_actors($db,$langs,$conf);
  192. // Reload modules
  193. migrate_reload_modules($db,$langs,$conf);
  194. // Reload menus
  195. migrate_reload_menu($db,$langs,$conf,$versionto);
  196. }
  197. // Script for VX (X<2.9) -> V2.9
  198. $afterversionarray=explode('.','2.8.9');
  199. $beforeversionarray=explode('.','2.9.9');
  200. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  201. {
  202. migrate_project_task_time($db,$langs,$conf);
  203. migrate_customerorder_shipping($db,$langs,$conf);
  204. migrate_shipping_delivery($db,$langs,$conf);
  205. migrate_shipping_delivery2($db,$langs,$conf);
  206. // Reload modules
  207. migrate_reload_modules($db,$langs,$conf);
  208. // Reload menus
  209. migrate_reload_menu($db,$langs,$conf,$versionto);
  210. }
  211. // Script for VX (X<3.0) -> V3.0
  212. $afterversionarray=explode('.','2.9.9');
  213. $beforeversionarray=explode('.','3.0.9');
  214. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  215. {
  216. // Reload modules
  217. migrate_reload_modules($db,$langs,$conf);
  218. // Reload menus
  219. migrate_reload_menu($db,$langs,$conf,$versionto);
  220. }
  221. // Script for VX (X<3.1) -> V3.1
  222. $afterversionarray=explode('.','3.0.9');
  223. $beforeversionarray=explode('.','3.1.9');
  224. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  225. {
  226. migrate_directories($db,$langs,$conf,'/rss','/externalrss');
  227. migrate_actioncomm_element($db,$langs,$conf);
  228. // Reload modules
  229. migrate_reload_modules($db,$langs,$conf);
  230. // Reload menus
  231. migrate_reload_menu($db,$langs,$conf,$versionto);
  232. }
  233. // Script for VX (X<3.2) -> V3.2
  234. $afterversionarray=explode('.','3.1.9');
  235. $beforeversionarray=explode('.','3.2.9');
  236. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  237. {
  238. migrate_price_contrat($db,$langs,$conf);
  239. migrate_mode_reglement($db,$langs,$conf);
  240. // Reload modules
  241. migrate_reload_modules($db,$langs,$conf);
  242. // Reload menus
  243. migrate_reload_menu($db,$langs,$conf,$versionto);
  244. }
  245. // Script for VX (X<3.3) -> V3.3
  246. $afterversionarray=explode('.','3.2.9');
  247. $beforeversionarray=explode('.','3.3.9');
  248. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  249. {
  250. migrate_categorie_association($db,$langs,$conf);
  251. }
  252. // Script for VX (X<3.4) -> V3.4
  253. // No specific scripts
  254. // Tasks to do always and only into last targeted version
  255. $afterversionarray=explode('.','3.4.9'); // target is after this
  256. $beforeversionarray=explode('.','3.5.9'); // target is before this
  257. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  258. {
  259. // Reload modules (this must be always and only into last targeted version)
  260. migrate_reload_modules($db,$langs,$conf);
  261. // Reload menus (this must be always and only into last targeted version)
  262. migrate_reload_menu($db,$langs,$conf,$versionto);
  263. }
  264. print '<tr><td colspan="4"><br>'.$langs->trans("MigrationFinished").'</td></tr>';
  265. // On commit dans tous les cas.
  266. // La procedure etant concue pour pouvoir passer plusieurs fois quelquesoit la situation.
  267. $db->commit();
  268. $db->close();
  269. // Actions for all version (not in database)
  270. migrate_delete_old_files($db,$langs,$conf);
  271. migrate_delete_old_dir($db,$langs,$conf);
  272. }
  273. print '</table>';
  274. }
  275. else
  276. {
  277. print '<div class="error">'.$langs->trans('ErrorWrongParameters').'</div>';
  278. $error++;
  279. }
  280. $ret=0;
  281. if ($error && isset($argv[1])) $ret=1;
  282. dol_syslog("Exit ".$ret);
  283. pFooter($error,$setuplang);
  284. if ($db->connected) $db->close();
  285. // Return code if ran from command line
  286. if ($ret) exit($ret);
  287. /**
  288. * Reporte liens vers une facture de paiements sur table de jointure (lien n-n paiements factures)
  289. *
  290. * @param DoliDB $db Database handler
  291. * @param Translate $langs Object langs
  292. * @param Conf $conf Object conf
  293. * @return void
  294. */
  295. function migrate_paiements($db,$langs,$conf)
  296. {
  297. print '<tr><td colspan="4">';
  298. print '<br>';
  299. print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
  300. $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement","fk_facture");
  301. $obj = $db->fetch_object($result);
  302. if ($obj)
  303. {
  304. $sql = "SELECT p.rowid, p.fk_facture, p.amount";
  305. $sql .= " FROM ".MAIN_DB_PREFIX."paiement as p";
  306. $sql .= " WHERE p.fk_facture > 0";
  307. $resql = $db->query($sql);
  308. dolibarr_install_syslog("upgrade2::migrate_paiements sql=".$sql);
  309. if ($resql)
  310. {
  311. $i = 0;
  312. $row = array();
  313. $num = $db->num_rows($resql);
  314. while ($i < $num)
  315. {
  316. $obj = $db->fetch_object($resql);
  317. $row[$i][0] = $obj->rowid ;
  318. $row[$i][1] = $obj->fk_facture;
  319. $row[$i][2] = $obj->amount;
  320. $i++;
  321. }
  322. }
  323. else
  324. {
  325. dol_print_error($db);
  326. }
  327. if ($num)
  328. {
  329. print $langs->trans('MigrationPaymentsNumberToUpdate', $num)."<br>\n";
  330. if ($db->begin())
  331. {
  332. $res = 0;
  333. $num=count($row);
  334. for ($i = 0; $i < $num; $i++)
  335. {
  336. $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
  337. $sql.= " VALUES (".$row[$i][1].",".$row[$i][0].",".$row[$i][2].")";
  338. $res += $db->query($sql);
  339. $sql = "UPDATE ".MAIN_DB_PREFIX."paiement SET fk_facture = 0 WHERE rowid = ".$row[$i][0];
  340. $res += $db->query($sql);
  341. print $langs->trans('MigrationProcessPaymentUpdate', $row[$i][0])."<br>\n";
  342. }
  343. }
  344. if ($res == (2 * count($row)))
  345. {
  346. $db->commit();
  347. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  348. }
  349. else
  350. {
  351. $db->rollback();
  352. print $langs->trans('MigrationUpdateFailed').'<br>';
  353. }
  354. }
  355. else
  356. {
  357. print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
  358. }
  359. }
  360. else
  361. {
  362. print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
  363. }
  364. print '</td></tr>';
  365. }
  366. /**
  367. * Corrige paiement orphelins (liens paumes suite a bugs)
  368. * Pour verifier s'il reste des orphelins:
  369. * 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)
  370. *
  371. * @param DoliDB $db Database handler
  372. * @param Translate $langs Object langs
  373. * @param Conf $conf Object conf
  374. * @return void
  375. */
  376. function migrate_paiements_orphelins_1($db,$langs,$conf)
  377. {
  378. print '<tr><td colspan="4">';
  379. print '<br>';
  380. print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
  381. $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement","fk_facture");
  382. $obj = $db->fetch_object($result);
  383. if ($obj)
  384. {
  385. // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
  386. $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
  387. $sql.= " bu2.url_id as socid";
  388. $sql.= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
  389. $sql.= " LEFT JOIN llx_paiement_facture as pf ON pf.fk_paiement = p.rowid";
  390. $sql.= " LEFT JOIN llx_bank_url as bu2 ON (bu.fk_bank=bu2.fk_bank AND bu2.type = 'company')";
  391. $sql.= " WHERE pf.rowid IS NULL AND (p.rowid=bu.url_id AND bu.type='payment') AND bu.fk_bank = b.rowid";
  392. $sql.= " AND b.rappro = 1";
  393. $sql.= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
  394. $resql = $db->query($sql);
  395. dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_1 sql=".$sql);
  396. $row = array();
  397. if ($resql)
  398. {
  399. $i = $j = 0;
  400. $num = $db->num_rows($resql);
  401. while ($i < $num)
  402. {
  403. $obj = $db->fetch_object($resql);
  404. if ($obj->pamount == $obj->bamount && $obj->socid) // Pour etre sur d'avoir bon cas
  405. {
  406. $row[$j]['paymentid'] = $obj->rowid ; // paymentid
  407. $row[$j]['pamount'] = $obj->pamount;
  408. $row[$j]['fk_bank'] = $obj->fk_bank;
  409. $row[$j]['bamount'] = $obj->bamount;
  410. $row[$j]['socid'] = $obj->socid;
  411. $row[$j]['datec'] = $obj->datec;
  412. $j++;
  413. }
  414. $i++;
  415. }
  416. }
  417. else
  418. {
  419. dol_print_error($db);
  420. }
  421. if (count($row))
  422. {
  423. print $langs->trans('OrphelinsPaymentsDetectedByMethod', 1).': '.count($row)."<br>\n";
  424. $db->begin();
  425. $res = 0;
  426. $num=count($row);
  427. for ($i = 0; $i < $num; $i++)
  428. {
  429. 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>';
  430. // On cherche facture sans lien paiement et du meme montant et pour meme societe.
  431. $sql=" SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
  432. $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
  433. $sql.=" WHERE f.fk_statut in (2,3) AND fk_soc = ".$row[$i]['socid']." AND total_ttc = ".$row[$i]['pamount'];
  434. $sql.=" AND pf.fk_facture IS NULL";
  435. $sql.=" ORDER BY f.fk_statut";
  436. //print $sql.'<br>';
  437. $resql=$db->query($sql);
  438. if ($resql)
  439. {
  440. $num = $db->num_rows($resql);
  441. //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
  442. if ($num >= 1)
  443. {
  444. $obj=$db->fetch_object($resql);
  445. $facid=$obj->rowid;
  446. $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
  447. $sql.= " VALUES (".$facid.",".$row[$i]['paymentid'].",".$row[$i]['pamount'].")";
  448. $res += $db->query($sql);
  449. print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
  450. }
  451. }
  452. else
  453. {
  454. print 'ERROR';
  455. }
  456. }
  457. if ($res > 0)
  458. {
  459. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  460. }
  461. else
  462. {
  463. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  464. }
  465. $db->commit();
  466. }
  467. else
  468. {
  469. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  470. }
  471. }
  472. else
  473. {
  474. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  475. }
  476. print '</td></tr>';
  477. }
  478. /**
  479. * Corrige paiement orphelins (liens paumes suite a bugs)
  480. * Pour verifier s'il reste des orphelins:
  481. * 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)
  482. *
  483. * @param DoliDB $db Database handler
  484. * @param Translate $langs Object langs
  485. * @param Conf $conf Object conf
  486. * @return void
  487. */
  488. function migrate_paiements_orphelins_2($db,$langs,$conf)
  489. {
  490. print '<tr><td colspan="4">';
  491. print '<br>';
  492. print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
  493. $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement","fk_facture");
  494. $obj = $db->fetch_object($result);
  495. if ($obj)
  496. {
  497. // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
  498. $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
  499. $sql.= " bu2.url_id as socid";
  500. $sql.= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
  501. $sql.= " LEFT JOIN llx_paiement_facture as pf ON pf.fk_paiement = p.rowid";
  502. $sql.= " LEFT JOIN llx_bank_url as bu2 ON (bu.fk_bank = bu2.fk_bank AND bu2.type = 'company')";
  503. $sql.= " WHERE pf.rowid IS NULL AND (p.fk_bank = bu.fk_bank AND bu.type = 'payment') AND bu.fk_bank = b.rowid";
  504. $sql.= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
  505. $resql = $db->query($sql);
  506. dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_2 sql=".$sql);
  507. $row = array();
  508. if ($resql)
  509. {
  510. $i = $j = 0;
  511. $num = $db->num_rows($resql);
  512. while ($i < $num)
  513. {
  514. $obj = $db->fetch_object($resql);
  515. if ($obj->pamount == $obj->bamount && $obj->socid) // Pour etre sur d'avoir bon cas
  516. {
  517. $row[$j]['paymentid'] = $obj->rowid ; // paymentid
  518. $row[$j]['pamount'] = $obj->pamount;
  519. $row[$j]['fk_bank'] = $obj->fk_bank;
  520. $row[$j]['bamount'] = $obj->bamount;
  521. $row[$j]['socid'] = $obj->socid;
  522. $row[$j]['datec'] = $obj->datec;
  523. $j++;
  524. }
  525. $i++;
  526. }
  527. }
  528. else
  529. {
  530. dol_print_error($db);
  531. }
  532. $nberr=0;
  533. $num=count($row);
  534. if ($num)
  535. {
  536. print $langs->trans('OrphelinsPaymentsDetectedByMethod', 2).': '.count($row)."<br>\n";
  537. $db->begin();
  538. $res = 0;
  539. for ($i = 0; $i < $num; $i++)
  540. {
  541. 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>';
  542. // On cherche facture sans lien paiement et du meme montant et pour meme societe.
  543. $sql=" SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
  544. $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
  545. $sql.=" WHERE f.fk_statut in (2,3) AND fk_soc = ".$row[$i]['socid']." AND total_ttc = ".$row[$i]['pamount'];
  546. $sql.=" AND pf.fk_facture IS NULL";
  547. $sql.=" ORDER BY f.fk_statut";
  548. //print $sql.'<br>';
  549. $resql=$db->query($sql);
  550. if ($resql)
  551. {
  552. $num = $db->num_rows($resql);
  553. //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
  554. if ($num >= 1)
  555. {
  556. $obj=$db->fetch_object($resql);
  557. $facid=$obj->rowid;
  558. $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
  559. $sql.= " VALUES (".$facid.",".$row[$i]['paymentid'].",".$row[$i]['pamount'].")";
  560. $res += $db->query($sql);
  561. print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
  562. }
  563. }
  564. else
  565. {
  566. print 'ERROR';
  567. $nberr++;
  568. }
  569. }
  570. if ($res > 0)
  571. {
  572. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  573. }
  574. else
  575. {
  576. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  577. }
  578. $db->commit();
  579. }
  580. else
  581. {
  582. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  583. }
  584. // Delete obsolete fields fk_facture
  585. $db->begin();
  586. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiement DROP COLUMN fk_facture";
  587. $db->query($sql);
  588. if (!$nberr)
  589. {
  590. $db->commit();
  591. }
  592. else
  593. {
  594. print 'ERROR';
  595. $db->rollback();
  596. }
  597. }
  598. else
  599. {
  600. print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
  601. }
  602. print '</td></tr>';
  603. }
  604. /**
  605. * Mise a jour des contrats (gestion du contrat + detail de contrat)
  606. *
  607. * @param DoliDB $db Database handler
  608. * @param Translate $langs Object langs
  609. * @param Conf $conf Object conf
  610. * @return void
  611. */
  612. function migrate_contracts_det($db,$langs,$conf)
  613. {
  614. print '<tr><td colspan="4">';
  615. $nberr=0;
  616. print '<br>';
  617. print '<b>'.$langs->trans('MigrationContractsUpdate')."</b><br>\n";
  618. $sql = "SELECT c.rowid as cref, c.date_contrat, c.statut, c.mise_en_service, c.fin_validite, c.date_cloture, c.fk_product, c.fk_facture, c.fk_user_author,";
  619. $sql.= " p.ref, p.label, p.description, p.price, p.tva_tx, p.duration, cd.rowid";
  620. $sql.= " FROM ".MAIN_DB_PREFIX."contrat as c";
  621. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p";
  622. $sql.= " ON c.fk_product = p.rowid";
  623. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd";
  624. $sql.= " ON c.rowid=cd.fk_contrat";
  625. $sql.= " WHERE cd.rowid IS NULL AND p.rowid IS NOT NULL";
  626. $resql = $db->query($sql);
  627. dolibarr_install_syslog("upgrade2::migrate_contracts_det sql=".$sql);
  628. if ($resql)
  629. {
  630. $i = 0;
  631. $row = array();
  632. $num = $db->num_rows($resql);
  633. if ($num)
  634. {
  635. print $langs->trans('MigrationContractsNumberToUpdate', $num)."<br>\n";
  636. $db->begin();
  637. while ($i < $num)
  638. {
  639. $obj = $db->fetch_object($resql);
  640. $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet (";
  641. $sql.= "fk_contrat, fk_product, statut, label, description,";
  642. $sql.= "date_ouverture_prevue, date_ouverture, date_fin_validite, tva_tx, qty,";
  643. $sql.= "subprice, price_ht, fk_user_author, fk_user_ouverture)";
  644. $sql.= " VALUES (";
  645. $sql.= $obj->cref.",".($obj->fk_product?$obj->fk_product:0).",";
  646. $sql.= ($obj->mise_en_service?"4":"0").",";
  647. $sql.= "'".$db->escape($obj->label)."', null,";
  648. $sql.= ($obj->mise_en_service?"'".$obj->mise_en_service."'":($obj->date_contrat?"'".$obj->date_contrat."'":"null")).",";
  649. $sql.= ($obj->mise_en_service?"'".$obj->mise_en_service."'":"null").",";
  650. $sql.= ($obj->fin_validite?"'".$obj->fin_validite."'":"null").",";
  651. $sql.= "'".$obj->tva_tx."', 1,";
  652. $sql.= "'".$obj->price."', '".$obj->price."',".$obj->fk_user_author.",";
  653. $sql.= ($obj->mise_en_service?$obj->fk_user_author:"null");
  654. $sql.= ")";
  655. if ($db->query($sql))
  656. {
  657. print $langs->trans('MigrationContractsLineCreation', $obj->cref)."<br>\n";
  658. }
  659. else
  660. {
  661. dol_print_error($db);
  662. $nberr++;
  663. }
  664. $i++;
  665. }
  666. if (! $nberr)
  667. {
  668. // $db->rollback();
  669. $db->commit();
  670. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  671. }
  672. else
  673. {
  674. $db->rollback();
  675. print $langs->trans('MigrationUpdateFailed').'<br>';
  676. }
  677. }
  678. else
  679. {
  680. print $langs->trans('MigrationContractsNothingToUpdate')."<br>\n";
  681. }
  682. }
  683. else
  684. {
  685. print $langs->trans('MigrationContractsFieldDontExist')."<br>\n";
  686. // dol_print_error($db);
  687. }
  688. print '</td></tr>';
  689. }
  690. /**
  691. * Function to migrate links into llx_bank_url
  692. *
  693. * @param DoliDB $db Database handler
  694. * @param Translate $langs Object langs
  695. * @param Conf $conf Object conf
  696. * @return void
  697. */
  698. function migrate_links_transfert($db,$langs,$conf)
  699. {
  700. print '<tr><td colspan="4">';
  701. $nberr=0;
  702. print '<br>';
  703. print '<b>'.$langs->trans('MigrationBankTransfertsUpdate')."</b><br>\n";
  704. $sql = "SELECT ba.rowid as barowid, bb.rowid as bbrowid";
  705. $sql.= " FROM ".MAIN_DB_PREFIX."bank as bb, ".MAIN_DB_PREFIX."bank as ba";
  706. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = ba.rowid";
  707. $sql.= " WHERE ba.amount = -bb.amount AND ba.fk_account <> bb.fk_account";
  708. $sql.= " AND ba.datev = bb.datev AND ba.datec = bb.datec";
  709. $sql.= " AND bu.fk_bank IS NULL";
  710. $resql = $db->query($sql);
  711. dolibarr_install_syslog("upgrade2::migrate_links_transfert sql=".$sql);
  712. if ($resql)
  713. {
  714. $i = 0;
  715. $row = array();
  716. $num = $db->num_rows($resql);
  717. if ($num)
  718. {
  719. print $langs->trans('MigrationBankTransfertsToUpdate', $num)."<br>\n";
  720. $db->begin();
  721. while ($i < $num)
  722. {
  723. $obj = $db->fetch_object($resql);
  724. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
  725. $sql.= "fk_bank, url_id, url, label, type";
  726. $sql.= ")";
  727. $sql.= " VALUES (";
  728. $sql.= $obj->barowid.",".$obj->bbrowid.", '/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert'";
  729. $sql.= ")";
  730. print $sql.'<br>';
  731. dolibarr_install_syslog("migrate_links_transfert sql=".$sql);
  732. if (! $db->query($sql))
  733. {
  734. dol_print_error($db);
  735. $nberr++;
  736. }
  737. $i++;
  738. }
  739. if (! $nberr)
  740. {
  741. // $db->rollback();
  742. $db->commit();
  743. print $langs->trans('MigrationSuccessfullUpdate')."<br>";
  744. }
  745. else
  746. {
  747. $db->rollback();
  748. print $langs->trans('MigrationUpdateFailed').'<br>';
  749. }
  750. }
  751. else {
  752. print $langs->trans('MigrationBankTransfertsNothingToUpdate')."<br>\n";
  753. }
  754. }
  755. else
  756. {
  757. dol_print_error($db);
  758. }
  759. print '</td></tr>';
  760. }
  761. /**
  762. * Mise a jour des date de contrats non renseignees
  763. *
  764. * @param DoliDB $db Database handler
  765. * @param Translate $langs Object langs
  766. * @param Conf $conf Object conf
  767. * @return void
  768. */
  769. function migrate_contracts_date1($db,$langs,$conf)
  770. {
  771. print '<tr><td colspan="4">';
  772. print '<br>';
  773. print '<b>'.$langs->trans('MigrationContractsEmptyDatesUpdate')."</b><br>\n";
  774. $sql="update llx_contrat set date_contrat=tms where date_contrat is null";
  775. dolibarr_install_syslog("upgrade2::migrate_contracts_date1 sql=".$sql);
  776. $resql = $db->query($sql);
  777. if (! $resql) dol_print_error($db);
  778. if ($db->affected_rows($resql) > 0)
  779. print $langs->trans('MigrationContractsEmptyDatesUpdateSuccess')."<br>\n";
  780. else
  781. print $langs->trans('MigrationContractsEmptyDatesNothingToUpdate')."<br>\n";
  782. $sql="update llx_contrat set datec=tms where datec is null";
  783. dolibarr_install_syslog("upgrade2::migrate_contracts_date1 sql=".$sql);
  784. $resql = $db->query($sql);
  785. if (! $resql) dol_print_error($db);
  786. if ($db->affected_rows($resql) > 0)
  787. print $langs->trans('MigrationContractsEmptyCreationDatesUpdateSuccess')."<br>\n";
  788. else
  789. print $langs->trans('MigrationContractsEmptyCreationDatesNothingToUpdate')."<br>\n";
  790. print '</td></tr>';
  791. }
  792. /*
  793. * Mise a jour date contrat avec date min effective mise en service si inferieur
  794. */
  795. function migrate_contracts_date2($db,$langs,$conf)
  796. {
  797. print '<tr><td colspan="4">';
  798. $nberr=0;
  799. print '<br>';
  800. print '<b>'.$langs->trans('MigrationContractsInvalidDatesUpdate')."</b><br>\n";
  801. $sql = "SELECT c.rowid as cref, c.datec, c.date_contrat, MIN(cd.date_ouverture) as datemin";
  802. $sql.= " FROM ".MAIN_DB_PREFIX."contrat as c,";
  803. $sql.= " ".MAIN_DB_PREFIX."contratdet as cd";
  804. $sql.= " WHERE c.rowid=cd.fk_contrat AND cd.date_ouverture IS NOT NULL";
  805. $sql.= " GROUP BY c.rowid, c.date_contrat";
  806. $resql = $db->query($sql);
  807. dolibarr_install_syslog("upgrade2::migrate_contracts_date2 sql=".$sql);
  808. if ($resql)
  809. {
  810. $i = 0;
  811. $row = array();
  812. $num = $db->num_rows($resql);
  813. if ($num)
  814. {
  815. $nbcontratsmodifie=0;
  816. $db->begin();
  817. while ($i < $num)
  818. {
  819. $obj = $db->fetch_object($resql);
  820. if ($obj->date_contrat > $obj->datemin)
  821. {
  822. print $langs->trans('MigrationContractsInvalidDateFix', $obj->cref, $obj->date_contrat, $obj->datemin)."<br>\n";
  823. $sql ="UPDATE ".MAIN_DB_PREFIX."contrat";
  824. $sql.=" SET date_contrat='".$obj->datemin."'";
  825. $sql.=" WHERE rowid=".$obj->cref;
  826. $resql2=$db->query($sql);
  827. if (! $resql2) dol_print_error($db);
  828. $nbcontratsmodifie++;
  829. }
  830. $i++;
  831. }
  832. $db->commit();
  833. if ($nbcontratsmodifie)
  834. print $langs->trans('MigrationContractsInvalidDatesNumber', $nbcontratsmodifie)."<br>\n";
  835. else
  836. print $langs->trans('MigrationContractsInvalidDatesNothingToUpdate')."<br>\n";
  837. }
  838. }
  839. else
  840. {
  841. dol_print_error($db);
  842. }
  843. print '</td></tr>';
  844. }
  845. /**
  846. * Mise a jour des dates de creation de contrat
  847. *
  848. * @param DoliDB $db Database handler
  849. * @param Translate $langs Object langs
  850. * @param Conf $conf Object conf
  851. * @return void
  852. */
  853. function migrate_contracts_date3($db,$langs,$conf)
  854. {
  855. print '<tr><td colspan="4">';
  856. print '<br>';
  857. print '<b>'.$langs->trans('MigrationContractsIncoherentCreationDateUpdate')."</b><br>\n";
  858. $sql="update llx_contrat set datec=date_contrat where datec is null or datec > date_contrat";
  859. dolibarr_install_syslog("upgrade2::migrate_contracts_date3 sql=".$sql);
  860. $resql = $db->query($sql);
  861. if (! $resql) dol_print_error($db);
  862. if ($db->affected_rows($resql) > 0)
  863. print $langs->trans('MigrationContractsIncoherentCreationDateUpdateSuccess')."<br>\n";
  864. else
  865. print $langs->trans('MigrationContractsIncoherentCreationDateNothingToUpdate')."<br>\n";
  866. print '</td></tr>';
  867. }
  868. /**
  869. * Reouverture des contrats qui ont au moins une ligne non fermee
  870. *
  871. * @param DoliDB $db Database handler
  872. * @param Translate $langs Object langs
  873. * @param Conf $conf Object conf
  874. * @return void
  875. */
  876. function migrate_contracts_open($db,$langs,$conf)
  877. {
  878. print '<tr><td colspan="4">';
  879. print '<br>';
  880. print '<b>'.$langs->trans('MigrationReopeningContracts')."</b><br>\n";
  881. $sql = "SELECT c.rowid as cref FROM llx_contrat as c, llx_contratdet as cd";
  882. $sql.= " WHERE cd.statut = 4 AND c.statut=2 AND c.rowid=cd.fk_contrat";
  883. dolibarr_install_syslog("upgrade2::migrate_contracts_open sql=".$sql);
  884. $resql = $db->query($sql);
  885. if (! $resql) dol_print_error($db);
  886. if ($db->affected_rows($resql) > 0) {
  887. $i = 0;
  888. $row = array();
  889. $num = $db->num_rows($resql);
  890. if ($num)
  891. {
  892. $nbcontratsmodifie=0;
  893. $db->begin();
  894. while ($i < $num)
  895. {
  896. $obj = $db->fetch_object($resql);
  897. print $langs->trans('MigrationReopenThisContract', $obj->cref)."<br>\n";
  898. $sql ="UPDATE ".MAIN_DB_PREFIX."contrat";
  899. $sql.=" SET statut=1";
  900. $sql.=" WHERE rowid=".$obj->cref;
  901. $resql2=$db->query($sql);
  902. if (! $resql2) dol_print_error($db);
  903. $nbcontratsmodifie++;
  904. $i++;
  905. }
  906. $db->commit();
  907. if ($nbcontratsmodifie)
  908. print $langs->trans('MigrationReopenedContractsNumber', $nbcontratsmodifie)."<br>\n";
  909. else
  910. print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
  911. }
  912. }
  913. else print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
  914. print '</td></tr>';
  915. }
  916. /**
  917. * Factures fournisseurs
  918. *
  919. * @param DoliDB $db Database handler
  920. * @param Translate $langs Object langs
  921. * @param Conf $conf Object conf
  922. * @return void
  923. */
  924. function migrate_paiementfourn_facturefourn($db,$langs,$conf)
  925. {
  926. global $bc;
  927. print '<tr><td colspan="4">';
  928. print '<br>';
  929. print '<b>'.$langs->trans('SuppliersInvoices')."</b><br>\n";
  930. print '</td></tr>';
  931. $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiementfourn","fk_facture_fourn");
  932. $obj = $db->fetch_object($result);
  933. if ($obj)
  934. {
  935. $error=0;
  936. $nb=0;
  937. $select_sql = 'SELECT rowid, fk_facture_fourn, amount';
  938. $select_sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn';
  939. $select_sql.= ' WHERE fk_facture_fourn IS NOT NULL';
  940. dolibarr_install_syslog("upgrade2::migrate_paiementfourn_facturefourn sql=".$select_sql);
  941. $select_resql = $db->query($select_sql);
  942. if ($select_resql)
  943. {
  944. $select_num = $db->num_rows($select_resql);
  945. $i=0;
  946. $var = true;
  947. // Pour chaque paiement fournisseur, on insere une ligne dans paiementfourn_facturefourn
  948. while (($i < $select_num) && (! $error))
  949. {
  950. $var = !$var;
  951. $select_obj = $db->fetch_object($select_resql);
  952. // Verifier si la ligne est deja dans la nouvelle table. On ne veut pas inserer de doublons.
  953. $check_sql = 'SELECT fk_paiementfourn, fk_facturefourn';
  954. $check_sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn';
  955. $check_sql.= ' WHERE fk_paiementfourn = '.$select_obj->rowid.' AND fk_facturefourn = '.$select_obj->fk_facture_fourn;
  956. $check_resql = $db->query($check_sql);
  957. if ($check_resql)
  958. {
  959. $check_num = $db->num_rows($check_resql);
  960. if ($check_num == 0)
  961. {
  962. $db->begin();
  963. if ($nb == 0)
  964. {
  965. print '<tr><td colspan="4" class="nowrap"><b>'.$langs->trans('SuppliersInvoices').'</b></td></tr>';
  966. print '<tr><td>fk_paiementfourn</td><td>fk_facturefourn</td><td>'.$langs->trans('Amount').'</td><td>&nbsp;</td></tr>';
  967. }
  968. print '<tr '.$bc[$var].'>';
  969. print '<td>'.$select_obj->rowid.'</td><td>'.$select_obj->fk_facture_fourn.'</td><td>'.$select_obj->amount.'</td>';
  970. $insert_sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn SET ';
  971. $insert_sql.= ' fk_paiementfourn = \''.$select_obj->rowid.'\',';
  972. $insert_sql.= ' fk_facturefourn = \''.$select_obj->fk_facture_fourn.'\',';
  973. $insert_sql.= ' amount = \''.$select_obj->amount.'\'';
  974. $insert_resql = $db->query($insert_sql);
  975. if ($insert_resql)
  976. {
  977. $nb++;
  978. print '<td><span style="color:green">'.$langs->trans("OK").'</span></td>';
  979. }
  980. else
  981. {
  982. print '<td><span style="color:red">Error on insert</span></td>';
  983. $error++;
  984. }
  985. print '</tr>';
  986. }
  987. }
  988. else
  989. {
  990. $error++;
  991. }
  992. $i++;
  993. }
  994. }
  995. else
  996. {
  997. $error++;
  998. }
  999. if (!$error)
  1000. {
  1001. if (!$nb)
  1002. {
  1003. print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
  1004. }
  1005. $db->commit();
  1006. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiementfourn DROP COLUMN fk_facture_fourn";
  1007. $db->query($sql);
  1008. }
  1009. else
  1010. {
  1011. print '<tr><td>'.$langs->trans("Error").'</td></tr>';
  1012. $db->rollback();
  1013. }
  1014. }
  1015. else
  1016. {
  1017. print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
  1018. }
  1019. }
  1020. /**
  1021. * Mise a jour des totaux lignes de facture
  1022. *
  1023. * @param DoliDB $db Database handler
  1024. * @param Translate $langs Object langs
  1025. * @param Conf $conf Object conf
  1026. * @return void
  1027. */
  1028. function migrate_price_facture($db,$langs,$conf)
  1029. {
  1030. $err=0;
  1031. $db->begin();
  1032. print '<tr><td colspan="4">';
  1033. print '<br>';
  1034. print '<b>'.$langs->trans('MigrationInvoice')."</b><br>\n";
  1035. // Liste des lignes facture non a jour
  1036. $sql = "SELECT fd.rowid, fd.qty, fd.subprice, fd.remise_percent, fd.tva_tx as tva_taux, fd.total_ttc, fd.info_bits,";
  1037. $sql.= " f.rowid as facid, f.remise_percent as remise_percent_global, f.total_ttc as total_ttc_f";
  1038. $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as fd, ".MAIN_DB_PREFIX."facture as f";
  1039. $sql.= " WHERE fd.fk_facture = f.rowid";
  1040. $sql.= " AND (((fd.total_ttc = 0 AND fd.remise_percent != 100) or fd.total_ttc IS NULL) or f.total_ttc IS NULL)";
  1041. //print $sql;
  1042. dolibarr_install_syslog("upgrade2::migrate_price_facture sql=".$sql);
  1043. $resql=$db->query($sql);
  1044. if ($resql)
  1045. {
  1046. $num = $db->num_rows($resql);
  1047. $i = 0;
  1048. if ($num)
  1049. {
  1050. while ($i < $num)
  1051. {
  1052. $obj = $db->fetch_object($resql);
  1053. $rowid = $obj->rowid;
  1054. $qty = $obj->qty;
  1055. $pu = $obj->subprice;
  1056. $txtva = $obj->tva_taux;
  1057. $remise_percent = $obj->remise_percent;
  1058. $remise_percent_global = $obj->remise_percent_global;
  1059. $total_ttc_f = $obj->total_ttc_f;
  1060. $info_bits = $obj->info_bits;
  1061. // On met a jour les 3 nouveaux champs
  1062. $facligne= new FactureLigne($db);
  1063. $facligne->fetch($rowid);
  1064. $result=calcul_price_total($qty,$pu,$remise_percent,$txtva, 0, 0,$remise_percent_global,'HT',$info_bits,0);
  1065. $total_ht = $result[0];
  1066. $total_tva = $result[1];
  1067. $total_ttc = $result[2];
  1068. $facligne->total_ht = $total_ht;
  1069. $facligne->total_tva = $total_tva;
  1070. $facligne->total_ttc = $total_ttc;
  1071. dolibarr_install_syslog("upgrade2: Line $rowid: facid=$obj->facid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent remise_global=$remise_percent_global -> $total_ht, $total_tva, $total_ttc");
  1072. print ". ";
  1073. $facligne->update_total();
  1074. /* On touche a facture mere uniquement si total_ttc = 0 */
  1075. if (! $total_ttc_f)
  1076. {
  1077. $facture = new Facture($db);
  1078. $facture->id=$obj->facid;
  1079. if ( $facture->fetch($facture->id) >= 0)
  1080. {
  1081. if ( $facture->update_price() > 0 )
  1082. {
  1083. //print $facture->id;
  1084. }
  1085. else
  1086. {
  1087. print "Error id=".$facture->id;
  1088. $err++;
  1089. }
  1090. }
  1091. else
  1092. {
  1093. print "Error #3";
  1094. $err++;
  1095. }
  1096. }
  1097. print " ";
  1098. $i++;
  1099. }
  1100. }
  1101. else
  1102. {
  1103. print $langs->trans("AlreadyDone");
  1104. }
  1105. $db->free($resql);
  1106. $db->commit();
  1107. }
  1108. else
  1109. {
  1110. print "Error #1 ".$db->error();
  1111. $err++;
  1112. $db->rollback();
  1113. }
  1114. print '<br>';
  1115. print '</td></tr>';
  1116. }
  1117. /**
  1118. * Mise a jour des totaux lignes de propal
  1119. *
  1120. * @param DoliDB $db Database handler
  1121. * @param Translate $langs Object langs
  1122. * @param Conf $conf Object conf
  1123. * @return void
  1124. */
  1125. function migrate_price_propal($db,$langs,$conf)
  1126. {
  1127. $db->begin();
  1128. print '<tr><td colspan="4">';
  1129. print '<br>';
  1130. print '<b>'.$langs->trans('MigrationProposal')."</b><br>\n";
  1131. // Liste des lignes propal non a jour
  1132. $sql = "SELECT pd.rowid, pd.qty, pd.subprice, pd.remise_percent, pd.tva_tx as tva_taux, pd.info_bits,";
  1133. $sql.= " p.rowid as propalid, p.remise_percent as remise_percent_global";
  1134. $sql.= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."propal as p";
  1135. $sql.= " WHERE pd.fk_propal = p.rowid";
  1136. $sql.= " AND ((pd.total_ttc = 0 AND pd.remise_percent != 100) or pd.total_ttc IS NULL)";
  1137. dolibarr_install_syslog("upgrade2::migrate_price_propal sql=".$sql);
  1138. $resql=$db->query($sql);
  1139. if ($resql)
  1140. {
  1141. $num = $db->num_rows($resql);
  1142. $i = 0;
  1143. if ($num)
  1144. {
  1145. while ($i < $num)
  1146. {
  1147. $obj = $db->fetch_object($resql);
  1148. $rowid = $obj->rowid;
  1149. $qty = $obj->qty;
  1150. $pu = $obj->subprice;
  1151. $txtva = $obj->tva_taux;
  1152. $remise_percent = $obj->remise_percent;
  1153. $remise_percent_global = $obj->remise_percent_global;
  1154. $info_bits = $obj->info_bits;
  1155. // On met a jour les 3 nouveaux champs
  1156. $propalligne= new PropaleLigne($db);
  1157. $propalligne->fetch($rowid);
  1158. $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits,0);
  1159. $total_ht = $result[0];
  1160. $total_tva = $result[1];
  1161. $total_ttc = $result[2];
  1162. $propalligne->total_ht = $total_ht;
  1163. $propalligne->total_tva = $total_tva;
  1164. $propalligne->total_ttc = $total_ttc;
  1165. dolibarr_install_syslog("upgrade2: Line $rowid: propalid=$obj->rowid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent remise_global=$remise_percent_global -> $total_ht, $total_tva, $total_ttc");
  1166. print ". ";
  1167. $propalligne->update_total($rowid);
  1168. /* On touche pas a propal mere
  1169. $propal = new Propal($db);
  1170. $propal->id=$obj->rowid;
  1171. if ( $propal->fetch($propal->id) >= 0 )
  1172. {
  1173. if ( $propal->update_price() > 0 )
  1174. {
  1175. print ". ";
  1176. }
  1177. else
  1178. {
  1179. print "Error id=".$propal->id;
  1180. }
  1181. }
  1182. else
  1183. {
  1184. print "Error #3";
  1185. }
  1186. */
  1187. $i++;
  1188. }
  1189. }
  1190. else
  1191. {
  1192. print $langs->trans("AlreadyDone");
  1193. }
  1194. $db->free($resql);
  1195. $db->commit();
  1196. }
  1197. else
  1198. {
  1199. print "Error #1 ".$db->error();
  1200. $db->rollback();
  1201. }
  1202. print '<br>';
  1203. print '</td></tr>';
  1204. }
  1205. /**
  1206. * Update total of contract lines
  1207. *
  1208. * @param DoliDB $db Database handler
  1209. * @param Translate $langs Object langs
  1210. * @param Conf $conf Object conf
  1211. * @return void
  1212. */
  1213. function migrate_price_contrat($db,$langs,$conf)
  1214. {
  1215. $db->begin();
  1216. $tmpmysoc=new Societe($db);
  1217. $tmpmysoc->setMysoc($conf);
  1218. if (empty($tmpmysoc->country_id)) $tmpmysoc->country_id=0; // Ti not have this set to '' or will make sql syntax error.
  1219. print '<tr><td colspan="4">';
  1220. print '<br>';
  1221. print '<b>'.$langs->trans('MigrationContract')."</b><br>\n";
  1222. // Liste des lignes contrat non a jour
  1223. $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as tva_taux, cd.info_bits,";
  1224. $sql.= " c.rowid as contratid";
  1225. $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c";
  1226. $sql.= " WHERE cd.fk_contrat = c.rowid";
  1227. $sql.= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100 AND cd.subprice > 0) or cd.total_ttc IS NULL)";
  1228. dolibarr_install_syslog("upgrade2::migrate_price_contrat sql=".$sql);
  1229. $resql=$db->query($sql);
  1230. if ($resql)
  1231. {
  1232. $num = $db->num_rows($resql);
  1233. $i = 0;
  1234. if ($num)
  1235. {
  1236. while ($i < $num)
  1237. {
  1238. $obj = $db->fetch_object($resql);
  1239. $rowid = $obj->rowid;
  1240. $qty = $obj->qty;
  1241. $pu = $obj->subprice;
  1242. $txtva = $obj->tva_taux;
  1243. $remise_percent = $obj->remise_percent;
  1244. $info_bits = $obj->info_bits;
  1245. // On met a jour les 3 nouveaux champs
  1246. $contratligne= new ContratLigne($db);
  1247. //$contratligne->fetch($rowid); Non requis car le update_total ne met a jour que chp redefinis
  1248. $contratligne->rowid=$rowid;
  1249. $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,0,'HT',$info_bits,0,$tmpmysoc);
  1250. $total_ht = $result[0];
  1251. $total_tva = $result[1];
  1252. $total_ttc = $result[2];
  1253. $contratligne->total_ht = $total_ht;
  1254. $contratligne->total_tva = $total_tva;
  1255. $contratligne->total_ttc = $total_ttc;
  1256. dolibarr_install_syslog("upgrade2: Line $rowid: contratdetid=$obj->rowid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent -> $total_ht, $total_tva, $total_ttc");
  1257. print ". ";
  1258. $contratligne->update_total($rowid);
  1259. $i++;
  1260. }
  1261. }
  1262. else
  1263. {
  1264. print $langs->trans("AlreadyDone");
  1265. }
  1266. $db->free($resql);
  1267. $db->commit();
  1268. }
  1269. else
  1270. {
  1271. print "Error #1 ".$db->error();
  1272. $db->rollback();
  1273. }
  1274. print '<br>';
  1275. print '</td></tr>';
  1276. }
  1277. /**
  1278. * Mise a jour des totaux lignes de commande
  1279. *
  1280. * @param DoliDB $db Database handler
  1281. * @param Translate $langs Object langs
  1282. * @param Conf $conf Object conf
  1283. * @return void
  1284. */
  1285. function migrate_price_commande($db,$langs,$conf)
  1286. {
  1287. $db->begin();
  1288. print '<tr><td colspan="4">';
  1289. print '<br>';
  1290. print '<b>'.$langs->trans('MigrationOrder')."</b><br>\n";
  1291. // Liste des lignes commande non a jour
  1292. $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as tva_taux, cd.info_bits,";
  1293. $sql.= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
  1294. $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."commande as c";
  1295. $sql.= " WHERE cd.fk_commande = c.rowid";
  1296. $sql.= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
  1297. dolibarr_install_syslog("upgrade2::migrate_price_commande sql=".$sql);
  1298. $resql=$db->query($sql);
  1299. if ($resql)
  1300. {
  1301. $num = $db->num_rows($resql);
  1302. $i = 0;
  1303. if ($num)
  1304. {
  1305. while ($i < $num)
  1306. {
  1307. $obj = $db->fetch_object($resql);
  1308. $rowid = $obj->rowid;
  1309. $qty = $obj->qty;
  1310. $pu = $obj->subprice;
  1311. $txtva = $obj->tva_taux;
  1312. $remise_percent = $obj->remise_percent;
  1313. $remise_percent_global = $obj->remise_percent_global;
  1314. $info_bits = $obj->info_bits;
  1315. // On met a jour les 3 nouveaux champs
  1316. $commandeligne= new OrderLine($db);
  1317. $commandeligne->fetch($rowid);
  1318. $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits,0);
  1319. $total_ht = $result[0];
  1320. $total_tva = $result[1];
  1321. $total_ttc = $result[2];
  1322. $commandeligne->total_ht = $total_ht;
  1323. $commandeligne->total_tva = $total_tva;
  1324. $commandeligne->total_ttc = $total_ttc;
  1325. dolibarr_install_syslog("upgrade2: Line $rowid: commandeid=$obj->rowid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent remise_global=$remise_percent_global -> $total_ht, $total_tva, $total_ttc");
  1326. print ". ";
  1327. $commandeligne->update_total($rowid);
  1328. /* On touche pas a facture mere
  1329. $commande = new Commande($db);
  1330. $commande->id = $obj->rowid;
  1331. if ( $commande->fetch($commande->id) >= 0 )
  1332. {
  1333. if ( $commande->update_price() > 0 )
  1334. {
  1335. print ". ";
  1336. }
  1337. else
  1338. {
  1339. print "Error id=".$commande->id;
  1340. }
  1341. }
  1342. else
  1343. {
  1344. print "Error #3";
  1345. }
  1346. */
  1347. $i++;
  1348. }
  1349. }
  1350. else
  1351. {
  1352. print $langs->trans("AlreadyDone");
  1353. }
  1354. $db->free($resql);
  1355. /*
  1356. $sql = "DELETE FROM ".MAIN_DB_PREFIX."commandedet";
  1357. $sql.= " WHERE price = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0 AND remise_percent = 0";
  1358. $resql=$db->query($sql);
  1359. if (! $resql)
  1360. {
  1361. dol_print_error($db);
  1362. }
  1363. */
  1364. $db->commit();
  1365. }
  1366. else
  1367. {
  1368. print "Error #1 ".$db->error();
  1369. $db->rollback();
  1370. }
  1371. print '<br>';
  1372. print '</td></tr>';
  1373. }
  1374. /**
  1375. * Mise a jour des totaux lignes de commande fournisseur
  1376. *
  1377. * @param DoliDB $db Database handler
  1378. * @param Translate $langs Object langs
  1379. * @param Conf $conf Object conf
  1380. * @return void
  1381. */
  1382. function migrate_price_commande_fournisseur($db,$langs,$conf)
  1383. {
  1384. $db->begin();
  1385. print '<tr><td colspan="4">';
  1386. print '<br>';
  1387. print '<b>'.$langs->trans('MigrationSupplierOrder')."</b><br>\n";
  1388. // Liste des lignes commande non a jour
  1389. $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as tva_taux, cd.info_bits,";
  1390. $sql.= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
  1391. $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd, ".MAIN_DB_PREFIX."commande_fournisseur as c";
  1392. $sql.= " WHERE cd.fk_commande = c.rowid";
  1393. $sql.= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
  1394. dolibarr_install_syslog("upgrade2::migrate_price_commande_fournisseur sql=".$sql);
  1395. $resql=$db->query($sql);
  1396. if ($resql)
  1397. {
  1398. $num = $db->num_rows($resql);
  1399. $i = 0;
  1400. if ($num)
  1401. {
  1402. while ($i < $num)
  1403. {
  1404. $obj = $db->fetch_object($resql);
  1405. $rowid = $obj->rowid;
  1406. $qty = $obj->qty;
  1407. $pu = $obj->subprice;
  1408. $txtva = $obj->tva_taux;
  1409. $remise_percent = $obj->remise_percent;
  1410. $remise_percent_global = $obj->remise_percent_global;
  1411. $info_bits = $obj->info_bits;
  1412. // On met a jour les 3 nouveaux champs
  1413. $commandeligne= new CommandeFournisseurLigne($db);
  1414. $commandeligne->fetch($rowid);
  1415. $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits,0);
  1416. $total_ht = $result[0];
  1417. $total_tva = $result[1];
  1418. $total_ttc = $result[2];
  1419. $commandeligne->total_ht = $total_ht;
  1420. $commandeligne->total_tva = $total_tva;
  1421. $commandeligne->total_ttc = $total_ttc;
  1422. dolibarr_install_syslog("upgrade2: Line $rowid: commandeid=$obj->rowid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent remise_global=$remise_percent_global -> $total_ht, $total_tva, $total_ttc");
  1423. print ". ";
  1424. $commandeligne->update_total($rowid);
  1425. /* On touche pas a facture mere
  1426. $commande = new Commande($db);
  1427. $commande->id = $obj->rowid;
  1428. if ( $commande->fetch($commande->id) >= 0 )
  1429. {
  1430. if ( $commande->update_price() > 0 )
  1431. {
  1432. print ". ";
  1433. }
  1434. else
  1435. {
  1436. print "Error id=".$commande->id;
  1437. }
  1438. }
  1439. else
  1440. {
  1441. print "Error #3";
  1442. }
  1443. */
  1444. $i++;
  1445. }
  1446. }
  1447. else
  1448. {
  1449. print $langs->trans("AlreadyDone");
  1450. }
  1451. $db->free($resql);
  1452. /*
  1453. $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet";
  1454. $sql.= " WHERE subprice = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0";
  1455. $resql=$db->query($sql);
  1456. if (! $resql)
  1457. {
  1458. dol_print_error($db);
  1459. }
  1460. */
  1461. $db->commit();
  1462. }
  1463. else
  1464. {
  1465. print "Error #1 ".$db->error();
  1466. $db->rollback();
  1467. }
  1468. print '<br>';
  1469. print '</td></tr>';
  1470. }
  1471. /**
  1472. * Mise a jour des modeles selectionnes
  1473. *
  1474. * @param DoliDB $db Database handler
  1475. * @param Translate $langs Object langs
  1476. * @param Conf $conf Object conf
  1477. * @return void
  1478. */
  1479. function migrate_modeles($db,$langs,$conf)
  1480. {
  1481. //print '<br>';
  1482. //print '<b>'.$langs->trans('UpdateModelsTable')."</b><br>\n";
  1483. dolibarr_install_syslog("upgrade2::migrate_modeles");
  1484. if (! empty($conf->facture->enabled))
  1485. {
  1486. include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  1487. $modellist=ModelePDFFactures::liste_modeles($db);
  1488. if (count($modellist)==0)
  1489. {
  1490. // Aucun model par defaut.
  1491. $sql=" insert into llx_document_model(nom,type) values('crabe','invoice')";
  1492. $resql = $db->query($sql);
  1493. if (! $resql) dol_print_error($db);
  1494. }
  1495. }
  1496. if (! empty($conf->commande->enabled))
  1497. {
  1498. include_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
  1499. $modellist=ModelePDFCommandes::liste_modeles($db);
  1500. if (count($modellist)==0)
  1501. {
  1502. // Aucun model par defaut.
  1503. $sql=" insert into llx_document_model(nom,type) values('einstein','order')";
  1504. $resql = $db->query($sql);
  1505. if (! $resql) dol_print_error($db);
  1506. }
  1507. }
  1508. if (! empty($conf->expedition->enabled))
  1509. {
  1510. include_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
  1511. $modellist=ModelePDFExpedition::liste_modeles($db);
  1512. if (count($modellist)==0)
  1513. {
  1514. // Aucun model par defaut.
  1515. $sql=" insert into llx_document_model(nom,type) values('rouget','shipping')";
  1516. $resql = $db->query($sql);
  1517. if (! $resql) dol_print_error($db);
  1518. }
  1519. }
  1520. //print $langs->trans("AlreadyDone");
  1521. }
  1522. /**
  1523. * Correspondance des expeditions et des commandes clients dans la table llx_co_exp
  1524. *
  1525. * @param DoliDB $db Database handler
  1526. * @param Translate $langs Object langs
  1527. * @param Conf $conf Object conf
  1528. * @return void
  1529. */
  1530. function migrate_commande_expedition($db,$langs,$conf)
  1531. {
  1532. dolibarr_install_syslog("upgrade2::migrate_commande_expedition");
  1533. print '<tr><td colspan="4">';
  1534. print '<br>';
  1535. print '<b>'.$langs->trans('MigrationShipmentOrderMatching')."</b><br>\n";
  1536. $result = $db->DDLDescTable(MAIN_DB_PREFIX."expedition","fk_commande");
  1537. $obj = $db->fetch_object($result);
  1538. if ($obj)
  1539. {
  1540. $error = 0;
  1541. $db->begin();
  1542. $sql = "SELECT e.rowid, e.fk_commande FROM ".MAIN_DB_PREFIX."expedition as e";
  1543. $resql = $db->query($sql);
  1544. if ($resql)
  1545. {
  1546. $i = 0;
  1547. $num = $db->num_rows($resql);
  1548. if ($num)
  1549. {
  1550. while ($i < $num)
  1551. {
  1552. $obj = $db->fetch_object($resql);
  1553. $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_exp (fk_expedition,fk_commande)";
  1554. $sql.= " VALUES (".$obj->rowid.",".$obj->fk_commande.")";
  1555. $resql2=$db->query($sql);
  1556. if (!$resql2)
  1557. {
  1558. $error++;
  1559. dol_print_error($db);
  1560. }
  1561. print ". ";
  1562. $i++;
  1563. }
  1564. }
  1565. if ($error == 0)
  1566. {
  1567. $db->commit();
  1568. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."expedition DROP COLUMN fk_commande";
  1569. print $langs->trans('FieldRenamed')."<br>\n";
  1570. $db->query($sql);
  1571. }
  1572. else
  1573. {
  1574. $db->rollback();
  1575. }
  1576. }
  1577. else
  1578. {
  1579. dol_print_error($db);
  1580. $db->rollback();
  1581. }
  1582. }
  1583. else
  1584. {
  1585. print $langs->trans('AlreadyDone')."<br>\n";
  1586. }
  1587. print '</td></tr>';
  1588. }
  1589. /**
  1590. * Correspondance des livraisons et des commandes clients dans la table llx_co_liv
  1591. *
  1592. * @param DoliDB $db Database handler
  1593. * @param Translate $langs Object langs
  1594. * @param Conf $conf Object conf
  1595. * @return void
  1596. */
  1597. function migrate_commande_livraison($db,$langs,$conf)
  1598. {
  1599. dolibarr_install_syslog("upgrade2::migrate_commande_livraison");
  1600. print '<tr><td colspan="4">';
  1601. print '<br>';
  1602. print '<b>'.$langs->trans('MigrationDeliveryOrderMatching')."</b><br>\n";
  1603. $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison","fk_commande");
  1604. $obj = $db->fetch_object($result);
  1605. if ($obj)
  1606. {
  1607. $error = 0;
  1608. $db->begin();
  1609. $sql = "SELECT l.rowid, l.fk_commande";
  1610. $sql.= ", c.ref_client, c.date_livraison";
  1611. $sql.= " FROM ".MAIN_DB_PREFIX."livraison as l, ".MAIN_DB_PREFIX."commande as c";
  1612. $sql.= " WHERE c.rowid = l.fk_commande";
  1613. $resql = $db->query($sql);
  1614. if ($resql)
  1615. {
  1616. $i = 0;
  1617. $num = $db->num_rows($resql);
  1618. if ($num)
  1619. {
  1620. while ($i < $num)
  1621. {
  1622. $obj = $db->fetch_object($resql);
  1623. $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_liv (fk_livraison,fk_commande)";
  1624. $sql.= " VALUES (".$obj->rowid.",".$obj->fk_commande.")";
  1625. $resql2=$db->query($sql);
  1626. if ($resql2)
  1627. {
  1628. $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
  1629. $sqlu.= " ref_client='".$obj->ref_client."'";
  1630. $sqlu.= ", date_livraison='".$obj->date_livraison."'";
  1631. $sqlu.= " WHERE rowid = ".$obj->rowid;
  1632. $resql3=$db->query($sqlu);
  1633. if (!$resql3)
  1634. {
  1635. $error++;
  1636. dol_print_error($db);
  1637. }
  1638. }
  1639. else
  1640. {
  1641. $error++;
  1642. dol_print_error($db);
  1643. }
  1644. print ". ";
  1645. $i++;
  1646. }
  1647. }
  1648. if ($error == 0)
  1649. {
  1650. $db->commit();
  1651. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_commande";
  1652. print $langs->trans('FieldRenamed')."<br>\n";
  1653. $db->query($sql);
  1654. }
  1655. else
  1656. {
  1657. $db->rollback();
  1658. }
  1659. }
  1660. else
  1661. {
  1662. dol_print_error($db);
  1663. $db->rollback();
  1664. }
  1665. }
  1666. else
  1667. {
  1668. print $langs->trans('AlreadyDone')."<br>\n";
  1669. }
  1670. print '</td></tr>';
  1671. }
  1672. /**
  1673. * Migration des details commandes dans les details livraisons
  1674. *
  1675. * @param DoliDB $db Database handler
  1676. * @param Translate $langs Object langs
  1677. * @param Conf $conf Object conf
  1678. * @return void
  1679. */
  1680. function migrate_detail_livraison($db,$langs,$conf)
  1681. {
  1682. dolibarr_install_syslog("upgrade2::migrate_detail_livraison");
  1683. print '<tr><td colspan="4">';
  1684. print '<br>';
  1685. print '<b>'.$langs->trans('MigrationDeliveryDetail')."</b><br>\n";
  1686. // This is done if field fk_commande_ligne exists.
  1687. // If not this means migration was already done.
  1688. $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet","fk_commande_ligne");
  1689. $obj = $db->fetch_object($result);
  1690. if ($obj)
  1691. {
  1692. $error = 0;
  1693. $db->begin();
  1694. $sql = "SELECT cd.rowid, cd.fk_product, cd.description, cd.subprice, cd.total_ht";
  1695. $sql.= ", ld.fk_livraison";
  1696. $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."livraisondet as ld";
  1697. $sql.= " WHERE ld.fk_commande_ligne = cd.rowid";
  1698. $resql = $db->query($sql);
  1699. if ($resql)
  1700. {
  1701. $i = 0;
  1702. $num = $db->num_rows($resql);
  1703. if ($num)
  1704. {
  1705. while ($i < $num)
  1706. {
  1707. $obj = $db->fetch_object($resql);
  1708. $sql = "UPDATE ".MAIN_DB_PREFIX."livraisondet SET";
  1709. $sql.= " fk_product=".$obj->fk_product;
  1710. $sql.= ",description='".$db->escape($obj->description)."'";
  1711. $sql.= ",subprice='".$obj->subprice."'";
  1712. $sql.= ",total_ht='".$obj->total_ht."'";
  1713. $sql.= " WHERE fk_commande_ligne = ".$obj->rowid;
  1714. $resql2=$db->query($sql);
  1715. if ($resql2)
  1716. {
  1717. $sql = "SELECT total_ht";
  1718. $sql.= " FROM ".MAIN_DB_PREFIX."livraison";
  1719. $sql.= " WHERE rowid = ".$obj->fk_livraison;
  1720. $resql3=$db->query($sql);
  1721. if ($resql3)
  1722. {
  1723. $obju = $db->fetch_object($resql3);
  1724. $total_ht = $obju->total_ht + $obj->total_ht;
  1725. $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
  1726. $sqlu.= " total_ht='".$total_ht."'";
  1727. $sqlu.= " WHERE rowid=".$obj->fk_livraison;
  1728. $resql4=$db->query($sqlu);
  1729. if (!$resql4)
  1730. {
  1731. $error++;
  1732. dol_print_error($db);
  1733. }
  1734. }
  1735. else
  1736. {
  1737. $error++;
  1738. dol_print_error($db);
  1739. }
  1740. }
  1741. else
  1742. {
  1743. $error++;
  1744. dol_print_error($db);
  1745. }
  1746. print ". ";
  1747. $i++;
  1748. }
  1749. }
  1750. if ($error == 0)
  1751. {
  1752. $db->commit();
  1753. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet CHANGE fk_commande_ligne fk_origin_line integer";
  1754. print $langs->trans('FieldRenamed')."<br>\n";
  1755. $db->query($sql);
  1756. }
  1757. else
  1758. {
  1759. $db->rollback();
  1760. }
  1761. }
  1762. else
  1763. {
  1764. dol_print_error($db);
  1765. $db->rollback();
  1766. }
  1767. }
  1768. else
  1769. {
  1770. $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet","fk_origin_line");
  1771. $obj = $db->fetch_object($result);
  1772. if (!$obj)
  1773. {
  1774. $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet ADD COLUMN fk_origin_line integer after fk_livraison";
  1775. $db->query($sql);
  1776. }
  1777. print $langs->trans('AlreadyDone')."<br>\n";
  1778. }
  1779. print '</td></tr>';
  1780. }
  1781. /**
  1782. * Migration du champ stock dans produits
  1783. *
  1784. * @param DoliDB $db Database handler
  1785. * @param Translate $langs Object langs
  1786. * @param Conf $conf Object conf
  1787. * @return void
  1788. */
  1789. function migrate_stocks($db,$langs,$conf)
  1790. {
  1791. dolibarr_install_syslog("upgrade2::migrate_stocks");
  1792. print '<tr><td colspan="4">';
  1793. print '<br>';
  1794. print '<b>'.$langs->trans('MigrationStockDetail')."</b><br>\n";
  1795. $error = 0;
  1796. $db->begin();
  1797. $sql = "SELECT SUM(reel) as total, fk_product";
  1798. $sql.= " FROM ".MAIN_DB_PREFIX."product_stock as ps";
  1799. $sql.= " GROUP BY fk_product";
  1800. $resql = $db->query($sql);
  1801. if ($resql)
  1802. {
  1803. $i = 0;
  1804. $num = $db->num_rows($resql);
  1805. if ($num)
  1806. {
  1807. while ($i < $num)
  1808. {
  1809. $obj = $db->fetch_object($resql);
  1810. $sql = "UPDATE ".MAIN_DB_PREFIX."product SET";
  1811. $sql.= " stock = '".$obj->total."'";
  1812. $sql.= " WHERE rowid=".$obj->fk_product;
  1813. $resql2=$db->query($sql);
  1814. if ($resql2)
  1815. {
  1816. }
  1817. else
  1818. {
  1819. $error++;
  1820. dol_print_error($db);
  1821. }
  1822. print ". ";
  1823. $i++;
  1824. }
  1825. }
  1826. if ($error == 0)
  1827. {
  1828. $db->commit();
  1829. }
  1830. else
  1831. {
  1832. $db->rollback();
  1833. }
  1834. }
  1835. else
  1836. {
  1837. dol_print_error($db);
  1838. $db->rollback();
  1839. }
  1840. print '</td></tr>';
  1841. }
  1842. /**
  1843. * Migration of menus (use only 1 table instead of 3)
  1844. * 2.6 -> 2.7
  1845. *
  1846. * @param DoliDB $db Database handler
  1847. * @param Translate $langs Object langs
  1848. * @param Conf $conf Object conf
  1849. * @return void
  1850. */
  1851. function migrate_menus($db,$langs,$conf)
  1852. {
  1853. dolibarr_install_syslog("upgrade2::migrate_menus");
  1854. print '<tr><td colspan="4">';
  1855. print '<br>';
  1856. print '<b>'.$langs->trans('MigrationMenusDetail')."</b><br>\n";
  1857. $error = 0;
  1858. if ($db->DDLInfoTable(MAIN_DB_PREFIX."menu_constraint"))
  1859. {
  1860. $db->begin();
  1861. $sql = "SELECT m.rowid, mc.action";
  1862. $sql.= " FROM ".MAIN_DB_PREFIX."menu_constraint as mc, ".MAIN_DB_PREFIX."menu_const as md, ".MAIN_DB_PREFIX."menu as m";
  1863. $sql.= " WHERE md.fk_menu = m.rowid AND md.fk_constraint = mc.rowid";
  1864. $sql.= " AND m.enabled = '1'";
  1865. $resql = $db->query($sql);
  1866. if ($resql)
  1867. {
  1868. $i = 0;
  1869. $num = $db->num_rows($resql);
  1870. if ($num)
  1871. {
  1872. while ($i < $num)
  1873. {
  1874. $obj = $db->fetch_object($resql);
  1875. $sql = "UPDATE ".MAIN_DB_PREFIX."menu SET";
  1876. $sql.= " enabled = '".$obj->action."'";
  1877. $sql.= " WHERE rowid=".$obj->rowid;
  1878. $sql.= " AND enabled = '1'";
  1879. $resql2=$db->query($sql);
  1880. if ($resql2)
  1881. {
  1882. }
  1883. else
  1884. {
  1885. $error++;
  1886. dol_print_error($db);
  1887. }
  1888. print ". ";
  1889. $i++;
  1890. }
  1891. }
  1892. if ($error == 0)
  1893. {
  1894. $db->commit();
  1895. }
  1896. else
  1897. {
  1898. $db->rollback();
  1899. }
  1900. }
  1901. else
  1902. {
  1903. dol_print_error($db);
  1904. $db->rollback();
  1905. }
  1906. }
  1907. else
  1908. {
  1909. print $langs->trans('AlreadyDone')."<br>\n";
  1910. }
  1911. print '</td></tr>';
  1912. }
  1913. /**
  1914. * Migration du champ fk_adresse_livraison dans expedition
  1915. * 2.6 -> 2.7
  1916. *
  1917. * @param DoliDB $db Database handler
  1918. * @param Translate $langs Object langs
  1919. * @param Conf $conf Object conf
  1920. * @return void
  1921. */
  1922. function migrate_commande_deliveryaddress($db,$langs,$conf)
  1923. {
  1924. dolibarr_install_syslog("upgrade2::migrate_commande_deliveryaddress");
  1925. print '<tr><td colspan="4">';
  1926. print '<br>';
  1927. print '<b>'.$langs->trans('MigrationDeliveryAddress')."</b><br>\n";
  1928. $error = 0;
  1929. if ($db->DDLInfoTable(MAIN_DB_PREFIX."co_exp"))
  1930. {
  1931. $db->begin();
  1932. $sql = "SELECT c.fk_adresse_livraison, ce.fk_expedition";
  1933. $sql.= " FROM ".MAIN_DB_PREFIX."commande as c";
  1934. $sql.= ", ".MAIN_DB_PREFIX."co_exp as ce";
  1935. $sql.= " WHERE c.rowid = ce.fk_commande";
  1936. $sql.= " AND c.fk_adresse_livraison IS NOT NULL AND c.fk_adresse_livraison != 0";
  1937. $resql = $db->query($sql);
  1938. if ($resql)
  1939. {
  1940. $i = 0;
  1941. $num = $db->num_rows($resql);
  1942. if ($num)
  1943. {
  1944. while ($i < $num)
  1945. {
  1946. $obj = $db->fetch_object($resql);
  1947. $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
  1948. $sql.= " fk_adresse_livraison = '".$obj->fk_adresse_livraison."'";
  1949. $sql.= " WHERE rowid=".$obj->fk_expedition;
  1950. $resql2=$db->query($sql);
  1951. if (!$resql2)
  1952. {
  1953. $error++;
  1954. dol_print_error($db);
  1955. }
  1956. print ". ";
  1957. $i++;
  1958. }
  1959. }
  1960. else
  1961. {
  1962. print $langs->trans('AlreadyDone')."<br>\n";
  1963. }
  1964. if ($error == 0)
  1965. {
  1966. $db->commit();
  1967. }
  1968. else
  1969. {
  1970. $db->rollback();
  1971. }
  1972. }
  1973. else
  1974. {
  1975. dol_print_error($db);
  1976. $db->rollback();
  1977. }
  1978. }
  1979. else
  1980. {
  1981. print $langs->trans('AlreadyDone')."<br>\n";
  1982. }
  1983. print '</td></tr>';
  1984. }
  1985. /**
  1986. * Migration du champ fk_remise_except dans llx_facturedet doit correspondre a
  1987. * lien dans llx_societe_remise_except vers llx_facturedet
  1988. *
  1989. * @param DoliDB $db Database handler
  1990. * @param Translate $langs Object langs
  1991. * @param Conf $conf Object conf
  1992. * @return void
  1993. */
  1994. function migrate_restore_missing_links($db,$langs,$conf)
  1995. {
  1996. dolibarr_install_syslog("upgrade2::migrate_restore_missing_links");
  1997. if (($db->type == 'mysql' || $db->type == 'mysqli'))
  1998. {
  1999. if (versioncompare($db->getVersionArray(),array(4,0)) < 0)
  2000. {
  2001. dolibarr_install_syslog("upgrade2::migrate_restore_missing_links Version of database too old to make this migrate action");
  2002. return 0;
  2003. }
  2004. }
  2005. print '<tr><td colspan="4">';
  2006. print '<br>';
  2007. print '<b>'.$langs->trans('MigrationFixData')."</b> (1)<br>\n";
  2008. $error = 0;
  2009. // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 1.
  2010. $table1='facturedet'; $field1='fk_remise_except';
  2011. $table2='societe_remise_except'; $field2='fk_facture_line';
  2012. $db->begin();
  2013. $sql = "SELECT t1.rowid, t1.".$field1." as field";
  2014. $sql.= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
  2015. $sql.= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
  2016. $sql.= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
  2017. $sql.= " WHERE t1.rowid = t2.".$field2.")";
  2018. dolibarr_install_syslog("upgrade2:migrate_restore_missing_links DIRECTION 1 sql=".$sql);
  2019. $resql = $db->query($sql);
  2020. if ($resql)
  2021. {
  2022. $i = 0;
  2023. $num = $db->num_rows($resql);
  2024. if ($num)
  2025. {
  2026. while ($i < $num)
  2027. {
  2028. $obj = $db->fetch_object($resql);
  2029. print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
  2030. $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
  2031. $sql.= " ".$field2." = '".$obj->rowid."'";
  2032. $sql.= " WHERE rowid=".$obj->field;
  2033. $resql2=$db->query($sql);
  2034. if (! $resql2)
  2035. {
  2036. $error++;
  2037. dol_print_error($db);
  2038. }
  2039. //print ". ";
  2040. $i++;
  2041. }
  2042. }
  2043. else print $langs->trans('AlreadyDone')."<br>\n";
  2044. if ($error == 0)
  2045. {
  2046. $db->commit();
  2047. }
  2048. else
  2049. {
  2050. $db->rollback();
  2051. }
  2052. }
  2053. else
  2054. {
  2055. dol_print_error($db);
  2056. $db->rollback();
  2057. }
  2058. print '</td></tr>';
  2059. print '<tr><td colspan="4">';
  2060. print '<br>';
  2061. print '<b>'.$langs->trans('MigrationFixData')."</b> (2)<br>\n";
  2062. // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 2.
  2063. $table2='facturedet'; $field2='fk_remise_except';
  2064. $table1='societe_remise_except'; $field1='fk_facture_line';
  2065. $db->begin();
  2066. $sql = "SELECT t1.rowid, t1.".$field1." as field";
  2067. $sql.= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
  2068. $sql.= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
  2069. $sql.= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
  2070. $sql.= " WHERE t1.rowid = t2.".$field2.")";
  2071. dolibarr_install_syslog("upgrade2:migrate_restore_missing_links DIRECTION 2 sql=".$sql);
  2072. $resql = $db->query($sql);
  2073. if ($resql)
  2074. {
  2075. $i = 0;
  2076. $num = $db->num_rows($resql);
  2077. if ($num)
  2078. {
  2079. while ($i < $num)
  2080. {
  2081. $obj = $db->fetch_object($resql);
  2082. print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
  2083. $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
  2084. $sql.= " ".$field2." = '".$obj->rowid."'";
  2085. $sql.= " WHERE rowid=".$obj->field;
  2086. $resql2=$db->query($sql);
  2087. if (! $resql2)
  2088. {
  2089. $error++;
  2090. dol_print_error($db);
  2091. }
  2092. //print ". ";
  2093. $i++;
  2094. }
  2095. }
  2096. else
  2097. {
  2098. print $langs->trans('AlreadyDone')."<br>\n";
  2099. }
  2100. if ($error == 0)
  2101. {
  2102. $db->commit();
  2103. }
  2104. else
  2105. {
  2106. $db->rollback();
  2107. }
  2108. }
  2109. else
  2110. {
  2111. dol_print_error($db);
  2112. $db->rollback();
  2113. }
  2114. print '</td></tr>';
  2115. }
  2116. /**
  2117. * Migration du champ fk_user_resp de llx_projet vers llx_element_contact
  2118. *
  2119. * @param DoliDB $db Database handler
  2120. * @param Translate $langs Object langs
  2121. * @param Conf $conf Object conf
  2122. * @return void
  2123. */
  2124. function migrate_project_user_resp($db,$langs,$conf)
  2125. {
  2126. dolibarr_install_syslog("upgrade2::migrate_project_user_resp");
  2127. print '<tr><td colspan="4">';
  2128. print '<br>';
  2129. print '<b>'.$langs->trans('MigrationProjectUserResp')."</b><br>\n";
  2130. $result = $db->DDLDescTable(MAIN_DB_PREFIX."projet","fk_user_resp");
  2131. $obj = $db->fetch_object($result);
  2132. if ($obj)
  2133. {
  2134. $error = 0;
  2135. $db->begin();
  2136. $sql = "SELECT rowid, fk_user_resp FROM ".MAIN_DB_PREFIX."projet";
  2137. $resql = $db->query($sql);
  2138. if ($resql)
  2139. {
  2140. $i = 0;
  2141. $num = $db->num_rows($resql);
  2142. if ($num)
  2143. {
  2144. while ($i < $num)
  2145. {
  2146. $obj = $db->fetch_object($resql);
  2147. $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
  2148. $sql2.= "datecreate";
  2149. $sql2.= ", statut";
  2150. $sql2.= ", element_id";
  2151. $sql2.= ", fk_c_type_contact";
  2152. $sql2.= ", fk_socpeople";
  2153. $sql2.= ") VALUES (";
  2154. $sql2.= "'".$db->idate(dol_now())."'";
  2155. $sql2.= ", '4'";
  2156. $sql2.= ", ".$obj->rowid;
  2157. $sql2.= ", '160'";
  2158. $sql2.= ", ".$obj->fk_user_resp;
  2159. $sql2.= ")";
  2160. if ($obj->fk_user_resp > 0)
  2161. {
  2162. $resql2=$db->query($sql2);
  2163. if (!$resql2)
  2164. {
  2165. $error++;
  2166. dol_print_error($db);
  2167. }
  2168. }
  2169. print ". ";
  2170. $i++;
  2171. }
  2172. }
  2173. if ($error == 0)
  2174. {
  2175. $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."projet DROP COLUMN fk_user_resp";
  2176. if ($db->query($sqlDrop))
  2177. {
  2178. $db->commit();
  2179. }
  2180. else
  2181. {
  2182. $db->rollback();
  2183. }
  2184. }
  2185. else
  2186. {
  2187. $db->rollback();
  2188. }
  2189. }
  2190. else
  2191. {
  2192. dol_print_error($db);
  2193. $db->rollback();
  2194. }
  2195. }
  2196. else
  2197. {
  2198. print $langs->trans('AlreadyDone')."<br>\n";
  2199. }
  2200. print '</td></tr>';
  2201. }
  2202. /**
  2203. * Migration de la table llx_projet_task_actors vers llx_element_contact
  2204. *
  2205. * @param DoliDB $db Database handler
  2206. * @param Translate $langs Object langs
  2207. * @param Conf $conf Object conf
  2208. * @return void
  2209. */
  2210. function migrate_project_task_actors($db,$langs,$conf)
  2211. {
  2212. dolibarr_install_syslog("upgrade2::migrate_project_task_actors");
  2213. print '<tr><td colspan="4">';
  2214. print '<br>';
  2215. print '<b>'.$langs->trans('MigrationProjectTaskActors')."</b><br>\n";
  2216. if ($db->DDLInfoTable(MAIN_DB_PREFIX."projet_task_actors"))
  2217. {
  2218. $error = 0;
  2219. $db->begin();
  2220. $sql = "SELECT fk_projet_task, fk_user FROM ".MAIN_DB_PREFIX."projet_task_actors";
  2221. $resql = $db->query($sql);
  2222. if ($resql)
  2223. {
  2224. $i = 0;
  2225. $num = $db->num_rows($resql);
  2226. if ($num)
  2227. {
  2228. while ($i < $num)
  2229. {
  2230. $obj = $db->fetch_object($resql);
  2231. $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
  2232. $sql2.= "datecreate";
  2233. $sql2.= ", statut";
  2234. $sql2.= ", element_id";
  2235. $sql2.= ", fk_c_type_contact";
  2236. $sql2.= ", fk_socpeople";
  2237. $sql2.= ") VALUES (";
  2238. $sql2.= "'".$db->idate(dol_now())."'";
  2239. $sql2.= ", '4'";
  2240. $sql2.= ", ".$obj->fk_projet_task;
  2241. $sql2.= ", '180'";
  2242. $sql2.= ", ".$obj->fk_user;
  2243. $sql2.= ")";
  2244. $resql2=$db->query($sql2);
  2245. if (!$resql2)
  2246. {
  2247. $error++;
  2248. dol_print_error($db);
  2249. }
  2250. print ". ";
  2251. $i++;
  2252. }
  2253. }
  2254. if ($error == 0)
  2255. {
  2256. $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX."projet_task_actors";
  2257. if ($db->query($sqlDrop))
  2258. {
  2259. $db->commit();
  2260. }
  2261. else
  2262. {
  2263. $db->rollback();
  2264. }
  2265. }
  2266. else
  2267. {
  2268. $db->rollback();
  2269. }
  2270. }
  2271. else
  2272. {
  2273. dol_print_error($db);
  2274. $db->rollback();
  2275. }
  2276. }
  2277. else
  2278. {
  2279. print $langs->trans('AlreadyDone')."<br>\n";
  2280. }
  2281. print '</td></tr>';
  2282. }
  2283. /**
  2284. * Migration des tables de relation
  2285. *
  2286. * @param DoliDB $db Database handler
  2287. * @param Translate $langs Object langs
  2288. * @param Conf $conf Object conf
  2289. * @param string $table Table name
  2290. * @param int $fk_source Id of element source
  2291. * @param type $sourcetype Type of element source
  2292. * @param int $fk_target Id of element target
  2293. * @param type $targettype Type of element target
  2294. * @return void
  2295. */
  2296. function migrate_relationship_tables($db,$langs,$conf,$table,$fk_source,$sourcetype,$fk_target,$targettype)
  2297. {
  2298. print '<tr><td colspan="4">';
  2299. print '<br>';
  2300. print '<b>'.$langs->trans('MigrationRelationshipTables',MAIN_DB_PREFIX.$table)."</b><br>\n";
  2301. $error = 0;
  2302. if ($db->DDLInfoTable(MAIN_DB_PREFIX.$table))
  2303. {
  2304. dolibarr_install_syslog("upgrade2::migrate_relationship_tables table = ".MAIN_DB_PREFIX.$table);
  2305. $db->begin();
  2306. $sqlSelect = "SELECT ".$fk_source.", ".$fk_target;
  2307. $sqlSelect.= " FROM ".MAIN_DB_PREFIX.$table;
  2308. $resql = $db->query($sqlSelect);
  2309. if ($resql)
  2310. {
  2311. $i = 0;
  2312. $num = $db->num_rows($resql);
  2313. if ($num)
  2314. {
  2315. while ($i < $num)
  2316. {
  2317. $obj = $db->fetch_object($resql);
  2318. $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
  2319. $sqlInsert.= "fk_source";
  2320. $sqlInsert.= ", sourcetype";
  2321. $sqlInsert.= ", fk_target";
  2322. $sqlInsert.= ", targettype";
  2323. $sqlInsert.= ") VALUES (";
  2324. $sqlInsert.= $obj->$fk_source;
  2325. $sqlInsert.= ", '".$sourcetype."'";
  2326. $sqlInsert.= ", ".$obj->$fk_target;
  2327. $sqlInsert.= ", '".$targettype."'";
  2328. $sqlInsert.= ")";
  2329. $result=$db->query($sqlInsert);
  2330. if (! $result)
  2331. {
  2332. $error++;
  2333. dol_print_error($db);
  2334. }
  2335. print ". ";
  2336. $i++;
  2337. }
  2338. }
  2339. else
  2340. {
  2341. print $langs->trans('AlreadyDone')."<br>\n";
  2342. }
  2343. if ($error == 0)
  2344. {
  2345. $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX.$table;
  2346. if ($db->query($sqlDrop))
  2347. {
  2348. $db->commit();
  2349. }
  2350. else
  2351. {
  2352. $db->rollback();
  2353. }
  2354. }
  2355. else
  2356. {
  2357. $db->rollback();
  2358. }
  2359. }
  2360. else
  2361. {
  2362. dol_print_error($db);
  2363. $db->rollback();
  2364. }
  2365. }
  2366. else
  2367. {
  2368. print $langs->trans('AlreadyDone')."<br>\n";
  2369. }
  2370. print '</td></tr>';
  2371. }
  2372. /**
  2373. * Migrate duration in seconds
  2374. *
  2375. * @param DoliDB $db Database handler
  2376. * @param Translate $langs Object langs
  2377. * @param Conf $conf Object conf
  2378. * @return void
  2379. */
  2380. function migrate_project_task_time($db,$langs,$conf)
  2381. {
  2382. dolibarr_install_syslog("upgrade2::migrate_project_task_time");
  2383. print '<tr><td colspan="4">';
  2384. print '<br>';
  2385. print '<b>'.$langs->trans('MigrationProjectTaskTime')."</b><br>\n";
  2386. $error = 0;
  2387. $db->begin();
  2388. $sql = "SELECT rowid, fk_task, task_duration";
  2389. $sql.= " FROM ".MAIN_DB_PREFIX."projet_task_time";
  2390. $resql = $db->query($sql);
  2391. if ($resql)
  2392. {
  2393. $i = 0;
  2394. $num = $db->num_rows($resql);
  2395. if ($num)
  2396. {
  2397. $totaltime = array();
  2398. $oldtime = 0;
  2399. while ($i < $num)
  2400. {
  2401. $obj = $db->fetch_object($resql);
  2402. if ($obj->task_duration > 0)
  2403. {
  2404. // convert to second
  2405. // only for int time and float time ex: 1,75 for 1h45
  2406. list($hour,$min) = explode('.',$obj->task_duration);
  2407. $hour = $hour*60*60;
  2408. $min = ($min/100)*60*60;
  2409. $newtime = $hour+$min;
  2410. $sql2 = "UPDATE ".MAIN_DB_PREFIX."projet_task_time SET";
  2411. $sql2.= " task_duration = ".$newtime;
  2412. $sql2.= " WHERE rowid = ".$obj->rowid;
  2413. $resql2=$db->query($sql2);
  2414. if (!$resql2)
  2415. {
  2416. $error++;
  2417. dol_print_error($db);
  2418. }
  2419. print ". ";
  2420. $oldtime++;
  2421. if (! empty($totaltime[$obj->fk_task])) $totaltime[$obj->fk_task] += $newtime;
  2422. else $totaltime[$obj->fk_task] = $newtime;
  2423. }
  2424. else
  2425. {
  2426. if (! empty($totaltime[$obj->fk_task])) $totaltime[$obj->fk_task] += $obj->task_duration;
  2427. else $totaltime[$obj->fk_task] = $obj->task_duration;
  2428. }
  2429. $i++;
  2430. }
  2431. if ($error == 0)
  2432. {
  2433. if ($oldtime > 0)
  2434. {
  2435. foreach($totaltime as $taskid => $total_duration)
  2436. {
  2437. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
  2438. $sql.= " duration_effective = ".$total_duration;
  2439. $sql.= " WHERE rowid = ".$taskid;
  2440. $resql=$db->query($sql);
  2441. if (!$resql)
  2442. {
  2443. $error++;
  2444. dol_print_error($db);
  2445. }
  2446. }
  2447. }
  2448. else
  2449. {
  2450. print $langs->trans('AlreadyDone')."<br>\n";
  2451. }
  2452. }
  2453. else
  2454. {
  2455. dol_print_error($db);
  2456. }
  2457. }
  2458. else
  2459. {
  2460. print $langs->trans('AlreadyDone')."<br>\n";
  2461. }
  2462. }
  2463. else
  2464. {
  2465. dol_print_error($db);
  2466. }
  2467. if ($error == 0)
  2468. {
  2469. $db->commit();
  2470. }
  2471. else
  2472. {
  2473. $db->rollback();
  2474. }
  2475. print '</td></tr>';
  2476. }
  2477. /**
  2478. * Migrate order ref_customer and date_delivery fields to llx_expedition
  2479. *
  2480. * @param DoliDB $db Database handler
  2481. * @param Translate $langs Object langs
  2482. * @param Conf $conf Object conf
  2483. * @return void
  2484. */
  2485. function migrate_customerorder_shipping($db,$langs,$conf)
  2486. {
  2487. print '<tr><td colspan="4">';
  2488. print '<br>';
  2489. print '<b>'.$langs->trans('MigrationCustomerOrderShipping')."</b><br>\n";
  2490. $error = 0;
  2491. $result1 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition","ref_customer");
  2492. $result2 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition","date_delivery");
  2493. $obj1 = $db->fetch_object($result1);
  2494. $obj2 = $db->fetch_object($result2);
  2495. if (!$obj1 && !$obj2)
  2496. {
  2497. dolibarr_install_syslog("upgrade2::migrate_customerorder_shipping");
  2498. $db->begin();
  2499. $sqlAdd1 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN ref_customer varchar(30) AFTER entity";
  2500. $sqlAdd2 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN date_delivery date DEFAULT NULL AFTER date_expedition";
  2501. if ($db->query($sqlAdd1) && $db->query($sqlAdd2))
  2502. {
  2503. $sqlSelect = "SELECT e.rowid as shipping_id, c.ref_client, c.date_livraison";
  2504. $sqlSelect.= " FROM ".MAIN_DB_PREFIX."expedition as e";
  2505. $sqlSelect.= ", ".MAIN_DB_PREFIX."element_element as el";
  2506. $sqlSelect.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON c.rowid = el.fk_source AND el.sourcetype = 'commande'";
  2507. $sqlSelect.= " WHERE e.rowid = el.fk_target";
  2508. $sqlSelect.= " AND el.targettype = 'shipping'";
  2509. $resql = $db->query($sqlSelect);
  2510. if ($resql)
  2511. {
  2512. $i = 0;
  2513. $num = $db->num_rows($resql);
  2514. if ($num)
  2515. {
  2516. while ($i < $num)
  2517. {
  2518. $obj = $db->fetch_object($resql);
  2519. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
  2520. $sqlUpdate.= " ref_customer = '".$obj->ref_client."'";
  2521. $sqlUpdate.= ", date_delivery = '".($obj->date_livraison?$obj->date_livraison:'null')."'";
  2522. $sqlUpdate.= " WHERE rowid = ".$obj->shipping_id;
  2523. $result=$db->query($sqlUpdate);
  2524. if (! $result)
  2525. {
  2526. $error++;
  2527. dol_print_error($db);
  2528. }
  2529. print ". ";
  2530. $i++;
  2531. }
  2532. }
  2533. else
  2534. {
  2535. print $langs->trans('AlreadyDone')."<br>\n";
  2536. }
  2537. if ($error == 0)
  2538. {
  2539. $db->commit();
  2540. }
  2541. else
  2542. {
  2543. dol_print_error($db);
  2544. $db->rollback();
  2545. }
  2546. }
  2547. else
  2548. {
  2549. dol_print_error($db);
  2550. $db->rollback();
  2551. }
  2552. }
  2553. else
  2554. {
  2555. dol_print_error($db);
  2556. $db->rollback();
  2557. }
  2558. }
  2559. else
  2560. {
  2561. print $langs->trans('AlreadyDone')."<br>\n";
  2562. }
  2563. print '</td></tr>';
  2564. }
  2565. /**
  2566. * Migrate link stored into fk_expedition into llx_element_element
  2567. *
  2568. * @param DoliDB $db Database handler
  2569. * @param Translate $langs Object langs
  2570. * @param Conf $conf Object conf
  2571. * @return void
  2572. */
  2573. function migrate_shipping_delivery($db,$langs,$conf)
  2574. {
  2575. print '<tr><td colspan="4">';
  2576. print '<br>';
  2577. print '<b>'.$langs->trans('MigrationShippingDelivery')."</b><br>\n";
  2578. $error = 0;
  2579. $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison","fk_expedition");
  2580. $obj = $db->fetch_object($result);
  2581. if ($obj)
  2582. {
  2583. dolibarr_install_syslog("upgrade2::migrate_shipping_delivery");
  2584. $db->begin();
  2585. $sqlSelect = "SELECT rowid, fk_expedition";
  2586. $sqlSelect.= " FROM ".MAIN_DB_PREFIX."livraison";
  2587. $sqlSelect.= " WHERE fk_expedition is not null";
  2588. $resql = $db->query($sqlSelect);
  2589. if ($resql)
  2590. {
  2591. $i = 0;
  2592. $num = $db->num_rows($resql);
  2593. if ($num)
  2594. {
  2595. while ($i < $num)
  2596. {
  2597. $obj = $db->fetch_object($resql);
  2598. $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
  2599. $sqlInsert.= "fk_source";
  2600. $sqlInsert.= ", sourcetype";
  2601. $sqlInsert.= ", fk_target";
  2602. $sqlInsert.= ", targettype";
  2603. $sqlInsert.= ") VALUES (";
  2604. $sqlInsert.= $obj->fk_expedition;
  2605. $sqlInsert.= ", 'shipping'";
  2606. $sqlInsert.= ", ".$obj->rowid;
  2607. $sqlInsert.= ", 'delivery'";
  2608. $sqlInsert.= ")";
  2609. $result=$db->query($sqlInsert);
  2610. if ($result)
  2611. {
  2612. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET fk_expedition = NULL";
  2613. $sqlUpdate.= " WHERE rowid = ".$obj->rowid;
  2614. $result=$db->query($sqlUpdate);
  2615. if (! $result)
  2616. {
  2617. $error++;
  2618. dol_print_error($db);
  2619. }
  2620. print ". ";
  2621. }
  2622. else
  2623. {
  2624. $error++;
  2625. dol_print_error($db);
  2626. }
  2627. $i++;
  2628. }
  2629. }
  2630. else
  2631. {
  2632. print $langs->trans('AlreadyDone')."<br>\n";
  2633. }
  2634. if ($error == 0)
  2635. {
  2636. $sqlDelete = "DELETE FROM ".MAIN_DB_PREFIX."element_element WHERE sourcetype = 'commande' AND targettype = 'delivery'";
  2637. $db->query($sqlDelete);
  2638. $db->commit();
  2639. // DDL commands must not be inside a transaction
  2640. $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_expedition";
  2641. $db->query($sqlDrop);
  2642. }
  2643. else
  2644. {
  2645. dol_print_error($db);
  2646. $db->rollback();
  2647. }
  2648. }
  2649. else
  2650. {
  2651. dol_print_error($db);
  2652. $db->rollback();
  2653. }
  2654. }
  2655. else
  2656. {
  2657. print $langs->trans('AlreadyDone')."<br>\n";
  2658. }
  2659. print '</td></tr>';
  2660. }
  2661. /**
  2662. * We try to complete field ref_customer and date_delivery that are empty into llx_livraison.
  2663. * We set them with value from llx_expedition.
  2664. *
  2665. * @param DoliDB $db Database handler
  2666. * @param Translate $langs Object langs
  2667. * @param Conf $conf Object conf
  2668. * @return void
  2669. */
  2670. function migrate_shipping_delivery2($db,$langs,$conf)
  2671. {
  2672. print '<tr><td colspan="4">';
  2673. print '<br>';
  2674. print '<b>'.$langs->trans('MigrationShippingDelivery2')."</b><br>\n";
  2675. $error = 0;
  2676. dolibarr_install_syslog("upgrade2::migrate_shipping_delivery2");
  2677. $db->begin();
  2678. $sqlSelect = "SELECT l.rowid as delivery_id, e.ref_customer, e.date_delivery";
  2679. $sqlSelect.= " FROM ".MAIN_DB_PREFIX."livraison as l,";
  2680. $sqlSelect.= " ".MAIN_DB_PREFIX."element_element as el,";
  2681. $sqlSelect.= " ".MAIN_DB_PREFIX."expedition as e";
  2682. $sqlSelect.= " WHERE l.rowid = el.fk_target";
  2683. $sqlSelect.= " AND el.targettype = 'delivery'";
  2684. $sqlSelect.= " AND e.rowid = el.fk_source AND el.sourcetype = 'shipping'";
  2685. $sqlSelect.= " AND (e.ref_customer IS NOT NULL OR e.date_delivery IS NOT NULL)"; // Useless to process this record if both are null
  2686. // Add condition to know if we never migrate this record
  2687. $sqlSelect.= " AND (l.ref_customer IS NULL".($db->type!='pgsql'?" or l.ref_customer = ''":"").")";
  2688. $sqlSelect.= " AND (l.date_delivery IS NULL".($db->type!='pgsql'?" or l.date_delivery = ''":"").")";
  2689. $resql = $db->query($sqlSelect);
  2690. if ($resql)
  2691. {
  2692. $i = 0;
  2693. $num = $db->num_rows($resql);
  2694. if ($num)
  2695. {
  2696. while ($i < $num)
  2697. {
  2698. $obj = $db->fetch_object($resql);
  2699. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
  2700. $sqlUpdate.= " ref_customer = '".$obj->ref_customer."',";
  2701. $sqlUpdate.= " date_delivery = ".($obj->date_delivery?"'".$obj->date_delivery."'":'null');
  2702. $sqlUpdate.= " WHERE rowid = ".$obj->delivery_id;
  2703. $result=$db->query($sqlUpdate);
  2704. if (! $result)
  2705. {
  2706. $error++;
  2707. dol_print_error($db);
  2708. }
  2709. print ". ";
  2710. $i++;
  2711. }
  2712. }
  2713. else
  2714. {
  2715. print $langs->trans('AlreadyDone')."<br>\n";
  2716. }
  2717. if ($error == 0)
  2718. {
  2719. $db->commit();
  2720. }
  2721. else
  2722. {
  2723. dol_print_error($db);
  2724. $db->rollback();
  2725. }
  2726. }
  2727. else
  2728. {
  2729. dol_print_error($db);
  2730. $db->rollback();
  2731. }
  2732. print '</td></tr>';
  2733. }
  2734. /**
  2735. * Migrate link stored into fk_xxxx into fk_element and elementtype
  2736. *
  2737. * @param DoliDB $db Database handler
  2738. * @param Translate $langs Object langs
  2739. * @param Conf $conf Object conf
  2740. * @return void
  2741. */
  2742. function migrate_actioncomm_element($db,$langs,$conf)
  2743. {
  2744. print '<tr><td colspan="4">';
  2745. print '<br>';
  2746. print '<b>'.$langs->trans('MigrationActioncommElement')."</b><br>\n";
  2747. $elements = array(
  2748. 'propal' => 'propalrowid',
  2749. 'order' => 'fk_commande',
  2750. 'invoice' => 'fk_facture',
  2751. 'contract' => 'fk_contract',
  2752. 'order_supplier' => 'fk_supplier_order',
  2753. 'invoice_supplier' => 'fk_supplier_invoice'
  2754. );
  2755. foreach($elements as $type => $field)
  2756. {
  2757. $result = $db->DDLDescTable(MAIN_DB_PREFIX."actioncomm",$field);
  2758. $obj = $db->fetch_object($result);
  2759. if ($obj)
  2760. {
  2761. dolibarr_install_syslog("upgrade2::migrate_actioncomm_element field=".$field);
  2762. $db->begin();
  2763. $sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm SET ";
  2764. $sql.= "fk_element = ".$field.", elementtype = '".$type."'";
  2765. $sql.= " WHERE ".$field." IS NOT NULL";
  2766. $sql.= " AND fk_element IS NULL";
  2767. $sql.= " AND elementtype IS NULL";
  2768. $resql = $db->query($sql);
  2769. if ($resql)
  2770. {
  2771. $db->commit();
  2772. // DDL commands must not be inside a transaction
  2773. // We will drop at next version because a migrate should be runnable several times if it fails.
  2774. //$sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."actioncomm DROP COLUMN ".$field;
  2775. //$db->query($sqlDrop);
  2776. //print ". ";
  2777. }
  2778. else
  2779. {
  2780. dol_print_error($db);
  2781. $db->rollback();
  2782. }
  2783. }
  2784. else
  2785. {
  2786. print $langs->trans('AlreadyDone')."<br>\n";
  2787. }
  2788. }
  2789. print '</td></tr>';
  2790. }
  2791. /**
  2792. * Migrate link stored into fk_mode_reglement
  2793. *
  2794. * @param DoliDB $db Database handler
  2795. * @param Translate $langs Object langs
  2796. * @param Conf $conf Object conf
  2797. * @return void
  2798. */
  2799. function migrate_mode_reglement($db,$langs,$conf)
  2800. {
  2801. print '<tr><td colspan="4">';
  2802. print '<br>';
  2803. print '<b>'.$langs->trans('MigrationPaymentMode')."</b><br>\n";
  2804. $elements = array(
  2805. 'old_id' => array(5,8,9,10,11),
  2806. 'new_id' => array(50,51,52,53,54),
  2807. 'code' => array('VAD','TRA','LCR','FAC','PRO'),
  2808. 'tables' => array('commande_fournisseur','commande','facture_rec','facture','propal')
  2809. );
  2810. $count=0;
  2811. foreach($elements['old_id'] as $key => $old_id)
  2812. {
  2813. $error=0;
  2814. dolibarr_install_syslog("upgrade2::migrate_mode_reglement code=".$elements['code'][$key]);
  2815. $sqlSelect = "SELECT id";
  2816. $sqlSelect.= " FROM ".MAIN_DB_PREFIX."c_paiement";
  2817. $sqlSelect.= " WHERE id = ".$old_id;
  2818. $sqlSelect.= " AND code = '".$elements['code'][$key]."'";
  2819. $resql = $db->query($sqlSelect);
  2820. if ($resql)
  2821. {
  2822. $num = $db->num_rows($resql);
  2823. if ($num)
  2824. {
  2825. $count++;
  2826. $db->begin();
  2827. $sqla = "UPDATE ".MAIN_DB_PREFIX."paiement SET ";
  2828. $sqla.= "fk_paiement = ".$elements['new_id'][$key];
  2829. $sqla.= " WHERE fk_paiement = ".$old_id;
  2830. $sqla.= " AND fk_paiement IN (SELECT id FROM ".MAIN_DB_PREFIX."c_paiement WHERE id = ".$old_id." AND code = '".$elements['code'][$key]."')";
  2831. $resqla = $db->query($sqla);
  2832. $sql = "UPDATE ".MAIN_DB_PREFIX."c_paiement SET ";
  2833. $sql.= "id = ".$elements['new_id'][$key];
  2834. $sql.= " WHERE id = ".$old_id;
  2835. $sql.= " AND code = '".$elements['code'][$key]."'";
  2836. $resql = $db->query($sql);
  2837. if ($resqla && $resql)
  2838. {
  2839. foreach($elements['tables'] as $table) // FIXME We must not update tables if oldid is not renamed
  2840. {
  2841. $sql = "UPDATE ".MAIN_DB_PREFIX.$table." SET ";
  2842. $sql.= "fk_mode_reglement = ".$elements['new_id'][$key];
  2843. $sql.= " WHERE fk_mode_reglement = ".$old_id;
  2844. $resql = $db->query($sql);
  2845. if (! $resql)
  2846. {
  2847. dol_print_error($db);
  2848. $error++;
  2849. }
  2850. print ". ";
  2851. }
  2852. if (! $error)
  2853. {
  2854. $db->commit();
  2855. }
  2856. else
  2857. {
  2858. dol_print_error($db);
  2859. $db->rollback();
  2860. }
  2861. }
  2862. else
  2863. {
  2864. dol_print_error($db);
  2865. $db->rollback();
  2866. }
  2867. }
  2868. }
  2869. }
  2870. if ($count == 0) print $langs->trans('AlreadyDone')."<br>\n";
  2871. print '</td></tr>';
  2872. }
  2873. /**
  2874. * Migrate categorie association
  2875. *
  2876. * @param DoliDB $db Database handler
  2877. * @param Translate $langs Object langs
  2878. * @param Conf $conf Object conf
  2879. * @return void
  2880. */
  2881. function migrate_categorie_association($db,$langs,$conf)
  2882. {
  2883. print '<tr><td colspan="4">';
  2884. print '<br>';
  2885. print '<b>'.$langs->trans('MigrationCategorieAssociation')."</b><br>\n";
  2886. $error = 0;
  2887. if ($db->DDLInfoTable(MAIN_DB_PREFIX."categorie_association"))
  2888. {
  2889. dolibarr_install_syslog("upgrade2::migrate_categorie_association");
  2890. $db->begin();
  2891. $sqlSelect = "SELECT fk_categorie_mere, fk_categorie_fille";
  2892. $sqlSelect.= " FROM ".MAIN_DB_PREFIX."categorie_association";
  2893. $resql = $db->query($sqlSelect);
  2894. if ($resql)
  2895. {
  2896. $i = 0;
  2897. $num = $db->num_rows($resql);
  2898. if ($num)
  2899. {
  2900. while ($i < $num)
  2901. {
  2902. $obj = $db->fetch_object($resql);
  2903. $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."categorie SET ";
  2904. $sqlUpdate.= "fk_parent = ".$obj->fk_categorie_mere;
  2905. $sqlUpdate.= " WHERE rowid = ".$obj->fk_categorie_fille;
  2906. $result=$db->query($sqlUpdate);
  2907. if (! $result)
  2908. {
  2909. $error++;
  2910. dol_print_error($db);
  2911. }
  2912. print ". ";
  2913. $i++;
  2914. }
  2915. }
  2916. else
  2917. {
  2918. print $langs->trans('AlreadyDone')."<br>\n";
  2919. }
  2920. if (! $error)
  2921. {
  2922. // TODO DROP table in the next release
  2923. /*
  2924. $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX."categorie_association";
  2925. if ($db->query($sqlDrop))
  2926. {
  2927. $db->commit();
  2928. }
  2929. else
  2930. {
  2931. $db->rollback();
  2932. }
  2933. */
  2934. $db->commit();
  2935. }
  2936. else
  2937. {
  2938. $db->rollback();
  2939. }
  2940. }
  2941. else
  2942. {
  2943. dol_print_error($db);
  2944. $db->rollback();
  2945. }
  2946. }
  2947. else
  2948. {
  2949. print $langs->trans('AlreadyDone')."<br>\n";
  2950. }
  2951. print '</td></tr>';
  2952. }
  2953. /**
  2954. * Migration directory
  2955. *
  2956. * @param DoliDB $db Database handler
  2957. * @param Translate $langs Object langs
  2958. * @param Conf $conf Object conf
  2959. * @param string $oldname Old name
  2960. * @param string $newname New name
  2961. * @return void
  2962. */
  2963. function migrate_directories($db,$langs,$conf,$oldname,$newname)
  2964. {
  2965. dolibarr_install_syslog("upgrade2::migrate_directories");
  2966. if (is_dir(DOL_DATA_ROOT.$oldname) && ! file_exists(DOL_DATA_ROOT.$newname))
  2967. {
  2968. dolibarr_install_syslog("upgrade2::migrate_directories move ".DOL_DATA_ROOT.$oldname.' into '.DOL_DATA_ROOT.$newname);
  2969. @rename(DOL_DATA_ROOT.$oldname,DOL_DATA_ROOT.$newname);
  2970. }
  2971. }
  2972. /**
  2973. * Delete deprecated files
  2974. *
  2975. * @param DoliDB $db Database handler
  2976. * @param Translate $langs Object langs
  2977. * @param Conf $conf Object conf
  2978. * @return void
  2979. */
  2980. function migrate_delete_old_files($db,$langs,$conf)
  2981. {
  2982. $result=true;
  2983. dolibarr_install_syslog("upgrade2::migrate_delete_old_files");
  2984. // List of files to delete
  2985. $filetodeletearray=array(
  2986. DOL_DOCUMENT_ROOT.'/core/triggers/interface_demo.class.php',
  2987. DOL_DOCUMENT_ROOT.'/core/menus/barre_left/default.php',
  2988. DOL_DOCUMENT_ROOT.'/core/menus/barre_top/default.php',
  2989. DOL_DOCUMENT_ROOT.'/core/modules/modComptabiliteExpert.class.php',
  2990. DOL_DOCUMENT_ROOT.'/core/modules/modCommercial.class.php',
  2991. DOL_DOCUMENT_ROOT.'/core/modules/modProduit.class.php',
  2992. DOL_DOCUMENT_ROOT.'/phenix/inc/triggers/interface_modPhenix_Phenixsynchro.class.php',
  2993. DOL_DOCUMENT_ROOT.'/webcalendar/inc/triggers/interface_modWebcalendar_webcalsynchro.class.php',
  2994. DOL_DOCUMENT_ROOT.'/core/triggers/interface_modWebcalendar_Webcalsynchro.class.php',
  2995. DOL_DOCUMENT_ROOT.'/core/triggers/interface_modCommande_Ecotax.class.php',
  2996. DOL_DOCUMENT_ROOT.'/core/triggers/interface_modCommande_fraisport.class.php',
  2997. DOL_DOCUMENT_ROOT.'/core/triggers/interface_modPropale_PropalWorkflow.class.php',
  2998. DOL_DOCUMENT_ROOT.'/core/menus/smartphone/iphone.lib.php',
  2999. DOL_DOCUMENT_ROOT.'/core/menus/smartphone/iphone_backoffice.php',
  3000. DOL_DOCUMENT_ROOT.'/core/menus/smartphone/iphone_frontoffice.php',
  3001. DOL_DOCUMENT_ROOT.'/core/menus/standard/auguria_backoffice.php',
  3002. DOL_DOCUMENT_ROOT.'/core/menus/standard/auguria_frontoffice.php',
  3003. DOL_DOCUMENT_ROOT.'/core/menus/standard/eldy_backoffice.php',
  3004. DOL_DOCUMENT_ROOT.'/core/menus/standard/eldy_frontoffice.php',
  3005. DOL_DOCUMENT_ROOT.'/core/modules/mailings/dolibarr_services_expired.modules.php',
  3006. DOL_DOCUMENT_ROOT.'/core/modules/mailings/peche.modules.php',
  3007. DOL_DOCUMENT_ROOT.'/core/modules/mailings/poire.modules.php',
  3008. DOL_DOCUMENT_ROOT.'/core/modules/mailings/kiwi.modules.php',
  3009. DOL_DOCUMENT_ROOT.'/core/modules/facture/pdf_crabe.modules.php',
  3010. DOL_DOCUMENT_ROOT.'/core/modules/facture/pdf_oursin.modules.php'
  3011. );
  3012. foreach ($filetodeletearray as $filetodelete)
  3013. {
  3014. //print '<b>'.$filetodelete."</b><br>\n";
  3015. $result=1;
  3016. if (file_exists($filetodelete))
  3017. {
  3018. $result=dol_delete_file($filetodelete);
  3019. if (! $result)
  3020. {
  3021. $langs->load("errors");
  3022. print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteFile",$filetodelete);
  3023. print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
  3024. }
  3025. else
  3026. {
  3027. //print $langs->trans("FileWasRemoved",$filetodelete);
  3028. }
  3029. }
  3030. }
  3031. return $result;
  3032. }
  3033. /**
  3034. * Remove deprecated directories
  3035. *
  3036. * @param DoliDB $db Database handler
  3037. * @param Translate $langs Object langs
  3038. * @param Conf $conf Object conf
  3039. * @return void
  3040. */
  3041. function migrate_delete_old_dir($db,$langs,$conf)
  3042. {
  3043. $result=true;
  3044. dolibarr_install_syslog("upgrade2::migrate_delete_old_dir");
  3045. // List of files to delete
  3046. $filetodeletearray=array(
  3047. DOL_DOCUMENT_ROOT.'/core/modules/facture/terre',
  3048. DOL_DOCUMENT_ROOT.'/core/modules/facture/mercure'
  3049. );
  3050. foreach ($filetodeletearray as $filetodelete)
  3051. {
  3052. //print '<b>'.$filetodelete."</b><br>\n";
  3053. if (file_exists($filetodelete))
  3054. {
  3055. $result=dol_delete_dir_recursive($filetodelete);
  3056. }
  3057. if (! $result)
  3058. {
  3059. $langs->load("errors");
  3060. print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteDir",$filetodelete);
  3061. print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
  3062. }
  3063. }
  3064. return $result;
  3065. }
  3066. /**
  3067. * Disable/Reenable features modules.
  3068. * We must do this when internal menu of module or permissions has changed
  3069. * or when triggers have moved.
  3070. *
  3071. * @param DoliDB $db Database handler
  3072. * @param Translate $langs Object langs
  3073. * @param Conf $conf Object conf
  3074. * @return void
  3075. */
  3076. function migrate_reload_modules($db,$langs,$conf)
  3077. {
  3078. dolibarr_install_syslog("upgrade2::migrate_reload_modules");
  3079. if (! empty($conf->global->MAIN_MODULE_AGENDA))
  3080. {
  3081. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Agenda");
  3082. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modAgenda.class.php';
  3083. if ($res) {
  3084. $mod=new modAgenda($db);
  3085. $mod->remove('noboxes');
  3086. $mod->init('newboxdefonly');
  3087. }
  3088. }
  3089. if (! empty($conf->global->MAIN_MODULE_SOCIETE))
  3090. {
  3091. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Societe");
  3092. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modSociete.class.php';
  3093. if ($res) {
  3094. $mod=new modSociete($db);
  3095. $mod->remove('noboxes');
  3096. $mod->init('newboxdefonly');
  3097. }
  3098. }
  3099. if (! empty($conf->global->MAIN_MODULE_PRODUIT)) // Permission has changed into 2.7
  3100. {
  3101. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Produit");
  3102. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modProduct.class.php';
  3103. if ($res) {
  3104. $mod=new modProduct($db);
  3105. //$mod->remove('noboxes');
  3106. $mod->init('newboxdefonly');
  3107. }
  3108. }
  3109. if (! empty($conf->global->MAIN_MODULE_SERVICE)) // Permission has changed into 2.7
  3110. {
  3111. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Service");
  3112. if ($res) {
  3113. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modService.class.php';
  3114. $mod=new modService($db);
  3115. //$mod->remove('noboxes');
  3116. $mod->init('newboxdefonly');
  3117. }
  3118. }
  3119. if (! empty($conf->global->MAIN_MODULE_COMMANDE)) // Permission has changed into 2.9
  3120. {
  3121. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Commande");
  3122. if ($res) {
  3123. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modCommande.class.php';
  3124. $mod=new modCommande($db);
  3125. //$mod->remove('noboxes');
  3126. $mod->init('newboxdefonly');
  3127. }
  3128. }
  3129. if (! empty($conf->global->MAIN_MODULE_FACTURE)) // Permission has changed into 2.9
  3130. {
  3131. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Facture");
  3132. if ($res) {
  3133. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modFacture.class.php';
  3134. $mod=new modFacture($db);
  3135. //$mod->remove('noboxes');
  3136. $mod->init('newboxdefonly');
  3137. }
  3138. }
  3139. if (! empty($conf->global->MAIN_MODULE_FOURNISSEUR)) // Permission has changed into 2.9
  3140. {
  3141. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Fournisseur");
  3142. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modFournisseur.class.php';
  3143. if ($res) {
  3144. $mod=new modFournisseur($db);
  3145. //$mod->remove('noboxes');
  3146. $mod->init('newboxdefonly');
  3147. }
  3148. }
  3149. if (! empty($conf->global->MAIN_MODULE_USER)) // Permission has changed into 3.0
  3150. {
  3151. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module User");
  3152. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modUser.class.php';
  3153. if ($res) {
  3154. $mod=new modUser($db);
  3155. //$mod->remove('noboxes'); // We need to remove because id of module has changed
  3156. $mod->init('newboxdefonly');
  3157. }
  3158. }
  3159. if (! empty($conf->global->MAIN_MODULE_DEPLACEMENT)) // Permission has changed into 3.0
  3160. {
  3161. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Deplacement");
  3162. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modDeplacement.class.php';
  3163. if ($res) {
  3164. $mod=new modDeplacement($db);
  3165. //$mod->remove('noboxes'); // We need to remove because a permission id has been removed
  3166. $mod->init('newboxdefonly');
  3167. }
  3168. }
  3169. if (! empty($conf->global->MAIN_MODULE_DON)) // Permission has changed into 3.0
  3170. {
  3171. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Don");
  3172. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modDon.class.php';
  3173. if ($res) {
  3174. $mod=new modDon($db);
  3175. //$mod->remove('noboxes'); // We need to remove because a permission id has been removed
  3176. $mod->init('newboxdefonly');
  3177. }
  3178. }
  3179. if (! empty($conf->global->MAIN_MODULE_ECM)) // Permission has changed into 3.0 and 3.1
  3180. {
  3181. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module ECM");
  3182. if ($res) {
  3183. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modECM.class.php';
  3184. $mod=new modECM($db);
  3185. $mod->remove('noboxes'); // We need to remove because a permission id has been removed
  3186. $mod->init('newboxdefonly');
  3187. }
  3188. }
  3189. if (! empty($conf->global->MAIN_MODULE_PAYBOX)) // Permission has changed into 3.0
  3190. {
  3191. dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module Paybox");
  3192. $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modPaybox.class.php';
  3193. if ($res) {
  3194. $mod=new modPaybox($db);
  3195. $mod->remove('noboxes'); // We need to remove because id of module has changed
  3196. $mod->init('newboxdefonly');
  3197. }
  3198. }
  3199. }
  3200. /**
  3201. * Reload menu if dynamic menus, if modified by version
  3202. *
  3203. * @param DoliDB $db Database handler
  3204. * @param Translate $langs Object langs
  3205. * @param Conf $conf Object conf
  3206. * @param string $versionto Version target
  3207. * @return void
  3208. */
  3209. function migrate_reload_menu($db,$langs,$conf,$versionto)
  3210. {
  3211. global $conf;
  3212. dolibarr_install_syslog("upgrade2::migrate_reload_menu");
  3213. // Define list of menu handlers to initialize
  3214. $listofmenuhandler=array();
  3215. $versiontoarray=explode('.',$versionto);
  3216. // Script for VX (X<2.9) -> V2.9
  3217. $afterversionarray=explode('.','2.8.9');
  3218. $beforeversionarray=explode('.','2.9.9');
  3219. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  3220. {
  3221. $listofmenuhandler['auguria']=1; // We set here only dynamic menu handlers
  3222. }
  3223. // Script for VX (X<3.2) -> V3.2
  3224. $afterversionarray=explode('.','3.1.9');
  3225. $beforeversionarray=explode('.','3.2.9');
  3226. if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
  3227. {
  3228. $listofmenuhandler['auguria']=1; // We set here only dynamic menu handlers
  3229. }
  3230. foreach ($listofmenuhandler as $key => $val)
  3231. {
  3232. print '<tr><td colspan="4">';
  3233. //print "x".$key;
  3234. print '<br>';
  3235. print '<b>'.$langs->trans('Upgrade').'</b>: '.$langs->trans('MenuHandler')." ".$key."<br>\n";
  3236. // Load sql ini_menu_handler.sql file
  3237. $dir = DOL_DOCUMENT_ROOT."/core/menus/";
  3238. $file='init_menu_'.$key.'.sql';
  3239. if (file_exists($dir.$file))
  3240. {
  3241. $result=run_sql($dir.$file,1,'',1,$key);
  3242. }
  3243. print '</td></tr>';
  3244. }
  3245. }
  3246. /* A faire egalement: Modif statut paye et fk_facture des factures payes completement
  3247. On recherche facture incorrecte:
  3248. 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
  3249. having f.total_ttc = sum(pf.amount)
  3250. On les corrige:
  3251. update llx_facture set paye=1, fk_statut=2 where close_code is null
  3252. and rowid in (...)
  3253. */
  3254. ?>