<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of User class
 * Use for user relarated functionlities
 * @author Asha
 */
class CouponCode {
    //put your code here
     public static function is_CouponCode_Existing($ccCode) {
        global $tableprefix;
        
        if(!empty($ccCode)) {
            /*check whether coupon with the same name exists in the database*/	
            $resSet = DbHelper::execute("SELECT ccID FROM ".$tableprefix."couponcode WHERE LOWER(ccCode) = ".$ccCode);
            $data = DbHelper::fetchOne($resSet);            
        }
            return $data;
    } // End

    
    
    //Function to Insert Coupon Code
    
    public static function add_CouponCode($objProductVo)
    {
            $txtCouponStartDate=$objProductVo['txtCouponStartDate'];
            $txtCouponStartDate=explode('/',$txtCouponStartDate);
            $txtCouponStartDate=$txtCouponStartDate[2]."-".$txtCouponStartDate[0]."-".$txtCouponStartDate[1];
	
            $txtCouponEndDate=$objProductVo['txtCouponEndDate'];
            $txtCouponEndDate=explode('/',$txtCouponEndDate);
            $txtCouponEndDate=$txtCouponEndDate[2]."-".$txtCouponEndDate[0]."-".$txtCouponEndDate[1];    
            
            
            if($objProductVo['product_id']!=''){
                    $product_ids   = implode(',',$objProductVo['product_id']);
             }else {
                    $product_ids   = '';
             }

            if($objProductVo['category_id']!=''){
                $product_cat_ids  = implode(',',$objProductVo['category_id']);
            }else {
                $product_cat_ids ='';
            }

           
            $objResult             = new stdClass();
            $tableName             = DbHelper::setTablePrefix('couponcode');
            $ccId                  = DbHelper::insert($tableName, array('ccStartDate'                              =>  generalMethods::escapeString($txtCouponStartDate),
                                                                                     'ccEndDate'                   =>  generalMethods::escapeString($txtCouponEndDate),
                                                                                     'ccCode'                      =>  generalMethods::escapeString($objProductVo['txtCouponCode']),
                                                                                     'sellerId'                    =>  generalMethods::escapeString($objProductVo['sellerId']),
                                                                                     'ccDescription'               =>  generalMethods::escapeString($objProductVo['txtCouponDescription']),
                                                                                     'ccDiscount'                  =>  generalMethods::escapeString($objProductVo['txtCouponDiscount']),
                                                                                     'coupon_type'                 =>  generalMethods::escapeString($objProductVo['coupon_type']),
                                                                                     'categoryId'                  =>  generalMethods::escapeString($product_cat_ids),
                                                                                     'productId'                   =>  generalMethods::escapeString($product_ids),
                                                                                     'subscriptionStatus'          => 'Y',
                                                                                     'ccStatus'                    => 'Y'
                                                                                ));
         if($ccId) {  
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =    $ccId; 
         }else{
             $objResult->status                 =    ERROR;
         }
            
         return $objResult;
    }
    
    // update coupon code
    public static function update_CouponCode($objProductVo)
    {
            global $tableprefix;
            $txtCouponStartDate=$objProductVo['ccStartDate'];
            $txtCouponStartDate=explode('/',$txtCouponStartDate);
            $txtCouponStartDate=$txtCouponStartDate[2]."-".$txtCouponStartDate[0]."-".$txtCouponStartDate[1];

            $txtCouponEndDate=$objProductVo['ccEndDate'];
            $txtCouponEndDate=explode('/',$txtCouponEndDate);
            $txtCouponEndDate=$txtCouponEndDate[2]."-".$txtCouponEndDate[0]."-".$txtCouponEndDate[1];           

            if($objProductVo['product_id']!=''){
                    $product_ids   = implode(',',$objProductVo['product_id']);
             }else {
                    $product_ids   = '';
             }

            if($objProductVo['category_id']!=''){
                $product_cat_ids  = implode(',',$objProductVo['category_id']);
            }else {
                $product_cat_ids ='';
            }
            
            $objResult             = new stdClass();
            $tableName             = DbHelper::setTablePrefix('couponcode'); 
            $where = " ccID = ".$_REQUEST['edit_id'];
            
            
            
            $data                  = DbHelper::update($tableName,array('ccStartDate'                               =>  generalMethods::escapeString($txtCouponStartDate),
                                                                                     'ccEndDate'                   =>  generalMethods::escapeString($txtCouponEndDate),
                                                                                     'ccCode'                      =>  generalMethods::escapeString($objProductVo['ccCode']),
                                                                                     'sellerId'                    =>  generalMethods::escapeString($objProductVo['sellerId']),
                                                                                     'ccDescription'               =>  generalMethods::escapeString($objProductVo['ccDescription']),
                                                                                     'coupon_type'                 =>  generalMethods::escapeString($objProductVo['coupon_type']),
                                                                                     'categoryId'                  =>  generalMethods::escapeString($product_cat_ids),
                                                                                     'productId'                   =>  generalMethods::escapeString($product_ids),
                                                                                     'ccDiscount'                  =>  generalMethods::escapeString($objProductVo['ccDiscount'])
                                                                                  
                                                                                ),$where);
            
            return;

   }
    
    
    // function to delete user favourites list 
    public static function delete_user_favourite_list($wishId,$userid)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('favourites');
            $where                               = "nId =".$wishId. " AND nUserId =".$userid;
            $affectedRows                         = DbHelper::delete($tableName,$where);
         if($affectedRows>0) {  
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =  $affectedRows; 
         }else{
             $objResult->status                 =    ERROR;
         }
            
         return $objResult;
    }



    
    
    
}
?>
