<?php
/**
 * Coupon Summary Report
 *
 * @category Report
 * @package  ClientExec
 * @author   Jason Yates <jason@clientexec.com>
 * @license  ClientExec License
 * @version  1.1
 * @link     http://www.clientexec.com
 *
 *************************************************
 *   1.1 Updated the report to use Pear Commenting & the new title handing to make app reports consistent.
 ************************************************
 */



/**
 * Coupon_Summary Report Class
 *
 * @category Report
 * @package  ClientExec
 * @author   Jason Yates <jason@clientexec.com>
 * @license  ClientExec License
 * @version  1.1
 * @link     http://www.clientexec.com
 */
class Coupon_Summary extends Report
{
    
    /**
     * Report Process Method
     *
     * @return null - direct output
     */
    function process()
    {
        // Set the report information
        $this->setName('Coupon Summary', true);
        $this->setVersion("1.1");
        $this->SetDescription($this->user->lang('Displays stats on usage of individual coupon codes.'));

        // Output the headers before anything else
        $this->showReportHeader();
        ?>

<script type="text/javascript">
    function viewCouponSelected(changedType)
    {
        var tagname =document.getElementsByTagName('input');
        for(var i = 0; i < tagname.length; i++) {
            if(tagname[i].type == 'radio' && tagname[i].checked == true){
                var couponType = tagname[i].value;
            }
        }
        if(changedType == 1){
            $("coupons").value = 0;
        }

        location.href='index.php?fuse=reports&report=Coupon_Summary&type=Transactions&view=ViewReport&couponId='+$("coupons").value+'&isRecurring='+couponType;
    }
</script>
        <?php

        $aGroup = array();
        $i = 0;
        $selected_normal = "";
        $selected_recurring = "";
        $isRecurring = (isset($_GET['isRecurring']))? $_GET['isRecurring']:0;

        if(isset($_GET['isRecurring'])) {
            if($_GET['isRecurring'] == 0) {
                $selected_normal = "checked";
            }else {
                $selected_recurring = "checked";
            }
        }

        echo "<input type='Radio' name='coupon_type' value='0' onChange='viewCouponSelected(1)' $selected_normal> Normal";
        echo "<br>";
        echo "<input type='Radio' name='coupon_type' value='1' onChange='viewCouponSelected(1)' $selected_recurring> Recurring";
        echo "<br>";

        $couponsInUse = "SELECT DISTINCT(coupons_id), coupons_name "
                ."FROM coupons "
                ."WHERE coupons_recurring = ?";
        $result = $this->db->query($couponsInUse, $isRecurring);

        echo "<br>".$this->user->lang('Select a coupon:');
        echo "<select id=coupons name=coupons onChange='viewCouponSelected(0)'>";
        echo"<option value=0>".$this->user->lang('--- SELECT ONE ---')."</option>";
        while (list($coupons_id, $coupons_name) = $result->fetch()) {
            if(isset($_GET['couponId']) && $_GET['couponId'] == $coupons_id) {
                echo"<option selected value=".$coupons_id.">".$coupons_name."</option>";
            }else {
                echo"<option value=".$coupons_id.">".$coupons_name."</option>";
            }
        }
        echo "</select><br /><br />";

        if(!isset($_GET['couponId'])) {
            $_GET['couponId'] = 0;
        }

        $notUsedCoupons = "SELECT coupons_quantity "
                ."FROM coupons "
                ."WHERE coupons_id =".$_GET['couponId'];

        $result = $this->db->query($notUsedCoupons);

        list($totalNotUsedCoupons) = $result->fetch();
        if($isRecurring == 0) {
            $couponsReport = "SELECT u.id, u.firstname, u.lastname, c.coupons_name, c.coupons_description, c.coupons_discount, c.coupons_type, c.coupons_archive, ie.invoiceid, ie.detail, inv.amount, ie.date "
                    ."FROM invoiceentry ie, coupons c, users u, coupons_usage cu, invoice inv "
                    ."WHERE ie.id = cu.invoiceentryid AND cu.couponid = ".$_GET['couponId']." AND ie.customerid = u.id AND c.coupons_id = cu.couponid AND ie.invoiceid = inv.id";
        }else {
            $couponsReport = "SELECT u.id, u.firstname, u.lastname, c.coupons_name, c.coupons_description, c.coupons_discount, c.coupons_type, c.coupons_archive, ie.invoiceid, ie.detail, inv.amount, ie.date "
                    ."FROM invoiceentry ie, coupons c, users u, coupons_usage cu, invoice inv, recurringfee rf "
                    ."WHERE rf.id = cu.invoiceentryid AND rf.status = 1 AND cu.couponid = ".$_GET['couponId']." AND ie.customerid = u.id AND c.coupons_id = cu.couponid AND ie.invoiceid = inv.id AND rf.id = ie. recurringappliesto GROUP BY u.firstname, u.lastname";
        }

        $result = $this->db->query($couponsReport);

        include_once 'modules/billing/models/Currency.php';
        $currency = new Currency($this->user);
        $totalSaved = 0;
        while (list($userId, $firstname, $lastname, $coupons_name, $coupons_description, $coupons_discount, $coupons_type, $coupons_archive, $invoiceid, $invoice_detail, $amount, $coupon_date) = $result->fetch()) {

            ($coupons_archive == 0) ? $archived = $this->user->lang('No') : $archived = $this->user->lang('Yes');
            $date = $this->convertDate($coupon_date);
            $tUser = new User($userId);
            if($coupons_type == 1) {
                $invoiceSaved = 0;
                if ($coupons_discount == 1) {
                    $invoiceSaved = $amount;
                    $totalSaved += $invoiceSaved;
                } else {
                    $invoiceSaved = ($amount/(1-$coupons_discount) - $amount);
                    $totalSaved += $invoiceSaved;
                }
                $aGroup[] = array("<a href=\"index.php?frmClientID=".$userId."&fuse=clients&view=ShowCustomerData\">".$lastname.", ".$firstname."</a>", $coupons_name,  "<a href=index.php?fuse=clients&frmClientID=".$userId."&view=ViewInvoices&billdetailid=".$invoiceid.">".$invoiceid."</a>", $date, ($coupons_discount*100)."% (".$currency->format($tUser->getCurrency(), $invoiceSaved, true, false).")", $currency->format($tUser->getCurrency(),$amount, true, false), $invoice_detail, $archived, true, false);
            }else {
                $totalSaved += $coupons_discount;
                $aGroup[] = array("<a href=\"index.php?frmClientID=".$userId."&fuse=clients&view=ShowCustomerData\">".$lastname.", ".$firstname."</a>", $coupons_name, "<a href=index.php?fuse=clients&frmClientID=".$userId."&view=ViewInvoices&billdetailid=".$invoiceid.">".$invoiceid."</a>", $date, $currency->format($tUser->getCurrency(), $coupons_discount, true, false), $currency->format($tUser->getCurrency(), $amount, true, false), $invoice_detail, $archived, true, false);

                if($isRecurring == 1) {
                    $sql = "SELECT ie.date, ie.invoiceid  FROM recurringfee rf, invoiceentry ie, coupons_usage cu WHERE  ie.customerid = ? AND rf.id = ie.recurringappliesto AND rf.status = 1 AND cu.couponid = ? AND cu.invoiceentryid = rf.id";
                    $result2 = $this->db->query($sql, $userId, $_GET['couponId']);
                    $timesUsed = 0;
                    $valuesOfPayment = "<b>Payments: </b>";
                    while(list($date, $idInvoiceEntry)=$result2->fetch()) {
                        $valuesOfPayment .= "(".$date.") ";
                        $timesUsed++;
                    }
                    //$valuesOfPayment2 = ;
                    $aGroup[] = array($valuesOfPayment, "<br><b>Coupon used ".$timesUsed." time(s)</b>");
                }


            }
            $i++;
        }

        if($totalSaved > 0) {
            $aGroup[] = array('', '', '', $this->user->lang('<b>Total saved</b>'), '<b>'.$currency->format($tUser->getCurrency(), $totalSaved, true, false).'</b>');
        }

        $this->SetDescription("<b>".(($totalNotUsedCoupons == -1) ? $this->user->lang('unlimited') : $totalNotUsedCoupons)."</b> ".$this->user->lang('Avaliable coupons')."<br><b>".$i."</b> ".$this->user->lang('Coupons have already been used'));

        if(count($aGroup) > 0) {
            $this->Add($aGroup,$this->user->lang('Customers'),array($this->user->lang('Customer'),$this->user->lang('Coupon Name'), $this->user->lang('Invoice #'), $this->user->lang('Date'), $this->user->lang('Discount'), $this->user->lang('Amount'), $this->user->lang('Details'), $this->user->lang('Archived')));
            $this->Show();
        }else if($_GET['couponId'] > 0) {
            echo "<br> <b>THERE ARE NO CUSTOMERS USING THIS COUPON YET</b> <br><br>";
        }

    }

    function convertDate($date)
    {
        $date = date("F j, Y", strtotime($date));
        return $date;
    }
}
?>
