/** * Prepare order shipment based on order items and requested items qty * * @param array $data * @return Mage_Sales_Model_Order_Shipment */ public function prepareShipment($qtys = array()) { $totalQty = 0; $shipment = $this->_convertor->toShipment($this->_order); foreach ($this->_order->getAllItems() as $orderItem) { if (!$this->_canShipItem($orderItem, $qtys)) { continue; } $item = $this->_convertor->itemToShipmentItem($orderItem); if ($orderItem->isDummy(true)) { $qty = 1; } else { if (isset($qtys[$orderItem->getId()])) { $qty = min($qtys[$orderItem->getId()], $orderItem->getQtyToShip()); } elseif (!count($qtys)) { $qty = $orderItem->getQtyToShip(); } else { continue; } } $totalQty += $qty; $item->setQty($qty); $shipment->addItem($item); } $shipment->setTotalQty($totalQty); return $shipment; }
/** * Prepare order shipment based on order items and requested items qty * * @param array $qtys * @return Mage_Sales_Model_Order_Shipment */ public function prepareShipment($qtys = array()) { $this->updateLocaleNumbers($qtys); $totalQty = 0; $shipment = $this->_convertor->toShipment($this->_order); foreach ($this->_order->getAllItems() as $orderItem) { if (!$this->_canShipItem($orderItem, $qtys)) { continue; } $item = $this->_convertor->itemToShipmentItem($orderItem); if ($orderItem->isDummy(true)) { $qty = 0; if (isset($qtys[$orderItem->getParentItemId()])) { $productOptions = $orderItem->getProductOptions(); if (isset($productOptions['bundle_selection_attributes'])) { $bundleSelectionAttributes = unserialize($productOptions['bundle_selection_attributes']); if ($bundleSelectionAttributes) { $qty = $bundleSelectionAttributes['qty'] * $qtys[$orderItem->getParentItemId()]; $qty = min($qty, $orderItem->getSimpleQtyToShip()); $item->setQty($qty); $shipment->addItem($item); continue; } else { $qty = 1; } } } else { $qty = 1; } } else { if (isset($qtys[$orderItem->getId()])) { $qty = min($qtys[$orderItem->getId()], $orderItem->getQtyToShip()); } elseif (!count($qtys)) { $qty = $orderItem->getQtyToShip(); } else { continue; } } $totalQty += $qty; $item->setQty($qty); $shipment->addItem($item); } $shipment->setTotalQty($totalQty); return $shipment; }
public function toMagento() { $path = Mage::getStoreConfig('harvey/configuration/import'); $content = file_get_contents($path); $data = explode("\n", $content); if (empty($data)) { return 'File is empty'; } $ship_details = $this->getShipDetails(); $shippedOrders = 0; foreach ($data as $v) { //$v1 = preg_split('/\s{2,}/', $v); $harvey_invoice = trim(substr($v, 0, 15)); $order_id = trim(substr($v, 15, 20)); $track_number = trim(substr($v, 35, 30)); $cust_name = trim(substr($v, 65, 35)); $addr1 = trim(substr($v, 100, 35)); $addr2 = trim(substr($v, 135, 35)); $city = trim(substr($v, 170, 30)); $state = trim(substr($v, 200, 5)); $zip = trim(substr($v, 205, 10)); $flg = trim(substr($v, 215, 1)); $box_number = trim(substr($v, 216, 4)); $weight = trim(substr($v, 220, 10)); $cost = trim(substr($v, 230, 8)); $ship_date = trim(substr($v, 238, 8)); $harvey_data = trim(substr($v, 246, 6)); $service_method = trim(substr($v, 252, 3)); $service_type = trim(substr($v, 255, 2)); $track_number = $track_number == '' ? 'Tracking Number Not Available' : $track_number; if ($service_method != '' && $service_type != '') { $ship_title = $ship_details[$service_method][$service_type]; } else { $ship_title = ""; } $carrier_code = $service_method == 'EPO' ? 'usps' : ($service_method == 'FEX' ? 'fedex' : 'custom'); /* Checking the values for testing... Mage::log($harvey_invoice."==".$order_id."==".$track_number."==".$cust_name."==" .$addr1."==".$addr2."==".$city."==".$state."==".$zip."==".$flg."==" .$box_number."==".$weight."==".$cost."==".$ship_date."==".$harvey_data ."==".$service_method."==".$service_type,"==".$ship_title."==".$carrier_code); Mage::log('======================================'); */ /** * Changing the flow of default Magento, which changes order status to 'complete' after shipment. * Below code will change the order status to 'Printed' */ $order = Mage::getModel('sales/order')->loadByIncrementId($order_id); if ($order->canShip()) { $convertOrder = new Mage_Sales_Model_Convert_Order(); $shipment = $convertOrder->toShipment($order); $items = $order->getAllItems(); $totalQty = 0; if (count($items)) { foreach ($items as $_eachItem) { $_eachShippedItem = $convertOrder->itemToShipmentItem($_eachItem); $_eachShippedItem->setQty($_eachItem->getQtyOrdered()); $shipment->addItem($_eachShippedItem); $totalQty += $_eachItem->getQtyOrdered(); } $shipment->setTotalQty($totalQty); } $arrTracking = array('carrier_code' => $carrier_code, 'title' => $ship_title, 'number' => $track_number); $track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking); $shipment->addTrack($track); try { $saveTransaction = Mage::getModel('core/resource_transaction')->addObject($shipment)->addObject($shipment->getOrder())->save(); /** //This will mark all the order items as shipped, //which will eventually change the order to Complete state. if (count($items)) { foreach ($items as $_eachItem) { $qtyOrdered = $_eachItem->getQtyOrdered(); $_eachItem->setQtyShipped($qtyOrdered); } }*/ //Updating status of order as "shipped"... $shipment->getOrder()->setIsInProcess(true); $shipment->getOrder()->setStatus($this->_status); $shipment->getOrder()->save(); return true; } catch (Exception $e) { Mage::log($e->__toString()); return $e->__toString(); } $shippedOrders += 1; } } if ($shippedOrders == 0) { return 'No orders found which can be shipped'; } }
public function convertInvoiceToShipment(Mage_Sales_Model_Order_Invoice $invoice) { $convertOrder = new Mage_Sales_Model_Convert_Order(); $order = $invoice->getOrder(); $shipment = $convertOrder->toShipment($order); $items = $invoice->getAllItems(); $totalQty = 0; if (count($items)) { foreach ($items as $eachItem) { if ($eachItem->getRowTotal() > 0) { $_eachShippedItem = $convertOrder->itemToShipmentItem($eachItem->getOrderItem()); $_eachShippedItem->setQty($eachItem->getQty()); $shipment->addItem($_eachShippedItem); $totalQty += $eachItem->getQty(); unset($_eachShippedItem); } } $shipment->setTotalQty($totalQty); } Mage::getModel('core/resource_transaction')->addObject($shipment)->addObject($shipment->getOrder())->save(); $order->save(); }
public function createMagentoShipment($orderId = null, $trackingNo = null) { if (is_null($orderId) || is_null($trackingNo)) { return false; } $order = Mage::getModel('sales/order')->loadByIncrementId($orderId); $convertOrder = new Mage_Sales_Model_Convert_Order(); $shipment = $convertOrder->toShipment($order); /** * This section adds the Ordered Items to the Shipment for Delivery. * * You can also make Partial Delivery in this section only, * by passing specific Quantity for the corresponding Ordered Item(s). */ $items = $order->getAllItems(); $totalQty = 0; if (count($items)) { foreach ($items as $_eachItem) { $_eachShippedItem = $convertOrder->itemToShipmentItem($_eachItem); $_eachShippedItem->setQty($_eachItem->getQtyOrdered()); $shipment->addItem($_eachShippedItem); $totalQty += $_eachItem->getQtyOrdered(); } $shipment->setTotalQty($totalQty); } $arrTracking = array('carrier_code' => 'sheepla', 'title' => 'Sheepla', 'number' => $trackingNo); $track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking); $shipment->addTrack($track); $saveTransaction = Mage::getModel('core/resource_transaction')->addObject($shipment)->addObject($shipment->getOrder())->save(); /** * We need to update the status of the Order, * so that it now shows as "Shipped" */ $orderShippedStatusCode = 'processing'; $order->addStatusToHistory($orderShippedStatusCode, $orderShippedStatusCode, true); $order->save(); }