Advertisement
Guest User

gx.php

a guest
Apr 20th, 2017
1,884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 208.42 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Goexpress extends CI_Controller {
  4.  
  5.     public $cs_email = 'no-reply@com.com';
  6.  
  7.     public $is_firebase = false;
  8.     public $bc_action = "";
  9.     public $bc_txno = "";
  10.  
  11.     protected $linkPage;
  12.     protected $segment      = 3;
  13.     protected $totalPage    = 0;
  14.     protected $perPage      = 20;
  15.  
  16.     function __construct()
  17.     {
  18.         parent::__construct();
  19.  
  20.         $this->load->model('user_model');
  21.         $this->load->model('app_model');
  22.         $this->load->model('admin_model');
  23.         $this->load->model('office_model');
  24.         $this->load->model('model_goexpress');
  25.         $this->load->model('security_model');
  26.         $this->load->model('driver_model');
  27.         $this->template->set_template('sbadmin2');
  28.  
  29.         //$user_info = $this->session->userdata('user_info');
  30.  
  31.         //if( !isset($user_info->username))
  32.         //    redirect('admin/index');
  33.  
  34.  
  35.         //load Language
  36.         if ( strlen($this->uri->segment(1)) == 2 )
  37.             $this->lang->load( $this->uri->segment(1), 'gox');
  38.  
  39.         //Define Api key
  40.         if ( $this->uri->segment(1) == "apikey" )
  41.             $apikey = $this->uri->segment(2);
  42.         else
  43.             $apikey = $this->uri->segment(3);
  44.  
  45.         if ( !$this->security_model->check_api($apikey) )
  46.         {
  47.             if ( $this->uri->segment(4) == "add_txno" )
  48.                 die('1@@Your Session is expired, please relogin.');
  49.             else {
  50.                 $error_m = array("error" => 1, "error_message" => "Your Session is expired, please relogin.");
  51.                 die(json_encode($error_m));
  52.             }
  53.         }
  54.  
  55.         $this->PROMO_ACTIVE = false;
  56.     }
  57.  
  58.     protected function pagination() {
  59.         $this->load->library('pagination');
  60.         $config = array();
  61.         $config['uri_segment'] = $this->segment;
  62.         $config['base_url'] = $this->linkPage;
  63.         $config['total_rows'] = $this->totalPage;
  64.         $config['per_page'] = $this->perPage;
  65.         $config['full_tag_open'] = '<ul class="pagination">';
  66.         $config['full_tag_close'] = '</ul>';
  67.         $this->pagination->initialize($config);
  68.         return $this->pagination->create_links();
  69.     }
  70.  
  71.     protected function start_page($uri) {
  72.         if ($this->uri->segment($uri) > 0) {
  73.             $start = $this->uri->segment($uri);
  74.         } else {
  75.             $start = 0;
  76.         }
  77.         return $start;
  78.     }
  79.  
  80.     function check_new_notif()
  81.     {
  82.         $res = array("error" => 0,
  83.                      "notif_id" => 3,
  84.                      "show" => 0,
  85.                      "message" => "Dear All Driver, \nWe launch lucky draw promotion. So all client will get new lucky point when they completed 5 trip and recursive. \n\nThank you.");
  86.  
  87.         die(json_encode($res));
  88.     }
  89.  
  90.     function current_millis()
  91.     {
  92.         list($usec, $sec) = explode(" ", microtime());
  93.         return round(((float)$usec + (float)$sec) * 1000);
  94.     }
  95.  
  96.     function broadcast_tx($tx, $range)
  97.     {
  98.         $tx_detail = $this->model_goexpress->get_tx_detail(array( 'txno' => $tx));
  99.  
  100.         $payload = $tx_detail->payload;
  101.  
  102.         if( !$payload )
  103.         {
  104.             echo '1@@Error: Empty payload';
  105.             die();
  106.         }
  107.  
  108.         $txno = $tx;
  109.  
  110.         $json = json_decode($payload);
  111.  
  112.         $jTarget = $this->_process_jsontarget( $json );
  113.  
  114.         $start_detail = sprintf("%s\n*) %s", $json->data->Start->Name, $json->data->Start->Note);
  115.         $target_detail = sprintf("%s\n*) %s", $jTarget->Name, $jTarget->Note);
  116.  
  117.         $USERTYPE_DRIVER = 50;
  118.         $UPTIME = UPTIME; // in Minutes.
  119.  
  120.         $gcparams = array(
  121.             'range' => $range,
  122.             'uptime' => $UPTIME,
  123.             'client_lat' => $json->data->Start->Lat,
  124.             'client_lng' => $json->data->Start->Lng,
  125.             );
  126.  
  127.         $gcresult = $this->_get_coord_nearby($gcparams);
  128.  
  129.         $dnparams = array(
  130.             'user_type' => $USERTYPE_DRIVER,
  131.             'transport' => $json->data->Vehicle,
  132.             'last_up_time' => $gcresult['last_up_time'],
  133.             'X1' => $gcresult['X1'],
  134.             'X2' => $gcresult['X2'],
  135.             'Y1' => $gcresult['Y1'],
  136.             'Y2' => $gcresult['Y2'],
  137.             );
  138.         $driver_nearby = $this->model_goexpress->get_driver_nearby($dnparams);
  139.  
  140.         $token_driver = array();
  141.         $token_driver = $this->filter_driver($driver_nearby);
  142.  
  143.         $data = array(
  144.             "Actionmode" => $json->data->Actionmode,
  145.             "PromoCode"  => $tx_detail->promo_code,
  146.             "Vehicle"    => $json->data->Vehicle,
  147.             "TxNo"       => $txno,
  148.             "Distance"   => $json->data->DistanceTotalText,
  149.             "Duration"   => $json->data->DurationTotalText,
  150.             "Fares"      => $json->data->Fares,
  151.             "Payment"    => $json->data->Payment,
  152.             "From"       => $start_detail,
  153.             "To"         => $target_detail,
  154.             "SToken"     => $json->data->SToken,
  155.             "StartLat"   => $json->data->Start->Lat,
  156.             "StartLng"   => $json->data->Start->Lng,
  157.             "TargetLat"  => $jTarget->Lat,
  158.             "TargetLng"  => $jTarget->Lng,
  159.             "Timestamp"  => number_format($this->current_millis(),0,'',''),
  160.         );
  161.  
  162.         if( !empty($token_driver) )
  163.             $this->sendFirebaseCloudMessage(
  164.                 $data,
  165.                 $ids = array() ,
  166.                 array('separate' => True, 'registration_ids' => $token_driver ),
  167.                 array('ret_int' => True) );
  168.     }
  169.  
  170.  
  171.    function second_bc($tx)
  172.    {
  173.         // validate the driver
  174.         $this->bc_action = "Second Broadcast";
  175.         $this->bc_txno = $tx;
  176.         $this->broadcast_tx($tx, (SECOND_BC_RANGE * COORD_IN_KM));
  177.         $res = array( 'error' => 0, 'error_message' => '');
  178.         query_log('Second Broadcast Work');
  179.         die(json_encode($res));
  180.    }
  181.  
  182.    function third_bc($tx)
  183.    {
  184.         $this->bc_action = "Third Broadcast";
  185.         $this->bc_txno = $tx;
  186.         $this->broadcast_tx($tx, (THIRD_BC_RANGE * COORD_IN_KM));
  187.         $res = array('error' => 0, 'error_message' => '');
  188.         query_log('Third Broadcast Work');
  189.         die(json_encode($res));
  190.  
  191.    }
  192.  
  193.    function manual_bc($tx, $token)
  194.    {
  195.         $tx_detail = $this->model_goexpress->get_tx_detail(array( 'txno' => $tx));
  196.  
  197.         $payload = $tx_detail->payload;
  198.  
  199.         if( !$payload )
  200.         {
  201.             echo '1@@Error: Empty payload';
  202.             die();
  203.         }
  204.  
  205.         $txno = $tx;
  206.  
  207.         $json = json_decode($payload);
  208.  
  209.         $jTarget = $this->_process_jsontarget( $json );
  210.  
  211.         $start_detail = sprintf("%s\n*) %s", $json->data->Start->Name, $json->data->Start->Note);
  212.         $target_detail = sprintf("%s\n*) %s", $jTarget->Name, $jTarget->Note);
  213.  
  214.         $this->bc_action = "Manual Broadcast";
  215.         $this->bc_txno = $tx;
  216.         $drvparams = array('last_bc' => time(),
  217.                         'bc_action' => "Manual Broadcast",
  218.                         'bc_tx' => $tx );
  219.         $driverdata = $this->model_goexpress->get_user(array("token" => $token));
  220.         $this->model_goexpress->update_driver_lastbc($drvparams, $driverdata->id);
  221.  
  222.         $token_driver[0] = $token;
  223.  
  224.         $data = array(
  225.             "Actionmode" => $json->data->Actionmode,
  226.             "PromoCode"  => $tx_detail->promo_code,
  227.             "Vehicle"    => $json->data->Vehicle,
  228.             "TxNo"       => $txno,
  229.             "Distance"   => $json->data->DistanceTotalText,
  230.             "Duration"   => $json->data->DurationTotalText,
  231.             "Fares"      => $json->data->Fares,
  232.             "Payment"    => $json->data->Payment,
  233.             "From"       => $start_detail,
  234.             "To"         => $target_detail,
  235.             "SToken"     => $json->data->SToken,
  236.             "StartLat"   => $json->data->Start->Lat,
  237.             "StartLng"   => $json->data->Start->Lng,
  238.             "TargetLat"  => $jTarget->Lat,
  239.             "TargetLng"  => $jTarget->Lng,
  240.             "Timestamp"  => number_format($this->current_millis(),0,'',''),
  241.         );
  242.  
  243.         if( !empty($token_driver) )
  244.             $this->sendFirebaseCloudMessage(
  245.                 $data,
  246.                 $ids = array() ,
  247.                 array('separate' => True, 'registration_ids' => $token_driver ),
  248.                 array('ret_int' => True) );
  249.  
  250.         $resjson = array( 'error' => 0,
  251.                           'error_message' => "Broadcast Tx Success");
  252.         die(json_encode($resjson));
  253.    }
  254.  
  255.     function _process_jsontarget( $json )
  256.     {
  257.         $targetlength = count($json->data->Target);
  258.         $iTgt = $targetlength - 1;
  259.         $jTarget = new StdClass();
  260.  
  261.         $jTarget->Name = $json->data->Target[$iTgt]->Name;
  262.         $jTarget->Note = $json->data->Target[$iTgt]->Note;
  263.         $jTarget->Lat  = $json->data->Target[$iTgt]->Lat;
  264.         $jTarget->Lng  = $json->data->Target[$iTgt]->Lng;
  265.  
  266.         if ( isset($json->data->UserID) )
  267.             $userid = $json->data->UserID;
  268.         else
  269.             $userid = "";
  270.         $this->model_goexpress->add_search_data($json->data->Target, $userid, "target");
  271.         $this->model_goexpress->add_search_data($json->data->Start, $userid, "start");
  272.         return $jTarget;
  273.     }
  274.  
  275.     function getPromoCode($transport)
  276.     {
  277.         $promo_code = "";
  278.         if ( priceRate() == floatval("0.1") ) {
  279.             if ( $transport == "MOTOBIKE")
  280.                 $promo_code = "GOXBIKE10";
  281.             else if ( $transport == "TUK-TUK")
  282.                 $promo_code = "GOXTUKTUK10";
  283.             else if ( $transport == "TAXI" )
  284.                 $promo_code = "GOXCAR10";
  285.             else if ( $transport == "CAR" )
  286.                 $promo_code = "GOXMVAN10";
  287.         }
  288.         else if ( priceRate() == floatval("0.2") ) {
  289.             if ( $transport == "MOTOBIKE")
  290.                 $promo_code = "GOXBIKE20";
  291.             else if ( $transport == "TUK-TUK")
  292.                 $promo_code = "GOXTUKTUK20";
  293.             else if ( $transport == "TAXI" )
  294.                 $promo_code = "GOXCAR20";
  295.             else if ( $transport == "CAR" )
  296.                 $promo_code = "GOXMVAN20";
  297.         }
  298.         else if ( priceRate() == floatval("0.15") ) {
  299.             if ( $transport == "MOTOBIKE")
  300.                 $promo_code = "GOXBIKE15";
  301.             else if ( $transport == "TUK-TUK")
  302.                 $promo_code = "GOXTUKTUK15";
  303.             else if ( $transport == "TAXI" )
  304.                 $promo_code = "GOXCAR15";
  305.             else if ( $transport == "CAR" )
  306.                 $promo_code = "GOXMVAN15";
  307.         }
  308.         else if ( priceRate() == floatval("0.3") ) {
  309.             if ( $transport == "MOTOBIKE")
  310.                 $promo_code = "GOXBIKE30";
  311.             else if ( $transport == "TUK-TUK")
  312.                 $promo_code = "GOXTUKTUK30";
  313.             else if ( $transport == "TAXI" )
  314.                 $promo_code = "GOXCAR30";
  315.             else if ( $transport == "CAR" )
  316.                 $promo_code = "GOXMVAN30";
  317.         }
  318.         else {
  319.             if ( $transport == "MOTOBIKE")
  320.                 $promo_code = "GOXBIKE";
  321.             else if ( $transport == "TUK-TUK")
  322.                 $promo_code = "GOXTUKTUK";
  323.             else if ( $transport == "TAXI" )
  324.                 $promo_code = "GOXCAR";
  325.             else if ( $transport == "CAR" )
  326.                 $promo_code = "GOXMVAN";
  327.         }
  328.         return $promo_code;
  329.     }
  330.  
  331.     function getfares()
  332.     {
  333.  
  334.        $input_payload = file_get_contents('php://input');
  335.         // validate the driver
  336.         $json = json_decode($input_payload,true);
  337.         if ( $this->model_goexpress->checkDistance($json) == 1 )
  338.         {
  339.             $returndata = array(
  340.                         "error" => 3,
  341.                         "error_message" => "Your total distance exceed ".getMaxdistance($json['transport'])." km..");
  342.         }
  343.         else {
  344.  
  345.             $userRecord = $this->model_goexpress->get_user(array("username" => $json["id"]));
  346.             $json["id"] = $userRecord->id;
  347.             $json["all_data"] = 1;
  348.  
  349.             if( !(isset($json["promo_code"]) and $json["promo_code"] != "" and $json["promo_code"] != " " )) {
  350.                 if ( $this->PROMO_ACTIVE == true ) {
  351.                     $json["promo_code"] = $this->getPromoCode($json["transport"]);
  352.                 }
  353.             }
  354.  
  355.             list($total_price, $normal_fare, $promo_value) = $this->model_goexpress->getFares($json);
  356.  
  357.             $returndata = array(
  358.                             "error" => 0,
  359.                             "fares" => $total_price,
  360.                             "promo_value" => $promo_value,
  361.                             "normal_fare" => $normal_fare,
  362.                             "error_message" => "");
  363.         }
  364.  
  365.  
  366.         die( json_encode($returndata));
  367.     }
  368.  
  369.     function gethistory()
  370.     {
  371.         $payload = file_get_contents('php://input');
  372.  
  373.         if( !$payload )
  374.         {
  375.             echo '1@@Error: Empty payload';
  376.             die();
  377.         }
  378.  
  379.         $json = json_decode( $payload );
  380.  
  381.         $params['userid'] = $json->id;
  382.         $params['searchtype'] = $json->type;
  383.  
  384.         $res = $this->model_goexpress->get_location_data($params);
  385.  
  386.         echo json_encode($res);
  387.     }
  388.  
  389.     function add_txno()
  390.     {
  391.  
  392.         $payload = file_get_contents('php://input');
  393.  
  394.         if( !$payload )
  395.         {
  396.             echo '1@@Error: Empty payload';
  397.             die();
  398.         }
  399.  
  400.         $txno = number_format($this->current_millis(),0,'','');
  401.         $json = json_decode( $payload );
  402.  
  403.         if( !isset( $json->data->UserEmail ) )
  404.         {
  405.             echo '1@@Error: Empty Email';
  406.             die();
  407.         }
  408.  
  409.         $uparams = array(
  410.             'email' => $json->data->UserEmail,
  411.             'username' => $json->data->UserID,
  412.         );
  413.  
  414.         if ( isset($json->map_data))
  415.         {
  416.             $this->model_goexpress->save_map(array( 'txno' => $txno,
  417.                                                     'map_data' => json_encode($json->map_data)
  418.                                                    )
  419.                                             );
  420.         }
  421.  
  422.         $userrecord = $this->model_goexpress->get_user($uparams);
  423.  
  424.         if ( $userrecord->firebase == 1 )
  425.             $this->is_firebasse = true;
  426.  
  427.         $this->db->trans_start();
  428.  
  429.         if( !isset($userrecord->id))
  430.         {
  431.             echo "1@@Error: User is not registered. Please clear data and relogin/register new User";
  432.             die();
  433.  
  434.             $USERTYPE_CLIENT = 90;
  435.             $uparams['name'] = $json->data->UserName;
  436.             $uparams['user_type_id'] = $USERTYPE_CLIENT;
  437.             $uparams['source_type_id'] = SOURCETYPE_STR_TO_INT($json->data->ModeLogin);
  438.             $uparams['create_date'] = date('Y-m-d H:i:s');
  439.  
  440.             list($flag, $client_id, $msg ) = $this->model_goexpress->add_user($uparams);
  441.  
  442.             if( !$flag )
  443.             {
  444.                 echo '1@@'.$msg;
  445.                 $this->db->trans_rollback();
  446.                 die();
  447.             }
  448.         }
  449.         else
  450.         {
  451.             $client_id = $userrecord->id;
  452.         }
  453.  
  454.         // For testing purpose
  455.         //echo "1@@Error: User is registered";
  456.         //die();
  457.         $promo_code = "";
  458.         if( isset($json->data->PromoCode) and $json->data->PromoCode != "" and $json->data->PromoCode != " ")
  459.             $promo_code = $json->data->PromoCode;
  460.         else {
  461.             if ( $this->PROMO_ACTIVE == true ) {
  462.                 $json->data->PromoCode = $this->getPromoCode($json->data->Vehicle);
  463.                 $promo_code = $this->getPromoCode($json->data->Vehicle);
  464.             }
  465.         }
  466.  
  467.         $calculate_data = array(
  468.                 "promo_code"  => $promo_code,
  469.                 "transport"    => $json->data->Vehicle,
  470.                 "distance"   => floatval(str_replace(" km","",$json->data->DistanceTotalText))*1000,
  471.                 "duration"   => $json->data->DurationTotalText,
  472.                 "id"    => $userrecord->id
  473.             );
  474.  
  475.  
  476.         $json->data->Fares = $this->model_goexpress->getFares($calculate_data);
  477.         $normalPrice = $this->model_goexpress->getNormalFares($calculate_data);
  478.         if( isset($userrecord->id) )
  479.             $promoValue = $this->model_goexpress->getDeduction(
  480.                                     array("promo_code" => $promo_code,
  481.                                           "userid" => $userrecord->id ));
  482.         else
  483.             $promoValue = 0;
  484.         $officeComission = $normalPrice * COMISSION_RATE;
  485.  
  486.         $driverFee = $normalPrice - $officeComission;
  487.  
  488.         if ( floatval($promoValue) <= 1 )
  489.         {
  490.             $promoValue = $normalPrice*floatval($promoValue);
  491.         }
  492.  
  493.         if ( $promoValue > $normalPrice )
  494.             $promoValue = $normalPrice;
  495.  
  496.         query_log("Payment value is ". $json->data->Payment);
  497.         if ( $json->data->Payment == "WALLET")
  498.         {
  499.             $balance =$this->model_transactions->get_wallet_balance($json->data->UserID);
  500.             query_log("Balance is ".$balance);
  501.             if ( $json->data->Fares > $balance )
  502.             {
  503.                 query_log("You don't have anough balance. Needed Fares is ".$json->data->Fares);
  504.                 die("1@@You don't have anough balance to complete the payment. Your balance is ".$balance);
  505.             }
  506.             else
  507.             {
  508.                 $data = array( "id" => $json->data->UserID,
  509.                                 "type" => 99,
  510.                                 "note" => "Make a payment for TX with no $txno",
  511.                                 "txno"   => $txno,
  512.                                 "tx_data" => date('Y-m-d H:i:s'),
  513.                                 "amount" => intval($json->data->Fares*(-1)));
  514.                 $this->model_transactions->rm_wallet($data);
  515.  
  516.                 query_log("Processing Remove wallet! Now balance is ".$this->model_transactions->get_wallet_balance($json->data->UserID));
  517.             }
  518.         }
  519.         else {
  520.  
  521.         }
  522.  
  523.         if ( strlen($promo_code) == 4 ) {
  524.             $referal = 1;
  525.         }
  526.         else
  527.             $referal = 0;
  528.         query_log("Referal is $referal and Model referal data is " . $this->model_goexpress->referal );
  529.  
  530.         $USERTYPE_DRIVER = 50;
  531.         $RANGE = FIRST_BC_RANGE * COORD_IN_KM; // in Km.
  532.         $UPTIME = UPTIME; // in Minutes.
  533.  
  534.         $gcparams = array(
  535.             'range' => $RANGE,
  536.             'uptime' => $UPTIME,
  537.             'client_lat' => $json->data->Start->Lat,
  538.             'client_lng' => $json->data->Start->Lng,
  539.             );
  540.  
  541.         $gcresult = $this->_get_coord_nearby($gcparams);
  542.  
  543.         $dnparams = array(
  544.             'user_type' => $USERTYPE_DRIVER,
  545.             'transport' => $json->data->Vehicle,
  546.             'last_up_time' => $gcresult['last_up_time'],
  547.             'X1' => $gcresult['X1'],
  548.             'X2' => $gcresult['X2'],
  549.             'Y1' => $gcresult['Y1'],
  550.             'Y2' => $gcresult['Y2'],
  551.             );
  552.         $driver_nearby = $this->model_goexpress->get_driver_nearby($dnparams);
  553. //        echo '1@@TEST@@';
  554. //var_dump($driver_nearby, $this->db->last_query());
  555. //error_log(json_encode($driver_nearby),$this->db->last_query());
  556. //die();
  557.  
  558.         $newpayload = json_decode($payload);
  559.         $newpayload->data->hp = $userrecord->hp;
  560.         $newpayload->data->office_phone = OFFICE_PHONE;
  561.         $newpayload->data->Fares = $json->data->Fares;
  562.         $newpayload->data->lang = $this->uri->segment(1);
  563.  
  564.         $payload = json_encode($newpayload);
  565.  
  566.         $data = array(
  567.             'txno' => $txno,
  568.             'payload' => $payload,
  569.             'client_id' => $client_id,
  570.             'tx_date' => date('Y-m-d H:i:s'),
  571.             'fares' => $json->data->Fares,
  572.             'payment' => $json->data->Payment,
  573.             'action_mode' => $json->data->Actionmode,
  574.             'promo_code' => $promo_code,
  575.             'ack_status_id' => 0,
  576.             'is_referal' => $referal,
  577.             'promo_deduction' => $promoValue,
  578.             'normal_price' => $normalPrice,
  579.             'comission' => $officeComission,
  580.             'comission_rate' => COMISSION_RATE
  581.         );
  582.  
  583.         if ( count($driver_nearby) == 0 ) {
  584.             $this->model_goexpress->save_tx_no_driver($data);
  585.             if ( $this->uri->segment(1) == "km") {
  586.                die("1@@សូមអភ័យទោស មិនមានអ្នកបើកបររបស់យើងនៅក្បែរលោកអ្នកនោះទេ។ សូមអរគុណដល់អតិថិជនទាំងឡាយដែលបានជួយគាំទ្រដល់ G0-X ។ សូមមេត្តាព្យាយាមម្ដងទៀត នៅខណៈពេលណាមួយ។");
  587.             }
  588.             else {
  589.                die("1@@Sorry, our driver aren't near you. \nThank you to keep support us by retrying after a few moment");
  590.             }
  591.         }
  592.  
  593.         list($flag, $id, $msg ) = $this->model_goexpress->add_tx($data);
  594.  
  595.         if ( ACTIVITY_RECORD == true ) {
  596.             $map_data = $json->map_data;
  597.             //var_dump($map_data);
  598.             if ( is_array($map_data) ) {
  599.                 $directions = array();
  600.                 for ( $directionno = 0; $directionno < count($map_data); $directionno++ ) {
  601.                     $mapDetail = json_decode($map_data[$directionno]);
  602.                     if ( isset($mapDetail->routes[0]->overview_polyline->points)) {
  603.                         $directions[$directionno] = $mapDetail->routes[0]->overview_polyline->points;
  604.                     }
  605.                     else if ( isset($mapDetail->routes->overview_polyline)) {
  606.                         $directions[$directionno] = $mapDetail->routes->overview_polyline;
  607.                     }
  608.                     else if ( isset($mapDetail->routes[0]->polyline->points) ) {
  609.                         $directions[$directionno] = $mapDetail->routes[0]->polyline->points;
  610.                     }
  611.                     else {
  612.  
  613.                     }
  614.                 }
  615.             }
  616.  
  617.  
  618.             $txDetail= array ( "txno" => $data["txno"],
  619.                                "start" => $json->data->Start,
  620.                                "target" => $json->data->Target,
  621.                                "direction" => $directions,
  622.                                "client_username" => $userrecord->username );
  623.             $activityDetail = array ( "type" => "tx_add",
  624.                                       "tx" => $txDetail);
  625.  
  626.             $this->model_goexpress->add_activity(json_encode($activityDetail));
  627.  
  628.         }
  629.  
  630.         if( $flag )
  631.         {
  632.             echo '0@@'.$txno.'@@';
  633.             $this->db->trans_commit();
  634.         }
  635.         else
  636.         {
  637.             echo '1@@'.$msg.'@@';
  638.             $this->db->trans_rollback();
  639.         }
  640.  
  641.         // ******* Update TX_EMAIL ****** //
  642.         if($flag)
  643.         {
  644.             $txeparams = array(
  645.                 'txno' => $txno,
  646.                 'mail_type' => 1,
  647.                 'tx_date' => $data['tx_date'],
  648.                 'mail_status' => 99,
  649.                 );
  650.  
  651.             list($flag, $id, $msg) = $this->_update_tx_email($txeparams);
  652.  
  653.             if(!$flag)
  654.             {
  655.                 log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'TX_EMAIL with ID:'.$id.' '.$msg, $txeparams));
  656.             }
  657.         }
  658.         // ********* END TX_EMAIL ********* //
  659.  
  660.         $jTarget = $this->_process_jsontarget( $json );
  661.  
  662.         $start_detail = sprintf("%s\n*) %s", $json->data->Start->Name, $json->data->Start->Note);
  663.         $target_detail = sprintf("%s\n*) %s", $jTarget->Name, $jTarget->Note);
  664.  
  665.         $token_driver = array();
  666.         $this->bc_action = "First Broadcast";
  667.         $this->bc_txno = $txno;
  668.         $token_driver = $this->filter_driver($driver_nearby);
  669.  
  670.         $data = array(
  671.             "Actionmode" => $json->data->Actionmode,
  672.             "PromoCode"  => $promo_code,
  673.             "Vehicle"    => $json->data->Vehicle,
  674.             "TxNo"       => $txno,
  675.             "Distance"   => $json->data->DistanceTotalText,
  676.             "Duration"   => $json->data->DurationTotalText,
  677.             "Fares"      => $json->data->Fares,
  678.             "Payment"    => $json->data->Payment,
  679.             "From"       => $start_detail,
  680.             "To"         => $target_detail,
  681.             "SToken"     => $json->data->SToken,
  682.             "StartLat"   => $json->data->Start->Lat,
  683.             "StartLng"   => $json->data->Start->Lng,
  684.             "TargetLat"  => $jTarget->Lat,
  685.             "TargetLng"  => $jTarget->Lng,
  686.             "Hp"         => $userrecord->hp,
  687.             "NormalFare" => $normalPrice,
  688.             "PromoDeduction" => $promoValue,
  689.             "Timestamp"  => number_format($this->current_millis(),0,'',''),
  690.         );
  691.  
  692.         if( !empty($token_driver) )
  693.             $this->sendFirebaseCloudMessage(  $data, $ids = array() , array('separate' => True, 'registration_ids' => $token_driver ));
  694.  
  695.         die();
  696.     }
  697.  
  698.     function tx_list()
  699.     {
  700.         $this->gox_acl->validate_read();
  701.         $user_info = $this->session->userdata('user_info');
  702.  
  703.         if( !isset($user_info->username))
  704.             show_404();
  705.  
  706.         $data = array();
  707.  
  708.         if( isset($_GET['status']) and $_GET['status'] == 'ALL')
  709.         { /* Do nothing, since null will query all*/ }
  710.         elseif( isset($_GET['status']) and $_GET['status'] != '')
  711.             $params['status_id']  = array($_GET['status']) ;
  712.         if($this->input->get('view') == 'P') {
  713.             $this->linkPage  = site_url("goexpress/tx_list/");
  714.             $this->segment   = 3;
  715.             $this->perPage   = 20;
  716.             $params['offset']= $this->start_page($this->segment);
  717.             $params['limit'] = $this->perPage;
  718.             $this->totalPage = $this->model_goexpress->get_tx($params, true);
  719.         }
  720.         $params['from']     = isset($_GET['from']) ? to_date($this->input->get('from')) : date('Y-m-01');
  721.         $params['to']       = isset($_GET['to']) ? to_date($this->input->get('to')) : date('Y-m-t');
  722.         $params['driver']   = trim($this->input->get('driver'));
  723.         $params['client']   = trim($this->input->get('client'));
  724.         $params['txno']     = trim($this->input->get('txno'));
  725.  
  726.         $data['records']    = $this->model_goexpress->get_tx($params, false);
  727.         $data['paging']     = $this->pagination();
  728.         $data['params']     = $params;
  729.         $this->template->write_view('content','goexpress/tx_list',$data);
  730.         $this->template->render();
  731.     }
  732.  
  733.     function tx_detail($id)
  734.     {
  735.         $user_info = $this->session->userdata('user_info');
  736.  
  737.         if( !isset($user_info->username))
  738.             show_404();
  739.  
  740.         $data = array();
  741.         $data['records'] = $this->model_goexpress->get_tx_detail(array('txno'=>$id));
  742.         $this->load->view('goexpress/tx_detail', $data);
  743.     }
  744.  
  745.     function client_profile($id)
  746.     {
  747.         $user_info = $this->session->userdata('user_info');
  748.  
  749.         if( !isset($user_info->username))
  750.             show_404();
  751.  
  752.         $data = array();
  753.         $data['records'] = $this->model_goexpress->get_client(array('client_id'=>$id));
  754.         $this->load->view('goexpress/client_detail', $data);
  755.     }
  756.  
  757.     function tx_tracking($id)
  758.     {
  759.         $data = array();
  760.         $data['records'] = $this->model_goexpress->get_tx_detail(array('txno'=>$id));
  761.         $this->load->view('goexpress/tx_tracking', $data);
  762.     }
  763.  
  764.     function tx_history($id)
  765.     {
  766.         $data = array();
  767.         $data['records'] = $this->model_goexpress->get_tx_all(array('driver_id'=>$id));
  768.         $this->load->view('goexpress/tx_history', $data);
  769.     }
  770.  
  771.     function tx_list_dummy()
  772.     {
  773.         $data = array();
  774.  
  775.         $_POST['order_by'] = 'id desc';
  776.         $data['records'] = $this->model_goexpress->get_tx($this->input->post());
  777.  
  778.         $this->template->write_view('content','goexpress/tx_list',$data);
  779.         $this->template->render();
  780.     }
  781.  
  782.     /*BAP*/
  783.     function tx_list_two()
  784.     {
  785.         $user_info = $this->session->userdata('user_info');
  786.  
  787.         if( !isset($user_info->username))
  788.             show_404();
  789.  
  790.         $data = array();
  791.  
  792.         if( isset($_GET['status']) and $_GET['status'] == 'ALL')
  793.         { /* Do nothing, since null will query all*/ }
  794.         elseif( isset($_GET['status']) and $_GET['status'] != '')
  795.             $params['status_id']  = array($_GET['status']) ;
  796.         if($this->input->get('view') == 'P') {
  797.             $this->linkPage  = site_url("goexpress/tx_list_two/");
  798.             $this->segment   = 3;
  799.             $this->perPage   = 20;
  800.             $params['offset']= $this->start_page($this->segment);
  801.             $params['limit'] = $this->perPage;
  802.             $this->totalPage = $this->model_goexpress->get_tx_two($params, true);
  803.         }
  804.         $params['from']     = isset($_GET['from']) ? to_date($this->input->get('from')) : date('Y-m-01');
  805.         $params['to']       = isset($_GET['to']) ? to_date($this->input->get('to')) : date('Y-m-t');
  806.         $params['driver']   = trim($this->input->get('driver'));
  807.         $params['client']   = trim($this->input->get('client'));
  808.         $params['txno']     = trim($this->input->get('txno'));
  809.  
  810.         $data['records']    = $this->model_goexpress->get_tx_two($params, false);
  811.         $data['paging']     = $this->pagination();
  812.         $data['params']     = $params;
  813.         $this->template->write_view('content','goexpress/tx_list_two',$data);
  814.         $this->template->render();
  815.     }
  816.     /*BAP*/
  817.  
  818.     function gcm_send_json($txno = null)
  819.     {
  820.         $RANGE = THIRD_BC_RANGE * COORD_IN_KM; // in Km.
  821.         $UPTIME = UPTIME; // in Minutes.
  822.         $user_info = $this->session->userdata('user_info');
  823.         $USERTYPE_DRIVER = 50;
  824.  
  825.         if( !isset($user_info->username))
  826.             show_404();
  827.  
  828.         if(is_null($txno))
  829.         {
  830.             $resjson = array('error' => 1,'error_message' => lang('ErrorIdIsNull'));
  831.             die(json_encode($resjson));
  832.         }
  833.  
  834.         $OPEN = 0;
  835.         //------------------------------
  836.         // Payload data you want to send
  837.         // to Android device (will be
  838.         // accessible via intent extras)
  839.         //------------------------------
  840.         $tparams = array('txno'=> $txno);
  841.         $record = $this->model_goexpress->get_tx($tparams);
  842.         if( empty($record) )
  843.         {
  844.             $resjson = array('error' => 1,'error_message' => lang('ErrorRecordEmpty'));
  845.             die(json_encode($resjson));
  846.         }
  847.         $record = $record[0];
  848.  
  849.         if( !isset($record->payload) )
  850.         {
  851.             $resjson = array('error' => 1,'error_message' => lang('ErrorPayloadEmpty'));
  852.             die(json_encode($resjson));
  853.         }
  854.  
  855.         $json = json_decode( $record->payload );
  856.  
  857.         $jTarget = $this->_process_jsontarget( $json );
  858.  
  859.         $start_detail = sprintf("%s\n*) %s", $json->data->Start->Name, $json->data->Start->Note);
  860.         $target_detail = sprintf("%s\n*) %s", $jTarget->Name, $jTarget->Note);
  861.  
  862.         $promo_code = "";
  863.         if( isset($json->data->PromoCode) and $json->data->PromoCode != "")
  864.             $promo_code = $json->data->PromoCode;
  865.  
  866.         $bBc_all = true;
  867.         $token_driver = array();
  868.  
  869.         if( $json->data->SToken == OFFICE_LOGIN )
  870.         {
  871.             $office_tx = $this->office_model->get_order(array("txno"=> $txno ));
  872.             if( isset($office_tx->driver_stoken  ) and $office_tx->driver_stoken != "" )
  873.             {
  874.                 $token_driver[] = $office_tx->driver_stoken;
  875.                 $bBc_all = false;
  876.             }
  877.         }
  878.  
  879.         if( $bBc_all )
  880.         {
  881.             $gcparams = array(
  882.                 'range' => $RANGE,
  883.                 'uptime' => $UPTIME,
  884.                 'client_lat' => $json->data->Start->Lat,
  885.                 'client_lng' => $json->data->Start->Lng,
  886.                 );
  887.  
  888.             $gcresult = $this->_get_coord_nearby($gcparams);
  889.  
  890.             $dnparams = array(
  891.                 'user_type' => $USERTYPE_DRIVER,
  892.                 'transport' => $json->data->Vehicle,
  893.                 'last_up_time' => $gcresult['last_up_time'],
  894.                 'X1' => $gcresult['X1'],
  895.                 'X2' => $gcresult['X2'],
  896.                 'Y1' => $gcresult['Y1'],
  897.                 'Y2' => $gcresult['Y2'],
  898.                 );
  899.             $driver_nearby = $this->model_goexpress->get_driver_nearby($dnparams);
  900.  
  901.             $token_driver = $this->filter_driver($driver_nearby);
  902.         }
  903.  
  904.         $data = array(
  905.             "Actionmode" => $json->data->Actionmode,
  906.             "PromoCode"  => $promo_code,
  907.             "Vehicle"    => $json->data->Vehicle,
  908.             "TxNo"       => $txno,
  909.             "Distance"   => $json->data->DistanceTotalText,
  910.             "Duration"   => $json->data->DurationTotalText,
  911.             "Fares"      => $json->data->Fares,
  912.             "Payment"    => $json->data->Payment,
  913.             "From"       => $start_detail,
  914.             "To"         => $target_detail,
  915.             "SToken"     => $json->data->SToken,
  916.             "StartLat"   => $json->data->Start->Lat,
  917.             "StartLng"   => $json->data->Start->Lng,
  918.             "TargetLat"  => $jTarget->Lat,
  919.             "TargetLng"  => $jTarget->Lng,
  920.             "Timestamp"  => number_format($this->current_millis(),0,'',''),
  921.         );
  922.         //------------------------------
  923.         // The recipient registration IDs
  924.         // that will receive the push
  925.         // (Should be stored in your DB)
  926.         //
  927.         // Read about it here:
  928.         // http://developer.android.com/google/gcm/
  929.         //------------------------------
  930.         $ids = array( 'abc', 'def' );
  931.  
  932.         //------------------------------
  933.         // Call our custom GCM function
  934.         //------------------------------
  935.  
  936.         if( !empty($token_driver))
  937.             $bresult = $this->sendFirebaseCloudMessage(  $data, $ids, array('registration_ids' => $token_driver ), array('ret_int' => true));
  938.         else
  939.             $bresult = false;
  940.  
  941.         if( $bresult )
  942.         {
  943.             $resjson = array('error' => 0, 'error_message' => 'Success');
  944.         }
  945.         else
  946.         {
  947.             $resjson = array('error' => 1, 'error_message' => 'Error: BC to Drivers are failed');
  948.         }
  949.  
  950.         die(json_encode($resjson));
  951.     }
  952.  
  953.     function gcm_send($txno = null)
  954.     {
  955.         $user_info = $this->session->userdata('user_info');
  956.  
  957.         if( !isset($user_info->username))
  958.             show_404();
  959.  
  960.         $OPEN = 0;
  961.         //------------------------------
  962.         // Payload data you want to send
  963.         // to Android device (will be
  964.         // accessible via intent extras)
  965.         //------------------------------
  966.         if( $txno != null )
  967.         {
  968.             $tparams = array('txno'=>$txno);
  969.         }
  970.         else
  971.         {
  972.             $tparams = array(
  973.                 'limit'=>1,
  974.                 'status_id' => $OPEN,
  975.                 'order_by' => 'id desc',
  976.             );
  977.         }
  978.  
  979.         $record = $this->model_goexpress->get_tx($tparams);
  980.  
  981.         if( empty($record) )
  982.         {
  983.             die('1@@No Tx');
  984.         }
  985.         $record = $record[0];
  986.  
  987.         if( !isset($record->payload) )
  988.         {
  989.             die('1@@No Payload');
  990.         }
  991.  
  992.         $json = json_decode( $record->payload );
  993.         $jTarget = $this->_process_jsontarget( $json );
  994.  
  995.         $start_detail = sprintf("%s\n*) %s", $json->data->Start->Name, $json->data->Start->Note);
  996.         $target_detail = sprintf("%s\n*) %s", $jTarget->Name, $jTarget->Note);
  997.  
  998.         $promo_code = "";
  999.         if( isset($json->data->PromoCode) and $json->data->PromoCode != "")
  1000.             $promo_code = $json->data->PromoCode;
  1001.  
  1002.         $data = array(
  1003.             "Actionmode" => $json->data->Actionmode,
  1004.             "PromoCode"  => $promo_code,
  1005.             "Vehicle"    => $json->data->Vehicle,
  1006.             "TxNo"       => $record->txno,
  1007.             "Distance"   => $json->data->DistanceTotalText,
  1008.             "Duration"   => $json->data->DurationTotalText,
  1009.             "Fares"      => $json->data->Fares,
  1010.             "Payment"    => $json->data->Payment,
  1011.             "From"       => $start_detail,
  1012.             "To"         => $target_detail,
  1013.             "SToken"     => $json->data->SToken,
  1014.             "StartLat"   => $json->data->Start->Lat,
  1015.             "StartLng"   => $json->data->Start->Lng,
  1016.             "TargetLat"  => $jTarget->Lat,
  1017.             "TargetLng"  => $jTarget->Lng,
  1018.             "Timestamp"  => number_format($this->current_millis(),0,'',''),
  1019.         );
  1020.  
  1021.         //------------------------------
  1022.         // The recipient registration IDs
  1023.         // that will receive the push
  1024.         // (Should be stored in your DB)
  1025.         //
  1026.         // Read about it here:
  1027.         // http://developer.android.com/google/gcm/
  1028.         //------------------------------
  1029.         $ids = array( 'abc', 'def' );
  1030.  
  1031.         //------------------------------
  1032.         // Call our custom GCM function
  1033.         //------------------------------
  1034.  
  1035.         $bresult = $this->sendFirebaseCloudMessage(  $data, $ids, null, array('stoken'=>'fxsN86pecxA:APA91bGbIFQYOWD26w44Ws3hytx6fSBnSZr9r4MmvHnrShDwv3dgcW8wgM8frU4a7Iz12RF9GiIuIzD2hOV-mBClK1k8cDpb8MLcKySAngGxOwKbLjlof8Fpcpeyr9JOX0Bhser18lAq'));
  1036.  
  1037.         var_dump($bresult);
  1038.     }
  1039.  
  1040.     function send_data_fcm($token)
  1041.     {
  1042.         $user_info = $this->session->userdata('user_info');
  1043.  
  1044.         if( !isset($user_info->username))
  1045.             show_404();
  1046.  
  1047.         $data = array(
  1048.             "test" => "Hello GoX",
  1049.         );
  1050.  
  1051.         $bresult = $this->sendFirebaseCloudMessage(  $data, null, null, array('stoken'=> $token, 'separate' => true, 'ret_int' => 1) );
  1052.  
  1053.         var_dump($bresult);
  1054.     }
  1055.  
  1056.     //------------------------------
  1057.     // Define custom GCM function
  1058.     //------------------------------
  1059.     function sendGoogleCloudMessage( $data, $ids, $params = array(), $options = array() )
  1060.     {
  1061.         //------------------------------
  1062.         // Replace with real GCM API
  1063.         // key from Google APIs Console
  1064.         //
  1065.         // https://code.google.com/apis/console/
  1066.         //------------------------------
  1067.  
  1068.         $apiKey = 'AIzaSyAMC9GU3zMDCOf8r2kZvVfJMhbGrh2iJTM';
  1069.  
  1070.         //------------------------------
  1071.         // Define URL to GCM endpoint
  1072.         //------------------------------
  1073.  
  1074.         $url = 'https://android.googleapis.com/gcm/send';
  1075.  
  1076.         //------------------------------
  1077.         // Set GCM post variables
  1078.         // (Device IDs and push payload)
  1079.         //------------------------------
  1080.  
  1081.         $post = array(
  1082.                     'data' => $data,
  1083.                 );
  1084.  
  1085.         if( isset( $params['registration_ids'] ) )
  1086.         {
  1087.             $post['registration_ids'] = $params['registration_ids'];
  1088.         }
  1089.         else if( isset( $options['stoken'] ) )
  1090.         {
  1091.             $post['to'] = $options["stoken"];
  1092.         }
  1093.         else
  1094.         {
  1095.             if( isset($data['Vehicle'] ))
  1096.                 $post['to'] = "/topics/" . $data['Vehicle'];
  1097.             else
  1098.                 $post['to'] = "/topics/goexglobal";
  1099.         }
  1100.  
  1101.         //------------------------------
  1102.         // Set CURL request headers
  1103.         // (Authentication and type)
  1104.         //------------------------------
  1105.  
  1106.         $headers = array(
  1107.             'Authorization: key=' . $apiKey,
  1108.             'Content-Type: application/json'
  1109.         );
  1110.  
  1111.         //------------------------------
  1112.         // Initialize curl handle
  1113.         //------------------------------
  1114.  
  1115.         $ch = curl_init();
  1116.  
  1117.         //------------------------------
  1118.         // Set URL to GCM endpoint
  1119.         //------------------------------
  1120.  
  1121.         curl_setopt( $ch, CURLOPT_URL, $url );
  1122.  
  1123.         //------------------------------
  1124.         // Set request method to POST
  1125.         //------------------------------
  1126.  
  1127.         curl_setopt( $ch, CURLOPT_POST, true );
  1128.  
  1129.         //------------------------------
  1130.         // Set our custom headers
  1131.         //------------------------------
  1132.  
  1133.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  1134.  
  1135.         //------------------------------
  1136.         // Get the response back as
  1137.         // string instead of printing it
  1138.         //------------------------------
  1139.  
  1140.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1141.  
  1142.         //------------------------------
  1143.         // Set post data as JSON
  1144.         //------------------------------
  1145.  
  1146.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  1147.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  1148.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1149.         //------------------------------
  1150.         // Actually send the push!
  1151.         //------------------------------
  1152.  
  1153.         $result = curl_exec( $ch );
  1154.  
  1155.         //------------------------------
  1156.         // Error? Display it!
  1157.         //------------------------------
  1158.  
  1159.         if ( curl_errno( $ch ) )
  1160.         {
  1161.             echo '1@@GCM error: ' . curl_error( $ch );
  1162.         }
  1163.  
  1164.         //------------------------------
  1165.         // Close curl handle
  1166.         //------------------------------
  1167.  
  1168.         curl_close( $ch );
  1169.  
  1170.         //------------------------------
  1171.         // Debug GCM response
  1172.         //------------------------------
  1173.         if( isset($options['ret_int']) )
  1174.             return $result;
  1175.  
  1176.         if( isset($params['separate']))
  1177.             echo '@@0@@'.$data['TxNo'].'@@'.$result;
  1178.         else
  1179.             echo '0@@'.$data['TxNo'].'@@'.$result;
  1180.     }
  1181.  
  1182.     function sendFirebaseCloudMessage( $data, $ids, $params = array(), $options = array() )
  1183.     {
  1184.         //------------------------------
  1185.         // Replace with real GCM API
  1186.         // key from Google APIs Console
  1187.         //
  1188.         // https://code.google.com/apis/console/
  1189.         //------------------------------
  1190.         // if ( $this->is_firebase == false ) {
  1191.         //     return $this->sendGoogleCloudMessage($data,$ids,$params,$options);
  1192.         // }
  1193.  
  1194.  
  1195.         $apiKey = 'AIzaSyAFUpRaVZCEcOrAZy-jc2iz_OaAAWEjgd4';
  1196.  
  1197.         //------------------------------
  1198.         // Define URL to GCM endpoint
  1199.         //------------------------------
  1200.  
  1201.         $url = 'https://fcm.googleapis.com/fcm/send';
  1202.  
  1203.         //------------------------------
  1204.         // Set GCM post variables
  1205.         // (Device IDs and push payload)
  1206.         //------------------------------
  1207.  
  1208.         $post = array(
  1209.                     'data' => $data,
  1210.                 );
  1211.  
  1212.         if( isset( $params['registration_ids'] ) )
  1213.         {
  1214.             $post['registration_ids'] = $params['registration_ids'];
  1215.         }
  1216.         else if( isset( $options['stoken'] ) )
  1217.         {
  1218.             $post['to'] = $options["stoken"];
  1219.         }
  1220.         else
  1221.         {
  1222.             // @TODO REMOVE THIS VALIDATION SINCE redundant
  1223.             // and already filtered at registration_ids filter
  1224.             if( isset($data['Vehicle'] ))
  1225.                 $post['to'] = "/topics/" . $data['Vehicle'];
  1226.             else
  1227.                 $post['to'] = "/topics/goexglobal";
  1228.         }
  1229.  
  1230.         query_log("Post Data " . json_encode($post));
  1231.  
  1232.         //------------------------------
  1233.         // Set CURL request headers
  1234.         // (Authentication and type)
  1235.         //------------------------------
  1236.  
  1237.         $headers = array(
  1238.             'Authorization: key=' . $apiKey,
  1239.             'Content-Type: application/json'
  1240.         );
  1241.  
  1242.         //------------------------------
  1243.         // Initialize curl handle
  1244.         //------------------------------
  1245.  
  1246.         $ch = curl_init();
  1247.  
  1248.         //------------------------------
  1249.         // Set URL to GCM endpoint
  1250.         //------------------------------
  1251.  
  1252.         curl_setopt( $ch, CURLOPT_URL, $url );
  1253.  
  1254.         //------------------------------
  1255.         // Set request method to POST
  1256.         //------------------------------
  1257.  
  1258.         curl_setopt( $ch, CURLOPT_POST, true );
  1259.  
  1260.         //------------------------------
  1261.         // Set our custom headers
  1262.         //------------------------------
  1263.  
  1264.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  1265.  
  1266.         //------------------------------
  1267.         // Get the response back as
  1268.         // string instead of printing it
  1269.         //------------------------------
  1270.  
  1271.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1272.  
  1273.         //------------------------------
  1274.         // Set post data as JSON
  1275.         //------------------------------
  1276.  
  1277.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  1278.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  1279.  
  1280.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1281.  
  1282.         //------------------------------
  1283.         // Actually send the push!
  1284.         //------------------------------
  1285.  
  1286.         $result = curl_exec( $ch );
  1287.  
  1288.         //------------------------------
  1289.         // Error? Display it!
  1290.         //------------------------------
  1291.  
  1292.         if ( curl_errno( $ch ) )
  1293.         {
  1294.             if( isset($options['ret_int']) and $options['ret_int'] === True )
  1295.                 error_log('1@@FCM error: ' . curl_error( $ch ));
  1296.             else
  1297.                 echo '1@@FCM error: ' . curl_error( $ch );
  1298.             // set a flag
  1299.         }
  1300.  
  1301.         //------------------------------
  1302.         // Close curl handle
  1303.         //------------------------------
  1304.  
  1305.         curl_close( $ch );
  1306.  
  1307.         //------------------------------
  1308.         // Debug GCM response
  1309.         //------------------------------
  1310.         //if( @$result->results[0]->error == "MissmatchSenderId")
  1311.         //    return $this->sendFirebaseCloudMessageIOS($data, $ids, $params, $options);
  1312.  
  1313.         $this->sendFirebaseCloudMessageIOS($data, $ids, $params, $options);
  1314.  
  1315.         if( isset($options['ret_int']) )
  1316.         {
  1317.             //error_log("BC is ". $result);
  1318.             return $result;
  1319.         }
  1320.  
  1321.         if( isset($params['separate']))
  1322.             echo '@@0@@'.$data['TxNo'].'@@'.$result;
  1323.         else
  1324.             echo '0@@'.$data['TxNo'].'@@'.$result;
  1325.     }
  1326.  
  1327.     function sendFirebaseCloudMessageIOS( $data, $ids, $params = array(), $options = array() )
  1328.     {
  1329.         //------------------------------
  1330.         // Replace with real GCM API
  1331.         // key from Google APIs Console
  1332.         //
  1333.         // https://code.google.com/apis/console/
  1334.         //------------------------------
  1335.         // if ( $this->is_firebase == false ) {
  1336.         //     return $this->sendGoogleCloudMessage($data,$ids,$params,$options);
  1337.         // }
  1338.  
  1339.  
  1340.         //$apiKey = 'AIzaSyBBUQNwpcJCk5wnqP_Z9gX49w8UWLevc_4';
  1341.         $apiKey = 'AIzaSyA7GZ8In7gav2nD0wDejYRWU2zOxCqCQWI';
  1342.  
  1343.         //------------------------------
  1344.         // Define URL to GCM endpoint
  1345.         //------------------------------
  1346.  
  1347.         $url = 'https://fcm.googleapis.com/fcm/send';
  1348.  
  1349.         //------------------------------
  1350.         // Set GCM post variables
  1351.         // (Device IDs and push payload)
  1352.         //------------------------------
  1353.  
  1354.         $post = array(
  1355.                     'data' => $data,
  1356.                 );
  1357.  
  1358.         if( isset( $params['registration_ids'] ) )
  1359.         {
  1360.             $post['registration_ids'] = $params['registration_ids'];
  1361.         }
  1362.         else if( isset( $options['stoken'] ) )
  1363.         {
  1364.             $post['to'] = $options["stoken"];
  1365.         }
  1366.         else
  1367.         {
  1368.             // @TODO REMOVE THIS VALIDATION SINCE redundant
  1369.             // and already filtered at registration_ids filter
  1370.             if( isset($data['Vehicle'] ))
  1371.                 $post['to'] = "/topics/" . $data['Vehicle'];
  1372.             else
  1373.                 $post['to'] = "/topics/goexglobal";
  1374.         }
  1375.  
  1376.         query_log("Post Data " . json_encode($post));
  1377.  
  1378.         //------------------------------
  1379.         // Set CURL request headers
  1380.         // (Authentication and type)
  1381.         //------------------------------
  1382.  
  1383.         $headers = array(
  1384.             'Authorization: key=' . $apiKey,
  1385.             'Content-Type: application/json'
  1386.         );
  1387.  
  1388.         //------------------------------
  1389.         // Initialize curl handle
  1390.         //------------------------------
  1391.  
  1392.         $ch = curl_init();
  1393.  
  1394.         //------------------------------
  1395.         // Set URL to GCM endpoint
  1396.         //------------------------------
  1397.  
  1398.         curl_setopt( $ch, CURLOPT_URL, $url );
  1399.  
  1400.         //------------------------------
  1401.         // Set request method to POST
  1402.         //------------------------------
  1403.  
  1404.         curl_setopt( $ch, CURLOPT_POST, true );
  1405.  
  1406.         //------------------------------
  1407.         // Set our custom headers
  1408.         //------------------------------
  1409.  
  1410.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  1411.  
  1412.         //------------------------------
  1413.         // Get the response back as
  1414.         // string instead of printing it
  1415.         //------------------------------
  1416.  
  1417.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1418.  
  1419.         //------------------------------
  1420.         // Set post data as JSON
  1421.         //------------------------------
  1422.  
  1423.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  1424.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  1425.  
  1426.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1427.  
  1428.         //------------------------------
  1429.         // Actually send the push!
  1430.         //------------------------------
  1431.  
  1432.         $result = curl_exec( $ch );
  1433.  
  1434.         //------------------------------
  1435.         // Error? Display it!
  1436.         //------------------------------
  1437.  
  1438.         if ( curl_errno( $ch ) )
  1439.         {
  1440.             error_log('@@1@@FCM error: ' . curl_error( $ch ));
  1441.         }
  1442.  
  1443.         //------------------------------
  1444.         // Close curl handle
  1445.         //------------------------------
  1446.  
  1447.         curl_close( $ch );
  1448.  
  1449.         //------------------------------
  1450.         // Debug GCM response
  1451.         //------------------------------
  1452.  
  1453.         $this->sendFirebaseCloudMessageIOS3($data, $ids, $params, $options);
  1454.  
  1455.         if( isset($options['ret_int']) )
  1456.             return $result;
  1457.  
  1458.         if( isset($params['separate']))
  1459.             echo '@@0@@'.$data['TxNo'].'@@'.$result;
  1460.         else
  1461.             echo '0@@'.$data['TxNo'].'@@'.$result;
  1462.     }
  1463.  
  1464.     function sendFirebaseCloudMessageIOS3( $data, $ids, $params = array(), $options = array() )
  1465.     {
  1466.         //------------------------------
  1467.         // Replace with real GCM API
  1468.         // key from Google APIs Console
  1469.         //
  1470.         // https://code.google.com/apis/console/
  1471.         //------------------------------
  1472.         // if ( $this->is_firebase == false ) {
  1473.         //     return $this->sendGoogleCloudMessage($data,$ids,$params,$options);
  1474.         // }
  1475.  
  1476.         // ade dashboard
  1477.         $apiKey = 'AIzaSyBBUQNwpcJCk5wnqP_Z9gX49w8UWLevc_4';
  1478.  
  1479.         //------------------------------
  1480.         // Define URL to GCM endpoint
  1481.         //------------------------------
  1482.  
  1483.         $url = 'https://fcm.googleapis.com/fcm/send';
  1484.  
  1485.         //------------------------------
  1486.         // Set GCM post variables
  1487.         // (Device IDs and push payload)
  1488.         //------------------------------
  1489.  
  1490.         $post = array(
  1491.                     'data' => $data,
  1492.                 );
  1493.  
  1494.         if( isset( $params['registration_ids'] ) )
  1495.         {
  1496.             $post['registration_ids'] = $params['registration_ids'];
  1497.         }
  1498.         else if( isset( $options['stoken'] ) )
  1499.         {
  1500.             $post['to'] = $options["stoken"];
  1501.         }
  1502.         else
  1503.         {
  1504.             // @TODO REMOVE THIS VALIDATION SINCE redundant
  1505.             // and already filtered at registration_ids filter
  1506.             if( isset($data['Vehicle'] ))
  1507.                 $post['to'] = "/topics/" . $data['Vehicle'];
  1508.             else
  1509.                 $post['to'] = "/topics/goexglobal";
  1510.         }
  1511.  
  1512.         query_log("Post Data " . json_encode($post));
  1513.  
  1514.         //------------------------------
  1515.         // Set CURL request headers
  1516.         // (Authentication and type)
  1517.         //------------------------------
  1518.  
  1519.         $headers = array(
  1520.             'Authorization: key=' . $apiKey,
  1521.             'Content-Type: application/json'
  1522.         );
  1523.  
  1524.         //------------------------------
  1525.         // Initialize curl handle
  1526.         //------------------------------
  1527.  
  1528.         $ch = curl_init();
  1529.  
  1530.         //------------------------------
  1531.         // Set URL to GCM endpoint
  1532.         //------------------------------
  1533.  
  1534.         curl_setopt( $ch, CURLOPT_URL, $url );
  1535.  
  1536.         //------------------------------
  1537.         // Set request method to POST
  1538.         //------------------------------
  1539.  
  1540.         curl_setopt( $ch, CURLOPT_POST, true );
  1541.  
  1542.         //------------------------------
  1543.         // Set our custom headers
  1544.         //------------------------------
  1545.  
  1546.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  1547.  
  1548.         //------------------------------
  1549.         // Get the response back as
  1550.         // string instead of printing it
  1551.         //------------------------------
  1552.  
  1553.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1554.  
  1555.         //------------------------------
  1556.         // Set post data as JSON
  1557.         //------------------------------
  1558.  
  1559.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  1560.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  1561.  
  1562.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1563.  
  1564.         //------------------------------
  1565.         // Actually send the push!
  1566.         //------------------------------
  1567.  
  1568.         $result = curl_exec( $ch );
  1569.  
  1570.         //------------------------------
  1571.         // Error? Display it!
  1572.         //------------------------------
  1573.  
  1574.         if ( curl_errno( $ch ) )
  1575.         {
  1576.             error_log('@@1@@FCM error: ' . curl_error( $ch ));
  1577.         }
  1578.  
  1579.         //------------------------------
  1580.         // Close curl handle
  1581.         //------------------------------
  1582.  
  1583.         curl_close( $ch );
  1584.  
  1585.         //------------------------------
  1586.         // Debug GCM response
  1587.         //------------------------------
  1588.  
  1589.         if( isset($options['ret_int']) )
  1590.             return $result;
  1591.  
  1592.         if( isset($params['separate']))
  1593.             echo '@@0@@'.$data['TxNo'].'@@'.$result;
  1594.         else
  1595.             echo '0@@'.$data['TxNo'].'@@'.$result;
  1596.     }
  1597.  
  1598.     function fcm_manual( )
  1599.     {
  1600.         //------------------------------
  1601.         // Replace with real GCM API
  1602.         // key from Google APIs Console
  1603.         //
  1604.         // https://code.google.com/apis/console/
  1605.         //------------------------------
  1606.         $apiKey = 'AIzaSyAFUpRaVZCEcOrAZy-jc2iz_OaAAWEjgd4';
  1607.  
  1608.         //------------------------------
  1609.         // Define URL to GCM endpoint
  1610.         //------------------------------
  1611.  
  1612.         $url = 'https://fcm.googleapis.com/fcm/send';
  1613.  
  1614.         //------------------------------
  1615.         // Set GCM post variables
  1616.         // (Device IDs and push payload)
  1617.         //------------------------------
  1618.  
  1619.  
  1620.         $notification = array(
  1621.                         "title" => "DRIVER MEET",
  1622.                         "body" => "test BOS",
  1623.                         "sound" => "default");
  1624.  
  1625.         $data = array(
  1626.                         "AA" => "BB");
  1627.         $post = array(
  1628.                     'data'         => $data,
  1629.                     'notification' => $notification
  1630.                 );
  1631.  
  1632.  
  1633.         $post['to'] = "feWQXuGLlD8:APA91bEnc8IfCwA6DsP8Uz1bXaIIWx3-BYfXB9yEYaabCTX7uOSrzLq8z9KYQkjPsMhETtC-LVEEihACP_SwkWNZMMGmuy6c7QdfQFKvrrxn4HshWqdDXRTf4vgrXkcOLN599FvFyk2F";
  1634.  
  1635.  
  1636.         //------------------------------
  1637.         // Set CURL request headers
  1638.         // (Authentication and type)
  1639.         //------------------------------
  1640.  
  1641.         $headers = array(
  1642.             'Authorization: key=' . $apiKey,
  1643.             'Content-Type: application/json'
  1644.         );
  1645.  
  1646.         //------------------------------
  1647.         // Initialize curl handle
  1648.         //------------------------------
  1649.  
  1650.         $ch = curl_init();
  1651.  
  1652.         //------------------------------
  1653.         // Set URL to GCM endpoint
  1654.         //------------------------------
  1655.  
  1656.         curl_setopt( $ch, CURLOPT_URL, $url );
  1657.  
  1658.         //------------------------------
  1659.         // Set request method to POST
  1660.         //------------------------------
  1661.  
  1662.         curl_setopt( $ch, CURLOPT_POST, true );
  1663.  
  1664.         //------------------------------
  1665.         // Set our custom headers
  1666.         //------------------------------
  1667.  
  1668.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  1669.  
  1670.         //------------------------------
  1671.         // Get the response back as
  1672.         // string instead of printing it
  1673.         //------------------------------
  1674.  
  1675.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1676.  
  1677.         //------------------------------
  1678.         // Set post data as JSON
  1679.         //------------------------------
  1680.  
  1681.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  1682.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  1683.  
  1684.         //------------------------------
  1685.         // Actually send the push!
  1686.         //------------------------------
  1687.  
  1688.         $result = curl_exec( $ch );
  1689.  
  1690.         //------------------------------
  1691.         // Error? Display it!
  1692.         //------------------------------
  1693.  
  1694.         var_dump($result);
  1695.         if ( curl_errno( $ch ) )
  1696.         {
  1697.             return array(1, curl_error( $ch ), -1);
  1698.         }
  1699.  
  1700.         //------------------------------
  1701.         // Close curl handle
  1702.         //------------------------------
  1703.  
  1704.         curl_close( $ch );
  1705.  
  1706.  
  1707.         //------------------------------
  1708.         // Debug GCM response
  1709.         //------------------------------
  1710.         return array(0,'Success', $result);
  1711.     }
  1712.  
  1713.     function sendFirebaseCloudMessage2( $data, $params = array(), $notification = null )
  1714.     {
  1715.         //------------------------------
  1716.         // Replace with real GCM API
  1717.         // key from Google APIs Console
  1718.         //
  1719.         // https://code.google.com/apis/console/
  1720.         //------------------------------
  1721.         $apiKey = 'AIzaSyAFUpRaVZCEcOrAZy-jc2iz_OaAAWEjgd4';
  1722.  
  1723.         //------------------------------
  1724.         // Define URL to GCM endpoint
  1725.         //------------------------------
  1726.  
  1727.         $url = 'https://fcm.googleapis.com/fcm/send';
  1728.  
  1729.         //------------------------------
  1730.         // Set GCM post variables
  1731.         // (Device IDs and push payload)
  1732.         //------------------------------
  1733.  
  1734.         $post = array(
  1735.                     'data' => $data,
  1736.                 );
  1737.  
  1738.         if( isset( $params['registration_ids'] ) )
  1739.         {
  1740.             $post['registration_ids'] = $params['registration_ids'];
  1741.         }
  1742.         else if( isset( $params['stoken'] ) )
  1743.         {
  1744.             $post['to'] = $params["stoken"];
  1745.         }
  1746.         else
  1747.         {
  1748.             $post['to'] = "/topics/global";
  1749.         }
  1750.  
  1751.         if ( $notification != null )
  1752.         {
  1753.             $post['notification'] = $notification;
  1754.         }
  1755.         //------------------------------
  1756.         // Set CURL request headers
  1757.         // (Authentication and type)
  1758.         //------------------------------
  1759.  
  1760.  
  1761.         $headers = array(
  1762.             'Authorization: key=' . $apiKey,
  1763.             'Content-Type: application/json'
  1764.         );
  1765.  
  1766.         //------------------------------
  1767.         // Initialize curl handle
  1768.         //------------------------------
  1769.  
  1770.         $ch = curl_init();
  1771.  
  1772.         //------------------------------
  1773.         // Set URL to GCM endpoint
  1774.         //------------------------------
  1775.  
  1776.         curl_setopt( $ch, CURLOPT_URL, $url );
  1777.  
  1778.         //------------------------------
  1779.         // Set request method to POST
  1780.         //------------------------------
  1781.  
  1782.         curl_setopt( $ch, CURLOPT_POST, true );
  1783.  
  1784.         //------------------------------
  1785.         // Set our custom headers
  1786.         //------------------------------
  1787.  
  1788.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  1789.  
  1790.         //------------------------------
  1791.         // Get the response back as
  1792.         // string instead of printing it
  1793.         //------------------------------
  1794.  
  1795.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1796.  
  1797.         //------------------------------
  1798.         // Set post data as JSON
  1799.         //------------------------------
  1800.  
  1801.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  1802.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  1803.  
  1804.         //------------------------------
  1805.         // Actually send the push!
  1806.         //------------------------------
  1807.  
  1808.         $result = curl_exec( $ch );
  1809.  
  1810.         //------------------------------
  1811.         // Error? Display it!
  1812.         //------------------------------
  1813.  
  1814.         if ( curl_errno( $ch ) )
  1815.         {
  1816.             // This  is crazy, we are returning value as
  1817.             // we had promised but will never return ERROR = 1
  1818.             // since we are trying to trigger other FCM funcs
  1819.             error_log('FCM2 '.curl_error( $ch ));
  1820.             //return array(1, curl_error( $ch ), -1);
  1821.         }
  1822.  
  1823.         //------------------------------
  1824.         // Close curl handle
  1825.         //------------------------------
  1826.  
  1827.         curl_close( $ch );
  1828.  
  1829.         $this->sendFirebaseCloudMessageIOS2($data, $params, $notification);
  1830.  
  1831.         //------------------------------
  1832.         // Debug GCM response
  1833.         //------------------------------
  1834.         return array(0,'Success', $result);
  1835.     }
  1836.  
  1837.     function sendFirebaseCloudMessageIOS2( $data, $params = array(), $notification = null )
  1838.     {
  1839.         //------------------------------
  1840.         // Replace with real GCM API
  1841.         // key from Google APIs Console
  1842.         //
  1843.         // https://code.google.com/apis/console/
  1844.         //------------------------------
  1845.         //$apiKey = 'AIzaSyBBUQNwpcJCk5wnqP_Z9gX49w8UWLevc_4';
  1846.         $apiKey = 'AIzaSyA7GZ8In7gav2nD0wDejYRWU2zOxCqCQWI';
  1847.  
  1848.         //------------------------------
  1849.         // Define URL to GCM endpoint
  1850.         //------------------------------
  1851.  
  1852.         $url = 'https://fcm.googleapis.com/fcm/send';
  1853.  
  1854.         //------------------------------
  1855.         // Set GCM post variables
  1856.         // (Device IDs and push payload)
  1857.         //------------------------------
  1858.  
  1859.         $post = array(
  1860.                     'data' => $data,
  1861.                 );
  1862.  
  1863.         if( isset( $params['registration_ids'] ) )
  1864.         {
  1865.             $post['registration_ids'] = $params['registration_ids'];
  1866.         }
  1867.         else if( isset( $params['stoken'] ) )
  1868.         {
  1869.             $post['to'] = $params["stoken"];
  1870.         }
  1871.         else
  1872.         {
  1873.             $post['to'] = "/topics/global";
  1874.         }
  1875.  
  1876.         if ( $notification != null )
  1877.         {
  1878.             $post['notification'] = $notification;
  1879.         }
  1880.  
  1881.         //------------------------------
  1882.         // Set CURL request headers
  1883.         // (Authentication and type)
  1884.         //------------------------------
  1885.  
  1886.         $headers = array(
  1887.             'Authorization: key=' . $apiKey,
  1888.             'Content-Type: application/json'
  1889.         );
  1890.  
  1891.         //------------------------------
  1892.         // Initialize curl handle
  1893.         //------------------------------
  1894.  
  1895.         $ch = curl_init();
  1896.  
  1897.         //------------------------------
  1898.         // Set URL to GCM endpoint
  1899.         //------------------------------
  1900.  
  1901.         curl_setopt( $ch, CURLOPT_URL, $url );
  1902.  
  1903.         //------------------------------
  1904.         // Set request method to POST
  1905.         //------------------------------
  1906.  
  1907.         curl_setopt( $ch, CURLOPT_POST, true );
  1908.  
  1909.         //------------------------------
  1910.         // Set our custom headers
  1911.         //------------------------------
  1912.  
  1913.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  1914.  
  1915.         //------------------------------
  1916.         // Get the response back as
  1917.         // string instead of printing it
  1918.         //------------------------------
  1919.  
  1920.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1921.  
  1922.         //------------------------------
  1923.         // Set post data as JSON
  1924.         //------------------------------
  1925.  
  1926.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  1927.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  1928.  
  1929.         //------------------------------
  1930.         // Actually send the push!
  1931.         //------------------------------
  1932.  
  1933.         $result = curl_exec( $ch );
  1934.  
  1935.         //------------------------------
  1936.         // Error? Display it!
  1937.         //------------------------------
  1938.  
  1939.         if ( curl_errno( $ch ) )
  1940.         {
  1941.             // This  is crazy, we are returning value as
  1942.             // we had promised but will never return ERROR = 1
  1943.             // since we are trying to trigger other FCM funcs
  1944.             error_log('FCMIOS2 '.curl_error( $ch ));
  1945.         }
  1946.  
  1947.         //------------------------------
  1948.         // Close curl handle
  1949.         //------------------------------
  1950.  
  1951.         curl_close( $ch );
  1952.  
  1953.         $this->sendFirebaseCloudMessageIOS4($data, $params);
  1954.  
  1955.         //------------------------------
  1956.         // Debug GCM response
  1957.         //------------------------------
  1958.         return array(0,'Success', $result);
  1959.     }
  1960.  
  1961.     function sendFirebaseCloudMessageIOS4( $data, $params = array() )
  1962.     {
  1963.         //------------------------------
  1964.         // Replace with real GCM API
  1965.         // key from Google APIs Console
  1966.         //
  1967.         // https://code.google.com/apis/console/
  1968.         //------------------------------
  1969.  
  1970.         // ade dashboard
  1971.         $apiKey = 'AIzaSyBBUQNwpcJCk5wnqP_Z9gX49w8UWLevc_4';
  1972.  
  1973.         //------------------------------
  1974.         // Define URL to GCM endpoint
  1975.         //------------------------------
  1976.  
  1977.         $url = 'https://fcm.googleapis.com/fcm/send';
  1978.  
  1979.         //------------------------------
  1980.         // Set GCM post variables
  1981.         // (Device IDs and push payload)
  1982.         //------------------------------
  1983.  
  1984.         $post = array(
  1985.                     'data' => $data,
  1986.                 );
  1987.  
  1988.         if( isset( $params['registration_ids'] ) )
  1989.         {
  1990.             $post['registration_ids'] = $params['registration_ids'];
  1991.         }
  1992.         else if( isset( $params['stoken'] ) )
  1993.         {
  1994.             $post['to'] = $params["stoken"];
  1995.         }
  1996.         else
  1997.         {
  1998.             $post['to'] = "/topics/global";
  1999.         }
  2000.  
  2001.         //------------------------------
  2002.         // Set CURL request headers
  2003.         // (Authentication and type)
  2004.         //------------------------------
  2005.  
  2006.         $headers = array(
  2007.             'Authorization: key=' . $apiKey,
  2008.             'Content-Type: application/json'
  2009.         );
  2010.  
  2011.         //------------------------------
  2012.         // Initialize curl handle
  2013.         //------------------------------
  2014.  
  2015.         $ch = curl_init();
  2016.  
  2017.         //------------------------------
  2018.         // Set URL to GCM endpoint
  2019.         //------------------------------
  2020.  
  2021.         curl_setopt( $ch, CURLOPT_URL, $url );
  2022.  
  2023.         //------------------------------
  2024.         // Set request method to POST
  2025.         //------------------------------
  2026.  
  2027.         curl_setopt( $ch, CURLOPT_POST, true );
  2028.  
  2029.         //------------------------------
  2030.         // Set our custom headers
  2031.         //------------------------------
  2032.  
  2033.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  2034.  
  2035.         //------------------------------
  2036.         // Get the response back as
  2037.         // string instead of printing it
  2038.         //------------------------------
  2039.  
  2040.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  2041.  
  2042.         //------------------------------
  2043.         // Set post data as JSON
  2044.         //------------------------------
  2045.  
  2046.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  2047.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  2048.  
  2049.         //------------------------------
  2050.         // Actually send the push!
  2051.         //------------------------------
  2052.  
  2053.         $result = curl_exec( $ch );
  2054.  
  2055.         //------------------------------
  2056.         // Error? Display it!
  2057.         //------------------------------
  2058.  
  2059.         if ( curl_errno( $ch ) )
  2060.         {
  2061.             // This  is crazy, we are returning value as
  2062.             // we had promised but will never return ERROR = 1
  2063.             // since we are trying to trigger other FCM funcs
  2064.             error_log('FCMIOS4 '.curl_error( $ch ));
  2065.         }
  2066.  
  2067.         //------------------------------
  2068.         // Close curl handle
  2069.         //------------------------------
  2070.  
  2071.         curl_close( $ch );
  2072.  
  2073.         //------------------------------
  2074.         // Debug GCM response
  2075.         //------------------------------
  2076.         return array(0,'Success', $result);
  2077.     }
  2078.  
  2079.     function sendGoogleCloudMessage2( $data, $params = array() )
  2080.     {
  2081.         //------------------------------
  2082.         // Replace with real GCM API
  2083.         // key from Google APIs Console
  2084.         //
  2085.         // https://code.google.com/apis/console/
  2086.         //------------------------------
  2087.  
  2088.         $apiKey = 'AIzaSyAMC9GU3zMDCOf8r2kZvVfJMhbGrh2iJTM';
  2089.  
  2090.         //------------------------------
  2091.         // Define URL to GCM endpoint
  2092.         //------------------------------
  2093.  
  2094.         $url = 'https://android.googleapis.com/gcm/send';
  2095.  
  2096.         //------------------------------
  2097.         // Set GCM post variables
  2098.         // (Device IDs and push payload)
  2099.         //------------------------------
  2100.  
  2101.         $post = array(
  2102.                     'data' => $data,
  2103.                 );
  2104.  
  2105.         if( isset( $params['registration_ids'] ) )
  2106.         {
  2107.             $post['registration_ids'] = $params['registration_ids'];
  2108.         }
  2109.         else if( isset( $params['stoken'] ) )
  2110.         {
  2111.             $post['to'] = $params["stoken"];
  2112.         }
  2113.         else
  2114.         {
  2115.             $post['to'] = "/topics/global";
  2116.         }
  2117.  
  2118.         //------------------------------
  2119.         // Set CURL request headers
  2120.         // (Authentication and type)
  2121.         //------------------------------
  2122.  
  2123.         $headers = array(
  2124.             'Authorization: key=' . $apiKey,
  2125.             'Content-Type: application/json'
  2126.         );
  2127.  
  2128.         //------------------------------
  2129.         // Initialize curl handle
  2130.         //------------------------------
  2131.  
  2132.         $ch = curl_init();
  2133.  
  2134.         //------------------------------
  2135.         // Set URL to GCM endpoint
  2136.         //------------------------------
  2137.  
  2138.         curl_setopt( $ch, CURLOPT_URL, $url );
  2139.  
  2140.         //------------------------------
  2141.         // Set request method to POST
  2142.         //------------------------------
  2143.  
  2144.         curl_setopt( $ch, CURLOPT_POST, true );
  2145.  
  2146.         //------------------------------
  2147.         // Set our custom headers
  2148.         //------------------------------
  2149.  
  2150.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  2151.  
  2152.         //------------------------------
  2153.         // Get the response back as
  2154.         // string instead of printing it
  2155.         //------------------------------
  2156.  
  2157.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  2158.  
  2159.         //------------------------------
  2160.         // Set post data as JSON
  2161.         //------------------------------
  2162.  
  2163.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  2164.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  2165.  
  2166.         //------------------------------
  2167.         // Actually send the push!
  2168.         //------------------------------
  2169.  
  2170.         $result = curl_exec( $ch );
  2171.  
  2172.         //------------------------------
  2173.         // Error? Display it!
  2174.         //------------------------------
  2175.  
  2176.         if ( curl_errno( $ch ) )
  2177.         {
  2178.             return array(1, curl_error( $ch ), -1);
  2179.         }
  2180.  
  2181.         //------------------------------
  2182.         // Close curl handle
  2183.         //------------------------------
  2184.  
  2185.         curl_close( $ch );
  2186.  
  2187.         //------------------------------
  2188.         // Debug GCM response
  2189.         //------------------------------
  2190.         return array(0,'Success', $result);
  2191.     }
  2192.  
  2193.     function gcm()
  2194.     {
  2195.         $this->sendgcmdevice();
  2196.     }
  2197.  
  2198.     function sendgcmdevice2( $params = array() )
  2199.     {
  2200.         //------------------------------
  2201.         // Replace with real GCM API
  2202.         // key from Google APIs Console
  2203.         //
  2204.         // https://code.google.com/apis/console/
  2205.         //------------------------------
  2206.  
  2207.         $apiKey = 'AIzaSyAMC9GU3zMDCOf8r2kZvVfJMhbGrh2iJTM';
  2208.  
  2209.         //------------------------------
  2210.         // Define URL to GCM endpoint
  2211.         //------------------------------
  2212.  
  2213.         $url = 'https://android.googleapis.com/gcm/send';
  2214.  
  2215.         //------------------------------
  2216.         // Set GCM post variables
  2217.         // (Device IDs and push payload)
  2218.         //------------------------------
  2219.         $params['Timestamp'] = number_format($this->current_millis(),0,'','');
  2220.  
  2221.         $post = array(
  2222.                     'to'  => $params['SToken'],
  2223.                     'data'=> $params,
  2224.                 );
  2225.  
  2226.         //------------------------------
  2227.         // Set CURL request headers
  2228.         // (Authentication and type)
  2229.         //------------------------------
  2230.  
  2231.         $headers = array(
  2232.                             'Authorization: key=' . $apiKey,
  2233.                             'Content-Type: application/json'
  2234.                         );
  2235.  
  2236.         //------------------------------
  2237.         // Initialize curl handle
  2238.         //------------------------------
  2239.  
  2240.         $ch = curl_init();
  2241.  
  2242.         //------------------------------
  2243.         // Set URL to GCM endpoint
  2244.         //------------------------------
  2245.  
  2246.         curl_setopt( $ch, CURLOPT_URL, $url );
  2247.  
  2248.         //------------------------------
  2249.         // Set request method to POST
  2250.         //------------------------------
  2251.  
  2252.         curl_setopt( $ch, CURLOPT_POST, true );
  2253.  
  2254.         //------------------------------
  2255.         // Set our custom headers
  2256.         //------------------------------
  2257.  
  2258.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  2259.  
  2260.         //------------------------------
  2261.         // Get the response back as
  2262.         // string instead of printing it
  2263.         //------------------------------
  2264.  
  2265.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  2266.  
  2267.         //------------------------------
  2268.         // Set post data as JSON
  2269.         //------------------------------
  2270.  
  2271.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  2272.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  2273.  
  2274.         //------------------------------
  2275.         // Actually send the push!
  2276.         //------------------------------
  2277.  
  2278.         $result = curl_exec( $ch );
  2279.  
  2280.         //------------------------------
  2281.         // Error? Display it!
  2282.         //------------------------------
  2283.         $SUCCESS = 0;
  2284.         $STR_SUCCESS = "SUCCESS";
  2285.         $ERROR = 1;
  2286.         $STR_ERROR = "ERROR";
  2287.  
  2288.         $resjson = array();
  2289.  
  2290.  
  2291.         if ( curl_errno( $ch ) )
  2292.         {
  2293.             $resjson = array(
  2294.                 'error' => $ERROR,
  2295.                 'error_message' => curl_error( $ch ),
  2296.             );
  2297.         }
  2298.  
  2299.         //------------------------------
  2300.         // Close curl handle
  2301.         //------------------------------
  2302.  
  2303.         curl_close( $ch );
  2304.  
  2305.         //------------------------------
  2306.         // Debug GCM response
  2307.         //------------------------------
  2308.         if( empty($resjson) )
  2309.             return $resjson = array(
  2310.                         'error' => $SUCCESS,
  2311.                         'error_message' => $STR_SUCCESS,
  2312.                         'data' => $params,
  2313.                     );
  2314.         else
  2315.             return $resjson;
  2316.     }
  2317.  
  2318.         function sendfcmdevice( $params = array() )
  2319.     {
  2320.         //------------------------------
  2321.         // Replace with real GCM API
  2322.         // key from Google APIs Console
  2323.         //
  2324.         // https://code.google.com/apis/console/
  2325.         //------------------------------
  2326.  
  2327.         $apiKey = 'AIzaSyAFUpRaVZCEcOrAZy-jc2iz_OaAAWEjgd4';
  2328.  
  2329.         //------------------------------
  2330.         // Define URL to GCM endpoint
  2331.         //------------------------------
  2332.  
  2333.         $url = 'https://fcm.googleapis.com/fcm/send';
  2334.  
  2335.         //------------------------------
  2336.         // Set GCM post variables
  2337.         // (Device IDs and push payload)
  2338.         //------------------------------
  2339.         $data = array('message' => 'Hey test',
  2340.             "Timestamp"=> number_format(30303,0,'',''),
  2341.             'driver_info'       => 'driver_map',
  2342.             'driver_lat'       => '6.98111',
  2343.             'driver_lng'       => '-116.98766',
  2344.         );
  2345.         $post = array(
  2346. //                        'registration_ids'  => $ids,
  2347.                         'to'  => "eA2HaLmxWOE:APA91bGV6o8UGW9ageFwCv9xqX_URkUw2_IHWzO82jX830A3LCDJL4LMD9dt0ShZdfxQ0FYAPKKE3d1PqPKlnwNDRu-AszErE9F-a8UIVrQthFJEg5UZYGs0GIrZBF_G-IjDoBcuY79I",
  2348.                         'data'              => $data,
  2349.                         );
  2350.  
  2351.         //------------------------------
  2352.         // Set CURL request headers
  2353.         // (Authentication and type)
  2354.         //------------------------------
  2355.  
  2356.         $headers = array(
  2357.                             'Authorization:key=' . $apiKey,
  2358.                             'Content-Type:application/json'
  2359.                         );
  2360.  
  2361.         //------------------------------
  2362.         // Initialize curl handle
  2363.         //------------------------------
  2364.  
  2365.         $ch = curl_init();
  2366.  
  2367.         //------------------------------
  2368.         // Set URL to GCM endpoint
  2369.         //------------------------------
  2370.  
  2371.         curl_setopt( $ch, CURLOPT_URL, $url );
  2372.  
  2373.         //------------------------------
  2374.         // Set request method to POST
  2375.         //------------------------------
  2376.  
  2377.         curl_setopt( $ch, CURLOPT_POST, true );
  2378.  
  2379.         //------------------------------
  2380.         // Set our custom headers
  2381.         //------------------------------
  2382.  
  2383.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  2384.  
  2385.         //------------------------------
  2386.         // Get the response back as
  2387.         // string instead of printing it
  2388.         //------------------------------
  2389.  
  2390.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  2391.  
  2392.         //------------------------------
  2393.         // Set post data as JSON
  2394.         //------------------------------
  2395.  
  2396.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  2397.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  2398. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  2399.         //------------------------------
  2400.         // Actually send the push!
  2401.         //------------------------------
  2402.  
  2403.         $result = curl_exec( $ch );
  2404.  
  2405.         //------------------------------
  2406.         // Error? Display it!
  2407.         //------------------------------
  2408.  
  2409.         if ( curl_errno( $ch ) )
  2410.         {
  2411.             echo '1@@GCM error: ' . curl_error( $ch );
  2412.         }
  2413.  
  2414.         //------------------------------
  2415.         // Close curl handle
  2416.         //------------------------------
  2417.  
  2418.         curl_close( $ch );
  2419.  
  2420.         //------------------------------
  2421.         // Debug GCM response
  2422.         //------------------------------
  2423.  
  2424.         if( isset($params['separate']))
  2425.             echo '@@0@@'.$result;
  2426.         else
  2427.             echo '0@@'.$result;
  2428.     }
  2429.  
  2430.     function sendgcmdevice( $params = array() )
  2431.     {
  2432.         //------------------------------
  2433.         // Replace with real GCM API
  2434.         // key from Google APIs Console
  2435.         //
  2436.         // https://code.google.com/apis/console/
  2437.         //------------------------------
  2438.  
  2439.         $apiKey = 'AIzaSyAMC9GU3zMDCOf8r2kZvVfJMhbGrh2iJTM';
  2440.  
  2441.         //------------------------------
  2442.         // Define URL to GCM endpoint
  2443.         //------------------------------
  2444.  
  2445.         $url = 'https://android.googleapis.com/gcm/send';
  2446.  
  2447.         //------------------------------
  2448.         // Set GCM post variables
  2449.         // (Device IDs and push payload)
  2450.         //------------------------------
  2451.         $data = array('message' => 'Hey test',
  2452.             "Timestamp"=> number_format($this->current_millis(),0,'',''),
  2453.             'driver_info'       => 'driver_map',
  2454.             'driver_lat'       => '6.98111',
  2455.             'driver_lng'       => '-116.98766',
  2456.         );
  2457.         $post = array(
  2458. //                        'registration_ids'  => $ids,
  2459.                         'to'  => "fFO5k2imbDU:APA91bGr3xSGvPJPvxRHle6rJClmvWMxpkXWliKAJRdYv5tIondUjZ8eNpzE38xXukLkDTuORDvoq_p2bijxdyQkn7F2ki0JPo977UT3Xo6ZtWW4VjhiHGFt0EDVb9V4YE2c4EwD8PWf",
  2460.                         'data'              => $data,
  2461.                         );
  2462.  
  2463.         //------------------------------
  2464.         // Set CURL request headers
  2465.         // (Authentication and type)
  2466.         //------------------------------
  2467.  
  2468.         $headers = array(
  2469.                             'Authorization: key=' . $apiKey,
  2470.                             'Content-Type: application/json'
  2471.                         );
  2472.  
  2473.         //------------------------------
  2474.         // Initialize curl handle
  2475.         //------------------------------
  2476.  
  2477.         $ch = curl_init();
  2478.  
  2479.         //------------------------------
  2480.         // Set URL to GCM endpoint
  2481.         //------------------------------
  2482.  
  2483.         curl_setopt( $ch, CURLOPT_URL, $url );
  2484.  
  2485.         //------------------------------
  2486.         // Set request method to POST
  2487.         //------------------------------
  2488.  
  2489.         curl_setopt( $ch, CURLOPT_POST, true );
  2490.  
  2491.         //------------------------------
  2492.         // Set our custom headers
  2493.         //------------------------------
  2494.  
  2495.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  2496.  
  2497.         //------------------------------
  2498.         // Get the response back as
  2499.         // string instead of printing it
  2500.         //------------------------------
  2501.  
  2502.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  2503.  
  2504.         //------------------------------
  2505.         // Set post data as JSON
  2506.         //------------------------------
  2507.  
  2508.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  2509.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  2510.  
  2511.         //------------------------------
  2512.         // Actually send the push!
  2513.         //------------------------------
  2514.  
  2515.         $result = curl_exec( $ch );
  2516.  
  2517.         //------------------------------
  2518.         // Error? Display it!
  2519.         //------------------------------
  2520.  
  2521.         if ( curl_errno( $ch ) )
  2522.         {
  2523.             echo '1@@GCM error: ' . curl_error( $ch );
  2524.         }
  2525.  
  2526.         //------------------------------
  2527.         // Close curl handle
  2528.         //------------------------------
  2529.  
  2530.         curl_close( $ch );
  2531.  
  2532.         //------------------------------
  2533.         // Debug GCM response
  2534.         //------------------------------
  2535.  
  2536.         if( isset($params['separate']))
  2537.             echo '@@0@@'.$result;
  2538.         else
  2539.             echo '0@@'.$result;
  2540.     }
  2541.  
  2542.     // FIXME shud use more secure ways
  2543.     function driver_payload($txno, $client_userid)
  2544.     {
  2545.         $ERROR = 1;
  2546.         error_log('Driver Payload logging');
  2547.  
  2548.         // validate the tx
  2549.         $tparams = array('txno'=>$txno);
  2550.         $txrecord = $this->model_goexpress->get_tx($tparams);
  2551.  
  2552.         if( empty($txrecord) )
  2553.         {
  2554.             $resjson = array(
  2555.                 'error' => $ERROR,
  2556.                 'error_message' => "Error: No TX",
  2557.                 'msgid' => "",
  2558.                 'payload' => "",
  2559.             );
  2560.             die( json_encode($resjson) );
  2561.         }
  2562.  
  2563.         $txrecord = $txrecord[0];
  2564.  
  2565.         $json = json_decode($txrecord->payload);
  2566.  
  2567.         $resjson = array(
  2568.             'error' => 0,
  2569.             'error_message' => '',
  2570.             'msgid' => '',
  2571.             'payload' => json_encode($json->data->driver_payload),
  2572.         );
  2573.  
  2574.         $retstr = json_encode($resjson);
  2575.         error_log('driver_payload ' . $retstr);
  2576.         die( $retstr );
  2577.     }
  2578.  
  2579.     function tx_checkack_version($txno)
  2580.     {
  2581.         $CLIENT = 90;
  2582.         $DRIVER = 50;
  2583.         $FATAL_ERROR = 2;
  2584.         $PERMISSIVE_ERROR = 1;
  2585.         $SUCCESS = 0;
  2586.  
  2587.         $payload = file_get_contents('php://input');
  2588.         $json = json_decode( $payload );
  2589.  
  2590.         error_log("CHECK_VERSION ". $payload);
  2591.  
  2592.         //Check Version
  2593.         //COMMENT SIR ADE UPDATE START 1
  2594.         if( $json->isclt == $DRIVER )
  2595.         {
  2596.             query_log("Current version is " . $json->version);
  2597.             // $json->version = "2.1.0";
  2598.             if( intval(str_replace(".","",$json->version)) >= I_DRIVER_VERSION )
  2599.             {
  2600.                 $resjson = array(
  2601.                     'error' => $SUCCESS,
  2602.                     'error_message' => 'Your permissive error is working!',
  2603.                 );
  2604.                 // die(json_encode($resjson));
  2605.             }
  2606.  
  2607.             else {
  2608.                 $resjson = array(
  2609.                     'error' => $FATAL_ERROR,
  2610.                     'error_message' => 'New Version needed.',
  2611.                     'driver_bc' => ''
  2612.                 );
  2613.  
  2614.                 //$resjson = array(
  2615.                 //    'error' => $SUCCESS,
  2616.                 //    'error_message' => 'Your permissive error is working!',
  2617.                 //);
  2618.                 error_log("CHECK_VERSION CLIENT REPLY". json_encode($resjson));
  2619.                 die(json_encode($resjson));
  2620.             }
  2621.         }
  2622.  
  2623.         // Check tx status
  2624.  
  2625.  
  2626.         $txrecord = $this->model_goexpress->get_tx_by_txno_only(array('txno'=>$txno));
  2627.  
  2628.         if ( empty($txrecord) )
  2629.         {
  2630.             // No need to show Alert to CLIENT because this supporting system not main system
  2631.             $resjson = array(
  2632.                 'error' => 0,
  2633.                 'driver_bc' => "",
  2634.                 'error_message' => "EMPTY PAYLOAD"
  2635.             );
  2636.  
  2637.             die( json_encode($resjson) );
  2638.         }
  2639.  
  2640.         if ( $txrecord->cancel_type_id > 0  )
  2641.         {
  2642.             $resjson = array(
  2643.                 'error' => 0,
  2644.                 'driver_bc' => "tx_canceled",
  2645.                 'driver_remark' => $txrecord->cancel_remark
  2646.             );
  2647.  
  2648.             die( json_encode($resjson) );
  2649.         }
  2650.  
  2651.         elseif ( $txrecord->complete_type_id > 0 )
  2652.         {
  2653.             $resjson = array (
  2654.                 'error' => 0,
  2655.                 'driver_bc' => "tx_completed",
  2656.                 'driver_remark' => $txrecord->complete_remark
  2657.             );
  2658.  
  2659.             die( json_encode($resjson) );
  2660.         }
  2661.  
  2662.         elseif ( strlen($txrecord->driver_username) > 5 ) {
  2663.  
  2664.             $json = json_decode($txrecord->payload);
  2665.             $driver_latlng = $this->model_goexpress->get_driver_last_latlng($txno);
  2666.             $driver_lat = "";
  2667.             $driver_lng = "";
  2668.             if ( isset($driver_latlng) and isset($driver_latlng->lat) ) {
  2669.                 $driver_lat = $driver_latlng->lat;
  2670.                 $driver_lng = $driver_latlng->lng;
  2671.             }
  2672.  
  2673.             $bMeet = $txrecord->has_meet;
  2674.             if( $txrecord->flag_meet == 1 AND $txrecord->has_meet == 1)
  2675.                 $bMeet = 0;
  2676.  
  2677.             $resjson = array (
  2678.                 'error' => 0,
  2679.                 'driver_bc' => "tx_ack",
  2680.                 'txno' => $txrecord->txno,
  2681.                 'has_meet' => $bMeet,
  2682.                 'payload' => json_encode($json->data->driver_payload),
  2683.                 'driver_lat' => $driver_lat,
  2684.                 'driver_lng' => $driver_lng
  2685.             );
  2686.  
  2687.             if( $txrecord->flag_meet == 0 AND $txrecord->has_meet == 1)
  2688.             {
  2689.                 $uparams = array('flag_meet' => 1 );
  2690.                 $this->model_goexpress->update_tx($uparams, $txrecord->id);
  2691.             }
  2692.  
  2693.             die( json_encode($resjson) );
  2694.         }
  2695.  
  2696.         elseif( $txrecord->status_id == 1 ){
  2697.             $resjson = array(
  2698.                 'error' => 0,
  2699.                 'driver_bc' => "tx_cancel"
  2700.             );
  2701.             die( json_encode($resjson) );
  2702.         }
  2703.  
  2704.         else {
  2705.  
  2706.             $bMeet = $txrecord->has_meet;
  2707.  
  2708.             if( $txrecord->flag_meet == 1 AND $txrecord->has_meet == 1)
  2709.                 $bMeet = 0;
  2710.  
  2711.             if( $txrecord->flag_meet == 0 AND $txrecord->has_meet == 1)
  2712.             {
  2713.                 $uparams = array('flag_meet' => 1 );
  2714.                 $this->model_goexpress->update_tx($uparams, $txrecord->id);
  2715.             }
  2716.  
  2717.             die( json_encode(array(
  2718.                                 'error' => 0,
  2719.                                 'has_meet' => $bMeet,
  2720.                                 'driver_bc' => "",
  2721.                                 'normal_fare' => $txrecord->normal_price,
  2722.                                 'promo_value' => $txrecord->promo_deduction
  2723.                             )
  2724.                         )
  2725.                     );
  2726.         }
  2727.  
  2728.  
  2729.     }
  2730.  
  2731.     function tx_checkack($txno)
  2732.     {
  2733.         $CANCEL = 1;
  2734.         $DRIVER_CANCEL = 5;
  2735.         $COMPLETE = 1;
  2736.         $WORKING = 9;
  2737.  
  2738.         $txrecord = $this->model_goexpress->get_tx_by_txno_only(array('txno'=>$txno));
  2739.  
  2740.         // No need to show Alert to CLIENT because this supporting system not main system
  2741.         if ( empty($txrecord) )
  2742.         {
  2743.             $resjson = array(
  2744.                 'error' => 0,
  2745.                 'driver_bc' => "",
  2746.                 'error_message' => "EMPTY PAYLOAD"
  2747.             );
  2748.  
  2749.             die( json_encode($resjson) );
  2750.         }
  2751.  
  2752.         if ( $txrecord->cancel_type_id > 0  )
  2753.         {
  2754.             $resjson = array(
  2755.                 'error' => 0,
  2756.                 'driver_bc' => "tx_canceled",
  2757.                 'driver_remark' => $txrecord->cancel_remark
  2758.             );
  2759.  
  2760.             die( json_encode($resjson) );
  2761.         }
  2762.  
  2763.         elseif ( $txrecord->complete_type_id > 0 )
  2764.         {
  2765.             $resjson = array (
  2766.                 'error' => 0,
  2767.                 'driver_bc' => "tx_completed",
  2768.                 'driver_remark' => $txrecord->complete_remark
  2769.             );
  2770.  
  2771.             die( json_encode($resjson) );
  2772.         }
  2773.  
  2774.         elseif( $txrecord->status_id == 1 ){
  2775.             $resjson = array(
  2776.                 'error' => 0,
  2777.                 'driver_bc' => "tx_cancel"
  2778.             );
  2779.             die( json_encode($resjson) );
  2780.         }
  2781.  
  2782.         elseif ( $txrecord->status_id == $WORKING ) {
  2783.  
  2784.             $json = json_decode($txrecord->payload);
  2785.             $driver_latlng = $this->model_goexpress->get_driver_last_latlng($txno);
  2786.             $driver_lat = "";
  2787.             $driver_lng = "";
  2788.             if ( isset($driver_latlng) and isset($driver_latlng->lat) ) {
  2789.                 $driver_lat = $driver_latlng->lat;
  2790.                 $driver_lng = $driver_latlng->lng;
  2791.             }
  2792.  
  2793.             $bMeet = $txrecord->has_meet;
  2794.             if( $txrecord->flag_meet == 1 AND $txrecord->has_meet == 1)
  2795.                 $bMeet = 0;
  2796.  
  2797.             $resjson = array (
  2798.                 'error' => 0,
  2799.                 'driver_bc' => "tx_ack",
  2800.                 'txno' => $txrecord->txno,
  2801.                 'has_meet' => $bMeet,
  2802.                 'payload' => json_encode($json->data->driver_payload),
  2803.                 'driver_lat' => $driver_lat,
  2804.                 'driver_lng' => $driver_lng
  2805.             );
  2806.  
  2807.             if( $txrecord->flag_meet == 0 AND $txrecord->has_meet == 1)
  2808.             {
  2809.                 $uparams = array('flag_meet' => 1 );
  2810.                 $this->model_goexpress->update_tx($uparams, $txrecord->id);
  2811.             }
  2812.  
  2813.             die( json_encode($resjson) );
  2814.         }
  2815.  
  2816.         else {
  2817.  
  2818.             $bMeet = $txrecord->has_meet;
  2819.  
  2820.             if( $txrecord->flag_meet == 1 AND $txrecord->has_meet == 1)
  2821.                 $bMeet = 0;
  2822.  
  2823.             if( $txrecord->flag_meet == 0 AND $txrecord->has_meet == 1)
  2824.             {
  2825.                 $uparams = array('flag_meet' => 1 );
  2826.                 $this->model_goexpress->update_tx($uparams, $txrecord->id);
  2827.             }
  2828.  
  2829.             die( json_encode(array(
  2830.                                 'error' => 0,
  2831.                                 'has_meet' => $bMeet,
  2832.                                 'driver_bc' => ""
  2833.                             )
  2834.                         )
  2835.                     );
  2836.         }
  2837.     }
  2838.  
  2839.  
  2840.     function tx_ack($txno, $driver_userid)
  2841.     {
  2842.         error_log('tx ack logging');
  2843.         // TODO make sure driver sent its GCM token
  2844.         $input_payload = file_get_contents('php://input');
  2845.         //error_log($input_payload);die();
  2846.         $ERROR = 1;
  2847.         $NOTIF_ERROR = 2;
  2848.         $SOFT_NOTIF = 3;
  2849.         $OPEN = 0;
  2850.         $WORKING =
  2851.             $DRIVING = 9;
  2852.         $DRIVER_ACK = 10;
  2853.         // validate the driver
  2854.         $json_input = json_decode($input_payload);
  2855.         if( empty($json_input->data->driver_phoneno)) {
  2856.             $resjson = array(
  2857.                 'error' => $ERROR,
  2858.                 'error_message' => "Error: No Phone Empty",
  2859.                 'msgid' => "",
  2860.                 'payload' => "",
  2861.             );
  2862.             error_log(json_encode($resjson));
  2863.             error_log($input_payload);
  2864.             die( json_encode($resjson) );
  2865.         }
  2866.  
  2867.         if( empty($json_input->data->driver_name)) {
  2868.             $resjson = array(
  2869.                 'error' => $ERROR,
  2870.                 'error_message' => "Error: Driver Name Empty",
  2871.                 'msgid' => "",
  2872.                 'payload' => ""
  2873.             );
  2874.             error_log(json_encode($resjson));
  2875.             error_log($input_payload);
  2876.             die( json_encode($resjson) );
  2877.         }
  2878.         query_log("Execute checkValid");
  2879.         list($flag,$maxval,$remainval) = $this->model_goexpress->checkvalid($json_input->data->driver_userid);
  2880.         if ( $this->uri->segment(1) == "km" ) {
  2881.             if ( $flag == 1 ) {
  2882.                 $resjson = array (
  2883.                     'error' => $NOTIF_ERROR,
  2884.                     'error_message' => "សូមអភ័យទោស ទឹកប្រាក់កម្រៃជើងសារដែលក្រុមហ៊ុន Go-X តម្រូវឲ្យបង់បានកើនលើសពីកំរិតកំណត់ហើយ! ចំនួនទឹកប្រាក់កម្រៃជើងសាររបស់ការិយាល័យច្រើនបំផុតគឺ ".$maxval."រៀល។
  2885. សូមមេត្តាមកកាន់ការិយាល័យ Go-X ដើម្បីធ្វើការបង់ប្រាក់ និងអាចទទួលយកការកម្ម៉ងបន្តទៀត។",
  2886.                     'msgid' => "",
  2887.                     'payload' => ""
  2888.                 );
  2889.                 die( json_encode($resjson) );
  2890.             }
  2891.             elseif ( $flag == 2 ) {
  2892.                 $resjson = array (
  2893.                     'error' => $NOTIF_ERROR,
  2894.                     'error_message' => "សូមអភ័យទោស ថ្ងៃបរិច្ឆេទដែលលោកអ្នកត្រូវមកកាន់ក្រុមហ៊ុន GO-X ដើម្បីធ្វើការទូទាត់ប្រាក់កម្រៃជើងសារត្រូវបានផុតកំណត់ហើយ! សូមមេត្តាមកកាន់ការិយាល័យ Go-X ដើម្បីធ្វើការបង់ប្រាក់ និងអាចទទួលយកការកម្ម៉ងបន្តទៀត។",
  2895.                     'msgid' => "",
  2896.                     'payload' => ""
  2897.                 );
  2898.                 die( json_encode($resjson) );
  2899.             }
  2900.             elseif ( $flag == 3 ) {
  2901.                 $notifjson = array (
  2902.                     'error' => $SOFT_NOTIF,
  2903.                     'error_message' => "ទឹកប្រាក់កម្រៃជើងសារដែលក្រុមហ៊ុនកំណត់ឲ្យលោកអ្នកច្រើនបំផុតគឺ ".$maxval."រៀល។ លោកអ្នកនៅសល់ចំនួនទឹកប្រាក់ ".$remainval."រៀល ទៀត ដែលនឹងឈានទៅដល់ទឹកប្រាក់កម្រៃជើងសារច្រើនបំផុតដែលក្រុមហ៊ុនបានកំណត់ឲ្យ!",
  2904.                     'msgid' => "",
  2905.                     'payload' => ""
  2906.                 );
  2907.                // die( json_encode($resjson) );
  2908.             }
  2909.             elseif ( $flag == 4 ) {
  2910.                 $notifjson = array (
  2911.                     'error' => $SOFT_NOTIF,
  2912.                     'error_message' => "Yសូមអភ័យទោស ថ្ងៃបរិច្ឆេទដែលលោកអ្នកត្រូវមកកាន់ក្រុមហ៊ុន GO-X ដើម្បីធ្វើការទូទាត់ប្រាក់កម្រៃជើងសារនឹងផុតកំណត់នៅថ្ងៃទី ".$maxval."!  រយៈពេលដែលនៅសល់ពីថ្ងៃផុតកំណត់គឺ ".$remainval." ថ្ងៃទៀត។",
  2913.                     'msgid' => "",
  2914.                     'payload' => ""
  2915.                 );
  2916.                 // die( json_encode($resjson) );
  2917.             }
  2918.         }
  2919.         else {
  2920.             if ( $flag == 1 ) {
  2921.                 $resjson = array (
  2922.                     'error' => $NOTIF_ERROR,
  2923.                     'error_message' => "Sorry, you already exceed maximum pending office comission. Maximum pending office comission is ".$maxval." KHR. Please go to office to continue take new order",
  2924.                     'msgid' => "",
  2925.                     'payload' => ""
  2926.                 );
  2927.                 die( json_encode($resjson) );
  2928.             }
  2929.             elseif ( $flag == 2 ) {
  2930.                 $resjson = array (
  2931.                     'error' => $NOTIF_ERROR,
  2932.                     'error_message' => "Sorry, your date to visit GO-X company is expired, please come to our office to continue take new order",
  2933.                     'msgid' => "",
  2934.                     'payload' => ""
  2935.                 );
  2936.                 die( json_encode($resjson) );
  2937.             }
  2938.             elseif ( $flag == 3 ) {
  2939.                 $notifjson = array (
  2940.                     'error' => $SOFT_NOTIF,
  2941.                     'error_message' => "Your maximum pending office comission is ".$maxval." KHR. The remaining ".$remainval." KHR before reaching the maximum limit",
  2942.                     'msgid' => "",
  2943.                     'payload' => ""
  2944.                 );
  2945.                // die( json_encode($resjson) );
  2946.             }
  2947.             elseif ( $flag == 4 ) {
  2948.                 $notifjson = array (
  2949.                     'error' => $SOFT_NOTIF,
  2950.                     'error_message' => "The date that you have to visit GO-X Company for commision payment will expired at .".$maxval."! The days remain is ".$remainval,
  2951.                     'msgid' => "",
  2952.                     'payload' => ""
  2953.                 );
  2954.                // die( json_encode($resjson) );
  2955.             }
  2956.         }
  2957.         // validate the tx
  2958.         $tparams = array('txno'=>$txno);
  2959.         $txrecord = $this->model_goexpress->get_tx($tparams);
  2960.  
  2961.         if( empty($txrecord) )
  2962.         {
  2963.             $resjson = array(
  2964.                 'error' => $ERROR,
  2965.                 'error_message' => "Error: No TX",
  2966.                 'msgid' => "",
  2967.                 'payload' => "",
  2968.             );
  2969.             die( json_encode($resjson) );
  2970.         }
  2971.  
  2972.         $txrecord = $txrecord[0];
  2973.  
  2974.         if( !isset($txrecord->txno) )
  2975.         {
  2976.             $resjson = array(
  2977.                 'error' => $ERROR,
  2978.                 'error_message' => "Error: TX is Broken",
  2979.                 'msgid' => "",
  2980.                 'payload' => "",
  2981.             );
  2982.             die( json_encode($resjson) );
  2983.         }
  2984.         error_log('ID '.$driver_userid.' status_id '.$txrecord->status_id);
  2985.         if( $txrecord->status_id != $OPEN )
  2986.         {
  2987.             $resjson = array(
  2988.                 'error' => $ERROR,
  2989.                 'error_message' => "Error: TX is Taken",
  2990.                 'msgid' => "",
  2991.                 'payload' => "",
  2992.             );
  2993.             die( json_encode($resjson) );
  2994.         }
  2995.  
  2996.         $json = json_decode($txrecord->payload);
  2997.         $json->data->driver_payload = json_decode($input_payload);
  2998.         $reqparams = array('username' => $driver_userid );
  2999.         $driverinfo = $this->model_goexpress->get_user($reqparams);
  3000.         $json->data->driver_payload->driver_picurl = $driverinfo->picurl;
  3001.         $json->data->NormalFare = $txrecord->normal_price;
  3002.         $json->data->PromoDeduction = $txrecord->promo_deduction;
  3003.  
  3004.         $tsparams = array(
  3005.             'payload' => json_encode($json),
  3006.             'ack_status_id' => $DRIVER_ACK,
  3007.             'status_id' => $WORKING,
  3008.             'driver_username' => $json_input->data->driver_userid,
  3009.             'driver_name' => $json_input->data->driver_name
  3010.         );
  3011.         // FIXME validate if the driver is previous accepted
  3012.         if( $txrecord->ack_status_id < $DRIVER_ACK )
  3013.         {
  3014.             list($flag, $void, $msg ) = $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3015.  
  3016.             $resjson = array(
  3017.                 'error' => ERROR_WISE_iFLAG($flag),
  3018.                 'error_message' => $msg,
  3019.                 'msgid' => '',
  3020.             );
  3021.             if ( isset($notifjson) && $resjson['error'] == 0 ) {
  3022.                 $resjson = $notifjson;
  3023.             }
  3024.         }
  3025.         else
  3026.         {
  3027.             $resjson = array(
  3028.                 'error' => 1,
  3029.                 'error_message' => 'Error: TX taken, ACK is no longer DRIVER_ACK is '. $txrecord->ack_status_id,
  3030.             );
  3031.         }
  3032.  
  3033.         if( !isset($resjson['msgid'] ) )
  3034.             $resjson['msgid'] = '';
  3035.  
  3036.         $resjson['payload'] = $json;
  3037.         $resjson['phone'] = $json_input->data->driver_phoneno;
  3038.  
  3039.         if ( ACTIVITY_RECORD == true ) {
  3040.             $ackDetail = array( "driver_username" => $json_input->data->driver_userid,
  3041.                                 "txno" => $txrecord->txno );
  3042.             $activityDetail = array ( "type" => "tx_ack",
  3043.                                       "tx" => $ackDetail
  3044.                               );
  3045.  
  3046.             $this->model_goexpress->add_activity(json_encode($activityDetail));
  3047.         }
  3048.         //error_log(json_encode($resjson));
  3049.         die( json_encode($resjson) );
  3050.     }
  3051.  
  3052.     function tx_ack_sendFcm($txno, $driver_id = null)
  3053.     {
  3054.         $tparams = array('txno' => $txno);
  3055.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3056.  
  3057.         if( empty($txrecord) )
  3058.         {
  3059.             $resjson = array(
  3060.                 'error' => $ERROR,
  3061.                 'error_message' => "Error: No TX",
  3062.                 'msgid' => "",
  3063.                 'payload' => "",
  3064.             );
  3065.             die( json_encode($resjson) );
  3066.         }
  3067.  
  3068.         $txrecord = $txrecord[0];
  3069.  
  3070.         if( !isset($txrecord->txno) )
  3071.         {
  3072.             $resjson = array(
  3073.                 'error' => $ERROR,
  3074.                 'error_message' => "Error: TX is Broken",
  3075.                 'msgid' => "",
  3076.                 'payload' => "",
  3077.             );
  3078.             die( json_encode($resjson) );
  3079.         }
  3080.  
  3081.         $json = json_decode($txrecord->payload);
  3082.  
  3083.         // send gcm to client
  3084.         $data = array(
  3085.             "driver_info" => "tx_ack",
  3086.             "txno" => $txno,
  3087.             "payload" => $txno,
  3088.         );
  3089.  
  3090.         if( $json->data->SToken == OFFICE_LOGIN)
  3091.         {
  3092.             error_log('tx_ack_send_gcm, gcm '.OFFICE_LOGIN.' '.OFFICE_LOGIN);
  3093.  
  3094.             $oparams = array('txno' => $txno);
  3095.             $officeorder = $this->office_model->get_order($oparams);
  3096.  
  3097.             $oiparams = array('driver_ack' => 1);
  3098.             $this->office_model->save_order($oiparams, $officeorder->id);
  3099.  
  3100.             $resjson = array(
  3101.                 'error' => 0,
  3102.                 'error_message' => OFFICE_LOGIN,
  3103.                 'msgid' => OFFICE_LOGIN,
  3104.             );
  3105.  
  3106.             die(json_encode($resjson));
  3107.         }
  3108.  
  3109.         $gparams = array('stoken' => $json->data->SToken );
  3110.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  3111.         //$flag = 0;
  3112.         //$msg = "test";
  3113.         //$msgid= 0;
  3114.  
  3115.         error_log('tx_ack_send_gcm, gcm '.$msg.' '.$msgid);
  3116.  
  3117.         $resjson = array(
  3118.             'error' => $flag,
  3119.             'error_message' => $msg,
  3120.             'msgid' => $msgid,
  3121.         );
  3122.  
  3123.         die(json_encode($resjson));
  3124.     }
  3125.  
  3126.     function tx_reack($txno, $driver_userid)
  3127.     {
  3128.         // TODO make sure driver sent its GCM token
  3129.         $ERROR = 1;
  3130.         $OPEN = 0;
  3131.         $WORKING = 9;
  3132.         $DRIVER_ACK = 10;
  3133.         // validate the driver
  3134.  
  3135.         // validate the tx
  3136.         $tparams = array('txno'=>$txno);
  3137.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3138.  
  3139.         if( empty($txrecord) )
  3140.         {
  3141.             $resjson = array(
  3142.                 'error' => $ERROR,
  3143.                 'error_message' => "Error: No TX",
  3144.                 'msgid' => "",
  3145.                 'payload' => "",
  3146.                 'source'=> 'tx_reack',
  3147.             );
  3148.             die( json_encode($resjson) );
  3149.         }
  3150.  
  3151.         $txrecord = $txrecord[0];
  3152.  
  3153.         if( !isset($txrecord->txno) )
  3154.         {
  3155.             $resjson = array(
  3156.                 'error' => $ERROR,
  3157.                 'error_message' => "Error: TX is Broken",
  3158.                 'msgid' => "",
  3159.                 'payload' => "",
  3160.             );
  3161.             die( json_encode($resjson) );
  3162.         }
  3163.  
  3164.         if( $txrecord->status_id != $OPEN )
  3165.         {
  3166.             $resjson = array(
  3167.                 'error' => $ERROR,
  3168.                 'error_message' => "Error: TX is Taken",
  3169.                 'msgid' => "",
  3170.                 'payload' => "",
  3171.             );
  3172.             die( json_encode($resjson) );
  3173.         }
  3174.  
  3175.         //$tsparams = array('status_id' => $WORKING );
  3176.         $json = json_decode($txrecord->payload);
  3177.         $json->data->driver_payload = json_decode($input_payload);
  3178.         $tsparams = array(
  3179.             'payload' => json_encode($json ),
  3180.             'ack_status_id' => $DRIVER_ACK,
  3181.         );
  3182.         // FIXME validate if the driver is previous accepted
  3183.         if( $txrecord->ack_status_id < $DRIVER_ACK )
  3184.             $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3185.  
  3186.         // send gcm to client
  3187.         $data = array(
  3188.             "driver_info" => "tx_ack",
  3189.             "payload" => $input_payload,
  3190.         );
  3191.  
  3192.         $gparams = array('stoken' => $json->data->SToken );
  3193.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  3194.  
  3195.         $resjson = array(
  3196.             'error' => $flag,
  3197.             'error_message' => $msg,
  3198.             'msgid' => $msgid,
  3199.             'payload' => $txrecord->payload,
  3200.         );
  3201.  
  3202.         $retstr = json_encode($resjson);
  3203.         error_log('tx_ack : '.$txno.' '.$retstr);
  3204.         die( $retstr );
  3205.     }
  3206.  
  3207.     function _get_coord_nearby($params = array())
  3208.     {
  3209.         if ( !isset($params['minrange']) )
  3210.             $params['minrange'] = $params['range'];
  3211.  
  3212.  
  3213.         $const = GET_COORD_CONST();
  3214.  
  3215.         $result = array('X1' => 0, 'X2' => 0, 'Y1' => 0, 'Y2' => 0);
  3216.  
  3217.         $range = $params['range'];
  3218.         $ddmax_result = ($params['range']/$const['distance'])*$const['degree'];
  3219.         $result['X1'] = $params['client_lat'] - $ddmax_result;
  3220.         $result['Y1'] = $params['client_lng'] - $ddmax_result;
  3221.  
  3222.         $result['X2'] = $params['client_lat'] + $ddmax_result;
  3223.         $result['Y2'] = $params['client_lng'] + $ddmax_result;
  3224.  
  3225.         $now_time = date('Y-m-d H:i:s', strtotime('now'));
  3226.         $result['last_up_time'] = CALC_TIME($params['uptime'], '-');
  3227.  
  3228.         return $result;
  3229.     }
  3230.  
  3231.     function driver_nearby()
  3232.     {
  3233.         $payload = file_get_contents('php://input');
  3234.         $json = json_decode( $payload );
  3235.         $response = array();
  3236.  
  3237.         $RANGE = 3; // in Km.
  3238.         $UPTIME = UPTIME; // in Minutes.
  3239.  
  3240.         $gcparams = array(
  3241.             'range' => $RANGE,
  3242.             'uptime' => $UPTIME,
  3243.             'client_lat' => $json->lat,
  3244.             'client_lng' => $json->lng,
  3245.             );
  3246.         $gcresult = $this->_get_coord_nearby($gcparams);
  3247.  
  3248.         $user_type_id = $json->isdrv;
  3249.         $transport = $json->transport;
  3250.  
  3251.         $params = array(
  3252.             'user_type'=> $user_type_id,
  3253.             'transport' => $transport,
  3254.             'X1' => $gcresult['X1'],
  3255.             'X2' => $gcresult['X2'],
  3256.             'Y1' => $gcresult['Y1'],
  3257.             'Y2' => $gcresult['Y2'],
  3258.             'last_up_time' => $gcresult['last_up_time']
  3259.             );
  3260.  
  3261.         $records = $this->model_goexpress->get_driver_nearby($params);
  3262.  
  3263.         if(count($records) > 0 )
  3264.         {
  3265.             $result = new StdClass();
  3266.             $results = array();
  3267.  
  3268.             foreach ($records as $index => $record)
  3269.             {
  3270.                 $result->driver_id = $record->id;
  3271.                 $result->driver_lat = $record->lat;
  3272.                 $result->driver_lng = $record->lng;
  3273.  
  3274.                 $results[$index] = $result;
  3275.                 unset($result);
  3276.             }
  3277.             $response['error'] = 0;
  3278.             $response['result'] = $results;
  3279.         }
  3280.         else
  3281.         {
  3282.             $response['error'] = 1;
  3283.             $response['result'] = null;
  3284.         }
  3285.  
  3286.         die(json_encode($response));
  3287.     }
  3288.  
  3289.     function get_all_driver()
  3290.     {
  3291.  
  3292.         $payload = file_get_contents('php://input');
  3293.         $json = json_decode( $payload );
  3294.         $response = array();
  3295.  
  3296.         $RANGE = 3; // in Km.
  3297.         $UPTIME = UPTIME; // in Minutes.
  3298.  
  3299.         $now_time = date('Y-m-d H:i:s', strtotime('now'));
  3300.         $last_up_time = CALC_TIME($UPTIME, '-');
  3301.         $user_type_id = $json->isdrv;
  3302.         $transport = $json->transport;
  3303.  
  3304.         $params = array(
  3305.             'user_type'=> $user_type_id,
  3306.             'transport' => $transport,
  3307.             'last_up_time' => $last_up_time
  3308.             );
  3309.  
  3310.         $records = $this->model_goexpress->get_driver_nearby($params);
  3311.  
  3312.         if(count($records) > 0 )
  3313.         {
  3314.             $result = new StdClass();
  3315.             $results = array();
  3316.  
  3317.             foreach ($records as $index => $record)
  3318.             {
  3319.                 $result->driver_id = $record->id;
  3320.                 $result->driver_lat = $record->lat;
  3321.                 $result->driver_lng = $record->lng;
  3322.                 $result->driver_name = $record->name;
  3323.                 $result->driver_hp = $record->hp;
  3324.                 $results[$index] = $result;
  3325.                 unset($result);
  3326.             }
  3327.             $response['error'] = 0;
  3328.             $response['result'] = $results;
  3329.         }
  3330.         else
  3331.         {
  3332.             $response['error'] = 1;
  3333.             $response['result'] = null;
  3334.         }
  3335.  
  3336.         die(json_encode($response));
  3337.     }
  3338.  
  3339.     function tx_ack_refresh($txno)
  3340.     {
  3341.         $tparams = array('txno'=>$txno);
  3342.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3343.  
  3344.         if ( isset($txrecord[0]->ack_status_id))
  3345.             die(json_encode(array( 'tx_ack_status' => $txrecord[0]->ack_status_id)));
  3346.         else
  3347.             die(json_encode(array( 'tx_ack_status' => 0)));
  3348.     }
  3349.  
  3350.     function txclient_ack($txno)
  3351.     {
  3352.         error_log('start client ACK ');
  3353.         $ERROR = 1;
  3354.         $SUCCESS = 0;
  3355.         $STR_SUCCESS = "SUCCESS";
  3356.         $CLIENT_ACK = 20;
  3357.         $WORKING = 9;
  3358.         // validate the driver
  3359.  
  3360.         // validate the tx
  3361.         $tparams = array('txno'=>$txno);
  3362.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3363.  
  3364.         if( empty($txrecord) )
  3365.         {
  3366.             $resjson = array(
  3367.                 'error' => $ERROR,
  3368.                 'error_message' => "Error: No TX",
  3369.                 'msgid' => "",
  3370.                 'payload' => "",
  3371.             );
  3372.             error_log('Error '. json_encode($resjson));
  3373.             die( json_encode($resjson) );
  3374.         }
  3375.  
  3376.         $txrecord = $txrecord[0];
  3377.  
  3378.         if( !isset($txrecord->txno) )
  3379.         {
  3380.             $resjson = array(
  3381.                 'error' => $ERROR,
  3382.                 'error_message' => "Error: TX is Broken",
  3383.                 'msgid' => "",
  3384.                 'payload' => "",
  3385.             );
  3386.             die( json_encode($resjson) );
  3387.         }
  3388.  
  3389.         $tsparams = array('ack_status_id' => $CLIENT_ACK );
  3390.         $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3391.  
  3392.         // send gcm to driver
  3393.         $data = array(
  3394.             "client_info" => "tx_ack",
  3395.             "payload" => "",
  3396.         );
  3397.  
  3398.         $json = json_decode($txrecord->payload);
  3399.  
  3400.         $gparams = array('stoken' => $json->data->driver_payload->data->driver_stoken );
  3401.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  3402.  
  3403.         error_log('txclient_ack, gcm '.$msg.' '.$msgid.' '. $json->data->driver_payload->data->driver_stoken);
  3404.         $resjson = array(
  3405.             'error' => $SUCCESS,
  3406.             'error_message' => $STR_SUCCESS,
  3407.             'msgid' => $msgid,
  3408.             'payload' => "",
  3409.         );
  3410.         $retstr = json_encode($resjson);
  3411.         error_log('txclient_ack : '.$txno.' '.$retstr);
  3412.         die( $retstr );
  3413.     }
  3414.  
  3415.     function txclientdriver_ack($txno, $driver_userid)
  3416.     {
  3417.         $ERROR = 1;
  3418.         $SUCCESS = 0;
  3419.         $STR_SUCCESS = "SUCCESS";
  3420.         $CLIENT_ACK = 20;
  3421.         $WORKING = 9;
  3422.         // validate the driver
  3423.  
  3424.         // validate the tx
  3425.         $tparams = array('txno'=>$txno);
  3426.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3427.  
  3428.         if( empty($txrecord) )
  3429.         {
  3430.             $resjson = array(
  3431.                 'error' => $ERROR,
  3432.                 'error_message' => "Error: No TX",
  3433.                 'msgid' => "",
  3434.                 'payload' => "",
  3435.                 'source'=> 'txclientdriver_ack',
  3436.                 'txno'=> $txno,
  3437.             );
  3438.             die( json_encode($resjson) );
  3439.         }
  3440.  
  3441.         $txrecord = $txrecord[0];
  3442.  
  3443.         if( !isset($txrecord->txno) )
  3444.         {
  3445.             $resjson = array(
  3446.                 'error' => $ERROR,
  3447.                 'error_message' => "Error: TX is Broken",
  3448.                 'msgid' => "",
  3449.                 'payload' => "",
  3450.             );
  3451.             die( json_encode($resjson) );
  3452.         }
  3453.  
  3454.         if( $txrecord->ack_status_id != $CLIENT_ACK )
  3455.         {
  3456.             $resjson = array(
  3457.                 'error' => $ERROR,
  3458.                 'error_message' => "Error: Client not ACK",
  3459.                 'msgid' => "",
  3460.                 'payload' => "",
  3461.             );
  3462.             die( json_encode($resjson) );
  3463.         }
  3464.  
  3465.         $resjson = array(
  3466.             'error' => $SUCCESS,
  3467.             'error_message' => $STR_SUCCESS,
  3468.             'msgid' => "",
  3469.             'payload' => "",
  3470.         );
  3471.         die( json_encode($resjson) );
  3472.     }
  3473.  
  3474.     function driver_registration()
  3475.     {
  3476.         // insert the driver
  3477.  
  3478.         // create the verification code
  3479.     }
  3480.  
  3481.     function payload_request( $driver_id, $txno)
  3482.     {
  3483.  
  3484.     }
  3485.  
  3486.     function tx_drivercomplete($txno)
  3487.     {
  3488.         $DRIVER_COMPLETE = 5;
  3489.         $COMPLETE = 3;
  3490.         $WORKING = 9;
  3491.  
  3492.         $payload = file_get_contents('php://input');
  3493.         if ( $payload != null && strlen($payload) > 5) {
  3494.             $payload = json_decode($payload);
  3495.         }
  3496.  
  3497.         $tparams = array('txno'=>$txno);
  3498.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3499.  
  3500.         if( empty($txrecord) )
  3501.         {
  3502.             die("1@@Error: TxNo not registered");
  3503.         }
  3504.         $txrecord = $txrecord[0];
  3505.  
  3506.         if( $txrecord->status_id != $WORKING )
  3507.         {
  3508.             die("1@@Error: TxNo is not in DRIVING status");
  3509.         }
  3510.  
  3511.         $complete_remark = "Driver Complete";
  3512.         if( isset($payload->complete_remark) and $payload->complete_remark !='' )
  3513.             $complete_remark = $payload->complete_remark;
  3514.  
  3515.         $tsparams = array('status_id' => $COMPLETE,
  3516.             'complete_type_id' => $DRIVER_COMPLETE,
  3517.             'complete_timestamp' => date('Y-m-d H:i:s'),
  3518.             'complete_remark' => $complete_remark
  3519.         );
  3520.  
  3521.         $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3522.  
  3523.         // Send GCM
  3524.         $json = json_decode( $txrecord->payload );
  3525.         $data = array(
  3526.             "driver_info" => "tx_completed",
  3527.         );
  3528.  
  3529.         // ******* Update TX_EMAIL ****** //
  3530.         $txeparams = array(
  3531.             'txno' => $txno,
  3532.             'mail_type' => 6,
  3533.             'tx_date' => $txrecord->tx_date,
  3534.             'mail_status' => 99,
  3535.             );
  3536.  
  3537.         list($flag, $id, $msg) = $this->_update_tx_email($txeparams);
  3538.  
  3539.         if(!$flag)
  3540.         {
  3541.             log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'TX_EMAIL with ID:'.$id.' '.$msg, $txeparams));
  3542.         }
  3543.         // ********* END TX_EMAIL ********* //
  3544.  
  3545.         $this->model_goexpress->tx_complete($txrecord);
  3546.  
  3547.         query_log("DriverComplete, Start FCM");
  3548.         query_log("DriverComplete, Client FCM ". @$json->data->SToken);
  3549.  
  3550.         if( isset($json->data->SToken) and $json->data->SToken != OFFICE_LOGIN)
  3551.         {
  3552.             query_log("DriverComplete, Token to sent " . $json->data->SToken);
  3553.             $gparams = array('stoken' => $json->data->SToken );
  3554.             if ( $json->data->lang == "km") {
  3555.  
  3556.                 switch($json->data->Actionmode)
  3557.                 {
  3558.                     case 'TRANSPORT':
  3559.                         $title = "អេច ដឹកជញ្ជូន";
  3560.                         break;
  3561.  
  3562.                     case 'DELIVERY':
  3563.                         $title = "អេច ដឹកទំនិញ";
  3564.                         break;
  3565.  
  3566.                     case 'SHOP':
  3567.                         $title = "អេច ទិញទំនិញ";
  3568.                         break;
  3569.  
  3570.                     default:
  3571.                         $title = $json->data->Actionmode;
  3572.                 }
  3573.                 $bodymsg = "ការបញ្ជារបស់លោកអ្នកត្រូវបានបញ្ចប់";
  3574.             }
  3575.             else {
  3576.                 $title = $json->data->Actionmode;
  3577.                 $bodymsg = "Your order has completed";
  3578.             }
  3579.             $notification = array(
  3580.                         'title' => $title,
  3581.                         'body' => $bodymsg,
  3582.                         'sound' => "default");
  3583.             list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams, $notification );
  3584.             query_log("DriverComplete, result " . $flag. " msg " . $msg ." msgid ". $msgid);
  3585.         }
  3586.         else
  3587.             $msgid = OFFICE_LOGIN;
  3588.  
  3589.         if ( ACTIVITY_RECORD == true ) {
  3590.             $completeDetail = array( "txno" => $txno,
  3591.                                    "complete_date" => TO_MySQL_DATE(),
  3592.                                    "complete_by" => "driver");
  3593.             $activityDetail = array( "type" => "tx_complete",
  3594.                                      "tx" => $completeDetail);
  3595.             $this->model_goexpress->add_activity(json_encode($activityDetail));
  3596.         }
  3597.         die("0@@Success@@". $msgid);
  3598.     }
  3599.  
  3600.     function tx_clientcomplete($txno)
  3601.     {
  3602.         $CLIENT_COMPLETE = 0;
  3603.         $COMPLETE = 3;
  3604.         $WORKING = 9;
  3605.  
  3606.         $payload = file_get_contents('php://input');
  3607.         if ( $payload != null && strlen($payload) > 5) {
  3608.             $payload = json_decode($payload);
  3609.         }
  3610.  
  3611.         $tparams = array('txno'=>$txno);
  3612.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3613.  
  3614.         if( empty($txrecord) )
  3615.         {
  3616.             die("1@@Error: TxNo not registered");
  3617.         }
  3618.         $txrecord = $txrecord[0];
  3619.  
  3620.         if( $txrecord->status_id != $WORKING )
  3621.         {
  3622.             die("1@@Error: TxNo is not in DRIVING status");
  3623.         }
  3624.  
  3625.         $complete_remark = "Client Complete";
  3626.         if( isset($payload->complete_remark) and $payload->complete_remark !='' )
  3627.             $complete_remark = $payload->complete_remark;
  3628.  
  3629.  
  3630.         $tsparams = array('status_id' => $COMPLETE,
  3631.             'complete_type_id' => $CLIENT_COMPLETE,
  3632.             'complete_timestamp' => date('Y-m-d H:i:s'),
  3633.             'complete_remark' => $complete_remark
  3634.         );
  3635.         $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3636.  
  3637.  
  3638.  
  3639.         // Send GCM
  3640.         $json = json_decode( $txrecord->payload );
  3641.         $data = array(
  3642.             "client_info" => "tx_completed",
  3643.             "client_remark" => "Client Completed",
  3644.         );
  3645.  
  3646.         // ***** Update TX_EMAIL ***** //
  3647.         $txeparams = array(
  3648.             'txno' => $txno,
  3649.             'mail_type' => 4,
  3650.             'tx_date' => $txrecord->tx_date,
  3651.             'mail_status' => 99,
  3652.             );
  3653.  
  3654.         list($flag, $id, $msg) = $this->_update_tx_email($txeparams);
  3655.  
  3656.         if(!$flag)
  3657.         {
  3658.             log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'TX_EMAIL with ID:'.$id.' '.$msg, $txeparams));
  3659.         }
  3660.         // ****** END TX_EMAIL ****** //
  3661.  
  3662.         $this->model_goexpress->tx_complete($txrecord);
  3663.  
  3664.         $gparams = array('stoken' => $json->data->driver_payload->data->driver_stoken );
  3665.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  3666.  
  3667.         if ( ACTIVITY_RECORD == true ) {
  3668.             $completeDetail = array( "txno" => $txno,
  3669.                                    "complete_date" => TO_MySQL_DATE(),
  3670.                                    "complete_by" => "client");
  3671.             $activityDetail = array( "type" => "tx_complete",
  3672.                                      "tx" => $completeDetail);
  3673.             $this->model_goexpress->add_activity(json_encode($activityDetail));
  3674.         }
  3675.  
  3676.         die("0@@Success@@". $msgid);
  3677.     }
  3678.  
  3679.     function tx_complete($txno)
  3680.     {
  3681.  
  3682.         $COMPLETE = 3;
  3683.         $WORKING = 9;
  3684.  
  3685.         $tparams = array('txno'=>$txno);
  3686.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3687.  
  3688.         if( empty($txrecord) )
  3689.         {
  3690.             die("1@@Error: TxNo not registered");
  3691.         }
  3692.         $txrecord = $txrecord[0];
  3693.  
  3694.         if( $txrecord->status_id != $WORKING )
  3695.         {
  3696.             die("1@@Error: TxNo is not in DRIVING status");
  3697.         }
  3698.  
  3699.         $tsparams = array('status_id' => $COMPLETE,
  3700.             'complete_timestamp' => date('Y-m-d H:i:s'),
  3701.         );
  3702.         $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3703.  
  3704.         // Send GCM
  3705.         $json = json_decode( $txrecord->payload );
  3706.         $data = array(
  3707.             "driver_info" => "tx_completed",
  3708.         );
  3709.  
  3710.         $this->model_goexpress->tx_complete($txrecord);
  3711.         $gparams = array('stoken' => $json->data->SToken );
  3712.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  3713.  
  3714.         if ( ACTIVITY_RECORD == true ) {
  3715.             $completeDetail = array( "txno" => $txno,
  3716.                                    "complete_date" => TO_MySQL_DATE(),
  3717.                                    "complete_by" => "office");
  3718.             $activityDetail = array( "type" => "tx_complete",
  3719.                                      "tx" => $completeDetail);
  3720.             $this->model_goexpress->add_activity(json_encode($activityDetail));
  3721.         }
  3722.  
  3723.         die("0@@Success@@". $msgid);
  3724.     }
  3725.  
  3726.     function tx_drivercancel($txno)
  3727.     {
  3728.         $payload = file_get_contents('php://input');
  3729.         $payload = json_decode($payload);
  3730.  
  3731.         $CANCEL = 1;
  3732.         $DRIVER_CANCEL= 5;
  3733.         $COMPLETE = 1;
  3734.         $WORKING = 9;
  3735.  
  3736.         $tparams = array('txno'=>$txno);
  3737.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3738.  
  3739.         if( empty($txrecord) )
  3740.         {
  3741.             die("1@@Error: TxNo not registered");
  3742.         }
  3743.         $txrecord = $txrecord[0];
  3744.  
  3745.         if( $txrecord->status_id != $WORKING )
  3746.         {
  3747.             //die("1@@Error: TxNo is not in DRIVING status");
  3748.         }
  3749.  
  3750.         $cancel_remark = "Driver cancel";
  3751.         if( isset($payload->cancel_remark) and $payload->cancel_remark !='' )
  3752.             $cancel_remark = $payload->cancel_remark;
  3753.  
  3754.         $tsparams = array('status_id' => $CANCEL,
  3755.             'cancel_type_id' => $DRIVER_CANCEL,
  3756.             'complete_timestamp' => date('Y-m-d H:i:s'),
  3757.             'cancel_remark' => $cancel_remark,
  3758.         );
  3759.         $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3760.  
  3761.         // Send GCM
  3762.         $json = json_decode( $txrecord->payload );
  3763.         $data = array(
  3764.             "driver_info" => "tx_canceled",
  3765.             "driver_remark" => $cancel_remark,
  3766.         );
  3767.  
  3768.         // ******* Update TX_EMAIL ****** //
  3769.         $txeparams = array(
  3770.             'txno' => $txno,
  3771.             'mail_type' => 5,
  3772.             'tx_date' => $txrecord->tx_date,
  3773.             'mail_status' => 99,
  3774.             );
  3775.  
  3776.         list($flag, $id, $msg) = $this->_update_tx_email($txeparams);
  3777.  
  3778.         if(!$flag)
  3779.         {
  3780.             log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'TX_EMAIL with ID:'.$id.' '.$msg, $txeparams));
  3781.         }
  3782.         // ********* END TX_EMAIL ********* //
  3783.  
  3784.         if( $json->data->SToken != OFFICE_LOGIN)
  3785.         {
  3786.             $gparams = array('stoken' => $json->data->SToken );
  3787.             if ( $json->data->lang == "km") {
  3788.                 switch($json->data->Actionmode)
  3789.                 {
  3790.                     case 'TRANSPORT':
  3791.                         $title = "អេច ដឹកជញ្ជូន";
  3792.                         break;
  3793.  
  3794.                     case 'DELIVERY':
  3795.                         $title = "អេច ដឹកទំនិញ";
  3796.                         break;
  3797.  
  3798.                     case 'SHOP':
  3799.                         $title = "អេច ទិញទំនិញ";
  3800.                         break;
  3801.  
  3802.                     default:
  3803.                         $title = $json->data->Actionmode;
  3804.                 }
  3805.                 $bodymsg = "ការបញ្ជារបស់លោកអ្នកត្រូវបានលុបចោល";
  3806.             }
  3807.             else {
  3808.                 $title = $json->data->Actionmode;
  3809.                 $bodymsg = "Your order has canceled";
  3810.             }
  3811.  
  3812.             if ( ACTIVITY_RECORD == true ) {
  3813.                 $cancelDetail = array( "txno" => $txno,
  3814.                                        "cancel_date" => TO_MySQL_DATE(),
  3815.                                        "cancel_by" => "driver",
  3816.                                        "cancel_remark" => $cancel_remark);
  3817.                 $activityDetail = array( "type" => "tx_cancel",
  3818.                                          "tx" => $cancelDetail);
  3819.                 $this->model_goexpress->add_activity(json_encode($activityDetail));
  3820.             }
  3821.             $notification = array(
  3822.                             'title' => $title,
  3823.                             'body' => $bodymsg,
  3824.                             'sound' => 'default');
  3825.             list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams, $notification );
  3826.         }
  3827.         else
  3828.             $msgid = OFFICE_LOGIN;
  3829.  
  3830.         die("0@@Success@@". $msgid);
  3831.     }
  3832.  
  3833.     function tx_clientcancel($txno)
  3834.     {
  3835.         $CLIENT_CANCEL = 0;
  3836.         $CANCEL = 1;
  3837.         $COMPLETE = 1;
  3838.         $WORKING = 9;
  3839.  
  3840.         $payload = file_get_contents('php://input');
  3841.         if ( $payload != null && strlen($payload) > 5) {
  3842.             $payload = json_decode($payload);
  3843.         }
  3844.  
  3845.       //  var_dump($payload);
  3846.         $tparams = array('txno'=>$txno);
  3847.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3848.  
  3849.         if( empty($txrecord) )
  3850.         {
  3851.             die("1@@Error: TxNo not registered");
  3852.         }
  3853.  
  3854.         $cancel_remark = "Client cancel";
  3855.         if( isset($payload->cancel_remark) and $payload->cancel_remark !='' )
  3856.             $cancel_remark = $payload->cancel_remark;
  3857.  
  3858.         $txrecord = $txrecord[0];
  3859.         $tsparams = array('status_id' => $CANCEL,
  3860.             'cancel_type_id' => $CLIENT_CANCEL,
  3861.             'complete_timestamp' => date('Y-m-d H:i:s'),
  3862.             'cancel_remark' => $cancel_remark,
  3863.         );
  3864.         $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3865.  
  3866.         // Send GCM
  3867.         $json = json_decode( $txrecord->payload );
  3868.         $data = array(
  3869.             "client_info" => "tx_canceled",
  3870.             "client_remark" => "Client Canceled",
  3871.         );
  3872.  
  3873.         // Checking TriggerType
  3874.         $cancel_onwait = 2;
  3875.         $cancel_by_cust = 3;
  3876.         if($txrecord->driver_username == '')
  3877.             $email_type = $cancel_onwait;
  3878.         else
  3879.             $email_type = $cancel_by_cust;
  3880.  
  3881.         // ******** Update TX_EMAIL ******** //
  3882.         $txeparams = array(
  3883.             'txno' => $txno,
  3884.             'mail_type' => $email_type,
  3885.             'tx_date' => $txrecord->tx_date,
  3886.             'mail_status' => 99,
  3887.             );
  3888.  
  3889.         list($flag, $id, $msg) = $this->_update_tx_email($txeparams);
  3890.  
  3891.         if(!$flag)
  3892.         {
  3893.             log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'TX_EMAIL with ID:'.$id.' '.$msg, $txeparams));
  3894.         }
  3895.         // ************ END TX_EMAIL *********** //
  3896.  
  3897.         if( isset( $json->data->driver_payload) )
  3898.         {
  3899.             $gparams = array('stoken' => $json->data->driver_payload->data->driver_stoken );
  3900.             list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  3901.             if ( ACTIVITY_RECORD == true ) {
  3902.                 $cancelDetail = array( "txno" => $txno,
  3903.                                        "cancel_date" => TO_MySQL_DATE(),
  3904.                                        "cancel_by" => "client",
  3905.                                        "cancel_remark" => $cancel_remark);
  3906.                 $activityDetail = array( "type" => "tx_cancel",
  3907.                                          "tx" => $cancelDetail);
  3908.                 $this->model_goexpress->add_activity(json_encode($activityDetail));
  3909.             }
  3910.         }
  3911.         else
  3912.         {
  3913.             $msgid='{"message_id": -1, "message" : "No Driver Payload, this is no driver cancellation"}';
  3914.         }
  3915.  
  3916.         die("0@@Success@@". $msgid);
  3917.     }
  3918.  
  3919.     function tx_cancel($txno)
  3920.     {
  3921.         $CANCEL = 1;
  3922.         $COMPLETE = 1;
  3923.         $WORKING = 9;
  3924.  
  3925.         $tparams = array('txno'=>$txno);
  3926.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3927.  
  3928.         if( empty($txrecord) )
  3929.         {
  3930.             die("1@@Error: TxNo not registered");
  3931.         }
  3932.         $txrecord = $txrecord[0];
  3933.  
  3934.         if( $txrecord->status_id != $WORKING )
  3935.         {
  3936.         //    die("1@@Error: TxNo is not in DRIVING status");
  3937.         }
  3938.  
  3939.         $tsparams = array('status_id' => $CANCEL,
  3940.             'complete_timestamp' => date('Y-m-d H:i:s'),
  3941.         );
  3942.         $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  3943.  
  3944.         // Send GCM
  3945.         $json = json_decode( $txrecord->payload );
  3946.         $data = array(
  3947.             "driver_info" => "tx_canceled",
  3948.         );
  3949.         // FIXME sent gcm to driver
  3950.         $gparams = array('stoken' => $json->data->SToken );
  3951.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  3952.  
  3953.         if ( ACTIVITY_RECORD == true ) {
  3954.             $cancelDetail = array( "txno" => $txno,
  3955.                                    "cancel_date" => TO_MySQL_DATE(),
  3956.                                    "cancel_by" => "office");
  3957.             $activityDetail = array( "type" => "tx_cancel",
  3958.                                      "tx" => $cancelDetail);
  3959.             $this->model_goexpress->add_activity(json_encode($activityDetail));
  3960.         }
  3961.  
  3962.         die("0@@Success@@". $msgid);
  3963.     }
  3964.  
  3965.     function tx_refresh($txno)
  3966.     {
  3967.         $tparams = array('txno'=>$txno);
  3968.         $txrecord = $this->model_goexpress->get_tx($tparams);
  3969.         $driver_latlng = $this->model_goexpress->get_driver_last_latlng($txno);
  3970.  
  3971.         if(count($txrecord) > 0 )
  3972.         {
  3973.             $result = new StdClass();
  3974.             $result->tx_ack = $txrecord[0]->ack_status_id;
  3975.             $result->status_id = $txrecord[0]->status_id;
  3976.  
  3977.             if( isset($driver_latlng->lat) )
  3978.             {
  3979.                 $result->driver_lat = $driver_latlng->lat;
  3980.                 $result->driver_lng = $driver_latlng->lng;
  3981.             }
  3982.             else
  3983.             {
  3984.                 $result->driver_lat = "";
  3985.                 $result->driver_lng = "";
  3986.             }
  3987.  
  3988.             die(json_encode($result));
  3989.         }
  3990.         else
  3991.             die();
  3992.     }
  3993.  
  3994.     function proxy_gcm($userid, $salt = null, $authtoken = null)
  3995.     {
  3996.         $ERROR = 1;
  3997.         $SUCCESS = 0;
  3998.  
  3999.         $payload = file_get_contents('php://input');
  4000.  
  4001.         //error_log('proxy_gcm logging');
  4002.         //
  4003.         if( !$payload )
  4004.         {
  4005.             $resultjson = array(
  4006.                 'error' => 1,
  4007.                 'error_message' => 'Empty payload',
  4008.             );
  4009.  
  4010.             $retstr = json_encode($resultjson);
  4011.             error_log('proxy_gcm error ' . $retstr);
  4012.             die( $retstr );
  4013.         }
  4014.  
  4015.         $json = json_decode($payload);
  4016.         //error_log('proxy_gcm txno ' . $json->data->txno );
  4017.         if( !isset($json->data->SToken) OR strlen($json->data->SToken) == 0)
  4018.         {
  4019.             $tparams = array('txno'=> $json->data->txno);
  4020.             $txrecord = $this->model_goexpress->get_tx($tparams);
  4021.  
  4022.             if( empty($txrecord) )
  4023.             {
  4024.                 $resjson = array(
  4025.                     'error' => $ERROR,
  4026.                     'error_message' => "Error: No TX",
  4027.                     'driver_info' => 'driver_map',
  4028.                     'driver_lat' => '',
  4029.                     'driver_lng' => '',
  4030.                     'SToken' => '',
  4031.                 );
  4032.                 die( json_encode($resjson) );
  4033.             }
  4034.  
  4035.             $txrecord = $txrecord[0];
  4036.             $jsonrecord = json_decode($txrecord->payload);
  4037.             $SToken = $jsonrecord->data->SToken;
  4038.             //error_log('proxy_gcm record ' . $txrecord->SToken);
  4039.             //error_log('proxy_gcm payload ' . serialize($json->data));
  4040.         }
  4041.         else
  4042.             $SToken = $json->data->SToken;
  4043.  
  4044.         //error_log('proxy_gcm SToken ' . $SToken);
  4045.  
  4046.         if( isset($json->data->txno) and $json->data->txno != '' )
  4047.         {
  4048.             error_log('proxy_gcm txno in pre insert ' . $json->data->txno);
  4049.             $dparams = array(
  4050.                 'tstamp' => date('Y-m-d H:i:s'),
  4051.                 'txno' => $json->data->txno,
  4052.                 'driver_id' => $userid,
  4053.                 'lat'=> $json->data->driver_lat,
  4054.                 'lng'=> $json->data->driver_lng,
  4055.             );
  4056.             list($flag, $void, $msg) = $this->model_goexpress->save_driving_log($dparams);
  4057.             error_log(sprintf('Result %s of %s ', $flag, $msg));
  4058.         }
  4059.  
  4060.         if( $SToken != OFFICE_LOGIN)
  4061.         {
  4062.             $resultjson = array(
  4063.                 'error' => 0,
  4064.                 'error_message' => 'Success',
  4065.                 'driver_info' => 'driver_map',
  4066.                 //'driver_lat' => "-6.1111",
  4067.                 'driver_lat' => $json->data->driver_lat,
  4068.                 //'driver_lng' => "116.787878",
  4069.                 'driver_lng' => $json->data->driver_lng,
  4070.                 'SToken' => $SToken,
  4071.             );
  4072.  
  4073.             $params = array('stoken' => $SToken);
  4074.             $this->sendFirebaseCloudMessage2($resultjson, $params);
  4075.             die(json_encode($resultjson));
  4076.         }
  4077.         else
  4078.         {
  4079.             $resultjson = array(
  4080.                 'error' => 0,
  4081.                 'error_message' => 'Success, '.OFFICE_LOGIN,
  4082.                 'data' => '',
  4083.             );
  4084.  
  4085.             die(json_encode($$resultjson));
  4086.         }
  4087.     }
  4088.  
  4089.     public function gox_promotion(){
  4090.         $promo = $this->model_goexpress->get_promotion();
  4091.         if ( $promo != null ) {
  4092.             die(json_encode($promo));
  4093.         }
  4094.         else {
  4095.             die(array( 'has_promotion' => 0,
  4096.                        'link' => "",
  4097.                        'redirect' => 0));
  4098.         }
  4099.     }
  4100.  
  4101.     public function promotion_page($userid)
  4102.     {
  4103.         if ( $this->model_goexpress->check_has_promotion() ) {
  4104.             $this->linkPage  = site_url("goexpress/promotion_page/$userid/");
  4105.             $this->segment   = 4;
  4106.             $this->perPage   = 10;
  4107.             $params['offset']= $this->start_page($this->segment);
  4108.             $params['limit'] = $this->perPage;
  4109.             $params['userid']= $userid;
  4110.             $this->totalPage = $this->model_goexpress->getLuckyPromo($params, true);
  4111.  
  4112.             $data['records'] = $this->model_goexpress->getLuckyPromo($params, false);
  4113.             $data['total']   = $this->totalPage;
  4114.             $data['start']   = $params['offset']+1;
  4115.             $data['paging']  = $this->pagination();
  4116.  
  4117.             //$data['records'] = $this->model_goexpress->getLuckyPromo(array( 'userid' => $userid));
  4118.             $this->load->view("webview/lucky_draw",$data);
  4119.         }
  4120.         else
  4121.             $this->load->view("webview/no_promotion"); // temporary when no promotion
  4122.     }
  4123.  
  4124.     public function driver_meet($txno)
  4125.     {
  4126.         $tparams = array('txno'=>$txno);
  4127.         $txrecord = $this->model_goexpress->get_tx($tparams);
  4128.         $txrecord = $txrecord[0];
  4129.         $json = json_decode($txrecord->payload);
  4130.         if ( $json->data->lang == "km") {
  4131.             switch($json->data->Actionmode)
  4132.             {
  4133.                 case 'TRANSPORT':
  4134.                     $title = "អេច ដឹកជញ្ជូន";
  4135.                     $msg = "អ្នកបើកបររបស់ GO-X បានត្រៀមខ្លួនរួចរាល់ក្នុងការដឹកជញ្ជូនលោកអ្នកហើយ";
  4136.                     break;
  4137.  
  4138.                 case 'DELIVERY':
  4139.                     $title = "អេច ដឹកទំនិញ";
  4140.                     $msg = "អ្នកបើកបររបស់ GO-X បានត្រៀមខ្លួនរួចរាល់ក្នុងការដឹកជញ្ជូនទំនិញរបស់លោកអ្នកហើយ";
  4141.                     break;
  4142.  
  4143.                 case 'SHOP':
  4144.                     $title = "អេច ទិញទំនិញ";
  4145.                     $msg = "អ្នកបើកបររបស់ GO-X បានត្រៀមខ្លួនរួចរាល់ក្នុងការដើរទិញទំនិញជូនលោកអ្នកហើយ";
  4146.  
  4147.                 default:
  4148.                     $title = $json->data->Actionmode;
  4149.                     $msg = "អ្នកបើកបររបស់ GO-X បានត្រៀមខ្លួនរួចរាល់ក្នុងការទទួលការបញ្ជារបស់លោកអ្នកហើយ";
  4150.             }
  4151.         }
  4152.         else {
  4153.             switch($json->data->Actionmode)
  4154.             {
  4155.                 case 'TRANSPORT':
  4156.                     $title = "TRANSPORT";
  4157.                     $msg = "Your Driver ready to transport";
  4158.                     break;
  4159.  
  4160.                 case 'DELIVERY':
  4161.                     $title = "DELIVERY";
  4162.                     $msg = "Your Driver ready to delivery";
  4163.                     break;
  4164.  
  4165.                 case 'SHOP':
  4166.                     $title = "SHOP";
  4167.                     $msg = "Your Driver ready to start shopping";
  4168.  
  4169.                 default:
  4170.                     $title = $json->data->Actionmode;
  4171.                     $msg = "Your Driver ready to carry your order";
  4172.             }
  4173.         }
  4174.  
  4175.         $uparams = array('has_meet' => 1 );
  4176.         $this->model_goexpress->update_tx($uparams, $txrecord->id);
  4177.  
  4178.         $resultjson = array(
  4179.             'error' => 0,
  4180.             'error_message' => 'Success',
  4181.             'driver_info' => 'driver_meet',
  4182.             'driver_remark' => $msg,
  4183.             'SToken' => $json->data->SToken,
  4184.         );
  4185.  
  4186.         $notification = array(
  4187.                         "title" => $title,
  4188.                         "body" => $msg,
  4189.                         "sound" => "default"
  4190.         );
  4191.  
  4192.         if ( ACTIVITY_RECORD == true ) {
  4193.             $meetDetail = array("txno" => $txno,
  4194.                                 "driver_username" => $txrecord->driver_username);
  4195.             $activityDetail = array("type" => "driver_meet",
  4196.                                     "meet" => $meetDetail);
  4197.             $this->model_goexpress->add_activity(json_encode($activityDetail));
  4198.         }
  4199.  
  4200.         die(json_encode(
  4201.                     $this->sendFirebaseCloudMessage2($resultjson, array('stoken' => $json->data->SToken), $notification)
  4202.             ));
  4203.     }
  4204.  
  4205.     public function driver_meet2($txno)
  4206.     {
  4207.         $tparams = array('txno'=>$txno);
  4208.         $txrecord = $this->model_goexpress->get_tx($tparams);
  4209.         $txrecord = $txrecord[0];
  4210.         $json = json_decode($txrecord->payload);
  4211.  
  4212.         switch($json->data->Actionmode)
  4213.         {
  4214.             case 'TRANSPORT':
  4215.                 $msg = "Your Driver ready to transport";
  4216.                 break;
  4217.  
  4218.             case 'DELIVERY':
  4219.                 $msg = "Your Driver ready to delivery";
  4220.                 break;
  4221.  
  4222.             case 'SHOP':
  4223.                 $msg = "Your Driver ready to shop";
  4224.  
  4225.             default:
  4226.                 $msg = "Your Driver ready to carry your order";
  4227.         }
  4228.  
  4229.         $uparams = array('has_meet' => 1 );
  4230.         $this->model_goexpress->update_tx($uparams, $txrecord->id);
  4231.  
  4232.         $resultjson = array(
  4233.             'error' => 0,
  4234.             'error_message' => 'Success',
  4235.             'driver_info' => 'driver_meet',
  4236.             'driver_remark' => $msg,
  4237.             'SToken' => $json->data->SToken,
  4238.         );
  4239.         die(json_encode(
  4240.                     $this->sendFirebaseCloudMessageIOS2($resultjson, array('stoken' => $json->data->SToken))
  4241.             ));
  4242.     }
  4243.  
  4244.     public function get_driver_marker($user_id, $txno){
  4245.  
  4246.         if( $user_id == -1 )
  4247.         {
  4248.             $resjson = array('error' => 1, 'error_message' => 'Error: UserID is -1 meaning no driver');
  4249.             die(json_encode($resjson));
  4250.         }
  4251.  
  4252.         $makers = array();
  4253.         $tparams    = array('driver_id'=>$user_id, 'txno' => $txno);
  4254.         $mapsrecord = $this->model_goexpress->get_driving_log($tparams);
  4255.         foreach ($mapsrecord as $row) {
  4256.             $makers['steps'][] = array('lat' => $row['lat'], 'lng' => $row['lng']);
  4257.             $makers['driver']   = $row['name'];
  4258.         }
  4259.  
  4260.         echo json_encode($makers);
  4261.     }
  4262.  
  4263.     function save_remark()
  4264.     {
  4265.         $this->gox_acl->validate_update();
  4266.         $OFFICE_CANCEL_or_COMPLETE = 10;
  4267.  
  4268.         $params['tx_id'] = $this->input->post('tx_no');
  4269.         $params['claim_id'] = $this->input->post('tx_id');
  4270.  
  4271.         $txrecord = $this->model_goexpress->get_tx($params);
  4272.  
  4273.         if( empty($txrecord) )
  4274.         {
  4275.             die("1@@Error: TxNo not registered");
  4276.         }
  4277.         $txrecord = $txrecord[0];
  4278.         query_log("Referal Value is ".$txrecord->is_referal);
  4279.         query_log("Tx data is ". json_encode($txrecord));
  4280.         if ( $txrecord->is_referal == 1 ) {
  4281.             $dataReferal = array(   'id' => $txrecord->client_id,
  4282.                                     'promo_code' => $txrecord->promo_code
  4283.                                     );
  4284.             $status = $this->model_goexpress->add_referal($dataReferal);
  4285.             query_log("Referal Work with status $status");
  4286.         }
  4287.  
  4288.         $push['status_id'] = $this->input->post('status');
  4289.         if($push['status_id'] == 1){
  4290.             $push['cancel_remark'] = $this->input->post('remark');
  4291.             $push['cancel_type_id'] = $OFFICE_CANCEL_or_COMPLETE;
  4292.         }
  4293.  
  4294.         if($push['status_id'] == 3){
  4295.             $push['complete_timestamp'] = TO_MySQL_DATE();
  4296.             $push['complete_remark'] = $this->input->post('remark');
  4297.             $push['complete_type_id'] = $OFFICE_CANCEL_or_COMPLETE;
  4298.         }
  4299.  
  4300.         // *** Checking TriggerType *** //
  4301.         $cancel_by_office = 7;
  4302.         $comp_by_office = 8;
  4303.  
  4304.         if($push['status_id'] == 1)
  4305.             $email_type = $cancel_by_office;
  4306.         else
  4307.             $email_type = $comp_by_office;
  4308.         // ******* Update TX_EMAIL ****** //
  4309.         $txeparams = array(
  4310.             'txno' => $txrecord->txno,
  4311.             'mail_type' => $email_type,
  4312.             'tx_date' => $txrecord->tx_date,
  4313.             'mail_status' => 99,
  4314.             );
  4315.  
  4316.         list($flag, $id, $msg) = $this->_update_tx_email($txeparams);
  4317.  
  4318.         if(!$flag)
  4319.         {
  4320.             log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'TX_EMAIL with ID:'.$id.' '.$msg, $txeparams));
  4321.         }
  4322.         // ********* END TX_EMAIL ********* //
  4323.  
  4324.         if ($this->model_goexpress->save_remark($params, $push) == true) {
  4325.             $data['message']    = 'successfully';
  4326.             $data['status']     = 'true';
  4327.         } else {
  4328.             $data['message']    = 'failed';
  4329.             $data['status']     = 'false';
  4330.         }
  4331.  
  4332.         echo json_encode($data);
  4333.     }
  4334.  
  4335.     function slink()
  4336.     {
  4337.         echo '<a href="http://bit.ly/1nH5HD6">Download</a>';
  4338.         die();
  4339.     }
  4340.  
  4341.     function _return_json($arr)
  4342.     {
  4343.         die(json_encode($arr));
  4344.     }
  4345.  
  4346.     function tx_rate($txno = null,$irating = 0)
  4347.     {
  4348.         $payload = file_get_contents('php://input');
  4349.         $json = json_decode($payload);
  4350.  
  4351.         if(is_null($txno))
  4352.         {
  4353.             $arr = array(
  4354.                 'error' => 1,
  4355.                 'error_message' => 'Error: TXNO is NULL',
  4356.             );
  4357.             return $this->_return_json($arr);
  4358.         }
  4359.  
  4360.         $params['txno'] = $txno;
  4361.         $records = $this->model_goexpress->get_tx($params);
  4362.         if( empty($records) )
  4363.         {
  4364.             $arr = array(
  4365.                 'error' => 1,
  4366.                 'error_message' => 'Error: No data with this txno',
  4367.             );
  4368.             return $this->_return_json($arr);
  4369.         }
  4370.         $record = $records[0];
  4371.  
  4372.         $tsparams = array('rating' => $irating,
  4373.         );
  4374.         list($flag, $void, $msg) = $this->model_goexpress->add_tx($tsparams, $record->id);
  4375.  
  4376.         $dparams = (array)$json;
  4377.         $dparams['tx_id'] = $record->id;
  4378.  
  4379.         list($flag, $void, $msg) = $this->model_goexpress->add_tx_survey($dparams);
  4380.  
  4381.         if( $flag )
  4382.             $arr['error'] = 0;
  4383.         else
  4384.             $arr['error'] = 1;
  4385.  
  4386.         $arr['error_message'] = $msg;
  4387.         return $this->_return_json($arr);
  4388.     }
  4389.  
  4390.     //fungsi khusus untuk mengextrack payload dan mengisi driver_username dan driver_name yang kosong di tx
  4391.     function extract_payload_tx() {
  4392.         $tparams = array('txno' => $this->uri->segment(3) );
  4393.         $txrecord = $this->model_goexpress->get_tx($tparams);
  4394.  
  4395.         foreach ( $txrecord as $row ) {
  4396.  
  4397.             $json = json_decode($row->payload);
  4398.  
  4399.             if( count($json) > 0 ) {
  4400.  
  4401.                 $data = array(
  4402.                     'driver_username' => $json->data->UserID,
  4403.                     'driver_name'     => $json->data->UserName
  4404.                 );
  4405.  
  4406.                 list($flag, $id, $msg ) = $this->model_goexpress->update_tx($data, $row->id);
  4407.  
  4408.                 if( $flag )
  4409.                 {
  4410.                     echo 'successfully : '.$row->id.'<br>';
  4411.                     $this->db->trans_commit();
  4412.                 }
  4413.                 else
  4414.                 {
  4415.                     echo 'failed : '.$msg.'<br>';
  4416.                     $this->db->trans_rollback();
  4417.                 }
  4418.             }else{
  4419.                 print_r($json);
  4420.             }
  4421.         }
  4422.     }
  4423.  
  4424.     function check_driver_ack($txno)
  4425.     {
  4426.         error_log('check driver ack logging');
  4427.  
  4428.         $CLIENT_ACK = 20;
  4429.         $ERROR = 1;
  4430.         $OPEN = 0;
  4431.         $WORKING =
  4432.             $DRIVING = 9;
  4433.         $DRIVER_ACK = 10;
  4434.  
  4435.         // validate the tx
  4436.         $tparams = array('txno'=>$txno);
  4437.         $txrecord = $this->model_goexpress->get_tx($tparams);
  4438.  
  4439.         if( empty($txrecord) )
  4440.         {
  4441.             $resjson = array(
  4442.                 'error' => $ERROR,
  4443.                 'error_message' => "Error: No TX",
  4444.                 'msgid' => "",
  4445.                 'payload' => "",
  4446.             );
  4447.             die( json_encode($resjson) );
  4448.         }
  4449.  
  4450.         $txrecord = $txrecord[0];
  4451.  
  4452.         if( !isset($txrecord->txno) )
  4453.         {
  4454.             $resjson = array(
  4455.                 'error' => $ERROR,
  4456.                 'error_message' => "Error: TX is Broken",
  4457.                 'msgid' => "",
  4458.                 'payload' => "",
  4459.             );
  4460.             die( json_encode($resjson) );
  4461.         }
  4462.  
  4463.         $json = json_decode($txrecord->payload);
  4464.  
  4465.         if( !isset($json->data->driver_payload->data))
  4466.         {
  4467.             $resjson = array(
  4468.                 'error' => 2,
  4469.                 'error_message' => 'Info: waiting a Driver to ACK',
  4470.                 'msgid' => '',
  4471.             );
  4472.  
  4473.             die(json_encode($resjson));
  4474.         }
  4475.  
  4476.         $json_driver = $json->data->driver_payload->data;
  4477.         error_log('ID '.$json_driver->driver_userid.' status_id '.$txrecord->status_id);
  4478.  
  4479.         // validate if it is closed
  4480.         //if( $txrecord->status_id != $OPEN )
  4481.         //{
  4482.         //    $resjson = array(
  4483.         //        'error' => $ERROR,
  4484.         //        'error_message' => "Error: TX is TAKEN",
  4485.         //        'msgid' => "",
  4486.         //        'payload' => "",
  4487.         //    );
  4488.         //    die( json_encode($resjson) );
  4489.         //}
  4490.  
  4491.         $oparams = array('txno' => $txrecord->txno);
  4492.         $officeorder = $this->office_model->get_order($oparams);
  4493.  
  4494.         if( $officeorder->driver_ack == 0)
  4495.         {
  4496.             $resjson = array('error' => 4, 'error_message' => 'Waiting Driver ACK, queueing OFFICE ACK');
  4497.             die(json_encode($resjson));
  4498.         }
  4499.  
  4500.         $resjson = array('error' => 1, 'error_message' => 'Error: default value');
  4501.  
  4502.         $tsparams = array('ack_status_id' => $CLIENT_ACK );
  4503.         $this->model_goexpress->add_tx($tsparams, $txrecord->id);
  4504.  
  4505.         // send gcm to driver
  4506.         $data = array(
  4507.             "client_info" => "tx_ack",
  4508.             "payload" => "",
  4509.         );
  4510.  
  4511.         $json = json_decode($txrecord->payload);
  4512.  
  4513.         $gparams = array('stoken' => $json->data->driver_payload->data->driver_stoken );
  4514.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  4515.  
  4516.         if( $flag == 0)
  4517.         {
  4518.             $resjson = array(
  4519.                 'error' => 0,
  4520.                 'error_message' => 'Success',
  4521.                 'payload' => $json_driver,
  4522.             );
  4523.         }
  4524.         else
  4525.         {
  4526.             $resjson = array(
  4527.                 'error' => 1,
  4528.                 'error_message' => 'Error: ClientACK sent to Driver failed',
  4529.                 'payload' => $json_driver,
  4530.             );
  4531.         }
  4532.  
  4533.         die(json_encode($resjson));
  4534.     }
  4535.  
  4536.     function broadcast_softupdate()
  4537.     {
  4538.         $user_info = $this->session->userdata('user_info');
  4539.  
  4540.         if( !isset($user_info->username))
  4541.             show_404();
  4542.  
  4543.         // send gcm to CLIENT channel
  4544.         $data = array(
  4545.             "driver_info" => "driver_softupdate",
  4546.             "message" => "hello",
  4547.         );
  4548.  
  4549.         $gparams = array('stoken' => '/topics/goxupdate' );
  4550.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  4551.  
  4552.         if( $flag == 0)
  4553.         {
  4554.             $resjson = array(
  4555.                 'error' => 0,
  4556.                 'error_message' => 'Success',
  4557.             );
  4558.         }
  4559.         else
  4560.         {
  4561.             $resjson = array(
  4562.                 'error' => 1,
  4563.                 'error_message' => 'Error: broadcast update failed',
  4564.             );
  4565.         }
  4566.  
  4567.         die(json_encode($resjson));
  4568.     }
  4569.  
  4570.     function broadcast_driver_loginreset()
  4571.     {
  4572.         $user_info = $this->session->userdata('user_info');
  4573.  
  4574.         if( !isset($user_info->username))
  4575.             show_404();
  4576.  
  4577.         // send gcm to CLIENT channel
  4578.         $data = array(
  4579.             "server_info" => "server_loginreset",
  4580.             "message" => "hello",
  4581.         );
  4582.  
  4583.         $gparams = array('stoken' => '/topics/MOTOBIKE' );
  4584.         list($flag, $msg, $msgid ) = $this->sendFirebaseCloudMessage2(  $data, $gparams );
  4585.  
  4586.         if( $flag == 0)
  4587.         {
  4588.             $resjson = array(
  4589.                 'error' => 0,
  4590.                 'error_message' => 'Success',
  4591.             );
  4592.         }
  4593.         else
  4594.         {
  4595.             $resjson = array(
  4596.                 'error' => 1,
  4597.                 'error_message' => 'Error: broadcast update failed',
  4598.             );
  4599.         }
  4600.  
  4601.         die(json_encode($resjson));
  4602.     }
  4603.  
  4604.     function verify_promocode()
  4605.     {
  4606.         $ACTIVE = 0;
  4607.         $payload = file_get_contents('php://input');
  4608.         $json = json_decode( $payload );
  4609.  
  4610.         if( !isset($json->user_id) or $json->user_id == '')
  4611.         {
  4612.             $resjson = array(
  4613.                 'error' => 1,
  4614.                 'error_message' => 'Error: PromoCode invalid',
  4615.                 'error_code' => 1,
  4616.             );
  4617.             die(json_encode($resjson));
  4618.         }
  4619.  
  4620.         if( !isset($json->promo_code) or $json->promo_code == '')
  4621.         {
  4622.             $resjson = array(
  4623.                 'error' => 1,
  4624.                 'error_message' => 'Error: PromoCode invalid',
  4625.                 'error_code' => 2,
  4626.             );
  4627.             die(json_encode($resjson));
  4628.         }
  4629.  
  4630.         if( !isset($json->isclt) or $json->isclt == '')
  4631.         {
  4632.             $resjson = array(
  4633.                 'error' => 1,
  4634.                 'error_message' => 'Error: PromoCode invalid',
  4635.                 'error_code' => 3,
  4636.             );
  4637.             die(json_encode($resjson));
  4638.         }
  4639.  
  4640.         $reqparams = array('username' => $json->user_id );
  4641.         $user_req = $this->model_goexpress->get_user($reqparams);
  4642.  
  4643.         if( isset($user_req->id) )
  4644.         {
  4645.             // continue
  4646.         }
  4647.         else
  4648.         {
  4649.             $resjson = array(
  4650.                 'error' => 1,
  4651.                 'error_message' => 'Error: Requester invalid',
  4652.                 'error_code' => 3,
  4653.             );
  4654.             die(json_encode($resjson));
  4655.         }
  4656.  
  4657.         // *******************************
  4658.         // check if is referral
  4659.         // *******************************
  4660.         $pFlag = false;
  4661.         $isReferralMode = false;
  4662.         $REFERRAL_MODE = 1;
  4663.         $REFERRAL_LEN = 4;
  4664.         $str_promocode = strtolower($json->promo_code);
  4665.         $deduction = 0;
  4666.         if( strlen($str_promocode) == $REFERRAL_LEN)
  4667.         {
  4668.             $isReferralMode = true;
  4669.             $pparams = array('referal_code' => $str_promocode);
  4670.             $user_rec = $this->model_goexpress->get_user($pparams);
  4671.             if(isset($user_rec->id))
  4672.             {
  4673.                 $promo_params = array(
  4674.                     'user_id' => $user_rec->id ,
  4675.                     'dpromo_code' => $str_promocode ,
  4676.                 );
  4677.                 $promo_detail = $this->model_goexpress->get_promo_detail($promo_params);
  4678.                 if( count($promo_detail) == 1 )
  4679.                 {
  4680.                     $promo_detail = $promo_detail[0];
  4681.                     // we have the promo code set to this userid
  4682.                     // but not taken then it is valid
  4683.                     if( isset($promo_detail->id) and $promo_detail->is_used == 0 )
  4684.                         $pFlag = true;
  4685.                 }
  4686.             }
  4687.  
  4688.             if(!isset($user_rec->id) and $pFlag === false)
  4689.             {
  4690.                 $pparams = array('username' => $json->user_id);
  4691.                 $user_rec = $this->model_goexpress->get_user($pparams);
  4692.  
  4693.                 if( isset($user_rec->id))
  4694.                 {
  4695.                     // check if this user already taken someone promo?
  4696.                     $promo_params = array(
  4697.                         'user_id' => $user_rec->id ,
  4698.                         'promo_id' => $REFERRAL_MODE,
  4699.                     );
  4700.                     $promo_detail = $this->model_goexpress->get_promo_detail($promo_params);
  4701.                     if( count($promo_detail) == 0 )
  4702.                     {
  4703.                         // if have not create one
  4704.                         $promo_params = array(
  4705.                             'dpromo_code' => $str_promocode ,
  4706.                             'user_id' => $user_rec->id,
  4707.                             'promo_id' => $REFERRAL_MODE,
  4708.                         );
  4709.                         $promo_detail = $this->model_goexpress->add_promo_detail($promo_params);
  4710.                         $pFlag = true;
  4711.                     }
  4712.                 }
  4713.             }
  4714.         }
  4715.  
  4716.         // *******************************
  4717.         // if not then PROMOCODE
  4718.         // *******************************
  4719.         if( $pFlag === false and $isReferralMode == false)
  4720.         {
  4721.             $promo_params = array(
  4722.                 'idelete' => $ACTIVE ,
  4723.                 'name' => $str_promocode ,
  4724.             );
  4725.             $promo_record = $this->model_goexpress->get_promo_code2($promo_params);
  4726.             if( isset($promo_record->id) )
  4727.             {
  4728.                 $pparams = array('username' => $json->user_id);
  4729.                 $user_rec = $this->model_goexpress->get_user($pparams);
  4730.  
  4731.                 if( isset($user_rec->id))
  4732.                 {
  4733.                     $promo_params = array(
  4734.                         'user_id' => $user_rec->id ,
  4735.                         'dpromo_code' => $str_promocode ,
  4736.                         'is_used' => 0,
  4737.                     );
  4738.                     $promo_detail = $this->model_goexpress->get_promo_detail($promo_params);
  4739.  
  4740.                     if( count($promo_detail) > 0 )
  4741.                     {
  4742.                         $pFlag = true;
  4743.                     }
  4744.                     else
  4745.                     {
  4746.                         $promo_params = array(
  4747.                             'dpromo_code' => $str_promocode ,
  4748.                             'user_id' => $user_rec->id,
  4749.                             'promo_id' => $promo_record->id,
  4750.                         );
  4751.                         $promo_detail = $this->model_goexpress->add_promo_detail($promo_params);
  4752.                         $pFlag = true;
  4753.                     }
  4754.                 }
  4755.             }
  4756.             else
  4757.             {
  4758.                 $resjson = array(
  4759.                     'error' => 1,
  4760.                     'error_message' => 'Invalid PromoCode',
  4761.                     'error_code' => 1,
  4762.                 );
  4763.                 die(json_encode($resjson));
  4764.             }
  4765.         }
  4766.  
  4767.         $params = array(
  4768.             'username'  => $json->user_id,
  4769.             'idelete'   => $ACTIVE,
  4770.         );
  4771.  
  4772.         $user = $this->model_goexpress->get_user($params);
  4773.  
  4774.         if( !isset($user->id) )
  4775.         {
  4776.             $resjson = array(
  4777.                 'error' => 1,
  4778.                 'error_message' => 'Error: PromoCode invalid',
  4779.                 'error_code' => 4,
  4780.             );
  4781.             die(json_encode($resjson));
  4782.         }
  4783.  
  4784.         $resjson = array(
  4785.             'error' => 0,
  4786.             'error_message' => 'Success',
  4787.             'error_code' => 0,
  4788.         );
  4789.         die(json_encode($resjson));
  4790.     }
  4791.  
  4792.     function verify_promocode2()
  4793.     {
  4794.         $ACTIVE = 0;
  4795.         $payload = file_get_contents('php://input');
  4796.         $json = json_decode( $payload );
  4797.  
  4798.         if( !isset($json->user_id) or $json->user_id == '')
  4799.         {
  4800.             $resjson = array(
  4801.                 'error' => 1,
  4802.                 'error_message' => 'Error: PromoCode Invalid',
  4803.                 'error_code' => 1,
  4804.             );
  4805.             die(json_encode($resjson));
  4806.         }
  4807.  
  4808.         if( !isset($json->promo_code) or $json->promo_code == '')
  4809.         {
  4810.             $resjson = array(
  4811.                 'error' => 1,
  4812.                 'error_message' => 'Error: PromoCode Invalid',
  4813.                 'error_code' => 2,
  4814.             );
  4815.             die(json_encode($resjson));
  4816.         }
  4817.  
  4818.         if( !isset($json->isclt) or $json->isclt == '')
  4819.         {
  4820.             $resjson = array(
  4821.                 'error' => 1,
  4822.                 'error_message' => 'Error: PromoCode Invalid',
  4823.                 'error_code' => 3,
  4824.             );
  4825.             die(json_encode($resjson));
  4826.         }
  4827.  
  4828.         $reqparams = array('username' => $json->user_id );
  4829.         $user_req = $this->model_goexpress->get_user($reqparams);
  4830.  
  4831.         if( isset($user_req->id) )
  4832.         {
  4833.             // continue
  4834.         }
  4835.         else
  4836.         {
  4837.             $resjson = array(
  4838.                 'error' => 1,
  4839.                 'error_message' => 'Error: Requester Invalid',
  4840.                 'error_code' => 3,
  4841.             );
  4842.             die(json_encode($resjson));
  4843.         }
  4844.  
  4845.         // ******************************* //
  4846.         // check Promo Code                //
  4847.         // ******************************* //
  4848.         $str_promocode = strtoupper($json->promo_code);
  4849.        
  4850.         $uparams = array('username' => $json->user_id);
  4851.  
  4852.         $user_rec = $this->model_goexpress->get_user($uparams);
  4853.  
  4854.         $promoValue = $this->model_goexpress->getDeduction(
  4855.                                 array("promo_code" => $str_promocode,
  4856.                                       "userid" => $user_rec->id ));
  4857.  
  4858.         if($promoValue == 0) {
  4859.             $resjson = array(
  4860.                 'error' => 1,
  4861.                 'error_message' => 'Error: PromoCode Invalid',
  4862.                 'error_code' => 5,
  4863.             );
  4864.             die(json_encode($resjson));
  4865.         }
  4866.  
  4867.         $params = array(
  4868.             'username'  => $json->user_id,
  4869.             'idelete'   => $ACTIVE,
  4870.         );
  4871.  
  4872.         $user = $this->model_goexpress->get_user($params);
  4873.  
  4874.         if( !isset($user->id) )
  4875.         {
  4876.             $resjson = array(
  4877.                 'error' => 1,
  4878.                 'error_message' => 'Error: PromoCode Invalid',
  4879.                 'error_code' => 4,
  4880.             );
  4881.             die(json_encode($resjson));
  4882.         }
  4883.  
  4884.         $resjson = array(
  4885.             'error' => 0,
  4886.             'error_message' => 'Success',
  4887.             'error_code' => 0,
  4888.         );
  4889.         die(json_encode($resjson));
  4890.     }
  4891.  
  4892.     function promo_deduction()
  4893.     {
  4894.         $payload = file_get_contents('php://input');
  4895.         $json = json_decode( $payload );
  4896.         $REFERRAL_LEN = 4;
  4897.  
  4898.         if( !isset($json->user_id) or $json->user_id == '')
  4899.         {
  4900.             $resjson = array(
  4901.                 'error' => 0,
  4902.                 'error_message' => 'Error: PromoCode invalid',
  4903.                 'error_code' => 1,
  4904.                 'deduction' => 0,
  4905.             );
  4906.             die(json_encode($resjson));
  4907.         }
  4908.  
  4909.         if( !isset($json->promo_code) or $json->promo_code == '')
  4910.         {
  4911.             $resjson = array(
  4912.                 'error' => 0,
  4913.                 'error_message' => 'Error: PromoCode invalid',
  4914.                 'error_code' => 2,
  4915.                 'deduction' => 0,
  4916.             );
  4917.             die(json_encode($resjson));
  4918.         }
  4919.  
  4920.         $resjson = array(
  4921.             'error' => 0,
  4922.             'error_message' => 'Success',
  4923.             'error_code' => 0,
  4924.             'deduction' => 2000,
  4925.         );
  4926.         die(json_encode($resjson));
  4927.     }
  4928.  
  4929.     function driver_token_reg()
  4930.     {
  4931.         $payload = file_get_contents('php://input');
  4932.         $json = json_decode( $payload );
  4933.  
  4934.         list($flag, $skip, $msg) = $this->user_model->update_driver_token_reg($json);
  4935.  
  4936.         $this->user_model->add_driver_token_log($json);
  4937.  
  4938.         if( isset($json->driving_session) AND $json->driving_session == true)
  4939.         {
  4940.             // save into driving log
  4941.             $dparams = array(
  4942.                 'tstamp' => date('Y-m-d H:i:s'),
  4943.                 'txno' => $json->txno,
  4944.                 "driver_id" => $json->user_id,
  4945.                 'lat'=> $json->lat,
  4946.                 'lng'=> $json->lng,
  4947.             );
  4948.             list($flag, $void, $msg) = $this->model_goexpress->save_driving_log($dparams);
  4949.  
  4950.             if(!$flag)
  4951.                 error_log("Save driving log error ". $json->txno);
  4952.         }
  4953.  
  4954.         if ( ACTIVITY_RECORD == true ) {
  4955.             $uparams = array(
  4956.                 'username' => $json->user_id
  4957.             );
  4958.  
  4959.             $driverRecord = $this->model_goexpress->get_user($uparams);
  4960.  
  4961.             $driverDetail = array( "lat" => $json->lat,
  4962.                                    "lng" => $json->lng,
  4963.                                    "username" => $json->user_id,
  4964.                                    "name" => $driverRecord->name,
  4965.                                    "online" => $json->status
  4966.                                    );
  4967.  
  4968.             $activityDetail = array( "type" => "driver_update",
  4969.                                      "client" => $driverDetail);
  4970.             $this->model_goexpress->add_activity(json_encode($activityDetail));
  4971.         }
  4972.         $resjson = array(
  4973.             'error' => REVERSE_iFLAG($flag),
  4974.             'error_message' => $msg
  4975.         );
  4976.  
  4977.         die(json_encode($resjson));
  4978.     }
  4979.  
  4980.     function getrating()
  4981.     {
  4982.         $payload = file_get_contents('php://input');
  4983.         $json = json_decode( $payload ,true);
  4984.  
  4985.         if ( !isset($json['userid']) OR !isset($json['isclt']) OR $json['isclt'] != 50 )
  4986.         {
  4987.             $resjson = array(
  4988.                 'error' => 1,
  4989.                 'error_message' => "Failed to get rating data"
  4990.             );
  4991.             die(json_encode($resjson));
  4992.         }
  4993.         $data =$this->model_goexpress->getRating($json);
  4994.         $resjson = array(
  4995.             'error' => 0,
  4996.             'totalRating' => number_format($data['totalRating'],2,",","."),
  4997.             'lifetimeTrips' => $data['lifetimeTrips'],
  4998.             'ratedTrips'    => $data['ratedTrips'],
  4999.             'fiveStars'     => $data['fiveStars'],
  5000.             'error_message' => "Failed to get rating data"
  5001.         );
  5002.         die(json_encode($resjson));
  5003.     }
  5004.  
  5005.     //function place_autocomplete($str)
  5006.     //{
  5007.     //    $uri = sprintf("https://maps.googleapis.com/maps/api/place/autocomplete/json/?key=%s&components=%s&input=%s",
  5008.     //                "AIzaSyAZJF74FLkXGZ4VhXXRetiO8gaMlL6lsZI",
  5009.     //                "country:kh",
  5010.     //                $str
  5011.     //            );
  5012.     //}
  5013.  
  5014.     function earning($userid=null)
  5015.     {
  5016.         if ( isset($_GET["userid"])) {
  5017.             $userid = $_GET["userid"];
  5018.         }
  5019.         $payload = file_get_contents('php://input');
  5020.         $this->linkPage  = site_url("goexpress/earning/".$userid."/");
  5021.         $this->segment   = 4;
  5022.         $this->perPage   = 10;
  5023.         $params['userid']= $userid;
  5024.         $params['offset']= $this->start_page($this->segment);
  5025.         $params['limit'] = $this->perPage;
  5026.  
  5027.         $this->totalPage = $this->model_goexpress->get_earning_data($params, true);
  5028.         $records = $this->model_goexpress->get_earning_data($params, false);
  5029.         $data['total']   = $this->totalPage;
  5030.         $data['start']   = $params['offset']+1;
  5031.         $data['paging']  = $this->pagination();
  5032.         $this->load->view('webview/header');
  5033.         $this->load->view('webview/earning', array('earning' => $records, 'data'=> $data ));
  5034.         $this->load->view('webview/footer');
  5035.     }
  5036.  
  5037.     function wallet()
  5038.     {
  5039.         $payload = file_get_contents('php://input');
  5040.         $userid= $this->input->get('userid');
  5041.         $total_wallet =$this->model_goexpress->get_driver_wallet_total($userid);
  5042.         $this->load->view('webview/header');
  5043.         $this->load->view('webview/driver_wallet', array('total_wallet' => $total_wallet));
  5044.         $this->load->view('webview/footer');
  5045.     }
  5046.  
  5047.     function news_update()
  5048.     {
  5049.         $this->load->model('news_model');
  5050.         $params['date'] = date('Y-m-d');
  5051.         $params['status'] = 1;
  5052.         $data['records'] = $this->news_model->get_all($params);
  5053.         $this->load->view('goexpress/news_update', $data);
  5054.     }
  5055.  
  5056.     function send_sms()
  5057.     {
  5058.         $payload = file_get_contents('php://input');
  5059.         $json = json_decode( $payload );
  5060.  
  5061.         $smsparams = array(
  5062.             'update_date' => date('Y-m-d H:i:s'),
  5063.             'isms_status' => $json->isms_status,
  5064.         );
  5065.         list($flag, $void, $msg) = $this->user_model->add_sendsms2($smsparams, $json->user_id);
  5066.  
  5067.         $resjson = array(
  5068.             'error' => REVERSE_iFLAG($flag),
  5069.             'error_message' => $msg,
  5070.         );
  5071.  
  5072.         error_log($json. " resjson ". $resjson);
  5073.  
  5074.         die(json_encode($resjson));
  5075.     }
  5076.  
  5077.     function check_version()
  5078.     {
  5079.         $CLIENT = 90;
  5080.         $DRIVER = 50;
  5081.         $FATAL_ERROR = 2;
  5082.         $PERMISSIVE_ERROR = 1;
  5083.         $SUCCESS = 0;
  5084.  
  5085.         $payload = file_get_contents('php://input');
  5086.         $json = json_decode( $payload );
  5087.  
  5088.         error_log("CHECK_VERSION ". $payload);
  5089.  
  5090.         if ( $this->uri->segment(1) == "km") {
  5091.            $message = "តម្រូវឲ្យធ្វើការទាញយកកំណែទម្រង់ថ្មី";
  5092.         }
  5093.         else {
  5094.            $message = "New Version Needed.";
  5095.         }
  5096.         //COMMENT SIR ADE UPDATE START 1
  5097.         if( $json->isclt == $DRIVER )
  5098.         {
  5099.             query_log("Current version is " . $json->version);
  5100.  
  5101.             if( intval(str_replace(".","",$json->version)) >= I_DRIVER_VERSION )
  5102.             {
  5103.                 $resjson = array(
  5104.                     'error' => $SUCCESS,
  5105.                     'error_message' => 'Your permissive error is working!',
  5106.                 );
  5107.                 die(json_encode($resjson));
  5108.             }
  5109.         }
  5110.         //COMMENT SIR ADE UPDATE END 1
  5111.  
  5112.         if( $json->isclt == $CLIENT )
  5113.         {
  5114.             if( isset($json->is_android) && $json->is_android == 1 )
  5115.             {
  5116.                 if( intval(str_replace(".","",$json->version)) >= I_CLIENT_VERSION_ANDROID )
  5117.                 {
  5118.                     $resjson = array(
  5119.                         'error' => $SUCCESS,
  5120.                         'error_message' => 'Your permissive error is working!',
  5121.                     );
  5122.                     die(json_encode($resjson));
  5123.                 }
  5124.             }
  5125.  
  5126.             if( isset($json->is_ios) && $json->is_ios == 1 )
  5127.             {
  5128.                 if( intval(str_replace(".","",$json->version)) >= I_CLIENT_VERSION_IOS )
  5129.                 {
  5130.                     $resjson = array(
  5131.                         'error' => $SUCCESS,
  5132.                         'error_message' => 'Your permissive error is working!',
  5133.                     );
  5134.                     die(json_encode($resjson));
  5135.                 }
  5136.             }
  5137.  
  5138.  
  5139.             $resjson = array(
  5140.                 'error' => $FATAL_ERROR,
  5141.                 'error_message' => $message,
  5142.             );
  5143.  
  5144.             //$resjson = array(
  5145.             //    'error' => $SUCCESS,
  5146.             //    'error_message' => 'Your permissive error is working!',
  5147.             //);
  5148.             error_log("CHECK_VERSION CLIENT REPLY". json_encode($resjson));
  5149.             die(json_encode($resjson));
  5150.         }
  5151.  
  5152.         //COMMENT SIR ADE UPDATE START 2
  5153.         $resjson = array(
  5154.             'error' => $FATAL_ERROR,
  5155.             'error_message' => $message,
  5156.         );
  5157.         //COMMENT SIR ADE UPDATE END 2
  5158.  
  5159.         error_log("CHECK_VERSION DRIVER REPLY". json_encode($resjson));
  5160.         die(json_encode($resjson));
  5161.     }
  5162.  
  5163.     function tx_refresh2($txno)
  5164.     {
  5165.         $tparams = array('txno'=>$txno);
  5166.         $txrecord = $this->model_goexpress->get_tx($tparams);
  5167.         $driver_latlng = $this->model_goexpress->get_driver_last_latlng($txno);
  5168.  
  5169.         if(count($txrecord) > 0 )
  5170.         {
  5171.             $result = new StdClass();
  5172.             $result->status_id = $txrecord[0]->status_id;
  5173.  
  5174.             if( isset($driver_latlng->lat) )
  5175.             {
  5176.                 $result->driver_lat = $driver_latlng->lat;
  5177.                 $result->driver_lng = $driver_latlng->lng;
  5178.             }
  5179.             else
  5180.             {
  5181.                 $result->driver_lat = "";
  5182.                 $result->driver_lng = "";
  5183.             }
  5184.  
  5185.             die(json_encode($result));
  5186.         }
  5187.         else
  5188.             die();
  5189.     }
  5190.  
  5191.     function my_history($userid)
  5192.     {
  5193.         $this->linkPage  = site_url("goexpress/my_history/$userid/");
  5194.         $this->segment   = 4;
  5195.         $this->perPage   = 10;
  5196.         $params['offset']= $this->start_page($this->segment);
  5197.         $params['limit'] = $this->perPage;
  5198.         $params['userid']= $userid;
  5199.         $this->totalPage = $this->model_goexpress->get_history_data($params, true);
  5200.         $data['history'] = $this->model_goexpress->get_history_data($params, false);
  5201.         $data['total']   = $this->totalPage;
  5202.         $data['start']   = $params['offset']+1;
  5203.         $data['paging']  = $this->pagination();
  5204.         $this->load->view('webview/header');
  5205.         $this->load->view('webview/history', $data);
  5206.         $this->load->view('webview/footer');
  5207.     }
  5208.  
  5209.     function send_email()
  5210.     {
  5211.         $payload = file_get_contents('php://input');
  5212.         query_log($payload);
  5213.         $json = json_decode( $payload );
  5214.         $uparams = array(
  5215.             'username' => $json->username
  5216.         );
  5217.  
  5218.         $user = $this->model_goexpress->get_user($uparams);
  5219.         if ( strlen($json->email_message) < 5 ) {
  5220.             die(json_encode(array( 'error' => 1, 'error_message' => 'Please insert minimal 5 char!'  )));
  5221.         }
  5222.  
  5223.         $to      = 'info@goexpress-kh.com';
  5224.         //$to = 'williamgunawan22@gmail.com';
  5225.         $from = $user->email;
  5226.         //$from = "mailerdaemon@goexpress-kh.com";
  5227.         $subject_text = "Question from customer " . $user->name . " (".$user->hp.")\n";
  5228.         $email_text = $subject_text;
  5229.         $email_text .= $json->email_message;
  5230.  
  5231.         $this->load->library('phpmailer2');
  5232.         query_log($email_text);
  5233.         $this->phpmailer2->addAddress($to, "GoExpress Office");
  5234.         $this->phpmailer2->addFrom($from, $user->name);
  5235.         $this->phpmailer2->subject($subject_text);
  5236.         $this->phpmailer2->message($email_text );
  5237.         if ( !$this->phpmailer2->send() )
  5238.         {
  5239.             query_log( $this->phpmailer2->ErrorInfo);
  5240.             log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'Email not sent', $this->phpmailer2->ErrorInfo));
  5241.         }
  5242.         die(json_encode(array( 'error' => 0, 'error_message' => 'Success to send email to office!' )));
  5243.  
  5244.     }
  5245.  
  5246.     /**
  5247.      * @param Trigger Type
  5248.      * @param TX Number
  5249.      * Email Trigger
  5250.      */
  5251.     function send_email2()
  5252.     {
  5253.         // ******* TRIGGER TYPE ****** //
  5254.         $confirm_order = 1;
  5255.         $cancel_onwait = 2;
  5256.         $cancel_by_cust = 3;
  5257.         $comp_by_cust = 4;
  5258.         $cancel_by_driver = 5;
  5259.         $comp_by_driver = 6;
  5260.         $cancel_by_office = 7;
  5261.         $comp_by_office = 8;
  5262.  
  5263.         $data = array();
  5264.  
  5265.         $payload = file_get_contents('php://input');
  5266.         query_log($payload);
  5267.         $json = json_decode($payload);
  5268.  
  5269.         // NOTE: For Testing without phone
  5270.         // if(empty($json->trigger_type)) {
  5271.         //     $json->trigger_type = $confirm_order;
  5272.         //     $json->txno = '1479112492228'; // tx on demo
  5273.         // }
  5274.  
  5275.         if(empty($json->trigger_type) || empty($json->txno))
  5276.         {
  5277.             echo "JSON Object is not Defined";
  5278.             die();
  5279.         }
  5280.  
  5281.         $txparams = array('txno' => $json->txno);
  5282.         $tx = $this->model_goexpress->get_tx_detail1($txparams);
  5283.         $data_payload = json_decode($tx->payload)->data;
  5284.  
  5285.         if($tx->transport == null)
  5286.         {
  5287.             $tx->transport = $data_payload->Vehicle;
  5288.         }
  5289.  
  5290.         $data['txrecord'] = $tx;
  5291.         $data['payload'] = json_decode($tx->payload);
  5292.         $data['payload'] = $data['payload']->data;
  5293.  
  5294.         $uparams = array(
  5295.             'username' => $data['payload']->UserID
  5296.         );
  5297.         $user = $this->model_goexpress->get_user($uparams);
  5298.  
  5299.         $from = 'info@goexpress-kh.com';
  5300.         $to = $user->email;
  5301.  
  5302.         switch ($json->trigger_type) {
  5303.             case $confirm_order:
  5304.                 $email_subject = lang('MailSubjConfirmation');
  5305.                 $data['item_title'] = $email_subject;
  5306.                 $data['email_info'] = $this->load->view('mail/booking_confirmation', $data, TRUE);
  5307.                 break;
  5308.             case $cancel_onwait:
  5309.                 $email_subject = lang('MailSubjCancelWait');
  5310.                 $data['item_title'] = $email_subject;
  5311.                 $data['email_info'] = $this->load->view('mail/cancel_on_wait', $data, TRUE);
  5312.                 break;
  5313.             case $cancel_by_cust:
  5314.                 $email_subject = lang('MailSubjCancelCust');
  5315.                 $data['item_title'] = $email_subject;
  5316.                 $data['email_info'] = $this->load->view('mail/cancel_by_cust', $data, TRUE);
  5317.                 break;
  5318.             case $cancel_by_driver:
  5319.                 $email_subject = lang('MailSubjCancelDriver');
  5320.                 $data['item_title'] = $email_subject;
  5321.                 $data['email_info'] = $this->load->view('mail/cancel_by_driver', $data, TRUE);
  5322.                 break;
  5323.             case $cancel_by_office:
  5324.                 $email_subject = lang('MailSubjCancelOffice');
  5325.                 $data['item_title'] = $email_subject;
  5326.                 $data['email_info'] = $this->load->view('mail/cancel_by_office', $data, TRUE);
  5327.                 break;
  5328.             case $comp_by_cust:
  5329.                 $email_subject = lang('MailSubjCompCust');
  5330.                 $data['item_title'] = $email_subject;
  5331.                 $data['email_info'] = $this->load->view('mail/complete_by_customer', $data, TRUE);
  5332.                 break;
  5333.             case $comp_by_driver:
  5334.                 $email_subject = lang('MailSubjCompDriver');
  5335.                 $data['item_title'] = $email_subject;
  5336.                 $data['email_info'] = $this->load->view('mail/complete_by_driver', $data, TRUE);
  5337.                 break;
  5338.             case $comp_by_office:
  5339.                 $email_subject = lang('MailSubjCompOffice');
  5340.                 $data['item_title'] = $email_subject;
  5341.                 $data['email_info'] = $this->load->view('mail/complete_by_office', $data, TRUE);
  5342.                 break;
  5343.             default:
  5344.                 $email_subject = lang('MailSubjConfirmation');
  5345.                 $data['item_title'] = $email_subject;
  5346.                 $data['email_info'] = $this->load->view('mail/booking_confirmation', $data, TRUE);
  5347.                 break;
  5348.         }
  5349.  
  5350.         $email_message = $this->load->view('mail/mail_template', $data, TRUE);
  5351.  
  5352.         $this->load->library('phpmailer2');
  5353.         query_log($email_message);
  5354.         $this->phpmailer2->addFrom($from, 'GoExpress Server');
  5355.         $this->phpmailer2->addAddress($to, $user->name);
  5356.         $this->phpmailer2->subject($email_subject);
  5357.         $this->phpmailer2->message($email_message);
  5358.         if ( !$this->phpmailer2->send() )
  5359.         {
  5360.             query_log( $this->phpmailer2->ErrorInfo);
  5361.             log_message('error',sprintf('%s (%s): %s, %s', __FILE__,__LINE__,'Email not sent', $this->phpmailer2->ErrorInfo));
  5362.         }
  5363.     }
  5364.  
  5365.     function filter_driver($driver_records)
  5366.     {
  5367.         $DRIVER_TOKEN = 0;
  5368.         $DRIVER_ID = 1;
  5369.         $SEND_THRESHOLD = 5;
  5370.         $NO_DRIVER = 0;
  5371.         $THRESHOLD = 8; // secs
  5372.         $driver_idx = 0;
  5373.         $curtime = time();
  5374.         $ret_array = array();
  5375.         $token_driver = array();
  5376.         $bUse = false;
  5377.  
  5378.         foreach ($driver_records as $index => $driver)
  5379.         {
  5380.             $bUse = false;
  5381.             if( $driver->last_bc == '' or $driver->last_bc == 0)
  5382.                 $bUse = true;
  5383.             else
  5384.             {
  5385.                 $sec_diff = $curtime - $driver->last_bc;
  5386.                 if( $sec_diff > $THRESHOLD )
  5387.                     $bUse = true;
  5388.             }
  5389.  
  5390.             if( $bUse )
  5391.             {
  5392.                 $token_driver[] = array($driver->token, $driver->id);
  5393.                 $driver_idx++;
  5394.             }
  5395.         }
  5396.  
  5397.         if( $driver_idx == $NO_DRIVER )
  5398.         {
  5399.             if( count($driver_records) > 0 )
  5400.             {
  5401.                 sleep(3); // secs
  5402.                 return $this->filter_driver($driver_records);
  5403.             }
  5404.             else
  5405.             {
  5406.                 return array();
  5407.             }
  5408.         }
  5409.         else
  5410.         {
  5411.             $count = floor($driver_idx / 2 );
  5412.             srand();
  5413.  
  5414.             if( $count > $SEND_THRESHOLD )
  5415.                 $count = $SEND_THRESHOLD;
  5416.  
  5417.             if( $count == 0 )
  5418.                 $count = $driver_idx;
  5419.  
  5420.             $numbers = range(1,count($token_driver));
  5421.             shuffle($numbers);
  5422.  
  5423.             for($idx = 0; $idx < $count; $idx++)
  5424.             {
  5425.                 // update lastbc of driver
  5426.                 $drvparams = array('last_bc' => $curtime,
  5427.                                    'bc_action' => $this->bc_action,
  5428.                                    'bc_tx' => $this->bc_txno );
  5429.                 $this->model_goexpress->update_driver_lastbc($drvparams, $token_driver[$numbers[$idx] - 1][$DRIVER_ID]);
  5430.                 if ( ACTIVITY_RECORD == true ) {
  5431.                     $bcList[$idx] = array( "driver_username" => $driver->username);
  5432.                 }
  5433.                 $ret_array[] = $token_driver[$numbers[$idx] - 1][$DRIVER_TOKEN];
  5434.             }
  5435.  
  5436.             if ( ACTIVITY_RECORD == true ) {
  5437.                 $bcData = array( "txno" => $this->bc_txno,
  5438.                                  "list" => $bcList);
  5439.                 $activityDetail = array( "type" => "tx_bc",
  5440.                                          "bc" => $bcData);
  5441.                 $this->model_goexpress->add_activity(json_encode($activityDetail));
  5442.             }
  5443.  
  5444.             return $ret_array;
  5445.         }
  5446.     }
  5447.  
  5448.     function promotion_banner($last_id = NULL)
  5449.     {
  5450.         $npic    = $this->model_goexpress->get_npic();
  5451.         $lastId  = $this->model_goexpress->get_lastId();
  5452.         $pic_url = $this->model_goexpress->get_pic_url();
  5453.  
  5454.         $i = 0;
  5455.         $allpic = array();
  5456.  
  5457.         foreach ($pic_url as $pic) {
  5458.             $allpic[$i] = base_url() . "upload/promo_banner/" .$pic->img;
  5459.             $i++;
  5460.         }
  5461.  
  5462.         if ( count($allpic) == 0 ) {
  5463.             $SHOW = 0;
  5464.         }
  5465.         else {
  5466.             $SHOW = SHOW_BANNER;
  5467.         }
  5468.  
  5469.         $resjson = array(
  5470.             "error" => 0,
  5471.             "error_message" => "Success",
  5472.             "npic" => $npic,
  5473.             "last_id" => $lastId->id,
  5474.             "show" => $SHOW,
  5475.             "pic_url" => $allpic
  5476.         );
  5477.  
  5478.         if( $last_id == NULL )
  5479.             $resjson["error_message"] = "Success, send latest";
  5480.  
  5481.         die(json_encode($resjson)) ;
  5482.     }
  5483.  
  5484.     function _update_tx_email($params = array())
  5485.     {
  5486.         // MAIL_TYPE
  5487.         // ORDER_CONFIRM    1
  5488.         // CANCEL_ONWAIT    2
  5489.         // CANCEL_BY_CUST   3
  5490.         // COMP_BY_CUST     4
  5491.         // CANCEL_BY_DRIVER 5
  5492.         // COMP_BY_DRIVER   6
  5493.         // CANCEL_BY_OFFICE 7
  5494.         // COMP_BY_OFFICE   8
  5495.  
  5496.         // MAIL_STATUS
  5497.         // PENDING          99
  5498.         //                   5
  5499.         //                   1
  5500.         // SUCCESS           0
  5501.  
  5502.         $ret_array = array(false, '', "Parameters is null");
  5503.         if(empty($params['txno']) || empty($params['mail_type']) || empty($params['tx_date']) || empty($params['mail_status']))
  5504.         {
  5505.             return $ret_array;
  5506.         }
  5507.  
  5508.         $txeparams = array(
  5509.             'txno' => $params['txno'],
  5510.             'email_type' => $params['mail_type'],
  5511.             'tr_date' => $params['tx_date'],
  5512.             'idelete' => $params['mail_status'],
  5513.             'sent_date' => TO_MySQL_DATE()
  5514.             );
  5515.  
  5516.         return $this->model_goexpress->update_tx_email($txeparams);
  5517.     }
  5518.  
  5519.  
  5520.     function tmp_registration()
  5521.     {
  5522.  
  5523.         $params = $this->input->post();
  5524.         if ( count($params) < 5 ) {
  5525.             $resjson = array( 'error' => 1,
  5526.                               'error_message' => "Failed to complete the registration!"
  5527.                             );
  5528.             die(json_encode($resjson));
  5529.         }
  5530.         foreach( $params as $key => $val ) {
  5531.             if ( $val == null || $val == "" ) {
  5532.                 if ( $key != "submit" ) {
  5533.                     $resjson = array( 'error' => 1,
  5534.                                       'error_message' => "All field must be filled!"
  5535.                                     );
  5536.                     die(json_encode($resjson));
  5537.                 }
  5538.             }
  5539.         }
  5540.         if ( !isset($params['email']) ) {
  5541.             $resjson = array( 'error' => 1,
  5542.                               'error_message' => "Failed to complete the registration!"
  5543.                             );
  5544.             die(json_encode($resjson));
  5545.         }
  5546.         $params['name'] = $params['firstname'] . " " . $params['lastname'];
  5547.         $params['register_date'] = TO_MySQL_DATE();
  5548.         $params['birthdate'] = TO_MySQL_DATE($params['birthdate']);
  5549.         unset($params['firstname']);
  5550.         unset($params['lastname']);
  5551.         unset($params['submit']);
  5552.         $res = $this->driver_model->add_tmp_registration($params);
  5553.         if ( $res ) {
  5554.             $resjson = array( 'error' => 0,
  5555.                               'error_message' => ""
  5556.                             );
  5557.             die(json_encode($resjson));
  5558.         }
  5559.         else {
  5560.             $resjson = array( 'error' => 1,
  5561.                               'error_message' => "Failed to complete the registration!"
  5562.                             );
  5563.             die(json_encode($resjson));
  5564.         }
  5565.  
  5566.  
  5567.     }
  5568.  
  5569.     function get_tx_nearby($tx)
  5570.     {
  5571.  
  5572.         $tx_detail = $this->model_goexpress->get_tx_detail(array( 'txno' => $tx));
  5573.  
  5574.         $payload = $tx_detail->payload;
  5575.  
  5576.         if( !$payload )
  5577.         {
  5578.             echo '1@@Error: Empty payload';
  5579.             die();
  5580.         }
  5581.  
  5582.         $txno = $tx;
  5583.  
  5584.         $json = json_decode($payload);
  5585.  
  5586.         $jTarget = $this->_process_jsontarget( $json );
  5587.  
  5588.         $start_detail = sprintf("%s\n*) %s", $json->data->Start->Name, $json->data->Start->Note);
  5589.         $target_detail = sprintf("%s\n*) %s", $jTarget->Name, $jTarget->Note);
  5590.  
  5591.         $USERTYPE_DRIVER = 50;
  5592.         $UPTIME = UPTIME; // in Minutes.
  5593.  
  5594.         $RANGE = 3;
  5595.  
  5596.         $gcparams = array(
  5597.             'range' => $RANGE,
  5598.             'uptime' => $UPTIME,
  5599.             'client_lat' => $json->data->Start->Lat,
  5600.             'client_lng' => $json->data->Start->Lng,
  5601.             );
  5602.  
  5603.         $gcresult = $this->_get_coord_nearby($gcparams);
  5604.  
  5605.         $dnparams = array(
  5606.             'user_type' => $USERTYPE_DRIVER,
  5607.             'transport' => $json->data->Vehicle,
  5608.             'last_up_time' => $gcresult['last_up_time'],
  5609.             'X1' => $gcresult['X1'],
  5610.             'X2' => $gcresult['X2'],
  5611.             'Y1' => $gcresult['Y1'],
  5612.             'Y2' => $gcresult['Y2'],
  5613.             );
  5614.         $driver_nearby = $this->model_goexpress->get_driver_nearby($dnparams);
  5615.  
  5616.         return $driver_nearby;
  5617.     }
  5618.  
  5619.  
  5620.  
  5621.     function get_driver_tracking($active, $tx) {
  5622.         $data['firstbc'] = $this->model_goexpress->get_bc_data($tx,"First Broadcast");
  5623.         $data['secondbc'] = $this->model_goexpress->get_bc_data($tx,"Second Broadcast");
  5624.         $data['thirdbc'] = $this->model_goexpress->get_bc_data($tx,"Third Broadcast");
  5625.         $data['manualbc'] = $this->model_goexpress->get_bc_data($tx,"Manual Broadcast");
  5626.         $data['nearbylist'] = $this->get_tx_nearby($tx);
  5627.         $data['tx'] = $tx;
  5628.         die(json_encode($data));
  5629.  
  5630.     }
  5631.  
  5632.     function load_category_menu() {
  5633.  
  5634.         $category_menu_data = $this->model_goexpress->get_category_menu();
  5635.  
  5636.         die(json_encode($category_menu_data));
  5637.     }
  5638.  
  5639.     function load_food_tags() {
  5640.         $food_tags = $this->model_goexpress->get_food_tags();
  5641.  
  5642.         die(json_encode($food_tags));
  5643.     }
  5644.  
  5645.     function load_resto_nearme() {
  5646.  
  5647.         $postdata = file_get_contents("php://input");
  5648.         $json = json_decode($postdata);
  5649.         //lat
  5650.         //lng
  5651.  
  5652.         if (isset($json->lat) and isset($json->lng) )
  5653.  
  5654.             $USERTYPE_DRIVER = 50;
  5655.             $UPTIME = UPTIME; // in Minutes.
  5656.             $range = 7;
  5657.             $gcparams = array(
  5658.                 'range' => $range,
  5659.                 'uptime' => $UPTIME,
  5660.                 'client_lat' => $json->lat,
  5661.                 'client_lng' => $json->lng,
  5662.                 );
  5663.  
  5664.             $gcresult = $this->_get_coord_nearby($gcparams);
  5665.  
  5666.             $dnparams = array(
  5667.                 'X1' => $gcresult['X1'],
  5668.                 'X2' => $gcresult['X2'],
  5669.                 'Y1' => $gcresult['Y1'],
  5670.                 'Y2' => $gcresult['Y2'],
  5671.                 );
  5672.             $resto_nearby = $this->model_goexpress->get_nearby_resto($dnparams);
  5673.  
  5674.         die(json_encode($resto_nearby));
  5675.  
  5676.     }
  5677.  
  5678.     function load_all_resto() {
  5679.         $all_resto = $this->model_goexpress->get_all_resto();
  5680.  
  5681.         die(json_encode($all_resto));
  5682.     }
  5683.  
  5684.     function load_all_resto_per_page($page){
  5685.         $this->segment   = 3;
  5686.         $this->perPage   = 5;
  5687.         $params['offset']= ($page-1)*$this->perPage;
  5688.         $params['limit'] = $this->perPage;
  5689.         $this->totalData = $this->model_goexpress->get_all_resto2($params, true);
  5690.         $this->totalPage = ceil($this->totalData/$this->perPage);
  5691.         $resto_data      = $this->model_goexpress->get_all_resto2($params, false);
  5692.  
  5693.         $json_data = array(
  5694.                             'show_all'     => true,
  5695.                             'current_page' => $page,
  5696.                             'resto_data'   => $resto_data,
  5697.                             'max_page'     => $this->totalPage
  5698.                           );
  5699.  
  5700.         die(json_encode($json_data));
  5701.     }
  5702.  
  5703.     function load_resto_list() {
  5704.  
  5705.         $postdata = file_get_contents("php://input");
  5706.         if (isset($postdata))
  5707.             $cat_id = json_decode($postdata);
  5708.             $cat = $this->model_goexpress->check_resto_cat($cat_id);
  5709.  
  5710.             // $resto_result = $this->model_goexpress->get_resto_list2($cat);
  5711.  
  5712.         die(json_encode($cat));
  5713.  
  5714.     }
  5715.  
  5716.     function load_resto_list2() {
  5717.  
  5718.         $postdata = file_get_contents("php://input");
  5719.         if (isset($postdata))
  5720.             $tag_id = json_decode($postdata);
  5721.             $tag = $this->model_goexpress->check_food_tag($tag_id);
  5722.  
  5723.             // $resto_result = $this->model_goexpress->get_resto_list2($cat);
  5724.  
  5725.         die(json_encode($tag));
  5726.  
  5727.     }
  5728.  
  5729.     function load_food_list() {
  5730.         $postdata = file_get_contents("php://input");
  5731.         if (isset($postdata))
  5732.             $restoId = json_decode($postdata);
  5733.             $cat_food_list = $this->model_goexpress->get_cat_food($restoId);
  5734.  
  5735.         die(json_encode($cat_food_list));
  5736.     }
  5737.  
  5738.     function load_food_list_new() {
  5739.         $postdata = file_get_contents("php://input");
  5740.         if (isset($postdata))
  5741.             $restoId = json_decode($postdata);
  5742.             $cat_food_list = $this->model_goexpress->get_resto_cat_with_menu($restoId);
  5743.  
  5744.         die(json_encode($cat_food_list));
  5745.     }
  5746.  
  5747.     function load_food_list2() {
  5748.         $postdata = file_get_contents("php://input");
  5749.         if (isset($postdata))
  5750.             $data = json_decode($postdata);
  5751.             $catId = $data->submenu_id;
  5752.             $restoId = $data->resto_id;
  5753.             $food_list = $this->model_goexpress->get_food($catId, $restoId);
  5754.  
  5755.         die(json_encode($food_list));
  5756.     }
  5757.  
  5758.     function search_resto() {
  5759.         $postdata = file_get_contents("php://input");
  5760.         if (isset($postdata))
  5761.             $data = json_decode($postdata);
  5762.             $resto_list = $this->model_goexpress->get_search_resto($data);
  5763.  
  5764.         die(json_encode($resto_list));
  5765.     }
  5766.  
  5767.     function search_embassy() {
  5768.         $postdata = file_get_contents("php://input");
  5769.         if (isset($postdata))
  5770.             $data = json_decode($postdata);
  5771.             $embassy_list = $this->model_goexpress->get_search_embassy($data);
  5772.  
  5773.         die(json_encode($embassy_list));
  5774.     }
  5775.  
  5776.     function iSendSmsViaGateway( )
  5777.     {
  5778.         $ERROR = 0;
  5779.         $CODE_SUCCESS = 0;
  5780.         $SUCCESS = 1;
  5781.  
  5782.         $apiKey = 'username=goexpress_sms@mekongnet&pass=dd5ad747f4c1659a1236bd37169e2fd0&sender=GO-X&smstext=';
  5783.  
  5784.         $sms = "GO-X.\nYour verification number is 123456\n លេខកូដរបស់អ្នកគឺ "."123456";
  5785.         $sms = str_replace(" ","+",$sms);
  5786.         $sms = str_replace("\n","%0A",$sms);
  5787.  
  5788.         $apiEnder = "&isflash=1&gsm=85587898689;85510759542";
  5789.  
  5790.         $url = 'http://client.mekongsms.com/api/sendsms.aspx?' .
  5791.                 $apiKey.
  5792.                 $sms.
  5793.                 $apiEnder;
  5794.  
  5795.         $headers = array(
  5796.             "Content-Type: application/x-www-form-urlencoded; charset=utf-8",
  5797.         );
  5798.  
  5799.         $ch = curl_init();
  5800.  
  5801.         curl_setopt( $ch, CURLOPT_URL, $url );
  5802.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  5803.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  5804.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  5805.  
  5806.         $result = curl_exec( $ch );
  5807.  
  5808.         if ( curl_errno( $ch ) )
  5809.         {
  5810.             echo '1@@FCM error: ' . curl_error( $ch );
  5811.             // set a flag
  5812.         }
  5813.  
  5814.         curl_close( $ch );
  5815.  
  5816.         //------------------------------
  5817.         // Debug GCM response
  5818.         //------------------------------
  5819.         //if( @$result->results[0]->error == "MissmatchSenderId")
  5820.         //    return $this->sendFirebaseCloudMessageIOS($data, $ids, $params, $options);
  5821.         $response = explode("\n", $result);
  5822.  
  5823.         if(count($response) > 0)
  5824.         {
  5825.             $arrResponse = explode(" ", $response[0]);
  5826.  
  5827.             if( $assResponse[0] == $CODE_SUCCESS)
  5828.                 return $SUCCESS;
  5829.             else
  5830.                 return $ERROR;
  5831.         }
  5832.         else
  5833.         {
  5834.             return $ERROR;
  5835.         }
  5836.     }
  5837.  
  5838.     function driver_get_notification($userid, $txno)
  5839.     {
  5840.         query_log("Driver get $txno notification");
  5841.         if ( ACTIVITY_RECORD == true ) {
  5842.             $receiveDetail = array( "driver_username" => $userid,
  5843.                                     "txno" => $txno,
  5844.                                     "receive_date" => TO_MySQL_DATE());
  5845.  
  5846.             $activeDetail = array( "type" => "driver_get_notif",
  5847.                                    "bc" => $receiveDetail);
  5848.  
  5849.             $this->model_goexpress->add_activity(json_encode($activeDetail));
  5850.         }
  5851.     }
  5852.  
  5853.     function release_driver_bc($userid)
  5854.     {
  5855.         if ( isset($userid) and strlen($userid) > 5 ) {
  5856.             $this->model_goexpress->update_release_lastbc($userid);
  5857.             $resjson = array( "error" => 0,
  5858.                              "error_message" => "");
  5859.             if ( ACTIVITY_RECORD == true ) {
  5860.                 $declineDetail = array( "driver_username" => $userid,
  5861.                                         "decline_date" => TO_MySQL_DATE());
  5862.  
  5863.  
  5864.                 $activityDetail = array( "type" => "driver_decline_notif",
  5865.                                        "bc" => $declineDetail);
  5866.                 $this->model_goexpress->add_activity(json_encode($activityDetail));
  5867.             }
  5868.         }
  5869.         else {
  5870.             $resjson = array( "error" => 1,
  5871.                              "error_message" => "No user id");
  5872.         }
  5873.         die(json_encode($resjson));
  5874.     }
  5875.  
  5876.     function load_police_nearme() {
  5877.  
  5878.         $postdata = file_get_contents("php://input");
  5879.         $json = json_decode($postdata);
  5880.  
  5881.         if (isset($json->lat) and isset($json->lng) ){
  5882.             $USERTYPE_DRIVER = 50;
  5883.             $UPTIME = UPTIME; // in Minutes.
  5884.             $range = 7;
  5885.             $gcparams = array(
  5886.                 'range' => $range,
  5887.                 'uptime' => $UPTIME,
  5888.                 'client_lat' => $json->lat,
  5889.                 'client_lng' => $json->lng,
  5890.                 );
  5891.  
  5892.             $gcresult = $this->_get_coord_nearby($gcparams);
  5893.  
  5894.             $dnparams = array(
  5895.                 'X1' => $gcresult['X1'],
  5896.                 'X2' => $gcresult['X2'],
  5897.                 'Y1' => $gcresult['Y1'],
  5898.                 'Y2' => $gcresult['Y2'],
  5899.                 );
  5900.             $police_nearby = $this->model_goexpress->get_nearby_police($dnparams);
  5901.         }
  5902.  
  5903.         die(json_encode($police_nearby));
  5904.  
  5905.     }
  5906.  
  5907.     function load_firefighter_nearme() {
  5908.  
  5909.         $postdata = file_get_contents("php://input");
  5910.         $json = json_decode($postdata);
  5911.  
  5912.         if (isset($json->lat) and isset($json->lng) ){
  5913.             $USERTYPE_DRIVER = 50;
  5914.             $UPTIME = UPTIME; // in Minutes.
  5915.             $range = 7;
  5916.             $gcparams = array(
  5917.                 'range' => $range,
  5918.                 'uptime' => $UPTIME,
  5919.                 'client_lat' => $json->lat,
  5920.                 'client_lng' => $json->lng,
  5921.                 );
  5922.  
  5923.             $gcresult = $this->_get_coord_nearby($gcparams);
  5924.  
  5925.             $dnparams = array(
  5926.                 'X1' => $gcresult['X1'],
  5927.                 'X2' => $gcresult['X2'],
  5928.                 'Y1' => $gcresult['Y1'],
  5929.                 'Y2' => $gcresult['Y2'],
  5930.                 );
  5931.             $firefighter_nearby = $this->model_goexpress->get_nearby_firefighter($dnparams);
  5932.         }
  5933.  
  5934.         die(json_encode($firefighter_nearby));
  5935.  
  5936.     }
  5937.  
  5938.     function load_hospital_nearme() {
  5939.  
  5940.         $postdata = file_get_contents("php://input");
  5941.         $json = json_decode($postdata);
  5942.  
  5943.         if (isset($json->lat) and isset($json->lng) ){
  5944.             $USERTYPE_DRIVER = 50;
  5945.             $UPTIME = UPTIME; // in Minutes.
  5946.             $range = 7;
  5947.             $gcparams = array(
  5948.                 'range' => $range,
  5949.                 'uptime' => $UPTIME,
  5950.                 'client_lat' => $json->lat,
  5951.                 'client_lng' => $json->lng,
  5952.                 );
  5953.  
  5954.             $gcresult = $this->_get_coord_nearby($gcparams);
  5955.  
  5956.             $dnparams = array(
  5957.                 'X1' => $gcresult['X1'],
  5958.                 'X2' => $gcresult['X2'],
  5959.                 'Y1' => $gcresult['Y1'],
  5960.                 'Y2' => $gcresult['Y2'],
  5961.                 );
  5962.             $hospital_nearby = $this->model_goexpress->get_nearby_hospital($dnparams);
  5963.         }
  5964.  
  5965.         die(json_encode($hospital_nearby));
  5966.     }
  5967.  
  5968.     function load_embassy() {
  5969.         $embassy = $this->model_goexpress->get_embassy();
  5970.         die(json_encode($embassy));
  5971.     }
  5972.  
  5973.     function load_police() {
  5974.  
  5975.         $police = $this->model_goexpress->get_police();
  5976.         die(json_encode($police));
  5977.  
  5978.     }
  5979.  
  5980.     function load_client_news() {
  5981.         $postdata = file_get_contents("php://input");
  5982.  
  5983.         if(isset($postdata)){
  5984.             $json = json_decode($postdata);
  5985.             $promo_start = strtotime("2017-4-7 14:00:00");
  5986.             $promo_end = strtotime("2018-4-7 23:59:59");
  5987.             $now = time();
  5988.             if ( $now >= $promo_start && $now <= $promo_end ) {
  5989.                 $userRecord = $this->model_goexpress->get_user(array("username" => $json->userID));
  5990.                 query_log("Promotion still run so check promotion executed ");
  5991.                 query_log("Username is " .$userRecord->username. "Registration date is " . $userRecord->registration_date . " time is " . strtotime($userRecord->registration_date));
  5992.                 if ( strtotime($userRecord->registration_date) >= $promo_start && strtotime($userRecord->registration_date) <= $promo_end ) {
  5993.                     query_log("Execute check registration promotion because in range promotion with username " .$userRecord->username." Name ". $userRecord->name);
  5994.                     $this->model_goexpress->check_tmp_promo($userRecord);
  5995.                 }
  5996.                 else
  5997.                     query_log("Discard check because registration date out of promotion range");
  5998.             }
  5999.             else {
  6000.                 query_log("out of range promotion");
  6001.             }
  6002.             $username = $json->userID;
  6003.             $global = $this->model_goexpress->get_client_news(null);
  6004.             $client = $this->model_goexpress->get_client_news($username);
  6005.             $index = 0;
  6006.  
  6007.             foreach ($global as $record) {
  6008.                 unset($record->is_broadcasted);
  6009.                 unset($record->status);
  6010.                 $news[strtotime($record->create_date)] = $record;
  6011.             }
  6012.  
  6013.             foreach ($client as $record) {
  6014.                 unset($record->is_broadcasted);
  6015.                 unset($record->status);
  6016.                 $news[strtotime($record->create_date)] = $record;
  6017.             }
  6018.  
  6019.             krsort($news);
  6020.  
  6021.             foreach( $news as $each ) {
  6022.                 $newdata[$index] = $each;
  6023.                 $index++;
  6024.             }
  6025.         }
  6026.         die(json_encode($newdata));
  6027.     }
  6028.  
  6029.     function lucky_code_page($userid)
  6030.     {
  6031.         $this->linkPage  = site_url("goexpress/lucky_code_page/$userid/");
  6032.         $this->segment   = 4;
  6033.         $this->perPage   = 10;
  6034.         $params['offset']= $this->start_page($this->segment);
  6035.         $params['limit'] = $this->perPage;
  6036.         $params['userid']= $userid;
  6037.         $this->totalPage = $this->model_goexpress->get_lucky_code($params, true);
  6038.         $data['records'] = $this->model_goexpress->get_lucky_code($params, false);
  6039.         $data['total']   = $this->totalPage;
  6040.         $data['start']   = $params['offset']+1;
  6041.         $data['paging']  = $this->pagination();
  6042.         $this->load->view("webview/lucky_code",$data);
  6043.     }
  6044.  
  6045.     function get_xfood_lang(){
  6046.         $lang = array(
  6047.                         array(
  6048.                             "lang" => 'en',
  6049.                             "name" => "English"
  6050.                             ),
  6051.                         array(
  6052.                             "lang" => 'km',
  6053.                             "name" => "ខ្មែរ"
  6054.                             ),
  6055.                         array(
  6056.                             "lang" => 'cn',
  6057.                             "name" => "中文"
  6058.                             )
  6059.                     );
  6060.  
  6061.         die(json_encode($lang));
  6062.     }
  6063.  
  6064.     function order_receipt($id){
  6065.        
  6066.         $dataList      = array();
  6067.         $rawdata = $this->model_goexpress->get_tx_detail(array('txno'=>$id));
  6068.         $data = json_decode($rawdata->payload)->data;
  6069.  
  6070.         if(isset($data->Actionmode)){
  6071.             if($data->Actionmode == 'FOOD'){
  6072.                 if(isset($data->Start->XfoodNote)){
  6073.                     if(isset($data->Start->XfoodNote->restoname)){
  6074.                         $dataList['resto_name'] = $data->Start->XfoodNote->restoname;
  6075.                     } else {
  6076.                         $dataList['resto_name'] = null;
  6077.                     }
  6078.                     if(isset($data->Start->XfoodNote->restoaddress)){
  6079.                         $dataList['resto_address'] = $data->Start->XfoodNote->restoaddress;
  6080.                     } else {
  6081.                         $dataList['resto_address'] = null;
  6082.                     }
  6083.                     if(isset($data->Start->XfoodNote->list_order)){
  6084.                         $dataList['records'] = $data->Start->XfoodNote->list_order;
  6085.                     } else {
  6086.                         $dataList['records'] = null;
  6087.                     }
  6088.                     if(isset($data->Start->XfoodNote->notes)){
  6089.                         $dataList['notes'] = $data->Start->XfoodNote->notes;
  6090.                     } else {
  6091.                         $dataList['notes'] = null;
  6092.                     }
  6093.                     if(isset($data->Start->XfoodNote->total)){
  6094.                         $dataList['est_total'] = $data->Start->XfoodNote->total;
  6095.                     } else {
  6096.                         $dataList['est_total'] = null;
  6097.                     }
  6098.                     if(isset($data->Start->XfoodNote->deliveryFee)){
  6099.                         $dataList['delivery_fee'] = $data->Start->XfoodNote->deliveryFee;
  6100.                     } else {
  6101.                         $dataList['delivery_fee'] = null;
  6102.                     }
  6103.                     if(isset($data->Start->XfoodNote->xfoodTotalOrder)){
  6104.                         $dataList['totalTotal'] = $data->Start->XfoodNote->xfoodTotalOrder;
  6105.                     } else {
  6106.                         $dataList['totalTotal'] = null;
  6107.                     }
  6108.                     if(isset($data->Start->XfoodNote->restoPhone)){
  6109.                         $dataList['resto_phone'] = $data->Start->XfoodNote->restoPhone;
  6110.                     } else {
  6111.                         $dataList['resto_phone'] = null;
  6112.                     }
  6113.                     if(isset($data->Start->XfoodNote->restoPhone2)){
  6114.                         $dataList['resto_phone_2'] = $data->Start->XfoodNote->restoPhone2;
  6115.                     } else {
  6116.                         $dataList['resto_phone_2'] = null;
  6117.                     }
  6118.                     $this->load->view('webview/xfood_receipt', $dataList);
  6119.                 }
  6120.             }
  6121.  
  6122.             // *** If Shopping *** //
  6123.             // else if($data->Actionmode == 'SHOPPING') {
  6124.             //     if(isset($data->Start->XshoppingNote)){
  6125.             //         $dataList['records'] = $data->Start->XshoppingNote->list_order;
  6126.             //         $this->load->view('webview/xshopping_receipt', $dataList);
  6127.             //     }
  6128.             // }
  6129.             // *** END If Shopping *** //
  6130.  
  6131.             else {
  6132.                 if(isset($data->Start->Note) && strlen($data->Start->Note) >= 3){
  6133.                     $dataList['note'] = $data->Start->Note;
  6134.                 }
  6135.                 else {
  6136.                     $dataList['note'] = "No Note Available!";
  6137.                 }
  6138.  
  6139.                 $this->load->view('webview/global_receipt', $dataList);
  6140.             }
  6141.         }
  6142.     }
  6143.  
  6144.     function gcm_ordermon()
  6145.     {
  6146.         $OPEN = 0;
  6147.        
  6148.         $data = array(
  6149.             "text" => "Hello World",
  6150.         );
  6151.  
  6152.         //------------------------------
  6153.         // The recipient registration IDs
  6154.         // that will receive the push
  6155.         // (Should be stored in your DB)
  6156.         //
  6157.         // Read about it here:
  6158.         // http://developer.android.com/google/gcm/
  6159.         //------------------------------
  6160.         $ids = array( 'abc', 'def' );
  6161.  
  6162.         //------------------------------
  6163.         // Call our custom GCM function
  6164.         //------------------------------
  6165.         $bresult = $this->sendFirebaseCloudMessageOM(  $data, $ids, null, array());
  6166.  
  6167.         var_dump($bresult);
  6168.     }
  6169.  
  6170.     function sendFirebaseCloudMessageOM( $data, $ids, $params = array(), $options = array() )
  6171.     {
  6172.         //------------------------------
  6173.         // Replace with real GCM API
  6174.         // key from Google APIs Console
  6175.         //
  6176.         // https://code.google.com/apis/console/
  6177.         //------------------------------
  6178.         // if ( $this->is_firebase == false ) {
  6179.         //     return $this->sendGoogleCloudMessage($data,$ids,$params,$options);
  6180.         // }
  6181.  
  6182.  
  6183.         $apiKey = 'AIzaSyBVtIzVFE7zyPDOREWYffkwOAOxw8RjYo0';
  6184.         //$apiKey = 'AIzaSyDSaYxDq8Fu8HfjYgb0w2rRqKxZbh0RiRM';
  6185.  
  6186.         //------------------------------
  6187.         // Define URL to GCM endpoint
  6188.         //------------------------------
  6189.  
  6190.         $url = 'https://fcm.googleapis.com/fcm/send';
  6191.  
  6192.         //------------------------------
  6193.         // Set GCM post variables
  6194.         // (Device IDs and push payload)
  6195.         //------------------------------
  6196.  
  6197.         $post = array(
  6198.                     'data' => $data,
  6199.                 );
  6200.  
  6201.         if( isset( $params['registration_ids'] ) )
  6202.         {
  6203.             $post['registration_ids'] = $params['registration_ids'];
  6204.         }
  6205.         else if( isset( $options['stoken'] ) )
  6206.         {
  6207.             $post['to'] = $options["stoken"];
  6208.         }
  6209.         else
  6210.         {
  6211.             // @TODO REMOVE THIS VALIDATION SINCE redundant
  6212.             // and already filtered at registration_ids filter
  6213.             if( isset($data['Vehicle'] ))
  6214.                 $post['to'] = "/topics/" . $data['Vehicle'];
  6215.             else
  6216.                 $post['to'] = "/topics/ordermon";
  6217.         }
  6218.  
  6219.         query_log("OM Post Data " . json_encode($post));
  6220.  
  6221.         //------------------------------
  6222.         // Set CURL request headers
  6223.         // (Authentication and type)
  6224.         //------------------------------
  6225.  
  6226.         $headers = array(
  6227.             'Authorization: key=' . $apiKey,
  6228.             'Content-Type: application/json'
  6229.         );
  6230.  
  6231.         //------------------------------
  6232.         // Initialize curl handle
  6233.         //------------------------------
  6234.  
  6235.         $ch = curl_init();
  6236.  
  6237.         //------------------------------
  6238.         // Set URL to GCM endpoint
  6239.         //------------------------------
  6240.  
  6241.         curl_setopt( $ch, CURLOPT_URL, $url );
  6242.  
  6243.         //------------------------------
  6244.         // Set request method to POST
  6245.         //------------------------------
  6246.  
  6247.         curl_setopt( $ch, CURLOPT_POST, true );
  6248.  
  6249.         //------------------------------
  6250.         // Set our custom headers
  6251.         //------------------------------
  6252.  
  6253.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  6254.  
  6255.         //------------------------------
  6256.         // Get the response back as
  6257.         // string instead of printing it
  6258.         //------------------------------
  6259.  
  6260.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  6261.  
  6262.         //------------------------------
  6263.         // Set post data as JSON
  6264.         //------------------------------
  6265.  
  6266.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
  6267.         curl_setopt($ch, CURLOPT_TIMEOUT ,10);
  6268.  
  6269.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  6270.  
  6271.         //------------------------------
  6272.         // Actually send the push!
  6273.         //------------------------------
  6274.  
  6275.         $result = curl_exec( $ch );
  6276.  
  6277.         //------------------------------
  6278.         // Error? Display it!
  6279.         //------------------------------
  6280.  
  6281.         if ( curl_errno( $ch ) )
  6282.         {
  6283.             if( isset($options['ret_int']) and $options['ret_int'] === True )
  6284.                 error_log('1@@FCM error: ' . curl_error( $ch ));
  6285.             else
  6286.                 echo '1@@FCM error: ' . curl_error( $ch );
  6287.             // set a flag
  6288.         }
  6289.  
  6290.         //------------------------------
  6291.         // Close curl handle
  6292.         //------------------------------
  6293.  
  6294.         curl_close( $ch );
  6295.  
  6296.             var_dump($result, $post);
  6297.         if( isset($options['ret_int']) )
  6298.         {
  6299.             //error_log("BC is ". $result);
  6300.             var_dump($result);
  6301.             return $result;
  6302.         }
  6303.  
  6304.         if( isset($params['separate']))
  6305.             echo '@@0@@'.$data['text'].'@@'.$result;
  6306.         else
  6307.             echo '0@@'.$data['text'].'@@'.$result;
  6308.     }
  6309.  
  6310.     // BAP
  6311.     function update_token()
  6312.     {
  6313.         //Define Api key
  6314.         if ( $this->uri->segment(1) == "apikey" )
  6315.             $apikey = $this->uri->segment(2);
  6316.         else
  6317.             $apikey = $this->uri->segment(3);
  6318.  
  6319.         $min_length = 20;
  6320.         if ( strlen($apikey) <= $min_length ) {
  6321.             $error_m = array("error" => 0, "error_message" => "Failed Api Key.");
  6322.             echo json_encode($error_m);
  6323.             die();
  6324.         }
  6325.  
  6326.         $data = file_get_contents('php://input');
  6327.  
  6328.         if( !$data )
  6329.         {
  6330.             $error_m = array("error" => 0, "error_message" => "Empty data.");
  6331.             echo json_encode($error_m);
  6332.             die();
  6333.         }
  6334.  
  6335.         $json = json_decode($data,true);
  6336.  
  6337.         if( !isset($json['fcm_token']) )
  6338.         {
  6339.             $error_m = array("error" => 0, "error_message" => "Empty Token.");
  6340.             echo json_encode($error_m);
  6341.             die();
  6342.         }
  6343.  
  6344.         $json["apikey"] = $apikey;
  6345.         if($this->model_goexpress->get_update_token($json))
  6346.         {
  6347.             $error_m = array("error" => 0, "error_message" => "Success Token Upadate.");
  6348.             echo json_encode($error_m);
  6349.             die();
  6350.         }
  6351.  
  6352.         $error_m = array("error" => 0, "error_message" => "Failed Upadate Token.");
  6353.         echo json_encode($error_m);
  6354.     }
  6355.     /**BAP*/
  6356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement