public function ItemEditForm()
 {
     Requirements::javascript("orders/javascript/entwine.orders.js");
     $form = parent::ItemEditForm();
     $fields = $form->Fields();
     $actions = $form->Actions();
     $record = $this->record;
     $member = Member::currentUser();
     $can_view = $this->record->canView();
     $can_edit = $this->record->canEdit();
     $can_change_status = $this->record->canChangeStatus();
     $can_delete = $this->record->canDelete();
     $can_create = $this->record->canCreate();
     // First remove the delete button
     $actions->removeByName("action_doDelete");
     // Deal with Estimate objects
     if ($record->ClassName == "Estimate") {
         if ($record->ID && $record->AccessKey) {
             $frontend_url = Controller::join_links(Director::absoluteBaseUrl(), "OrdersFront", "quote", $record->ID, $record->AccessKey);
             $html = '<a href="' . $frontend_url . '" ';
             $html .= 'target="_blank" ';
             $html .= 'class="action ss-ui-button ui-button ui-corner-all open-external" ';
             $html .= '>' . _t('Orders.ViewQuote', 'View Quote') . '</a>';
             $actions->insertAfter(LiteralField::create('openQuote', $html), "action_doSave");
         }
         if ($record->ID && $can_edit) {
             $actions->insertAfter(FormAction::create('doConvert', _t('Orders.ConvertToOrder', 'Convert To Order'))->setUseButtonTag(true), "action_doSave");
         }
     }
     // Deal with Order objects
     if ($record->ClassName == "Order") {
         // Set our status field as a dropdown (has to be here to
         // ignore canedit)
         // Allow users to change status (as long as they have permission)
         if ($can_edit || $can_change_status) {
             $status_field = DropdownField::create('Status', null, $record->config()->statuses);
             // Set default status if we can
             if (!$record->Status && !$record->config()->default_status) {
                 $status_field->setValue($record->config()->default_status);
             } else {
                 $status_field->setValue($record->Status);
             }
             $fields->replaceField("Status", $status_field);
         }
         // Setup order history
         if (Permission::check(array('COMMERCE_ORDER_HISTORY', 'ADMIN'), 'any', $member)) {
             $versions = $record->AllVersions();
             $first_version = $versions->First();
             $curr_version = $first_version ? $versions->First() : null;
             $message = "";
             foreach ($versions as $version) {
                 $i = $version->Version;
                 $name = "History_{$i}";
                 if ($i > 0) {
                     $frm = Versioned::get_version($record->class, $record->ID, $i - 1);
                     $to = Versioned::get_version($record->class, $record->ID, $i);
                     $diff = new DataDifferencer($frm, $to);
                     if ($version->Author()) {
                         $message = "<p>{$version->Author()->FirstName} ({$version->LastEdited})</p>";
                     } else {
                         $message = "<p>Unknown ({$version->LastEdited})</p>";
                     }
                     if ($diff->ChangedFields()->exists()) {
                         $message .= "<ul>";
                         // Now loop through all changed fields and track as message
                         foreach ($diff->ChangedFields() as $change) {
                             if ($change->Name != "LastEdited") {
                                 $message .= "<li>{$change->Title}: {$change->Diff}</li>";
                             }
                         }
                         $message .= "</ul>";
                     }
                     $fields->addFieldToTab("Root.History", LiteralField::create($name, "<div class=\"field\">{$message}</div>"));
                 }
             }
         }
         // Is user cannot edit, but can change status, add change
         // status button
         if ($record->ID && !$can_edit && $can_change_status) {
             $actions->push(FormAction::create('doChangeStatus', _t('Orders.Save', 'Save'))->setUseButtonTag(true)->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept'));
         }
         if ($record->ID && $record->AccessKey) {
             $frontend_url = Controller::join_links(Director::absoluteBaseUrl(), "OrdersFront", "invoice", $record->ID, $record->AccessKey);
             $html = '<a href="' . $frontend_url . '" ';
             $html .= 'target="_blank" ';
             $html .= 'class="action ss-ui-button ui-button ui-corner-all open-external" ';
             $html .= '>' . _t('Orders.ViewInvoice', 'View Invoice') . '</a>';
             $link_field = LiteralField::create('openQuote', $html);
             if ($actions->find("Name", "action_doSave")) {
                 $actions->insertAfter($link_field, "action_doSave");
             }
             if ($actions->find("Name", "action_doChangeStatus")) {
                 $actions->insertAfter($link_field, "action_doChangeStatus");
             }
         }
     }
     // Add a duplicate button, either after the save button or
     // the change status "save" button.
     if ($record->ID) {
         $duplicate_button = FormAction::create('doDuplicate', _t('Orders.Duplicate', 'Duplicate'))->setUseButtonTag(true);
         if ($actions->find("Name", "action_doSave")) {
             $actions->insertAfter($duplicate_button, "action_doSave");
         }
         if ($actions->find("Name", "action_doChangeStatus")) {
             $actions->insertAfter($duplicate_button, "action_doChangeStatus");
         }
     }
     // Finally, if allowed, re-add the delete button (so it is last)
     if ($record->ID && $can_delete) {
         $actions->push(FormAction::create('doDelete', _t('GridFieldDetailForm.Delete', 'Delete'))->setUseButtonTag(true)->addExtraClass('ss-ui-action-destructive action-delete'));
     }
     // Set our custom template
     $form->setTemplate("OrdersItemEditForm");
     $this->extend("updateItemEditForm", $form);
     return $form;
 }
 public function getCMSFields()
 {
     $fields = parent::getCMSFields();
     // Remove defailt item admin
     $fields->removeByName('Items');
     $fields->removeByName('EmailDispatchSent');
     $fields->removeByName('PostageID');
     $fields->removeByName('PaymentID');
     $fields->removeByName('GatewayData');
     // Remove Billing Details
     $fields->removeByName('Address1');
     $fields->removeByName('Address2');
     $fields->removeByName('City');
     $fields->removeByName('PostCode');
     $fields->removeByName('Country');
     // Remove Delivery Details
     $fields->removeByName('DeliveryFirstnames');
     $fields->removeByName('DeliverySurname');
     $fields->removeByName('DeliveryAddress1');
     $fields->removeByName('DeliveryAddress2');
     $fields->removeByName('DeliveryCity');
     $fields->removeByName('DeliveryPostCode');
     $fields->removeByName('DeliveryCountry');
     // Remove default postage fields
     $fields->removeByName('PostageType');
     $fields->removeByName('PostageCost');
     $fields->removeByName('PostageTax');
     $fields->addFieldToTab('Root.Main', ReadonlyField::create('OrderNumber', "#"), 'Status');
     $fields->addFieldToTab('Root.Main', ReadonlyField::create('Created'));
     $fields->addFieldToTab('Root.Main', ReadonlyField::create('LastEdited', 'Last time order was saved'));
     // Structure billing details
     $billing_fields = ToggleCompositeField::create('BillingDetails', 'Billing Details', array(TextField::create('Address1', 'Address 1'), TextField::create('Address2', 'Address 2'), TextField::create('City', 'City'), TextField::create('PostCode', 'Post Code'), TextField::create('Country', 'Country')))->setHeadingLevel(4);
     // Structure delivery details
     $delivery_fields = ToggleCompositeField::create('DeliveryDetails', 'Delivery Details', array(TextField::create('DeliveryFirstnames', 'First Name(s)'), TextField::create('DeliverySurname', 'Surname'), TextField::create('DeliveryAddress1', 'Address 1'), TextField::create('DeliveryAddress2', 'Address 2'), TextField::create('DeliveryCity', 'City'), TextField::create('DeliveryPostCode', 'Post Code'), TextField::create('DeliveryCountry', 'Country')))->setHeadingLevel(4);
     // Postage details
     // Structure billing details
     $postage_fields = ToggleCompositeField::create('Postage', 'Postage Details', array(ReadonlyField::create('PostageType'), ReadonlyField::create('PostageCost'), ReadonlyField::create('PostageTax')))->setHeadingLevel(4);
     $fields->addFieldToTab('Root.Main', $billing_fields);
     $fields->addFieldToTab('Root.Main', $delivery_fields);
     $fields->addFieldToTab('Root.Main', $postage_fields);
     // Add order items and totals
     $fields->addFieldToTab('Root.Items', GridField::create('Items', "Order Items", $this->Items(), GridFieldConfig::create()->addComponents(new GridFieldSortableHeader(), new GridFieldDataColumns())));
     $fields->addFieldToTab("Root.Items", ReadonlyField::create("SubTotal")->setValue($this->getSubTotal()));
     $fields->addFieldToTab("Root.Items", ReadonlyField::create("DiscountAmount")->setValue($this->DiscountAmount));
     $fields->addFieldToTab("Root.Items", ReadonlyField::create("Tax")->setValue($this->getTaxTotal()));
     $fields->addFieldToTab("Root.Items", ReadonlyField::create("Total")->setValue($this->getTotal()));
     $member = Member::currentUser();
     if (Permission::check('ADMIN', 'any', $member)) {
         // Add non-editable payment ID
         $paymentid_field = TextField::create('PaymentID', "Payment gateway ID number")->setReadonly(true)->performReadonlyTransformation();
         $gateway_data = LiteralField::create("FormattedGatewayData", "<strong>Data returned from the payment gateway:</strong><br/><br/>" . str_replace(",", ",<br/>", $this->GatewayData));
         $fields->addFieldToTab('Root.Gateway', $paymentid_field);
         $fields->addFieldToTab("Root.Gateway", $gateway_data);
     }
     if (Permission::check(array('COMMERCE_ORDER_HISTORY', 'ADMIN'), 'any', $member)) {
         // Setup basic history of this order
         $versions = $this->AllVersions();
         $curr_version = $versions->First()->Version;
         $message = "";
         foreach ($versions as $version) {
             $i = $version->Version;
             $name = "History_{$i}";
             if ($i > 1) {
                 $frm = Versioned::get_version($this->class, $this->ID, $i - 1);
                 $to = Versioned::get_version($this->class, $this->ID, $i);
                 $diff = new DataDifferencer($frm, $to);
                 if ($version->Author()) {
                     $message = "<p>{$version->Author()->FirstName} ({$version->LastEdited})</p>";
                 } else {
                     $message = "<p>Unknown ({$version->LastEdited})</p>";
                 }
                 if ($diff->ChangedFields()->exists()) {
                     $message .= "<ul>";
                     // Now loop through all changed fields and track as message
                     foreach ($diff->ChangedFields() as $change) {
                         if ($change->Name != "LastEdited") {
                             $message .= "<li>{$change->Title}: {$change->Diff}</li>";
                         }
                     }
                     $message .= "</ul>";
                 }
             }
             $fields->addFieldToTab("Root.History", LiteralField::create($name, "<div class=\"field\">{$message}</div>"));
         }
     }
     $this->extend("updateCMSFields", $fields);
     return $fields;
 }