Advertisement
Guest User

goexpress

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