/** * Refund spent points and cancel earned points * @param type $observer * @return type */ public function orderRefund($observer) { $creditmemo = $observer->getCreditmemo(); $order = $creditmemo->getOrder(); /* If guest, return */ if (!$order || !$order->getCustomerId() || !$order->getTotalQtyOrdered()) { return; } $customer = Mage::getModel('customer/customer')->load($order->getCustomerId()); if (!$customer->getId()) { return; } $data = array('memo' => $creditmemo, 'order' => $order, 'customer' => $customer); if (Mage::helper('points/config')->isCancelPoints($order->getStoreId())) { $this->_cancelRefererPoints($data); $this->_cancelEarnedPoints($data); } if (Mage::helper('points/config')->isRefundPoints($order->getStoreId())) { $this->_refundSpentPoints($data); } }
/** * Changed By Adam: Update commission for tier transaction * @param type $observer * @return type */ public function reduceTransaction($observer) { /* hainh edit 25-04-2014 */ if (!Mage::helper('affiliatepluslevel')->isPluginEnabled()) { return; } $transaction = $observer->getTransaction(); $creditmemo = $observer->getCreditmemo(); $tierTransactions = $this->_getTierTransactions($transaction); $commissionObj = $observer->getCommissionObj(); $baseReduce = $commissionObj->getBaseReduce(); $totalReduce = $commissionObj->getTotalReduce(); try { foreach ($tierTransactions as $tierTransaction) { $transactionTotal = 0; $reduceCommission = 0; foreach ($creditmemo->getAllItems() as $item) { $orderItem = $item->getOrderItem(); $affiliateplusCommissionItem = explode(",", $orderItem->getAffiliateplusCommissionItem()); $totalCommissionItemLevel = $affiliateplusCommissionItem[$tierTransaction->getLevel()]; $transactionTotal += $totalCommissionItemLevel * $item->getQty() / $orderItem->getQtyOrdered(); $reduceCommission += $totalCommissionItemLevel * $item->getQty() / $orderItem->getQtyOrdered(); } if ($tierTransaction->getCommission() <= $transactionTotal) { $tierTransaction->setCommission(0)->save(); // Adam: 22/09/2014: Miss ham save() nen bi sai ket qua khi refund } else { $tierTransaction->setCommission($tierTransaction->getCommission() - $transactionTotal)->save(); } $totalCommission = $reduceCommission + $tierTransaction->getCommissionPlus() * $item->getQty() / $orderItem->getQtyOrdered(); $commissionObj->setBaseReduce($reduceCommission); $commissionObj->setTotalReduce($totalCommission); // send email to tier if ($tierTransaction->getLevel() > 0) { $tierTransaction->sendMailReducedTransactionToAccount($transaction, $reduceCommission, $totalCommission); } } } catch (Exception $e) { print_r($e->getMessage()); die('x'); } // $ratio = $baseReduce / ($baseReduce + $transaction->getCommission()); // try { // $account = Mage::getModel('affiliateplus/account')->setStoreId($transaction->getStoreId()); // foreach ($tierTransactions as $tierTransaction) { // $account->load($tierTransaction->getTierId()); // // $balance = $account->getBalance() - ($tierTransaction->getCommission() + $tierTransaction->getCommissionPlus()) * $ratio; // // $reduceCommission = $tierTransaction->getCommission() * $ratio; // $totalCommission = ($tierTransaction->getCommission() + $tierTransaction->getCommissionPlus()) * $ratio; // if ($tierTransaction->getLevel() == 0) { // $balance += $totalReduce; // $commissionObj->setBaseReduce($reduceCommission); // $commissionObj->setTotalReduce($totalCommission); // } // // $tierTransaction->setCommission($tierTransaction->getCommission() * (1 - $ratio)) // ->setCommissionPlus($tierTransaction->getCommissionPlus() * (1 - $ratio)) // ->save(); // $account->setBalance($balance)->save(); // // // send email to tier // if ($tierTransaction->getLevel() > 0) // $tierTransaction->sendMailReducedTransactionToAccount($transaction, $reduceCommission, $totalCommission); // } // } catch (Exception $e) { // // } }
/** * Builds request for DeliverInvoice as Credit for Invoice. Calls from * Magento Refund * * @param type $payment * @param type $auth * @param type $sveaOrderId * @return type */ public function getRefundRequest($payment, $auth, $sveaOrderId) { $conf = new SveaMageConfigProvider($auth); $sveaObject = WebPay::deliverOrder($conf); $order = $payment->getOrder(); $creditMemo = $payment->getCreditmemo(); $storeId = $order->getStoreId(); $countryCode = $order->getBillingAddress()->getCountryId(); $taxCalculationModel = Mage::getSingleton('tax/calculation'); $taxConfig = Mage::getSingleton('tax/config'); $store = Mage::app()->getStore($storeId); foreach ($creditMemo->getAllItems() as $item) { $orderItem = $item->getOrderItem(); if ($orderItem->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { continue; } // Default to the item price $name = $item->getName(); $price = $orderItem->getPrice(); $priceInclTax = $orderItem->getPriceInclTax(); $taxPercent = $orderItem->getTaxPercent(); if (!(int) $taxPercent) { $taxPercent = false; } $qty = $item->getQty(); $parentItem = $orderItem->getParentItem(); if ($parentItem) { switch ($parentItem->getProductType()) { case Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE: $price = $parentItem->getPrice(); $priceInclTax = $parentItem->getPriceInclTax(); $taxPercent = $parentItem->getTaxPercent(); $qty = $parentItem->getQtyRefunded(); break; case Mage_Catalog_Model_Product_Type::TYPE_BUNDLE: $taxPercent = $priceInclTax = $price = 0; $name = '- ' . $name; break; } } if ($taxPercent === false) { // If it's a bundle item we have to calculate the tax from // the including/excluding tax values $taxPercent = round(100 * ($priceInclTax / $price - 1)); } $orderRow = WebPayItem::orderRow()->setArticleNumber($item->getSku())->setQuantity((int) $qty)->setName($name)->setUnit(Mage::helper('svea_webpay')->__('unit'))->setVatPercent((int) $taxPercent)->setAmountIncVat((double) $priceInclTax); $sveaObject->addOrderRow($orderRow); } $request = $taxCalculationModel->getRateRequest($order->getShippingAddress(), $order->getBillingAddress(), null, $store); // Shipping if ($creditMemo->getShippingAmount() > 0) { $shippingFee = WebPayItem::shippingFee()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setName($order->getShippingDescription()); // We require shipping tax to be set $shippingTaxClass = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $storeId); $rate = $taxCalculationModel->getRate($request->setProductClassId($shippingTaxClass)); $shippingFee->setVatPercent((int) $rate); if ($taxConfig->shippingPriceIncludesTax($storeId)) { $shippingFee->setAmountIncVat($creditMemo->getShippingInclTax()); } else { $shippingFee->setAmountExVat($creditMemo->getShippingAmount()); } $sveaObject->addFee($shippingFee); } // Discount $discount = abs($creditMemo->getDiscountAmount()); if ($discount > 0) { $discountRow = WebPayItem::fixedDiscount()->setAmountIncVat($discount)->setName(Mage::helper('svea_webpay')->__('discount'))->setUnit(Mage::helper('svea_webpay')->__('unit')); $sveaObject->addDiscount($discountRow); } // Gift cards if (abs($creditMemo->getGiftCardsAmount()) > 0) { $giftCardRow = WebPayItem::fixedDiscount()->setAmountIncVat(abs($creditMemo->getGiftCardsAmount()))->setUnit(Mage::helper('svea_webpay')->__('unit')); $sveaObject->addDiscount($giftCardRow); } // Invoice fee $paymentFee = $creditMemo->getSveaPaymentFeeAmount(); $paymentFeeInclTax = $creditMemo->getSveaPaymentFeeInclTax(); $refunded = $creditMemo->getOrder()->getSveaPaymentFeeRefunded(); if ($paymentFee > 0 && $refunded == 0) { $invoiceFee = WebPayItem::invoiceFee()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setName(Mage::helper('svea_webpay')->__('invoice_fee'))->setAmountExVat($paymentFee)->setAmountIncVat($paymentFeeInclTax); $sveaObject = $sveaObject->addFee($invoiceFee); $creditMemo->getOrder()->setSveaPaymentFeeRefunded($paymentFeeInclTax); } $adjustmentFee = $creditMemo->getAdjustmentPositive(); if ($adjustmentFee > 0) { $invoiceAdjustment = WebPayItem::invoiceFee()->setVatPercent(0)->setAmountIncVat($adjustmentFee); $sveaObject->addFee($invoiceAdjustment); } $response = $sveaObject->setCountryCode($countryCode)->setOrderId($sveaOrderId)->setInvoiceDistributionType(Mage::getStoreConfig("payment/svea_invoice/deliver_method")); $order->setData('svea_refund_request', $response); return $order->getData('svea_refund_request'); }
/** * partial refund: reduce commission from affiliate's balance * @param type $observer * @return Magestore_Affiliateplus_Model_Observer */ public function creditmemoSaveAfter($observer) { // Changed By Adam 28/07/2014 if (!Mage::helper('affiliateplus')->isAffiliateModuleEnabled()) { return; } $creditmemo = $observer->getCreditmemo(); if ($creditmemo->getState() != Mage_Sales_Model_Order_Creditmemo::STATE_REFUNDED) { return $this; } // Refund for Affiliate Credit $this->creditmemoRefund($creditmemo); $storeId = $creditmemo->getStoreId(); if (!$this->_getConfigHelper()->getCommissionConfig('decrease_commission_creditmemo', $storeId)) { return $this; } $order = $creditmemo->getOrder(); $cancelStatus = explode(',', $this->_getConfigHelper()->getCommissionConfig('cancel_transaction_orderstatus', $storeId)); $transaction = Mage::getModel('affiliateplus/transaction')->load($order->getIncrementId(), 'order_number'); if (in_array('closed', $cancelStatus) && !$order->canCreditmemo()) { // edit by viet $transaction->reduce($creditmemo); // end by viet return $this; } if ($transaction->getId()) { $transaction->reduce($creditmemo); } }