Bug 22008: Add tests for new constraints
[koha.git] / t / Prices.t
index 7ba1cc1..4039233 100644 (file)
@@ -1,19 +1,45 @@
 use Modern::Perl;
-use Test::More tests => 16;
+use Test::More;
 use Test::MockModule;
 
 use t::lib::Mocks;
 
+use Module::Load::Conditional qw/check_install/;
+
 BEGIN {
-    my $context_module = t::lib::Mocks::mock_dbh;
-    use_ok('C4::Acquisition');
-    use_ok('C4::Bookseller');
-    use_ok('C4::Context');
-    use_ok('Koha::Number::Price');
-};
+    if ( check_install( module => 'Test::DBIx::Class' ) ) {
+        plan tests => 16;
+    } else {
+        plan skip_all => "Need Test::DBIx::Class"
+    }
+}
+
+use_ok('C4::Acquisition');
+use_ok('C4::Context');
+use_ok('Koha::Number::Price');
 
 t::lib::Mocks::mock_preference( 'gist', '0.02|0.05|0.196' );
 
+use Test::DBIx::Class;
+
+my $db = Test::MockModule->new('Koha::Database');
+$db->mock( _new_schema => sub { return Schema(); } );
+Koha::Database::flush_schema_cache();
+
+fixtures_ok [
+    Currency => [
+        [ qw/ currency symbol rate active / ],
+        [ 'my_cur', '€', 1, 1, ],
+    ],
+    Aqbookseller => [
+        [ qw/ id name listincgst invoiceincgst / ],
+        [ 1, '0 0', 0, 0 ],
+        [ 2, '0 1', 0, 1 ],
+        [ 3, '1 0', 1, 0 ],
+        [ 4, '1 1', 1, 1 ],
+    ],
+], 'add currency fixtures';
+
 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
 
 my ( $basketno_0_0,  $basketno_1_1,  $basketno_1_0,  $basketno_0_1 );
@@ -23,13 +49,7 @@ my $today;
 for my $currency_format ( qw( US FR ) ) {
     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
     subtest 'Configuration 1: 0 0' => sub {
-        plan tests => 12;
-        $bookseller_module->mock(
-            'fetch',
-            sub {
-                return { listincgst => 0, invoiceincgst => 0 };
-            }
-        );
+        plan tests => 8;
 
         my $biblionumber_0_0 = 42;
 
@@ -43,135 +63,95 @@ for my $currency_format ( qw( US FR ) ) {
             invoiceid        => $invoiceid_0_0,
             rrp              => 82.00,
             ecost            => 73.80,
-            gstrate          => 0.0500,
+            tax_rate         => 0.0500,
             discount         => 10.0000,
             datereceived     => $today
         };
         $order_0_0 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_0_0,
-                booksellerid => 'just_something',
+                booksellerid => 1,
                 ordering     => 1,
             }
         );
 
-        # Note that this configuration is correct \o/
         compare(
             {
-                got      => $order_0_0->{rrpgsti},
+                got      => $order_0_0->{rrp_tax_included},
                 expected => 86.10,
                 conf     => '0 0',
-                field    => 'rrpgsti'
+                field    => 'rrp_tax_included'
             }
         );
         compare(
             {
-                got      => $order_0_0->{rrpgste},
+                got      => $order_0_0->{rrp_tax_excluded},
                 expected => 82.00,
                 conf     => '0 0',
-                field    => 'rrpgste'
+                field    => 'rrp_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_0_0->{ecostgsti},
+                got      => $order_0_0->{ecost_tax_included},
                 expected => 77.49,
                 conf     => '0 0',
-                field    => 'ecostgsti'
+                field    => 'ecost_tax_included'
             }
         );
         compare(
             {
-                got      => $order_0_0->{ecostgste},
+                got      => $order_0_0->{ecost_tax_excluded},
                 expected => 73.80,
                 conf     => '0 0',
-                field    => 'ecostgste'
+                field    => 'ecost_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_0_0->{gstvalue},
+                got      => $order_0_0->{tax_value_on_ordering},
                 expected => 7.38,
                 conf     => '0 0',
-                field    => 'gstvalue'
-            }
-        );
-        compare(
-            {
-                got      => $order_0_0->{totalgsti},
-                expected => 154.98,
-                conf     => '0 0',
-                field    => 'totalgsti'
-            }
-        );
-        compare(
-            {
-                got      => $order_0_0->{totalgste},
-                expected => 147.60,
-                conf     => '0 0',
-                field    => 'totalgste'
+                field    => 'tax_value'
             }
         );
 
         $order_0_0 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_0_0,
-                booksellerid => 'just_something',
+                booksellerid => 1,
                 receiving    => 1,
             }
         );
 
-        # Note that this configuration is correct \o/
         compare(
             {
-                got      => $order_0_0->{unitpricegsti},
+                got      => $order_0_0->{unitprice_tax_included},
                 expected => 77.49,
                 conf     => '0 0',
-                field    => 'unitpricegsti'
+                field    => 'unitprice_tax_included'
             }
         );
         compare(
             {
-                got      => $order_0_0->{unitpricegste},
+                got      => $order_0_0->{unitprice_tax_excluded},
                 expected => 73.80,
                 conf     => '0 0',
-                field    => 'unitpricegste'
+                field    => 'unitprice_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_0_0->{gstvalue},
+                got      => $order_0_0->{tax_value_on_receiving},
                 expected => 7.38,
                 conf     => '0 0',
-                field    => 'gstvalue'
-            }
-        );
-        compare(
-            {
-                got      => $order_0_0->{totalgsti},
-                expected => 154.98,
-                conf     => '0 0',
-                field    => 'totalgsti'
-            }
-        );
-        compare(
-            {
-                got      => $order_0_0->{totalgste},
-                expected => 147.60,
-                conf     => '0 0',
-                field    => 'totalgste'
+                field    => 'tax_value'
             }
         );
     };
 
     subtest 'Configuration 1: 1 1' => sub {
-        plan tests => 12;
-        $bookseller_module->mock(
-            'fetch',
-            sub {
-                return { listincgst => 1, invoiceincgst => 1 };
-            }
-        );
+        plan tests => 8;
 
         my $biblionumber_1_1 = 43;
         my $order_1_1        = {
@@ -184,7 +164,7 @@ for my $currency_format ( qw( US FR ) ) {
             invoiceid        => $invoiceid_1_1,
             rrp              => 82.00,
             ecost            => 73.80,
-            gstrate          => 0.0500,
+            tax_rate         => 0.0500,
             discount         => 10.0000,
             datereceived     => $today
         };
@@ -192,142 +172,101 @@ for my $currency_format ( qw( US FR ) ) {
         $order_1_1 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_1_1,
-                booksellerid => 'just_something',
+                booksellerid => 4,
                 ordering     => 1,
             }
         );
 
-        # Note that this configuration is *not* correct
-        # gstvalue should be 7.03 instead of 7.02
         compare(
             {
-                got      => $order_1_1->{rrpgsti},
+                got      => $order_1_1->{rrp_tax_included},
                 expected => 82.00,
                 conf     => '1 1',
-                field    => 'rrpgsti'
+                field    => 'rrp_tax_included'
             }
         );
         compare(
             {
-                got      => $order_1_1->{rrpgste},
+                got      => $order_1_1->{rrp_tax_excluded},
                 expected => 78.10,
                 conf     => '1 1',
-                field    => 'rrpgste'
+                field    => 'rrp_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_1_1->{ecostgsti},
+                got      => $order_1_1->{ecost_tax_included},
                 expected => 73.80,
                 conf     => '1 1',
-                field    => 'ecostgsti'
+                field    => 'ecost_tax_included'
             }
         );
         compare(
             {
-                got      => $order_1_1->{ecostgste},
+                got      => $order_1_1->{ecost_tax_excluded},
                 expected => 70.29,
                 conf     => '1 1',
-                field    => 'ecostgste'
-            }
-        );
-        compare(
-            {
-                got      => $order_1_1->{gstvalue},
-                expected => 7.02,
-                conf     => '1 1',
-                field    => 'gstvalue'
+                field    => 'ecost_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_1_1->{totalgsti},
-                expected => 147.60,
+                got      => $order_1_1->{tax_value_on_ordering},
+                expected => 7.03,
                 conf     => '1 1',
-                field    => 'totalgsti'
-            }
-        );
-        compare(
-            {
-                got      => $order_1_1->{totalgste},
-                expected => 140.58,
-                conf     => '1 1',
-                field    => 'totalgste'
+                field    => 'tax_value'
             }
         );
 
         $order_1_1 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_1_1,
-                booksellerid => 'just_something',
+                booksellerid => 4,
                 receiving    => 1,
             }
         );
-        # Note that this configuration is *not* correct!
-        # gstvalue should be 7.03
+
         compare(
             {
-                got      => $order_1_1->{unitpricegsti},
+                got      => $order_1_1->{unitprice_tax_included},
                 expected => 73.80,
                 conf     => '1 1',
-                field    => 'unitpricegsti'
+                field    => 'unitprice_tax_included'
             }
         );
         compare(
             {
-                got      => $order_1_1->{unitpricegste},
+                got      => $order_1_1->{unitprice_tax_excluded},
                 expected => 70.29,
                 conf     => '1 1',
-                field    => 'unitpricegste'
+                field    => 'unitprice_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_1_1->{gstvalue},
-                expected => 7.02,
+                got      => $order_1_1->{tax_value_on_receiving},
+                expected => 7.03,
                 conf     => '1 1',
-                field    => 'gstvalue'
-            }
-        );
-        compare(
-            {
-                got      => $order_1_1->{totalgsti},
-                expected => 147.60,
-                conf     => '1 1',
-                field    => 'totalgsti'
-            }
-        );
-        compare(
-            {
-                got      => $order_1_1->{totalgste},
-                expected => 140.58,
-                conf     => '1 1',
-                field    => 'totalgste'
+                field    => 'tax_value'
             }
         );
     };
 
     subtest 'Configuration 1: 1 0' => sub {
-        plan tests => 12;
-        $bookseller_module->mock(
-            'fetch',
-            sub {
-                return { listincgst => 1, invoiceincgst => 0 };
-            }
-        );
+        plan tests => 8;
 
         my $biblionumber_1_0 = 44;
         my $order_1_0        = {
             biblionumber     => $biblionumber_1_0,
             quantity         => 2,
             listprice        => 82.000000,
-            unitprice        => 73.804500,
+            unitprice        => 70.290000,
             quantityreceived => 2,
             basketno         => $basketno_1_1,
             invoiceid        => $invoiceid_1_1,
-            rrp              => 82.01,
+            rrp              => 82.00,
             ecost            => 73.80,
-            gstrate          => 0.0500,
+            tax_rate         => 0.0500,
             discount         => 10.0000,
             datereceived     => $today
         };
@@ -335,145 +274,101 @@ for my $currency_format ( qw( US FR ) ) {
         $order_1_0 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_1_0,
-                booksellerid => 'just_something',
+                booksellerid => 3,
                 ordering     => 1,
             }
         );
 
-        # Note that this configuration is *not* correct!
-        # rrp gsti should be 82 (what we inserted!)
-        # => Actually we need to fix the inserted value (here we have 82.01 in DB)
-        # gstvalue should be 7.03 instead of 7.02
-
         compare(
             {
-                got      => $order_1_0->{rrpgsti},
-                expected => 82.01,
+                got      => $order_1_0->{rrp_tax_included},
+                expected => 82,
                 conf     => '1 0',
-                field    => 'rrpgsti'
+                field    => 'rrp_tax_included'
             }
         );
         compare(
             {
-                got      => $order_1_0->{rrpgste},
+                got      => $order_1_0->{rrp_tax_excluded},
                 expected => 78.10,
                 conf     => '1 0',
-                field    => 'rrpgste'
+                field    => 'rrp_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_1_0->{ecostgsti},
+                got      => $order_1_0->{ecost_tax_included},
                 expected => 73.80,
                 conf     => '1 0',
-                field    => 'ecostgsti'
+                field    => 'ecost_tax_included'
             }
         );
         compare(
             {
-                got      => $order_1_0->{ecostgste},
+                got      => $order_1_0->{ecost_tax_excluded},
                 expected => 70.29,
                 conf     => '1 0',
-                field    => 'ecostgste'
+                field    => 'ecost_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_1_0->{gstvalue},
-                expected => 7.02,
+                got      => $order_1_0->{tax_value_on_ordering},
+                expected => 7.03,
                 conf     => '1 0',
-                field    => 'gstvalue'
-            }
-        );
-        compare(
-            {
-                got      => $order_1_0->{totalgsti},
-                expected => 147.60,
-                conf     => '1 0',
-                field    => 'totalgsti'
-            }
-        );
-        compare(
-            {
-                got      => $order_1_0->{totalgste},
-                expected => 140.58,
-                conf     => '1 0',
-                field    => 'totalgste'
+                field    => 'tax_value'
             }
         );
 
         $order_1_0 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_1_0,
-                booksellerid => 'just_something',
+                booksellerid => 3,
                 receiving    => 1,
             }
         );
-        # Note that this configuration is *not* correct!
-        # gstvalue should be 7.03
+
         compare(
             {
-                got      => $order_1_0->{unitpricegsti},
+                got      => $order_1_0->{unitprice_tax_included},
                 expected => 73.80,
                 conf     => '1 0',
-                field    => 'unitpricegsti'
+                field    => 'unitprice_tax_included'
             }
         );
         compare(
             {
-                got      => $order_1_0->{unitpricegste},
+                got      => $order_1_0->{unitprice_tax_excluded},
                 expected => 70.29,
                 conf     => '1 0',
-                field    => 'unitpricegste'
-            }
-        );
-        compare(
-            {
-                got      => $order_1_0->{gstvalue},
-                expected => 7.02,
-                conf     => '1 0',
-                field    => 'gstvalue'
-            }
-        );
-        compare(
-            {
-                got      => $order_1_0->{totalgsti},
-                expected => 147.60,
-                conf     => '1 0',
-                field    => 'totalgsti'
+                field    => 'unitprice_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_1_0->{totalgste},
-                expected => 140.58,
+                got      => $order_1_0->{tax_value_on_receiving},
+                expected => 7.03,
                 conf     => '1 0',
-                field    => 'totalgste'
+                field    => 'tax_value'
             }
         );
     };
 
     subtest 'Configuration 1: 0 1' => sub {
-        plan tests => 12;
-        $bookseller_module->mock(
-            'fetch',
-            sub {
-                return { listincgst => 0, invoiceincgst => 1 };
-            }
-        );
+        plan tests => 8;
 
         my $biblionumber_0_1 = 45;
         my $order_0_1        = {
             biblionumber     => $biblionumber_0_1,
             quantity         => 2,
             listprice        => 82.000000,
-            unitprice        => 73.800000,
+            unitprice        => 77.490000,
             quantityreceived => 2,
             basketno         => $basketno_1_1,
             invoiceid        => $invoiceid_1_1,
             rrp              => 82.00,
             ecost            => 73.80,
-            gstrate          => 0.0500,
+            tax_rate         => 0.0500,
             discount         => 10.0000,
             datereceived     => $today
         };
@@ -481,115 +376,82 @@ for my $currency_format ( qw( US FR ) ) {
         $order_0_1 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_0_1,
-                booksellerid => 'just_something',
+                booksellerid => 2,
                 ordering     => 1,
             }
         );
 
-        # Note that this configuration is correct \o/
         compare(
             {
-                got      => $order_0_1->{rrpgsti},
+                got      => $order_0_1->{rrp_tax_included},
                 expected => 86.10,
-                conf     => '1 0',
-                field    => 'rrpgsti'
+                conf     => '0 1',
+                field    => 'rrp_tax_included'
             }
         );
         compare(
             {
-                got      => $order_0_1->{rrpgste},
+                got      => $order_0_1->{rrp_tax_excluded},
                 expected => 82.00,
-                conf     => '1 0',
-                field    => 'rrpgste'
+                conf     => '0 1',
+                field    => 'rrp_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_0_1->{ecostgsti},
+                got      => $order_0_1->{ecost_tax_included},
                 expected => 77.49,
-                conf     => '1 0',
-                field    => 'ecostgsti'
+                conf     => '0 1',
+                field    => 'ecost_tax_included'
             }
         );
         compare(
             {
-                got      => $order_0_1->{ecostgste},
+                got      => $order_0_1->{ecost_tax_excluded},
                 expected => 73.80,
-                conf     => '1 0',
-                field    => 'ecostgste'
+                conf     => '0 1',
+                field    => 'ecost_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_0_1->{gstvalue},
+                got      => $order_0_1->{tax_value_on_ordering},
                 expected => 7.38,
-                conf     => '1 0',
-                field    => 'gstvalue'
-            }
-        );
-        compare(
-            {
-                got      => $order_0_1->{totalgsti},
-                expected => 154.98,
-                conf     => '1 0',
-                field    => 'totalgsti'
-            }
-        );
-        compare(
-            {
-                got      => $order_0_1->{totalgste},
-                expected => 147.60,
-                conf     => '1 0',
-                field    => 'totalgste'
+                conf     => '0 1',
+                field    => 'tax_value'
             }
         );
 
         $order_0_1 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_0_1,
-                booksellerid => 'just_something',
+                booksellerid => 2,
                 receiving    => 1,
             }
         );
-        # Note that this configuration is correct
+
         compare(
             {
-                got      => $order_0_1->{unitpricegsti},
+                got      => $order_0_1->{unitprice_tax_included},
                 expected => 77.49,
                 conf     => '0 1',
-                field    => 'unitpricegsti'
+                field    => 'unitprice_tax_included'
             }
         );
         compare(
             {
-                got      => $order_0_1->{unitpricegste},
+                got      => $order_0_1->{unitprice_tax_excluded},
                 expected => 73.80,
                 conf     => '0 1',
-                field    => 'unitpricegste'
+                field    => 'unitprice_tax_excluded'
             }
         );
         compare(
             {
-                got      => $order_0_1->{gstvalue},
+                got      => $order_0_1->{tax_value_on_receiving},
                 expected => 7.38,
                 conf     => '0 1',
-                field    => 'gstvalue'
-            }
-        );
-        compare(
-            {
-                got      => $order_0_1->{totalgsti},
-                expected => 154.98,
-                conf     => '0 1',
-                field    => 'totalgsti'
-            }
-        );
-        compare(
-            {
-                got      => $order_0_1->{totalgste},
-                expected => 147.60,
-                conf     => '0 1',
-                field    => 'totalgste'
+                field    => 'tax_value'
             }
         );
     };