1#!perl 2 3use strict; 4use warnings; 5 6use Test::More; 7use Test::Exception; 8 9use aliased 'Interchange6::Cart'; 10use aliased 'Interchange6::Cart::Product'; 11 12my ( $product, $cart ); 13 14lives_ok { $cart = Cart->new } "create new cart"; 15 16lives_ok { 17 $cart->add( sku => "SKU01", name => "One", price => 10, quantity => 1 ) 18} 19"add SKU01, One, price 10, qty 1"; 20 21lives_ok { 22 $cart->add( sku => "SKU02", name => "Two", price => 20, quantity => 2 ) 23} 24"add SKU02, Two, price 20, qty 2"; 25 26cmp_ok( $cart->subtotal, '==', 50, "subtotal is 50" ); 27cmp_ok( $cart->total, '==', 50, "total is 50" ); 28 29$product = $cart->product_get(0); 30 31lives_ok { 32 $product->apply_cost( amount => -2, name => "discount", compound => 1 ) 33} 34"apply compound fixed discount of -2 to 1st product"; 35 36cmp_ok( $product->cost(0), '==', -2, "cost 0 is -2" ); 37cmp_ok( $product->cost("discount"), '==', -2, "cost discount is -2" ); 38cmp_ok( $cart->subtotal, '==', 48, "subtotal is 48" ); 39cmp_ok( $cart->total, '==', 48, "total is 48" ); 40 41lives_ok { 42 $product->apply_cost( 43 name => "tax", 44 amount => 0.1, 45 relative => 1, 46 inclusive => 1 47 ) 48} 49"apply 10% inclusive tax after discount"; 50 51cmp_ok( $product->cost(1), '==', 0.8, "cost 1 is 0.8" ); 52cmp_ok( $product->cost("tax"), '==', 0.8, "cost tax is 0.8" ); 53cmp_ok( $cart->subtotal, '==', 48, "subtotal is 48" ); 54cmp_ok( $cart->total, '==', 48, "total is 48" ); 55 56$product = $cart->product_get(1); 57 58lives_ok { 59 $product->apply_cost( 60 amount => -0.1, 61 relative => 1, 62 name => "discount", 63 compound => 1 64 ) 65} 66"apply compound relative discount of -10% to 2nd product"; 67 68cmp_ok( $product->cost(0), '==', -4, "cost 0 is -4" ); 69cmp_ok( $product->cost("discount"), '==', -4, "cost discount is -4" ); 70cmp_ok( $cart->subtotal, '==', 44, "subtotal is 44" ); 71cmp_ok( $cart->total, '==', 44, "total is 44" ); 72 73lives_ok { 74 $product->apply_cost( 75 name => "tax", 76 amount => 0.2, 77 relative => 1, 78 inclusive => 1 79 ) 80} 81"apply 20% inclusive tax after discount"; 82 83cmp_ok( $product->cost(1), '==', 7.2, "cost 1 is 7.2" ); 84cmp_ok( $product->cost("tax"), '==', 7.2, "cost tax is 7.2" ); 85cmp_ok( $cart->subtotal, '==', 44, "subtotal is 44" ); 86cmp_ok( $cart->total, '==', 44, "total is 44" ); 87 88lives_ok { $cart->update( SKU02 => 1 ) } "change qty of 2nd product to 1"; 89 90cmp_ok( $product->cost(0), '==', -2, "cost 0 is -2" ); 91cmp_ok( $product->cost("discount"), '==', -2, "cost discount is -2" ); 92cmp_ok( $product->cost(1), '==', 3.6, "cost 1 is 3.6" ); 93cmp_ok( $product->cost("tax"), '==', 3.6, "cost tax is 3.6" ); 94cmp_ok( $cart->subtotal, '==', 26, "subtotal is 26" ); 95cmp_ok( $cart->total, '==', 26, "total is 26" ); 96 97lives_ok { 98 $cart->apply_cost( name => "handling", amount => 0.05, relative => 1 ) 99} 100"add 5% handling charge to cart"; 101 102cmp_ok( $cart->cost(0), '==', 1.3, "cost 0 is 1.3" ); 103cmp_ok( $cart->cost("handling"), '==', 1.3, "cost handling is 1.3" ); 104cmp_ok( $cart->subtotal, '==', 26, "subtotal is 26" ); 105cmp_ok( $cart->total, '==', 27.3, "total is 27.3" ); 106 107lives_ok { $cart->update( SKU02 => 2 ) } "change qty of 2nd product to 2"; 108 109cmp_ok( $cart->cost(0), '==', 2.2, "cost 0 is 2.2" ); 110cmp_ok( $cart->cost("handling"), '==', 2.2, "cost handling is 2.2" ); 111cmp_ok( $cart->subtotal, '==', 44, "subtotal is 44" ); 112cmp_ok( $cart->total, '==', 46.2, "total is 46.2" ); 113 114lives_ok { 115 $cart->apply_cost( name => "shipping", amount => "23" ); 116} 117"add 23 fixed shipping charge to cart"; 118 119cmp_ok( $cart->cost(1), '==', 23, "cost 1 is 23" ); 120cmp_ok( $cart->cost("shipping"), '==', 23, "cost shipping is 23" ); 121cmp_ok( $cart->subtotal, '==', 44, "subtotal is 44" ); 122cmp_ok( $cart->total, '==', 69.2, "total is 69.2" ); 123 124done_testing; 125