New files after 2.1RC merge
[fa-stable.git] / sql / alter2.1.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 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                 //      Move all debtor and supplier trans tax details to new table
71                 // (INSERT INTO t  SELECT ... FROM t ... available after 4.0.14)
72                 // No easy way to restore net amount for 0% tax rate for moved
73                 // FA 2.0 transactions, but who cares?
74                 //
75         $move_sql =array( 
76         "debtor_trans_tax_details" =>
77                 "SELECT tr.tran_date, tr.type, tr.trans_no, dt.tax_type_id, 
78                         dt.rate, dt.included_in_price, dt.amount, tr.reference as ref,
79                         tr.rate as ex_rate
80                 FROM ".$pref."debtor_trans_tax_details dt       
81                         LEFT JOIN ".$pref."trans_tax_details tt
82                                 ON dt.debtor_trans_no=tt.trans_no 
83                                 AND dt.debtor_trans_type=tt.trans_type,
84                         ".$pref."debtor_trans tr
85                 WHERE tt.trans_type is NULL
86                         AND dt.debtor_trans_no = tr.trans_no 
87                         AND dt.debtor_trans_type = tr.type",
88         
89         "supp_invoice_tax_items" =>
90                 "SELECT tr.tran_date, tr.type, tr.trans_no, st.tax_type_id, 
91                         st.rate, st.included_in_price, st.amount, tr.supp_reference as ref,
92                         tr.rate as ex_rate
93                         FROM ".$pref."supp_invoice_tax_items st 
94                                 LEFT JOIN ".$pref."trans_tax_details tt
95                                         ON st.supp_trans_no=tt.trans_no 
96                                         AND st.supp_trans_type=tt.trans_type,
97                                         ".$pref."supp_trans tr
98                                 WHERE tt.trans_type is NULL
99                                         AND st.supp_trans_no = tr.trans_no 
100                                         AND st.supp_trans_type = tr.type");
101
102         foreach ($move_sql as $tbl => $sql) {
103                 if (!check_table($pref, $tbl)){
104                         $res = db_query($sql, "Cannot retrieve trans tax details from $tbl");
105                         while ($row = db_fetch($res)) {
106                                 $net_amount = $row['rate'] == 0 ?
107                                         0 : ($row['included_in_price'] ? 
108                                                         ($row['amount']/$row['rate']*(100-$row['rate']))
109                                                         :($row['amount']/$row['rate']*100));
110                                 $sql2 = "INSERT INTO ".$pref."trans_tax_details 
111                                 (trans_type,trans_no,tran_date,tax_type_id,rate,ex_rate,
112                                         included_in_price, net_amount, amount, memo)
113                                 VALUES ('".$row['type']."','".$row['trans_no']."','"
114                                         .$row['tran_date']."','".$row['tax_type_id']."','"
115                                         .$row['rate']."','".$row['ex_rate']."','"
116                                         .$row['included_in_price']."','".$net_amount
117                                         ."','".$row['amount']."','".$row['ref']."')";
118                                 db_query($sql2, "Cannot move trans tax details from $tbl");
119                         }
120                         db_query("DROP TABLE ".$pref.$tbl, "cannot remove $tbl");
121                 }
122         }
123                 
124                 return true;
125         }
126         //
127         //      Checking before install
128         //
129         function pre_check($pref)
130         {
131         // We cannot perform successfull upgrade on system where the
132         // trans tax details tables was deleted during previous try.  
133                 if (check_table($pref, 'debtor_trans_tax_details') 
134                         || check_table($pref, 'supp_invoice_tax_items')) {
135                         display_error(_("Seems that system upgrade to version 2.1 has 
136                         been performed for this company already.<br> If something has gone 
137                         wrong and you want to retry upgrade process you MUST perform 
138                         database restore from last backup file first."));
139
140                         return false;
141                 } 
142
143                 return true; // true when ok, fail otherwise
144         }
145         //
146         //      Test if patch was applied before.
147         //
148         function installed($pref) {
149                 if (check_table($pref, 'item_codes')) return false;
150                 if (check_table($pref, 'company', 'foreign_codes')) return false;
151                 if (check_table($pref, 'suppliers', 'credit_limit')) return false;
152                 if (check_table($pref, 'bank_trans', 'reconciled', 
153                         array('Type'=>'date'))) return false;
154                 if (check_table($pref, 'trans_tax_details')) return false;
155                 return true;
156         }
157 };
158
159 $install = new fa2_1;
160 ?>