+ //
+ // Move all debtor and supplier trans tax details to new table
+ // (INSERT INTO t SELECT ... FROM t ... available after 4.0.14)
+ // No easy way to restore net amount for 0% tax rate for moved
+ // FA 2.0 transactions, but who cares?
+ //
+ $move_sql =array(
+ "debtor_trans_tax_details" =>
+ "SELECT tr.tran_date, tr.type, tr.trans_no, dt.tax_type_id,
+ dt.rate, dt.included_in_price, dt.amount, tr.reference as ref,
+ tr.rate as ex_rate
+ FROM ".$pref."debtor_trans_tax_details dt
+ LEFT JOIN ".$pref."trans_tax_details tt
+ ON dt.debtor_trans_no=tt.trans_no
+ AND dt.debtor_trans_type=tt.trans_type,
+ ".$pref."debtor_trans tr
+ WHERE tt.trans_type is NULL
+ AND dt.debtor_trans_no = tr.trans_no
+ AND dt.debtor_trans_type = tr.type",
+
+ "supp_invoice_tax_items" =>
+ "SELECT tr.tran_date, tr.type, tr.trans_no, st.tax_type_id,
+ st.rate, st.included_in_price, st.amount, tr.supp_reference as ref,
+ tr.rate as ex_rate
+ FROM ".$pref."supp_invoice_tax_items st
+ LEFT JOIN ".$pref."trans_tax_details tt
+ ON st.supp_trans_no=tt.trans_no
+ AND st.supp_trans_type=tt.trans_type,
+ ".$pref."supp_trans tr
+ WHERE tt.trans_type is NULL
+ AND st.supp_trans_no = tr.trans_no
+ AND st.supp_trans_type = tr.type");
+
+ foreach ($move_sql as $tbl => $sql) {
+ if (!check_table($pref, $tbl)){
+ $res = db_query($sql, "Cannot retrieve trans tax details from $tbl");
+ while ($row = db_fetch($res)) {
+ $net_amount = $row['rate'] == 0 ?
+ 0 : ($row['included_in_price'] ?
+ ($row['amount']/$row['rate']*(100-$row['rate']))
+ :($row['amount']/$row['rate']*100));
+ $sql2 = "INSERT INTO ".$pref."trans_tax_details
+ (trans_type,trans_no,tran_date,tax_type_id,rate,ex_rate,
+ included_in_price, net_amount, amount, memo)
+ VALUES ('".$row['type']."','".$row['trans_no']."','"
+ .$row['tran_date']."','".$row['tax_type_id']."','"
+ .$row['rate']."','".$row['ex_rate']."','"
+ .$row['included_in_price']."','".$net_amount
+ ."','".$row['amount']."','".$row['ref']."')";
+ db_query($sql2, "Cannot move trans tax details from $tbl");
+ }
+ db_query("DROP TABLE ".$pref.$tbl, "cannot remove $tbl");
+ }
+ }