/** * 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; }
/** * Construct the SofortLib_TransactionData object * Collect every order's item and set it accordingly * TransactionData is used encapsulated in this class to retrieve information about the order's details * @return object SofortLib_TransactionData * @private */ private function _setupTransactionData() { $SofortLib_TransactionData = new SofortLib_TransactionData($this->_configKey, $this->_apiUrl); $SofortLib_TransactionData->setTransaction($this->_transactionId); $SofortLib_TransactionData->sendRequest(); if (!$SofortLib_TransactionData->getCount()) { return false; } $this->setStatus($SofortLib_TransactionData->getStatus()); $this->setStatusReason($SofortLib_TransactionData->getStatusReason()); $this->setStatusOfInvoice($SofortLib_TransactionData->getInvoiceStatus()); $this->setInvoiceObjection($SofortLib_TransactionData->getInvoiceObjection()); $this->setLanguageCode($SofortLib_TransactionData->getLanguageCode()); $this->setTransaction($this->getTransactionId()); $this->setTime($SofortLib_TransactionData->getTime()); $this->setPaymentMethod($SofortLib_TransactionData->getPaymentMethod()); $this->setInvoiceUrl($SofortLib_TransactionData->getInvoiceUrl()); $this->setAmount($SofortLib_TransactionData->getAmount()); $this->setAmountRefunded($SofortLib_TransactionData->getAmountRefunded()); $itemArray = $SofortLib_TransactionData->getItems(); // should there be any items, fetch them accordingly $this->_items = array(); if (is_array($itemArray) && !empty($itemArray)) { foreach ($itemArray as $item) { $this->setItem($item['item_id'], $item['product_number'], $item['product_type'], $item['title'], $item['description'], $item['quantity'], $item['unit_price'], $item['tax']); $this->_amount += $item['unit_price'] * $item['quantity']; } } /* * set the state according to the state given by transaction information (status, status_reason, invoice_status) * @see $statusMask */ $this->setState($this->_calcInvoiceStatusCode()); return $SofortLib_TransactionData; }
private function _setupTransactionData() { $SofortLib_TransactionData = new SofortLib_TransactionData($this->_configKey, $this->_apiUrl); $SofortLib_TransactionData->setTransaction($this->_transactionId); $SofortLib_TransactionData->sendRequest(); if (!$SofortLib_TransactionData->getCount()) { return false; } $this->setStatus($SofortLib_TransactionData->getStatus()); $this->setStatusReason($SofortLib_TransactionData->getStatusReason()); $this->setStatusOfInvoice($SofortLib_TransactionData->getInvoiceStatus()); $this->setInvoiceObjection($SofortLib_TransactionData->getInvoiceObjection()); $this->setLanguageCode($SofortLib_TransactionData->getLanguageCode()); $this->setTransaction($this->getTransactionId()); $this->setTime($SofortLib_TransactionData->getTime()); $this->setPaymentMethod($SofortLib_TransactionData->getPaymentMethod()); $this->setInvoiceUrl($SofortLib_TransactionData->getInvoiceUrl()); $this->setAmount($SofortLib_TransactionData->getAmount()); $this->setAmountRefunded($SofortLib_TransactionData->getAmountRefunded()); $itemArray = $SofortLib_TransactionData->getItems(); $this->_items = array(); if (is_array($itemArray) && !empty($itemArray)) { foreach ($itemArray as $item) { $this->setItem($item['item_id'], $item['product_number'], $item['product_type'], $item['title'], $item['description'], $item['quantity'], $item['unit_price'], $item['tax']); $this->_amount += $item['unit_price'] * $item['quantity']; } } $this->setState($this->_calcInvoiceStatusCode()); return $SofortLib_TransactionData; }