/** * 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; }