/**
  * Removes any virtual gift card items from the order create request and
  * re-adds them as parts of ship groups with destinations using the recipient
  * email address as the destination.
  *
  * @param Mage_Sales_Model_Order
  * @param IOrderCreateRequest
  * @return self
  */
 public function fixOrderCreateVirtualGiftCards(Mage_Sales_Model_Order $order, IOrderCreateRequest $orderCreateRequest)
 {
     $virtualGiftCards = $this->getOrderedVirtualGiftCards($order);
     if ($virtualGiftCards) {
         $this->logger->debug('Order contains {count} virtual gift cards', $this->logContext->getMetaData(__CLASS__, ['count' => count($virtualGiftCards)]));
         $shipGroups = $orderCreateRequest->getShipGroups();
         $destinations = $orderCreateRequest->getDestinations();
         // Assume virtual gift cards were added to the wrong ship group
         // with the wrong destination. Remove them from where they are to
         // re-add them in the right place.
         $removedItemPayloads = $this->removeVirtualGiftCardPayloads($shipGroups, $virtualGiftCards);
         $this->logger->debug('Removed {count} virtual gift card item payloads', $this->logContext->getMetaData(__CLASS__, ['count' => count($removedItemPayloads)]));
         // Each of the removed items needs to be added back into the OCR
         // as part of a ship group with the recipient's email address as the
         // destination of the ship group.
         foreach ($removedItemPayloads as $itemRefPair) {
             $shipGroup = $this->createShipGroupForVirtualGiftCard($itemRefPair, $shipGroups, $destinations);
             $shipGroups->offsetSet($shipGroup);
         }
         // After pulling virtual gift cards out of existing ship groups, make
         // sure the OCR isn't left with any empty ship groups.
         $this->removeEmptyShipGroups($shipGroups, $destinations);
         $orderCreateRequest->setShipGroups($shipGroups);
     } else {
         $this->logger->debug('Order does not contain any virtual gift cards', $this->logContext->getMetaData(__CLASS__));
     }
     return $this;
 }
 /**
  * Set the tax has errors flag on the order create request if the tax
  * collector or any of the collected tax records contain an error.
  *
  * @param  IOrderCreateRequest
  * @param  Mage_Sales_Model_Order
  * @return self
  */
 public function addTaxHeaderErrorFlag()
 {
     // This may be made cleaner by just setting the flag to the result
     // of this condition but it is unclear if not setting the flag is the
     // same as setting the flag to false.
     if ($this->_taxCollectorHasErrors() || $this->_itemsHaveErrors()) {
         $this->_orderCreateRequest->setTaxHasErrors(true);
     }
     return $this;
 }
 /**
  * Detect an order as a test order when the second street line
  * address of the order billing address matches the constant
  * IOrderCreateRequest::TEST_TYPE_AUTOCANCEL. Flag the OCR
  * payload as a test order.
  *
  * @return self
  */
 protected function handleTestOrder()
 {
     /** @var Mage_Customer_Model_Address_Abstract */
     $billingAddress = $this->_order->getBillingAddress();
     if ($this->isTestOrder($billingAddress)) {
         $this->_payload->setTestType(IOrderCreateRequest::TEST_TYPE_AUTOCANCEL);
     }
     return $this;
 }
 /**
  * Set the tax has errors flag on the order create request if the tax
  * collector or any of the collected tax records contain an error.
  *
  * @param  IOrderCreateRequest
  * @param  Mage_Sales_Model_Order
  * @return self
  */
 public function addTaxHeaderErrorFlag()
 {
     $hasErrors = $this->_taxCollectorHasErrors() || $this->_itemsHaveErrors();
     $this->_orderCreateRequest->setTaxHasErrors($hasErrors);
     return $this;
 }
 /**
  * Add ship groups to the request. For each address in the order, dispatch
  * an event for handling ship group destinations. For each ship group
  * destination added, trigger additional events for each item in the ship
  * group.
  *
  * @param Mage_Sales_Model_Order
  * @param IOrderCreateRequest
  * @return self
  */
 protected function _setShipGroups(Mage_Sales_Model_Order $order, IOrderCreateRequest $request)
 {
     $shipGroups = $request->getShipGroups();
     $orderItems = $request->getOrderItems();
     $destinations = $request->getDestinations();
     $itemCollection = $order->getItemsCollection();
     foreach ($order->getAddressesCollection() as $address) {
         $items = $this->_getItemsForAddress($address, $itemCollection);
         if ($items) {
             $shipGroups->offsetSet($this->_buildShipGroupForAddress($address, $items, $order, $shipGroups, $destinations, $orderItems));
         }
     }
     return $this;
 }
 /**
  * Validate the relationship references within an order create request
  * only reference known order items in the request.
  *
  * @param IOrderCreateRequest
  * @return self
  */
 protected function validateItemRelationshipReferences(IOrderCreateRequest $payload)
 {
     $relationships = $payload->getItemRelationships();
     $orderItems = $payload->getOrderItems();
     foreach ($relationships as $relationship) {
         $parentItem = $relationship->getParentItem();
         $this->validateOptionalPayloadReference($relationship, 'parent item', $orderItems, $parentItem)->validateOrderItemReferences($relationship, $orderItems);
     }
     return $this;
 }