能够以编程方式创建Magento货件但无法将其标记为已发货吗?

I have the following code that would create a shipment for an order. But still the shipped item isn't marked as shipped. And SHIP button at the top is still there. Therefore, I cant create RMA if needed.

Please check screenshot (link)

$order = Mage::getModel('sales/order') -> loadByIncrementId($order_id);
$itemQty = $order -> getItemsCollection() -> count();

$convertOrder = new Mage_Sales_Model_Convert_Order();
$shipment = Mage::getModel('sales/service_order', $order) -> prepareShipment($itemQty);

$items = $order -> getAllItems();

foreach ($items as $item) {
    $shipped_item = $convertOrder -> itemToShipmentItem($item);
    $shipped_item -> setQty($item -> getQtyOrdered());
    $shipment -> addItem($shipped_item);
}

$shipment -> register();
$shipment -> setOrder($order);
$shipment -> save();

I always hop to the source for problems like these. The code that determines if that button displays is here

    #File: app/code/core/Mage/Adminhtml/Block/Sales/Order/View.php
    if ($this->_isAllowedAction('ship') && $order->canShip()
        && !$order->getForcedDoShipmentWithInvoice()) {
        $this->_addButton('order_ship', array(
            'label'     => Mage::helper('sales')->__('Ship'),
            'onclick'   => 'setLocation(\'' . $this->getShipUrl() . '\')',
            'class'     => 'go'
        ));
    }

Looking at that, your best bet is the canShip method

#File: app/code/core/Mage/Sales/Model/Order.php
public function canShip()
{
    if ($this->canUnhold() || $this->isPaymentReview()) {
        return false;
    }

    if ($this->getIsVirtual() || $this->isCanceled()) {
        return false;
    }

    if ($this->getActionFlag(self::ACTION_FLAG_SHIP) === false) {
        return false;
    }

    foreach ($this->getAllItems() as $item) {
        if ($item->getQtyToShip()>0 && !$item->getIsVirtual()
            && !$item->getLockedDoShip())
        {
            return true;
        }
    }
    return false;
}

Drop some var_dump/Mage::log debugging in here and you should be able to figure out why Magento thinks it needs to display the shipping button. Once you know that, you should be able to figure out what additional state you need to save.