51136eac266e98d0f6faefa4b8bac725eb0f9983
[fa-stable.git] / sql / alter2.4.php
1 <?php
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 ***********************************************************************/
12
13 class fa2_4 extends fa_patch {
14         var $previous = '2.3rc';                // applicable database version
15         var $version = '2.4.1'; // version installed
16         var $description;
17         var $sql = 'alter2.4.sql';
18         var $preconf = true;
19         var     $max_upgrade_time = 900;        // table recoding is really long process
20         
21         function __construct() {
22                 parent::__construct();
23                 $this->description = _('Upgrade from version 2.3 to 2.4');
24         }
25         
26     /*
27             Shows parameters to be selected before upgrade (if any)
28         */
29     function show_params($comp)
30         {
31           display_note(_('Set optimal parameters and start upgrade:'));
32           start_table(TABLESTYLE);
33           start_row();
34                 collations_list_row(_('Text collation optimization:'), 'collation', substr($_SESSION['language']->code, 0, 2));
35           end_row();
36           end_table();
37           br();
38     }
39
40         /*
41             Fetches selected upgrade parameters.
42     */
43         function prepare()
44     {
45                 $this->collation = get_post('collation');
46                 return true;
47         }
48
49         //
50         //      Install procedure. All additional changes 
51         //      not included in sql file should go here.
52         //
53         function install($company, $force=false)
54         {
55                 global $db_version, $db_connections;
56
57                 $pref = $db_connections[$company]['tbpref'];
58
59                 if (get_company_pref('grn_clearing_act') === null) { // available form 2.3.1, can be not defined on pre-2.4 installations
60                         set_company_pref('grn_clearing_act', 'glsetup.purchase', 'varchar', 15, 0);
61                 }
62                 if (get_company_pref('default_quote_valid_days') === null) { // new in 2.3.23 installations
63                         set_company_pref('default_quote_valid_days', 'glsetup.sales', 'smallint', 6, 30);
64                 }
65                 if (get_company_pref('bcc_email') === null) { // available from 2.3.14, can be not defined on pre-2.4 installations
66                         set_company_pref('bcc_email', 'setup.company', 'varchar', 100, '');
67                 }
68                 if (get_company_pref('alternative_tax_include_on_docs') === null) { // available from 2.3.14, can be not defined on pre-2.4 installations
69                         set_company_pref('alternative_tax_include_on_docs', 'setup.company', 'tinyint', 1, '0');
70                 }
71                 if (get_company_pref('suppress_tax_rates') === null) { // available from 2.3.14, can be not defined on pre-2.4 installations
72                         set_company_pref('suppress_tax_rates', 'setup.company', 'tinyint', 1, '0');
73                 }
74                 if (get_company_pref('company_logo_report') === null) { // available from 2.4.2, during updates
75                         set_company_pref('company_logo_report', 'setup.company', 'tinyint', 1, '0');
76                 }
77                 if (get_company_pref('barcodes_on_stock') === null) { // available from 2.4.3, during updates
78                         set_company_pref('barcodes_on_stock', 'setup.company', 'tinyint', 1, '0');
79                 }
80                 if (get_company_pref('shortname_name_in_list') === null) { // available from 2.4.2, during updates
81                         set_company_pref('shortname_name_in_list', 'setup.company', 'tinyint', 1, '0');
82                 }
83
84                 $result = $this->update_workorders()  && $this->update_grn_rates() && $this->switch_database_to_utf($pref);
85
86                 if ($result)
87                         $result = $this->do_cleanup();
88                 if ($result)
89                 {
90                         $db_connections[$company]['collation'] = $this->collation;
91                         if (write_config_db())
92                                 return $this->log_error(_("Cannot update config_db.php file."));
93                 }
94
95                 $sec_updates = array(
96                         'SA_SETUPCOMPANY' => array(
97                                 'SA_ASSET', 'SA_ASSETCATEGORY', 'SA_ASSETCLASS',
98                                 'SA_ASSETSTRANSVIEW','SA_ASSETTRANSFER', 'SA_ASSETDISPOSAL',
99                                 'SA_DEPRECIATION', 'SA_ASSETSANALYTIC'),
100                 );
101                 $result = $this->update_security_roles($sec_updates);
102
103                 return $result;
104         }
105
106         //
107         // optional procedure done after upgrade fail, before backup is restored
108         //
109         function post_fail($company)
110         {
111                 $pref = $this->companies[$company]['tbpref'];
112                 db_query("DROP TABLE IF EXISTS " . $pref . 'wo_costing');
113                 db_query("DROP TABLE IF EXISTS " . $pref . 'stock_fa_class');
114                 db_query("DELETE FROM ".$pref."sys_prefs
115                         WHERE `name` in (
116                                 'gl_closing_date', 'deferred_income_act', 'no_zero_lines_amount', 'accounts_alpha',
117                                 'tax_algorithm', 'grn_clearing_act', 'default_receival_required',
118                                 'default_quote_valid_days',     'no_zero_lines_amount', 'show_po_item_codes', 'accounts_alpha',
119                                 'loc_notification', 'print_invoice_no', 'allow_negative_prices', 'print_item_images_on_quote',
120                                 'bcc_email', 'alternative_tax_include_on_docs', 'suppress_tax_rates', 'company_logo_report',
121                                 'barcodes_on_report')");
122         }
123
124         function update_workorders()
125         {
126                 global $db;
127
128                 $sql = "SELECT DISTINCT type, type_no, tran_date, person_id FROM ".TB_PREF."gl_trans WHERE `type`=".ST_WORKORDER
129                         ." AND person_type_id=1";
130                 $res = db_query($sql);
131                 if (!$res)
132                         return $this->log_error(sprintf(_("Cannot update work orders costs:\n%s"), db_error_msg($db)));
133
134                 while ($row = db_fetch($res))
135                 {
136                         $journal_id = get_next_trans_no(ST_JOURNAL);
137
138                         $sql1 = "UPDATE ".TB_PREF."gl_trans SET `type`=".ST_JOURNAL.", type_no={$journal_id},
139                                 person_type_id=NULL, person_id=0
140                                 WHERE `type`=".ST_WORKORDER." AND type_no={$row['type_no']} AND tran_date='{$row['tran_date']}'
141                                 AND person_id='{$row['person_id']}'";
142                         if (!db_query($sql1)) return false;
143
144                         $sql2 = "INSERT INTO ".TB_PREF."wo_costing (workorder_id, cost_type, trans_no) 
145                                 VALUES ({$row['type_no']}, {$row['person_id']}, {$journal_id})";
146                         if (!db_query($sql2)) return false;
147                 }
148                 return true;
149         }
150
151 /*
152         In previous versions FA ignored encoding settings on database/tables, so it depended on server settings,
153         but data stored is encoded in user language encoding. Now we switch to utf8 internal database encoding, while
154         user encoding can be selected independently.
155
156         To perform safe FA database switch to utf-8 encoding we have to first ensure that all text/char columns 
157         have properly set encoding (the same as its content), so the algorithm performed on every table is as follows:
158         . set default table encoding for the table to currently used on client side;
159         . for all text/char column:
160          - suppress autorecoding by change of the type to related binary/blob type
161          - change column to utf8 encodding and selected collation.
162         . change default table encoding to utf8 and selected collation
163 */
164         function switch_database_to_utf($pref, $dbg = false) {
165
166                 global $installed_languages, $dflt_lang;
167
168                 $old_encoding = 'latin1'; // default client encoding
169
170                 // uncomment in case of 1071 errors (requires SUPER privileges)
171                 // db_query("SET @@global.innodb_large_prefix=1", "Cannot set large prefix");
172
173                  // site default encoding is presumed as encoding for all databases!
174                 $lang = array_search_value($dflt_lang, $installed_languages, 'code');
175                 $new_encoding = get_mysql_encoding_name(strtoupper($lang['encoding']));
176
177                 $this->log_error(sprintf('Switching database to utf8 encoding from %s', $old_encoding), 'Info');
178                 $collation = get_mysql_collation($this->collation);
179                 $tsql = "SHOW TABLES LIKE '".($pref=='' ? '' : substr($pref, 0, -1).'\\_')."%'";
180                 $tresult = db_query($tsql, "Cannot select all tables with prefix '$pref'");
181                 while($tbl = db_fetch($tresult)) {
182                         $table = $tbl[0];
183
184                         db_query("ALTER TABLE `$table` CONVERT TO CHARACTER SET $old_encoding"); // convert encoding on utf-8 tables
185
186                         // set proper default table encoding for current user language (used on binary->text conversion)
187                         db_query("ALTER TABLE `$table` CHARSET $new_encoding");
188                         $csql = "SHOW COLUMNS FROM $table";
189                         $cresult = db_query($csql, "Cannot select column names for table '$table'");
190                         $convert = false;
191
192                         $to_binary = $to_default = $to_utf = array();
193                         while($col = db_fetch($cresult)) {
194
195                                 $bintype = strtr($col['Type'], array('varchar' => 'varbinary', 'char'=>'varbinary', 'text'=>'blob', 'tinytext'=>'tinyblob'));
196
197                                 if ($bintype != $col['Type'])
198                                 { // this is char/text column, so change encoding to proper encoding
199                                         if ($dbg)
200                                                 $this->log_error(sprintf('%s switched to uft8.', $table.'.'.$col['Field']), 'Debug');
201
202                                         $null = $col['Null'] === 'YES' ? ' NULL ' : ' NOT NULL ';
203                                         $default = $col['Null'] !== 'YES' && isset($col['Default']) ? ' DEFAULT '.db_escape($col['Default']) : '';
204
205                                         // to avoid column width multiplication x3 we old->binary->ui->utf column type change instead of column CONVERT
206
207                                         $to_binary[] = "CHANGE `".$col['Field']."` `".$col['Field']."` ".$bintype;
208                                         $to_default[] = "CHANGE `".$col['Field']."` `".$col['Field']."` ".$col['Type'].$null.$default;
209                                         $to_utf[] = "MODIFY COLUMN `".$col['Field']."` ".$col['Type']." COLLATE ".$collation.$null.$default;
210                                         $convert = true;
211                                 }
212                         }
213                         if(count($to_binary))
214                         {
215                                 $sql = "ALTER TABLE `$table` ".implode(',',$to_binary);
216                                 db_query($sql);
217                                 $sql = "ALTER TABLE `$table` ".implode(',',$to_default);
218                                 db_query($sql);
219                                 $sql = "ALTER TABLE `$table` ".implode(',',$to_utf);
220                                 db_query($sql);
221                         }
222                         db_query("ALTER TABLE `$table` COLLATE $collation");
223                 }
224                 db_query("ALTER DATABASE COLLATE $collation");
225                 $this->log_error(_('Convertion to utf8 done.'), 'Info');
226
227                 return true;
228         }
229
230         function update_grn_rates()
231         {
232                 $sql = "SELECT grn.id, grn.delivery_date, supp.curr_code 
233                         FROM ".TB_PREF."grn_batch grn, ".TB_PREF."suppliers supp
234                         WHERE supp.supplier_id=grn.supplier_id AND supp.curr_code!='".get_company_pref('curr_default')."'";
235                 $result = db_query($sql);
236
237                 if (!$result)
238                         return false;
239
240                 $sql = "UPDATE ".TB_PREF."grn_batch SET rate=%s WHERE id=%d";
241                 while ($grn = db_fetch($result))
242                         db_query(sprintf($sql, get_exchange_rate_from_home_currency($grn['curr_code'], sql2date($grn['delivery_date'])), $grn['id']));
243
244                 return true;
245         }
246
247         function do_cleanup()
248         {
249                 global $db;
250
251                 //remove obsolete and temporary columns.
252                 // this have to be done here as db_import rearranges alter query order
253                 $dropcol = array(
254                                 'tax_groups' => array('tax_shipping'),
255                                 'tax_group_items' => array('rate'),
256                                 'budget_trans' => array('type', 'type_no', 'person_id', 'person_type_id', 'memo_'),
257                                 'cust_branch' => array('contact_name', 'disable_trans'),
258                                 'stock_moves' => array('discount_percent', 'visible', 'person_id'),
259                 );
260
261                 foreach($dropcol as $table => $columns)
262                         foreach($columns as $col) {
263                                 if (db_query("ALTER TABLE `".TB_PREF."{$table}` DROP `$col`") == false) {
264                                         return $this->log_error(sprintf(_("Cannot drop column in %s table: %s"), $table, db_error_msg($db)));
265                                 }
266                         }
267                 return true;
268   }
269 }
270
271 $install = new fa2_4;