public function onSalesModelOrderCreditmemoRefund($observer)
 {
     if ($observer->getCreditmemo()->getOrder()->getInstallmentTypeId()) {
         if (NGC_Installment_Model_Master::refundInstallmentPayment($observer)) {
             return mage::throwException('Unable to refund installment payments');
         }
     }
 }
 protected static function _calculateNextInstallmentDueDate()
 {
     $daysToPayment = Mage::getStoreConfig('installment/grace_period_options/grace_period_days');
     return NGC_Installment_Model_Master::calculateAmountDueDate($daysToPayment);
 }
 protected static function _refundPartialOrder($observer)
 {
     $creditMemo = $observer->getCreditmemo();
     $order = $creditMemo->getOrder();
     $payment = $order->getPayment();
     $orderId = $order->getRealOrderId();
     $newGrandTotal = $order->getGrandTotal();
     $refunded = 0;
     $totalRefund = $creditMemo->getGrandTotal();
     $totalPaid = self::getTotalPaid($orderId);
     $master = Mage::getModel('installment/master')->getCollection();
     $master->addFieldToFilter('order_id', $orderId);
     if (self::isPlanComplete($orderId)) {
         $payment->getMethodInstance()->setStore($order->getStoreId())->refund($order->getPayment(), $totalRefund);
         $payment->addTransaction(Mage_Sales_Model_Order_Payment_Transaction::TYPE_REFUND, $payment, false, sprintf('Refunded amount of %s', $totalRefund))->save();
     } else {
         //  Customer has paid equal to the new grand total. Suspend future installment payments and close invoice
         if ($totalPaid == $newGrandTotal) {
             //  Suspend all future installments
             if (!NGC_Installment_Model_Master::suspendInstallmentPayment($orderId, 1, 'Order was refunded')) {
                 return Mage::throwException('Unable to suspend future installment payments');
             }
             //  Close invoice
             foreach ($order->getInvoiceCollection() as $invoice) {
                 $invoice->setState(Mage_Sales_Model_Order_Invoice::STATE_PAID)->save();
             }
             //  Customer has paid less than the new grand total. Update future installments to take into
             //  account the new order grand total
         } elseif ($totalPaid <= $newGrandTotal) {
             //  Iterate installment payments and calculate the remaining amount due.
             $planTotal = $totalPaid;
             foreach ($master as $installment) {
                 if (!$installment->getInstallmentMasterInstallmentPaid()) {
                     $newTotal = $planTotal + $installment->getInstallmentMasterAmountDue();
                     //  If the $newTotal plan total is less than the new grand total refund continue to next transaction
                     if ($newTotal < $newGrandTotal) {
                         $planTotal = $newTotal;
                         continue;
                         //  If the $newTotal plan total is equal to the new grand total then suspend all future installments
                     } elseif ($newTotal == $newGrandTotal) {
                         $nextSeqNum = $installment->getInstallmentMasterSequenceNumber() + 1;
                         if (!NGC_Installment_Model_Master::suspendInstallmentPayment($orderId, $nextSeqNum, 'Order was refunded')) {
                             return Mage::throwException('Unable to suspend future installment payments');
                         }
                         //  The $newTotal plan total is greater than the new grand total. Calculate the difference,
                         //  decrease the installment payment by the difference, and suspend future payments
                     } else {
                         $totalDiff = $newTotal - $newGrandTotal;
                         $newInstallmentTotal = $installment->getInstallmentMasterAmountDue() - $totalDiff;
                         $installment->setInstallmentMasterAmountDue($newInstallmentTotal)->save();
                         $nextSeqNum = $installment->getInstallmentMasterSequenceNumber() + 1;
                         if (!NGC_Installment_Model_Master::suspendInstallmentPayment($orderId, $nextSeqNum, 'Order was refunded')) {
                             return Mage::throwException('Unable to suspend future installment payments');
                         }
                     }
                 }
             }
             //  Customer has paid more than the new order grand total. Refund the difference and suspend any future
             //  installment payments, close invoice, and mark order as complete
         } elseif ($totalPaid > $newGrandTotal) {
             $totalDiff = $totalPaid - $newGrandTotal;
             $payment->getMethodInstance()->setStore($order->getStoreId())->refund($order->getPayment(), $totalDiff);
             $payment->addTransaction(Mage_Sales_Model_Order_Payment_Transaction::TYPE_REFUND, $payment, false, sprintf('Refunded amount of %s', $totalRefund))->save();
             $master = Mage::getModel('installment/master')->getCollection();
             $master->addFieldToFilter('installment_master_installment_paided', 0)->addFieldToFilter('installment_master_installment_authorized', 0);
             foreach ($master as $installment) {
                 $nextSeqNum = $installment->getInstallmentMasterSequenceNumber();
                 break;
             }
             //  Suspend all future installments
             if (!NGC_Installment_Model_Master::suspendInstallmentPayment($orderId, $nextSeqNum, 'Order was refunded')) {
                 return Mage::throwException('Unable to suspend future installment payments');
             }
             //  Close invoice
             foreach ($order->getInvoiceCollection() as $invoice) {
                 $invoice->setState(Mage_Sales_Model_Order_Invoice::STATE_PAID)->save();
             }
         }
     }
 }