/** * Prepare order creditmemo based on invoice items and requested requested params * * @param array $data * @return Mage_Sales_Model_Order_Creditmemo */ public function prepareInvoiceCreditmemo($invoice, $data = array()) { $totalQty = 0; $qtys = isset($data['qtys']) ? $data['qtys'] : array(); $creditmemo = $this->_convertor->toCreditmemo($this->_order); $creditmemo->setInvoice($invoice); foreach ($invoice->getAllItems() as $invoiceItem) { $orderItem = $invoiceItem->getOrderItem(); if (!$this->_canRefundItem($orderItem, $qtys)) { continue; } $item = $this->_convertor->itemToCreditmemoItem($orderItem); if ($orderItem->isDummy()) { $qty = 1; } else { if (isset($qtys[$orderItem->getId()])) { $qty = (double) $qtys[$orderItem->getId()]; } elseif (!count($qtys)) { $qty = $orderItem->getQtyToRefund(); } else { continue; } } $qty = min($qty, $invoiceItem->getQty()); $totalQty += $qty; $item->setQty($qty); $creditmemo->addItem($item); } $creditmemo->setTotalQty($totalQty); $this->_initCreditmemoData($creditmemo, $data); if (!isset($data['shipping_amount'])) { $order = $invoice->getOrder(); $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded(); $creditmemo->setBaseShippingAmount(min($baseAllowedAmount, $invoice->getBaseShippingAmount())); } $creditmemo->collectTotals(); return $creditmemo; }
/** * Prepare order creditmemo based on invoice items and requested requested params * * @param array $data * @return Mage_Sales_Model_Order_Creditmemo */ public function prepareInvoiceCreditmemo($invoice, $data = array()) { $totalQty = 0; $qtys = isset($data['qtys']) ? $data['qtys'] : array(); $creditmemo = $this->_convertor->toCreditmemo($this->_order); $creditmemo->setInvoice($invoice); $invoiceQtysRefunded = array(); foreach ($invoice->getOrder()->getCreditmemosCollection() as $createdCreditmemo) { if ($createdCreditmemo->getState() != Mage_Sales_Model_Order_Creditmemo::STATE_CANCELED && $createdCreditmemo->getInvoiceId() == $invoice->getId()) { foreach ($createdCreditmemo->getAllItems() as $createdCreditmemoItem) { $orderItemId = $createdCreditmemoItem->getOrderItem()->getId(); if (isset($invoiceQtysRefunded[$orderItemId])) { $invoiceQtysRefunded[$orderItemId] += $createdCreditmemoItem->getQty(); } else { $invoiceQtysRefunded[$orderItemId] = $createdCreditmemoItem->getQty(); } } } } $invoiceQtysRefundLimits = array(); foreach ($invoice->getAllItems() as $invoiceItem) { $invoiceQtyCanBeRefunded = $invoiceItem->getQty(); $orderItemId = $invoiceItem->getOrderItem()->getId(); if (isset($invoiceQtysRefunded[$orderItemId])) { $invoiceQtyCanBeRefunded = $invoiceQtyCanBeRefunded - $invoiceQtysRefunded[$orderItemId]; } $invoiceQtysRefundLimits[$orderItemId] = $invoiceQtyCanBeRefunded; } foreach ($invoice->getAllItems() as $invoiceItem) { $orderItem = $invoiceItem->getOrderItem(); if (!$this->_canRefundItem($orderItem, $qtys, $invoiceQtysRefundLimits)) { continue; } $item = $this->_convertor->itemToCreditmemoItem($orderItem); if ($orderItem->isDummy()) { $qty = 1; } else { if (isset($qtys[$orderItem->getId()])) { $qty = (double) $qtys[$orderItem->getId()]; } elseif (!count($qtys)) { $qty = $orderItem->getQtyToRefund(); } else { continue; } if (isset($invoiceQtysRefundLimits[$orderItem->getId()])) { $qty = min($qty, $invoiceQtysRefundLimits[$orderItem->getId()]); } } $qty = min($qty, $invoiceItem->getQty()); $totalQty += $qty; $item->setQty($qty); $creditmemo->addItem($item); } $creditmemo->setTotalQty($totalQty); $this->_initCreditmemoData($creditmemo, $data); if (!isset($data['shipping_amount'])) { $order = $invoice->getOrder(); $isShippingInclTax = Mage::getSingleton('tax/config')->displaySalesShippingInclTax($order->getStoreId()); if ($isShippingInclTax) { $baseAllowedAmount = $order->getBaseShippingInclTax() - $order->getBaseShippingRefunded() - $order->getBaseShippingTaxRefunded(); } else { $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded(); $baseAllowedAmount = min($baseAllowedAmount, $invoice->getBaseShippingAmount()); } $creditmemo->setBaseShippingAmount($baseAllowedAmount); } $creditmemo->collectTotals(); return $creditmemo; }
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(); }
/** * Convert order payment to quote payment * * @param Mage_Sales_Model_Order_Payment $payment * @return Mage_Sales_Model_Quote_Payment */ public function paymentToQuotePayment(Mage_Sales_Model_Order_Payment $payment, $quotePayment = null) { $quotePayment = parent::paymentToQuotePayment($payment, $quotePayment); $quotePayment->setSuAccountNumber($payment->getSuAccountNumber())->setSuBankCode($payment->getSuBankCode())->setSuNlBankCode($payment->getSuNlBankCode())->setSuPaycode($payment->getSuPaycode())->setSuSecurity($payment->getSuSecurity())->setSuIban($payment->getSuIban())->setSuBic($payment->getSuBic())->setSuHolder($payment->getSuHolder()); return $quotePayment; }
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(); }