function getMerchantsOutlet($id)
{
    $rarray = array();
    $conditions = array();
    $conditions['user_id'] = $id;
    $restaurants = findByConditionArray($conditions, 'merchantoutlets');
    if (!empty($restaurants)) {
        $restaurants = array_map(function ($t) {
            $location = findByIdArray($t['location_id'], 'locations');
            $t['city'] = $location['city'];
            $t['imageurl'] = SITEURL . 'merchantoutlet_images/' . $t['image'];
            return $t;
        }, $restaurants);
        for ($i = 0; $i < count($restaurants); $i++) {
            $restaurants[$i]['locations'] = json_decode(findByCondition(array('outlet_id' => $restaurants[$i]['id']), 'merchantoutlet_location_map'));
        }
        $rarray = array('type' => 'success', 'data' => $restaurants);
    } else {
        $rarray = array('type' => 'error', 'message' => 'No outlets found');
    }
    echo json_encode($rarray);
}
function activeProfile($unique_id)
{
    $request = Slim::getInstance()->request();
    $body = json_decode($request->getBody());
    //print_r($body);exit;
    //$email = $body->email;
    //$email = isset($user->email)?$user->email:$user->username;
    //echo $email;exit;
    //$pass = md5($body->password);
    $result = '';
    if (!empty($unique_id)) {
        //	    $db = getConnection();
        //	    $sql = "SELECT * FROM users WHERE unique_code=:unique_code";
        //	    $stmt = $db->prepare($sql);
        //	    $stmt->bindParam("unique_code", $unique_id);
        //	    $stmt->execute();
        //	    $count = $stmt->rowCount();
        //	    //echo $count;exit;
        //	    $user = $stmt->fetchObject();
        //	    $db = null;
        $user = json_decode(findByCondition(array('unique_code' => $unique_id), 'users'));
        $count = count($user);
        if ($count == 0) {
            $result = '{"type":"error","message":"You are Not Valid User"}';
        } elseif ($count == 1) {
            //$result = json_encode($user);
            $user = $user['0'];
            $logged_in = $user->is_logged_in;
            $id = $user->id;
            $email = $user->email;
            //$arr['is_logged_in'] = 1;
            $arr['is_active'] = 1;
            $arr['last_login'] = date('Y-m-d h:m:s');
            $updateinfo['save_data'] = $arr;
            // print_r($updateinfo);exit;
            $update = edit(json_encode($updateinfo), 'users', $id);
            if (!empty($update)) {
                //print_r($update);exit;
                $user->is_active = 1;
                //$user->is_logged_in=1;
                $user->last_login = $arr['last_login'];
                $user_details = json_encode($user);
                $db = getConnection();
                $sql1 = "SELECT * FROM member_user_map WHERE email=:email order by id desc";
                $stmt = $db->prepare($sql1);
                $stmt->bindParam("email", $email);
                $stmt->execute();
                $membercount = $stmt->rowCount();
                //echo $count;exit;
                //$tempvoucher = $stmt->fetchObject();
                $allmember = $stmt->fetchAll(PDO::FETCH_OBJ);
                //print_r($allmember);
                //exit;
                $db = null;
                if ($membercount != 0) {
                    //$arr['is_active'] = 1;
                    $member_offer_id = $allmember[0]->offer_id;
                    $member_voucher_id = $allmember[0]->voucher_id;
                    $member_name = $allmember[0]->name;
                    $first_name = '';
                    $last_name = '';
                    if (!empty($member_name)) {
                        $member_name = explode(' ', $member_name);
                        $first_name = $member_name[0];
                        $last_name = $member_name[1];
                        if (isset($member_name[2])) {
                            $last_name = $last_name . ' ' . $member_name[2];
                        }
                        $arr = array();
                        $arr['first_name'] = $first_name;
                        $arr['last_name'] = $last_name;
                        $updateinfo = array();
                        $updateinfo['save_data'] = $arr;
                        // print_r($updateinfo);exit;
                        $update = edit(json_encode($updateinfo), 'users', $id);
                    }
                    $member_voucher_query = "SELECT * FROM `voucher_owner` WHERE offer_id={$member_offer_id} and is_active=1 and voucher_id={$member_voucher_id}";
                    $memberVoucherDetails = findByQuery($member_voucher_query);
                    //print_r($memberVoucherDetails);
                    //exit;
                    if (!empty($memberVoucherDetails)) {
                        $voucher_owner_id = $memberVoucherDetails[0]['id'];
                        $maparr = array();
                        $maparr['to_user_id'] = $id;
                        $voucherupdateinfo['save_data'] = $maparr;
                        $cndtn = array();
                        $cndtn['id'] = $voucher_owner_id;
                        $updatemap = editByField(json_encode($voucherupdateinfo), 'voucher_owner', $cndtn);
                    }
                    $maparr = array();
                    $maparr['user_id'] = $id;
                    $mapupdateinfo['save_data'] = $maparr;
                    $cndtn = array();
                    $cndtn['email'] = $email;
                    $updatemap = editByField(json_encode($mapupdateinfo), 'member_user_map', $cndtn);
                }
                $db = getConnection();
                $sql = "SELECT * FROM gift_voucher_non_user WHERE email=:email";
                $stmt = $db->prepare($sql);
                $stmt->bindParam("email", $email);
                $stmt->execute();
                $tempvouchercount = $stmt->rowCount();
                //echo $count;exit;
                //$tempvoucher = $stmt->fetchObject();
                $tempvouchers = $stmt->fetchAll(PDO::FETCH_OBJ);
                $db = null;
                $expired_vouchers = 0;
                if ($tempvouchercount != 0) {
                    foreach ($tempvouchers as $tempvoucher) {
                        $date1 = strtotime($tempvoucher->gift_date);
                        $date2 = time();
                        $interval = floor(($date2 - $date1) / (60 * 60 * 24));
                        $tempid = $tempvoucher->id;
                        if ($interval < 3) {
                            $to_user = $id;
                            $from_user = $tempvoucher->user_id;
                            $vid = $tempvoucher->voucher_id;
                            //$vid = $tempvoucher->voucher_id;
                            if (!empty($to_user)) {
                                if (!empty($from_user)) {
                                    $db = getConnection();
                                    $sql = "SELECT voucher_owner.id, voucher_owner.is_active, vouchers.id as voucher_id, vouchers.offer_id, vouchers.view_id, vouchers.offer_price, vouchers.offer_percent, vouchers.price, users.first_name, users.last_name, users.email  FROM vouchers, voucher_owner, users WHERE voucher_owner.voucher_id = vouchers.id and voucher_owner.voucher_id=:vid and voucher_owner.is_active = '1' and voucher_owner.to_user_id=:userid";
                                    $stmt = $db->prepare($sql);
                                    $stmt->bindParam("vid", $vid);
                                    $stmt->bindParam("userid", $from_user);
                                    $stmt->execute();
                                    $voucherowner = $stmt->fetchObject();
                                    if (!empty($voucherowner)) {
                                        $ownerid = $voucherowner->id;
                                        $offerid = $voucherowner->offer_id;
                                        $viewid = $voucherowner->view_id;
                                        $offer_price = $voucherowner->offer_price;
                                        $offer_percent = $voucherowner->offer_percent;
                                        $price = $voucherowner->price;
                                        $vid = $voucherowner->voucher_id;
                                        $arr = array();
                                        $arr['is_active'] = 0;
                                        //$arr['buy_price'] = $buy_price;
                                        $arr['sold_date'] = date('Y-m-d h:i:s');
                                        $allinfo['save_data'] = $arr;
                                        $old_owner_details = edit(json_encode($allinfo), 'voucher_owner', $ownerid);
                                        if ($old_owner_details) {
                                            $data = array();
                                            $data['voucher_id'] = $vid;
                                            $data['offer_id'] = $offerid;
                                            $data['voucher_view_id'] = $viewid;
                                            $data['from_user_id'] = $from_user;
                                            $data['to_user_id'] = $to_user;
                                            $data['price'] = $price;
                                            $data['offer_price'] = $offer_price;
                                            $data['offer_percent'] = $offer_percent;
                                            $data['is_active'] = '1';
                                            $data['buy_price'] = '0.00';
                                            $data['purchased_date'] = date('Y-m-d h:i:s');
                                            $data['transfer_type'] = 'Gift';
                                            $newinfo['save_data'] = $data;
                                            $new_owner_details = add(json_encode($newinfo), 'voucher_owner');
                                            if (!empty($new_owner_details)) {
                                                $new = json_decode($new_owner_details);
                                                $giveData['voucher_id'] = $vid;
                                                $giveData['offer_id'] = $offerid;
                                                $giveData['from_user_id'] = $from_user;
                                                $giveData['to_user_id'] = $to_user;
                                                $giveData['created_on'] = date('Y-m-d h:i:s');
                                                $giveData['is_active'] = 1;
                                                $newgiveData['save_data'] = $giveData;
                                                $new_owner_details = add(json_encode($newgiveData), 'give_voucher');
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            $expired_vouchers++;
                        }
                        $deletetemptable = delete('gift_voucher_non_user', $tempid);
                    }
                }
                if (isset($expired_vouchers) && $expired_vouchers > 0) {
                    $result = '{"type":"success","message":"Logged In Succesfully","user_details":' . $user_details . ',"voucher_message":"Your ' . $expired_vouchers . ' Promo has been expired."}';
                } else {
                    $result = '{"type":"success","message":"Logged In Succesfully","user_details":' . $user_details . '}';
                }
            } else {
                $result = '{"type":"error","message":"Error occured"}';
            }
        }
        //
        echo $result;
    }
}
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);
}