public function testCronFunction()
 {
     $objInstagramScrape = new API\InstagramAPI\Instagram_scrape();
     $instagramUsername = "******";
     $startIndex = 1;
     $endIndex = 2;
     $res = $objInstagramScrape->getDetailsByStartAndLastSpreadIndex($instagramUsername, $startIndex, $endIndex);
     dd($res);
     $objInstagramUserModel = new Instagram_User();
     $insUsersId = [161, 40];
     $messages = array();
     foreach ($insUsersId as $key => $insUserId) {
         $queryResult = $objInstagramUserModel->updateUserDetails(['rawQuery' => 'ins_user_id=?', 'bindParams' => [$insUserId]], ['pics_fetch_count' => 0, 'pics_done' => 0, 'daily_post_done' => 0, 'reset_counter_time' => time()]);
         $instagramUserDetails = $objInstagramUserModel->getUserDetails(['rawQuery' => 'ins_user_id=?', 'bindParams' => [$insUserId]], ['ins_username', 'pics_limit']);
         if ($queryResult) {
             $messages[$key] = 'Done! We have reset the post done count for # ' . $instagramUserDetails[0]->ins_username . ' .Autolikes script can process ' . $instagramUserDetails[0]->pics_limit . ' more new posts.';
         } else {
             $messages[$key] = 'Sorry! Some Problem Occurred. Please reload the page and try again.';
         }
         //                                    $response->code = 200;
         //                                    $response->message = "Success";
         //                                    $response->data = $instagramUserDetails[0]->ins_username;
         //                                    echo json_encode($response, true);die;
     }
     $records["customActionStatus"] = "OK";
     // pass custom message(useful for getting status of group actions)
     $records["customActionMessage"] = $messages;
     dd($records["customActionMessage"]);
     foreach ($insUsersId as $key => $insUserId) {
         //code modified by saurabh //just use the autolikesscript() function here.
         $instagramUserDetails = $objInstagramUserModel->getUserDetails(['rawQuery' => 'ins_user_id=?', 'bindParams' => [$insUserId]]);
         //                                    dd($instagramUserDetails[0]->pics_fetch_count);
         $userExists = $objInstagramScrape->isUsernameExists($instagramUserDetails[0]->ins_username);
         if (is_numeric($userExists)) {
             $oldPicsFetchCount = $instagramUserDetails[0]->pics_fetch_count;
             //            dd($oldPicsFetchCount);
             if (($instagramUserDetails[0]->ig_user_status == 2 || $instagramUserDetails[0]->ig_user_status == 3) && $instagramUserDetails[0]->cronjob_status == 0) {
                 //                                        if (!empty($instagramUserDetails[0]) || $instagramUserDetails[0] != 0) {
                 //                                            $this->checkUserProfile($instagramUserDetails[0]);
                 //                                            $messages[$key] = 'hahahsfashash';
                 //                                        }
                 $this->checkUserProfile($instagramUserDetails);
                 //                dd($user);
                 $instagramUserDetails = $objInstagramUserModel->getUserDetails(['rawQuery' => 'ins_user_id=?', 'bindParams' => [$insUserId]], ['pics_fetch_count', 'ins_username']);
                 //                dd($instagramUserDetails[0]->pics_fetch_count);
                 $picsFetchCount = $instagramUserDetails[0]->pics_fetch_count - $oldPicsFetchCount;
                 //                dd($picsFetchCount);
                 if ($picsFetchCount != 0) {
                     $messages[$key] = 'Done! We got ' . $picsFetchCount . ' new posts and added for processing. Please check your order history.';
                 } else {
                     $messages[$key] = 'There is no any new post for this profile ' . $instagramUserDetails[0]->ins_username . ' .';
                 }
             } else {
                 $messages[$key] = '#' . $instagramUserDetails[0]->ins_username . ' has been finished or failed';
             }
         } else {
             $messages[$key] = $instagramUserDetails[0]->ins_username . ' does not exists OR may be private';
         }
     }
     dd($messages);
     $endIndex = 11;
     $link = "https://www.instagram.com/p/BE8Z1Iwq1Ss/?taken-by=saurabh_bond";
     //        $res = $objScrapeWebsta->instagram_scrape($username, $lastPostCreatedTime = 123);
     //        $res = $objScrapeWebsta->instagramScrape($username, $lastPostCreatedTime = 123, 'image');
     //        $res = $objScrapeWebsta->instagramScrapeOfDirectLink($username, $lastPostCreatedTime = 123);
     //        $res = $objScrapeWebsta->isUsernameExists($username);
     //        $res = $objScrapeWebsta->isVideoPost($username);
     //        $res = $objScrapeWebsta->instagramScrapeOfDirectLink($link);
     $res = $objScrapeWebsta->getProfilePicUrl($username);
     //        $res = $objScrapeWebsta->instagramScrapeByEndIndex($username, $endIndex);
     //       dd(intval(str_replace(",", "",$res)));
     dd($res);
     $res = "{\"code\":200,\"Message\":\"Your requested information has been processed.\"}";
     $res = json_decode($res, true);
     dd($res['code']);
     //        $objModelSocialNator=new API\SocialNator();
     //        $order_details = [];
     //        $order_details['instagramprofileurl'] = 'https://www.instagram.com/p/BE8Z1IwqSs/';
     //        $order_details['amount'] = 1;
     //        $order_details['method'] = "add_likes";
     //
     ////$response = $api->add_order($order_details['instagramprofileurl'], $order_details['amount'], $order_details['method']);
     //        $result=$objModelSocialNator->add_order($order_details['instagramprofileurl'], $order_details['amount'], $order_details['method']);
     //        $result=json_encode($result,true);
     $objUsersmeta = new Usersmeta();
     $objUsersmeta->updateUsermetaWhere(['rawQuery' => 'id=?', 'bindParams' => [7]], ['invite_id' => time() - 1459233158]);
 }
 public function sendScheduleEmails($userDetails)
 {
     $objUsersmetaModel = new Usersmeta();
     $objInstagramUserModel = new Instagram_User();
     foreach ($userDetails as $user) {
         $where = ['rawQuery' => 'user_id=?', 'bindParams' => [$user->user_id]];
         $queryResult = $objUsersmetaModel->updateUsermetaWhere($where, ['cronjob_status' => 0]);
         // replace with 1
     }
     foreach ($userDetails as $user) {
         // code for notify balance
         if ($user->notify_bal != 0) {
             if ($user->account_bal <= $user->notify_bal) {
                 $html = '<div>
                             <h3>Alert Message</h3><br>
                             <span>Your account balance is less than $' . $user->notify_bal . ' Please add balance in your account to continue autolikes service. </span><br>
                          </div>';
                 $subject = 'Less Balance Notification';
                 $toEmail = $user->email;
                 $result = $this->sendEmailNotification($html, $subject, $toEmail);
             }
         }
         $where = ['rawQuery' => 'instagram_users.by_user_id=?', 'bindParams' => [$user->user_id]];
         $data = ['instagram_users.ins_user_id', 'instagram_users.by_user_id', 'instagram_users.ins_username', 'instagram_users.pics_done', 'instagram_users.pics_limit', 'instagram_users.start_date_time', 'instagram_users.end_date_time'];
         $instagramUserDetails = $objInstagramUserModel->getUserDetails($where, $data);
         if ($instagramUserDetails) {
             // code for notify autolikes profile
             if ($user->notify_profile_likes != 0) {
                 $notifyUserCount = 0;
                 $tableBody = '<tbody>';
                 foreach ($instagramUserDetails as $insUser) {
                     $postLeft = $insUser->pics_limit - $insUser->pics_done;
                     if ($postLeft != 0 && $postLeft <= $user->notify_profile_likes) {
                         $tableBody = $tableBody . '<tr><td>' . $insUser->ins_username . '</td><td>' . $postLeft . '</td></tr>';
                         $notifyUserCount += 1;
                     }
                 }
                 //End of autolikes profile - Inner for loop
                 $tableBody = $tableBody . '<tbody>';
                 if ($notifyUserCount != 0) {
                     $html = '<div><h3>Alert Message</h3><br><span>The following autolikes users profile has less than ' . $user->notify_profile_likes . ' Post left. </span><br>
                               <table>
                                  <thead>
                                     <tr> <th>Instagram Username</th> <th>Autolikes Profile Post left</th>  </tr>
                                  </thead>
                                 ' . $tableBody . '
                               </table>
                           </div>';
                     $subject = 'Instagram Users Profile Notification';
                     $toEmail = $user->email;
                     $result = $this->sendEmailNotification($html, $subject, $toEmail);
                 }
             }
             // code for notify autolikes profile subscription
             if ($user->notify_daily_subscription != 0) {
                 $notifyUserCount = 0;
                 $dateTimeDiff = 0;
                 $tableBody = '<tbody>';
                 foreach ($instagramUserDetails as $insUser) {
                     if (intval($insUser->end_date_time) != 0) {
                         $endDateTime = intval($insUser->end_date_time);
                         $currentDateTime = time();
                         if ($currentDateTime <= $endDateTime) {
                             $dateTimeDiff = $endDateTime - $currentDateTime;
                             $notifyTime = intval($user->notify_daily_subscription);
                             if ($notifyTime == 3) {
                                 if ($dateTimeDiff <= 3600 * 24) {
                                     // 24 hr=3600*24
                                     $tableBody = $tableBody . '<tr><td>' . $insUser->ins_username . '</td><td>' . $this->getDateDifference($endDateTime) . ' </td></tr>';
                                     $where = ['rawQuery' => 'ins_user_id=?', 'bindParams' => [$insUser->ins_user_id]];
                                     $customMessageData['message'] = 'This instagram users Auto Likes profile subscription will expire in th next 24 hrs) or (last day of Auto Likes profile subscription. It will expire today';
                                     $queryResult = $objInstagramUserModel->updateUserDetails($where, $customMessageData);
                                     $notifyUserCount += 1;
                                 }
                             } else {
                                 if ($notifyTime == 1) {
                                     if ($dateTimeDiff >= 3600 * 24 * 1 && $dateTimeDiff <= 3600 * 24 * 2) {
                                         // 48 hr=3600*24*2
                                         $tableBody = $tableBody . '<tr><td>' . $insUser->ins_username . '</td><td>' . $this->getDateDifference($endDateTime) . ' </td></tr>';
                                         $where = ['rawQuery' => 'ins_user_id=?', 'bindParams' => [$insUser->ins_user_id]];
                                         $customMessageData['message'] = 'This instagram users Auto Likes profile subscription will expire after 1 day) or ( only 2 days remaining for Auto Likes profile subscription.';
                                         $queryResult = $objInstagramUserModel->updateUserDetails($where, $customMessageData);
                                         $notifyUserCount += 1;
                                     }
                                 } else {
                                     if ($notifyTime == 2) {
                                         if ($dateTimeDiff >= 3600 * 24 * 2 && $dateTimeDiff <= 3600 * 24 * 3) {
                                             // 72 hr=3600*24*3
                                             $tableBody = $tableBody . '<tr><td>' . $insUser->ins_username . '</td><td>' . $this->getDateDifference($endDateTime) . ' </td></tr>';
                                             $where = ['rawQuery' => 'ins_user_id=?', 'bindParams' => [$insUser->ins_user_id]];
                                             $customMessageData['message'] = 'This instagram users Auto Likes profile subscription will expire after 2 days) or ( only 3 days remaining for Auto Likes profile subscription.';
                                             $queryResult = $objInstagramUserModel->updateUserDetails($where, $customMessageData);
                                             $notifyUserCount += 1;
                                         }
                                     }
                                 }
                             }
                         } else {
                             //Code for for Autolikes subscription expired.
                             //After end date reached system will pause the auto likes and set the profile status to finished
                             $where = ['rawQuery' => 'ins_user_id=?', 'bindParams' => [$insUser->ins_user_id]];
                             $queryResult = $objInstagramUserModel->updateUserDetails($where, ['ig_user_status' => 1]);
                         }
                     }
                 }
                 //End of profile subscription - Inner for loop
                 $tableBody = $tableBody . '<tbody>';
                 if ($notifyUserCount != 0) {
                     $html = '<div><h3>Alert Message</h3><br> <span>The following Instagram Auto-Likes profile are about to expire. </span><br>
                             <table>
                                 <thead>
                                     <tr> <th>Instagram Username</th> <th>Autolikes Profile subscription Expire after </th>  </tr>
                                 </thead>
                             ' . $tableBody . '
                             </table>
                         </div>';
                     $subject = 'Instagram Users Profile daily subscription Notification';
                     $toEmail = $user->email;
                     $result = $this->sendEmailNotification($html, $subject, $toEmail);
                 }
             }
         }
         $where = ['rawQuery' => 'user_id=?', 'bindParams' => [$user->user_id]];
         $queryResult = $objUsersmetaModel->updateUsermetaWhere($where, ['cronjob_status' => 0]);
     }
     //End of Outer for loop
 }