upgrade2.php 150 KB

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