public function cancelaPedido(Mage_Sales_Model_Order $order) { $paymentMethod = $order->getPayment()->getMethodInstance()->getCode(); $listPaymentMethodsIdeasa = Mage::helper('base')->listPaymentMethods(); $this->logger->info('Processando cancelamento do pedido ' . $order->getRealOrderId() . ', do modulo: ' . $paymentMethod); try { if ($order->getState() != Mage_Sales_Model_Order::STATE_CANCELED) { $order->cancel(); //força o cancelamento if ($order->getStatus() != Mage_Sales_Model_Order::STATE_CANCELED) { $order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, Mage::helper('base')->__('Pedido cancelado'), $notified = false); } else { $order->addStatusToHistory($order->getStatus(), Mage::helper('base')->__('Pedido cancelado.')); } if ($order->hasInvoices() != '') { $order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, Mage::helper('base')->__('O pagamento e o pedido foram cancelados, mas não foi possível retornar os produtos ao estoque pois já havia uma fatura gerada para este pedido.'), $notified = false); } $order->save(); } if (Mage::helper('base')->isIdeasaPaymentMethod($paymentMethod) && Mage::helper('base/module')->isPagSeguroDiretoExists() && $paymentMethod == $listPaymentMethodsIdeasa[0]) { Mage::getModel('pagsegurodireto/notification')->sendEmail($order); } } catch (Exception $e) { $this->logger->error("Erro ao cancelar pedido {$orderId} \n {$e->__toString}()"); } $this->logger->info('Cancelamento do pedido foi concluido ' . $order->getRealOrderId() . ', do modulo: ' . $paymentMethod); return; }
/** * Saves an invoice and sets total-paid for the order * * @return bool */ protected function _saveInvoice() { if ($this->_order->canInvoice() && !$this->_order->hasInvoices()) { $payment = $this->_order->getPayment(); $payment->registerCaptureNotification($this->_order->getBaseGrandTotal()); $this->_order->save(); $this->_debugEmail .= 'Invoice created and saved. \\n'; //sets the invoice's transaction ID as the Buckaroo TRX. This is to allow the order to be refunded using Buckaroo later on. foreach ($this->_order->getInvoiceCollection() as $invoice) { if (!isset($this->_postArray['brq_transactions'])) { continue; } $invoice->setTransactionId($this->_postArray['brq_transactions'])->save(); } return true; } return false; }
public function canCancel(Mage_Sales_Model_Order $order) { if ($order->getCustomerId() != Mage::getSingleton('customer/session')->getCustomerId()) { return false; } if (!in_array($order->getState(), Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates(), $strict = true)) { return false; } if (!$order->canCancel() || $order->hasInvoices() || $order->hasShipments()) { return false; } if ($order->getState() == Mage_Sales_Model_Order::STATE_NEW && $this->canCancelNew($order->getStore())) { return true; } if ($order->getState() == Mage_Sales_Model_Order::STATE_PENDING_PAYMENT && $this->canCancelPending($order->getStore())) { return true; } return false; }
/** * @return self */ public function process() { $orderId = $this->_payload->getCustomerOrderId(); $this->_order = $this->_getOrder($orderId); $logData = ['order_id' => $orderId]; if (!$this->_order) { $logMessage = 'Order ({order_id}) not found in Magento. Could not process that order.'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__, $logData)); return $this; } if (!$this->_order->canCreditMemo()) { $logMessage = 'Credit memo cannot be created for order {order_id}'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__, $logData)); return $this; } $this->_order->getAllVisibleItems(); if ($this->_order->hasInvoices()) { $this->_processInvoicedCreditMemos(); } else { $this->_processCreditMemo(); } return $this; }
/** * edit order if something changed * * @param SofortLib_TransactionData $transData * @param Mage_Sales_Model_Order $order * @return boolean */ public function updateOrderFromTransactionData($transData, $order) { // total amount without refunded $amount = number_format($order->getGrandTotal() - $order->getBaseTotalRefunded(), 2, '.', ''); // if amount still the same, there was nothing edit if ($amount == $transData->getAmount()) { return false; } // transaction was cancel, nothing change, order will cancel full if ($transData->isLoss()) { return false; } // store items get from remote $checkItems = array(); foreach ($transData->getItems() as $item) { $checkItems[$item['item_id']] = $item; } // order already invoice => create creditmemo if ($order->hasInvoices()) { return $this->_createCreditMemo($transData, $order, $checkItems); } // update total $order->setGrandTotal($transData->getAmount()); $order->setBaseGrandTotal($transData->getAmount()); $subTotal = 0; $taxAmount = array(); // if discount value change the discount store on each row is broken // so we just remove it $removeDiscount = false; // edit discount amount if (empty($checkItems[2])) { $order->setDiscountAmount(0); $removeDiscount = true; } else { $order->setDiscountAmount($checkItems[2]['quantity'] * $checkItems[2]['unit_price']); $removeDiscount = true; } // check all items in the current order foreach ($order->getAllVisibleItems() as $item) { $uid = md5($item->getSku() . "-" . $item->getItemId()); // if not exist it should removed if (empty($checkItems[$uid])) { // item was cancel $this->_cancelItem($item); unset($checkItems[$uid]); continue; } // quantity or price change, new row values will be calculated if ($checkItems[$uid]['quantity'] != $item->getQtyOrdered() || $item->getPrice() != $checkItems[$uid]['unit_price']) { $item->setQtyCanceled($item->getQtyOrdered() - $checkItems[$uid]['quantity']); $singleTax = $checkItems[$uid]['unit_price'] - $checkItems[$uid]['unit_price'] * (100 / ($checkItems[$uid]['tax'] + 100)); $item->setPrice($checkItems[$uid]['unit_price'] - $singleTax); $item->setBasePrice($checkItems[$uid]['unit_price'] - $singleTax); $item->setPriceInclTax($checkItems[$uid]['unit_price']); $item->setBasePriceInclTax($checkItems[$uid]['unit_price']); $rowTotalInclTag = $checkItems[$uid]['quantity'] * $checkItems[$uid]['unit_price']; $rowTax = $rowTotalInclTag - $rowTotalInclTag * (100 / ($checkItems[$uid]['tax'] + 100)); $rowTotal = $rowTotalInclTag - $rowTax; $item->setRowTotalInclTax($rowTotalInclTag); $item->setBaseRowTotalInclTax($rowTotalInclTag); $item->setRowTotal($rowTotal); $item->setBaseRowTotal($rowTotal); $item->setTaxAmount($rowTax); $item->setBaseTaxAmount($rowTax); } // add to subtotal $subTotal += $checkItems[$uid]['quantity'] * $checkItems[$uid]['unit_price']; // appent to tax group if (empty($taxAmount[$checkItems[$uid]['tax']])) { $taxAmount[$checkItems[$uid]['tax']] = 0; } $taxAmount[$checkItems[$uid]['tax']] += $item->getRowTotalInclTax(); // remove discount from order row if ($removeDiscount) { $item->setDiscountPercent(0); $item->setDiscountAmount(0); $item->setBaseDiscountAmount(0); } unset($checkItems[$uid]); } // edit shipment amount if it was removed if (empty($checkItems[1]) && $order->getShippingAmount()) { $order->setShippingAmount(0); $order->setBaseShippingAmount(0); $order->setShippingTaxAmount(0); $order->setBaseShippingTaxAmount(0); $order->setShippingInclTax(0); $order->setBaseShippingInclTax(0); } else { $shippingWithTax = $checkItems[1]['quantity'] * $checkItems[1]['unit_price']; $shippingTax = $shippingWithTax - $shippingWithTax * (100 / ($checkItems[1]['tax'] + 100)); $shippingAmount = $shippingWithTax - $shippingTax; $order->setShippingAmount($shippingAmount); $order->setBaseShippingAmount($shippingAmount); $order->setShippingTaxAmount($shippingTax); $order->setBaseShippingTaxAmount($shippingTax); $order->setShippingInclTax($shippingWithTax); $order->setBaseShippingInclTax($shippingWithTax); } // fix tax from discount and shipping foreach ($checkItems as $item) { if (empty($taxAmount[$item['tax']])) { $taxAmount[$item['tax']] = 0; } $taxAmount[$item['tax']] += $item['unit_price'] * $item['quantity']; } // update subtotal $order->setBaseSubtotalInclTax($subTotal); $order->setSubtotalInclTax($subTotal); // sum for all tax amount $totalTaxAmount = 0; // update all tax rate items $rates = Mage::getModel('tax/sales_order_tax')->getCollection()->loadByOrder($order); foreach ($rates as $rate) { // format rate $tRate = sprintf("%01.2f", $rate->getPercent()); if (!empty($taxAmount[$tRate])) { // calc new tax value $tAmount = $taxAmount[$tRate] - $taxAmount[$tRate] * (100 / ($tRate + 100)); $totalTaxAmount += $tAmount; $rate->setAmount($tAmount); $rate->setBaseAmount($tAmount); $rate->setBaseRealAmount($tAmount); $rate->save(); } } // update total tax amount $order->setTaxAmount($totalTaxAmount); $order->setBaseTaxAmount($totalTaxAmount); // update subtotal without tax $order->setBaseSubtotal($subTotal - $totalTaxAmount + $order->getShippingTaxAmount()); $order->setSubtotal($subTotal - $totalTaxAmount + $order->getShippingTaxAmount()); $order->save(); return true; }
/** * Depending on the settings we create an invoice and capture it. * * @param Mage_Sales_Model_Order $order * @param boolean Returns true if an invoice has been created. */ protected function createInvoice(Mage_Sales_Model_Order $order, Customweb_SaferpayCw_Model_Transaction $transaction) { try { $isSettlementAfterOrder = $this->getPaymentMethodConfigurationValue('settlement'); $isCaptureAfterOrder = $transaction->getTransactionObject()->isCaptured(); $this->getHelper()->log("Payment config: settlement='" . $isSettlementAfterOrder . "' capturing='" . $isCaptureAfterOrder . "'"); if ($isSettlementAfterOrder != 'settlement_direct') { return false; } // Do not create multiple invoices if ($order->hasInvoices()) { return false; } $invoice = $order->prepareInvoice(); if ($isCaptureAfterOrder) { $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE); } $invoice->register(); $invoice->setTransactionId($transaction->getTransactionObject()->getPaymentId()); $transactionSave = Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder()); $transactionSave->save(); if ($this->getConfigData('invoice_send_email')) { $invoice->sendEmail(); } return true; } catch (Exception $e) { $this->getHelper()->log("Create Invoice: " . $e->getMessage()); return false; } }