function get_expected_recurring_earning($date_start, $date_end, $member_type)
 {
     global $wpdb;
     $start = 0;
     $limit = 1000;
     //user meta fields
     $fields = array('user_id', 'meta_value');
     // sql
     $sql = "SELECT count(*) FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'mgm_member_options' AND `user_id` <> 1";
     $count = $wpdb->get_var($sql);
     // init
     $members = array();
     // packs
     $packs = mgm_get_class('subscription_packs');
     $sformat = mgm_get_date_format('date_format_short');
     $curr_date = mgm_get_current_datetime();
     $current_date = $curr_date['timestamp'];
     $on_going = array();
     $recuring_cycles = array();
     $dt = array();
     //setting the default dates
     if (empty($date_end)) {
         $end_date = date('Y-m-d', $current_date);
         $end_date = date('Y-m-d', strtotime("{$end_date} + 6 months"));
     } else {
         $end_date = mgm_format_inputdate_to_mysql($date_end, $sformat);
         //$end_date=str_replace('/','-',$date_end);
     }
     if (!empty($date_start)) {
         $start_date = mgm_format_inputdate_to_mysql($date_start, $sformat);
         //$start_date=str_replace('/','-',$date_start);
         if (strtotime($start_date) > $current_date) {
             $current_date = strtotime($start_date);
         }
     }
     //count
     if ($count) {
         for ($k = $start; $k < $count; $k = $k + $limit) {
             $users = mgm_patch_partial_user_member_options($k, $limit, $fields);
             foreach ($users as $user) {
                 $user_id = $user->user_id;
                 $member = unserialize($user->meta_value);
                 // convert
                 $member = mgm_convert_array_to_memberobj($member, $user_id);
                 $expire_date = $member->expire_date;
                 $membership_type = $member->membership_type;
                 if (!empty($expire_date) && $member->status == 'Active' && $member->amount > 0) {
                     if ($membership_type == $member_type || $member_type == 'all') {
                         if (strtotime($expire_date) >= $current_date) {
                             //echo "<br/>Expire_date : ".$expire_date;
                             //echo "<br/>Current_date : ".date('Y-m-d',$current_date);
                             // check pack
                             $subs_pack = null;
                             if ($member->pack_id) {
                                 $subs_pack = $packs->get_pack($member->pack_id);
                             }
                             if (empty($subs_pack)) {
                                 if ($member_type != 'all') {
                                     $subs_pack = $packs->validate_pack($member->amount, $member->duration, $member->duration_type, $member_type);
                                 } else {
                                     $subs_pack = $packs->validate_pack($member->amount, $member->duration, $member->duration_type, $membership_type);
                                 }
                             }
                             $num_cycles = isset($member->active_num_cycles) && !empty($member->active_num_cycles) ? $member->active_num_cycles : $subs_pack['num_cycles'];
                             $num_cycles = trim($num_cycles);
                             $rebilled = 0;
                             if ($num_cycles > 0) {
                                 $rebilled = $member->rebilled;
                                 if ($num_cycles > $rebilled) {
                                     $num = $num_cycles - $rebilled;
                                     for ($i = 1; $i <= $num; $i++) {
                                         $duration = $i * $subs_pack['duration'];
                                         //days
                                         if ($member->duration_type == 'd') {
                                             $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " days"));
                                         }
                                         //months
                                         if ($member->duration_type == 'm') {
                                             $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " months"));
                                         }
                                         //years
                                         if ($member->duration_type == 'y') {
                                             $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " years"));
                                         }
                                         if (strtotime($d) <= strtotime($end_date)) {
                                             if (!isset($recuring_cycles[$d])) {
                                                 $dt[] = $d;
                                                 $recuring_cycles[$d]['cost'] = $subs_pack['cost'];
                                                 $recuring_cycles[$d]['expire_date'] = $d;
                                             } else {
                                                 $recuring_cycles[$d]['cost'] = $test[$d]['cost'] + $subs_pack['cost'];
                                             }
                                         }
                                     }
                                 }
                             }
                             //num_cycles >0
                             if ($num_cycles == 0 && $num_cycles != '' && !empty($subs_pack)) {
                                 $exp_date = $expire_date;
                                 //days
                                 if ($member->duration_type == 'd') {
                                     $i = 1;
                                     while (strtotime($exp_date) <= strtotime($end_date)) {
                                         $duration = $i * $subs_pack['duration'];
                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " days"));
                                         $exp_date = $d;
                                         $i++;
                                         if (strtotime($d) <= strtotime($end_date)) {
                                             if (!isset($on_going[$d])) {
                                                 $dt[] = $d;
                                                 $on_going[$d]['cost'] = $subs_pack['cost'];
                                                 $on_going[$d]['expire_date'] = $d;
                                             } else {
                                                 $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                             }
                                         }
                                     }
                                 }
                                 //days end
                                 //months
                                 if ($member->duration_type == 'm') {
                                     $i = 1;
                                     while (strtotime($exp_date) <= strtotime($end_date)) {
                                         $duration = $i * $subs_pack['duration'];
                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " months"));
                                         $exp_date = $d;
                                         $i++;
                                         if (strtotime($d) <= strtotime($end_date)) {
                                             if (!isset($on_going[$d])) {
                                                 $dt[] = $d;
                                                 $on_going[$d]['cost'] = $subs_pack['cost'];
                                                 $on_going[$d]['expire_date'] = $d;
                                             } else {
                                                 $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                             }
                                         }
                                     }
                                 }
                                 //moths end
                                 //years
                                 if ($member->duration_type == 'y') {
                                     $i = 1;
                                     while (strtotime($exp_date) <= strtotime($end_date)) {
                                         $duration = $i * $subs_pack['duration'];
                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " years"));
                                         $exp_date = $d;
                                         $i++;
                                         if (strtotime($d) <= strtotime($end_date)) {
                                             if (!isset($on_going[$d])) {
                                                 $dt[] = $d;
                                                 $on_going[$d]['cost'] = $subs_pack['cost'];
                                                 $on_going[$d]['expire_date'] = $d;
                                             } else {
                                                 $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                             }
                                         }
                                     }
                                 }
                                 //years end
                             }
                             //$num_cycles ==0
                         }
                         //EXPIRE DATE GRATER THAN CURRENT DATE
                     }
                     //Membership Type
                 }
                 //EMPTY EXPIRE DATE
                 //checking recurring payments for other membership levels of a user
                 if (isset($member->other_membership_types) && is_object($member->other_membership_types) && !empty($member->other_membership_types)) {
                     foreach ($member->other_membership_types as $key => $other_member) {
                         if (!empty($other_member)) {
                             $other_member = mgm_convert_array_to_memberobj($other_member, $user_id);
                             $membership_type = $other_member->membership_type;
                             $expire_date = $other_member->expire_date;
                             if (!empty($expire_date) && $other_member->status == 'Active' && $other_member->amount > 0) {
                                 if ($membership_type == $member_type || $member_type == 'all') {
                                     if (strtotime($expire_date) >= $current_date) {
                                         //echo "<br/>Expire_date : ".$expire_date;
                                         //echo "<br/>Current_date : ".date('Y-m-d',$current_date);
                                         // check pack
                                         $subs_pack = null;
                                         if ($other_member->pack_id) {
                                             $subs_pack = $packs->get_pack($other_member->pack_id);
                                         }
                                         if (empty($subs_pack)) {
                                             if ($member_type != 'all') {
                                                 $subs_pack = $packs->validate_pack($other_member->amount, $other_member->duration, $other_member->duration_type, $member_type);
                                             } else {
                                                 $subs_pack = $packs->validate_pack($other_member->amount, $other_member->duration, $other_member->duration_type, $membership_type);
                                             }
                                         }
                                         $num_cycles = isset($other_member->active_num_cycles) && !empty($other_member->active_num_cycles) ? $other_member->active_num_cycles : $subs_pack['num_cycles'];
                                         $num_cycles = trim($num_cycles);
                                         $rebilled = 0;
                                         if ($num_cycles > 0) {
                                             $rebilled = $member->rebilled;
                                             if ($num_cycles > $rebilled) {
                                                 $num = $num_cycles - $rebilled;
                                                 for ($i = 1; $i <= $num; $i++) {
                                                     $duration = $i * $subs_pack['duration'];
                                                     //days
                                                     if ($other_member->duration_type == 'd') {
                                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " days"));
                                                     }
                                                     //months
                                                     if ($other_member->duration_type == 'm') {
                                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " months"));
                                                     }
                                                     //years
                                                     if ($other_member->duration_type == 'y') {
                                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " years"));
                                                     }
                                                     if (strtotime($d) <= strtotime($end_date)) {
                                                         if (!isset($recuring_cycles[$d])) {
                                                             $dt[] = $d;
                                                             $recuring_cycles[$d]['cost'] = $subs_pack['cost'];
                                                             $recuring_cycles[$d]['expire_date'] = $d;
                                                         } else {
                                                             $recuring_cycles[$d]['cost'] = $test[$d]['cost'] + $subs_pack['cost'];
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         //num_cycles >0
                                         if ($num_cycles == 0 && $num_cycles != '' && !empty($subs_pack)) {
                                             $exp_date = $expire_date;
                                             //days
                                             if ($other_member->duration_type == 'd') {
                                                 $i = 1;
                                                 while (strtotime($exp_date) <= strtotime($end_date)) {
                                                     $duration = $i * $subs_pack['duration'];
                                                     $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " days"));
                                                     $exp_date = $d;
                                                     $i++;
                                                     if (strtotime($d) <= strtotime($end_date)) {
                                                         if (!isset($on_going[$d])) {
                                                             $dt[] = $d;
                                                             $on_going[$d]['cost'] = $subs_pack['cost'];
                                                             $on_going[$d]['expire_date'] = $d;
                                                         } else {
                                                             $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                                         }
                                                     }
                                                 }
                                             }
                                             //days end
                                             //months
                                             if ($other_member->duration_type == 'm') {
                                                 $i = 1;
                                                 while (strtotime($exp_date) <= strtotime($end_date)) {
                                                     $duration = $i * $subs_pack['duration'];
                                                     $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " months"));
                                                     $exp_date = $d;
                                                     $i++;
                                                     if (strtotime($d) <= strtotime($end_date)) {
                                                         if (!isset($on_going[$d])) {
                                                             $dt[] = $d;
                                                             $on_going[$d]['cost'] = $subs_pack['cost'];
                                                             $on_going[$d]['expire_date'] = $d;
                                                         } else {
                                                             $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                                         }
                                                     }
                                                 }
                                             }
                                             //moths end
                                             //years
                                             if ($other_member->duration_type == 'y') {
                                                 $i = 1;
                                                 while (strtotime($exp_date) <= strtotime($end_date)) {
                                                     $duration = $i * $subs_pack['duration'];
                                                     $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " years"));
                                                     $exp_date = $d;
                                                     $i++;
                                                     if (strtotime($d) <= strtotime($end_date)) {
                                                         if (!isset($on_going[$d])) {
                                                             $dt[] = $d;
                                                             $on_going[$d]['cost'] = $subs_pack['cost'];
                                                             $on_going[$d]['expire_date'] = $d;
                                                         } else {
                                                             $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                                         }
                                                     }
                                                 }
                                             }
                                             //years end
                                         }
                                         //$num_cycles ==0
                                     }
                                     //EXPIRE DATE GRATER THAN CURRENT DATE
                                 }
                                 //Membership Type
                             }
                         }
                     }
                 }
             }
             //users
         }
     }
     //exit;
     sort($dt);
     $dt = array_unique($dt);
     $dcount = count($dt);
     $data[0]['date_start'] = $date_start;
     $data[0]['date_end'] = $date_end;
     $data[0]['member_type'] = $member_type;
     // Migrating the recuring  and on going data to array
     for ($i = 0; $i < $dcount; $i++) {
         $udt = $dt[$i];
         if (!empty($udt)) {
             $data[$i]['date'] = $udt;
             //issue #1311
             if (isset($recuring_cycles[$udt]['cost']) && array_key_exists($udt, $recuring_cycles)) {
                 $data[$i]['cost'] = $data[$i]['cost'] + $recuring_cycles[$udt]['cost'];
             }
             if (isset($on_going[$udt]['cost']) && array_key_exists($udt, $on_going)) {
                 $data[$i]['cost'] = $data[$i]['cost'] + $on_going[$udt]['cost'];
             }
         }
     }
     return $data;
 }
 function _do_export($export_format, $export_option)
 {
     global $wpdb;
     $status = 'error';
     $message = __('Error while exporting Coupons.', 'mgm');
     // users
     if ($export_option != 'unused') {
         //$users = mgm_get_all_userids(array('ID'), 'get_results');
         $start = 0;
         $limit = 1000;
         //user meta fields
         $fields = array('user_id', 'meta_value');
         // sql
         $user_sql = "SELECT count(*) FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'mgm_member_options' AND `user_id` <> 1";
         $count = $wpdb->get_var($user_sql);
         $users = wp_cache_get('all_mgm_user_meta', 'users');
         // if empty read from db:
         if (empty($users)) {
             $users = array();
             if ($count) {
                 // read again
                 for ($i = $start; $i < $count; $i = $i + $limit) {
                     $users = array_merge($users, (array) mgm_patch_partial_user_member_options($i, $limit, $fields));
                     //a small delay of 0.01 second
                     usleep(10000);
                 }
                 wp_cache_set('all_mgm_user_meta', $users, 'users');
             }
         }
     }
     //mgm_log(mgm_array_dump($users,true),__FUNCTION__);
     //array
     $export_coupons = array();
     //export options issue #1494
     if ($export_option == 'used') {
         $query = " AND `used_count` > 0";
     } elseif ($export_option == 'unused') {
         $query = " AND `used_count` IS NULL";
     } else {
         $query = '';
     }
     // count
     $total = $wpdb->get_var("SELECT COUNT(*) FROM `" . TBL_MGM_COUPON . "` WHERE 1 " . $query);
     //
     $perpage = 50;
     // check
     if ($total > $perpage) {
         $pages = ceil($total / $perpage);
     } else {
         $pages = 1;
     }
     // loop pages
     for ($i = 1; $i <= $pages; $i++) {
         //@set_time_limit(300);//300s
         //@ini_set('memory_limit', 134217728);// 128M
         // offset
         $offset = ($i - 1) * $perpage;
         // coupons
         $sql = "SELECT `id`, `name` FROM `" . TBL_MGM_COUPON . "` WHERE 1 {$query} ORDER BY `create_dt` DESC LIMIT {$offset},{$perpage}";
         // coupons
         $coupons = $wpdb->get_results($sql);
         // log
         // mgm_log('Page: ' .$i. '. '. $wpdb->last_query, __FUNCTION__);
         // loop
         foreach ($coupons as $coupon) {
             // found
             if ($users && $export_option != 'unused') {
                 $coupon_used = false;
                 foreach ($users as $user) {
                     if ($user->user_id) {
                         // member
                         $user_id = $user->user_id;
                         $member = unserialize($user->meta_value);
                         $member = mgm_convert_array_to_memberobj($member, $user_id);
                         // show
                         $show = false;
                         // check
                         if (mgm_member_has_coupon($member, $coupon->id)) {
                             $show = true;
                         } else {
                             if (isset($member->other_membership_types) && !empty($member->other_membership_types) && count($member->other_membership_types) > 0) {
                                 // loop
                                 foreach ($member->other_membership_types as $key => $member_oth) {
                                     // as object
                                     if (is_array($member_oth)) {
                                         $o_mgm_member = mgm_convert_array_to_memberobj($member_oth, $user_id);
                                     } else {
                                         if (is_object($member_oth)) {
                                             $o_mgm_member = $member_oth;
                                         }
                                     }
                                     // check
                                     if (mgm_member_has_coupon($o_mgm_member, $coupon->id)) {
                                         $show = true;
                                         break;
                                     }
                                     // unset
                                     unset($o_mgm_member);
                                 }
                             }
                         }
                         // include
                         if ($show) {
                             //mgm_log(mgm_array_dump($member,true),__FUNCTION__);
                             // user
                             $user = get_userdata($user_id);
                             // int
                             $row = new stdClass();
                             // export fields
                             $row->coupon_id = $coupon->id;
                             $row->coupon_name = $coupon->name;
                             $row->user_id = $user->ID;
                             $row->user_login = $user->user_login;
                             $row->user_email = $user->user_email;
                             $row->user_membership_type = ucwords(str_replace('_', ' ', $member->membership_type));
                             // cache
                             $export_coupons[] = $row;
                             // unset
                             unset($row);
                             $coupon_used = true;
                         }
                         unset($user);
                         unset($member);
                     }
                 }
                 if (!$coupon_used && $export_option == 'all') {
                     // int
                     $row = new stdClass();
                     // export fields
                     $row->coupon_id = $coupon->id;
                     $row->coupon_name = $coupon->name;
                     $row->user_id = '';
                     $row->user_login = '';
                     $row->user_email = '';
                     $row->user_membership_type = '';
                     // cache
                     $export_coupons[] = $row;
                     // unset
                     unset($row);
                 }
             } else {
                 // int
                 $row = new stdClass();
                 // export fields
                 $row->coupon_id = $coupon->id;
                 $row->coupon_name = $coupon->name;
                 $row->user_id = '';
                 $row->user_login = '';
                 $row->user_email = '';
                 $row->user_membership_type = '';
                 // cache
                 $export_coupons[] = $row;
                 // unset
                 unset($row);
             }
         }
     }
     // log
     // mgm_log($export_coupons, 'export_coupons');
     // check
     if (count($export_coupons) > 0) {
         // success
         $success = count($export_coupons);
         // create
         if ($export_format == 'csv') {
             $filename = mgm_create_csv_file($export_coupons, 'export_coupons');
         } else {
             $filename = mgm_create_xls_file($export_coupons, 'export_coupons');
         }
         // src
         $file_src = MGM_FILES_EXPORT_URL . $filename;
         // message
         $message = sprintf(__('Successfully exported %d %s.', 'mgm'), $success, $success > 1 ? 'coupons' : 'coupon');
         //status
         $status = 'success';
         //return
         return array('status' => $status, 'message' => $message, 'action' => 'export', 'src' => $file_src);
     } else {
         $message .= ' ' . __('No coupons found with associated users.', 'mgm');
     }
     // return
     return array('status' => $status, 'message' => $message, 'action' => 'export');
 }
/**
 * find members with selected custom filed critera
 * @param string $field
 * @return mixed array|int  
 */
function mgm_get_members_with_customfiled($field, $value)
{
    global $wpdb;
    $start = 0;
    $limit = 1000;
    //user meta fields
    $fields = array('user_id', 'meta_value');
    // sql
    $sql = "SELECT count(*) FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'mgm_member_options' AND `user_id` <> 1";
    $count = $wpdb->get_var($sql);
    // init
    $members = array();
    //count
    if ($count) {
        //for
        for ($i = $start; $i < $count; $i = $i + $limit) {
            //users
            $users = mgm_patch_partial_user_member_options($i, $limit, $fields);
            // check
            if ($users) {
                // loop
                foreach ($users as $user) {
                    // vlid
                    $valid = false;
                    //name
                    $name = '';
                    $user_id = $user->user_id;
                    $member = unserialize($user->meta_value);
                    // convert
                    $member = mgm_convert_array_to_memberobj($member, $user_id);
                    // check
                    if (!empty($value) && isset($member->status) && $member->status == 'Active') {
                        // from custom field
                        if (isset($member->custom_fields->{$field}) && !empty($member->custom_fields->{$field})) {
                            $name = $member->custom_fields->{$field};
                        }
                        // clean
                        $value = mgm_stripslashes_deep($value);
                        // if there is a match
                        if (!empty($name) && strpos(strtolower($name), strtolower($value)) !== false) {
                            $valid = true;
                        } else {
                            $valid = false;
                        }
                    }
                    // store
                    if ($valid) {
                        $members[] = $user_id;
                    }
                    // unset object
                    unset($member);
                    unset($user);
                }
            }
        }
    }
    // return
    return $members;
}