Bug 13323: Tax rate can change on receiving - DB changes
[koha.git] / installer / data / mysql / atomicupdate / bug_13323.perl
1 #!/usr/bin/env perl
2
3 use Modern::Perl;
4 use C4::Context;
5
6 my $dbh = C4::Context->dbh;
7
8 # Add the new columns
9 $dbh->do(q|
10     ALTER TABLE aqorders
11         ADD COLUMN tax_rate_on_ordering   decimal(6,4) default NULL AFTER tax_rate,
12         ADD COLUMN tax_rate_on_receiving  decimal(6,4) default NULL AFTER tax_rate_on_ordering,
13         ADD COLUMN tax_value_on_ordering  decimal(28,6) default NULL AFTER tax_value,
14         ADD COLUMN tax_value_on_receiving decimal(28,6) default NULL AFTER tax_value_on_ordering
15 |);
16
17 my $orders = $dbh->selectall_arrayref(q|
18     SELECT * FROM aqorders
19 |, { Slice => {} } );
20
21 my $sth_update_order = $dbh->prepare(q|
22     UPDATE aqorders
23     SET tax_rate_on_ordering = tax_rate,
24         tax_rate_on_receiving = tax_rate,
25         tax_value_on_ordering = ?,
26         tax_value_on_receiving = ?
27     WHERE ordernumber = ?
28 |);
29
30 require Koha::Number::Price;
31 for my $order (@$orders) {
32     my $tax_value_on_ordering =
33       $order->{quantity} *
34       $order->{ecost_tax_excluded} *
35       $order->{tax_rate};
36
37     my $tax_value_on_receiving =
38       ( defined $order->{unitprice_tax_excluded} )
39       ? $order->{quantity} * $order->{unitprice_tax_excluded} * $order->{tax_rate}
40       : undef;
41
42     $sth_update_order->execute( $tax_value_on_ordering,
43         $tax_value_on_receiving, $order->{ordernumber} );
44 }
45
46 # Remove the old columns
47 $dbh->do(q|
48     ALTER TABLE aqorders
49         CHANGE COLUMN tax_value tax_value_bak  decimal(28,6) default NULL,
50         CHANGE COLUMN tax_rate tax_rate_bak decimal(6,4) default NULL
51 |);