<?php
/*
*
* @author G J Jidni
*
*/
class Mailer_model extends CI_Model
{
    /**
     * Constructor
     *
     * @return Mailer_model
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->library('email');
    }

    /**
     * sends mail
     * @param --- $data - information to place in the mail
     * $templateName - html template to use in mail body
     * @return --- none
     * author ---- Tania Noor
     * modified by ----- Shafiul Alam
     * date --- 12/28/2007 (mm/dd/yyyy)
     */
    function sendOrderPlaceEmail ($data, $templateName)
    {
        $this->email->set_mailtype('html');
        $this->email->from($data['from_address'], $data['full_name']);
        $this->email->to($data['to_address']);
        $this->email->cc($data['cc_address']);
        $this->email->subject($data['subject']);
        $data['AffiliatePasswordEditUrl'] = $this->getAffiliatePasswordEditUrl($data['merchant_row_id'], $data['affiliate_row_id']);
        $body = $this->load->view('mailScripts/' . $templateName, $data, true);
        $this->email->message($body);
        $this->email->send();
        $this->email->clear();
    }

    function sendEmailFromDatabaseTemplate ($data, $customer_email_number)
    {
        $this->email->set_mailtype('html');
        $this->email->from($data['from_address'],$data['merchant_full_name']);
        $this->email->to($data['to_address']);
        $this->email->cc($data['cc_address']);

        $tagValue['<#CustomerName#>']                         = $data['customer_name'];
        $tagValue['<#MerchantName#>']                             = $data['merchant_full_name'];
        $tagValue['<#ProductDescription#>']                 = $data['product_description'];
        $tagValue['<#LicenseKey#>']                             = $data['licenseKey'];
        $tagValue['<#OrderId#>']                                     = $data['order_id'];
        $tagValue['<#MerchantContractProfile#>']     = $data['contact_profile_merchant']['footer_text'];
        $tagValue['<#EmailFooter#>']                             = $data['email_footer'];

        $tagValue['<#DownloadPageLink#>']                 = $data['DownloadPageLink'];
        $tagValue['<#ProductId#>']                 = $data['product_id'];

        $customerEmailInfo = $this->getCustomerEmailInfo($data['merchant_row_id'], $customer_email_number);

        $subject = html_entity_decode($customerEmailInfo->email_subject);
        $subject = strtr($subject, $tagValue);
        $this->email->subject($subject);

        $body = html_entity_decode($customerEmailInfo->email_body);
        $body = strtr($body, $tagValue);
        $this->email->message($body);

        $this->email->send();
        $this->email->clear();
    }

    function sendOrderPlaceCustomerEmail ($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($data['from_address'],$data['full_name']);
        $this->email->to($data['to_address']);

        if(isset($data['cc_address']))
        {
            $this->email->cc($data['cc_address']);
        }
        $this->email->subject($data['subject']);
        $this->email->message($data['body']);
        $this->email->send();

        $this->email->clear();
    }

    function sendOrderPlaceAffiliateEmail ($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($data['from_address'],$data['full_name']);
        $this->email->to($data['to_address']);
        $this->email->cc($data['cc_address']);
        $this->email->subject($data['subject']);
        $this->email->message($data['body']);
        $this->email->send();

        $this->email->clear();
    }

    /**
    *Get Affiliate email body
    *
    */
    function getInstractionMailBody($email_body,$data)
    {
        $body = html_entity_decode($email_body);

        $body = str_replace('<#AffiliateLink#>',$data['generated_link'],$body);
        $body = str_replace('<#AffiliateLogin#>','<a href="'.$data['aff_login_url'].'">'.$data['aff_login_url'].'</a>',$body);
        $body = str_replace('<#CompanyName#>',$data['merchant_company_name'],$body);
        $body = str_replace('<#MerchantName#>',$data['merchant_last_name'],$body);
        $body = str_replace('<#MerchantEmail#>',$data['merchant_email_address'],$body);
        $body = str_replace('<#affCompany#>',$data['affiliate_company'],$body);
        $body = str_replace('<#affName#>',$data['affiliate_name'],$body);
        $body = str_replace('<#affEmail#>',$data['affiliate_email'],$body);
        $body = str_replace('<#affID#>',$data['affiliate_row_id'],$body);
        $body = str_replace('<#affAdminEmail#>',$data['aff_admin_url'],$body);
        $body = str_replace('<#affUsername#>',$data['affiliate_email'],$body);
        $body = str_replace('<#affPassword#>',$data['affiliate_password'],$body);
        $body = str_replace('<#payOutDate#>', getCurrentDateTimeForDB(),$body);
        $body = str_replace('<#affPayOutAmount#>',$data['paidTotalcommission'],$body);
        $body = str_replace('<#salesValue#>',$_SESSION['currency_symbol'].' '.$data['sales_value'],$body);
        $body = str_replace('<#totalFirstLevelCommission#>',$_SESSION['currency_symbol'].' '.$data['total_first_level_commission'],$body);
        $body = str_replace('<#footerText#>',$data['contact_profile_merchant']['footer_text'],$body);

        return $body;
    }

    /**
     * @author Shafiul alam
     * @desc This function return billing information edit url for customer
     * @edited by Jilani Jidni
     * @version PWC1.2
     * @desc remove merchant_row_id as parameter & encrypt the customer id
     */
    function getCustomerBillingAddressEditUrl ($customer_id)
    {
        return BASEURL.'edit_billinginfo/editBillingInfo/'.md5($customer_id);
    }

    /**
     *@author Shafiul alam
     *This function return password edit url for affiliate
     */
    function getAffiliatePasswordEditUrl ($merchant_row_id,$affiliate_row_id )
    {
        return BASEURL . 'affiliatePanel/affiliate_login/changePassword/' . $merchant_row_id . '/' . md5($affiliate_row_id) . '.html';
    }

    /**
     *@author Shafiul alam
     *This function return activation url for merchant
     */
    function getMerchantActivationUrl ($merchant_row_id, $activationCode)
    {
        return BASEURL . 'signup/activateAccount/' . $merchant_row_id . '/' . $activationCode;
    }

    /**
     *@author Shafiul Alam
     *Send Activation email to the merchant
     */
    function sendActivationEmail ($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($this->config->item('emailFromAddress'));
        $this->email->to($data['email_address']);
        $this->email->subject('Account Activation');
        $pageData['last_name'] = $data['last_name'];
        $pageData['activation_url'] = $this->getMerchantActivationUrl($data['merchant_row_id'], $data['activation_code']);
        $body = $this->load->view('mailScripts/accountActivationEmail', $pageData, true);
        $this->email->message($body);
        $this->email->send();
        $this->email->clear();
    }

    /**
     *@author Shafiul Alam
     *Send Activation email to the merchant
     */
    function sendActivationsuccessfullEmail ($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($this->config->item('emailFromAddress'));
        $this->email->to($data['email_address']);
        $this->email->subject('Account Activation successfull');
        $pageData['last_name'] = $data['last_name'];
        $body = $this->load->view('mailScripts/activationSuccessfull', $pageData, true);
        $this->email->message($body);
        $this->email->send();
        $this->email->clear();
    }

    /**
     * @author Jilani Jidni
     *
     * @param Array $data
     * @param String $templateName
     */
    function sendMails($data,$templateName)
    {
        $this->email->set_mailtype('html');
        $this->email->from($data['from_address'],$data['full_name']);
        $this->email->reply_to($data['from_address']);
        $this->email->to($data['to_address']);

        if(isset($data['cc_address']))
        {
            $this->email->cc($data['cc_address']);
        }
        $this->email->subject($data['subject']);
        $body = $this->load->view('mailScripts/' . $templateName, $data, true);
        $this->email->message($body);
        $this->email->send();
        $this->email->clear();
    }

    function getCustomerEmailInfo($merchant_row_id,$customer_email_number, $isArray = false)
    {
        $this->db->select('*');
        $this->db->from('tbl_email_manager');
        $this->db->where('merchant_row_id', $merchant_row_id);
        $this->db->where('customer_email_number', $customer_email_number);
        $resultSet = $this->db->get();
        return $isArray ? $resultSet->row_array() : $resultSet->row();
    }
    /*
    function getTemplateEmailInfo($merchant_row_id,$customer_email_number)
    {
        $this->db->select('*');
        $this->db->from('tbl_email_manager');
        $this->db->where('merchant_row_id', $merchant_row_id);
        $this->db->where('customer_email_number', $customer_email_number);
        $resultSet = $this->db->get();
        return $resultSet->row();
    }
    */
    function updateCustomerEmailInfo($data)
    {
        $this->db->where ( 'merchant_row_id', $data['merchant_row_id'] );
        $this->db->where ( 'customer_email_number', $data ['customer_email_number'] );
        $this->db->update ( 'tbl_email_manager', $data );
    }

    function getParshCustomerMailBody($email_body,$data)
    {

        $data = assignVariables($data, array('customer_name',
                                            'first_name',
                                            'last_name',
                                            'buyerSurveyReport',
                                            'refund_comments',
                                            'product_description',
                                            'merchant_name',
                                            'download_page_link',
                                            'email_footer'));

        $data = assignObjects($data, array('customer_info', 'orderDetails'));
        $data['customer_info'] = assignProperties($data['customer_info'], array('first_name',
                                                                                'last_name'));
        $data['orderDetails'] = assignProperties($data['orderDetails'], array('order_id'));
        $body = html_entity_decode($email_body, ENT_QUOTES);
        $data['orderInvoice'] = $this->getOrderInvoice($data);

        $data['RefundSummary'] = '';

        if(isset($data['refundOrder']) && $data['refundOrder'])
        {
            $data['RefundSummary'] = $this->getRefundSummary($data['refundOrder']);
        }
        $data['recuringProductDetails']=$this->getRecurringProductDetails($data);
        $data['orderProductDetails']=$this->getOrderProductDetails($data);

        $data['editUrl']=$this->getEditUrl($data['customerbillingaddresseditUrl']);
        if($data['customer_info']->first_name)
        {
            $body = str_replace('<#CustomerName#>',$data['customer_info']->first_name . ' '.$data['customer_info']->last_name,$body);
        }
        if($data['customer_name'])
        {
            $body = str_replace('<#CustomerName#>',$data['customer_name'],$body);
        }
        if($data['first_name'] || $data['last_name'] )
        {
            $body = str_replace('<#CustomerName#>',$data['first_name'].' '.$data['last_name'],$body);
        }

        $body = str_replace('<#MerchantCompanyName#>',$data['merchant_info']->company_name,$body);
        $body = str_replace('<#OrderId#>',$data['orderDetails']->order_id,$body);
        $body = str_replace('<#OrderInvoice#>',$data['orderInvoice'],$body);
        $body = str_replace('<#BuyerSurveyReport#>',$data['buyerSurveyReport'],$body);
        $body = str_replace('<#RefundComments#>',$data['refund_comments'],$body);
        $body = str_replace('<#RefundComents#>',$data['refund_comments'],$body);
        $body = str_replace('<#RefundSummary#>',$data['RefundSummary'],$body);
        $body = str_replace('<#RefundSummery#>',$data['RefundSummary'],$body);
        $body = str_replace('<#ProductDescription#>',$data['product_description'],$body);
        $body = str_replace('<#MerchantName#>',$data['merchant_name'],$body);
        $body = str_replace('<#DownloadPageLink#>',$data['download_page_link'],$body);
        #----------------------------------For Recurring---------------------------------------------------
        $body = str_replace('<#RecurringCycle#>',$data['recurring_cycle'],$body);
        $body = str_replace('<#RecurringPrice#>',$data['currency_symbol'].' '.$data['recurring_price'],$body);
        $body = str_replace('<#RecurringProductDes#>',$data['productDesc'],$body);
        $body = str_replace('<#EditBillingInformationLink#>',$data['editUrl'],$body);
        $body = str_replace('<#EditBillingAddressUrl#>',$data['editUrl'],$body);

        $body = str_replace('<#RecurringProductDetails#>',$data['recuringProductDetails'],$body);
        $body = str_replace('<#OrderedProductDetails#>',$data['orderProductDetails'],$body);
        #----------------------------------End Recurring---------------------------------------------------
        $body = str_replace('<#MerchantContractProfile#>',$data['contact_profile_merchant']['footer_text'],$body);
        $body = str_replace('<#EmailFooter#>',$data['email_footer'],$body);
        $body = str_replace('<#NextSubscriptionDate#>',$data['next_subscription_date'],$body);

        return $body;
    }

    function getEditUrl($customerbillingaddresseditUrl)
    {
        return "<p><div><a href=".$customerbillingaddresseditUrl.">".$customerbillingaddresseditUrl."</a></div></p>";
    }

    function getOrderInvoice($data)
    {
       $currency_symbol = $data['currency_symbol'];
       $data = assignObjects($data, array('customer_info'));
       $data['customer_info'] = assignProperties($data['customer_info'], array('company_name',
                                                                                'first_name',
                                                                                'last_name',
                                                                                'work_phone_number',
                                                                                'home_phone_number',
                                                                                'customer_email',
                                                                                'shipping_name',
                                                                                'shipping_company_name',
                                                                                'address_line1',
                                                                                'address_line2',
                                                                                'shipping_address_line1',
                                                                                'shipping_address_line2',
                                                                                'customer_city',
                                                                                'shipping_customer_city',
                                                                                'customer_state',
                                                                                'shipping_customer_state',
                                                                                'customer_zip',
                                                                                'shipping_customer_zip',
                                                                                'country',
                                                                                'shipping_country'));
       $data = assignObjects($data, array('orderDetails'));
       $data['orderDetails'] = assignProperties($data['orderDetails'], array('order_id',
                                                                            'order_discount',
                                                                            'country_tax',
                                                                            'state_tax',
                                                                            'shpping_amount',
                                                                            'coupon_discount',
                                                                            'total_amount'));

       $returnHtml = '<p>
                        <table cellpadding="4" cellspacing="0" border="0" width="70%" align="center">
                            <tr>
                                <td colspan="2" style="font-size:14px; font-weight:bold;">' . (!empty($data['customer_info']->company_name) ? $data['customer_info']->company_name . ' ' : '') . 'Invoice<hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="2" height="10"></td>
                            </tr>
                            <tr>
                                <td width="35%">First Name:</td>
                                <td>' . $data['customer_info']->first_name . '</td>
                            </tr>
                            <tr>
                                <td>Last Name:</td>
                                <td>' . $data['customer_info']->last_name . '</td>
                            </tr>
                            <tr>
                                <td>Work Phone Number:</td>
                                <td>' . $data['customer_info']->work_phone_number . '</td>
                            </tr>
                            <tr>
                                <td>Home Phone Number:</td>
                                <td>' . $data['customer_info']->home_phone_number . '</td>
                            </tr>
                            <tr>
                                <td>Email:</td>
                                <td>' . $data['customer_info']->customer_email . '</td>
                            </tr>
                            <tr>
                                <td><b>Billing Address</b><hr class="headHR"/></td>
                                <td><b>Shipping Address</b><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td>' . $data['customer_info']->first_name . ' ' . $data['customer_info']->last_name . '</td>
                                <td>' . $data['customer_info']->shipping_name . '</td>
                            </tr>
                            <tr>
                                <td>' . $data['customer_info']->company_name . '</td>
                                <td>' . $data['customer_info']->shipping_company_name . '</td>
                            </tr>
                            <tr>
                                <td>' . $data['customer_info']->address_line1 . '<br />' . $data['customer_info']->address_line2 . '</td>
                                <td>' . $data['customer_info']->shipping_address_line1 . '<br />' . $data['customer_info']->shipping_address_line2 . '</td>
                            </tr>
                            <tr>
                                <td>' . $data['customer_info']->customer_city . '</td>
                                <td>' . $data['customer_info']->shipping_customer_city . '</td>
                            </tr>
                            <tr>
                                <td>' . $data['customer_info']->customer_state . '</td>
                                <td>' . $data['customer_info']->shipping_customer_state . '</td>
                            </tr>
                            <tr>
                                <td>' . $data['customer_info']->customer_zip . '</td>
                                <td>' . $data['customer_info']->shipping_customer_zip . '</td>
                            </tr>
                            <tr>
                                <td>' . $data['customer_info']->country . '</td>
                                <td>' . $data['customer_info']->shipping_country . '</td>
                            </tr>
                            <tr>
                                <td colspan="2">Order Details (Order Id : ' . $data['orderDetails']->order_id . ')</td>
                            </tr>
                        </table>
                        <div>&nbsp;</div>
                        <table cellpadding="4" cellspacing="0" border="0" width="70%" align="center">
                            <tr>
                                <th align="left">SKU</th>
                                <th align="left">Description</th>
                                <th align="left">Option</th>
                                <th align="left">Quantity</th>
                                <th align="left">Price</th>
                                <th align="right">Total Price</th>
                            </tr>
                            <tr><td colspan="6"><hr class="headHR"/></td></tr>';
       $bundleId = '';
       $isFirst = TRUE;

       if(isset($data['orderProductList']) && $data['orderProductList'])
       {
           foreach($data['orderProductList'] as $aOrder)
           {
               $returnHtml .= '
                            <tr>
                                <td>' . $aOrder->product_code . '&nbsp;</td>
                                <td>';

               if($aOrder->bundle_type == $this->config->item('offerTypeAdvanced'))
               {
                   if($bundleId != $aOrder->bundle_id)
                   {
                       $returnHtml .= $aOrder->offer_name;
                       $bundleId = $aOrder->bundle_id;
                       $isFirst = TRUE;
                   }
                   else
                   {
                       $returnHtml .= $aOrder->product_quantity.' X '.$aOrder->product_description;
                       $isFirst = FALSE;
                   }
               }
               else
               {
                   $returnHtml .= $aOrder->product_description;
                   $isFirst = TRUE;
               }
               $returnHtml .= ' </td>
                                <td>';

               $returnHtml .=$aOrder->product_attribute;
               if($aOrder->free_text_attribute)
                 $returnHtml .= ',' . $aOrder->free_text_attribute;
               $returnHtml .= '&nbsp;</td>
                                <td>';

               if($isFirst)
               {
                   $returnHtml .= $aOrder->product_quantity;
               }

               $returnHtml .= '&nbsp;</td>
                                <td>';

               if($isFirst)
               {
                   $returnHtml .= $currency_symbol . $aOrder->product_price;
               }

               $returnHtml .= '&nbsp;</td>
                                 <td align="right">';

               if($isFirst)
               {
                   $returnHtml .= $currency_symbol . $aOrder->product_total_price;
               }

               $returnHtml .= '&nbsp;</td>
                            </tr>';
            }
        }

        if($data['orderDetails']->order_discount > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Order Discount</td>
                                <td align="right">' . $currency_symbol . number_format($data['orderDetails']->order_discount,2) . '</td>
                            </tr>';
        }

        if($data['orderDetails']->country_tax > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Country Tax</td>
                                <td align="right">' . $currency_symbol . number_format($data['orderDetails']->country_tax,2) . '</td>
                            </tr>';
        }

        if($data['orderDetails']->state_tax > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">State Tax</td>
                                <td align="right">' . $currency_symbol . number_format($data['orderDetails']->state_tax,2) . '</td>
                            </tr>';
        }

        if($data['orderDetails']->shpping_amount > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Shipping ';
            if($data['shippingDetails']->shipping_id != $this->config->item('shipping_flat_rate_advanced') && !empty($data['shippingDetails']->method_name)){
                $returnHtml .= ' &gt;&gt; ' . $data['shippingDetails']->method_name;
            }
            if(!empty($data['shippingDetails']->shipping_service)){
                $returnHtml .= ' - ' . $data['shippingDetails']->shipping_service;
            }
            $returnHtml .= '    </td>
                                <td align="right">' . $currency_symbol . number_format($data['orderDetails']->shpping_amount,2) . '</td>
                            </tr>';
        }

        if($data['orderDetails']->coupon_discount > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="3" align="left">
                                    Applied Coupon Codes --';

            $couponsArray = array();

            foreach($data['appliedCoupons'] as $aCoupon)
            {
                $couponsArray[] = $aCoupon->coupon_code;
            }

            $couponCodes = implode(',',$couponsArray);

            $returnHtml .= $couponCodes . '
                                </td>
                                <td colspan="2" align="right">Coupon Discount</td>
                                <td align="right">' . $currency_symbol . number_format($data['orderDetails']->coupon_discount,2) . '</td>
                            </tr>';
        }

        $orderAmount = number_format($data['orderDetails']->total_amount, 2);

        if(isset($data['orderDetails']->gift_certificate_payment) && $data['orderDetails']->gift_certificate_payment > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Total</td>
                                <td align="right">' . $currency_symbol . $orderAmount . '</td>
                            </tr>
                            <tr>
                                <td colspan="6"><hr class="headHR" /></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Gift Certificate Payment</td>
                                <td align="right">' . $currency_symbol . number_format($data['orderDetails']->gift_certificate_payment,2) . '</td>
                            </tr>';

            $orderAmount -= $data['orderDetails']->gift_certificate_payment;
        }

        $returnHtml .= '    <tr>
                                <td colspan="6"><hr class="headHR" /></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Grand Total</td>
                                <td align="right">' . $currency_symbol . $orderAmount . '</td>
                            </tr>
                        </table>
                    </p>';

        if($data['orderDetails']->special_comments)
        {
            $returnHtml .= '<p>
                <table cellpadding="4" cellspacing="0" border="0" width="70%" align="center">
                    <tr>
                        <td width="175" valign="top"><b>Special Instructions/ Comments<b></td>
                        <td valign="top">' . $data['orderDetails']->special_comments . '</td>
                    </tr>
                </table>
            </p>';
        }
        return $returnHtml;
    }

    function getRefundSummary($refundOrder)
    {
        $currency_symbol=$_SESSION['currency_symbol'];

        $returnHtml ="
                        <table width=\"100%\" cellpadding=\"4\" cellspacing=\"4\" align=\"center\" border=\"0\">
                        <tr>
                            <td align=\"left\">
                                <b>Refund Summary</b>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table width=\"90%\" cellpadding=\"4\" cellspacing=\"4\" align=\"center\" border=\"0\">
                                    <tr>
                                        <th align=\"left\">Refund Number</th>
                                        <th align=\"left\">Refund Status</th>
                                        <th align=\"left\">Refund Type</th>
                                        <th align=\"left\">Refund Total</th>
                                    </tr>
                                    <tr>
                                        <td colspan=\"4\">
                                            <hr class=\"headHR\"/>
                                        </td>
                                    </tr>";
                                    if(count($refundOrder))
                                    {
                                        foreach($refundOrder as $rOrder)
                                        {

                                            $returnHtml .=" <tr>
                                                                <td align=\"left\">";
                                                            $rOrder->refund_row_id;
                                                         $returnHtml .="</td>
                                                        <td align=\"left\">";

                                                            if($rOrder->amount_refund_status==$this->config->item('refund_amount_partial'))
                                                            {
                                                              $returnHtml .=" Partial Refund";

                                                            }
                                                            else
                                                            {
                                                        $returnHtml .="
                                                            Full Refund ";

                                                            }
                                                         $returnHtml .="
                                                        </td>
                                                        <td align=\"left\">";

                                                            if($rOrder->refund_status==$this->config->item('refund_status_charge_back'))
                                                            {
                                                         $returnHtml .="
                                                            Charge Back";

                                                            }
                                                            else
                                                            {
                                                         $returnHtml .="
                                                            Refund ";

                                                            }
                                                         $returnHtml .="
                                                        </td>
                                                        <td align=\"left\">";
                                                            $currency_symbol .' '. $rOrder->refund_total;
                                                         $returnHtml .="</td>
                                                    </tr>";

                                        }
                                    }
                                $returnHtml .="
                                </table>
                            </td>
                        </tr>";
        return $returnHtml;
    }

    function getRecurringProductDetails($data)
    {
        $data = assignObjects($data, array('orderDetails'));
        $data['orderDetails'] = assignProperties($data['orderDetails'], array('order_discount',
                                                                                'country_tax',
                                                                                'state_tax',
                                                                                'shpping_amount',
                                                                                'coupon_discount',
                                                                                'total_amount'));

        $currency_symbol = isset($_SESSION['currency_symbol']) ? $_SESSION['currency_symbol'] : $data['currency_symbol'];

        $returnHtml ="<p><b>Recurring Product Description</b>
                    <table cellpadding=\"2\" cellspacing=\"1\" border=\"1\" width=\"500\">

                    <tr>
                        <td>
                            <table cellpadding=\"4\" cellspacing=\"0\" border=\"0\" width=\"100%\">
                                <tr>
                                    <th class=\"leftMenuText\">SKU</th>
                                    <th class=\"leftMenuText\">Description</th>
                                    <th class=\"leftMenuText\">Option</th>
                                    <th class=\"leftMenuText\">Quantity</th>
                                    <th class=\"leftMenuText\">Price</th>
                                    <th class=\"leftMenuText\" nowrap=\"nowrap\">Total Price</th>
                                </tr>
                                <tr><td colspan=\"6\"><hr class=\"headHR\"/></td></tr>";

                                    $bundleId = '';
                                    $isFirst = TRUE;
                                    if(isset($data['orderProductList']) && count($data['orderProductList']))
                                    {
                                        foreach($data['orderProductList'] as $aOrder)
                                        {
                                    $returnHtml .="<tr>
                                    <td align=\"left\" valign=\"top\">";
                                    $returnHtml .=$aOrder->product_code;
                                    $returnHtml .="</td>
                                    <td align=\"left\" valign=\"top\">";
                                    if($aOrder->bundle_type==$this->config->item("offerTypeAdvanced"))
                                                {
                                                    if($bundleId!=$aOrder->bundle_id){
                                                        $returnHtml .= $aOrder->offer_name;
                                                        $bundleId=$aOrder->bundle_id;
                                                        $isFirst = TRUE;
                                                    }
                                                    else{
                                                        $returnHtml .= $aOrder->product_quantity.' X '.$aOrder->product_description;
                                                        $isFirst = FALSE;
                                                    }
                                                }
                                                else
                                                {
                                                    $returnHtml .= $aOrder->product_description;
                                                    $isFirst = TRUE;
                                                }
                                    $returnHtml .="</td>
                                    <td align=\"left\" valign=\"top\">";
                                    $returnHtml .=$aOrder->product_attribute;
                                    if($aOrder->free_text_attribute)
                                      $returnHtml .= ',' . $aOrder->free_text_attribute;
                                    $returnHtml .="</td>
                                    <td align=\"left\" valign=\"top\">";
                                    if($isFirst){ $returnHtml .= $aOrder->product_quantity;}
                                    $returnHtml .="</td>
                                    <td align=\"left\" valign=\"top\">";
                                    if($isFirst){ $returnHtml .= $currency_symbol.$aOrder->recurring_price;}
                                    $returnHtml .="</td>
                                    <td  align=\"right\" valign=\"top\">";
                                    if($isFirst){ $returnHtml .= $currency_symbol.$aOrder->recurring_price;}
                                    $returnHtml .="</td>
                                </tr>";
                                        }
                                    }

                                $returnHtml .="<tr><td colspan=\"6\"><hr class=\"headHR\"/></td></tr>";

                                   /* if($data['orderDetails']->order_discount>0){
                                $returnHtml .="
                                <tr>
                                    <td colspan=\"4\"></td>
                                    <td valign=\"top\">Order Discount</td>
                                    <td align=\"left\" valign=\"top\">";
                                   $returnHtml .= $currency_symbol.' '.number_format($data['orderDetails']->order_discount,2);
                                    $returnHtml .="</td>
                                </tr>
                                <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>";
                                 }*/
                                     /* if($data['orderDetails']->country_tax>0){
                                $returnHtml .="
                                <tr>
                                    <td colspan=\"4\"></td>
                                    <td valign=\"top\">Country Tax</td>
                                    <td align=\"left\" valign=\"top\">";
                                   $returnHtml .= $currency_symbol.' '.number_format($data['orderDetails']->country_tax,2);
                                    $returnHtml .="</td>
                                </tr>
                                <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>";
                                 }*/
                                      /*if($data['orderDetails']->state_tax>0){
                                $returnHtml .="
                                <tr>
                                    <td colspan=\"4\"></td>
                                    <td valign=\"top\">State Tax</td>
                                    <td align=\"left\" >";
                                   $returnHtml .= $currency_symbol.' '.number_format($data['orderDetails']->state_tax,2);
                                    $returnHtml .="</td>
                                </tr>";
                                 }*/
                                      /*if($data['orderDetails']->shpping_amount>0){
                               $returnHtml .="
                                 <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>

                                <tr>
                                    <td colspan=\"4\" valign=\"top\"></td>
                                    <td valign=\"top\">Shipping</td>
                                    <td align=\"left\" valign=\"top\">";
                                   $returnHtml .= $currency_symbol.' '.number_format($orderDetails->shpping_amount,2);
                                    $returnHtml .="</td>
                                </tr>
                                <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>";
                                 }*/
                                    /*if($data['orderDetails']->coupon_discount>0)
                                    {
                                $returnHtml .="
                                <tr>
                                    <td colspan=\"4\" valign=\"top\"></td>
                                    <td valign=\"top\">Coupon Discount</td>
                                    <td align=\"left\" valign=\"top\">";
                                   $returnHtml .= $currency_symbol.' '.number_format($data['orderDetails']->coupon_discount,2);
                                    $returnHtml .="</td>
                                </tr>
                                <tr>
                                    <td colspan=\"6\" align=\"left\" valign=\"top\">
                                        Applied Coupon Codes --";

                                            $index = 0;
                                            $couponsArray = array();
                                            foreach($data['appliedCoupons'] as $aCoupon)
                                            {
                                                $couponsArray[$index] = $aCoupon->coupon_code;
                                                $index++;
                                            }

                                            $couponCodes = implode(',',$couponsArray);

                                        $returnHtml .= $couponCodes . "</td>
                                </tr>";
                                 }*/
                                    $returnHtml .="<tr>
                                    <td colspan=\"4\" valign=\"top\"></td>
                                    <td nowrap=\"nowrap\" align=\"right\" valign=\"top\">Total</td>
                                    <td align=\"right\" valign=\"top\">";
                                    $returnHtml .=  $currency_symbol.$aOrder->recurring_price;
                                    $returnHtml .="</td>

                                </tr>
                            </table>
                         </td>
                      </tr>
                   </table></p>";
       return $returnHtml;
    }

    function getOrderProductDetails($data)
    {
        $data = assignObjects($data, array('orderDetails'));
        $data['orderDetails'] = assignProperties($data['orderDetails'], array('order_discount',
                                                                                'country_tax',
                                                                                'state_tax',
                                                                                'shpping_amount',
                                                                                'coupon_discount',
                                                                                'total_amount'));

       $currency_symbol = isset($_SESSION['currency_symbol']) ? $_SESSION['currency_symbol'] : $data['currency_symbol'];

       $returnHtml ='<p>
                        <b>Ordered Product Description</b>
                        <table cellpadding="2" cellspacing="1" border="1" width="500">
                            <tr>
                                <td>
                                    <table cellpadding="4" cellspacing="0" border="0" width="100%">
                                        <tr>
                                            <th class="leftMenuText">SKU</th>
                                            <th class="leftMenuText">Description</th>
                                            <th class="leftMenuText">Option</th>
                                            <th class="leftMenuText">Quantity</th>
                                            <th class="leftMenuText">Price</th>
                                            <th class="leftMenuText" nowrap="nowrap">Total Price</th>
                                        </tr>
                                        <tr>
                                            <td colspan="6"><hr class="headHR"/></td>
                                        </tr>';
        $bundleId = '';
        $isFirst = TRUE;

        if(isset($data['orderProductList']) && $data['orderProductList'])
        {
            foreach($data['orderProductList'] as $aOrder)
            {
                $returnHtml .= '        <tr>
                                            <td>' . $aOrder->product_code . '&nbsp;</td>
                                            <td>';

                if($aOrder->bundle_type == $this->config->item('offerTypeAdvanced'))
                {
                    if($bundleId!=$aOrder->bundle_id)
                    {
                        $returnHtml .= $aOrder->offer_name;
                        $bundleId=$aOrder->bundle_id;
                        $isFirst = TRUE;
                    }
                    else
                    {
                        $returnHtml .= $aOrder->product_quantity.' X '.$aOrder->product_description;
                        $isFirst = FALSE;
                    }
                }
                else
                {
                    $returnHtml .= $aOrder->product_description;
                    $isFirst = TRUE;
                }
                $returnHtml .= '            </td>
                                            <td>';
                $returnHtml .=$aOrder->product_attribute;
                if($aOrder->free_text_attribute)
                  $returnHtml .= ',' . $aOrder->free_text_attribute;
                $returnHtml .= '&nbsp;</td>
                                            <td align="left">';

                if($isFirst)
                {
                    $returnHtml .= $aOrder->product_quantity;
                }

                $returnHtml .= '&nbsp;</td>
                                            <td align="left">';
                if($isFirst)
                {
                    $returnHtml .= $currency_symbol.$aOrder->product_price;
                }

                $returnHtml .= '&nbsp;</td>
                                            <td align="right">';

                if($isFirst)
                {
                    $returnHtml .= $currency_symbol.$aOrder->product_total_price;
                }

                $returnHtml .= '&nbsp;</td>
                                        </tr>';
            }
        }

        if($data['orderDetails']->order_discount > 0)
        {
            $returnHtml .= '            <tr>
                                            <td colspan="6"><hr class="headHR"/></td>
                                        </tr>
                                        <tr>
                                            <td colspan="5" align="right">Order Discount</td>
                                            <td align="right">' . $currency_symbol . number_format($data['orderDetails']->order_discount,2) . '</td>
                                        </tr>';
         }

         if($data['orderDetails']->country_tax > 0)
         {
             $returnHtml .= '           <tr>
                                            <td colspan="6"><hr class="headHR"/></td>
                                        </tr>
                                        <tr>
                                            <td colspan="5" align="right">Country Tax</td>
                                            <td align="right">' . $currency_symbol . number_format($data['orderDetails']->country_tax,2) . '</td>
                                        </tr>';
         }

         if($data['orderDetails']->state_tax > 0)
         {
             $returnHtml .= '           <tr>
                                            <td colspan="6"><hr class="headHR"/></td>
                                        </tr>
                                        <tr>
                                            <td colspan="5" align="right">State Tax</td>
                                            <td align="right">' . $currency_symbol . number_format($data['orderDetails']->state_tax,2) . '</td>
                                        </tr>';
         }

         if($data['orderDetails']->shpping_amount > 0)
         {
             $returnHtml .= '           <tr>
                                            <td colspan="6"><hr class="headHR"/></td>
                                        </tr>
                                        <tr>
                                            <td colspan="5" align="right">Shipping';

             if($data['shippingDetails']->shipping_id != $this->config->item('shipping_flat_rate_advanced') && !empty($data['shippingDetails']->method_name)){
                $returnHtml .= ' &gt;&gt; ' . $data['shippingDetails']->method_name;
             }
             if(!empty($data['shippingDetails']->shipping_service)){
                $returnHtml .= ' - ' . $data['shippingDetails']->shipping_service;
             }

             $returnHtml .= '               </td>
                                            <td align="right">' . $currency_symbol . number_format($data['orderDetails']->shpping_amount,2) . '</td>
                                        </tr>';
         }

         if($data['orderDetails']->coupon_discount > 0)
         {
             $returnHtml .= '           <tr>
                                            <td colspan="6"><hr class="headHR"/></td>
                                        </tr>
                                        <tr>
                                            <td colspan="3" align="left">
                                                Applied Coupon Codes --';
             $index = 0;
             $couponsArray = array();

             foreach($data['appliedCoupons'] as $aCoupon)
             {
                 $couponsArray[$index] = $aCoupon->coupon_code;
                 $index++;
             }

             $couponCodes = implode(',',$couponsArray);

             $returnHtml .= $couponCodes . '
                                            </td>
                                            <td colspan="2" align="right">Coupon Discount</td>
                                            <td align="right">' . $currency_symbol . number_format($data['orderDetails']->coupon_discount,2) . '</td>
                                        </tr>';
         }

         $orderAmount = number_format($data['orderDetails']->total_amount, 2);

         if(isset($data['orderDetails']->gift_certificate_payment) && $data['orderDetails']->gift_certificate_payment > 0)
         {
             $returnHtml .= '           <tr>
                                            <td colspan="6"><hr class="headHR"/></td>
                                        </tr>
                                        <tr>
                                            <td colspan="5" align="right">Total</td>
                                            <td align="right">' . $currency_symbol . $orderAmount . '</td>
                                        </tr>
                                        <tr>
                                            <td colspan="6"><hr class="headHR" /></td>
                                        </tr>
                                        <tr>
                                            <td colspan="5" align="right">Gift Certificate Payment</td>
                                            <td align="right">' . $currency_symbol . number_format($data['orderDetails']->gift_certificate_payment,2) . '</td>
                                        </tr>';
             $orderAmount -= $data['orderDetails']->gift_certificate_payment;
         }

         $returnHtml .= '               <tr>
                                            <td colspan="6"><hr class="headHR" /></td>
                                        </tr>
                                        <tr>
                                            <td colspan="5" align="right">Grand Total</td>
                                            <td align="right">' . $currency_symbol . $orderAmount . '</td>
                                        </tr>
                                    </table>
                                </p>';

         if($data['orderDetails']->special_comments)
         {
            $returnHtml .= '<p>
                <table cellpadding="4" cellspacing="0" border="0" width="70%" align="center">
                    <tr>
                        <td width="175" valign="top"><b>Special Instructions/ Comments<b></td>
                        <td valign="top">' . $data['orderDetails']->special_comments . '</td>
                    </tr>
                </table>
            </p>';
         }
         return $returnHtml;
    }

    function sendDeclineNotification($aRecurring, $merchantBasicInfo, $merchantDetails,$merchantContactFooter, $customerInfo, $attemptNo = 0)
    {
        $data['merchant_row_id']=$aRecurring->merchant_row_id;
        $data['recurring_cycle']=$aRecurring->recurring_cycle;
        $data['recurring_price']=$aRecurring->recurring_price;

        // getting merchants information
        $data['currency_symbol']= $merchantBasicInfo->currency_symbol;
        $data['merchant_info'] = $merchantDetails;
        $data['contact_profile_merchant'] = $merchantContactFooter;

        // getting customer info
        $data['customer_id'] = $aRecurring->customer_id;
        $data['first_name'] = $customerInfo->first_name;
        $data['last_name'] = $customerInfo->last_name;

        // get product information
        $CI =& get_instance();
        $CI->load->model('products/Product_model', 'Product_model', TRUE);
        $productDescription=$CI->Product_model->getProductDescriptionByProductRowId($aRecurring->product_row_id);

        $data['productDesc']=$productDescription['product_description'];

        $data['customerbillingaddresseditUrl'] = $data['editUrl'] = base_url().'edit_billinginfo/editBillingInfo/'.md5($aRecurring->customer_id);

        if($attemptNo == 0) //decline mail
        {
            $customer_email_number=1007;
        }
        else if($attemptNo == 1)
        {
            $customer_email_number=1035;
        }
        else if($attemptNo == 2)
        {
            $customer_email_number=1036;
        }
        else if($attemptNo == 3)
        {
            $customer_email_number=1037;
        }
        $customerEmailInfo = $this->getCustomerEmailInfo($aRecurring->merchant_row_id,$customer_email_number);

        $email_body = $customerEmailInfo->email_body;

        $orderPlaceEmailBody = $this->getParshCustomerMailBody($email_body,$data);

        $this->email->set_mailtype('html');
        $this->email->from($merchantBasicInfo->from_address, $merchantBasicInfo->from_name);
        $this->email->to($customerInfo->customer_email);
        $this->email->subject($customerEmailInfo->email_subject);
        $this->email->message($orderPlaceEmailBody);
        $this->email->send();
        $this->email->clear();
    }

    function getAdminCustomEmailInfo($email_code)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_email_manager');
        $this->db->where('email_code', $email_code);
        $resultSet = $this->db->get();
        return $resultSet->row_array();
    }

    function saveAdminCustomEmailInfo($data)
    {
        if($this->getAdminCustomEmailInfo($data['email_code']))
        {
            $this->db->where ( 'email_code', $data ['email_code'] );
            $this->db->update ( 'tbl_admin_email_manager', $data );
        }
        else
        {
            $this->db->insert('tbl_admin_email_manager', $data);
        }
    }

    function sendCustomizedMails($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($data['from_address'],$data['full_name']);
        $this->email->reply_to($data['from_address']);
        $this->email->to($data['to_address']);
        $this->email->cc($data['cc_address']);
        $this->email->subject($data['subject']);
        $body = $data['body'];
        $this->email->message($body);
        $this->email->send();
        $this->email->clear();
    }

    /*This function is written for customized mail*/
    function getMailBody($mailData, $mailBody)
    {
        $mailBody = html_entity_decode($mailBody, ENT_QUOTES);

        $tags = array();
        $values = array();

        $tags[] = '<#MerchantFirstName#>';
        $values[] = !empty($mailData['first_name']) ? $mailData['first_name'] : 'n/a';

        $tags[] = '<#MerchantLastName#>';
        $values[] = !empty($mailData['last_name']) ? $mailData['last_name'] : 'n/a';

        $tags[] = '<#MerchantCompanyName#>';
        $values[] = !empty($mailData['merchant_company']) ? $mailData['merchant_company'] : 'n/a';

        $projectName = $this->config->item('project_name');
        $tags[] = '<#ProjectName#>';
        $values[] = $projectName;

        $tags[] = '<#ProjectURL#>';
        $values[] = anchor(BASEURL, $projectName);

        $tags[] = '<#ProjectLoginUrl#>';
        $values[] = anchor(BASEURL, $projectName);

        $tags[] = '<#Username#>';
        $values[] = !empty($mailData['user_name']) ? $mailData['user_name'] : '';

        $tags[] = '<#AffiliatePassword#>';
        $values[] = !empty($mailData['affiliate_password']) ? $mailData['affiliate_password'] : '';
        $tags[] = '<#AffiliateEmail#>';
        $values[] = !empty($mailData['affiliate_email']) ? $mailData['affiliate_email'] : '';

        $tags[] = '<#NewPassword#>';
        $values[] = !empty($mailData['new_password']) ? $mailData['new_password'] : '';

        $tags[] = '<#Password#>';
        $values[] = !empty($mailData['user_password']) ? $mailData['user_password'] : '';

        $tags[] = '<#NewPasswordActivationLink#>';
        $values[] = !empty($mailData['retrievalLink']) ? '<a href="'.$mailData['retrievalLink'].'" rel="nofollow" target="_blank">'.$mailData['retrievalLink'].'</a>' : '';

        $tags[] = '<#AffiliateName#>';
        $values[] = !empty($mailData['affiliate_name']) ? $mailData['affiliate_name'] : '';

        $tags[] = '<#AffiliateID#>';
        $values[] = !empty($mailData['affiliate_id']) ? $mailData['affiliate_id'] : '';

        $tags[] = '<#OrderID#>';
        $values[] = !empty($mailData['order_row_id']) ? $mailData['order_row_id'] : '';
        
        $tags[] = '<#SalesValue#>';
        $values[] = !empty($mailData['sales_value']) ? $mailData['currency_symbol'].$mailData['sales_value'] : '';

        $tags[] = '<#FirstLevelCommission#>';
        $values[] = !empty($mailData['total_first_level_commission']) ? $mailData['currency_symbol'].$mailData['total_first_level_commission'] : '';

        $tags[] = '<#AffAdminEmail#>';
        $values[] = !empty($mailData['affAdminEmail']) ? $mailData['affAdminEmail'] : '';

        $tags[] = '<#MerchantFooterText#>';
        $values[] = !empty($mailData['footer_text']) ? $mailData['footer_text'] : '';

        $tags[] = '<#PaidCommission#>';
        $values[] = !empty($mailData['paidTotalcommission']) ? $mailData['paidTotalcommission'] : '';

        $tags[] = '<#AdminFooterText#>';
        $values[] = !empty($mailData['contact_profile_admin']['footer_text']) ? $mailData['contact_profile_admin']['footer_text'] : '';

        $invoice = $this->getMerchantOrderInvoice($mailData);
        $tags[] = '<#OrderInvoice#>';
        $values[] = $invoice ? $invoice : '';

        $recurringInvoice = $this->getRecurringOrderInvoice($mailData);
        $tags[] = '<#RecurringOrderInvoice#>';
        $values[] = $recurringInvoice ? $recurringInvoice : '';

        $tags[] = '<#AffiliatePasswordEditUrl#>';
        if(!empty($mailData['merchant_row_id']) && !empty($mailData['affiliate_row_id']))
        {
            $affiliatePasswordEditUrl = $this->getAffiliatePasswordEditUrl($mailData['merchant_row_id'], $mailData['affiliate_row_id']);

            $values[] = $affiliatePasswordEditUrl ? '<a href="'.$affiliatePasswordEditUrl.'">'.$affiliatePasswordEditUrl.'</a>' : '';
        }
        else
        {
            $values[] = '';
        }

        $subscriptionDetails = $this->getSubscriptionDetails($mailData);
        $tags[] = '<#SubscriptionDetails#>';
        $values[] = $subscriptionDetails ? $subscriptionDetails : '';

        $tags[] = '<#ExpireDate#>';
        $values[] = !empty($mailData['expiresDate']) ? $mailData['expiresDate'] : '';

        $tags[] = '<#SubscriptionPrice#>';
        $values[] = !empty($mailData['subscription_price']) ? $mailData['currency_symbol'] . $mailData['subscription_price'] : '';

        $tags[] = '<#SubscriptionType#>';
        $values[] = !empty($mailData['subscription_type']) ? $mailData['subscription_type'] : '';

        $tags[] = '<#MerchantID#>';
        $values[] = !empty($mailData['merchant_id']) ? $mailData['merchant_id'] : '';

        $tags[] = '<#MerchantEmail#>';
        $values[] = !empty($mailData['email_address']) ? $mailData['email_address'] : '';

        $tags[] = '<#MerchantContactPhone#>';
        $values[] = !empty($mailData['merchant_contact_phone']) ? $mailData['merchant_contact_phone'] : '';

        $tags[] = '<#AffiliateUserName#>';
        $values[] = !empty($mailData['affiliate_username']) ? $mailData['affiliate_username'] : '';

        $tags[] = '<#AffiliatePassword#>';
        $values[] = !empty($mailData['affiliate_password']) ? $mailData['affiliate_password'] : '';

        $tags[] = '<#AffiliateDetailsList#>';
        $values[] = !empty($mailData['affiliateList']) ? $this->getAffiliateList($mailData['affiliateList']) : '';

        $packageInformation = $this->getPackageInformation($mailData);
        $tags[] = '<#PackageAccountInformation#>';
        $values[] = $packageInformation ? $packageInformation : '';

        $tags[] = '<#AffiliateList#>';
        $values[] = !empty($mailData['summaryList']) ? $this->getAffiliatePaymentSummary($mailData['summaryList']) : '';

        $tags[] = '<#VerificationLink#>';
        $values[] = !empty($mailData['emailVerificationLink']) ? $mailData['emailVerificationLink'] : '';

        $tags[] = '<#WebsiteURL#>';
        $values[] = !empty($mailData['merchant_website_url']) ? $mailData['merchant_website_url'] : '';

        $tags[] = '<#City#>';
        $values[] = !empty($mailData['merchant_city']) ? $mailData['merchant_city'] : '';

        $tags[] = '<#ZipCode#>';
        $values[] = !empty($mailData['merchant_zip_code']) ? $mailData['merchant_zip_code'] : '';

        $tags[] = '<#LoginEmail#>';
        $values[] = !empty($mailData['merchant_email_address']) ? $mailData['merchant_email_address'] : '';

        $tags[] = '<#PostalCode#>';
        $values[] = !empty($mailData['merchant_postal_code']) ? $mailData['merchant_postal_code'] : '';

        $tags[] = '<#ContactPhone#>';
        $values[] = !empty($mailData['merchant_contact_phone']) ? $mailData['merchant_contact_phone'] : '';

        $tags[] = '<#State#>';
        $values[] = !empty($mailData['merchant_state_province_name']) ? $mailData['merchant_state_province_name'] : '';

        $tags[] = '<#ActiveStatus#>';
        $values[] = !empty($mailData['merchant_active_status']) ? $mailData['merchant_active_status'] : '';

        $tags[] = '<#Address#>';
        $values[] = !empty($mailData['merchant_address']) ? $mailData['merchant_address'] : '';

        $tags[] = '<#Expire#>';
        $values[] = !empty($mailData['merchant_is_expires']) ? $mailData['merchant_is_expires'] : '';

        return str_replace($tags, $values, $mailBody);
    }

    function getMerchantOrderInvoice($mailData)
    {
        if(empty($mailData['order_info']) && empty($mailData['customer_info']))
        {
            return '';
        }

        $mailData = assignObjects($mailData, array('customer_info'));
        $currency_symbol = $mailData['currency_symbol'];
        $invoiceData = $mailData['order_info'];
        $customerData = $mailData['customer_info'];
        $orderProductList = $mailData['orderProductList'];
        $appliedCoupons = $mailData['appliedCoupons'] ;

        $customerData = assignProperties($customerData, array(  'card_type',
                                                                'company_name',
                                                                'first_name',
                                                                'last_name',
                                                                'work_phone_number',
                                                                'home_phone_number',
                                                                'customer_email',
                                                                'shipping_name',
                                                                'shipping_company_name',
                                                                'address_line1',
                                                                'address_line2',
                                                                'shipping_address_line1',
                                                                'shipping_address_line2',
                                                                'customer_city',
                                                                'shipping_customer_city',
                                                                'customer_state',
                                                                'shipping_customer_state',
                                                                'customer_zip',
                                                                'shipping_customer_zip',
                                                                'country',
                                                                'shipping_country'));

        $returnHtml = '
            <table cellpadding="4" cellspacing="0" border="0" width="70%" align="center">
                <tr>
                    <td colspan="2" bgcolor="#e6e6e6"><b>Order Receipt</b></td>
                </tr>
                <tr>
                    <td colspan="2" height="10"></td>
                </tr>
                <tr>
                    <td width="20%">Order ID:</td>
                    <td>' . $invoiceData->order_id . '</td>
                </tr>
                <tr>
                    <td>Date:</td>
                    <td>' . $invoiceData->created_on . '</td>
                </tr>
                <tr>
                    <td>Payment Type:</td>
                    <td>' . $customerData->card_type . '</td>
                </tr>
                <tr>
                    <td>Order Status:</td>
                    <td>';

        switch($invoiceData->order_status)
        {
            case $this->config->item('cos_accepted_num') :
                $returnHtml .=$this->config->item('cos_accepted_text');
                break;
            case $this->config->item('cos_declined_num') :
                $returnHtml .=$this->config->item('cos_declined_text');
                break;
            case $this->config->item('cos_gateway_err_num') :
                $returnHtml .=$this->config->item('cos_gateway_err_text');
                break;
            case $this->config->item('cos_pending_num') :
                $returnHtml =$this->config->item('cos_pending_text');
                break;
            case $this->config->item('cos_refunded_num') :
                $returnHtml .=$this->config->item('cos_refunded_text');
                break;
            case $this->config->item('cos_unknown_num') :
                $returnHtml .="$this->config->item('cos_unknown_text')";
                break;
        }

        $returnHtml .= '
                    </td>
                </tr>
                <tr>
                    <td colspan="2" height="10"></td>
                </tr>
                <tr>
                    <td colspan="2" bgcolor="#e6e6e6" style="font-size:14px; font-weight:bold;">Invoice</td>
                </tr>
                <tr>
                    <td colspan="2" height="10"></td>
                </tr>';

        if(!empty($customerData->company_name))
        {
            $returnHtml .= '
                    <tr>
                        <td colspan="2"><b>' . $customerData->company_name . '</b>
                        <hr class="headHR"/></td>
                    </tr>';
        }

        $returnHtml .= '
                <tr>
                    <td width="35%">First Name:</td>
                    <td>' . $customerData->first_name . '</td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td>' . $customerData->last_name . '</td>
                </tr>
                <tr>
                    <td>Work Phone Number:</td>
                    <td>' . (empty($customerData->work_phone_number) ? 'n/a' : $customerData->work_phone_number) . '</td>
                </tr>
                <tr>
                    <td>Home Phone Number:</td>
                    <td>' . (empty($customerData->home_phone_number) ? 'n/a' : $customerData->home_phone_number) . '</td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td>' . $customerData->customer_email . '</td>
                </tr>
                <tr>
                    <td><b>Billing Address</b><hr class="headHR"/></td>
                    <td><b>Shipping Address</b><hr class="headHR"/></td>
                </tr>
                <tr>
                    <td>' . $customerData->first_name . ' ' . $customerData->last_name . '</td>
                    <td>' . $customerData->shipping_name . '</td>
                </tr>
                <tr>
                    <td>' . (empty($customerData->company_name) ? '' : $customerData->company_name) . '</td>
                    <td>' . (empty($customerData->shipping_company_name) ? '' : $customerData->shipping_company_name) . '</td>
                </tr>
                <tr>
                    <td>' . $customerData->address_line1 . (empty($customerData->address_line2) ? '' : '<br />' . $customerData->address_line2) . '</td>
                    <td>' . $customerData->shipping_address_line1 . (empty($customerData->shipping_address_line2) ? '' : '<br />' . $customerData->shipping_address_line2) . '</td>
                </tr>
                <tr>
                    <td>' . $customerData->customer_city . '</td>
                    <td>' . $customerData->shipping_customer_city . '</td>
                </tr>
                <tr>
                    <td>' . $customerData->customer_state . '</td>
                    <td>' . $customerData->shipping_customer_state . '</td>
                </tr>
                <tr>
                    <td>' . $customerData->customer_zip . '</td>
                    <td>' . $customerData->shipping_customer_zip . '</td>
                </tr>
                <tr>
                    <td>' . $customerData->country . '</td>
                    <td>' . $customerData->shipping_country . '</td>
                </tr>
                <tr>
                    <td colspan="2" bgcolor="#e6e6e6"><b>Order Details</b></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <table cellpadding="4" cellspacing="0" border="0" width="100%">
                            <tr>
                                <th>SKU</th>
                                <th>Description</th>
                                <th>Option</th>
                                <th>Quantity</th>
                                <th>Price</th>
                                <th>Total Price</th>
                            </tr>
                            <tr><td colspan="6"><hr class="headHR"/></td></tr>';

        $bundleId = '';
        $isFirst = TRUE;

        foreach($orderProductList as $aOrder)
        {
            $returnHtml .= '<tr>
                                <td>' . $aOrder->product_code . '&nbsp;</td>
                                <td>';

            if($aOrder->bundle_type == $this->config->item("offerTypeAdvanced"))
            {
                if($bundleId!=$aOrder->bundle_id)
                {
                    $returnHtml .= $aOrder->offer_name;
                    $bundleId = $aOrder->bundle_id;
                    $isFirst = TRUE;
                }
                else
                {
                    $returnHtml .= $aOrder->product_quantity . ' X ' . $aOrder->product_description;
                    $isFirst = FALSE;
                }
            }
            else
            {
                $returnHtml .= $aOrder->product_description;
                $isFirst = TRUE;
            }

            $returnHtml .= '    </td>
                                <td>';

                            $returnHtml .=$aOrder->product_attribute;
                            if($aOrder->free_text_attribute)
                              $returnHtml .= ',' . $aOrder->free_text_attribute;
                            $returnHtml .= '&nbsp;
                                </td>
                                <td>';

            if($isFirst)
            {
                $returnHtml .= $aOrder->product_quantity;
            }

            $returnHtml .= '&nbsp;</td>
                                <td>';

            if($isFirst)
            {
                $returnHtml .= $currency_symbol.$aOrder->product_price;
            }

            $returnHtml .= '&nbsp;</td>
                                <td align="right">';

            if($isFirst)
            {
                $returnHtml .= $currency_symbol.$aOrder->product_total_price;
            }

            $returnHtml .= '&nbsp;</td>
                            </tr>';

            $grandTotal+= $aOrder->product_total_price;
        }

        if($invoiceData->order_discount > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Order Discount</td>
                                <td align="right">' . $currency_symbol . number_format($invoiceData->order_discount,2) . '</td>
                            </tr>';
        }

        if($invoiceData->country_tax > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Country Tax</td>
                                <td align="right">' . $currency_symbol . number_format($invoiceData->country_tax, 2) . '</td>
                            </tr>';
        }

        if($invoiceData->state_tax > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">State Tax</td>
                                <td align="right">' . $currency_symbol . number_format($invoiceData->state_tax, 2) . '</td>
                            </tr>';
        }

        if($invoiceData->shpping_amount > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Shipping';
            if($mailData['shippingDetails']->shipping_id != $this->config->item('shipping_flat_rate_advanced') && !empty($mailData['shippingDetails']->method_name)){
                $returnHtml .= ' &gt;&gt; ' . $mailData['shippingDetails']->method_name;
            }
            if(!empty($mailData['shippingDetails']->shipping_service)){
                $returnHtml .= ' - ' . $mailData['shippingDetails']->shipping_service;
            }
            $returnHtml .= '</td>
                                <td align="right">' . $currency_symbol . number_format($invoiceData->shpping_amount, 2) . '</td>
                            </tr>';
        }

        if($invoiceData->coupon_discount > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    Applied Coupon Codes -- ';
            $index = 0;
            $couponsArray = array();

            foreach($appliedCoupons as $aCoupon)
            {
                $couponsArray[$index] = $aCoupon->coupon_code;
                $index++;
            }

            $couponCodes = implode(',',$couponsArray);
            $returnHtml .= $couponCodes .'
                                </td>
                                <td colspan="2" align="right">Coupon Discount</td>
                                <td align="right">' . $currency_symbol . number_format($invoiceData->coupon_discount, 2) . '</td>
                            </tr>';
        }

        $orderAmount = number_format($invoiceData->total_amount, 2);

        if($invoiceData->gift_certificate_payment > 0)
        {
            $returnHtml .= '<tr>
                                <td colspan="6"><hr class="headHR"/></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Total</td>
                                <td align="right">' . $currency_symbol . $orderAmount . '</td>
                            </tr>
                            <tr>
                                <td colspan="6"><hr class="headHR" /></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Gift Certificate Payment</td>
                                <td align="right">' . $currency_symbol . number_format($invoiceData->gift_certificate_payment, 2) . '</td>
                            </tr>';

            $orderAmount -= $invoiceData->gift_certificate_payment;
        }

        $returnHtml .= '    <tr>
                                <td colspan="6"><hr class="headHR" /></td>
                            </tr>
                            <tr>
                                <td colspan="5" align="right">Grand Total</td>
                                <td align="right">' . $currency_symbol . $orderAmount . '</td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>';

        if($invoiceData->special_comments || $invoiceData->hear_about)
        {
            $returnHtml .= '<p>
                <table cellpadding="4" cellspacing="0" border="0" width="70%" align="center">';

            if($invoiceData->special_comments)
            {
                $returnHtml .= '<tr>
                        <td width="175" valign="top"><b>Special Instructions/ Comments<b></td>
                        <td valign="top">' . $invoiceData->special_comments . '</td>
                    </tr>';
            }
            if($invoiceData->hear_about)
            {
                $returnHtml .= '<tr>
                        <td width="175" valign="top"><b>How Did You Hear of Us?<b></td>
                        <td valign="top">' . $invoiceData->hear_about . '</td>
                    </tr>';
            }
            $returnHtml .= '</table>
                        </p>';
        }
        return $returnHtml;
    }

    function getRecurringOrderInvoice($mailData)
    {    
        if(empty($mailData['order_info']) && empty($mailData['customer_info']))
        {
            return '';
        }

        $invoiceData = $mailData['order_info'];
        $customerData = $mailData['customer_info'];
        $orderProductList = $mailData['orderRecurringProductList'] ? $mailData['orderRecurringProductList'] : $mailData['orderProductList'];
        $appliedCoupons = $mailData['appliedCoupons'] ;

        $returnHTML = "<table cellpadding=\"2\" cellspacing=\"1\" border=\"0\" width=\"100%\">
                            <tr>
                                <td>
                                    <table align=\"left\" cellpadding=\"2\" cellspacing=\"1\" border=\"0\" width=\"90%\">

                        <tr><td class=\"sectionHead\" colspan=\"2\">Order Details</td></tr>
                        <tr><td>&nbsp;</td></tr>
                        <tr>
                        <td>Customer Name:</td>
                        <td align=\"left\">".$customerData->first_name."&nbsp;".$customerData->last_name."</td>
                    </tr>
                    <tr>
                        <td>Phone:</td>
                        <td align=\"left\">".$customerData->phone_number."</td>
                    </tr>
                    <tr>
                        <td>Email</td>
                        <td align=\"left\">".$customerData->customer_email."</td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td>Address Line 1:</td>
                        <td align=\"left\">".$customerData->address_line1."</td>
                    </tr>
                    <tr>
                        <td>Address Line 2:</td>
                        <td align=\"left\">".$customerData->address_line2."</td>
                    </tr>
                    <tr>
                        <td>City:</td>
                        <td align=\"left\">".$customerData->customer_city."</td>
                    </tr>
                    <tr>
                        <td>State:</td>
                        <td  align=\"left\">".$customerData->customer_state."</td>
                    </tr>
                    <tr>
                        <td>Zip Code:</td>
                        <td align=\"left\">".$customerData->customer_zip."</td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>

                    <tr>
                        <td width=\"20%\">Order ID:</td>
                        <td align=\"left\">".$invoiceData->order_id."</td>
                    </tr>
                    <tr>
                        <td>Date:</td>
                        <td align=\"left\">".$invoiceData->created_on."</td>
                    </tr>
                    <tr>
                        <td>Recurring Amount:</td>
                        <td>".$mailData['currency_symbol']. $mailData['recurring_amount']."</td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    </table>        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td>

                             <table cellpadding=\"2\" cellspacing=\"1\" border=\"1\" width=\"500\">
                                 <tr>
                                    <td width=\"30%\" align=\"center\">Order Description</td></tr>

                            <tr><td>
                    <table cellpadding=\"4\" cellspacing=\"0\" border=\"0\" width=\"100%\">
                        <tr>
                            <td class=\"leftMenuText\">SKU</td>
                            <td class=\"leftMenuText\">Description</td>
                            <td class=\"leftMenuText\">Option</td>
                            <td class=\"leftMenuText\">Quantity</td>
                            <td class=\"leftMenuText\">Price</td>
                            <td class=\"leftMenuText\">Total Price</td>
                        </tr>
                        <tr><td colspan=\"6\"><hr class=\"headHR\"/></td></tr>";

                            $bundleId = '';
                            $isFirst = TRUE;
                            foreach($orderProductList as $aOrder)
                            {
                        $returnHTML .="
                        <tr>
                            <td align=\"left\">".$aOrder->product_code."</td>
                            <td align=\"left\">";
                            if($aOrder->bundle_type==$this->config->item("offerTypeAdvanced"))
                                        {
                                            if($bundleId!=$aOrder->bundle_id){
                                                $returnHTML .= $aOrder->offer_name;
                                                $bundleId=$aOrder->bundle_id;
                                                $isFirst = TRUE;
                                            }
                                            else{
                                                $returnHTML .= $aOrder->product_quantity." X ".$aOrder->product_description;
                                                $isFirst = FALSE;
                                            }
                                        }
                                        else
                                        {
                                            $returnHTML .= $aOrder->product_description;
                                            $isFirst = TRUE;
                                        }
                            $returnHTML .="</td>
                            <td align=\"left\">";
                            $returnHTML .=$aOrder->product_attribute;
                            if($aOrder->free_text_attribute)
                              $returnHTML .= ',' . $aOrder->free_text_attribute;
                            $returnHTML .= "</td>
                            <td align=\"left\">";
                            if($isFirst){ $returnHTML .= $aOrder->product_quantity;}
                            $returnHTML .= "</td>
                            <td align=\"left\">";
                            if($isFirst){ $returnHTML .= $mailData['currency_symbol'] . $aOrder->recurring_price;}
                            $returnHTML .= "</td>
                            <td  align=\"left\">";                    
                            if($isFirst){ $returnHTML .= $mailData['currency_symbol'] . $aOrder->recurring_price;}
                            $returnHTML .="</td>
                        </tr>";


                               # $grandTotal+= $aOrder->product_total_price;
                            }
                        if($invoiceData->order_discount>0){
                        $returnHTML .="
                        <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>
                        <tr>
                            <td colspan=\"4\"></td>
                            <td>Order Discount</td>
                            <td align=\"left\">".$mailData['currency_symbol'].number_format($invoiceData->order_discount,2)."</td>
                        </tr>
                        ";
                        }
                        if($invoiceData->country_tax>0){
                        $returnHTML .="
                        <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>
                        <tr>
                            <td colspan=\"4\"></td>
                            <td>Country Tax</td>
                            <td align=\"left\" >".$mailData['currency_symbol'].number_format($invoiceData->country_tax,2)."</td>
                        </tr>
                        ";
                        }
                        if($invoiceData->state_tax>0){
                        $returnHTML .="
                         <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>
                        <tr>
                            <td colspan=\"4\"></td>
                            <td>State Tax</td>
                            <td align=\"left\" >".$mailData['currency_symbol'].number_format($invoiceData->state_tax,2)."</td>
                        </tr>";
                         }
                              if($invoiceData->shpping_amount>0){
                        $returnHTML .="
                        <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>
                        <tr>
                            <td colspan=\"4\"></td>
                            <td>Shipping</td>
                            <td align=\"left\" >".$mailData['currency_symbol'].number_format($invoiceData->shpping_amount,2)."</td>
                        </tr>
                        ";
                        }
                            if($invoiceData->coupon_discount>0)
                            {
                        $returnHTML .="
                        <tr><td colspan=\"6\"><hr class=\"middleHR\" /></td></tr>
                        <tr>
                            <td colspan=\"4\"></td>
                            <td>Coupon Discount</td>
                            <td align=\"left\" >".$mailData['currency_symbol'].number_format($invoiceData->coupon_discount,2)."</td>
                        </tr>
                        <tr>
                            <td colspan=\"6\" align=\"left\">
                                Applied Coupon Codes --";

                        $index = 0;
                        $couponsArray = array();
                        foreach($appliedCoupons as $aCoupon)
                        {
                            $couponsArray[$index] = $aCoupon->coupon_code;
                            $index++;
                        }

                        $couponCodes = implode(',',$couponsArray);
                        $returnHTML .= $couponCodes;


                        $returnHTML .="   </td>
                        </tr>";
                         }
                         $returnHTML .= "<tr><td colspan=\"6\"><hr class=\"headHR\" /></td></tr>
                        <tr>
                            <td colspan=\"4\"></td>
                            <td>Grand Total</td>
                            <td align=\"left\">".$mailData['currency_symbol'].$invoiceData->total_amount."</td>
                        </tr>
                    </table></td></tr>";



                        if($mailData['success']==0)
                        {
                    $returnHTML .="

                <tr>        <td> Your recurring payment is failed. Please contact with followings:</td>
                    </tr>
                    <tr><td>Customer E-mail :<a href=\"mailto:".$customerData->customer_email."\">".$customerData->customer_email."</a></td>
                    </tr>
                    <tr><td>Customer Phone:".$customerData->work_phone_number."</td></tr>";

                    }
                   $returnHTML .="
                                 </table>            </td>
                    </tr>
                </table>";
                return $returnHTML;
    }

    function getSubscriptionDetails($mailData)
    {
        if(empty($mailData['contact_info']))
        {
            return '';
        }

        $contact_info = $mailData['contact_info'];

        if($mailData['formType'] == 1)
        {
            $returnHTML = '<table cellpadding="4" cellspacing="0" border="0" width="70%" align="center">
                    <tr>
                        <td colspan="2" style="font-size:14px; font-weight:bold;">Subscription Information<hr class="headHR"/></td>
                    </tr>
                    <tr>
                        <td colspan="2" height="10"></td>
                    </tr>';
            $formFields = array('name' => 'Name',
                                'email' => 'Email Address',
                                'company' => 'Company',
                                'phone' => 'Phone',
                                'secondary_phone' => 'Secondary Phone',
                                'address1' => 'Address Line 1',
                                'address2' => 'Address Line 2',
                                'fax' => 'Fax',
                                'city_town' => 'City / Town',
                                'state_province' => 'State / Province',
                                'zip_postal' => 'Zip / Postal Code',
                                'country' => 'Country',
                                'remote_ip' => 'IP Address'
                            );
            foreach($formFields as $field => $label)
            {
                if(!empty($contact_info[$field]))
                {
                    $returnHTML .= '<tr>
                                        <td width="35%">' . $label .':</td>
                                        <td>' . $contact_info[$field] . '</td>
                                    </tr>';
                }
            }

            $returnHTML .= '</table>';
        }
        else
        {
            $returnHTML = '<table cellpadding="4" cellspacing="0" border="0" width="70%" align="center">
                <tr>
                    <td colspan="2" style="font-size:14px; font-weight:bold;">Subscription Information<hr class="headHR"/></td>
                </tr>
                <tr>
                    <td colspan="2" height="10"></td>
                </tr>
                <tr>
                    <td width="35%">Name:</td>
                    <td>' . $contact_info['name'] . '</td>
                </tr>
                <tr>
                    <td width="35%">Email Address:</td>
                    <td>' . $contact_info['email'] . '</td>
                </tr>
                <tr>
                    <td width="35%">IP Address:</td>
                    <td>' . $contact_info['remote_ip'] . '</td>
                </tr>
            </table>';
        }
        return $returnHTML;
    }

    function getPackageInformation($mailData)
    {
        if(empty($mailData['packageModules']))
        {
            return '';
        }

        $packageModules = $mailData['packageModules'];
        $prCredential = $mailData['prCredential'];
        $pwtCredential = $mailData['pwtCredential'];
        $this->load->config('web-service');

        if(count($packageModules))
        {
            foreach ($packageModules as $aModule)
            {
                 switch ($aModule->module_id)
                 {
                    /**
                     * user create from premiumwebcart to PR & PWT
                     */
                     case $this->config->item('premium_response_module_id'):

                        $returnHTML .= "<tr><td height=\"15px\"></td></tr>
                        <tr><td>Your ".anchor($this->config->item('premium_response_login_link'),$this->config->item('premium_response_project_name'),'target="_blank"')." account has been created</td></tr>
                        <tr><td>you can access your account with following information</td></tr>
                        <tr><td>User Name : ".$prCredential['email_address']."</td></tr>
                        <tr><td>Password : ".$prCredential['customer_password']."</td></tr>";

                        break;

                    case $this->config->item('premium_web_tracking_module_id'):

                        $returnHTML .= "<tr><td height=\"15px\"></td></tr>
                        <tr><td>Your ".anchor($this->config->item('premium_web_tracking_login_link'),$this->config->item('premium_web_tracking_project_name'),'target="_blank"')." account has been created </td></tr>
                        <tr><td>you can access your account with following information</td></tr>
                        <tr><td>User Name : ".$pwtCredential['email_address']."</td></tr>
                        <tr><td>Password : ".$pwtCredential['customer_password']."</td></tr>";

                        break;

                    case $this->config->item('premium_auto_responder_module_id'):

                        $returnHTML = "<tr><td height=\"15px\"></td></tr>
                        <tr><td>Please create your ".$this->config->item('premium_auto_responder_project_name')." account from the following ".anchor($this->config->item('premium_auto_responder_project_link'),'link','target="_blank"')."</td></tr>";

                        break;
                 }
            }
        }
        return $returnHTML;
    }

    function getAffiliatePaymentSummary($summaryList)
    {
        $returnHTML = '<table cellpadding="2" cellspacing="0" border="0" width="100%" align="center">
                        <tr>
                            <th>Affiliate Name</th>
                            <th>Affiliate ID</th>
                            <th>Company Name</th>
                            <th>Commission Type</th>
                            <th>Commission Amount</th>
                            <th>Refunded Amount</th>
                            <th>Paid Amount</th>
                            <th>Payment Information </th>
                        </tr>
                        <tr>
                            <td colspan="8"><hr height="1"></hr></td>
                        </tr>';
        $totalAmount = 0;
        foreach($summaryList as $row)
        {
            $returnHTML .= '<tr>
                                <td align="left" valign="top">'.$row['Affiliate Name'].'</td>
                                <td  align="left" valign="top">'.$row['Affiliate ID'].'</td>
                                <td  align="left" valign="top">'.$row['Company Name'].'</td>
                                <td  align="left" valign="top">'.$row['Commission Type'].'</td>
                                <td  align="right" valign="top">'.$row['Commission Amount'].'</td>
                                <td  align="right" valign="top">'.$row['Refunded Amount'].'</td>
                                <td  align="right" valign="top">'.$row['Paid Amount'].'</td>
                                <td align="left">'.$row['Payment Information'].'</td>
                           </tr>';
            $row_amount = explode($_SESSION['currency_symbol'], $row['Paid Amount']);
            $totalAmount += $row_amount[1];
        }
        $returnHTML .= '<tr>
                            <td colspan="8"><hr height="1"></hr></td>
                        </tr>
                        <tr>
                            <td colspan="5">&nbsp;</td>
                            <td colspan="2" align="right"><b>Total Amount: '.$_SESSION['currency_symbol'].$totalAmount.'</b></td>
                            <td>&nbsp;</td>
                        </tr>';
        $returnHTML .= '</table>';
        return $returnHTML;
    }

    function getAffiliateList($affiliateList)
    {
        $returnHTML = '<table cellpadding="2" cellspacing="0" border="0" width="100%" align="center">
                        <tr>
                            <th>Affiliate Name</th>
                            <th>Username</th>
                            <th>Password</th>
                        </tr>';
        foreach($affiliateList as $anAffiliate)
        {
            $returnHTML .= '<tr>
                                <td align="center"><b>'.$anAffiliate['affiliate_name'].'</b></td>
                                <td  align="center"><b>'.$anAffiliate['affiliate_username'].'</b></td>
                                <td  align="center"><b>'.$anAffiliate['affiliate_password'].'</b></td>
                            </tr>';
        }
        $returnHTML .= '</table>';
        return $returnHTML;

    }

    function sendTestEmail($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($this->config->item('adminNotifyEmail'),$this->config->item('adminNotifyName'));
        $this->email->to($data['email'], $data['name']);
        $this->email->subject($data['subject']);
        $this->email->message($data['body']);
        if($this->email->send())
        {
            $this->email->clear();
            return true;
        }
        $this->email->clear();
        return false;
    }

    function sendTrialExpirationNotice($data)
    {
        $emailInfo = $this->getAdminCustomEmailInfo('1033');

        $this->email->set_mailtype('html');
        $this->email->from($data['from_address'],$data['full_name']);
        $this->email->reply_to($data['from_address']);
        $this->email->to($data['to_address']);
        $this->email->subject($emailInfo['email_subject']);
        $body = html_entity_decode($emailInfo['email_body']);

        $tags = array(  '<#MerchantFirstName#>',
                        '<#MerchantLastName#>',
                        '<#NumberOfDaysPassed#>',
                        '<#NumberOfDaysLeft#>',
                        '<#AdminFooterText#>');
        $values = array($data['first_name'],
                        $data['last_name'],
                        $data['number_of_days_passed'],
                        $data['number_of_days_left'],
                        $data['contact_profile_admin']['footer_text']);

        $body = str_replace($tags, $values, $body);
        $this->email->message($body);
        $this->email->send();
        $this->email->clear();
    }

    function sendAdminAffiliateActivationEmailToAffiliate($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($this->config->item('adminNotifyEmail'),'PremiumWebCart - Josh');

        $this->email->to($data['affiliate_email'], $data['affiliate_name']);
        $this->email->subject('Webmaster Partnership Account Activation Notification.');
        $body = $this->load->view('administration/mails/10001',$data,true);

        $this->email->message($body);
        if($this->email->send())
        {
            $this->email->clear();
            return true;
        }

        $this->email->clear();
        return false;
    }

    function sendAdminAffiliateActivationEmailToAdmin($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($this->config->item('adminNotifyEmail'),$this->config->item('adminNotifyName'));

        if($data['aff_admin_url'])
            $this->email->to($data['aff_admin_url'], $data['first_name'] . ' ' . $data['last_name']);
        else
            $this->email->to($data['email_address'], $data['first_name'] . ' ' . $data['last_name']);

        $this->email->subject('Webmaster Partnership Account Activation Notification.');

        $body = $this->load->view('administration/mails/10002',$data,true);
        $this->email->message($body);
        if($this->email->send())
        {

            $this->email->clear();
            return true;
        }

        $this->email->clear();
        return false;
    }

    function sendAdminAffiliateSignupEmailToAffiliate($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($this->config->item('adminNotifyEmail'),'PremiumWebCart - Josh');

        $this->email->to($data['affiliate_email'], $data['affiliate_name']);
        $this->email->subject('Webmaster Partnership Account Activation Notification.');
        $body = $this->load->view('administration/mails/10003',$data,true);

        $this->email->message($body);
        if($this->email->send())
        {
            $this->email->clear();
            return true;
        }

        $this->email->clear();
        return false;
    }

    function sendAdminAffiliateSignupEmailToAdmin($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($this->config->item('adminNotifyEmail'),$this->config->item('adminNotifyName'));

        if($data['aff_admin_url'])
            $this->email->to($data['aff_admin_url'], $data['first_name'] . ' ' . $data['last_name']);
        else
            $this->email->to($data['email_address'], $data['first_name'] . ' ' . $data['last_name']);

        $this->email->subject('Webmaster Partnership Account Activation Notification.');

        $body = $this->load->view('administration/mails/10004',$data,true);
        $this->email->message($body);
        if($this->email->send())
        {
           $this->email->clear();
           return true;
        }

        $this->email->clear();
        return false;
    }

    function sendRecurringCartExpireNotification($aRecurring, $merchantBasicInfo, $merchantContactFooter, $merchantDetails)
    {
        $data['merchant_row_id'] = $aRecurring->merchant_row_id;
        $data['recurring_cycle'] = $aRecurring->recurring_cycle;
        $data['recurring_price'] = $aRecurring->recurring_price;

        // getting merchants information
        $data['currency_symbol']          = $merchantBasicInfo->currency_symbol;
        $data['merchant_info']            = $merchantDetails;
        $data['contact_profile_merchant'] = $merchantContactFooter;

        // getting customer info
        $data['customer_id'] = $aRecurring->customer_id;
        $data['first_name']  = $aRecurring->first_name;
        $data['last_name']   = $aRecurring->last_name;
        $data['next_subscription_date']   = $aRecurring->next_subscription_date;

        // get product information
        $CI =& get_instance();
        $CI->load->model('products/Product_model', 'Product_model', TRUE);
        $productDescription = $CI->Product_model->getProductDescriptionByProductRowId($aRecurring->product_row_id);

        $data['productDesc'] = $productDescription['product_description'];

        $data['customerbillingaddresseditUrl'] = $data['editUrl'] = base_url().'edit_billinginfo/editBillingInfo/'.md5($aRecurring->customer_id);

        $customerEmailInfo = $this->getCustomerEmailInfo($aRecurring->merchant_row_id, 1040);

        $email_body = $customerEmailInfo->email_body;

        $orderPlaceEmailBody = $this->getParshCustomerMailBody($email_body,$data);

        $this->email->set_mailtype('html');
        $this->email->from($merchantBasicInfo->from_address, $merchantBasicInfo->from_name);
        $this->email->to($aRecurring->customer_email);
        $this->email->subject($customerEmailInfo->email_subject);
        $this->email->message($orderPlaceEmailBody);
        $this->email->send();
        $this->email->clear();
    }

    function tellFriend($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($data['affiliate_email'],$data['affiliate_name']);

        $this->email->to($data['email']);
        $this->email->reply_to($this->config->item('adminNotifyEmail'), $this->config->item('adminNotifyName'));

        $this->email->subject($data['subject']);

        $this->email->message($data['body']);

        if($this->email->send())
        {
           $this->email->clear();
           return true;
        }

        $this->email->clear();
        return false;
    }

    function sendStoresNotification($data)
    {
        $this->email->set_mailtype('html');
        $this->email->from($this->config->item('adminNotifyEmail'), $this->config->item('adminNotifyName'));
        $this->email->reply_to($this->config->item('adminNotifyEmail'), $this->config->item('adminNotifyName'));
        $this->email->to($this->config->item('store_notify_email'));
        $this->email->subject('A request for PWC Stores has been submitted by ' . $data['merchant']->merchant_id);
        $body = $this->load->view('mailScripts/storeRequestMail', $data, true);
        $this->email->message($body);
        $this->email->send();
        $this->email->clear();
    }
}
?>