/** * Tries to create an order item with a non-existent version. */ function testProductVersionDoesNotExist() { $currentorder = ShoppingCart::current_order(); $brokenitem = new Product_OrderItem(array("ProductID" => $productSocks->ID, "ProductVersion" => 99999)); $this->assertEquals($brokenitem->UnitPrice(), null); //TODO: what should happen here??? }
public function testGetTotalStockInCarts() { $this->setStockFor($this->phone, 10); $order = new Order(array('Status' => 'Cart')); $order->write(); $orderItem = new Product_OrderItem(array('ProductID' => $this->phone->ID, 'OrderID' => $order->ID, 'Quantity' => '5')); $orderItem->write(); $this->assertEquals(5, $this->phone->getTotalStockInCarts()); // test variations $this->setStockFor($this->ballRedSmall, 5); $orderItem = $orderItem->newClassInstance('ProductVariation_OrderItem'); $orderItem->ProductVariationID = $this->ballRedSmall->ID; $orderItem->write(); $this->assertEquals(5, $this->ballRedSmall->getTotalStockInCarts()); }
function getUnitPrice($recalculate = false) { $unitPrice = 0; if ($this->priceHasBeenFixed($recalculate) && !$recalculate) { $unitPrice = parent::getUnitPrice($recalculate); } elseif ($productVariation = $this->ProductVariation()) { if (!isset(self::$calculated_buyable_price[$this->ID]) || $recalculate) { self::$calculated_buyable_price[$this->ID] = $productVariation->getCalculatedPrice(); } $unitPrice = self::$calculated_buyable_price[$this->ID]; } else { $unitPrice = 0; } $updatedUnitPrice = $this->extend('updateUnitPrice', $unitPrice); if ($updatedUnitPrice !== null && is_array($updatedUnitPrice) && count($updatedUnitPrice)) { $unitPrice = $updatedUnitPrice[0]; } return $unitPrice; }
function requireDefaultRecords() { parent::requireDefaultRecords(); // we must check for individual database types here because each deals with schema in a none standard way //can we use Table::has_field ??? $db = DB::getConn(); if ($db->hasTable("Product_OrderItem")) { if ($db instanceof PostgreSQLDatabase) { $exist = DB::query("SELECT column_name FROM information_schema.columns WHERE table_name ='Product_OrderItem' AND column_name = 'ProductVariationVersion'")->numRecords(); } else { // default is MySQL - broken for others, each database conn type supported must be checked for! $exist = DB::query("SHOW COLUMNS FROM \"Product_OrderItem\" LIKE 'ProductVariationVersion'")->numRecords(); } if ($exist > 0) { DB::query("\n\t\t\t\t\tUPDATE \"OrderItem\", \"ProductVariation_OrderItem\"\n\t\t\t\t\t\tSET \"OrderItem\".\"Version\" = \"ProductVariation_OrderItem\".\"ProductVariationVersion\"\n\t\t\t\t\tWHERE \"OrderItem\".\"ID\" = \"ProductVariation_OrderItem\".\"ID\"\n\t\t\t\t"); DB::query("\n\t\t\t\t\tUPDATE \"OrderItem\", \"ProductVariation_OrderItem\"\n\t\t\t\t\t\tSET \"OrderItem\".\"BuyableID\" = \"ProductVariation_OrderItem\".\"ProductVariationID\"\n\t\t\t\t\tWHERE \"OrderItem\".\"ID\" = \"ProductVariation_OrderItem\".\"ID\"\n\t\t\t\t"); DB::query("ALTER TABLE \"ProductVariation_OrderItem\" CHANGE COLUMN \"ProductVariationVersion\" \"_obsolete_ProductVariationVersion\" Integer(11)"); DB::query("ALTER TABLE \"ProductVariation_OrderItem\" CHANGE COLUMN \"ProductVariationID\" \"_obsolete_ProductVariationID\" Integer(11)"); DB::alteration_message('made ProductVariationVersion and ProductVariationID obsolete in ProductVariation_OrderItem', 'obsolete'); } } }
public function debug() { $title = $this->TableTitle(); $productVariationID = $this->_productVariationID; $productVariationVersion = $this->_productVariationVersion; return parent::debug() . <<<HTML \t\t\t<h3>ProductVariation_OrderItem class details</h3> \t\t\t<p> \t\t\t\t<b>Title : </b>{$title}<br/> \t\t\t\t<b>ProductVariation ID : </b>{$productVariationID}<br/> \t\t\t\t<b>ProductVariation Version : </b>{$productVariationVersion}<br/> \t\t\t</p> HTML; }
function onBeforeDelete() { parent::onBeforeDelete(); CartResponse::set_force_reload(); }
/** * Tries to create an order item with a non-existent version. */ public function testProductVersionDoesNotExist() { $brokenitem = new Product_OrderItem(array("ProductID" => $this->socks->ID, "ProductVersion" => 99999)); $this->assertNull($brokenitem->Product(), "version does not exist"); }
/** * Create vouchers on order payment success event */ public function onPayment() { parent::onPayment(); if ($this->Coupons()->Count() < $this->Quantity) { $remaining = $this->Quantity - $this->Coupons()->Count(); for ($i = 0; $i < $remaining; $i++) { if ($coupon = $this->createCoupon()) { $this->sendVoucher($coupon); } } } }
public function testDelete() { Config::inst()->update('FlatTaxModifier', 'rate', 0.25); Config::inst()->update('Order', 'modifiers', array('FlatTaxModifier')); $order = Order::create(); $shirt = $this->objFromFixture("Product", "tshirt"); $mp3player = $this->objFromFixture("Product", "mp3player"); $order->Items()->add($shirt->createItem(3)); $order->Items()->add($mp3player->createItem(1)); $order->write(); $order->calculate(); $statusLogId = OrderStatusLog::create(array('Title' => 'Test status log', 'OrderID' => $order->ID))->write(); $paymentId = Payment::create(array('OrderID' => $order->ID))->init('Manual', 343.75, 'NZD')->write(); $this->assertEquals(4, $order->Items()->Quantity()); $this->assertEquals(1, $order->Modifiers()->count()); $this->assertEquals(1, $order->OrderStatusLogs()->count()); $this->assertEquals(1, $order->Payments()->count()); $itemIds = Product_OrderItem::get()->filter('OrderID', $order->ID)->column('ID'); $modifierIds = OrderModifier::get()->filter('OrderID', $order->ID)->column('ID'); $order->delete(); // Items should no longer be linked to order $this->assertEquals(0, $order->Items()->count()); $this->assertEquals(0, $order->Modifiers()->count()); $this->assertEquals(0, $order->OrderStatusLogs()->count()); $this->assertEquals(0, $order->Payments()->count()); // Ensure the order items have been deleted! $this->assertEquals(0, Product_OrderItem::get()->filter('ID', $itemIds)->count()); $this->assertEquals(0, OrderModifier::get()->filter('ID', $modifierIds)->count()); $this->assertEquals(0, OrderStatusLog::get()->filter('ID', $statusLogId)->count()); // Keep the payment… it might be relevant for book keeping $this->assertEquals(1, Payment::get()->filter('ID', $paymentId)->count()); }
private function createorder() { $order = new Order(); $order->UseShippingAddress = true; $order->CustomerOrderNote = "THIS IS AN AUTO-GENERATED ORDER"; $order->write(); $member = new Member(); $member->FirstName = 'Tom'; $member->Surname = 'Cruize'; $member->Email = '*****@*****.**'; $member->Password = '******'; $member->write(); $order->MemberID = $member->ID; $billingAddress = new BillingAddress(); $billingAddress->Prefix = "Dr"; $billingAddress->FirstName = "Tom"; $billingAddress->Surname = "Cruize"; $billingAddress->Address = "Lamp Drive"; $billingAddress->Address2 = "Linux Mountain"; $billingAddress->City = "Apache Town"; $billingAddress->PostalCode = "555"; $billingAddress->Country = "NZ"; $billingAddress->Phone = "555 5555555"; $billingAddress->Email = "*****@*****.**"; $billingAddress->write(); $order->BillingAddressID = $billingAddress->ID; $shippingAddress = new ShippingAddress(); $shippingAddress->ShippingPrefix = "Dr"; $shippingAddress->ShippingFirstName = "Tom"; $shippingAddress->ShippingSurname = "Cruize"; $shippingAddress->ShippingAddress = "Lamp Drive"; $shippingAddress->ShippingAddress2 = "Linux Mountain"; $shippingAddress->ShippingCity = "Apache Town"; $shippingAddress->ShippingPostalCode = "555"; $shippingAddress->ShippingCountry = "NZ"; $shippingAddress->ShippingPhone = "555 5555555"; $shippingAddress->write(); $order->ShippingAddressID = $shippingAddress->ID; //get a random product $extension = ""; if (Versioned::current_stage() == "Live") { $extension = "_Live"; } $count = 0; $noProductYet = true; $triedArray = array(0 => 0); while ($noProductYet && $count < 50) { $product = Product::get()->where("\"ClassName\" = 'Product' AND \"Product{$extension}\".\"ID\" NOT IN (" . implode(",", $triedArray) . ") AND Price > 0")->First(); if ($product) { if ($product->canPurchase()) { $noProductYet = false; } else { $triedArray[] = $product->ID; } } $count++; } //adding product order item $item = new Product_OrderItem(); $item->addBuyableToOrderItem($product, 7); $item->OrderID = $order->ID; $item->write(); //final save $order->write(); $order->tryToFinaliseOrder(); }
public function onPlacement() { parent::onPlacement(); $this->isPlaced = true; }
/** * creates order from repeatorder for a specific day. * IF it does not already exists. * */ protected function createOrderFromRepeatOrder($orderDateInteger) { if ($order = DataObject::get_one("Order", "\"OrderDateInteger\" = '" . $orderDateInteger . "' AND \"RepeatOrderID\" = " . $this->ID)) { //do nothing } else { $order = new Order(); $order->OrderDate = date("Y-m-d", $orderDateInteger); $order->OrderDateInteger = $orderDateInteger; $order->RepeatOrderID = $this->ID; $order->MemberID = $this->MemberID; $order->CustomerOrderNote = "Created as part of a repeating order."; $order->write(); if ($this->OrderItems()) { foreach ($this->OrderItems() as $repeatOrderOrderItem) { $product = DataObject::get_by_id('Product', $repeatOrderOrderItem->ProductID); if ($product) { //START CHECK AVAILABILITY if (class_exists("ProductStockCalculatedQuantity")) { $numberAvailable = ProductStockCalculatedQuantity::get_quantity_by_product_id($product->ID); if ($numberAvailable < $repeatOrderOrderItem->Quantity) { $alternatives = $repeatOrderOrderItem->AlternativesPerProduct(); $product = null; if ($dos) { foreach ($alternatives as $alternative) { $stillLookingForAlternative = true; $numberAvailable = ProductStockCalculatedQuantity::get_quantity_by_product_id($alternative->ID); if ($numberAvailable > $repeatOrderOrderItem->Quantity && $stillLookingForAlternative) { $stillLookingForAlternative = false; $product = $alternative; } } } } } //END CHECK AVAILABILITY if ($product) { $newProductOrderItem = new Product_OrderItem(); $newProductOrderItem->addBuyableToOrderItem($product, $repeatOrderOrderItem->Quantity); $newProductOrderItem->OrderID = $order->ID; $newProductOrderItem->write(); } } else { USER_ERROR("Product does not exist", E_USER_WARNING); } } } else { USER_ERROR("There are no order items", E_USER_WARNING); } //FINALISE!!! $order->write(); $order->tryToFinaliseOrder(); } }