/**
  * getCMSFields
  * Construct the FieldList used in the CMS. To provide a
  * smarter UI we don't use the scaffolding provided by
  * parent::getCMSFields.
  * 
  * @return FieldList
  */
 public function getCMSFields()
 {
     Requirements::css('torindul-silverstripe-shop/css/LeftAndMain.css');
     $customer = $this->Customer();
     $fields = FieldList::create(Tabset::create("Root", Tabset::create("Order", Tab::create("CustomerDetails", HeaderField::create("Customer Selection"), CompositeField::create(DropdownField::create("CustomerID", $this->exists() ? "Customer" : "Select a Customer", Customer::get()->sort(array('Surname' => 'ASC', 'FirstName' => 'ASC'))->map())->setEmptyString("(Select a Customer)")->setRightTitle($this->exists() ? "To change the customer against this order select the new customer above (when editing from the\n\t\t\t\t\t\t\t\t'Store Orders' screen) and click save." : "Which customer is this order for?")), $this->exists() ? HeaderField::create("Customer Details") : null, $this->exists() ? CompositeField::create(ReadonlyField::create("Customer.FirstName", "First Name", $customer->FirstName), ReadonlyField::create("Customer.Surname", "Surname", $customer->Surname), ReadonlyField::create("Customer.CompanyName", "Company", $customer->CompanyName), ReadonlyField::create("Customer.Landline", "Landline Number", $customer->LandlineNumber), ReadonlyField::create("Customer.Mobile", "Mobile Number", $customer->MobileNumber)) : ""), Tab::create("OrderDetails", HeaderField::create("Order Details"), CompositeField::create(DropdownField::create("Status", "Order Status", Order_Statuses::get()->map()), DropdownField::create("BillingAddressID", "Billing Address", DataObject::get("Customer_AddressBook", "(`CustomerID`=" . $this->Customer()->ID . ")")->map())->setRightTitle("Customers' billing address.")->setEmptyString("(Select one)"), DropdownField::create("ShippingAddressID", "Shipping Address", DataObject::get("Customer_AddressBook", "(`CustomerID`=" . $this->Customer()->ID . ")")->map())->setRightTitle("Customers' shipping address.")->setEmptyString("(Select one)")), HeaderField::create("Order Items (Basket)"), CompositeField::create($items = GridField::create("OrderItems", "", $this->Order_Items(), GridFieldConfig_RecordEditor::create()), ReadonlyField::create("SubTotal", "Basket Total (" . Product::getDefaultCurrency() . ")", $this->calculateSubTotal())), HeaderField::create("Shipping"), CompositeField::create(FieldGroup::create(DropdownField::create("Courier", "Courier", $this->getCouriers($this->ID))->setRightTitle("Which courier is being used for this order?")->setEmptyString("(Select one)"), Textfield::create("TrackingNo", "Tracking Number (Optional)")->setRightTitle("If the shipment has a tracking number, enter it here.")), ReadonlyField::create("Shipping", "Shipping Total (" . Product::getDefaultCurrency() . ")", $this->calculateShippingTotal())), HeaderField::create("Order Totals"), CompositeField::create(FieldGroup::create("Tax (" . Product::getDefaultCurrency() . ")", ReadonlyField::create("ProductTaxInclusive", "Product Tax (Inclusive)", $this->calculateProductTax(1))->setRightTitle("Basket total is inclusive of this tax."), ReadonlyField::create("ProductTaxExclusive", "Product Tax (Exclusive)", $this->calculateProductTax(2))->setRightTitle("Basket total is exclusive of this tax."), ReadonlyField::create("ShippingTax", "Shipping Tax", $this->calculateShippingTax($this->calculateShippingTotal()))->setRightTitle(StoreSettings::get_settings()->TaxSettings_ShippingInclusiveExclusive == 1 ? "Shipping price is inclusive of this tax." : "Shipping price is exclusive of this tax.")), FieldGroup::create("Final Totals (" . Product::getDefaultCurrency() . ")", ReadonlyField::create("Total", "Final Total", $this->calculateOrderTotal()), ReadonlyField::create("TotalPayments", "Total Payments", $this->calculatePaymentTotal()), ReadonlyField::create("OutstandingBalance", "Outstanding Balance", $this->calculateRemainingBalance()))), HeaderField::create("Payments"), CompositeField::create($items = GridField::create("OrderPayments", "", $this->Order_Payment(), GridFieldConfig_RecordEditor::create()))), Tab::create("OrderComments", HeaderField::create("Order Comments"), CompositeField::create(ReadonlyField::create("CustomerComments", "Customer Comments")->setRightTitle("These are the comments of this customer."), TextareaField::create("AdminComments", "Admin Comments")->setRightTitle("Only store admins can see these comments"))), Tab::create("OrderEmails", HeaderField::create("Order Emails"), CompositeField::create($items = GridField::create("Order_Emails", "", $this->Order_Emails(), GridFieldConfig_RecordViewer::create()))))));
     //If record doesn't exist yet, hide certain tabs.
     !$this->exists() ? $fields->RemoveFieldsFromTab("Root.Order", array("OrderDetails", "OrderComments", "OrderEmails")) : null;
     /* 
      * If record exists, and order is unpaid show warning message to prevent shipment of unpaid orders.
      * Otherwise show a success message.
      */
     if ($this->exists() && $this->calculateRemainingBalance() > 0) {
         $alert = new LiteralField("OrderPayment_LiteralField", "<div class=\"literal-field field\">\n\t\t\t\t\t<div class=\"message error\">\n\t\t\t\t\t\t<strong>ORDER REMAINS UNPAID</strong> - This order has an outstanding balance of " . Product::getDefaultCurrency() . $this->calculateRemainingBalance() . ". If the customer has paid then the\n\t\t\t\t\t\t payment gateway may not have provided a payment status yet.\n\t\t\t\t\t</div>\n\t\t\t\t</div>");
         $fields->addFieldToTab("Root.Order", $alert, "Status");
     } elseif ($this->exists() && $this->calculateRemainingBalance() == 0) {
         $alert = new LiteralField("OrderPayment_LiteralField", "<div class=\"literal-field field\">\n\t\t\t\t\t<div class=\"message good\">\n\t\t\t\t\t\t<strong>BALANCE PAID</strong> - This balance of this order has been paid. You may now prepare and \n\t\t\t\t\t\tdispatch the order items below.\n\t\t\t\t\t</div> \n\t\t\t\t</div>");
         $fields->addFieldToTab("Root.Order", $alert, "Status");
     }
     return $fields;
 }
 /**
  * Add default records to database. This function is called whenever the
  * database is built, after the database tables have all been created.
  * 
  * @uses DataExtension->requireDefaultRecords()
  */
 public function requireDefaultRecords()
 {
     /* Inherit Default Record Creation */
     parent::requireDefaultRecords();
     /* If no records exist, create defaults */
     if (!DataObject::get_one(get_class($this))) {
         $defaults = array(array("Title" => "Pending / Awaiting Payment", "SystemTitle" => "Pending / Awaiting Payment", "Content" => "An order has been received but payment or notification of successful payment is still pending."), array("Title" => "Processing", "SystemTitle" => "Processing", "Content" => "Your payment has been received and your order is being processed."), array("Title" => "Awaiting Stock", "SystemTitle" => "Awaiting Stock", "Content" => "We are currently awaiting stock before we can progress your order."), array("Title" => "Completed", "SystemTitle" => "Completed", "Content" => "Your order is now complete."), array("Title" => "Shipped", "SystemTitle" => "Shipped", "Content" => "Your order has been handed to the courier and is on its way."), array("Title" => "Refunded", "SystemTitle" => "Refunded", "Content" => "Your order has been refunded."), array("Title" => "Part Refunded", "SystemTitle" => "Part Refunded", "Content" => "Your order has been part refunded."), array("Title" => "Cancelled", "SystemTitle" => "Cancelled", "Content" => "Your order has been cancelled."));
         foreach ($defaults as $default) {
             $n = new Order_Statuses();
             $n->Title = $default["Title"];
             $n->SystemTitle = $default["SystemTitle"];
             $n->Content = $default["Content"];
             $n->SystemCreated = "1";
             $n->write();
             unset($n);
         }
         DB::alteration_message('Created default order statuses', 'created');
     }
 }