function getAllBlogs()
{
    $rarray = array();
    $conditions = array();
    $restaurants = findByConditionArray(array(), 'blogs');
    if (!empty($restaurants)) {
        $restaurants = array_map(function ($t) {
            $s = array();
            $user = findByIdArray($t['user_id'], 'users');
            $s['id'] = $t['id'];
            $s['user_id'] = $t['user_id'];
            //$s['merchant_name'] = (!empty($user['merchant_name'])?$user['merchant_name']:'');
            $s['title'] = $t['title'];
            $s['description'] = $t['description'];
            $s['image'] = $t['image'];
            if (!empty($s['image'])) {
                $s['image_url'] = SITEURL . 'blog_images/' . $s['image'];
            }
            $s['money_point'] = $t['money_point'];
            $s['posted_on'] = date('d M, Y h:i A', strtotime($t['posted_on']));
            //$t['restaurant'] = findByIdArray( $t['restaurant_id'],'restaurants');
            return $s;
        }, $restaurants);
        $rarray = array('type' => 'success', 'data' => $restaurants);
    } else {
        $rarray = array('type' => 'error', 'message' => 'No blogs found');
    }
    echo json_encode($rarray);
}
function getMenuByUser($user_id)
{
    $rarray = array();
    try {
        $sql = "SELECT * FROM menus WHERE user_id=:user_id ORDER BY id DESC";
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("user_id", $user_id);
        $stmt->execute();
        $points = $stmt->fetchAll(PDO::FETCH_OBJ);
        if (!empty($points)) {
            $points = array_map(function ($t, $k) {
                $t->sl = $k + 1;
                if (!empty($t->image)) {
                    $t->imageurl = SITEURL . 'menu_images/' . $t->image;
                }
                if (!empty($t->status)) {
                    $t->status = 'Active';
                } else {
                    $t->status = 'Inactive';
                }
                if (!empty($t->is_featured)) {
                    $t->is_featured = 'Yes';
                } else {
                    $t->is_featured = 'No';
                }
                $t->formatprice = number_format($t->price, 1, '.', ',');
                $restaurant_details = findByIdArray($t->merchantrestaurant_id, 'merchantrestaurants');
                $t->restaurant = $restaurant_details['title'];
                // Data binding in angularJs is automatic synchronisation of data between model and view
                //$t->type = ($t->type=='C'?'Credit':'Debit');
                //$t->date = date('m/d/Y',  strtotime($t->date));
                //$t->expire_date = date('m/d/Y',  strtotime($t->expire_date));
                return $t;
            }, $points, array_keys($points));
        }
        $rarray = array('status' => 'success', 'data' => $points);
    } catch (PDOException $e) {
        $rarray = array('status' => 'error', 'error' => array('text' => $e->getMessage()));
    }
    echo json_encode($rarray);
    exit;
}
function getMerchantRestaurantDetails($res_id)
{
    $rarray = array();
    $details = findByIdArray($res_id, 'merchantrestaurants');
    if (!empty($details)) {
        $details['logo_url'] = SITEURL . 'merchantrestaurant_images/' . $details['logo'];
        $outlet_query = "SELECT * FROM merchantoutlets WHERE restaurant_id={$res_id} and is_active=1";
        $outlets = findByQuery($outlet_query);
        $outlets = array_map(function ($t) {
            $t['image_url'] = SITEURL . 'merchantoutlet_images/' . $t['image'];
            $s['iconurl'] = "";
            if (!empty($t['icon'])) {
                $t['iconurl'] = SITEURL . 'merchantrestaurant_icon/' . $t['icon'];
            }
            return $t;
        }, $outlets);
        $details['outlets'] = $outlets;
        $rarray = array('type' => 'success', 'data' => $details);
    } else {
        $rarray = array('type' => 'error', 'message' => 'Restaurant not found.');
    }
    echo json_encode($rarray);
}
function getAdsDetails($id)
{
    $rarray = array();
    $restaurant = findByIdArray($id, 'advertisements');
    if (!empty($restaurant)) {
        $rarray['restaurant'] = $restaurant;
        if (!empty($restaurant['image'])) {
            $rarray['restaurant']['image_url'] = SITEURL . "advertisement_images/" . $restaurant['image'];
        }
        $rarray['locations'] = findByConditionArray(array('advertisement_id' => $id), 'advertisement_location_map');
        $rarray['outlets'] = findByConditionArray(array('advertisement_id' => $id), 'advertisement_outlet_map');
        $rarray['merchant'] = findByIdArray($restaurant['merchant_id'], 'users');
        $result = json_encode(array('type' => "success", 'data' => $rarray));
    } else {
        $result = '{type:"error",message:"Not Added"}';
    }
    echo $result;
    exit;
}
function updateOutlet()
{
    $rarray = array();
    $request = Slim::getInstance()->request();
    $body = json_decode($request->getBody());
    $categories = $body->category_id;
    $locations = $body->location_id;
    $outlet_id = $body->id;
    unset($body->id);
    if (isset($body->category_id)) {
        unset($body->category_id);
    }
    if (isset($body->location_id)) {
        unset($body->location_id);
    }
    //$body->restaurant_id = $body->restaurant_id;
    unset($body->resturant_id);
    $allinfo['save_data'] = $body;
    $restaurant = findByIdArray($body->restaurant_id, 'restaurants');
    //print_r($restaurant);
    //exit;
    if (!empty($restaurant)) {
        $body->user_id = $restaurant['user_id'];
        $cat_details = edit(json_encode($allinfo), 'outlets', $outlet_id);
        //var_dump($cat_details);
        //exit;
        if (!empty($cat_details)) {
            $outlet_details = json_decode($cat_details);
            deleteAll('outlet_category_map', array('outlet_id' => $outlet_id));
            foreach ($categories as $category) {
                $temp = array();
                $temp['outlet_id'] = $outlet_details->id;
                $temp['category_id'] = $category->id;
                $data = add(json_encode(array('save_data' => $temp)), 'outlet_category_map');
            }
            deleteAll('outlet_location_map', array('outlet_id' => $outlet_id));
            foreach ($locations as $location) {
                $temp = array();
                $temp['outlet_id'] = $outlet_details->id;
                $temp['location_id'] = $location->id;
                $data = add(json_encode(array('save_data' => $temp)), 'outlet_location_map');
            }
            $rarray = array('type' => 'success', 'message' => 'Added Succesfully');
            #$result = '{"type":"success","message":"Added Succesfully"}';
        } else {
            $rarray = array('type' => 'error', 'message' => 'Internal server error.');
        }
    } else {
        $rarray = array('type' => 'error', 'message' => 'No restaurant found');
    }
    echo json_encode($rarray);
    exit;
}
function getUser($id)
{
    $request = findById($id, 'users');
    $body = json_decode($request);
    //print_r($body);exit;
    if (!empty($body)) {
        $site_path = SITEURL;
        $aa = date("d M, Y", strtotime($body->registration_date));
        $body->registration_date = $aa;
        $ab = date("d M, Y", strtotime($body->last_login));
        $body->last_login = $ab;
        if (!empty($body->image)) {
            $bb = $site_path . "user_images/" . $body->image;
        } else {
            $bb = $site_path . "user_images/default-profile.png";
        }
        $body->image = $bb;
        $body->locations = json_decode(findByCondition(array('user_id' => $id), 'merchant_location_map'));
        // print_r($body->locations);
        // exit;
        $body->categories = json_decode(findByCondition(array('user_id' => $id), 'merchant_category_map'));
        foreach ($body->categories as $k => $cat) {
            $categoryname = findByIdArray($cat->category_id, 'category');
            $body->categories[$k]->category = strtoupper($categoryname['name']);
        }
        $user_details = json_encode($body);
        $result = '{"type":"success","user_details":' . $user_details . '}';
    }
    echo $result;
}
function getSwapVoucherDetail($sid)
{
    $is_active = 1;
    $newdate = date('Y-m-d');
    $sql = "SELECT offers.title, offers.price, offers.offer_percent, vouchers.item_expire_date as offer_to_date, offers.image, swap.id, swap.voucher_id, swap.user_id, swap.offer_id,swap.posted_on,swap.offering_end_date,swap.offering_start_date,swap.description  FROM vouchers, swap, offers where swap.offer_id=offers.id and vouchers.id=swap.voucher_id and swap.is_active=1 and swap.id =:sid";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("sid", $sid);
        $stmt->execute();
        $vouchers = $stmt->fetchAll(PDO::FETCH_OBJ);
        $count = $stmt->rowCount();
        for ($i = 0; $i < $count; $i++) {
            $todate = date('d M, Y', strtotime($vouchers[$i]->offer_to_date));
            $vouchers[$i]->expire_date = $todate;
            if ($vouchers[$i]->offering_end_date == "0000-00-00 00:00:00") {
                $vouchers[$i]->offering_end_date = "";
            } else {
                $vouchers[$i]->offering_end_date = date('d M, Y', strtotime($vouchers[$i]->offering_end_date));
            }
            if ($vouchers[$i]->offering_start_date == "0000-00-00 00:00:00") {
                $vouchers[$i]->offering_start_date = "";
            } else {
                $vouchers[$i]->offering_start_date = date('d M, Y', strtotime($vouchers[$i]->offering_start_date));
            }
            $vouchers[$i]->posted_on = date('d M,Y', strtotime($vouchers[$i]->posted_on));
            if (!empty($vouchers[$i]->image)) {
                $vouchers[$i]->image_url = SITEURL . 'voucher_images/' . $vouchers[$i]->image;
            }
            $netprice = $vouchers[$i]->price - $vouchers[$i]->price * $vouchers[$i]->offer_percent / 100;
            $vouchers[$i]->price = number_format($vouchers[$i]->price, 1, '.', ',');
            $vouchers[$i]->netprice = number_format($netprice, 1, '.', ',');
            //$vouchers[$i]->categories = json_decode(findByCondition(array('swap_id'=>$vouchers[$i]->id),'swap_category_map'));
            //$vouchers[$i]->locations = json_decode(findByCondition(array('swap_id'=>$vouchers[$i]->id),'swap_location_map'));
            $vouchers[$i]->locations = array();
            $locations = findByConditionArray(array('swap_id' => $sid), 'swap_location_map');
            foreach ($locations as $cat) {
                $vouchers[$i]->locations[] = findByIdArray($cat['location_id'], 'locations');
            }
            $vouchers[$i]->categories = array();
            $category = findByConditionArray(array('swap_id' => $sid), 'swap_category_map');
            foreach ($category as $cat) {
                $vouchers[$i]->categories[] = findByIdArray($cat['category_id'], 'category');
            }
        }
        $db = null;
        $vouchers = json_encode($vouchers);
        $result = '{"type":"success","data":' . $vouchers . ',"count":' . $count . '}';
    } catch (PDOException $e) {
        $result = '{"type":"error","message":' . $e->getMessage() . '}';
    }
    echo $result;
}
function addToCart()
{
    //echo strtotime('+'.VALIDITYFORCART);
    //exit;
    $cart = array();
    $request = Slim::getInstance()->request();
    $body = json_decode($request->getBody());
    $user_id = $body->user_id;
    $point_id_array = array();
    if (isset($body->condition)) {
        unset($body->condition);
    }
    //print_r($body->cart);
    //exit;
    if (!empty($body->cart)) {
        foreach ($body->cart as $offer) {
            if ($offer->event == 1) {
                $temp = array();
                $temp['user_id'] = $user_id;
                $temp['offer_id'] = $offer->offer_id;
                $temp['quantity'] = 1;
                $temp['price'] = 1;
                $temp['point'] = 0;
                $temp['resell'] = 0;
                $temp['resell_id'] = 0;
                $temp['resell_price'] = 0;
                $temp['resell_mpoint'] = 0;
                $temp['event'] = $offer->event;
                $temp['event_id'] = $offer->event_id;
                $temp['event_price'] = $offer->event_price;
                $temp['event_bid_id'] = $offer->event_bid_id;
                $temp['validity'] = strtotime('+' . VALIDITYFORCART);
                $temp['typemem'] = '';
                add(json_encode(array('save_data' => $temp)), 'cart');
            } else {
                $query = "SELECT * from cart where user_id={$user_id} and offer_id=" . $offer->offer_id;
                $ifexist = findByQuery($query);
                if (empty($ifexist)) {
                    $offerrr = findByIdArray($offer->offer_id, 'offers');
                    if ($offerrr['quantity'] - $offerrr['buy_count'] > 0) {
                        $temp = array();
                        if ($offer->paymentscash == 1) {
                            $offer->paymentscash = 1;
                        } else {
                            $offer->paymentscash = 0;
                        }
                        if ($offer->payments == 1) {
                            $offer->payments = 1;
                        } else {
                            $offer->payments = 0;
                        }
                        $temp['user_id'] = $user_id;
                        $temp['offer_id'] = $offer->offer_id;
                        $temp['quantity'] = $offer->quantity;
                        $temp['price'] = $offer->paymentscash;
                        $temp['point'] = $offer->payments;
                        $temp['resell'] = $offer->resell;
                        $temp['resell_id'] = $offer->resell_id;
                        $temp['resell_price'] = $offer->price;
                        $temp['resell_mpoint'] = $offer->mpoints;
                        $temp['validity'] = strtotime('+' . VALIDITYFORCART);
                        $temp['typemem'] = $offer->typemem;
                        add(json_encode(array('save_data' => $temp)), 'cart');
                    }
                }
            }
        }
    }
    $all_cart = findByConditionArray(array('user_id' => $user_id), 'cart');
    if (!empty($all_cart)) {
        //print_r($all_cart);
        foreach ($all_cart as $prod) {
            if ($prod['event'] == 1) {
                $event = findByIdArray($prod['event_id'], 'events');
                $temp_cart = array();
                $temp_cart['offer_id'] = 0;
                $temp_cart['offer_percent'] = 0;
                $temp_cart['price'] = $prod['event_price'];
                $temp_cart['mpoints'] = 0;
                $temp_cart['offer_price'] = $prod['event_price'];
                $temp_cart['resell'] = 0;
                $temp_cart['resell_id'] = 0;
                $temp_cart['quantity'] = 1;
                $temp_cart['image'] = SITEURL . 'event_images/' . $event['image'];
                $temp_cart['restaurant_id'] = 0;
                $temp_cart['restaurant_title'] = 'Event';
                $temp_cart['point_id'] = 0;
                $temp_cart['point_name'] = '';
                $temp_cart['condtn'] = 0;
                $temp_cart['payments'] = false;
                $temp_cart['paymentscash'] = true;
                $temp_cart['event'] = 1;
                $temp_cart['event_id'] = $prod['event_id'];
                $temp_cart['event_price'] = $prod['event_price'];
                $temp_cart['event_bid_id'] = $prod['event_bid_id'];
                $temp_cart['offer_title'] = $event['title'];
                $temp_cart['typemem'] = $prod['typemem'];
                $cart[] = $temp_cart;
            } else {
                $point_name = '';
                $point_id = '';
                $point_needed = 0;
                $temp_cart = array();
                $offer = findByIdArray($prod['offer_id'], 'offers');
                $point_id = $offer['point_master_id'];
                $point_needed = $offer['mpoints'];
                if (!empty($offer['point_master_id'])) {
                    $point_details = findByIdArray($offer['point_master_id'], 'point_master');
                    if (!empty($point_details)) {
                        $point_name = $point_details['name'];
                    }
                }
                if (!empty($offer) && ($offer['buy_count'] < $offer['quantity'] || $prod['resell'] == 1)) {
                    $temp_cart['offer_id'] = $offer['id'];
                    $temp_cart['offer_percent'] = $offer['offer_percent'];
                    $temp_cart['offer_title'] = $offer['title'];
                    if ($prod['resell'] == 1) {
                        $temp_cart['price'] = $prod['resell_price'];
                        $temp_cart['mpoints'] = $prod['resell_mpoint'];
                        $temp_cart['offer_price'] = $prod['resell_price'];
                    } else {
                        $temp_cart['price'] = $offer['price'];
                        $temp_cart['mpoints'] = $offer['mpoints'];
                        $temp_cart['offer_price'] = $offer['offer_price'];
                    }
                    $temp_cart['resell'] = $prod['resell'];
                    $temp_cart['resell_id'] = $prod['resell_id'];
                    if ($prod['quantity'] < $offer['quantity'] - $offer['buy_count'] || $prod['resell'] == 1) {
                        $temp_cart['quantity'] = $prod['quantity'];
                    } else {
                        $temp_cart['quantity'] = $offer['quantity'] - $offer['buy_count'];
                    }
                    $temp_cart['image'] = SITEURL . 'voucher_images/' . $offer['image'];
                    $temp_cart['restaurant_id'] = $offer['restaurant_id'];
                    $restaurant = findByIdArray($offer['restaurant_id'], 'restaurants');
                    $temp_cart['restaurant_title'] = $restaurant['title'];
                    $temp_cart['point_id'] = $offer['point_master_id'];
                    $temp_cart['point_name'] = $point_name;
                    $temp_cart['condtn'] = $offer['conditions'];
                    $temp_cart['event'] = 0;
                    $temp_cart['event_id'] = 0;
                    $temp_cart['event_price'] = 0;
                    $temp_cart['event_bid_id'] = 0;
                    $temp_cart['typemem'] = $prod['typemem'];
                    if ($offer['conditions'] == 1) {
                        $temp_cart['payments'] = true;
                        $temp_cart['paymentscash'] = true;
                    } else {
                        if (!empty($point_id)) {
                            $point_sql = "select * from point_details where user_id='" . $user_id . "' and point_id='" . $point_id . "'";
                            $point_res = findByQuery($point_sql);
                            $available_point = 0;
                            $total_point = 0;
                            $redeem_point = 0;
                            foreach ($point_res as $point_detail_val) {
                                $total_point = $total_point + $point_detail_val['points'];
                                $redeem_point = $redeem_point + $point_detail_val['redeemed_points'];
                            }
                            $available_point = $total_point - $redeem_point;
                            //echo $available_point;
                            if ($available_point >= $point_needed) {
                                if (!empty($point_id_array)) {
                                    $temp_cart['payments'] = true;
                                    $temp_cart['paymentscash'] = false;
                                    $point_id_array[$point_id] = $available_point - $point_needed;
                                } else {
                                    if (array_key_exists($point_id, $point_id_array)) {
                                        if ($point_id_array[$point_id] >= $point_needed) {
                                            $temp_cart['payments'] = true;
                                            $temp_cart['paymentscash'] = false;
                                            $point_id_array[$point_id] = $point_id_array[$point_id] - $point_needed;
                                        } else {
                                            $temp_cart['payments'] = false;
                                            $temp_cart['paymentscash'] = true;
                                        }
                                    } else {
                                        $temp_cart['payments'] = true;
                                        $temp_cart['paymentscash'] = false;
                                        $point_id_array[$point_id] = $available_point - $point_needed;
                                    }
                                }
                            } else {
                                $temp_cart['payments'] = false;
                                $temp_cart['paymentscash'] = true;
                            }
                        } else {
                            $temp_cart['payments'] = false;
                            $temp_cart['paymentscash'] = true;
                        }
                    }
                    if ($offer['mpoints'] == 0) {
                        $temp_cart['paymentscash'] = true;
                        $temp_cart['payments'] = false;
                    }
                    $pc = $temp_cart['paymentscash'] ? 1 : 0;
                    $p = $temp_cart['payments'] ? 1 : 0;
                    $query = "UPDATE cart set price=" . $pc . ", point=" . $p . " where user_id=" . $user_id . " and offer_id=" . $offer['id'];
                    updateByQuery($query);
                    $temp_quantity = $temp_cart['quantity'];
                    $offer_id = $temp_cart['offer_id'];
                    $up_query = "UPDATE offers SET buy_count=buy_count+" . $temp_quantity . " where id=" . $offer_id;
                    updateByQuery($up_query);
                    if ($temp_cart['quantity'] >= 1) {
                        $cart[] = $temp_cart;
                    }
                } else {
                    //delete('cart',$prod['id']);
                }
            }
        }
    }
    echo json_encode($cart);
}
function getRestaurantDetails($id)
{
    $rarray = array();
    $restaurant = findByIdArray($id, 'restaurants');
    if (!empty($restaurant)) {
        $rarray['restaurant'] = $restaurant;
        if (!empty($restaurant['user_id'])) {
            $rarray['user'] = findByIdArray($restaurant['user_id'], 'users');
            $rarray['categories'] = findByConditionArray(array('restaurant_id' => $id), 'resturant_category_map');
        }
        if (!empty($restaurant['logo'])) {
            $rarray['restaurant']['logo_url'] = SITEURL . "restaurant_images/" . $restaurant['logo'];
        }
        $result = json_encode(array('type' => "success", 'data' => $rarray));
    } else {
        $result = '{type:"error",message:"Not Added"}';
    }
    echo $result;
    exit;
}
function getMerchantsRelatedEvents($id)
{
    $rarray = array();
    $events = array();
    $loc_events = array();
    $cat_events = array();
    if ($id) {
        $merchant_id = $id;
        $merchant_details = findByIdArray($merchant_id, 'users');
        //print_r($merchant_details);
        $outlet_categories = array();
        if (!empty($merchant_details)) {
            $merchant_outlets = findByConditionArray(array('user_id' => $merchant_id), 'outlets');
            if (!empty($merchant_outlets)) {
                $outlet_ids = array_column($merchant_outlets, 'id');
                $outlet_locations = array_column($merchant_outlets, 'location_id');
                foreach ($outlet_ids as $oid) {
                    $categories = findByConditionArray(array('outlet_id' => $oid), 'outlet_category_map');
                    foreach ($categories as $cat) {
                        array_push($outlet_categories, $cat['category_id']);
                    }
                    // $outlet_categories.push
                }
                //$categories = findByConditionArray(array('outlet_id' => 'IN (1,2)'),'outlet_category_map');
                //$outlet_categories = array_unique(array_column($categories, 'category_id'));
                //$db = getConnection();
                //$in  = str_repeat('?,', count($outlet_locations) - 1) . '?';
                $sql = "SELECT * FROM event_location_map WHERE location_id in(" . implode(',', $outlet_locations) . ")";
                //$stm = $db->prepare($sql);
                //$stm->execute($outlet_locations);
                $data = findByQuery($sql);
                if (!empty($data)) {
                    $events = array_column($data, 'event_id');
                }
                //$in  = str_repeat('?,', count($outlet_categories) - 1) . '?';
                $sql = "SELECT * FROM event_category_map WHERE category_id in(" . implode(',', $outlet_categories) . ")";
                //$stm = $db->prepare($sql);
                //$stm->execute($outlet_categories);
                $cat_data = findByQuery($sql);
                //print_r($events);
                if (!empty($cat_data)) {
                    $cat_events = array_column($cat_data, 'event_id');
                    if (!empty($cat_events)) {
                        foreach ($cat_events as $ta) {
                            $events[] = $ta;
                        }
                    }
                }
                //print_r($loc_events);
                //print_r($cat_events);
                //array_push($events, $loc_events, $cat_events);
                $events = array_values(array_unique($events));
                //print_r($events);
                if (!empty($events)) {
                    $tdt = date('Y-m-d H:i:s');
                    //$event_in  = str_repeat('?,', count($events) - 1) . '?';
                    $sql = "SELECT * FROM events WHERE id in(" . implode(',', $events) . ") and offer_to_date > '" . $tdt . "' and status Not in('C','E')";
                    //$db = getConnection();
                    //$stmt = $db->prepare($sql);
                    //$stmt->bindParam("user_id", $user_id);
                    // $stmt->execute($events);
                    $points = findByQuery($sql);
                    //print_r($points);
                    //exit;
                    $count = count($points);
                    for ($i = 0; $i < $count; $i++) {
                        $eveBid = findByConditionArray(array('event_id' => $points[$i]['id'], 'user_id' => $merchant_id), 'event_bids');
                        $today = date('Y-m-d');
                        $checkdate = date('Y-m-d', strtotime($points[$i]['offer_to_date']));
                        $sql = "SELECT Max(price) as price FROM event_bids WHERE event_id = " . $points[$i]['id'] . "";
                        $maxp = findByQuery($sql);
                        //echo '<pre>';print_r($maxp);
                        $points[$i]['bid_price'] = number_format($maxp[0]['price'], 2, '.', ',');
                        //echo 'Event_id:'.$points[$i]['id'].'| UserId:'.$merchant_id.'<br>';
                        //echo '<pre>';print_r($eveBid);
                        if (!empty($eveBid)) {
                            $points[$i]['is_bid'] = true;
                        } else {
                            $points[$i]['is_bid'] = false;
                        }
                        $created_on = date('m/d/Y', strtotime($points[$i]['created_on']));
                        $points[$i]['created_on'] = $created_on;
                        $from_date = date('m/d/Y', strtotime($points[$i]['offer_from_date']));
                        $points[$i]['offer_from_date'] = $from_date;
                        $to_date = date('m/d/Y', strtotime($points[$i]['offer_to_date']));
                        $points[$i]['offer_to_date'] = $to_date;
                        $event_date = date('m/d/Y', strtotime($points[$i]['from_date']));
                        $points[$i]['event_date'] = $event_date;
                        $event_start_time = date('h:i A', strtotime($points[$i]['from_date']));
                        $points[$i]['event_start_time'] = $event_start_time;
                        $event_end_time = date('h:i A', strtotime($points[$i]['to_date']));
                        $points[$i]['event_end_time'] = $event_end_time;
                        if (!empty($points[$i]['image'])) {
                            $points[$i]['image_url'] = SITEURL . 'event_images/' . $points[$i]['image'];
                        }
                        if ($checkdate < $today) {
                            $points[$i]['status'] = 'Expired';
                        } else {
                            if ($points[$i]['status'] == 'O') {
                                if ($points[$i]['is_bid']) {
                                    $points[$i]['status'] = 'Bid';
                                } else {
                                    $points[$i]['status'] = 'Open';
                                }
                            } else {
                                if ($points[$i]['status'] == 'E') {
                                    $points[$i]['status'] = 'Expired';
                                } else {
                                    $points[$i]['status'] = 'Completed';
                                }
                            }
                        }
                        $points[$i]['categories'] = json_decode(findByCondition(array('event_id' => $points[$i]['id']), 'event_category_map'));
                        $points[$i]['locations'] = json_decode(findByCondition(array('event_id' => $points[$i]['id']), 'event_location_map'));
                    }
                    /*if(!empty($points))
                      {
                          $points = array_map(function($t,$k){
                              $t->sl = $k+1;
                              //$t->type = ($t->type=='C'?'Credit':'Debit');
                                              $t->date = date('m/d/Y',  strtotime($t->date));
                              $t->expire_date = date('m/d/Y',  strtotime($t->expire_date));
                              return $t;
                          }, $points,  array_keys($points));
                      }*/
                    $rarray = array('status' => 'success', 'data' => $points);
                    //}
                } else {
                    $rarray = array('type' => 'error', 'message' => 'No events found.');
                }
            } else {
                $rarray = array('type' => 'error', 'message' => 'No outlet found');
            }
        } else {
            $rarray = array('type' => 'error', 'message' => 'Merchant not found.');
        }
    }
    echo json_encode($rarray);
}
function getMerResDetail($mid, $rid, $uid, $vid)
{
    $restaurant_details = findByIdArray($rid, 'restaurants');
    $merchant_details = findByIdArray($mid, 'users');
    $restaurant_id = $restaurant_details['restaurant_id'];
    $merchant_id = $merchant_details['merchant_id'];
    //echo "SELECT * FROM `member_user_map` WHERE merchant_id='".$merchant_id."' and user_id=$uid and  voucher_id=$vid";
    $member_query = "SELECT * FROM `member_user_map` WHERE merchant_id='" . $merchant_id . "' and user_id={$uid} and  voucher_id={$vid}";
    $memberIdDetails = findByQuery($member_query);
    //print_r($memberIdDetails);
    if (!empty($memberIdDetails)) {
        if (!empty($memberIdDetails[0]['member_id'])) {
            $member_membership_id = $memberIdDetails[0]['member_id'];
            $member_membership_end_date = date('d M,Y', strtotime($memberIdDetails[0]['membership_end_date']));
            $member_membership_name = $memberIdDetails[0]['name'];
        } else {
            $members = "SELECT * FROM `users` WHERE id={$uid}";
            $memberDetails = findByQuery($members);
            $member_membership_id = '';
            $member_membership_end_date = '';
            $member_membership_name = $memberDetails[0]['first_name'] . ' ' . $memberDetails[0]['last_name'];
        }
    } else {
        $members = "SELECT * FROM `users` WHERE id={$uid}";
        $memberDetails = findByQuery($members);
        $member_membership_id = '';
        $member_membership_end_date = '';
        $member_membership_name = $memberDetails[0]['first_name'] . ' ' . $memberDetails[0]['last_name'];
    }
    $result = array('type' => 'success', 'restaurant_id' => $restaurant_id, 'merchant_id' => $merchant_id, 'member_id' => $member_membership_id, 'expire_date' => $member_membership_end_date, 'memname' => $member_membership_name);
    echo json_encode($result);
}
function getRedeemVoucher($user_id)
{
    $rarray = array();
    $conditions = array();
    $site_path = SITEURL;
    $allPromo = findByConditionArray(array('merchant_id' => $user_id), 'offers');
    $all_promo_ids = array_column($allPromo, 'id');
    //print_r($all_promo_ids);
    $reddeem_voucher_sql = "select merchantoutlets.title,merchantoutlets.restaurant_id,vouchers.id,vouchers.offer_id,vouchers.price,vouchers.offer_price,vouchers.to_date,vouchers.RedeemDate,vouchers.Status from vouchers,merchantoutlets where vouchers.redeem_outlet = merchantoutlets.outlet_id  and vouchers.Status='redeem' and vouchers.offer_id in(" . implode(',', $all_promo_ids) . ")";
    $reddeem_voucher_res = findByQuery($reddeem_voucher_sql);
    if (!empty($reddeem_voucher_res)) {
        foreach ($reddeem_voucher_res as $reddeem_voucher_res_key => $reddeem_voucher_res_val) {
            $reddeem_voucher_res[$reddeem_voucher_res_key]['price'] = number_format($reddeem_voucher_res[$reddeem_voucher_res_key]['price'], 0, '.', ',');
            $reddeem_voucher_res[$reddeem_voucher_res_key]['offer_price'] = number_format($reddeem_voucher_res[$reddeem_voucher_res_key]['offer_price'], 0, '.', ',');
            $reddeem_voucher_res[$reddeem_voucher_res_key]['to_date'] = date('m-d-Y', strtotime($reddeem_voucher_res[$reddeem_voucher_res_key]['to_date']));
            $reddeem_voucher_res[$reddeem_voucher_res_key]['RedeemDate'] = date('m-d-Y', strtotime($reddeem_voucher_res[$reddeem_voucher_res_key]['RedeemDate']));
            $offerdetail = findByIdArray($reddeem_voucher_res[$reddeem_voucher_res_key]['offer_id'], 'offers');
            $reddeem_voucher_res[$reddeem_voucher_res_key]['offer_title'] = $offerdetail['title'];
            $reddeem_voucher_res[$reddeem_voucher_res_key]['voucher_number'] = 'MFG-000000000' . $offerdetail['id'];
            $restaurant_detail = findByIdArray($reddeem_voucher_res[$reddeem_voucher_res_key]['restaurant_id'], 'merchantrestaurants');
            $reddeem_voucher_res[$reddeem_voucher_res_key]['restaurant_title'] = $offerdetail['title'];
        }
        $rarray = array('type' => 'success', 'data' => $reddeem_voucher_res);
    } else {
        $rarray = array('type' => 'success', 'data' => $reddeem_voucher_res);
    }
    echo json_encode($rarray);
}
function getBannerDetails($id)
{
    $rarray = array();
    $restaurant = findByIdArray($id, 'banners');
    if (!empty($restaurant)) {
        $rarray['restaurant'] = $restaurant;
        if (!empty($restaurant['image'])) {
            $rarray['restaurant']['image_url'] = SITEURL . "banner_images/" . $restaurant['image'];
        }
        $result = json_encode(array('type' => "success", 'data' => $rarray));
    } else {
        $result = '{type:"error",message:"Not Added"}';
    }
    echo $result;
    exit;
}
function getBannerDetails($id)
{
    $rarray = array();
    $restaurant = findByIdArray($id, 'banners');
    if (!empty($restaurant)) {
        $rarray['banner'] = $restaurant;
        $rest = findByIdArray($restaurant['restaurant_id'], 'restaurants');
        $merchant = findByIdArray($restaurant['merchant_id'], 'users');
        $rarray['restaurant'] = $rest;
        $rarray['merchant'] = $merchant;
        if (!empty($restaurant['image'])) {
            $rarray['banner']['image_url'] = SITEURL . "banner_images/" . $restaurant['image'];
        }
        $outlets = findByConditionArray(array('banner_id' => $id), 'banners_outlet_map');
        $result = json_encode(array('type' => "success", 'data' => $rarray, 'outlets' => $outlets));
    } else {
        $result = '{type:"error",message:"Not Added"}';
    }
    echo $result;
    exit;
}
function getPurchasedMerchantMembershipPromo($user_id)
{
    $rarray = array();
    $promo_query = "SELECT offers.id,offers.visible_id,vouchers.created_on,vouchers.id as voucher_id,voucher_owner.to_user_id FROM `offers`,`vouchers`,`voucher_owner` WHERE vouchers.offer_id = offers.id and voucher_owner.voucher_id = vouchers.id and voucher_owner.is_active=1 and offers.merchant_id={$user_id} and offers.offer_type_id=3";
    $details = findByQuery($promo_query);
    //echo '<pre>';
    //print_r($details);
    //exit;
    $data = array();
    if (!empty($details)) {
        foreach ($details as $key => $details_val) {
            $member_id = '';
            $offer_id = '';
            $purchased_date = '';
            $expire_date = '';
            $promo_id = '';
            $restaurant_id = '';
            $member_email = '';
            $member_phone = '';
            $member_address = '';
            $member_name = '';
            $merchant_id = '';
            $sub_data = array();
            $member_id = $details_val['to_user_id'];
            $offer_id = $details_val['id'];
            $voucher_id = $details_val['voucher_id'];
            $purchased_date_format = $details_val['created_on'];
            $purchased_date = date('d M, Y', strtotime($details_val['created_on']));
            $promo_id = $details_val['visible_id'];
            $member_details = findByIdArray($member_id, 'users');
            $member_email = $member_details['email'];
            if (!empty($member_details['phone'])) {
                $member_phone = $member_details['phone'];
            }
            if (!empty($member_details['address'])) {
                $member_address = $member_details['address'];
            }
            $member_name = $member_details['first_name'] . ' ' . $member_details['last_name'];
            $merchant_details = findByIdArray($user_id, 'users');
            $merchant_id = $merchant_details['merchant_id'];
            $restaurants = findByConditionArray(array('offer_id' => $offer_id), 'offer_restaurent_map');
            //$memberIdDetails = findByConditionArray(array('offer_id' => $offer_id,'voucher_id' => $voucher_id,'user_id' => $member_id),'member_user_map');
            $member_query = "SELECT * FROM `member_user_map` WHERE offer_id = {$offer_id} and voucher_id = {$voucher_id} and user_id={$member_id}";
            $memberIdDetails = findByQuery($member_query);
            //echo $offer_id . ' '.$voucher_id.' '.$member_id;
            //echo '<pre>';
            //print_r($memberIdDetails);
            if (!empty($memberIdDetails)) {
                if (!empty($memberIdDetails[0]['member_id'])) {
                    $member_membership_id = $memberIdDetails[0]['member_id'];
                    $purchased_date = date('d M, Y', strtotime($memberIdDetails[0]['membership_start_date']));
                    $expire_date = date('d M, Y', strtotime($memberIdDetails[0]['membership_end_date']));
                } else {
                    $member_membership_id = '';
                }
                $member_name = $memberIdDetails[0]['name'];
            } else {
                $member_membership_id = '';
            }
            //$res_ids = array_column($restaurants, 'restaurent_id');
            //echo '<pre>';
            //print_r($restaurants);
            //print_r($res_ids);
            if (!empty($restaurants[0]['restaurent_id'])) {
                $restaurant_details = findByIdArray($restaurants[0]['restaurent_id'], 'restaurants');
                $restaurant_id = $restaurant_details['restaurant_id'];
            } else {
                $restaurant_id = '';
            }
            $merchant_details = findByIdArray($user_id, 'users');
            $sub_data['member_id'] = $member_id;
            $sub_data['offer_id'] = $offer_id;
            $sub_data['voucher_id'] = $voucher_id;
            $sub_data['promo_id'] = $promo_id;
            $sub_data['member_email'] = $member_email;
            $sub_data['member_phone'] = $member_phone;
            $sub_data['member_address'] = $member_address;
            $sub_data['member_name'] = $member_name;
            $sub_data['merchant_id'] = $merchant_id;
            $sub_data['restaurant_id'] = $restaurant_id;
            $sub_data['purchased_date'] = $purchased_date;
            $sub_data['expiry_date'] = $expire_date;
            $sub_data['purchased_date_format'] = $purchased_date_format;
            $sub_data['member_membership_id'] = $member_membership_id;
            $data[] = $sub_data;
        }
        $rarray = array("type" => "success", "data" => $data);
    } else {
        $rarray = array("type" => "error", "message" => "Sorry no promo found", "data" => array());
    }
    echo json_encode($rarray);
    exit;
}
function getActivePointMasterByMerchant($user_id)
{
    $rarray = array();
    $conditions = array();
    $restaurants = array();
    //$restaurants_query = "SELECT * FROM point_master WHERE "
    $restaurants = findByConditionArray(array('type' => 0), 'point_master');
    if (!empty($restaurants)) {
        $restaurants = array_map(function ($t) {
            $s = array();
            $user = findByIdArray($t['user_id'], 'users');
            $s['id'] = $t['id'];
            $s['user_id'] = $t['user_id'];
            $s['merchant_name'] = !empty($user['merchant_name']) ? $user['merchant_name'] : '';
            $s['name'] = $t['name'];
            $s['type'] = $t['type'];
            $s['value'] = $t['value'];
            $s['money_point'] = $t['money_point'];
            $s['valid_days'] = $t['valid_days'];
            $s['start_date'] = $t['start_date'];
            $s['expire_date'] = date('d M, Y', strtotime($t['expire_date']));
            //$t['restaurant'] = findByIdArray( $t['restaurant_id'],'restaurants');
            return $s;
        }, $restaurants);
        //$rarray = array('type' => 'success', 'data' => $restaurants);
    }
    //print_r($restaurants);
    if ($user_id != 1) {
        $merchant_point = findByConditionArray(array('user_id' => $user_id), 'point_master');
        if (!empty($merchant_point)) {
            foreach ($merchant_point as $t) {
                $s = array();
                $user = findByIdArray($t['user_id'], 'users');
                $s['id'] = $t['id'];
                $s['user_id'] = $t['user_id'];
                $s['merchant_name'] = !empty($user['merchant_name']) ? $user['merchant_name'] : '';
                $s['name'] = $t['name'];
                $s['type'] = $t['type'];
                $s['value'] = $t['value'];
                $s['money_point'] = $t['money_point'];
                $s['valid_days'] = $t['valid_days'];
                $s['start_date'] = $t['start_date'];
                $s['expire_date'] = date('d M, Y', strtotime($t['expire_date']));
                $restaurants[] = $s;
            }
            /*$restaurants = array_map(function($t){
                  $s = array();
                  $user = findByIdArray( $t['user_id'],'users');
                  $s['id'] = $t['id'];
                  $s['user_id'] = $t['user_id'];
                  $s['merchant_name'] = (!empty($user['merchant_name'])?$user['merchant_name']:''); 
                  $s['name'] = $t['name'];
                  $s['type'] = $t['type'];
                  $s['value'] = $t['value'];
                  $s['money_point'] = $t['money_point'];
                  $s['valid_days'] = $t['valid_days'];
                  $s['start_date'] = $t['start_date'];
                  $s['expire_date'] =  date('d M, Y',  strtotime($t['expire_date']));
                  //$t['restaurant'] = findByIdArray( $t['restaurant_id'],'restaurants'); 
                  return $s;
              }, $restaurants);*/
            //$rarray = array('type' => 'success', 'data' => $restaurants);
        }
    }
    if (!empty($restaurants)) {
        $rarray = array('type' => 'success', 'data' => $restaurants);
    } else {
        $rarray = array('type' => 'error', 'message' => 'No points found');
    }
    echo json_encode($rarray);
}
function transactionTabDetails()
{
    $rarray = array();
    $request = Slim::getInstance()->request();
    $body = json_decode($request->getBody());
    //print_r($body);
    //exit;
    $restaurant_id = array();
    $outlet_id = array();
    $offer_id = array();
    $total_res_ids = array();
    $total_outlet_ids = array();
    $offers_ids = array();
    $merchant_id = $body->merchant_id;
    $start_date = $body->start_date;
    $end_date = $body->end_date;
    if (isset($body->selRes)) {
        $restaurant_id = $body->selRes;
        //print_r($restaurant_id);
        foreach ($restaurant_id as $res_key => $res_value) {
            $res_ids = array();
            $res_sql = "select * from restaurants where restaurant_id='" . $res_value . "'";
            $res_sql_res = findByQuery($res_sql);
            $res_ids = array_column($res_sql_res, 'id');
            if (!empty($res_ids)) {
                $total_res_ids[] = $res_ids[0];
            }
        }
    }
    if (isset($body->selout)) {
        $outlet_id = $body->selout;
        foreach ($outlet_id as $outlet_key => $outlet_value) {
            $out_ids = array();
            $out_sql = "select * from outlets where outlet_id='" . $outlet_value . "'";
            $out_sql_res = findByQuery($out_sql);
            $out_ids = array_column($out_sql_res, 'id');
            if (!empty($out_ids)) {
                $total_outlet_ids[] = $out_ids[0];
            }
        }
    }
    $type = $body->type;
    if (!empty($total_res_ids)) {
        $sql = "SELECT * FROM offer_restaurent_map where offer_restaurent_map.restaurent_id in(" . implode(",", $total_res_ids) . ")";
        $res_offer_res = findByQuery($sql);
        $offers_ids = array_column($res_offer_res, 'offer_id');
        //print_r($offers_ids);
    }
    if (!empty($total_outlet_ids)) {
        if (!empty($offers_ids)) {
            $sql = "SELECT * FROM offer_outlet_map where offer_outlet_map.offer_id in(" . implode(",", $offers_ids) . ") and offer_outlet_map.outlet_id in(" . implode(",", $total_outlet_ids) . ")";
            $out_offer_res = findByQuery($sql);
            $offers_ids = array_column($out_offer_res, 'offer_id');
            //print_r($offers_ids);
        }
    }
    $allPaymentOffer = 0;
    $allMenuOffer = 0;
    $allMembershipOffer = 0;
    $allRedeemPaymentOffer = 0;
    $allRedeemMenuOffer = 0;
    $allRedeemMembershipOffer = 0;
    $allAvailablePaymentOffer = 0;
    $allAvailableMenuOffer = 0;
    $allAvailableMembershipOffer = 0;
    $offerPaymentOffer = 0;
    $offerMenuOffer = 0;
    $offerMembershipOffer = 0;
    $redeemPaymentOffer = 0;
    $redeemMenuOffer = 0;
    $redeemMembershipOffer = 0;
    $availablePaymentOffer = 0;
    $availableMenuOffer = 0;
    $availableMembershipOffer = 0;
    $willActivePaymentOffer = 0;
    $willActiveMenuOffer = 0;
    $willActiveMembershipOffer = 0;
    $newPaymentOffer = 0;
    $newMenuOffer = 0;
    $newMembershipOffer = 0;
    $hotPaymentOffer = 0;
    $hotMenuOffer = 0;
    $hotMembershipOffer = 0;
    $lastdayPaymentOffer = 0;
    $lastdayMenuOffer = 0;
    $lastdayMembershipOffer = 0;
    $specialPaymentOffer = 0;
    $specialMenuOffer = 0;
    $specialMembershipOffer = 0;
    $viewPaymentOffer = 0;
    $viewMenuOffer = 0;
    $viewMembershipOffer = 0;
    $willActiveNews = 0;
    $willActiveAddv = 0;
    $willActiveBanner = 0;
    $activeNews = 0;
    $activeAddv = 0;
    $activeBanner = 0;
    $targetNews = 0;
    $targetAddv = 0;
    $targetBanner = 0;
    $actualNews = 0;
    $actualAddv = 0;
    $actualBanner = 0;
    $remainingNews = 0;
    $remainingAddv = 0;
    $remainingBanner = 0;
    $exceedNews = 0;
    $exceedAddv = 0;
    $exceedBanner = 0;
    $exceedNews = 0;
    $exceedAddv = 0;
    $exceedBanner = 0;
    $availableEvent = 0;
    $dealEvent = 0;
    $openEvent = 0;
    $feedbackEvent = 0;
    $myEvent = 0;
    $myOfferEvent = 0;
    $generatedPoint = 0;
    $redeemPoint = 0;
    $expiredPoint = 0;
    $balancedPoint = 0;
    $nextMonthExpPoint = 0;
    $thisYearExpPoint = 0;
    //print_r($offers_ids);
    if (!empty($offers_ids)) {
        $payment_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=2";
        $menu_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=1";
        $membership_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=3";
        $all_payment_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and offer_type_id=2";
        $all_menu_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and offer_type_id=1";
        $all_membership_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and offer_type_id=3";
        $payment_promo_res = findByQuery($payment_promo_sql);
        $menu_promo_res = findByQuery($menu_promo_sql);
        $membership_promo_res = findByQuery($membership_promo_sql);
        $all_payment_promo_res = findByQuery($all_payment_promo_sql);
        $all_menu_promo_res = findByQuery($all_menu_promo_sql);
        $all_membership_promo_res = findByQuery($all_membership_promo_sql);
        //print_r($all_payment_promo_res);
        //print_r($all_menu_promo_res);
        //print_r($all_membership_promo_res);
        if (!empty($all_payment_promo_res)) {
            if ($type == 'unit') {
                $allPaymentOffer = count($all_payment_promo_res);
                $allRedeemPaymentOffer = 0;
                $allAvailablePaymentOffer = count($all_payment_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($all_payment_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $allPaymentOffer = $cntt;
                $allRedeemPaymentOffer = $cntr;
                $allAvailablePaymentOffer = $cnta;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($all_payment_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $allPaymentOffer = $cntt;
                $allRedeemPaymentOffer = $cntr;
                $allAvailablePaymentOffer = $cnta;
            }
        }
        if (!empty($all_menu_promo_res)) {
            if ($type == 'unit') {
                $allMenuOffer = count($all_menu_promo_res);
                $allRedeemMenuOffer = 0;
                $allAvailableMenuOffer = count($all_menu_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($all_menu_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $allMenuOffer = $cntt;
                $allRedeemMenuOffer = $cntr;
                $allAvailableMenuOffer = $cnta;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($all_menu_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $allMenuOffer = $cntt;
                $allRedeemMenuOffer = $cntr;
                $allAvailableMenuOffer = $cnta;
            }
        }
        if (!empty($all_membership_promo_res)) {
            if ($type == 'unit') {
                $allMembershipOffer = count($all_membership_promo_res);
                $allRedeemMembershipOffer = 0;
                $allAvailableMembershipOffer = count($all_membership_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($all_membership_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $allMembershipOffer = $cntt;
                $allRedeemMembershipOffer = $cntr;
                $allAvailableMembershipOffer = $cnta;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($all_membership_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $allMembershipOffer = $cntt;
                $allRedeemMembershipOffer = $cntr;
                $allAvailableMembershipOffer = $cnta;
            }
        }
        if (!empty($payment_promo_res)) {
            if ($type == 'unit') {
                $offerPaymentOffer = count($payment_promo_res);
                $redeemPaymentOffer = 0;
                $availablePaymentOffer = count($payment_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $offerPaymentOffer = $cntt;
                $redeemPaymentOffer = $cntr;
                $availablePaymentOffer = $cnta;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $offerPaymentOffer = $cntt;
                $redeemPaymentOffer = $cntr;
                $availablePaymentOffer = $cnta;
            }
        }
        if (!empty($menu_promo_res)) {
            if ($type == 'unit') {
                $offerMenuOffer = count($menu_promo_res);
                $redeemMenuOffer = 0;
                $availableMenuOffer = count($menu_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $offerMenuOffer = $cntt;
                $redeemMenuOffer = $cntr;
                $availableMenuOffer = $cnta;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $offerMenuOffer = $cntt;
                $redeemMenuOffer = $cntr;
                $availableMenuOffer = $cnta;
            }
        }
        if (!empty($membership_promo_res)) {
            if ($type == 'unit') {
                $offerMembershipOffer = count($membership_promo_res);
                $redeemMembershipOffer = 0;
                $availableMembershipOffer = count($membership_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $offerMembershipOffer = $cntt;
                $redeemMembershipOffer = $cntr;
                $availableMembershipOffer = $cnta;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $offerMembershipOffer = $cntt;
                $redeemMembershipOffer = $cntr;
                $availableMembershipOffer = $cnta;
            }
        }
        $payment_will_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date)>=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=2";
        $menu_will_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date)>=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=1";
        $membership_will_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date)>=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=3";
        $payment_new_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date)=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=2";
        $menu_new_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date)=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=1";
        $membership_new_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date)=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=3";
        $site_settings = findByIdArray('1', 'site_settings');
        if (!empty($site_settings['hot_percentage'])) {
            $perc = $site_settings['hot_percentage'];
        } else {
            $perc = 0;
        }
        $payment_hot_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=2 and DATE(offer_from_date) <=CURDATE() and buy_count < quantity and ((buy_count/quantity)*100)>={$perc}";
        $menu_hot_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=1 and DATE(offer_from_date) <=CURDATE() and buy_count < quantity and ((buy_count/quantity)*100)>={$perc}";
        $membership_hot_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=3 and DATE(offer_from_date) <=CURDATE() and buy_count < quantity and ((buy_count/quantity)*100)>={$perc}";
        $payment_last_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_to_date)=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=2";
        $menu_last_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_to_date)=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=1";
        $membership_last_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_to_date)=CURDATE() and id in(" . implode(',', $offers_ids) . ") and offer_type_id=3";
        $payment_special_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=2 and is_special=1";
        $menu_special_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=1 and is_special=1";
        $membership_special_promo_sql = "select * from offers where merchant_id='" . $merchant_id . "' and DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and id in(" . implode(',', $offers_ids) . ") and offer_type_id=3 and is_special=1";
        $payment_promo_view_sql = "select * from promo_visit where DATE(created_date) between '" . $start_date . "' AND '" . $end_date . "' and merchant_id='" . $merchant_id . "'";
        $menu_promo_view_sql = "select * from promo_visit where DATE(created_date) between '" . $start_date . "' AND '" . $end_date . "' and merchant_id='" . $merchant_id . "'";
        $membership_promo_view_sql = "select * from promo_visit where DATE(created_date) between '" . $start_date . "' AND '" . $end_date . "' and merchant_id='" . $merchant_id . "'";
        $payment_will_promo_res = findByQuery($payment_will_promo_sql);
        $menu_will_promo_res = findByQuery($menu_will_promo_sql);
        $membership_will_promo_res = findByQuery($membership_will_promo_sql);
        $payment_new_promo_res = findByQuery($payment_new_promo_sql);
        $menu_new_promo_res = findByQuery($menu_new_promo_sql);
        $membership_new_promo_res = findByQuery($membership_new_promo_sql);
        $payment_hot_promo_res = findByQuery($payment_hot_promo_sql);
        $menu_hot_promo_res = findByQuery($menu_hot_promo_sql);
        $membership_hot_promo_res = findByQuery($membership_hot_promo_sql);
        $payment_last_promo_res = findByQuery($payment_last_promo_sql);
        $menu_last_promo_res = findByQuery($menu_last_promo_sql);
        $membership_last_promo_res = findByQuery($membership_last_promo_sql);
        $payment_special_promo_res = findByQuery($payment_special_promo_sql);
        $menu_special_promo_res = findByQuery($menu_special_promo_sql);
        $membership_special_promo_res = findByQuery($membership_special_promo_sql);
        $payment_promo_view_res = findByQuery($payment_promo_view_sql);
        $menu_promo_view_res = findByQuery($menu_promo_view_sql);
        $membership_promo_view_res = findByQuery($membership_promo_view_sql);
        if (!empty($payment_will_promo_res)) {
            if ($type == 'unit') {
                $willActivePaymentOffer = count($payment_will_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_will_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $willActivePaymentOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_will_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $willActivePaymentOffer = $cntt;
            }
        }
        if (!empty($menu_will_promo_res)) {
            if ($type == 'unit') {
                $willActiveMenuOffer = count($menu_will_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_will_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $willActiveMenuOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_will_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $willActiveMenuOffer = $cntt;
            }
        }
        if (!empty($membership_will_promo_res)) {
            if ($type == 'unit') {
                $willActiveMembershipOffer = count($membership_will_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_will_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $willActiveMembershipOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_will_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $willActiveMembershipOffer = $cntt;
            }
        }
        if (!empty($payment_new_promo_res)) {
            if ($type == 'unit') {
                $newPaymentOffer = count($payment_new_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_new_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $newPaymentOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_new_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $newPaymentOffer = $cntt;
            }
        }
        if (!empty($menu_new_promo_res)) {
            if ($type == 'unit') {
                $newMenuOffer = count($menu_new_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_new_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $newMenuOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_new_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $newMenuOffer = $cntt;
            }
        }
        if (!empty($membership_new_promo_res)) {
            if ($type == 'unit') {
                $newMembershipOffer = count($membership_new_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_new_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $newMembershipOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_new_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $newMembershipOffer = $cntt;
            }
        }
        if (!empty($payment_hot_promo_res)) {
            if ($type == 'unit') {
                $hotPaymentOffer = count($payment_hot_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_hot_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $hotPaymentOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_hot_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $hotPaymentOffer = $cntt;
            }
        }
        if (!empty($menu_hot_promo_res)) {
            if ($type == 'unit') {
                $hotMenuOffer = count($menu_hot_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_hot_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $hotMenuOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_hot_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $hotMenuOffer = $cntt;
            }
        }
        if (!empty($membership_hot_promo_res)) {
            if ($type == 'unit') {
                $hotMembershipOffer = count($membership_hot_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_hot_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $hotMembershipOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_hot_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $hotMembershipOffer = $cntt;
            }
        }
        if (!empty($payment_last_promo_res)) {
            if ($type == 'unit') {
                $lastdayPaymentOffer = count($payment_last_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_last_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $lastdayPaymentOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_last_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $lastdayPaymentOffer = $cntt;
            }
        }
        if (!empty($menu_last_promo_res)) {
            if ($type == 'unit') {
                $lastdayMenuOffer = count($menu_last_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_last_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $lastdayMenuOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_last_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $lastdayMenuOffer = $cntt;
            }
        }
        if (!empty($membership_last_promo_res)) {
            if ($type == 'unit') {
                $lastdayMembershipOffer = count($membership_last_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_last_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $lastdayMembershipOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_last_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $lastdayMembershipOffer = $cntt;
            }
        }
        if (!empty($payment_special_promo_res)) {
            if ($type == 'unit') {
                $specialPaymentOffer = count($payment_special_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_special_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $specialPaymentOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($payment_special_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $specialPaymentOffer = $cntt;
            }
        }
        if (!empty($menu_special_promo_res)) {
            if ($type == 'unit') {
                $specialMenuOffer = count($menu_special_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_special_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $specialMenuOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($menu_special_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $specialMenuOffer = $cntt;
            }
        }
        if (!empty($membership_special_promo_res)) {
            if ($type == 'unit') {
                $specialMembershipOffer = count($membership_special_promo_res);
            } elseif ($type == 'quantity') {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_special_promo_res as $key => $value) {
                    $cntr = $cntr + $value['buy_count'];
                    $cntt = $cntt + $value['quantity'];
                    $available = 0;
                    $available = $value['quantity'] - $value['buy_count'];
                    $cnta = $cnta + $available;
                }
                $specialMembershipOffer = $cntt;
            } else {
                $cntr = 0;
                $cnta = 0;
                $cntt = 0;
                foreach ($membership_special_promo_res as $key => $value) {
                    $cntr = $cntr + $value['offer_price'] * $value['buy_count'];
                    $cntt = $cntt + $value['offer_price'] * $value['quantity'];
                    $available = 0;
                    $available = ($value['quantity'] - $value['buy_count']) * $value['offer_price'];
                    $cnta = $cnta + $available;
                }
                $specialMembershipOffer = $cntt;
            }
        }
        if (!empty($payment_promo_view_res)) {
            if ($type == 'unit') {
                $viewPaymentOffer = count($payment_promo_view_res);
            } elseif ($type == 'quantity') {
                $viewPaymentOffer = count($payment_promo_view_res);
            } else {
                $viewPaymentOffer = count($payment_promo_view_res);
            }
        }
        if (!empty($menu_promo_view_res)) {
            if ($type == 'unit') {
                $viewMenuOffer = count($menu_promo_view_res);
            } elseif ($type == 'quantity') {
                $viewMenuOffer = count($menu_promo_view_res);
            } else {
                $viewMenuOffer = count($menu_promo_view_res);
            }
        }
        if (!empty($membership_promo_view_res)) {
            if ($type == 'unit') {
                $viewMembershipOffer = count($membership_promo_view_res);
            } elseif ($type == 'quantity') {
                $viewMembershipOffer = count($membership_promo_view_res);
            } else {
                $viewMembershipOffer = count($membership_promo_view_res);
            }
        }
        if (!empty($total_res_ids)) {
            $will_active_add_sql = "select * from advertisements where merchant_id='" . $merchant_id . "' and restaurant_id in(" . implode(',', $total_res_ids) . ") and DATE(start_date)>CURDATE() and is_active=1";
            $active_add_sql = "select * from advertisements where merchant_id='" . $merchant_id . "' and restaurant_id in(" . implode(',', $total_res_ids) . ") and DATE(start_date) between '" . $start_date . "' AND '" . $end_date . "' and is_active=1";
            $will_active_banner_sql = "select * from banners where merchant_id='" . $merchant_id . "' and restaurant_id in(" . implode(',', $total_res_ids) . ") and DATE(start_date)>CURDATE() and is_active=1";
            $active_banner_sql = "select * from banners where merchant_id='" . $merchant_id . "' and restaurant_id in(" . implode(',', $total_res_ids) . ") and DATE(start_date) between '" . $start_date . "' AND '" . $end_date . "' and is_active=1";
            $will_active_news_sql = "select * from news where user_id='" . $merchant_id . "' and DATE(published_date)>CURDATE()";
            $active_news_sql = "select * from news where user_id='" . $merchant_id . "' and DATE(published_date) between '" . $start_date . "' AND '" . $end_date . "'";
            $will_active_add_res = findByQuery($will_active_add_sql);
            $active_add_res = findByQuery($active_add_sql);
            $will_active_banner_res = findByQuery($will_active_banner_sql);
            $active_banner_res = findByQuery($active_banner_sql);
            $will_active_news_res = findByQuery($will_active_news_sql);
            $active_news_res = findByQuery($active_news_sql);
            if (!empty($will_active_add_res)) {
                $willActiveAddv = count($will_active_add_res);
            }
            if (!empty($will_active_banner_res)) {
                $willActiveBanner = count($will_active_banner_res);
            }
            if (!empty($will_active_news_res)) {
                $willActiveNews = count($will_active_news_res);
            }
            if (!empty($active_news_res)) {
                $activeNews = count($active_news_res);
                $targetNews = count($active_news_res);
                $actualNews = count($active_news_res);
                $remainingNews = count($active_news_res);
                $exceedNews = count($active_news_res);
            }
            if (!empty($active_add_res)) {
                $activeAddv = count($active_add_res);
                $cntt = 0;
                $cnta = 0;
                $cntr = 0;
                $cnte = 0;
                foreach ($active_add_res as $key => $value) {
                    $cntt = $cntt + $value['target_click'];
                    $cnta = $cnta + $value['number_of_click'];
                    if ($value['target_click'] > $value['number_of_click']) {
                        $cntr = $cntr + ($value['target_click'] - $value['number_of_click']);
                    } else {
                        $cnte = $cnte + ($value['number_of_click'] - $value['target_click']);
                    }
                }
                $targetAddv = $cntt;
                $actualAddv = $cnta;
                $remainingAddv = $cntr;
                $exceedAddv = $cnte;
            }
            if (!empty($active_banner_res)) {
                $activeBanner = count($active_banner_res);
                $cntt = 0;
                $cnta = 0;
                $cntr = 0;
                $cnte = 0;
                foreach ($active_banner_res as $key => $value) {
                    $cntt = $cntt + $value['target_click'];
                    $cnta = $cnta + $value['number_of_click'];
                    if ($value['target_click'] > $value['number_of_click']) {
                        $cntr = $cntr + ($value['target_click'] - $value['number_of_click']);
                    } else {
                        $cnte = $cnte + ($value['number_of_click'] - $value['target_click']);
                    }
                }
                $targetBanner = $cntt;
                $actualBanner = $cnta;
                $remainingBanner = $cntr;
                $exceedBanner = $cnte;
            }
        }
    }
    $available_event_sql = "select * from events where status='O' and DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and is_active=1";
    $available_event_res = findByQuery($available_event_sql);
    $events_ids = array_column($available_event_res, 'id');
    $availableEvent = count($available_event_res);
    //print_r($events_ids);
    //print_r($available_event_res);
    $deal_event_sql = "select * from events where status='C' and DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and is_active=1";
    $deal_event_res = findByQuery($deal_event_sql);
    $dealEvent = count($deal_event_res);
    //print_r($deal_event_res);
    if (!empty($events_ids)) {
        $open_event_sql = "select * from event_bids where event_id NOT in(" . implode(',', $events_ids) . ") and is_accepted = 0";
        $open_event_res = findByQuery($open_event_sql);
        $openEvent = count($open_event_res);
        //print_r($open_event_res);
        $feedback_event_sql = "select * from event_bids where event_id in(" . implode(',', $events_ids) . ") and is_accepted = 0 and user_id != '" . $merchant_id . "'";
        $feedback_event_res = findByQuery($feedback_event_sql);
        $feedbackEvent = count($feedback_event_res);
    }
    $events = array();
    $merchant_category = findByConditionArray(array('user_id' => $merchant_id), 'merchant_category_map');
    $category_ids = array_column($merchant_category, 'category_id');
    $merchant_location = findByConditionArray(array('user_id' => $merchant_id), 'merchant_location_map');
    $location_ids = array_column($merchant_location, 'location_id');
    $sql = "SELECT * FROM event_location_map WHERE location_id in(" . implode(',', $location_ids) . ")";
    $data = findByQuery($sql);
    if (!empty($data)) {
        $events = array_column($data, 'event_id');
    }
    $sql = "SELECT * FROM event_category_map WHERE category_id in(" . implode(',', $category_ids) . ")";
    $cat_data = findByQuery($sql);
    if (!empty($cat_data)) {
        $cat_events = array_column($cat_data, 'event_id');
        if (!empty($cat_events)) {
            foreach ($cat_events as $ta) {
                $events[] = $ta;
            }
        }
    }
    $events = array_values(array_unique($events));
    if (!empty($events)) {
        $my_event_sql = "select * from events where DATE(offer_from_date) between '" . $start_date . "' AND '" . $end_date . "' and is_active=1 and id in(" . implode(',', $events) . ")";
        $my_event_res = findByQuery($my_event_sql);
        $myeventsid = array_column($my_event_res, 'id');
        $myEvent = count($my_event_res);
        if (!empty($myeventsid)) {
            $offer_event_sql = "select * from event_bids where event_id in(" . implode(',', $myeventsid) . ") and user_id = '" . $merchant_id . "'";
            $offer_event_res = findByQuery($offer_event_sql);
            $myOfferEvent = count($offer_event_res);
        }
    }
    $cur_month = date('m');
    $next_month = $cur_month + 1;
    $cur_year = date('Y');
    $point_master_sql = "select * from point_master where user_id='" . $merchant_id . "' and DATE(start_date) between '" . $start_date . "' AND '" . $end_date . "'";
    $exp_master_sql = "select * from point_master where user_id='" . $merchant_id . "' and DATE(expire_date) < CURDATE()";
    $next_month_exp_master_sql = "select * from point_master where user_id='" . $merchant_id . "' and MONTH(expire_date)= '" . $next_month . "'";
    $cur_year_exp_master_sql = "select * from point_master where user_id='" . $merchant_id . "' and YEAR(expire_date)= '" . $cur_year . "'";
    $point_master_res = findByQuery($point_master_sql);
    $exp_master_res = findByQuery($exp_master_sql);
    $next_month_exp_master_res = findByQuery($next_month_exp_master_sql);
    $cur_year_exp_master_res = findByQuery($cur_year_exp_master_sql);
    $pointsid = array_column($point_master_res, 'id');
    $exppointsid = array_column($exp_master_res, 'id');
    $nextmonthpointsid = array_column($next_month_exp_master_res, 'id');
    $curyearpointsid = array_column($cur_year_exp_master_res, 'id');
    //print_r($pointsid);
    //print_r($exppointsid);
    //print_r($nextmonthpointsid);
    //print_r($curyearpointsid);
    if (!empty($pointsid)) {
        $generated_mpoint_sql = "select sum(points) as generated,sum(redeemed_points) as redeem,sum(remaining_points) as balance from point_details where merchant_id='" . $merchant_id . "' and point_id in(" . implode(',', $pointsid) . ")";
        $generated_mpoint_res = findByQuery($generated_mpoint_sql);
        //print_r($generated_mpoint_res);
        if (!empty($generated_mpoint_res)) {
            $generatedPoint = $generated_mpoint_res[0]['generated'];
            $redeemPoint = $generated_mpoint_res[0]['redeem'];
            $balancedPoint = $generated_mpoint_res[0]['balance'];
            //echo $generatedPoint;
        }
    }
    if (!empty($exppointsid)) {
        $expired_mpoint_sql = "select sum(remaining_points) as balance from point_details where merchant_id='" . $merchant_id . "' and point_id in(" . implode(',', $exppointsid) . ")";
        $expired_mpoint_res = findByQuery($expired_mpoint_sql);
        //print_r($expired_mpoint_res);
        if (!empty($expired_mpoint_res)) {
            $expiredPoint = $expired_mpoint_res[0]['balance'];
            //echo $expiredPoint;
        }
    }
    if (!empty($nextmonthpointsid)) {
        $nextmonth_mpoint_sql = "select sum(remaining_points) as balance from point_details where merchant_id='" . $merchant_id . "' and point_id in(" . implode(',', $nextmonthpointsid) . ")";
        $nextmonth_mpoint_res = findByQuery($nextmonth_mpoint_sql);
        //print_r($nextmonth_mpoint_res);
        if (!empty($nextmonth_mpoint_res)) {
            $nextMonthExpPoint = $nextmonth_mpoint_res[0]['balance'];
            //echo $expiredPoint;
        }
    }
    if (!empty($curyearpointsid)) {
        $curyear_mpoint_sql = "select sum(remaining_points) as balance from point_details where merchant_id='" . $merchant_id . "' and point_id in(" . implode(',', $curyearpointsid) . ")";
        $curyear_mpoint_res = findByQuery($curyear_mpoint_sql);
        //print_r($curyear_mpoint_res);
        if (!empty($curyear_mpoint_res)) {
            $thisYearExpPoint = $curyear_mpoint_res[0]['balance'];
            //echo $expiredPoint;
        }
    }
    //exit;
    $data = array('allMenuOffer' => $allMenuOffer, 'allMembershipOffer' => $allMembershipOffer, 'allPaymentOffer' => $allPaymentOffer, 'allRedeemPaymentOffer' => $allRedeemPaymentOffer, 'allRedeemMenuOffer' => $allRedeemMenuOffer, 'allRedeemMembershipOffer' => $allRedeemMembershipOffer, 'allAvailablePaymentOffer' => $allAvailablePaymentOffer, 'allAvailableMenuOffer' => $allAvailableMenuOffer, 'allAvailableMembershipOffer' => $allAvailableMembershipOffer, 'offerPaymentOffer' => $offerPaymentOffer, 'offerMenuOffer' => $offerMenuOffer, 'offerMembershipOffer' => $offerMembershipOffer, 'redeemPaymentOffer' => $redeemPaymentOffer, 'redeemMenuOffer' => $redeemMenuOffer, 'redeemMembershipOffer' => $redeemMembershipOffer, 'availablePaymentOffer' => $availablePaymentOffer, 'availableMenuOffer' => $availableMenuOffer, 'availableMembershipOffer' => $availableMembershipOffer, 'willActivePaymentOffer' => $willActivePaymentOffer, 'willActiveMenuOffer' => $willActiveMenuOffer, 'willActiveMembershipOffer' => $willActiveMembershipOffer, 'newPaymentOffer' => $newPaymentOffer, 'newMenuOffer' => $newMenuOffer, 'newMembershipOffer' => $newMembershipOffer, 'newMembershipOffer' => $newMembershipOffer, 'hotPaymentOffer' => $hotPaymentOffer, 'hotMenuOffer' => $hotMenuOffer, 'hotMembershipOffer' => $hotMembershipOffer, 'lastdayPaymentOffer' => $lastdayPaymentOffer, 'lastdayMenuOffer' => $lastdayMenuOffer, 'lastdayMembershipOffer' => $lastdayMembershipOffer, 'specialPaymentOffer' => $specialPaymentOffer, 'specialMenuOffer' => $specialMenuOffer, 'specialMembershipOffer' => $specialMembershipOffer, 'viewPaymentOffer' => $viewPaymentOffer, 'viewMenuOffer' => $viewMenuOffer, 'viewMembershipOffer' => $viewMembershipOffer, 'willActiveNews' => $willActiveNews, 'willActiveAddv' => $willActiveAddv, 'willActiveBanner' => $willActiveBanner, 'activeNews' => $activeNews, 'activeAddv' => $activeAddv, 'activeBanner' => $activeBanner, 'targetNews' => $targetNews, 'targetAddv' => $targetAddv, 'targetBanner' => $targetBanner, 'actualNews' => $actualNews, 'actualAddv' => $actualAddv, 'actualBanner' => $actualBanner, 'remainingNews' => $remainingNews, 'remainingAddv' => $remainingAddv, 'remainingBanner' => $remainingBanner, 'exceedNews' => $exceedNews, 'exceedAddv' => $exceedAddv, 'exceedBanner' => $exceedBanner, 'availableEvent' => $availableEvent, 'dealEvent' => $dealEvent, 'openEvent' => $openEvent, 'feedbackEvent' => $feedbackEvent, 'myEvent' => $myEvent, 'myOfferEvent' => $myOfferEvent, 'generatedPoint' => $generatedPoint, 'redeemPoint' => $redeemPoint, 'expiredPoint' => $expiredPoint, 'balancedPoint' => $balancedPoint, 'nextMonthExpPoint' => $nextMonthExpPoint, 'thisYearExpPoint' => $thisYearExpPoint);
    $rarray = array('type' => 'success', 'data' => $data);
    echo json_encode($rarray);
}