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.1'; // version installed
15 var $sql = 'alter2.1.sql';
18 $this->description = _('Upgrade from version 2.0 to 2.1');
21 // Install procedure. All additional changes
22 // not included in sql file should go here.
24 function install($pref, $force)
28 Statement below is allowed only for MySQL >=4.0.4:
29 UPDATE `0_bank_trans`, `0_bank_accounts`
30 SET 0_bank_trans.bank_act=0_bank_accounts.id
31 WHERE 0_bank_trans.bank_act=0_bank_accounts.account_code;
33 $sql = "SELECT id, account_code FROM ".$pref."bank_accounts";
34 if(!($res = db_query($sql))) {
35 display_error(_("Cannot retrieve bank accounts codes")
36 .':<br>'. db_error_msg($db));
39 while ($acc = db_fetch($res)) {
40 $sql = "UPDATE ".$pref."bank_trans SET bank_act='"
41 .$acc['id']."' WHERE bank_act=".$acc['account_code'];
42 if (db_query($sql)==false) {
43 display_error(_("Cannot update bank transactions")
44 .':<br>'. db_error_msg($db));
48 // copy all item codes from stock_master into item_codes
49 $sql = "SELECT `stock_id`,`description`,`category_id` FROM ".$pref."stock_master";
50 $result = db_query($sql);
52 display_error(_("Cannot select stock identificators")
53 .':<br>'. db_error_msg($db));
56 while ($row = db_fetch_assoc($result)) {
57 $sql = "INSERT IGNORE "
58 .$pref."item_codes (`item_code`,`stock_id`,`description`,`category_id`)
59 VALUES('".$row['stock_id']."','".$row['stock_id']."','"
60 .$row['description']."','".$row['category_id']."')";
61 $res2 = db_query($sql);
63 display_error(_("Cannot insert stock id into item_codes")
64 .':<br>'. db_error_msg($db));
69 // remove obsolete bank_trans_types table
70 // (DROP queries are skipped during non-forced upgrade)
71 $sql = "DROP TABLE IF EXISTS `0_bank_trans_types`";
74 // Move all debtor and supplier trans tax details to new table
75 // (INSERT INTO t SELECT ... FROM t ... available after 4.0.14)
76 // No easy way to restore net amount for 0% tax rate for moved
77 // FA 2.0 transactions, but who cares?
80 "debtor_trans_tax_details" =>
81 "SELECT tr.tran_date, tr.type, tr.trans_no, dt.tax_type_id,
82 dt.rate, dt.included_in_price, dt.amount, tr.reference as ref,
84 FROM ".$pref."debtor_trans_tax_details dt
85 LEFT JOIN ".$pref."trans_tax_details tt
86 ON dt.debtor_trans_no=tt.trans_no
87 AND dt.debtor_trans_type=tt.trans_type,
88 ".$pref."debtor_trans tr
89 WHERE tt.trans_type is NULL
90 AND dt.debtor_trans_no = tr.trans_no
91 AND dt.debtor_trans_type = tr.type",
93 "supp_invoice_tax_items" =>
94 "SELECT tr.tran_date, tr.type, tr.trans_no, st.tax_type_id,
95 st.rate, st.included_in_price, st.amount, tr.supp_reference as ref,
97 FROM ".$pref."supp_invoice_tax_items st
98 LEFT JOIN ".$pref."trans_tax_details tt
99 ON st.supp_trans_no=tt.trans_no
100 AND st.supp_trans_type=tt.trans_type,
101 ".$pref."supp_trans tr
102 WHERE tt.trans_type is NULL
103 AND st.supp_trans_no = tr.trans_no
104 AND st.supp_trans_type = tr.type");
106 foreach ($move_sql as $tbl => $sql) {
107 if (!check_table($pref, $tbl)){
108 $res = db_query($sql, "Cannot retrieve trans tax details from $tbl");
109 while ($row = db_fetch($res)) {
110 $net_amount = $row['rate'] == 0 ?
111 0 : ($row['included_in_price'] ?
112 ($row['amount']/$row['rate']*(100-$row['rate']))
113 :($row['amount']/$row['rate']*100));
114 $sql2 = "INSERT INTO ".$pref."trans_tax_details
115 (trans_type,trans_no,tran_date,tax_type_id,rate,ex_rate,
116 included_in_price, net_amount, amount, memo)
117 VALUES ('".$row['type']."','".$row['trans_no']."','"
118 .$row['tran_date']."','".$row['tax_type_id']."','"
119 .$row['rate']."','".$row['ex_rate']."','"
120 .$row['included_in_price']."','".$net_amount
121 ."','".$row['amount']."','".$row['ref']."')";
122 db_query($sql2, "Cannot move trans tax details from $tbl");
124 db_query("DROP TABLE ".$pref.$tbl, "cannot remove $tbl");
131 // Checking before install
133 function pre_check($pref)
135 // We cannot perform successfull upgrade on system where the
136 // trans tax details tables was deleted during previous try.
137 if (check_table($pref, 'debtor_trans_tax_details')
138 || check_table($pref, 'supp_invoice_tax_items')) {
139 display_error(_("Seems that system upgrade to version 2.1 has
140 been performed for this company already.<br> If something has gone
141 wrong and you want to retry upgrade process you MUST perform
142 database restore from last backup file first."));
147 return true; // true when ok, fail otherwise
150 // Test if patch was applied before.
152 function installed($pref) {
153 $n = 4; // number of features to be installed
154 if (!check_table($pref, 'item_codes')) $n--;
155 // if (!check_table($pref, 'company', 'foreign_codes')) $n--;
156 if (!check_table($pref, 'suppliers', 'credit_limit')) $n--;
157 if (!check_table($pref, 'bank_trans', 'reconciled',
158 array('Type'=>'date'))) $n--;
159 if (!check_table($pref, 'trans_tax_details')) $n--;
160 return $n == 0 ? true : 5 - $n;
164 $install = new fa2_1;