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 extends fa_patch {
13 var $previous = ''; // applicable database version
14 var $version = '2.1'; // version installed
16 var $sql = 'alter2.1.sql';
19 $this->description = _('Upgrade from version 2.0 to 2.1');
22 // Install procedure. All additional changes
23 // not included in sql file should go here.
25 function install($company, $force=false)
30 Statement below is allowed only for MySQL >=4.0.4:
31 UPDATE `0_bank_trans`, `0_bank_accounts`
32 SET 0_bank_trans.bank_act=0_bank_accounts.id
33 WHERE 0_bank_trans.bank_act=0_bank_accounts.account_code;
35 $sql = "SELECT id, account_code FROM ".TB_PREF."bank_accounts";
36 if(!($res = db_query($sql))) {
37 display_error(_("Cannot retrieve bank accounts codes")
38 .':<br>'. db_error_msg($db));
41 while ($acc = db_fetch($res)) {
42 $sql = "UPDATE ".TB_PREF."bank_trans SET bank_act='"
43 .$acc['id']."' WHERE bank_act=".$acc['account_code'];
44 if (db_query($sql)==false) {
45 display_error(_("Cannot update bank transactions")
46 .':<br>'. db_error_msg($db));
50 // copy all item codes from stock_master into item_codes
51 $sql = "SELECT `stock_id`,`description`,`category_id` FROM ".TB_PREF."stock_master";
52 $result = db_query($sql);
54 display_error(_("Cannot select stock identificators")
55 .':<br>'. db_error_msg($db));
58 while ($row = db_fetch_assoc($result)) {
59 $sql = "INSERT IGNORE "
60 .TB_PREF."item_codes (`item_code`,`stock_id`,`description`,`category_id`)
61 VALUES('".$row['stock_id']."','".$row['stock_id']."','"
62 .$row['description']."','".$row['category_id']."')";
63 $res2 = db_query($sql);
65 display_error(_("Cannot insert stock id into item_codes")
66 .':<br>'. db_error_msg($db));
71 // remove obsolete bank_trans_types table
72 // (DROP queries are skipped during non-forced upgrade)
73 $sql = "DROP TABLE IF EXISTS `".TB_PREF."bank_trans_types`";
76 // Move all debtor and supplier trans tax details to new table
77 // (INSERT INTO t SELECT ... FROM t ... available after 4.0.14)
78 // No easy way to restore net amount for 0% tax rate for moved
79 // FA 2.0 transactions, but who cares?
82 "debtor_trans_tax_details" =>
83 "SELECT tr.tran_date, tr.type, tr.trans_no, dt.tax_type_id,
84 dt.rate, dt.included_in_price, dt.amount, tr.reference as ref,
86 FROM ".TB_PREF."debtor_trans_tax_details dt
87 LEFT JOIN ".TB_PREF."trans_tax_details tt
88 ON dt.debtor_trans_no=tt.trans_no
89 AND dt.debtor_trans_type=tt.trans_type,
90 ".TB_PREF."debtor_trans tr
91 WHERE tt.trans_type is NULL
92 AND dt.debtor_trans_no = tr.trans_no
93 AND dt.debtor_trans_type = tr.type",
95 "supp_invoice_tax_items" =>
96 "SELECT tr.tran_date, tr.type, tr.trans_no, st.tax_type_id,
97 st.rate, st.included_in_price, st.amount, tr.supp_reference as ref,
99 FROM ".TB_PREF."supp_invoice_tax_items st
100 LEFT JOIN ".TB_PREF."trans_tax_details tt
101 ON st.supp_trans_no=tt.trans_no
102 AND st.supp_trans_type=tt.trans_type,
103 ".$pref."supp_trans tr
104 WHERE tt.trans_type is NULL
105 AND st.supp_trans_no = tr.trans_no
106 AND st.supp_trans_type = tr.type");
108 foreach ($move_sql as $tbl => $sql) {
109 if (!check_table(TB_PREF, $tbl)){
110 $res = db_query($sql, "Cannot retrieve trans tax details from $tbl");
111 while ($row = db_fetch($res)) {
112 $net_amount = $row['rate'] == 0 ?
113 0 : ($row['included_in_price'] ?
114 ($row['amount']/$row['rate']*(100-$row['rate']))
115 :($row['amount']/$row['rate']*100));
116 $sql2 = "INSERT INTO ".TB_PREF."trans_tax_details
117 (trans_type,trans_no,tran_date,tax_type_id,rate,ex_rate,
118 included_in_price, net_amount, amount, memo)
119 VALUES ('".$row['type']."','".$row['trans_no']."','"
120 .$row['tran_date']."','".$row['tax_type_id']."','"
121 .$row['rate']."','".$row['ex_rate']."','"
122 .$row['included_in_price']."','".$net_amount
123 ."','".$row['amount']."','".$row['ref']."')";
124 db_query($sql2, "Cannot move trans tax details from $tbl");
126 db_query("DROP TABLE ".TB_PREF.$tbl, "cannot remove $tbl");
134 // Checking before install
138 // We cannot perform successfull upgrade on system where the
139 // trans tax details tables was deleted during previous try.
140 $pref = $this->companies[$company]['tbpref'];
142 if (check_table($pref, 'debtor_trans_tax_details')
143 || check_table($pref, 'supp_invoice_tax_items')) {
144 display_error(_("Seems that system upgrade to version 2.1 has
145 been performed for this company already.<br> If something has gone
146 wrong and you want to retry upgrade process you MUST perform
147 database restore from last backup file first."));
152 return true; // true when ok, fail otherwise
156 $install = new fa2_1;