#!/usr/bin/php -q
<?php 
// #!/usr/bin/php 
require_once("Classes/initializer.php");
require_once("index.php");

//Where the server should find ONLINE Orders
//define("ONLINE_ORDER_URL", "http://videals.biz/APEX_POS_TEST/APEX-POS-DB/index.php?get_ONLINE_ORDERS=get_ONLINE_ORDERS");
//define("ONLINE_ORDER_CURL", "http://videals.biz/APEX_POS_TEST/APEX-POS-DB/index.php");



global $database;

// $f = json_decode (file_get_contents(ONLINE_ORDER_URL));

// error_log( print_R($f,true) );
// echo json_encode ($f);

 


// $geteData = "SELECT `Interactive_date` FROM `Orders` ORDER BY `Interactive_date` ASC LIMIT 1";
// $Interactive_date = $database->query($geteData);
// $FinalData = mysqli_fetch_array( $Interactive_date, MYSQLI_NUM) ;


// $getCusomerData = "SELECT `Interactive_date` FROM `Customer` ORDER BY `Interactive_date` ASC LIMIT 1";
// $Interactive_date_Customer = $database->query($getCusomerData);
// $FinalDataCustomer = mysqli_fetch_array( $Interactive_date_Customer, MYSQLI_NUM) ;

//error_log( print_R( $FinalData[0][0],true) );
//
//
//

    $i=0;
    while ($i<=2) {
    
        echo ("sleeping");
    
       sleep(15);
    
    
    $post = [
       'action' => 'TEST_cURL'
    ];
    
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, ONLINE_ORDER_CURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

    $response = json_decode (curl_exec($ch));
    // echo ("after executing curl");
    // close the connection, release resources used
    // echo curl_error($ch);
    //echo curl_error($ch);
    curl_close($ch);
    
    //var_export($response);
    //in here we handle the response  and we shoul store it in our local server
    
    //$resutl = $response;
     
     
    
    
    //storing new customers
    $customerOnServer = $response->Customers ;
    
    foreach( $customerOnServer as $key=>$value) {
       $Customer = new Customer ("Blank");
    
       foreach( $customerOnServer[$key] as $attribute=>$value){ 
            if(property_exists($Customer,$attribute)) {
                $Customer->$attribute =trim($value) ;
            }
          }
    
          //we need to set the other attributes before saving object
         $Customer->Sent_to_POS  = "YES";
        // $Customer->Interactive_date  =  date('Y-m-d');

      ////*******LOCAL SERVER ONLY **********//////
         error_log( print_R("before saving ",true) );
         error_log( print_R($Customer,true) );
         unset($Customer->Interactive_date);
         unset($Customer->Points);
         unset($Customer->Blocked);
         unset($Customer->Accredited);
         unset($Customer->Credits);

        // error_log( print_R($Customer,true) );
    
         
       
          //we need to sent the updated customer to the server
         if ($Customer->verifyCustomer()) {
            $updatedCustomer = Customer::find_by_id($Customer->id);
         
         //   error_log( print_R("sending to SErver",true) );
         // error_log( print_R($updatedCustomer,true) );

            $post = [
                'action' => 'APP_VerifyUser',
                'CustomerUdating'=>true,
                'updatedCustomer'=>  $updatedCustomer
               ];

            ONLINE_SERVER_MARKER ($post);
          
            
         }
        //////*************************///////////
        
    
       
    }
    
    
    foreach( $response->Orders as $index=>$value) {
        RegisterONLINE_Sales($value);
       
    }
    
    
    
    
    
    
    
    
    
    //Tell server to Mark Users//dont waste resourse
   // Ask_ONLINE_To_Mark_Customer ( $customerOnServer);
    
    
    
    $i++;
    }
    
  //  echo json_encode ( $response);
    


   

?>