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 memberBillingAddress_52()
 {
     DB::alteration_message("\r\n\t\t\t<h1>52. Member Billing Address</h1>\r\n\t\t\t<p>Move address details in the member table to its own class (billingaddress)</p>\r\n\t\t");
     if ($this->hasTableAndField("Member", "Address")) {
         if ($this->hasTableAndField("Member", "City")) {
             $orders = DataObject::get('Order', "\"MemberID\" > 0 AND \"BillingAddress\".\"ID\" IS NULL AND \"BillingAddressID\" = 0", "", " LEFT JOIN \"BillingAddress\" ON \"Order\".\"BillingAddressID\" = \"BillingAddress\".\"ID\"", $this->start . "," . $this->limit);
             if ($orders) {
                 foreach ($orders as $order) {
                     if (!$order->BillingAddressID) {
                         $member = DataObject::get_by_id("Member", $order->MemberID);
                         if ($member) {
                             $obj = new BillingAddress();
                             if (isset($member->Email)) {
                                 $obj->BillingEmail = $member->Email;
                             }
                             if (isset($member->FirstName)) {
                                 $obj->BillingFirstName = $member->FirstName;
                             }
                             if (isset($member->Surname)) {
                                 $obj->BillingSurname = $member->Surname;
                             }
                             if (isset($member->Address)) {
                                 $obj->BillingAddress = $member->Address;
                             }
                             if (isset($member->AddressLine2)) {
                                 $obj->BillingAddress2 = $member->AddressLine2;
                             }
                             if (isset($member->City)) {
                                 $obj->BillingCity = $member->City;
                             }
                             if (isset($member->PostalCode)) {
                                 $obj->BillingPostalCode = $member->PostalCode;
                             }
                             if (isset($member->State)) {
                                 $obj->BillingState = $member->State;
                             }
                             if (isset($member->Country)) {
                                 $obj->BillingCountry = $member->Country;
                             }
                             if (isset($member->Phone)) {
                                 $obj->BillingPhone = $member->Phone;
                             }
                             if (isset($member->HomePhone)) {
                                 $obj->BillingPhone .= $member->HomePhone;
                             }
                             if (isset($member->MobilePhone)) {
                                 $obj->MobilePhone = $member->MobilePhone;
                             }
                             $obj->OrderID = $order->ID;
                             $obj->write();
                             $order->BillingAddressID = $obj->ID;
                             $order->write();
                         } else {
                             DB::alteration_message("There is no memmber associated with this order " . $order->ID, "deleted");
                         }
                     } else {
                         DB::alteration_message("Strange contraduction occurred!", "deleted");
                     }
                 }
                 $this->start + $this->limit;
             } else {
                 DB::alteration_message("No orders need adjusting even though they followed the old pattern.");
             }
         } else {
             DB::alteration_message("There is no Address2 field, but there is an Address field in Member - this might be an issue.", "deleted");
         }
     } else {
         DB::alteration_message("Members do not have a billing address to migrate.");
     }
     return 0;
 }