public function applyForLeave()
 {
     $all = Input::all();
     $mgr = $this->getManager();
     $authUserId = $this->getUserId();
     $all['mgr_id'] = $mgr['id'];
     $all['user_id'] = $authUserId;
     $all['status'] = 'Eequest';
     //TODO
     //1) check if user exceed the no of days entitled for leaves fer year.
     $entitle = Entitlement::where('id', '=', $all['entitlement_id'])->first();
     /*
     $leaves = LeaveRequest::where('user_id', '=', $authUserId)
       ->where('entitlement_id', '=',  $all['entitlement_id'])->get();
     */
     $noOfDays = $entitle['no_of_days'];
     $leaveSum = DB::table('leave_requests')->where('user_id', '=', $authUserId)->where('entitlement_id', '=', $all['entitlement_id'])->sum('no_of_days');
     $totalLeaves = $leaveSum + $all['no_of_days'];
     if ($totalLeaves > $noOfDays) {
         return Response::jsend('fail', array('msg' => sprintf("You are Exceeding the Limit of %s", $entitle['name'])));
     }
     //2) if the user is already applied to that leave
     //3) create Leave request.
     LeaveRequest::create($all);
     return Response::jsend('success', array('msg' => 'Leave request is submitted.'));
 }
$account->setMerchantAccountId($merchantAccountId);
$autobill = new AutoBill();
$response = $autobill->fetchByAccount($account, false);
print_r($response);
if ($response['returnCode'] == 200) {
    $targetAutoBill = $response['data']->autobills[0];
    $paymentMethod = $targetAutoBill->paymentMethod;
    $merchantPaymentMethodId = $paymentMethod->merchantPaymentMethodId;
    print "\$merchantPaymentMethodId={$merchantPaymentMethodId}" . PHP_EOL;
    $results = hoaAccountUpdatePaymentMethod($merchantAccountId, $merchantPaymentMethodId);
    $apiReturnCode = $results['apiReturnCode'];
    $validated = $results['validated'];
    print "results: apiReturnCode={$apiReturnCode}, validated={$validated}" . PHP_EOL;
    if ($apiReturnCode == '200' && $validated == '1') {
        print "Card successfully validated when updated.  Now check if AutoBill re-activated:" . PHP_EOL;
        $entitlement = new Entitlement();
        $response = $entitlement->fetchByAccount($account, true, false);
        print_r($response);
        $now = strtotime('now');
        # current time
        $entitlements = $response['data']->entitlements;
        if (isset($entitlements) && is_array($entitlements)) {
            foreach ($entitlements as $index => $entitlement) {
                # print "\n\$index = $index\n";
                print "entitlements[" . $index . "]: = \n";
                # print_r ($entitlement);
                print "endTimestamp = " . $entitlement->endTimestamp . "\n";
                if (empty($entitlement->endTimestamp) || is_null($entitlement->endTimestamp) || strtotime($entitlement->endTimestamp) > $now) {
                    print "...entitled!  ==> subscription activated successfully.\n";
                    return;
                }
<?php

require_once 'Vindicia/Soap/Vindicia.php';
require_once 'Vindicia/Soap/Const.php';
$accountID = $argv[1];
$showAll = false;
$includeChildren = false;
print "merchantAccountId: {$accountID} \n";
$account = new Account();
$account->setMerchantAccountId($accountID);
$entitlement = new Entitlement();
$response = $entitlement->fetchByAccount($account, $showAll, $includeChildren);
print_r($response);
function fetchEntitlements()
{
    $soapId = null;
    $num_updated = 0;
    logEnvironment();
    // read next_start from database, set from last call
    $start = load_next_start();
    if (empty($start)) {
        $start = timestamp(-1);
        logMsg("TEntitlement: Initializing " . dateString("next_start", $start) . "\n");
    }
    $end = timestamp();
    // now
    $page = 0;
    $pageSize = 10;
    $entitlement = new Entitlement();
    $returnCode = '200';
    do {
        logEFDS($start, $page, $pageSize, $end);
        $response = $entitlement->fetchDeltaSince($start, $page, $pageSize, $end);
        $return = $response['data']->return;
        $results = logReturn($return, "TEntitlement result:");
        $returnCode = $results['returnCode'];
        $soapId = $results['soapId'];
        // soapId of this paged call to fetchDeltaSince
        print "soapId: " . $soapId . ", page: " . $page . "\n";
        if ('200' != $returnCode) {
            logEnvironment();
            break;
        }
        $entitlements = isset($response['data']->entitlements) ? $response['data']->entitlements : array();
        // update local entitlement status:
        $num_updated += updateEntitlements($entitlements);
        $page++;
    } while (count($entitlements) > 0);
    // Update next_start to advance to the next time window
    // only if the above loop completed successfully.
    //
    // Otherwise, we want to keep it at the same value
    // so the next execution of the loop above will read
    // the same entitlements again, to get all of the data
    // hopefully with no errors the next time around.
    //
    if ('200' == $returnCode) {
        $start = $end;
        // save end as start for next time.
        $start = date('c', strtotime($end) - 60);
        // subtract 1 minute for next call to be
        // inclusive of any updates to db in last minute
        print "Total pages: " . $page . "\n\n";
        logEFDS($start, $page, $pageSize, $end);
        print "1 minute before current end: " . $start . "\n\n";
        // use next_start as the start timestamp for next call
        // save next_start to database for next call
        save_next_start($start);
    } else {
        print "Error occurred: next_start NOT updated, the same start timestamp will " . "be used for the next iteration, so all of the same data " . "will be requested again!\n\n";
    }
    $new_next_start = load_next_start();
    // check next_start new value in database
    logMsg("\nThe next execution will use startTimestamp: " . $new_next_start . "\n");
    return $num_updated;
}