*** empty log message ***
[fa-stable.git] / sql / alter2.1.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 class fa2_1 {
13         var $version = '2.1';   // version installed
14         var $description = 'Version 2.1';
15         var $sql = 'alter2.1.sql';
16         //
17         //      Install procedure. All additional changes 
18         //      not included in sql file should go here.
19         //
20         function install($pref, $force) 
21         {
22                 global $db;
23         /*
24         Statement below is allowed only for MySQL >=4.0.4:
25         UPDATE `0_bank_trans`, `0_bank_accounts` 
26                 SET 0_bank_trans.bank_act=0_bank_accounts.id 
27                 WHERE 0_bank_trans.bank_act=0_bank_accounts.account_code;
28         */
29                 $sql = "SELECT id, account_code FROM ".$pref."bank_accounts";
30                 if(!($res = db_query($sql))) {
31                         display_error(_("Cannot retrieve bank accounts codes")
32                                 .':<br>'. db_error_msg($db));
33                         return false;
34                 }
35                 while ($acc = db_fetch($res)) {
36                         $sql = "UPDATE ".$pref."bank_trans SET bank_act='"
37                                 .$acc['id']."' WHERE bank_act=".$acc['account_code'];
38                         if (db_query($sql)==false) {
39                         display_error(_("Cannot update bank transactions")
40                                 .':<br>'. db_error_msg($db));
41                                 return false;
42                         }
43                 }
44                 // copy all item codes from stock_master into item_codes
45                 $sql = "SELECT `stock_id`,`description`,`category_id` FROM ".$pref."stock_master";
46                 $result = db_query($sql);
47                 if (!$result) {
48                         display_error(_("Cannot select stock identificators")
49                                 .':<br>'. db_error_msg($db));
50                         return false;
51                 } else {
52                         while ($row = db_fetch_assoc($result)) {
53                                 $sql = "INSERT IGNORE "
54                                         .$pref."item_codes (`item_code`,`stock_id`,`description`,`category_id`)
55                                         VALUES('".$row['stock_id']."','".$row['stock_id']."','"
56                                         .$row['description']."','".$row['category_id']."')";
57                                 $res2 = db_query($sql);
58                                 if (!$res2) {
59                                         display_error(_("Cannot insert stock id into item_codes")
60                                                 .':<br>'. db_error_msg($db));
61                                         return false;
62                                 }
63                         }
64                 }
65                 // remove obsolete bank_trans_types table 
66                 // (DROP queries are skipped during non-forced upgrade)
67                 $sql = "DROP TABLE IF EXISTS `0_bank_trans_types`";
68                 db_query($sql);
69                 
70                 return true;
71         }
72         //
73         //      Checking before install
74         //
75         function pre_check($pref) 
76         {
77                 return true; // true when ok, fail otherwise
78         }
79         //
80         //      Test if patch was applied before.
81         //
82         function installed($pref) {
83                 if (check_table($pref, 'item_codes')) return false;
84                 if (check_table($pref, 'company', 'foreign_codes')) return false;
85                 if (check_table($pref, 'supplier', 'credit_limit') return false;                
86                 return true;
87         }
88 };
89
90 $install = new fa2_1;
91 ?>