/** * Changed By Adam to solve the problem of partial refund 26/08/2014 * @param type $creditmemo * @return \Magestore_Affiliateplus_Model_Transaction */ public function reduce($creditmemo) { if ($this->getStatus() == 1) { if ($this->canRestore()) { return $this; } // edit by viet if (!$this->getId() || !$creditmemo->getId()) { return $this; } $reducedIds = explode(',', $this->getCreditmemoIds()); if (is_array($reducedIds) && in_array($creditmemo->getId(), $reducedIds)) { return $this; } $reducedIds[] = $creditmemo->getId(); // calculate reduced commission $reduceCommission = 0; // Reduce commission for affiliate level 0 $reduceTransactionCommission = 0; // Reduce commission for transaction (all affiliate + tier affiliate) $reduceTransactionDiscount = 0; foreach ($creditmemo->getAllItems() as $item) { if ($item->getOrderItem()->isDummy()) { continue; } // Calculate the reduce commission for affiliate if (!$item->getAffiliateplusCommissionFlag()) { $orderItem = $item->getOrderItem(); if ($orderItem->getAffiliateplusCommission()) { // Calculate the reduce commission for affiliate $affiliateplusCommissionItem = explode(",", $orderItem->getAffiliateplusCommissionItem()); $firstComs = $affiliateplusCommissionItem[0]; $reduceCommission += $firstComs * $item->getQty() / $orderItem->getQtyOrdered(); // Calculate the reduce commission for transaction $orderItemQty = $orderItem->getQtyOrdered(); $orderItemCommission = (double) $orderItem->getAffiliateplusCommission(); if ($orderItemCommission && $orderItemQty) { $reduceTransactionCommission += $orderItemCommission * $item->getQty() / $orderItemQty; } $reduceTransactionDiscount += $orderItem->getBaseAffiliateplusAmount() * $item->getQty() / $orderItemQty; $item->setAffiliateplusCommissionFlag(1)->save(); Mage::dispatchEvent('update_tiercommission_to_tieraffiliate_partial_refund', array('transaction' => $this, 'creditmemo_item' => $item)); } } } if ($reduceCommission <= 0) { return $this; } // check reduced commission is over than total commission if ($reduceTransactionCommission > $this->getCommission()) { $reduceTransactionCommission = $this->getCommission(); } $account = Mage::getModel('affiliateplus/account')->setStoreId($this->getStoreId())->load($this->getAccountId()); try { // $commission = $reduceCommission + $this->getCommissionPlus() * $reduceCommission / $this->getCommission() + $reduceCommission * $this->getPercentPlus() / 100; // $commission = $reduceCommission + $this->getCommissionPlus() * $reduceTransactionCommission / $this->getCommission() + $reduceTransactionCommission * $this->getPercentPlus() / 100; if ($commission) { $account->setBalance($account->getData('balance') - $commission)->save(); // $account->setBalance($account->getBalance() - $commission) // ->save(); } $creditMemoIds = implode(',', array_filter($reducedIds)); $this->setCreditmemoIds($creditMemoIds); $this->setCommissionPlus($this->getCommissionPlus() - $this->getCommissionPlus() * $reduceTransactionCommission / $this->getCommission()); $order = $creditmemo->getOrder(); if ($reduceTransactionCommission) { if ($this->getCommission() <= $reduceTransactionCommission && $order->getBaseSubtotal() == $order->getBaseSubtotalRefunded()) { $this->setCommission(0)->setStatus(3); } else { $this->setCommission($this->getCommission() - $reduceTransactionCommission); } } if ($reduceTransactionDiscount) { if ($this->getDiscount() > $reduceTransactionDiscount) { $this->setDiscount(0); } else { $this->setDiscount($this->getDiscount() + $reduceTransactionDiscount); } } $this->save(); if ($reduceTransactionCommission) { // Update affiliateplusprogram transaction Mage::dispatchEvent('update_affiliateplusprogram_transaction_partial_refund', array('transaction' => $this, 'creditmemo' => $creditmemo)); // update balance for tier transaction $commissionObj = new Varien_Object(array('base_reduce' => $reduceTransactionCommission, 'total_reduce' => $commission)); Mage::dispatchEvent('affiliateplus_reduce_transaction', array('transaction' => $this, 'creditmemo' => $creditmemo, 'commission_obj' => $commissionObj)); $reduceCommission = $commissionObj->getBaseReduce(); // Tong commission se bi tru di o transaction $commission = $commissionObj->getTotalReduce(); // Tong commission se bi tru di khoi tai khoan customer // Send email for affiliate account $this->sendMailReduceCommissionToAccount($reduceCommission, $commission); } } catch (Exception $e) { print_r($e->getMessage()); die('zzz'); } } return $this; }
public function reduce($creditmemo) { if ($this->canRestore()) { return $this; } // edit by viet if (!$this->getId() || !$creditmemo->getId()) { return $this; } $reducedIds = explode(',', $this->getCreditmemoIds()); if (is_array($reducedIds) && in_array($creditmemo->getId(), $reducedIds)) { return $this; } $reducedIds[] = $creditmemo->getId(); // calculate reduced commission $reduceCommission = 0; foreach ($creditmemo->getAllItems() as $item) { if ($item->getOrderItem()->isDummy()) { continue; } $orderItem = $item->getOrderItem(); $orderItemCommission = (double) $orderItem->getAffiliateplusCommission(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemCommission && $orderItemQty) { $reduceCommission += $orderItemCommission * $item->getQty() / $orderItemQty; } } if ($reduceCommission <= 0) { return $this; } // check reduced commission is over than total commission if ($reduceCommission > $this->getCommission()) { $reduceCommission = $this->getCommission(); } $account = Mage::getModel('affiliateplus/account')->setStoreId($this->getStoreId())->load($this->getAccountId()); try { $commission = $reduceCommission + $this->getCommissionPlus() * $reduceCommission / $this->getCommission() + $reduceCommission * $this->getPercentPlus() / 100; //edit by viet if ($this->getStatus() == '1') { $account->setBalance($account->getBalance() - $commission)->save(); } //end by viet $this->setCreditmemoIds(implode(',', array_filter($reducedIds)))->setCommissionPlus($this->getCommissionPlus() - $this->getCommissionPlus() * $reduceCommission / $this->getCommission())->setCommission($this->getCommission() - $reduceCommission)->save(); // update balance for tier affiliate $commissionObj = new Varien_Object(array('base_reduce' => $reduceCommission, 'total_reduce' => $commission)); Mage::dispatchEvent('affiliateplus_reduce_transaction', array('transaction' => $this, 'commission_obj' => $commissionObj)); $reduceCommission = $commissionObj->getBaseReduce(); $commission = $commissionObj->getTotalReduce(); // Send email for affiliate account $this->sendMailReduceCommissionToAccount($reduceCommission, $commission); } catch (Exception $e) { } return $this; }