upgrade2.php 151 KB

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