loadByIncrementId( $orderId ); if ( !$order->getId() ) { $order = Mage::getModel('sales/order')->load ( $orderId ); } if ( !$order->getId() ) throw new Exception("Cannot find order " . $orderId); if ( !$this->_exportedOrderStatus ) $status = $order->getStatus(); else $status = $this->_exportedOrderStatus; $order->addStatusToHistory($status, $this->_exportedOrderComment, false); $order->save(); } public function confirmMemo ( $memoId ) { //$memo = Mage::getModel('sales/order')->loadByIncrementId( $memoId ); $memos = Mage::getModel('sales/order_creditmemo')->getCollection() ->addAttributeToFilter('increment_id', $memoId) ->getAllIds(); if ( count($memos) ) $memoId = $memos[0]; if ( $memoId ) { $memo = Mage::getModel('sales/order_creditmemo')->load ( $memoId ); } if ( !$memo->getId() ) throw new Exception("Cannot find creditmemo " . $memoId); $memo->addComment($this->_exportedCreditmemoComment); $memo->save(); } public function getCreditmemos() { $params = $this->_adapterParams; if ( $params['creditmemo_ids'] ) { $memos = array(); foreach ( $params['creditmemo_ids'] as $memo_id ) { array_push ( $memos, Mage::getModel('sales/order_creditmemo')->load ( $memo_id ) ); } } else { $memos = Mage::getModel('sales/order_creditmemo') ->getCollection() ->addAttributeToSelect("*"); if ( isset ( $this->_startFromMemoId ) ) { $memos->addAttributeToFilter ( 'entity_id', array ('gt' => $this->_startFromMemoId ) ); } } if ( $params['ignore_exported_check'] ) return $memos; $returnMemos = array(); foreach ( $memos as $memo ) { if ( !$this->isCreditmemoExported ( $memo ) ) $returnMemos []= $memo; } return $returnMemos; } public function handleGls ( $order ) { $gls_info = Mage::getModel('gls/orderInfo')->load($order->getIncrementId(), 'order_id'); if( $gls_info->getData('order_id') ) { $replaceFields = array ( 'company_name' => 'company', 'street_name' => 'street', 'street_name_2' => 'street', 'zip_code' => 'postcode', 'city_name' => 'city', 'pickup_name' => 'firstname' ); $shippingAddress = $order->getShippingAddress(); $street = array(); foreach ( $replaceFields as $map => $code ) { if ( !$code ) $code = $map; if ( $code == 'street' ) { $street []= $gls_info->getData ( $map ); $shippingAddress->setStreet ( $street ); } else { if ( $code == 'pickup_name' ) { $shippingAddress->setMiddlename(''); $shippingAddress->setLastname(''); } $shippingAddress->setData ( $code, $gls_info->getData ( $map ) ); } } } } protected $_dibsInfoMap = array ( 'transaction' => 'transaction_id', 'amount' => 'amount' ); public function handlePayment($order) { $payment = $order->getPayment(); $paymentInfo = new Varien_Object(); switch ( $payment->getMethod() ) { case 'Dibspw': $sql = "SELECT * FROM `" .Mage::getConfig()->getTablePrefix() . "dibs_pw_results" . "` WHERE `orderid` = '" .$order->getIncrementId(). "' LIMIT 1;"; $connection = Mage::getSingleton('core/resource')->getConnection('core_read'); $rows = $connection->fetchAll($sql); foreach ( $rows[0] as $column => $value ) { if ( $this->_dibsInfoMap[$column] ) { $paymentInfo->setData ( $this->_dibsInfoMap[$column], $value ); } } $order->setPaymentInfo ( $paymentInfo ); break; } } public function getOrders() { $params = $this->_adapterParams; if ( $params['order_ids'] ) { $orders = array(); foreach ( $params['order_ids'] as $order_id ) { array_push ( $orders, Mage::getModel('sales/order')->load ( $order_id ) ); } } else { $orders = Mage::getModel('sales/order') ->getCollection() ->addAttributeToSelect("*") ->addAttributeToFilter('status', $this->_getOrderStatus); if ( isset ( $this->_startFromOrderId ) ) { $orders->addAttributeToFilter ( 'entity_id', array ('gt' => $this->_startFromOrderId ) ); } } if ( $params['ignore_exported_check'] ) return $orders; $returnOrders = array(); foreach ( $orders as $order ) { if ( !$this->isOrderExported ( $order ) ) { if ( $this->_replaceShippingInfo && Mage::getModel('gls/orderInfo') ) { $this->handleGls ( $order ); } if ( $this->_addPaymentInfo && $order->getPayment() ) { $this->handlePayment($order); } $returnOrders []= $order; } } return $returnOrders; } public function isCreditmemoExported ( $memo ) { foreach ( $memo->getCommentsCollection() as $comment ) { if ( $comment->getComment() == $this->_exportedCreditmemoComment ) return true; } return false; } public function isOrderExported ( $order ) { foreach ($order->getAllStatusHistory() as $orderComment){ if ( $orderComment->getComment() == $this->_exportedOrderComment ) return true; } return false; } public function putOrdersStatus ( $orders ) { foreach ( $orders as $order ) { $orderId = $order->getIncrementId(); $status = $order->getStatus(); $capturedAmount = null; if ( $order->getCapturedAmount() !== null) { $capturedAmount = $order->getCapturedAmount(); } $order = Mage::getModel('sales/order')->loadByIncrementId( $orderId ); if ( $capturedAmount !== null && $order->getId() ) { if ( $order->getGrandTotal() != $capturedAmount ) { throw new Exception( "Fatal error when completing the order. Grand total does not match amount to capture. Increment ID: " . $orderId . ", grand total: " . $order->getGrandTotal() . ", " . "amount to capture: " . $capturedAmount ); } } if ( $order->getId() && $status == "completed" && $order->getState() != "complete" && $order->getState() != "cancelled" && $order->getState() != "closed" ) { if(!$order->canInvoice()) { $order->setData('state', "complete"); $order->setStatus("complete"); $history = $order->addStatusHistoryComment('Order marked as complete automatically, but could not invoice it.', false); $history->setIsCustomerNotified(true); $order->save(); //Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.')); } else { $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice(); if (!$invoice->getTotalQty()) { Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.')); } $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE); $invoice->register(); $transactionSave = Mage::getModel('core/resource_transaction') ->addObject($invoice) ->addObject($invoice->getOrder()); $transactionSave->save(); $order->setData('state', "complete"); $order->setStatus("complete"); $history = $order->addStatusHistoryComment('Order marked as complete automatically.', false); $history->setIsCustomerNotified(true); $order->save(); $invoice->sendEmail(true); } } } } }