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();
 }
 protected function orderShippingAddress_50()
 {
     DB::alteration_message("\r\n\t\t\t<h1>50. Order Shipping Address</h1>\r\n\t\t\t<p>Move a shipping address from within Order to its own class.</p>\r\n\t\t");
     if ($this->hasTableAndField("Order", "ShippingAddress")) {
         if ($this->hasTableAndField("Order", "UseShippingAddress")) {
             $orders = DataObject::get('Order', "\"UseShippingAddress\" = 1 AND \"ShippingAddress\".\"ID\" IS NULL", "", " LEFT JOIN \"ShippingAddress\" ON \"Order\".\"ShippingAddressID\" = \"ShippingAddress\".\"ID\"", $this->start . "," . $this->limit);
             if ($orders) {
                 foreach ($orders as $order) {
                     if (!$order->ShippingAddressID) {
                         $obj = new ShippingAddress();
                         if (isset($order->ShippingName)) {
                             $obj->ShippingName = $order->ShippingName;
                         }
                         if (isset($order->ShippingAddress)) {
                             $obj->ShippingAddress = $order->ShippingAddress;
                         }
                         if (isset($order->ShippingAddress2)) {
                             $obj->ShippingAddress2 = $order->ShippingAddress2;
                         }
                         if (isset($order->ShippingCity)) {
                             $obj->ShippingCity = $order->ShippingCity;
                         }
                         if (isset($order->ShippingPostalCode)) {
                             $obj->ShippingPostalCode = $order->ShippingPostalCode;
                         }
                         if (isset($order->ShippingState)) {
                             $obj->ShippingState = $order->ShippingState;
                         }
                         if (isset($order->ShippingCountry)) {
                             $obj->ShippingCountry = $order->ShippingCountry;
                         }
                         if (isset($order->ShippingPhone)) {
                             $obj->ShippingPhone = $order->ShippingPhone;
                         }
                         if (isset($order->ShippingHomePhone)) {
                             $obj->ShippingPhone .= $order->ShippingHomePhone;
                         }
                         if (isset($order->ShippingMobilePhone)) {
                             $obj->ShippingMobilePhone = $order->ShippingMobilePhone;
                         }
                         $obj->OrderID = $order->ID;
                         $obj->write();
                         $order->ShippingAddressID = $obj->ID;
                         $order->write();
                     } else {
                         DB::alteration_message("Strange contradiction occurred in Order with ID" . $order->ID, "deleted");
                     }
                 }
                 return $this->start + $this->limit;
             } else {
                 DB::alteration_message("No orders need adjusting even though they followed the old pattern.");
             }
             $this->makeFieldObsolete("Order", "ShippingName");
             $this->makeFieldObsolete("Order", "ShippingAddress");
             $this->makeFieldObsolete("Order", "ShippingAddress2");
             $this->makeFieldObsolete("Order", "ShippingCity");
             $this->makeFieldObsolete("Order", "ShippingPostalCode");
             $this->makeFieldObsolete("Order", "ShippingState");
             $this->makeFieldObsolete("Order", "ShippingCountry");
             $this->makeFieldObsolete("Order", "ShippingPhone");
             $this->makeFieldObsolete("Order", "ShippingHomePhone");
             $this->makeFieldObsolete("Order", "ShippingMobilePhone");
         } else {
             DB::alteration_message("There is no UseShippingAddress field even though there is a ShippingAddress Field - this is an issue.", "deleted");
         }
     } else {
         DB::alteration_message("Orders do not have the shipping address to migrate.");
     }
     return 0;
 }