title: Magento: can not create shipment for orders with an invoice
body: <p>im having problem with creating shipment for some orders programmatically.
  for the orders without an invoice, they get dispatched without any problems.</p>
  
  <p>i have pasted my code here, could you please help me to solve this issue.</p>
  
  <pre><code>&lt;?php 
   /*
  check if orderId is set    
  */
  if(isset($orderId)){
  $Semail = false;
  $includeComment = true;
  $shipment = false;
  
  
  /*
  Update order control systems table with tracking 
  */
  $dispatchOrder  = $sql-&gt;query("update _trackingOrders SET trackingNumber =   '".$trackingNumber."' where order_number = '".$orderId."'");
  $dispatchOrderCustomer  = $sql-&gt;query("update _trackingCustomer SET orderStatus =  'dispatched' where order_number = '".$orderId."'");
  /*
  End of updates
     */
  
  if(($dispatchOrder)&amp;&amp;($dispatchOrderCustomer)){
  
   /*
   Load order by id
   */
   $order = Mage::getModel('sales/order')-&gt;load($orderId);
  
   /*
   check if order exist
   */
    if (!$order-&gt;getId()) {
              echo 'The order no longer exists.';
              return false;
          }
  **EDIT:**
      /*
  check if order can be invoiced
  */
          if(!$order-&gt;canInvoice()){
              echo 'order can not be invoiced';
              return false;
          }
  /*
  check if order can be ship
  */
      if (!$order-&gt;canShip()) {
     echo 'Order can not be shipped';
     return false;
      }
       /*
      * New shipment coding begins here 
      */      
       $shipment = Mage::getModel('sales/service_order', $order)-&gt;prepareShipment();
  
        /*
        **END OF EDIT**
        */
  /* 
  get all items from the order
  */
  foreach ($order-&gt;getAllItems() as $orderItem) {
  
  if (!$orderItem-&gt;getQtyToShip()) {
      continue;
  }
  if ($orderItem-&gt;getIsVirtual()) {
      continue;
  }
  
  $item = $convertor-&gt;itemToShipmentItem($orderItem);
  
  $qty = $orderItem-&gt;getQtyToShip();
  
  $item-&gt;setQty($qty);
  $shipment-&gt;addItem($item);
  }   
  
  
  
  /*
  shipment details
  */
  $tracking = array();
  $tracking['carrier_code'] = 'Royalmail';
  $tracking['title'] = $shippingMethod;
  $tracking['number'] = $trackingNumber;
  
  /*
  add shipment tracking details
  */
  $track = Mage::getModel('sales/order_shipment_track')-&gt;addData($tracking);
  $shipment-&gt;addTrack($track);
  
  /*
  register current shipment
  */
  Mage::register('current_shipment_'.$orderId.'', $shipment);
  
  /*
  register shipment
  add comment to shipment
  send shipment email
  */  
  $shipment-&gt;register();
  $shipment-&gt;addComment($comments, $Semail &amp;&amp; $includeComment);
  $shipment-&gt;setEmailSent(true);
  
  /*
  set order state as processign
  set order is in process
  set order status as complete
  */
  $shipment-&gt;getOrder()-&gt;setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);
  $shipment-&gt;getOrder()-&gt;setIsInProcess(false);
  $shipment-&gt;getOrder()-&gt;setStatus('complete');
  
  
  /*
  get shipment order
  add statushistory
  */
  $_order = $shipment-&gt;getOrder();
  $_order-&gt;addStatusHistoryComment($comments, 'complete')
                  -&gt;setIsVisibleOnFront(1)
                  -&gt;setIsCustomerNotified(1);
          $_order-&gt;sendOrderUpdateEmail('1', $comments);
     $_order-&gt;save();
  
  
  /*
  save transactions
  */
  $transactionSave = Mage::getModel('core/resource_transaction')
  -&gt;addObject($shipment)
  -&gt;addObject($shipment-&gt;getOrder())
  -&gt;save();
  
  echo "order has been dispatched";
  ?&gt;
  </code></pre>
  
  <p>Thanks inadvance.</p>
  
  <p>Jey</p>