repair.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. * Copyright (C) 2021 Frédéric France <frederic.france@free.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/install/repair.php
  23. * \brief Run repair script
  24. */
  25. include_once 'inc.php';
  26. if (file_exists($conffile)) {
  27. include_once $conffile;
  28. }
  29. require_once $dolibarr_main_document_root.'/core/lib/admin.lib.php';
  30. include_once $dolibarr_main_document_root.'/core/lib/images.lib.php';
  31. require_once $dolibarr_main_document_root.'/core/class/extrafields.class.php';
  32. require_once 'lib/repair.lib.php';
  33. $step = 2;
  34. $ok = 0;
  35. // Cette page peut etre longue. On augmente le delai autorise.
  36. // Ne fonctionne que si on est pas en safe_mode.
  37. $err = error_reporting();
  38. error_reporting(0);
  39. @set_time_limit(120);
  40. error_reporting($err);
  41. $setuplang = GETPOST("selectlang", 'aZ09', 3) ?GETPOST("selectlang", 'aZ09', 3) : 'auto';
  42. $langs->setDefaultLang($setuplang);
  43. $langs->loadLangs(array("admin", "install", "other"));
  44. if ($dolibarr_main_db_type == "mysqli") {
  45. $choix = 1;
  46. }
  47. if ($dolibarr_main_db_type == "pgsql") {
  48. $choix = 2;
  49. }
  50. if ($dolibarr_main_db_type == "mssql") {
  51. $choix = 3;
  52. }
  53. dolibarr_install_syslog("--- repair: entering upgrade.php page");
  54. if (!is_object($conf)) {
  55. dolibarr_install_syslog("repair: conf file not initialized", LOG_ERR);
  56. }
  57. /*
  58. * View
  59. */
  60. pHeader('', "upgrade2", GETPOST('action', 'aZ09'));
  61. // Action to launch the repair script
  62. $actiondone = 1;
  63. print '<h3>'.$langs->trans("Repair").'</h3>';
  64. print 'Option standard (\'test\' or \'confirmed\') is '.(GETPOST('standard', 'alpha') ?GETPOST('standard', 'alpha') : 'undefined').'<br>'."\n";
  65. // Disable modules
  66. print 'Option force_disable_of_modules_not_found (\'test\' or \'confirmed\') is '.(GETPOST('force_disable_of_modules_not_found', 'alpha') ?GETPOST('force_disable_of_modules_not_found', 'alpha') : 'undefined').'<br>'."\n";
  67. // Files
  68. print 'Option restore_thirdparties_logos (\'test\' or \'confirmed\') is '.(GETPOST('restore_thirdparties_logos', 'alpha') ?GETPOST('restore_thirdparties_logos', 'alpha') : 'undefined').'<br>'."\n";
  69. print 'Option restore_user_pictures (\'test\' or \'confirmed\') is '.(GETPOST('restore_user_pictures', 'alpha') ?GETPOST('restore_user_pictures', 'alpha') : 'undefined').'<br>'."\n";
  70. print 'Option rebuild_product_thumbs (\'test\' or \'confirmed\') is '.(GETPOST('rebuild_product_thumbs', 'alpha') ?GETPOST('rebuild_product_thumbs', 'alpha') : 'undefined').'<br>'."\n";
  71. // Clean tables and data
  72. print 'Option clean_linked_elements (\'test\' or \'confirmed\') is '.(GETPOST('clean_linked_elements', 'alpha') ?GETPOST('clean_linked_elements', 'alpha') : 'undefined').'<br>'."\n";
  73. print 'Option clean_menus (\'test\' or \'confirmed\') is '.(GETPOST('clean_menus', 'alpha') ?GETPOST('clean_menus', 'alpha') : 'undefined').'<br>'."\n";
  74. print 'Option clean_orphelin_dir (\'test\' or \'confirmed\') is '.(GETPOST('clean_orphelin_dir', 'alpha') ?GETPOST('clean_orphelin_dir', 'alpha') : 'undefined').'<br>'."\n";
  75. print 'Option clean_product_stock_batch (\'test\' or \'confirmed\') is '.(GETPOST('clean_product_stock_batch', 'alpha') ?GETPOST('clean_product_stock_batch', 'alpha') : 'undefined').'<br>'."\n";
  76. print 'Option clean_perm_table (\'test\' or \'confirmed\') is '.(GETPOST('clean_perm_table', 'alpha') ?GETPOST('clean_perm_table', 'alpha') : 'undefined').'<br>'."\n";
  77. print 'Option repair_link_dispatch_lines_supplier_order_lines, (\'test\' or \'confirmed\') is '.(GETPOST('repair_link_dispatch_lines_supplier_order_lines', 'alpha') ?GETPOST('repair_link_dispatch_lines_supplier_order_lines', 'alpha') : 'undefined').'<br>'."\n";
  78. // Init data
  79. print 'Option set_empty_time_spent_amount (\'test\' or \'confirmed\') is '.(GETPOST('set_empty_time_spent_amount', 'alpha') ?GETPOST('set_empty_time_spent_amount', 'alpha') : 'undefined').'<br>'."\n";
  80. // Structure
  81. print 'Option force_utf8_on_tables (force utf8 + row=dynamic), for mysql/mariadb only (\'test\' or \'confirmed\') is '.(GETPOST('force_utf8_on_tables', 'alpha') ?GETPOST('force_utf8_on_tables', 'alpha') : 'undefined').'<br>'."\n";
  82. print "Option force_utf8mb4_on_tables (force utf8mb4 + row=dynamic, EXPERIMENTAL!), for mysql/mariadb only ('test' or 'confirmed') is ".(GETPOST('force_utf8mb4_on_tables', 'alpha') ? GETPOST('force_utf8mb4_on_tables', 'alpha') : 'undefined')."<br>\n";
  83. // Rebuild sequence
  84. print 'Option rebuild_sequences, for postgresql only (\'test\' or \'confirmed\') is '.(GETPOST('rebuild_sequences', 'alpha') ?GETPOST('rebuild_sequences', 'alpha') : 'undefined').'<br>'."\n";
  85. print '<br>';
  86. print '<table cellspacing="0" cellpadding="1" border="0" width="100%">';
  87. $error = 0;
  88. // If password is encoded, we decode it
  89. if (preg_match('/crypted:/i', $dolibarr_main_db_pass) || !empty($dolibarr_main_db_encrypted_pass)) {
  90. require_once $dolibarr_main_document_root.'/core/lib/security.lib.php';
  91. if (preg_match('/crypted:/i', $dolibarr_main_db_pass)) {
  92. $dolibarr_main_db_pass = preg_replace('/crypted:/i', '', $dolibarr_main_db_pass);
  93. $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_pass);
  94. $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
  95. } else {
  96. $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_encrypted_pass);
  97. }
  98. }
  99. // $conf is already instancied inside inc.php
  100. $conf->db->type = $dolibarr_main_db_type;
  101. $conf->db->host = $dolibarr_main_db_host;
  102. $conf->db->port = $dolibarr_main_db_port;
  103. $conf->db->name = $dolibarr_main_db_name;
  104. $conf->db->user = $dolibarr_main_db_user;
  105. $conf->db->pass = $dolibarr_main_db_pass;
  106. // For encryption
  107. $conf->db->dolibarr_main_db_encryption = isset($dolibarr_main_db_encryption) ? $dolibarr_main_db_encryption : '';
  108. $conf->db->dolibarr_main_db_cryptkey = isset($dolibarr_main_db_cryptkey) ? $dolibarr_main_db_cryptkey : '';
  109. $db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, $conf->db->port);
  110. if ($db->connected) {
  111. print '<tr><td class="nowrap">';
  112. print $langs->trans("ServerConnection")." : $dolibarr_main_db_host</td><td class=\"right\">".$langs->trans("OK")."</td></tr>";
  113. dolibarr_install_syslog("repair: ".$langs->transnoentities("ServerConnection").": ".$dolibarr_main_db_host.$langs->transnoentities("OK"));
  114. $ok = 1;
  115. } else {
  116. print "<tr><td>".$langs->trans("ErrorFailedToConnectToDatabase", $dolibarr_main_db_name)."</td><td class=\"right\">".$langs->transnoentities("Error")."</td></tr>";
  117. dolibarr_install_syslog("repair: ".$langs->transnoentities("ErrorFailedToConnectToDatabase", $dolibarr_main_db_name));
  118. $ok = 0;
  119. }
  120. if ($ok) {
  121. if ($db->database_selected) {
  122. print '<tr><td class="nowrap">';
  123. print $langs->trans("DatabaseConnection")." : ".$dolibarr_main_db_name."</td><td class=\"right\">".$langs->trans("OK")."</td></tr>";
  124. dolibarr_install_syslog("repair: database connection successful: ".$dolibarr_main_db_name);
  125. $ok = 1;
  126. } else {
  127. print "<tr><td>".$langs->trans("ErrorFailedToConnectToDatabase", $dolibarr_main_db_name)."</td><td class=\"right\">".$langs->trans("Error")."</td></tr>";
  128. dolibarr_install_syslog("repair: ".$langs->transnoentities("ErrorFailedToConnectToDatabase", $dolibarr_main_db_name));
  129. $ok = 0;
  130. }
  131. }
  132. // Show database version
  133. if ($ok) {
  134. $version = $db->getVersion();
  135. $versionarray = $db->getVersionArray();
  136. print '<tr><td>'.$langs->trans("ServerVersion").'</td>';
  137. print '<td class="right">'.$version.'</td></tr>';
  138. dolibarr_install_syslog("repair: ".$langs->transnoentities("ServerVersion").": ".$version);
  139. //print '<td class="right">'.join('.',$versionarray).'</td></tr>';
  140. }
  141. $conf->setValues($db);
  142. // Reset forced setup after the setValues
  143. if (defined('SYSLOG_FILE')) {
  144. $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
  145. }
  146. $conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
  147. /* Start action here */
  148. $oneoptionset = 0;
  149. $oneoptionset = (GETPOST('standard', 'alpha') || GETPOST('restore_thirdparties_logos', 'alpha') || GETPOST('clean_linked_elements', 'alpha') || GETPOST('clean_menus', 'alpha')
  150. || GETPOST('clean_orphelin_dir', 'alpha') || GETPOST('clean_product_stock_batch', 'alpha') || GETPOST('set_empty_time_spent_amount', 'alpha') || GETPOST('rebuild_product_thumbs', 'alpha')
  151. || GETPOST('clean_perm_table', 'alpha')
  152. || GETPOST('force_disable_of_modules_not_found', 'alpha')
  153. || GETPOST('force_utf8_on_tables', 'alpha') || GETPOST('force_utf8mb4_on_tables', 'alpha')
  154. || GETPOST('rebuild_sequences', 'alpha'));
  155. if ($ok && $oneoptionset) {
  156. // Show wait message
  157. print '<tr><td colspan="2">'.$langs->trans("PleaseBePatient").'<br><br></td></tr>';
  158. flush();
  159. }
  160. // run_sql: Run repair SQL file
  161. if ($ok && GETPOST('standard', 'alpha')) {
  162. $dir = "mysql/migration/";
  163. $filelist = array();
  164. $i = 0;
  165. $ok = 0;
  166. // Recupere list fichier
  167. $filesindir = array();
  168. $handle = opendir($dir);
  169. if (is_resource($handle)) {
  170. while (($file = readdir($handle)) !== false) {
  171. if (preg_match('/\.sql$/i', $file)) {
  172. $filesindir[] = $file;
  173. }
  174. }
  175. }
  176. sort($filesindir);
  177. foreach ($filesindir as $file) {
  178. if (preg_match('/repair/i', $file)) {
  179. $filelist[] = $file;
  180. }
  181. }
  182. // Loop on each file
  183. foreach ($filelist as $file) {
  184. print '<tr><td class="nowrap">*** ';
  185. print $langs->trans("Script").'</td><td class="right">'.$file.'</td></tr>';
  186. $name = substr($file, 0, dol_strlen($file) - 4);
  187. // Run sql script
  188. $ok = run_sql($dir.$file, 0, '', 1);
  189. }
  190. }
  191. // sync_extrafields: Search list of fields declared and list of fields created into databases, then create fields missing
  192. if ($ok && GETPOST('standard', 'alpha')) {
  193. $extrafields = new ExtraFields($db);
  194. $listofmodulesextra = array('societe'=>'societe', 'adherent'=>'adherent', 'product'=>'product',
  195. 'socpeople'=>'socpeople', 'propal'=>'propal', 'commande'=>'commande', 'facture'=>'facture',
  196. 'supplier_proposal'=>'supplier_proposal', 'commande_fournisseur'=>'commande_fournisseur', 'facture_fourn'=>'facture_fourn',
  197. 'actioncomm'=>'actioncomm', 'bom_bom'=>'bom_bom', 'mrp_mo'=>'mrp_mo',
  198. 'adherent_type'=>'adherent_type', 'user'=>'user', 'projet'=>'projet', 'projet_task'=>'projet_task');
  199. print '<tr><td colspan="2"><br>*** Check fields into extra table structure match table of definition. If not add column into table</td></tr>';
  200. foreach ($listofmodulesextra as $tablename => $elementtype) {
  201. // Get list of fields
  202. $tableextra = MAIN_DB_PREFIX.$tablename.'_extrafields';
  203. // Define $arrayoffieldsdesc
  204. $arrayoffieldsdesc = $extrafields->fetch_name_optionals_label($elementtype);
  205. // Define $arrayoffieldsfound
  206. $arrayoffieldsfound = array();
  207. $resql = $db->DDLDescTable($tableextra);
  208. if ($resql) {
  209. print '<tr><td>Check availability of extra field for '.$tableextra."<br>\n";
  210. $i = 0;
  211. while ($obj = $db->fetch_object($resql)) {
  212. $fieldname = $fieldtype = '';
  213. if (preg_match('/mysql/', $db->type)) {
  214. $fieldname = $obj->Field;
  215. $fieldtype = $obj->Type;
  216. } else {
  217. $fieldname = isset($obj->Key) ? $obj->Key : $obj->attname;
  218. $fieldtype = isset($obj->Type) ? $obj->Type : 'varchar';
  219. }
  220. if (empty($fieldname)) {
  221. continue;
  222. }
  223. if (in_array($fieldname, array('rowid', 'tms', 'fk_object', 'import_key'))) {
  224. continue;
  225. }
  226. $arrayoffieldsfound[$fieldname] = array('type'=>$fieldtype);
  227. }
  228. // If it does not match, we create fields
  229. foreach ($arrayoffieldsdesc as $code => $label) {
  230. if (!in_array($code, array_keys($arrayoffieldsfound))) {
  231. print 'Found field '.$code.' declared into '.MAIN_DB_PREFIX.'extrafields table but not found into desc of table '.$tableextra." -> ";
  232. $type = $extrafields->attributes[$elementtype]['type'][$code]; $length = $extrafields->attributes[$elementtype]['size'][$code]; $attribute = ''; $default = ''; $extra = ''; $null = 'null';
  233. if ($type == 'boolean') {
  234. $typedb = 'int';
  235. $lengthdb = '1';
  236. } elseif ($type == 'price') {
  237. $typedb = 'double';
  238. $lengthdb = '24,8';
  239. } elseif ($type == 'phone') {
  240. $typedb = 'varchar';
  241. $lengthdb = '20';
  242. } elseif ($type == 'mail') {
  243. $typedb = 'varchar';
  244. $lengthdb = '128';
  245. } elseif (($type == 'select') || ($type == 'sellist') || ($type == 'radio') || ($type == 'checkbox') || ($type == 'chkbxlst')) {
  246. $typedb = 'text';
  247. $lengthdb = '';
  248. } elseif ($type == 'link') {
  249. $typedb = 'int';
  250. $lengthdb = '11';
  251. } else {
  252. $typedb = $type;
  253. $lengthdb = $length;
  254. }
  255. $field_desc = array(
  256. 'type'=>$typedb,
  257. 'value'=>$lengthdb,
  258. 'attribute'=>$attribute,
  259. 'default'=>$default,
  260. 'extra'=>$extra,
  261. 'null'=>$null
  262. );
  263. //var_dump($field_desc);exit;
  264. $result = 0;
  265. if (GETPOST('standard', 'alpha') == 'confirmed') {
  266. $result = $db->DDLAddField($tableextra, $code, $field_desc, "");
  267. if ($result < 0) {
  268. print "KO ".$db->lasterror."<br>\n";
  269. } else {
  270. print "OK<br>\n";
  271. }
  272. } else {
  273. print ' - Mode test, no column added.';
  274. }
  275. }
  276. }
  277. print "</td><td>&nbsp;</td></tr>\n";
  278. } else {
  279. dol_print_error($db);
  280. }
  281. }
  282. }
  283. // clean_data_ecm_dir: Clean data into ecm_directories table
  284. if ($ok && GETPOST('standard', 'alpha')) {
  285. clean_data_ecm_directories();
  286. }
  287. // clean declaration constants
  288. if ($ok && GETPOST('standard', 'alpha')) {
  289. print '<tr><td colspan="2"><br>*** Clean constant record of modules not enabled</td></tr>';
  290. $sql = "SELECT name, entity, value";
  291. $sql .= " FROM ".MAIN_DB_PREFIX."const as c";
  292. $sql .= " WHERE name LIKE 'MAIN_MODULE_%_TPL' OR name LIKE 'MAIN_MODULE_%_CSS' OR name LIKE 'MAIN_MODULE_%_JS' OR name LIKE 'MAIN_MODULE_%_HOOKS'";
  293. $sql .= " OR name LIKE 'MAIN_MODULE_%_TRIGGERS' OR name LIKE 'MAIN_MODULE_%_THEME' OR name LIKE 'MAIN_MODULE_%_SUBSTITUTIONS' OR name LIKE 'MAIN_MODULE_%_MODELS'";
  294. $sql .= " OR name LIKE 'MAIN_MODULE_%_MENUS' OR name LIKE 'MAIN_MODULE_%_LOGIN' OR name LIKE 'MAIN_MODULE_%_BARCODE' OR name LIKE 'MAIN_MODULE_%_TABS_%'";
  295. $sql .= " OR name LIKE 'MAIN_MODULE_%_MODULEFOREXTERNAL'";
  296. $sql .= " ORDER BY name, entity";
  297. $resql = $db->query($sql);
  298. if ($resql) {
  299. $num = $db->num_rows($resql);
  300. if ($num) {
  301. $db->begin();
  302. $i = 0;
  303. while ($i < $num) {
  304. $obj = $db->fetch_object($resql);
  305. $reg = array();
  306. if (preg_match('/MAIN_MODULE_([^_]+)_(.+)/i', $obj->name, $reg)) {
  307. $name = $reg[1];
  308. $type = $reg[2];
  309. $sql2 = "SELECT COUNT(*) as nb";
  310. $sql2 .= " FROM ".MAIN_DB_PREFIX."const as c";
  311. $sql2 .= " WHERE name = 'MAIN_MODULE_".$name."'";
  312. $sql2 .= " AND entity = ".((int) $obj->entity);
  313. $resql2 = $db->query($sql2);
  314. if ($resql2) {
  315. $obj2 = $db->fetch_object($resql2);
  316. if ($obj2 && $obj2->nb == 0) {
  317. // Module not found, so we can remove entry
  318. $sqldelete = "DELETE FROM ".MAIN_DB_PREFIX."const WHERE name = '".$db->escape($obj->name)."' AND entity = ".((int) $obj->entity);
  319. if (GETPOST('standard', 'alpha') == 'confirmed') {
  320. $db->query($sqldelete);
  321. print '<tr><td>Widget '.$obj->name.' set in entity '.$obj->entity.' with value '.$obj->value.' -> Module '.$name.' not enabled in entity '.((int) $obj->entity).', we delete record</td></tr>';
  322. } else {
  323. print '<tr><td>Widget '.$obj->name.' set in entity '.$obj->entity.' with value '.$obj->value.' -> Module '.$name.' not enabled in entity '.((int) $obj->entity).', we should delete record (not done, mode test)</td></tr>';
  324. }
  325. } else {
  326. //print '<tr><td>Constant '.$obj->name.' set in entity '.$obj->entity.' with value '.$obj->value.' -> Module found in entity '.$obj->entity.', we keep record</td></tr>';
  327. }
  328. }
  329. }
  330. $i++;
  331. }
  332. $db->commit();
  333. }
  334. } else {
  335. dol_print_error($db);
  336. }
  337. }
  338. // clean box of not enabled modules
  339. if ($ok && GETPOST('standard', 'alpha')) {
  340. print '<tr><td colspan="2"><br>*** Clean definition of boxes of modules not enabled</td></tr>';
  341. $sql = "SELECT file, entity FROM ".MAIN_DB_PREFIX."boxes_def";
  342. $sql .= " WHERE file like '%@%'";
  343. $resql = $db->query($sql);
  344. if ($resql) {
  345. $num = $db->num_rows($resql);
  346. if ($num) {
  347. $db->begin();
  348. $i = 0;
  349. while ($i < $num) {
  350. $obj = $db->fetch_object($resql);
  351. $reg = array();
  352. if (preg_match('/^(.+)@(.+)$/i', $obj->file, $reg)) {
  353. $name = $reg[1];
  354. $module = $reg[2];
  355. $sql2 = "SELECT COUNT(*) as nb";
  356. $sql2 .= " FROM ".MAIN_DB_PREFIX."const as c";
  357. $sql2 .= " WHERE name = 'MAIN_MODULE_".strtoupper($module)."'";
  358. $sql2 .= " AND entity = ".((int) $obj->entity);
  359. $sql2 .= " AND value <> 0";
  360. $resql2 = $db->query($sql2);
  361. if ($resql2) {
  362. $obj2 = $db->fetch_object($resql2);
  363. if ($obj2 && $obj2->nb == 0) {
  364. // Module not found, so we canremove entry
  365. $sqldeletea = "DELETE FROM ".MAIN_DB_PREFIX."boxes WHERE entity = ".((int) $obj->entity)." AND box_id IN (SELECT rowid FROM ".MAIN_DB_PREFIX."boxes_def WHERE file = '".$db->escape($obj->file)."' AND entity = ".((int) $obj->entity).")";
  366. $sqldeleteb = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def WHERE file = '".$db->escape($obj->file)."' AND entity = ".((int) $obj->entity);
  367. if (GETPOST('standard', 'alpha') == 'confirmed') {
  368. $db->query($sqldeletea);
  369. $db->query($sqldeleteb);
  370. print '<tr><td>Constant '.$obj->file.' set in boxes_def for entity '.$obj->entity.' but MAIN_MODULE_'.strtoupper($module).' not defined in entity '.((int) $obj->entity).', we delete record</td></tr>';
  371. } else {
  372. print '<tr><td>Constant '.$obj->file.' set in boxes_def for entity '.$obj->entity.' but MAIN_MODULE_'.strtoupper($module).' not defined in entity '.((int) $obj->entity).', we should delete record (not done, mode test)</td></tr>';
  373. }
  374. } else {
  375. //print '<tr><td>Constant '.$obj->name.' set in entity '.$obj->entity.' with value '.$obj->value.' -> Module found in entity '.$obj->entity.', we keep record</td></tr>';
  376. }
  377. }
  378. }
  379. $i++;
  380. }
  381. $db->commit();
  382. }
  383. }
  384. }
  385. // restore_thirdparties_logos: Move logos to correct new directory.
  386. if ($ok && GETPOST('restore_thirdparties_logos')) {
  387. //$exts=array('gif','png','jpg');
  388. $ext = '';
  389. print '<tr><td colspan="2"><br>*** Restore thirdparties logo<br>';
  390. $sql = "SELECT s.rowid, s.nom as name, s.logo FROM ".MAIN_DB_PREFIX."societe as s ORDER BY s.nom";
  391. $resql = $db->query($sql);
  392. if ($resql) {
  393. $num = $db->num_rows($resql);
  394. $i = 0;
  395. while ($i < $num) {
  396. $obj = $db->fetch_object($resql);
  397. /*
  398. $name=preg_replace('/é/','',$obj->name);
  399. $name=preg_replace('/ /','_',$name);
  400. $name=preg_replace('/\'/','',$name);
  401. */
  402. $tmp = explode('.', $obj->logo);
  403. $name = $tmp[0];
  404. if (isset($tmp[1])) {
  405. $ext = '.'.$tmp[1];
  406. }
  407. if (!empty($name)) {
  408. $filetotest = $dolibarr_main_data_root.'/societe/logos/'.$name.$ext;
  409. $filetotestsmall = $dolibarr_main_data_root.'/societe/logos/thumbs/'.$name.'_small'.$ext;
  410. $exists = dol_is_file($filetotest);
  411. print 'Check thirdparty '.$obj->rowid.' name='.$obj->name.' logo='.$obj->logo.' file '.$filetotest." exists=".$exists."<br>\n";
  412. if ($exists) {
  413. $filetarget = $dolibarr_main_data_root.'/societe/'.$obj->rowid.'/logos/'.$name.$ext;
  414. $filetargetsmall = $dolibarr_main_data_root.'/societe/'.$obj->rowid.'/logos/thumbs/'.$name.'_small'.$ext;
  415. $existt = dol_is_file($filetarget);
  416. if (!$existt) {
  417. if (GETPOST('restore_thirdparties_logos', 'alpha') == 'confirmed') {
  418. dol_mkdir($dolibarr_main_data_root.'/societe/'.$obj->rowid.'/logos');
  419. }
  420. print " &nbsp; &nbsp; &nbsp; -> Copy file ".$filetotest." -> ".$filetarget."<br>\n";
  421. if (GETPOST('restore_thirdparties_logos', 'alpha') == 'confirmed') {
  422. dol_copy($filetotest, $filetarget, '', 0);
  423. }
  424. }
  425. $existtt = dol_is_file($filetargetsmall);
  426. if (!$existtt) {
  427. if (GETPOST('restore_thirdparties_logos', 'alpha') == 'confirmed') {
  428. dol_mkdir($dolibarr_main_data_root.'/societe/'.$obj->rowid.'/logos/thumbs');
  429. }
  430. print " &nbsp; &nbsp; &nbsp; -> Copy file ".$filetotestsmall." -> ".$filetargetsmall."<br>\n";
  431. if (GETPOST('restore_thirdparties_logos', 'alpha') == 'confirmed') {
  432. dol_copy($filetotestsmall, $filetargetsmall, '', 0);
  433. }
  434. }
  435. }
  436. }
  437. $i++;
  438. }
  439. } else {
  440. $ok = 0;
  441. dol_print_error($db);
  442. }
  443. print '</td></tr>';
  444. }
  445. // restore_user_pictures: Move pictures to correct new directory.
  446. if ($ok && GETPOST('restore_user_pictures', 'alpha')) {
  447. //$exts=array('gif','png','jpg');
  448. $ext = '';
  449. print '<tr><td colspan="2"><br>*** Restore user pictures<br>';
  450. $sql = "SELECT s.rowid, s.firstname, s.lastname, s.login, s.photo FROM ".MAIN_DB_PREFIX."user as s ORDER BY s.rowid";
  451. $resql = $db->query($sql);
  452. if ($resql) {
  453. $num = $db->num_rows($resql);
  454. $i = 0;
  455. while ($i < $num) {
  456. $obj = $db->fetch_object($resql);
  457. /*
  458. $name=preg_replace('/é/','',$obj->name);
  459. $name=preg_replace('/ /','_',$name);
  460. $name=preg_replace('/\'/','',$name);
  461. */
  462. $tmp = explode('.', $obj->photo);
  463. $name = $tmp[0];
  464. if (isset($tmp[1])) {
  465. $ext = '.'.$tmp[1];
  466. }
  467. if (!empty($name)) {
  468. $filetotest = $dolibarr_main_data_root.'/users/'.substr(sprintf('%08d', $obj->rowid), -1, 1).'/'.substr(sprintf('%08d', $obj->rowid), -2, 1).'/'.$name.$ext;
  469. $filetotestsmall = $dolibarr_main_data_root.'/users/'.substr(sprintf('%08d', $obj->rowid), -1, 1).'/'.substr(sprintf('%08d', $obj->rowid), -2, 1).'/thumbs/'.$name.'_small'.$ext;
  470. $filetotestmini = $dolibarr_main_data_root.'/users/'.substr(sprintf('%08d', $obj->rowid), -1, 1).'/'.substr(sprintf('%08d', $obj->rowid), -2, 1).'/thumbs/'.$name.'_mini'.$ext;
  471. $exists = dol_is_file($filetotest);
  472. print 'Check user '.$obj->rowid.' lastname='.$obj->lastname.' firstname='.$obj->firstname.' photo='.$obj->photo.' file '.$filetotest." exists=".$exists."<br>\n";
  473. if ($exists) {
  474. $filetarget = $dolibarr_main_data_root.'/users/'.$obj->rowid.'/'.$name.$ext;
  475. $filetargetsmall = $dolibarr_main_data_root.'/users/'.$obj->rowid.'/thumbs/'.$name.'_small'.$ext;
  476. $filetargetmini = $dolibarr_main_data_root.'/users/'.$obj->rowid.'/thumbs/'.$name.'_mini'.$ext;
  477. $existt = dol_is_file($filetarget);
  478. if (!$existt) {
  479. if (GETPOST('restore_user_pictures', 'alpha') == 'confirmed') {
  480. dol_mkdir($dolibarr_main_data_root.'/users/'.$obj->rowid);
  481. }
  482. print " &nbsp; &nbsp; &nbsp; -> Copy file ".$filetotest." -> ".$filetarget."<br>\n";
  483. if (GETPOST('restore_user_pictures', 'alpha') == 'confirmed') {
  484. dol_copy($filetotest, $filetarget, '', 0);
  485. }
  486. }
  487. $existtt = dol_is_file($filetargetsmall);
  488. if (!$existtt) {
  489. if (GETPOST('restore_user_pictures', 'alpha') == 'confirmed') {
  490. dol_mkdir($dolibarr_main_data_root.'/users/'.$obj->rowid.'/thumbs');
  491. }
  492. print " &nbsp; &nbsp; &nbsp; -> Copy file ".$filetotestsmall." -> ".$filetargetsmall."<br>\n";
  493. if (GETPOST('restore_user_pictures', 'alpha') == 'confirmed') {
  494. dol_copy($filetotestsmall, $filetargetsmall, '', 0);
  495. }
  496. }
  497. $existtt = dol_is_file($filetargetmini);
  498. if (!$existtt) {
  499. if (GETPOST('restore_user_pictures', 'alpha') == 'confirmed') {
  500. dol_mkdir($dolibarr_main_data_root.'/users/'.$obj->rowid.'/thumbs');
  501. }
  502. print " &nbsp; &nbsp; &nbsp; -> Copy file ".$filetotestmini." -> ".$filetargetmini."<br>\n";
  503. if (GETPOST('restore_user_pictures', 'alpha') == 'confirmed') {
  504. dol_copy($filetotestmini, $filetargetmini, '', 0);
  505. }
  506. }
  507. }
  508. }
  509. $i++;
  510. }
  511. } else {
  512. $ok = 0;
  513. dol_print_error($db);
  514. }
  515. print '</td></tr>';
  516. }
  517. // rebuild_product_thumbs: Rebuild thumbs for product files
  518. if ($ok && GETPOST('rebuild_product_thumbs', 'alpha')) {
  519. $ext = '';
  520. global $maxwidthsmall, $maxheightsmall, $maxwidthmini, $maxheightmini;
  521. print '<tr><td colspan="2"><br>*** Rebuild product thumbs<br>';
  522. $sql = "SELECT s.rowid, s.ref FROM ".MAIN_DB_PREFIX."product as s ORDER BY s.ref";
  523. $resql = $db->query($sql);
  524. if ($resql) {
  525. $num = $db->num_rows($resql);
  526. $i = 0;
  527. while ($i < $num) {
  528. $obj = $db->fetch_object($resql);
  529. if (!empty($obj->ref)) {
  530. $files = dol_dir_list($dolibarr_main_data_root.'/produit/'.$obj->ref, 'files', 0);
  531. foreach ($files as $file) {
  532. // Generate thumbs.
  533. if (image_format_supported($file['fullname']) == 1) {
  534. $imgThumbSmall = 'notbuild';
  535. if (GETPOST('rebuild_product_thumbs', 'alpha') == 'confirmed') {
  536. // Used on logon for example
  537. $imgThumbSmall = vignette($file['fullname'], $maxwidthsmall, $maxheightsmall, '_small', 50, "thumbs");
  538. }
  539. print 'Check product '.$obj->rowid.", file ".$file['fullname']." -> ".$imgThumbSmall." maxwidthsmall=".$maxwidthsmall." maxheightsmall=".$maxheightsmall."<br>\n";
  540. $imgThumbMini = 'notbuild';
  541. if (GETPOST('rebuild_product_thumbs', 'alpha') == 'confirmed') {
  542. // Create mini thumbs for image (Ratio is near 16/9)
  543. // Used on menu or for setup page for example
  544. $imgThumbMini = vignette($file['fullname'], $maxwidthmini, $maxheightmini, '_mini', 50, "thumbs");
  545. }
  546. print 'Check product '.$obj->rowid.", file ".$file['fullname']." -> ".$imgThumbMini." maxwidthmini=".$maxwidthmini." maxheightmini=".$maxheightmini."<br>\n";
  547. }
  548. }
  549. }
  550. $i++;
  551. }
  552. } else {
  553. $ok = 0;
  554. dol_print_error($db);
  555. }
  556. print '</td></tr>';
  557. }
  558. // clean_linked_elements: Check and clean linked elements
  559. if ($ok && GETPOST('clean_linked_elements', 'alpha')) {
  560. print '<tr><td colspan="2"><br>*** Check table of linked elements and delete orphelins links</td></tr>';
  561. // propal => order
  562. print '<tr><td colspan="2">'.checkLinkedElements('propal', 'commande')."</td></tr>\n";
  563. // propal => invoice
  564. print '<tr><td colspan="2">'.checkLinkedElements('propal', 'facture')."</td></tr>\n";
  565. // order => invoice
  566. print '<tr><td colspan="2">'.checkLinkedElements('commande', 'facture')."</td></tr>\n";
  567. // order => shipping
  568. print '<tr><td colspan="2">'.checkLinkedElements('commande', 'shipping')."</td></tr>\n";
  569. // shipping => delivery
  570. print '<tr><td colspan="2">'.checkLinkedElements('shipping', 'delivery')."</td></tr>\n";
  571. // order_supplier => invoice_supplier
  572. print '<tr><td colspan="2">'.checkLinkedElements('order_supplier', 'invoice_supplier')."</td></tr>\n";
  573. }
  574. // clean_menus: Check orphelins menus
  575. if ($ok && GETPOST('clean_menus', 'alpha')) {
  576. print '<tr><td colspan="2"><br>*** Clean menu entries coming from disabled modules</td></tr>';
  577. $sql = "SELECT rowid, module";
  578. $sql .= " FROM ".MAIN_DB_PREFIX."menu as c";
  579. $sql .= " WHERE module IS NOT NULL AND module <> ''";
  580. $sql .= " ORDER BY module";
  581. $resql = $db->query($sql);
  582. if ($resql) {
  583. $num = $db->num_rows($resql);
  584. if ($num) {
  585. $i = 0;
  586. while ($i < $num) {
  587. $obj = $db->fetch_object($resql);
  588. $modulecond = $obj->module;
  589. $modulecondarray = explode('|', $obj->module); // Name of module
  590. print '<tr><td>';
  591. print $modulecond;
  592. $db->begin();
  593. if ($modulecond) { // And menu entry for module $modulecond was found in database.
  594. $moduleok = 0;
  595. foreach ($modulecondarray as $tmpname) {
  596. if ($tmpname == 'margins') {
  597. $tmpname = 'margin'; // TODO Remove this when normalized
  598. }
  599. $result = 0;
  600. if (!empty($conf->$tmpname)) {
  601. $result = $conf->$tmpname->enabled;
  602. }
  603. if ($result) {
  604. $moduleok++;
  605. }
  606. }
  607. if (!$moduleok && $modulecond) {
  608. print ' - Module condition '.$modulecond.' seems ko, we delete menu entry.';
  609. if (GETPOST('clean_menus') == 'confirmed') {
  610. $sql2 = "DELETE FROM ".MAIN_DB_PREFIX."menu WHERE module = '".$db->escape($modulecond)."'";
  611. $resql2 = $db->query($sql2);
  612. if (!$resql2) {
  613. $error++;
  614. dol_print_error($db);
  615. } else {
  616. print ' - <span class="warning">Cleaned</span>';
  617. }
  618. } else {
  619. print ' - <span class="warning">Canceled (test mode)</span>';
  620. }
  621. } else {
  622. print ' - Module condition '.$modulecond.' is ok, we do nothing.';
  623. }
  624. }
  625. if (!$error) {
  626. $db->commit();
  627. } else {
  628. $db->rollback();
  629. }
  630. print'</td></tr>';
  631. if ($error) {
  632. break;
  633. }
  634. $i++;
  635. }
  636. } else {
  637. print '<tr><td>No menu entries of disabled menus found</td></tr>';
  638. }
  639. } else {
  640. dol_print_error($db);
  641. }
  642. }
  643. // clean_orphelin_dir: Run purge of directory
  644. if ($ok && GETPOST('clean_orphelin_dir', 'alpha')) {
  645. $listmodulepart = array('company', 'invoice', 'invoice_supplier', 'propal', 'order', 'order_supplier', 'contract', 'tax');
  646. foreach ($listmodulepart as $modulepart) {
  647. $filearray = array();
  648. $upload_dir = isset($conf->$modulepart->dir_output) ? $conf->$modulepart->dir_output : '';
  649. if ($modulepart == 'company') {
  650. $upload_dir = $conf->societe->dir_output; // TODO change for multicompany sharing
  651. }
  652. if ($modulepart == 'invoice') {
  653. $upload_dir = $conf->facture->dir_output;
  654. }
  655. if ($modulepart == 'invoice_supplier') {
  656. $upload_dir = $conf->fournisseur->facture->dir_output;
  657. }
  658. if ($modulepart == 'order') {
  659. $upload_dir = $conf->commande->dir_output;
  660. }
  661. if ($modulepart == 'order_supplier') {
  662. $upload_dir = $conf->fournisseur->commande->dir_output;
  663. }
  664. if ($modulepart == 'contract') {
  665. $upload_dir = $conf->contrat->dir_output;
  666. }
  667. if (empty($upload_dir)) {
  668. continue;
  669. }
  670. print '<tr><td colspan="2"><br>*** Clean orphelins files into files '.$upload_dir.'</td></tr>';
  671. $filearray = dol_dir_list($upload_dir, "files", 1, '', array('^SPECIMEN\.pdf$', '^\.', '(\.meta|_preview.*\.png)$', '^temp$', '^payments$', '^CVS$', '^thumbs$'), '', SORT_DESC, 1, true);
  672. // To show ref or specific information according to view to show (defined by $module)
  673. if ($modulepart == 'company') {
  674. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  675. $object_instance = new Societe($db);
  676. }
  677. if ($modulepart == 'invoice') {
  678. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  679. $object_instance = new Facture($db);
  680. } elseif ($modulepart == 'invoice_supplier') {
  681. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  682. $object_instance = new FactureFournisseur($db);
  683. } elseif ($modulepart == 'propal') {
  684. include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  685. $object_instance = new Propal($db);
  686. } elseif ($modulepart == 'order') {
  687. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  688. $object_instance = new Commande($db);
  689. } elseif ($modulepart == 'order_supplier') {
  690. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  691. $object_instance = new CommandeFournisseur($db);
  692. } elseif ($modulepart == 'contract') {
  693. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  694. $object_instance = new Contrat($db);
  695. } elseif ($modulepart == 'tax') {
  696. include_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  697. $object_instance = new ChargeSociales($db);
  698. }
  699. foreach ($filearray as $key => $file) {
  700. if (!is_dir($file['name'])
  701. && $file['name'] != '.'
  702. && $file['name'] != '..'
  703. && $file['name'] != 'CVS'
  704. ) {
  705. // Define relative path used to store the file
  706. $relativefile = preg_replace('/'.preg_quote($upload_dir.'/', '/').'/', '', $file['fullname']);
  707. //var_dump($file);
  708. $id = 0; $ref = ''; $object_instance->id = 0; $object_instance->ref = ''; $label = '';
  709. // To show ref or specific information according to view to show (defined by $module)
  710. if ($modulepart == 'invoice') {
  711. preg_match('/(.*)\/[^\/]+$/', $relativefile, $reg); $ref = $reg[1];
  712. }
  713. if ($modulepart == 'invoice_supplier') {
  714. preg_match('/(\d+)\/[^\/]+$/', $relativefile, $reg); $id = empty($reg[1]) ? '' : $reg[1];
  715. }
  716. if ($modulepart == 'propal') {
  717. preg_match('/(.*)\/[^\/]+$/', $relativefile, $reg); $ref = $reg[1];
  718. }
  719. if ($modulepart == 'order') {
  720. preg_match('/(.*)\/[^\/]+$/', $relativefile, $reg); $ref = $reg[1];
  721. }
  722. if ($modulepart == 'order_supplier') {
  723. preg_match('/(.*)\/[^\/]+$/', $relativefile, $reg); $ref = $reg[1];
  724. }
  725. if ($modulepart == 'contract') {
  726. preg_match('/(.*)\/[^\/]+$/', $relativefile, $reg); $ref = $reg[1];
  727. }
  728. if ($modulepart == 'tax') {
  729. preg_match('/(\d+)\/[^\/]+$/', $relativefile, $reg); $id = $reg[1];
  730. }
  731. if ($id || $ref) {
  732. //print 'Fetch '.$id.' or '.$ref.'<br>';
  733. $result = $object_instance->fetch($id, $ref);
  734. //print $result.'<br>';
  735. if ($result == 0) { // Not found but no error
  736. // Clean of orphelins directories are done into repair.php
  737. print '<tr><td colspan="2">';
  738. print 'Delete orphelins file '.$file['fullname'].'<br>';
  739. if (GETPOST('clean_orphelin_dir', 'alpha') == 'confirmed') {
  740. dol_delete_file($file['fullname'], 1, 1, 1);
  741. dol_delete_dir(dirname($file['fullname']), 1);
  742. }
  743. print "</td></tr>";
  744. } elseif ($result < 0) {
  745. print 'Error in '.get_class($object_instance).'.fetch of id'.$id.' ref='.$ref.', result='.$result.'<br>';
  746. }
  747. }
  748. }
  749. }
  750. }
  751. }
  752. // clean_linked_elements: Check and clean linked elements
  753. if ($ok && GETPOST('clean_product_stock_batch', 'alpha')) {
  754. $methodtofix = GETPOST('methodtofix', 'alpha') ?GETPOST('methodtofix', 'alpha') : 'updatestock';
  755. print '<tr><td colspan="2"><br>*** Clean table product_batch, methodtofix='.$methodtofix.' (possible values: updatestock or updatebatch)</td></tr>';
  756. $sql = "SELECT p.rowid, p.ref, p.tobatch, ps.rowid as psrowid, ps.fk_entrepot, ps.reel, SUM(pb.qty) as reelbatch";
  757. $sql .= " FROM ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."product_stock as ps LEFT JOIN ".MAIN_DB_PREFIX."product_batch as pb ON ps.rowid = pb.fk_product_stock";
  758. $sql .= " WHERE p.rowid = ps.fk_product";
  759. $sql .= " GROUP BY p.rowid, p.ref, p.tobatch, ps.rowid, ps.fk_entrepot, ps.reel";
  760. $sql .= " HAVING (SUM(pb.qty) IS NOT NULL AND reel != SUM(pb.qty)) OR (SUM(pb.qty) IS NULL AND p.tobatch > 0)";
  761. print $sql;
  762. $resql = $db->query($sql);
  763. if ($resql) {
  764. $num = $db->num_rows($resql);
  765. if ($num) {
  766. $i = 0;
  767. while ($i < $num) {
  768. $obj = $db->fetch_object($resql);
  769. print '<tr><td>Product '.$obj->rowid.'-'.$obj->ref.' in warehouse id='.$obj->fk_entrepot.' -> product_stock.id='.$obj->psrowid.': '.$obj->reel.' (product_stock.reel) != '.($obj->reelbatch ? $obj->reelbatch : '0').' (sum product_batch)';
  770. // Fix is required
  771. if ($obj->reel != $obj->reelbatch) {
  772. if (empty($obj->tobatch)) {
  773. // If product is not a product that support batches, we can clean stock by deleting the product batch lines
  774. print ' -> Delete qty '.$obj->reelbatch.' for any lot linked to fk_product_stock='.$obj->psrowid;
  775. $sql2 = "DELETE FROM ".MAIN_DB_PREFIX."product_batch";
  776. $sql2 .= " WHERE fk_product_stock = ".((int) $obj->psrowid);
  777. print '<br>'.$sql2;
  778. if (GETPOST('clean_product_stock_batch') == 'confirmed') {
  779. $resql2 = $db->query($sql2);
  780. if (!$resql2) {
  781. $error++;
  782. dol_print_error($db);
  783. }
  784. }
  785. } else {
  786. if ($methodtofix == 'updatebatch') {
  787. // Method 1
  788. print ' -> Insert qty '.($obj->reel - $obj->reelbatch).' with lot 000000 linked to fk_product_stock='.$obj->psrowid;
  789. $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."product_batch(fk_product_stock, batch, qty)";
  790. $sql2 .= "VALUES(".((int) $obj->psrowid).", '000000', ".((float) ($obj->reel - $obj->reelbatch)).")";
  791. print '<br>'.$sql2;
  792. if (GETPOST('clean_product_stock_batch') == 'confirmed') {
  793. $resql2 = $db->query($sql2);
  794. if (!$resql2) {
  795. // TODO If it fails, we must make update
  796. //$sql2 ="UPDATE ".MAIN_DB_PREFIX."product_batch";
  797. //$sql2.=" SET ".$obj->psrowid.", '000000', ".($obj->reel - $obj->reelbatch).")";
  798. //$sql2.=" WHERE fk_product_stock = ".((int) $obj->psrowid)
  799. }
  800. }
  801. }
  802. if ($methodtofix == 'updatestock') {
  803. // Method 2
  804. print ' -> Update qty of product_stock with qty = '.($obj->reelbatch ? ((float) $obj->reelbatch) : '0').' for ps.rowid = '.((int) $obj->psrowid);
  805. $sql2 = "UPDATE ".MAIN_DB_PREFIX."product_stock";
  806. $sql2 .= " SET reel = ".($obj->reelbatch ? ((float) $obj->reelbatch) : '0')." WHERE rowid = ".((int) $obj->psrowid);
  807. print '<br>'.$sql2;
  808. if (GETPOST('clean_product_stock_batch') == 'confirmed') {
  809. $error = 0;
  810. $db->begin();
  811. $resql2 = $db->query($sql2);
  812. if ($resql2) {
  813. // We update product_stock, so we must fill p.stock into product too.
  814. $sql3 = 'UPDATE '.MAIN_DB_PREFIX.'product p SET p.stock= (SELECT SUM(ps.reel) FROM '.MAIN_DB_PREFIX.'product_stock ps WHERE ps.fk_product = p.rowid)';
  815. $resql3 = $db->query($sql3);
  816. if (!$resql3) {
  817. $error++;
  818. dol_print_error($db);
  819. }
  820. } else {
  821. $error++;
  822. dol_print_error($db);
  823. }
  824. if (!$error) {
  825. $db->commit();
  826. } else {
  827. $db->rollback();
  828. }
  829. }
  830. }
  831. }
  832. }
  833. print'</td></tr>';
  834. $i++;
  835. }
  836. } else {
  837. print '<tr><td colspan="2">Nothing to do</td></tr>';
  838. }
  839. } else {
  840. dol_print_error($db);
  841. }
  842. }
  843. // clean_product_stock_negative_if_batch
  844. if ($ok && GETPOST('clean_product_stock_negative_if_batch', 'alpha')) {
  845. print '<tr><td colspan="2"><br>Clean table product_batch, methodtofix='.$methodtofix.' (possible values: updatestock or updatebatch)</td></tr>';
  846. $sql = "SELECT p.rowid, p.ref, p.tobatch, ps.rowid as psrowid, ps.fk_entrepot, ps.reel, SUM(pb.qty) as reelbatch";
  847. $sql .= " FROM ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."product_stock as ps, ".MAIN_DB_PREFIX."product_batch as pb";
  848. $sql .= " WHERE p.rowid = ps.fk_product AND ps.rowid = pb.fk_product_stock";
  849. $sql .= " AND p.tobatch > 0";
  850. $sql .= " GROUP BY p.rowid, p.ref, p.tobatch, ps.rowid, ps.fk_entrepot, ps.reel";
  851. $sql .= " HAVING reel != SUM(pb.qty)";
  852. $resql = $db->query($sql);
  853. if ($resql) {
  854. $num = $db->num_rows($resql);
  855. if ($num) {
  856. $i = 0;
  857. while ($i < $num) {
  858. $obj = $db->fetch_object($resql);
  859. print '<tr><td>'.$obj->rowid.'-'.$obj->ref.'-'.$obj->fk_entrepot.' -> '.$obj->psrowid.': '.$obj->reel.' != '.$obj->reelbatch;
  860. // TODO
  861. }
  862. }
  863. }
  864. }
  865. // set_empty_time_spent_amount
  866. if ($ok && GETPOST('set_empty_time_spent_amount', 'alpha')) {
  867. print '<tr><td colspan="2"><br>*** Set value of time spent without amount</td></tr>';
  868. $sql = "SELECT COUNT(ptt.rowid) as nb, u.rowid as user_id, u.login, u.thm as user_thm";
  869. $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as ptt, ".MAIN_DB_PREFIX."user as u";
  870. $sql .= " WHERE ptt.fk_user = u.rowid";
  871. $sql .= " AND ptt.thm IS NULL and u.thm > 0";
  872. $sql .= " GROUP BY u.rowid, u.login, u.thm";
  873. $resql = $db->query($sql);
  874. if ($resql) {
  875. $num = $db->num_rows($resql);
  876. if ($num) {
  877. $i = 0;
  878. while ($i < $num) {
  879. $obj = $db->fetch_object($resql);
  880. print '<tr><td>'.$obj->login.'-'.$obj->user_id.' ('.$obj->nb.' lines to fix) -> '.$obj->user_thm;
  881. $db->begin();
  882. if (GETPOST('set_empty_time_spent_amount') == 'confirmed') {
  883. $sql2 = "UPDATE ".MAIN_DB_PREFIX."projet_task_time";
  884. $sql2 .= " SET thm = ".$obj->user_thm." WHERE thm IS NULL AND fk_user = ".((int) $obj->user_id);
  885. $resql2 = $db->query($sql2);
  886. if (!$resql2) {
  887. $error++;
  888. dol_print_error($db);
  889. }
  890. }
  891. if (!$error) {
  892. $db->commit();
  893. } else {
  894. $db->rollback();
  895. }
  896. print'</td></tr>';
  897. if ($error) {
  898. break;
  899. }
  900. $i++;
  901. }
  902. } else {
  903. print '<tr><td>No time spent with empty line on users with a hourly rate defined</td></tr>';
  904. }
  905. } else {
  906. dol_print_error($db);
  907. }
  908. }
  909. // force_disable_of_modules_not_found
  910. if ($ok && GETPOST('force_disable_of_modules_not_found', 'alpha')) {
  911. print '<tr><td colspan="2"><br>*** Force modules not found physicaly to be disabled (only modules adding js, css or hooks can be detected as removed physicaly)</td></tr>';
  912. $arraylistofkey = array('hooks', 'js', 'css');
  913. foreach ($arraylistofkey as $key) {
  914. $sql = "SELECT DISTINCT name, value";
  915. $sql .= " FROM ".MAIN_DB_PREFIX."const as c";
  916. $sql .= " WHERE name LIKE 'MAIN_MODULE_%_".strtoupper($key)."'";
  917. $sql .= " ORDER BY name";
  918. $resql = $db->query($sql);
  919. if ($resql) {
  920. $num = $db->num_rows($resql);
  921. if ($num) {
  922. $i = 0;
  923. while ($i < $num) {
  924. $obj = $db->fetch_object($resql);
  925. $constantname = $obj->name; // Name of constant for hook or js or css declaration
  926. print '<tr><td>';
  927. print dol_escape_htmltag($constantname);
  928. $db->begin();
  929. $reg = array();
  930. if (preg_match('/MAIN_MODULE_(.*)_'.strtoupper($key).'/i', $constantname, $reg)) {
  931. $name = strtolower($reg[1]);
  932. if ($name) { // An entry for key $key and module $name was found in database.
  933. $reloffile = '';
  934. $result = 'found';
  935. if ($key == 'hooks') {
  936. $reloffile = $name.'/class/actions_'.$name.'.class.php';
  937. }
  938. if ($key == 'js') {
  939. $value = $obj->value;
  940. $valuearray = json_decode($value);
  941. $reloffile = $valuearray[0];
  942. $reloffile = preg_replace('/^\//', '', $valuearray[0]);
  943. }
  944. if ($key == 'css') {
  945. $value = $obj->value;
  946. $valuearray = json_decode($value);
  947. if ($value && (!is_array($valuearray) || count($valuearray) == 0)) {
  948. $valuearray = array();
  949. $valuearray[0] = $value; // If value was not a json array but a string
  950. }
  951. $reloffile = preg_replace('/^\//', '', $valuearray[0]);
  952. }
  953. if ($reloffile) {
  954. //var_dump($key.' - '.$value.' - '.$reloffile);
  955. try {
  956. $result = dol_buildpath($reloffile, 0, 2);
  957. } catch (Exception $e) {
  958. $result = 'found'; // If error, we force like if we found to avoid any deletion
  959. }
  960. } else {
  961. $result = 'found'; //
  962. }
  963. if (!$result) {
  964. print ' - File of '.$key.' ('.$reloffile.') NOT found, we disable the module.';
  965. if (GETPOST('force_disable_of_modules_not_found') == 'confirmed') {
  966. $sql2 = "DELETE FROM ".MAIN_DB_PREFIX."const WHERE name = 'MAIN_MODULE_".strtoupper($name)."_".strtoupper($key)."'";
  967. $resql2 = $db->query($sql2);
  968. if (!$resql2) {
  969. $error++;
  970. dol_print_error($db);
  971. }
  972. $sql3 = "DELETE FROM ".MAIN_DB_PREFIX."const WHERE name = 'MAIN_MODULE_".strtoupper($name)."'";
  973. $resql3 = $db->query($sql3);
  974. if (!$resql3) {
  975. $error++;
  976. dol_print_error($db);
  977. } else {
  978. print ' - <span class="warning">Cleaned</span>';
  979. }
  980. } else {
  981. print ' - <span class="warning">Canceled (test mode)</span>';
  982. }
  983. } else {
  984. print ' - File of '.$key.' ('.$reloffile.') found, we do nothing.';
  985. }
  986. }
  987. if (!$error) {
  988. $db->commit();
  989. } else {
  990. $db->rollback();
  991. }
  992. }
  993. print'</td></tr>';
  994. if ($error) {
  995. break;
  996. }
  997. $i++;
  998. }
  999. } else {
  1000. print '<tr><td>No active module with missing files found by searching on MAIN_MODULE_(.*)_'.strtoupper($key).'</td></tr>';
  1001. }
  1002. } else {
  1003. dol_print_error($db);
  1004. }
  1005. }
  1006. }
  1007. // clean_old_module_entries: Clean data into const when files of module were removed without being
  1008. if ($ok && GETPOST('clean_perm_table', 'alpha')) {
  1009. print '<tr><td colspan="2"><br>*** Clean table user_rights from lines of external modules no more enabled</td></tr>';
  1010. $listofmods = '';
  1011. foreach ($conf->modules as $key => $val) {
  1012. $listofmods .= ($listofmods ? ',' : '')."'".$db->escape($val)."'";
  1013. }
  1014. $sql = "SELECT id, libelle as label, module from ".MAIN_DB_PREFIX."rights_def WHERE module NOT IN (".$db->sanitize($listofmods, 1).") AND id > 100000";
  1015. $resql = $db->query($sql);
  1016. if ($resql) {
  1017. $num = $db->num_rows($resql);
  1018. if ($num) {
  1019. $i = 0;
  1020. while ($i < $num) {
  1021. $obj = $db->fetch_object($resql);
  1022. if ($obj->id > 0) {
  1023. print '<tr><td>Found line with id '.$obj->id.', label "'.$obj->label.'" of module "'.$obj->module.'" to delete';
  1024. if (GETPOST('clean_perm_table', 'alpha') == 'confirmed') {
  1025. $sqldelete = "DELETE FROM ".MAIN_DB_PREFIX."rights_def WHERE id = ".((int) $obj->id);
  1026. $resqldelete = $db->query($sqldelete);
  1027. if (!$resqldelete) {
  1028. dol_print_error($db);
  1029. }
  1030. print ' - deleted';
  1031. }
  1032. print '</td></tr>';
  1033. }
  1034. $i++;
  1035. }
  1036. } else {
  1037. print '<tr><td>No lines of a disabled external module (with id > 100000) found into table rights_def</td></tr>';
  1038. }
  1039. } else {
  1040. dol_print_error($db);
  1041. }
  1042. }
  1043. // force utf8 on tables
  1044. if ($ok && GETPOST('force_utf8_on_tables', 'alpha')) {
  1045. print '<tr><td colspan="2"><br>*** Force page code and collation of tables into utf8/utf8_unicode_ci and row_format=dynamic (for mysql/mariadb only)</td></tr>';
  1046. if ($db->type == "mysql" || $db->type == "mysqli") {
  1047. $force_utf8_on_tables = GETPOST('force_utf8_on_tables', 'alpha');
  1048. $listoftables = $db->DDLListTables($db->database_name);
  1049. // Disable foreign key checking for avoid errors
  1050. if ($force_utf8_on_tables == 'confirmed') {
  1051. $sql = 'SET FOREIGN_KEY_CHECKS=0';
  1052. print '<!-- '.$sql.' -->';
  1053. $resql = $db->query($sql);
  1054. }
  1055. foreach ($listoftables as $table) {
  1056. // do not convert llx_const if mysql encrypt/decrypt is used
  1057. if ($conf->db->dolibarr_main_db_encryption != 0 && preg_match('/\_const$/', $table)) {
  1058. continue;
  1059. }
  1060. print '<tr><td colspan="2">';
  1061. print $table;
  1062. $sql1 = "ALTER TABLE ".$table." ROW_FORMAT=dynamic";
  1063. $sql2 = "ALTER TABLE ".$table." CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
  1064. print '<!-- '.$sql1.' -->';
  1065. print '<!-- '.$sql2.' -->';
  1066. if ($force_utf8_on_tables == 'confirmed') {
  1067. $resql1 = $db->query($sql1);
  1068. if ($resql1) {
  1069. $resql2 = $db->query($sql2);
  1070. } else {
  1071. $resql2 = false;
  1072. }
  1073. print ' - Done ('.(($resql1 && $resql2) ? 'OK' : 'KO').')';
  1074. } else {
  1075. print ' - Disabled';
  1076. }
  1077. print '</td></tr>';
  1078. }
  1079. // Enable foreign key checking
  1080. if ($force_utf8_on_tables == 'confirmed') {
  1081. $sql = 'SET FOREIGN_KEY_CHECKS=1';
  1082. print '<!-- '.$sql.' -->';
  1083. $resql = $db->query($sql);
  1084. }
  1085. } else {
  1086. print '<tr><td colspan="2">Not available with database type '.$db->type.'</td></tr>';
  1087. }
  1088. }
  1089. // force utf8mb4 on tables EXPERIMENTAL !
  1090. if ($ok && GETPOST('force_utf8mb4_on_tables', 'alpha')) {
  1091. print '<tr><td colspan="2"><br>*** Force page code and collation of tables into utf8mb4/utf8mb4_unicode_ci (for mysql/mariadb only)</td></tr>';
  1092. if ($db->type == "mysql" || $db->type == "mysqli") {
  1093. $force_utf8mb4_on_tables = GETPOST('force_utf8mb4_on_tables', 'alpha');
  1094. $listoftables = $db->DDLListTables($db->database_name);
  1095. // Disable foreign key checking for avoid errors
  1096. if ($force_utf8mb4_on_tables == 'confirmed') {
  1097. $sql = 'SET FOREIGN_KEY_CHECKS=0';
  1098. print '<!-- '.$sql.' -->';
  1099. $resql = $db->query($sql);
  1100. }
  1101. foreach ($listoftables as $table) {
  1102. // do not convert llx_const if mysql encrypt/decrypt is used
  1103. if ($conf->db->dolibarr_main_db_encryption != 0 && preg_match('/\_const$/', $table)) {
  1104. continue;
  1105. }
  1106. print '<tr><td colspan="2">';
  1107. print $table;
  1108. $sql1 = "ALTER TABLE ".$table." ROW_FORMAT=dynamic";
  1109. $sql2 = "ALTER TABLE ".$table." CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
  1110. print '<!-- '.$sql1.' -->';
  1111. print '<!-- '.$sql2.' -->';
  1112. if ($force_utf8mb4_on_tables == 'confirmed') {
  1113. $resql1 = $db->query($sql1);
  1114. if ($resql1) {
  1115. $resql2 = $db->query($sql2);
  1116. } else {
  1117. $resql2 = false;
  1118. }
  1119. print ' - Done ('.(($resql1 && $resql2) ? 'OK' : 'KO').')';
  1120. } else {
  1121. print ' - Disabled';
  1122. }
  1123. print '</td></tr>';
  1124. flush();
  1125. ob_flush();
  1126. }
  1127. // Enable foreign key checking
  1128. if ($force_utf8mb4_on_tables == 'confirmed') {
  1129. $sql = 'SET FOREIGN_KEY_CHECKS=1';
  1130. print '<!-- '.$sql.' -->';
  1131. $resql = $db->query($sql);
  1132. }
  1133. } else {
  1134. print '<tr><td colspan="2">Not available with database type '.$db->type.'</td></tr>';
  1135. }
  1136. }
  1137. // rebuild sequences for pgsql
  1138. if ($ok && GETPOST('rebuild_sequences', 'alpha')) {
  1139. print '<tr><td colspan="2"><br>*** Force to rebuild sequences (for postgresql only)</td></tr>';
  1140. if ($db->type == "pgsql") {
  1141. $rebuild_sequence = GETPOST('rebuild_sequences', 'alpha');
  1142. if ($rebuild_sequence == 'confirmed') {
  1143. $sql = "SELECT dol_util_rebuild_sequences();";
  1144. print '<!-- '.$sql.' -->';
  1145. $resql = $db->query($sql);
  1146. }
  1147. } else {
  1148. print '<tr><td colspan="2">Not available with database type '.$db->type.'</td></tr>';
  1149. }
  1150. }
  1151. //
  1152. if ($ok && GETPOST('repair_link_dispatch_lines_supplier_order_lines')) {
  1153. /*
  1154. * This script is meant to be run when upgrading from a dolibarr version < 3.8
  1155. * to a newer version.
  1156. *
  1157. * Version 3.8 introduces a new column in llx_commande_fournisseur_dispatch, which
  1158. * matches the dispatch to a specific supplier order line (so that if there are
  1159. * several with the same product, the user can specifically tell which products of
  1160. * which line were dispatched where).
  1161. *
  1162. * However when migrating, the new column has a default value of 0, which means that
  1163. * old supplier orders whose lines were dispatched using the old dolibarr version
  1164. * have unspecific dispatch lines, which are not taken into account by the new version,
  1165. * thus making the order look like it was never dispatched at all.
  1166. *
  1167. * This scripts sets this foreign key to the first matching supplier order line whose
  1168. * product (and supplier order of course) are the same as the dispatch’s.
  1169. *
  1170. * If the dispatched quantity is more than indicated on the order line (this happens if
  1171. * there are several order lines for the same product), it creates new dispatch lines
  1172. * pointing to the other order lines accordingly, until all the dispatched quantity is
  1173. * accounted for.
  1174. */
  1175. $repair_link_dispatch_lines_supplier_order_lines = GETPOST('repair_link_dispatch_lines_supplier_order_lines', 'alpha');
  1176. echo '<tr><th>Repair llx_commande_fournisseur_dispatch.fk_commandefourndet</th></tr>';
  1177. echo '<tr><td>Repair in progress. This may take a while.</td></tr>';
  1178. $sql_dispatch = 'SELECT * FROM '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch WHERE COALESCE(fk_commandefourndet, 0) = 0';
  1179. $db->begin();
  1180. $resql_dispatch = $db->query($sql_dispatch);
  1181. $n_processed_rows = 0;
  1182. $errors = array();
  1183. if ($resql_dispatch) {
  1184. if ($db->num_rows($resql_dispatch) == 0) {
  1185. echo '<tr><td>Nothing to do.</td></tr>';
  1186. exit;
  1187. }
  1188. while ($obj_dispatch = $db->fetch_object($resql_dispatch)) {
  1189. $sql_line = 'SELECT line.rowid, line.qty FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet AS line';
  1190. $sql_line .= ' WHERE line.fk_commande = '.((int) $obj_dispatch->fk_commande);
  1191. $sql_line .= ' AND line.fk_product = '.((int) $obj_dispatch->fk_product);
  1192. $resql_line = $db->query($sql_line);
  1193. // s’il y a plusieurs lignes avec le même produit sur cette commande fournisseur,
  1194. // on divise la ligne de dispatch en autant de lignes qu’on en a sur la commande pour le produit
  1195. // et on met la quantité de la ligne dans la limite du "budget" indiqué par dispatch.qty
  1196. $remaining_qty = $obj_dispatch->qty;
  1197. $first_iteration = true;
  1198. if (!$resql_line) {
  1199. echo '<tr><td>Unable to find a matching supplier order line for dispatch #'.$obj_dispatch->rowid.'</td></tr>';
  1200. $errors[] = $sql_line;
  1201. $n_processed_rows++;
  1202. continue;
  1203. }
  1204. if ($db->num_rows($resql_line) == 0) {
  1205. continue;
  1206. }
  1207. while ($obj_line = $db->fetch_object($resql_line)) {
  1208. if (!$remaining_qty) {
  1209. break;
  1210. }
  1211. if (!$obj_line->rowid) {
  1212. continue;
  1213. }
  1214. $qty_for_line = min($remaining_qty, $obj_line->qty);
  1215. if ($first_iteration) {
  1216. $sql_attach = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch';
  1217. $sql_attach .= ' SET fk_commandefourndet = '.((int) $obj_line->rowid).', qty = '.((float) $qty_for_line);
  1218. $sql_attach .= ' WHERE rowid = '.((int) $obj_dispatch->rowid);
  1219. $first_iteration = false;
  1220. } else {
  1221. $sql_attach_values = array(
  1222. ((int) $obj_dispatch->fk_commande),
  1223. ((int) $obj_dispatch->fk_product),
  1224. ((int) $obj_line->rowid),
  1225. ((float) $qty_for_line),
  1226. ((int) $obj_dispatch->fk_entrepot),
  1227. ((int) $obj_dispatch->fk_user),
  1228. $obj_dispatch->datec ? "'".$db->idate($db->jdate($obj_dispatch->datec))."'" : 'NULL',
  1229. $obj_dispatch->comment ? "'".$db->escape($obj_dispatch->comment)."'" : 'NULL',
  1230. $obj_dispatch->status ? ((int) $obj_dispatch->status) : 'NULL',
  1231. $obj_dispatch->tms ? "'".$db->idate($db->jdate($obj_dispatch->tms))."'" : 'NULL',
  1232. $obj_dispatch->batch ? "'".$db->escape($obj_dispatch->batch)."'" : 'NULL',
  1233. $obj_dispatch->eatby ? "'".$db->escape($obj_dispatch->eatby)."'" : 'NULL',
  1234. $obj_dispatch->sellby ? "'".$db->escape($obj_dispatch->sellby)."'" : 'NULL'
  1235. );
  1236. $sql_attach_values = join(', ', $sql_attach_values);
  1237. $sql_attach = 'INSERT INTO '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch';
  1238. $sql_attach .= ' (fk_commande, fk_product, fk_commandefourndet, qty, fk_entrepot, fk_user, datec, comment, status, tms, batch, eatby, sellby)';
  1239. $sql_attach .= " VALUES (".$sql_attach_values.")";
  1240. }
  1241. if ($repair_link_dispatch_lines_supplier_order_lines == 'confirmed') {
  1242. $resql_attach = $db->query($sql_attach);
  1243. } else {
  1244. $resql_attach = true; // Force success in test mode
  1245. }
  1246. if ($resql_attach) {
  1247. $remaining_qty -= $qty_for_line;
  1248. } else {
  1249. $errors[] = $sql_attach;
  1250. }
  1251. $first_iteration = false;
  1252. }
  1253. $n_processed_rows++;
  1254. // report progress every 256th row
  1255. if (!($n_processed_rows & 0xff)) {
  1256. echo '<tr><td>Processed '.$n_processed_rows.' rows with '.count($errors).' errors…'."</td></tr>\n";
  1257. flush();
  1258. ob_flush();
  1259. }
  1260. }
  1261. } else {
  1262. echo '<tr><td>Unable to find any dispatch without an fk_commandefourndet.'."</td></tr>\n";
  1263. echo $sql_dispatch."\n";
  1264. }
  1265. echo '<tr><td>Fixed '.$n_processed_rows.' rows with '.count($errors).' errors…'."</td></tr>\n";
  1266. echo '<tr><td>DONE.'."</td></tr>\n";
  1267. if (count($errors)) {
  1268. $db->rollback();
  1269. echo '<tr><td>The transaction was rolled back due to errors: nothing was changed by the script.</td></tr>';
  1270. } else {
  1271. $db->commit();
  1272. }
  1273. $db->close();
  1274. echo '<tr><td><h3>SQL queries with errors:</h3></tr></td>';
  1275. echo '<tr><td>'.join('</td></tr><tr><td>', $errors).'</td></tr>';
  1276. }
  1277. print '</table>';
  1278. if (empty($actiondone)) {
  1279. print '<div class="error">'.$langs->trans("ErrorWrongParameters").'</div>';
  1280. }
  1281. if ($oneoptionset) {
  1282. print '<div class="center" style="padding-top: 10px"><a href="../index.php?mainmenu=home&leftmenu=home'.(GETPOSTISSET("login") ? '&username='.urlencode(GETPOST("login")) : '').'">';
  1283. print $langs->trans("GoToDolibarr");
  1284. print '</a></div>';
  1285. } else {
  1286. print '<div class="center warning" style="padding-top: 10px">';
  1287. print $langs->trans("SetAtLeastOneOptionAsUrlParameter");
  1288. print '</div>';
  1289. }
  1290. dolibarr_install_syslog("--- repair: end");
  1291. pFooter(1, $setuplang);
  1292. if ($db->connected) {
  1293. $db->close();
  1294. }
  1295. // Return code if ran from command line
  1296. if (!$ok && isset($argv[1])) {
  1297. exit(1);
  1298. }