+ // set proper default table encoding for current user language (used on binary->text conversion)
+ db_query("ALTER TABLE `$table` CHARSET $new_encoding");
+ $csql = "SHOW COLUMNS FROM $table";
+ $cresult = db_query($csql, "Cannot select column names for table '$table'");
+ $convert = false;
+
+ $to_binary = $to_default = $to_utf = array();
+ while($col = db_fetch($cresult)) {
+
+ $bintype = strtr($col['Type'], array('varchar' => 'varbinary', 'char'=>'varbinary', 'text'=>'blob', 'tinytext'=>'tinyblob'));
+
+ if ($bintype != $col['Type'])
+ { // this is char/text column, so change encoding to proper encoding
+ if ($dbg)
+ $this->log_error(sprintf('%s switched to uft8.', $table.'.'.$col['Field']), 'Debug');
+
+ $null = $col['Null'] === 'YES' ? ' NULL ' : ' NOT NULL ';
+ $default = $col['Null'] !== 'YES' && isset($col['Default']) ? ' DEFAULT '.db_escape($col['Default']) : '';
+
+ // to avoid column width multiplication x3 we old->binary->ui->utf column type change instead of column CONVERT
+
+ $to_binary[] = "CHANGE `".$col['Field']."` `".$col['Field']."` ".$bintype;
+ $to_default[] = "CHANGE `".$col['Field']."` `".$col['Field']."` ".$col['Type'].$null.$default;
+ $to_utf[] = "MODIFY COLUMN `".$col['Field']."` ".$col['Type']." COLLATE ".$collation.$null.$default;
+ $convert = true;
+ }
+ }
+ if(count($to_binary))
+ {
+ $sql = "ALTER TABLE `$table` ".implode(',',$to_binary);
+ db_query($sql);
+ $sql = "ALTER TABLE `$table` ".implode(',',$to_default);
+ db_query($sql);
+ $sql = "ALTER TABLE `$table` ".implode(',',$to_utf);
+ db_query($sql);
+ }
+ db_query("ALTER TABLE `$table` COLLATE $collation");
+ }
+ db_query("ALTER DATABASE COLLATE $collation");
+ $this->log_error(_('Convertion to utf8 done.'), 'Info');
+
+ return true;
+ }
+
+ function update_grn_rates()
+ {
+ $sql = "SELECT grn.id, grn.delivery_date, supp.curr_code
+ FROM ".TB_PREF."grn_batch grn, ".TB_PREF."suppliers supp
+ WHERE supp.supplier_id=grn.supplier_id AND supp.curr_code!='".get_company_pref('curr_default')."'";
+ $result = db_query($sql);
+
+ if (!$result)
+ return false;
+
+ $sql = "UPDATE ".TB_PREF."grn_batch SET rate=%s WHERE id=%d";
+ while ($grn = db_fetch($result))
+ db_query(sprintf($sql, get_exchange_rate_from_home_currency($grn['curr_code'], sql2date($grn['delivery_date'])), $grn['id']));
+
+ return true;