2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 var $version = '2.4.0'; // version installed
15 var $sql = 'alter2.4.sql';
19 $this->description = _('Upgrade from version 2.3 to 2.4');
23 // Install procedure. All additional changes
24 // not included in sql file should go here.
26 function install($company, $force=false)
28 global $db_version, $db_connections;
30 $pref = $db_connections[$company]['tbpref'];
32 if (get_company_pref('grn_clearing_act') === null) { // available form 2.3.1, can be not defined on pre-2.4 installations
33 set_company_pref('grn_clearing_act', 'glsetup.purchase', 'varchar', 15, 0);
35 if (get_company_pref('default_receival_required') === null) { // new in 2.4 installations
36 set_company_pref('default_receival_required', 'glsetup.purchase', 'smallint', 6, 10);
38 $result = $this->update_workorders() && $this->switch_database_to_utf($pref);
40 $result = $this->do_cleanup();
42 return update_company_prefs(array('version_id'=>$db_version));
45 // Checking before install
47 function pre_check($pref, $force)
53 // optional procedure done after upgrade fail, before backup is restored
55 function post_fail($pref)
57 db_query("DROP TABLE IF EXISTS " . $pref . 'wo_costing');
60 // Test if patch was applied before.
62 function installed($pref)
64 $n = 2; // number of patches to be installed
67 if (!check_table($pref, 'suppliers', 'tax_algorithm')) $patchcnt++;
68 if (!check_table($pref, 'wo_costing')) $patchcnt++;
69 return $n == $patchcnt ? true : ($patchcnt ? ($patchcnt.'/'. $n) : 0);
72 function update_workorders()
76 $sql = "SELECT DISTINCT type, type_no, tran_date, person_id FROM ".TB_PREF."gl_trans WHERE `type`=".ST_WORKORDER
77 ." AND person_type_id=1";
78 $res = db_query($sql);
81 display_error("Cannot update work orders costs"
82 .':<br>'. db_error_msg($db));
85 while ($row = db_fetch($res))
87 $journal_id = get_next_trans_no(ST_JOURNAL);
89 $sql1 = "UPDATE ".TB_PREF."gl_trans SET `type`=".ST_JOURNAL.", type_no={$journal_id},
90 person_type_id=NULL, person_id=0
91 WHERE `type`=".ST_WORKORDER." AND type_no={$row['type_no']} AND tran_date='{$row['tran_date']}'
92 AND person_id='{$row['person_id']}'";
93 if (!db_query($sql1)) return false;
95 $sql2 = "INSERT INTO ".TB_PREF."wo_costing (workorder_id, cost_type, trans_no)
96 VALUES ({$row['type_no']}, {$row['person_id']}, {$journal_id})";
97 if (!db_query($sql2)) return false;
103 In previous versions FA ignored encoding settings on database/tables, so it depended on server settings,
104 but data stored is encoded in user language encoding. Now we switch to utf8 internal database encoding, while
105 user encoding can be selected independently.
107 To perform safe FA database switch to utf-8 encoding we have to first ensure that all text/char columns
108 have properly set encoding (the same as its content), so the algorithm performed on every table is as follows:
109 . set default table encoding for the table to currently used on client side;
110 . for all text/char column:
111 - suppress autorecoding by change of the type to related binary/blob type
112 - change column to utf8 encodding and selected collation.
113 . change default table encoding to utf8
115 function switch_database_to_utf($pref, $test = false) {
117 global $installed_languages, $dflt_lang;
119 $old_encoding = 'latin1'; // default client encoding
121 // site default encoding is presumed as encoding for all databases!
122 $lang = array_search_value($dflt_lang, $installed_languages, 'code');
123 $new_encoding = get_mysql_encoding_name(strtoupper($lang['encoding']));
126 error_log('Switching database to utf8 encoding from '.$old_encoding);
127 $collation = get_mysql_collation();
128 $tsql = "SHOW TABLES LIKE '".($pref=='' ? '' : substr($pref, 0, -1).'\\_')."%'";
129 $tresult = db_query($tsql, "Cannot select all tables with prefix '$pref'");
130 while($tbl = db_fetch($tresult)) {
132 // if ($table != '1_chart_master') continue; _vd($table); get_usec(); // fast debug on single table
134 db_query("ALTER TABLE `$table` CONVERT TO CHARACTER SET $old_encoding"); // convert encoding on utf-8 tables
136 // set proper default table encoding for current user language (used on binary->text conversion)
137 db_query("ALTER TABLE `$table` CHARSET $new_encoding");
138 $csql = "SHOW COLUMNS FROM $table";
139 $cresult = db_query($csql, "Cannot select column names for table '$table'");
142 $to_binary = $to_default = $to_utf = array();
143 while($col = db_fetch($cresult)) {
145 $bintype = strtr($col['Type'], array('varchar' => 'varbinary', 'char'=>'varbinary', 'text'=>'blob', 'tinytext'=>'tinyblob'));
147 if ($bintype != $col['Type'])
148 { // this is char/text column, so change encoding to proper encoding
150 error_log($table.'.'.$col['Field']);
152 $null = $col['Null'] === 'YES' ? ' NULL ' : ' NOT NULL ';
153 $default = $col['Null'] !== 'YES' && isset($col['Default']) ? ' DEFAULT '.db_escape($col['Default']) : '';
155 // to avoid column width multiplication x3 we old->binary->ui->utf column type change instead of column CONVERT
157 $to_binary[] = "CHANGE `".$col['Field']."` `".$col['Field']."` ".$bintype;
158 $to_default[] = "CHANGE `".$col['Field']."` `".$col['Field']."` ".$col['Type'].$null.$default;
159 $to_utf[] = "MODIFY COLUMN `".$col['Field']."` ".$col['Type']." COLLATE ".$collation.$null.$default;
163 if(count($to_binary))
165 $sql = "ALTER TABLE `$table` ".implode(',',$to_binary);
167 $sql = "ALTER TABLE `$table` ".implode(',',$to_default);
169 $sql = "ALTER TABLE `$table` ".implode(',',$to_utf);
172 db_query("ALTER TABLE `$table` COLLATE $collation");
174 db_query("ALTER DATABASE COLLATE $collation");
176 error_log('Convertion to utf8 done.');
181 function do_cleanup()
184 'tax_group_items' => array('rate'),
185 'budget_trans' => array('type', 'type_no', 'person_id', 'person_type_id', 'memo_'),
188 foreach($dropcol as $table => $columns)
189 foreach($columns as $col) {
190 if (db_query("ALTER TABLE `".TB_PREF."{$table}` DROP `$col`") == false) {
191 display_error("Cannot drop {$table}.{$col} column:<br>".db_error_msg($db));
198 $install = new fa2_4;