public function testProducts()
 {
     $discount = OrderDiscount::create(array("Title" => "20% off each products", "Percent" => 0.2, "ProductTypes" => "Product"));
     $discount->write();
     $this->assertTrue($discount->validateOrder($this->cart));
     $this->assertFalse($discount->validateOrder($this->giftcart));
 }
 public function testMembership()
 {
     $discount = OrderDiscount::create(array("Title" => "Membership Coupon", "Type" => "Amount", "Amount" => 1.33));
     $discount->write();
     $member = $this->objFromFixture("Member", "joebloggs");
     $discount->Members()->add($member);
     $this->assertFalse($discount->validateOrder($this->cart), "Invalid, because no member");
     $context = array("Member" => $this->objFromFixture("Member", "bobjones"));
     $this->assertFalse($discount->validateOrder($this->cart, $context), "Invalid because wrong member present");
     $context = array("Member" => $member);
     $this->assertTrue($discount->validateOrder($this->cart, $context), "Valid because correct member present" . $discount->getMessage());
 }
 public function testCategoryDiscount()
 {
     $discount = OrderDiscount::create(array("Title" => "5% off clothing", "Type" => "Percent", "Percent" => 0.05));
     $discount->write();
     $discount->Categories()->add($this->objFromFixture("ProductCategory", "clothing"));
     $this->assertTrue($discount->validateOrder($this->cart), "Order contains a t-shirt. " . $discount->getMessage());
     $calculator = new Calculator($this->cart);
     $this->assertEquals($calculator->calculate(), 0.4, "5% discount for socks in cart");
     $this->assertFalse($discount->validateOrder($this->othercart), "Order does not contain clothing");
     $calculator = new Calculator($this->othercart);
     $this->assertEquals($calculator->calculate(), 0, "No discount, because no product in category");
     $discount->Categories()->removeAll();
     $discount->Categories()->add($this->objFromFixture("ProductCategory", "kites"));
     $this->assertTrue($discount->validateOrder($this->kitecart), "Order contains a kite. " . $discount->getMessage());
     $calculator = new Calculator($this->kitecart);
     $this->assertEquals($calculator->calculate(), 1.75, "5% discount for kite in cart");
 }
 function testProductDiscount()
 {
     $discount = OrderDiscount::create(array("Title" => "20% off each selected products", "Percent" => 0.2, "Active" => 1, "ExactProducts" => 1));
     $discount->write();
     //selected products
     $discount->Products()->add($this->socks);
     $discount->Products()->add($this->tshirt);
     //should work for megacart
     //20 * socks($8) = 160 ...20% off each = 32
     //10 * tshirt($25) = 250 ..20% off each  = 50
     //2 * mp3player($200) = 400 ..nothing off = 0
     //total discount: 82
     $calculator = new Calculator($this->megacart);
     $this->assertEquals(82, $calculator->calculate(), "20% off selected products");
     //no discount for cart
     $calculator = new Calculator($this->cart);
     $this->assertEquals(0, $calculator->calculate(), "20% off selected products");
     //no discount for modifiedcart
     $calculator = new Calculator($this->modifiedcart);
     $this->assertEquals(0, $calculator->calculate(), "20% off selected products");
     //partial match
     $discount->ExactProducts = 0;
     $discount->write();
     //total discount: 82
     $calculator = new Calculator($this->megacart);
     $this->assertEquals(82, $calculator->calculate(), "20% off selected products");
     //discount for cart: 32 (just socks)
     $calculator = new Calculator($this->cart);
     $this->assertEquals(1.6, $calculator->calculate(), "20% off selected products");
     //no discount for modified cart
     $calculator = new Calculator($this->modifiedcart);
     $this->assertEquals(0, $calculator->calculate(), "20% off selected products");
     //get individual item discounts
     $discount = $this->objFromFixture("Product_OrderItem", "megacart_socks")->Discounts()->first();
     $this->assertEquals(32, $discount->DiscountAmount);
     $this->markTestIncomplete("Test variations also");
 }
 public function testShippingAndCartLevels()
 {
     if (!class_exists('ShippingFrameworkModifier')) {
         return;
     }
     $order = $this->othercart;
     //$200
     $shipping = new ShippingFrameworkModifier(array('Amount' => 30, 'OrderID' => $order->ID));
     $shipping->write();
     $order->Modifiers()->add($shipping);
     //order total is $230
     $discount = OrderDiscount::create(array("Title" => "\$10 off entire order", "Type" => "Amount", "Amount" => 10, "ForShipping" => 1, "ForItems" => 0, "ForCart" => 1));
     $discount->write();
     $calculator = new Calculator($order);
     $this->assertTrue($discount->validateOrder($order), "Discount is valid");
     $this->assertEquals(10, $calculator->calculate(), "\$10 is maximum discount");
 }
 public function testAmount()
 {
     OrderDiscount::create(array("Title" => "\$5 off", "Type" => "Amount", "Amount" => 5))->write();
     $this->assertDOSEquals(array(array("Title" => "\$5 off")), OrderDiscount::get_matching($this->cart));
 }