<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class C_api_trx extends CI_Controller {
    
  function __construct(){
    parent::__construct();    
    $this->load->model('m_masters');
        $this->load->helper(array('url','html','form'));
        $this->load->library('session');
    ini_set('max_execution_time', 0);    
    ini_set('memory_limit', '20000M');
    date_default_timezone_set("Asia/Jakarta");

  }
 
 
  public function index()
  {
    //   show_404($page = 'gasdgasdg', $log_error = TRUE);

        echo "hahiho";
  }
  
  public function tidythisdate($dirtydate)
  {
      $tahun = substr($dirtydate, 7,4);
      $tanggal = substr($dirtydate, 0,2);
      $jam = substr($dirtydate, 12, 10);
      
      $bulan = substr($dirtydate, 3,3);
      
      if ($bulan == 'Jan')
      {
          $bulan = '01';
      }
      else if ($bulan == 'Feb')
      {
          $bulan = '02';
      }
      else if ($bulan == 'Mar')
      {
          $bulan = '03';
      }
      else if ($bulan == 'Apr')
      {
          $bulan = '04';
      }
      else if ($bulan == 'May')
      {
          $bulan = '05';
      }
      else if ($bulan == 'Jun')
      {
          $bulan = '06';
      }
      else if ($bulan == 'Jul')
      {
          $bulan = '07';
      }
      else if ($bulan == 'Aug')
      {
          $bulan = '08';
      }
      else if ($bulan == 'Sep')
      {
          $bulan = '09';
      }
      else if ($bulan == 'Oct')
      {
          $bulan = '10';
      }
      else if ($bulan == 'Nov')
      {
          $bulan = '11';
      }
      else if ($bulan == 'Dec')
      {
          $bulan = '12';
      }
      
      $cleandate = $tahun . "-".$bulan."-".$tanggal." ".$jam;
      return $cleandate;
  }
  
  public function checkpassword()
  {
      
    //   echo "asdf";
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
        // echo "asdf";
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['result'] = "salah";
          
          
      }
      else
      {
          
        $response['result'] = "benar";
        
      }
      $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
    }
  }
  
  
  
  public function getwebdetails()
  {
      
    //   echo "asdf";
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
        // echo "asdf";
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $querystring = "select * from websmaster where web like '%".$fromjsondata['web']."%'";
          
          
        //   echo "sadf";
          $weblist = $this->m_masters->querying($querystring)->result();
          if(count($weblist)==0)
          {
            $response['queryresult'] = "No Such Web Exist!";
          }
          else
          {
              $response['queryresult'] = $weblist;
              
          }
            
            
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
     
  }
  
  public function updateticketdeposit_permata()
  {
   
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $brand = $fromjsondata['brand'];
          $ticketnumber = $fromjsondata['ticketnumber'];
          $status = $fromjsondata['status'];
          
          if ( $status == '1' )
          {
              $querystring = "update deposits_permata_panelform set approveddate = '1' where ticketnumber = '".$ticketnumber."' and brand = '".$brand."'";
          }
          else if ($status == '')
          {
              $querystring = "update deposits_permata_panelform set approveddate = DATE_ADD(NOW(), INTERVAL 1 HOUR) where ticketnumber = '".$ticketnumber."' and brand = '".$brand."'";
          }
          else
          {
              $querystring = "update deposits_permata_panelform set approveddate = '".$status."' where ticketnumber = '".$ticketnumber."' and brand = '".$brand."'";
              
          }
        //   echo $querystring;
          $this->m_masters->querying($querystring);
          
          
          
          $querystring = str_replace("'","''",$querystring);
          $querylog = "insert into logs values('".$querystring."',now())";
          $this->m_masters->querying($querylog);
          
          $response['result'] = "Ticket Successfully Updated!";
        
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
         
         
         
    
    
      }
      
    }
  }
  
  public function cekticket()
  {
   
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $brand = $fromjsondata['brand'];
          $ticketnumber = $fromjsondata['ticketnumber'];
          
              $querystring = "select ticketnumber , brand, case when authorizeddate = '' then 'OS' when authorizeddate = '2' then 'Fail' when authorizeddate = '1' then 'Success' Else 'Success' END as keterangan  from withdrawals where ticketnumber = '".$ticketnumber."' and brand = '".$brand."'";
          
        //   echo $querystring;
          $resultnya = $resultnya = $this->m_masters->querying($querystring)->result();
          
          $querystring = str_replace("'","''",$querystring);
          $querylog = "insert into logs values('".$querystring."',now())";
          $this->m_masters->querying($querylog);
          
          $response['result'] = "Ticket Successfully Updated!";
          $response['ticketnumber'] = $resultnya[0]->ticketnumber;
          $response['brand'] = $resultnya[0]->brand;
          $response['keterangan'] = $resultnya[0]->keterangan;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
    
      }
      
    }
  }
  
  
  
  public function updateticket()
  {
   
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $taken = $fromjsondata['taken'];
          $ticketnumber = $fromjsondata['ticketnumber'];
          if ( $taken == '1' )
          {
              $querystring = "update withdrawals set authorizeddate = '1', takendate = DATE_ADD(NOW(), INTERVAL 1 HOUR) where ticketnumber = '".$ticketnumber."'";
          }
          else if ($taken == '')
          {
              $querystring = "update withdrawals set authorizeddate = DATE_ADD(NOW(), INTERVAL 1 HOUR), takendate = DATE_ADD(NOW(), INTERVAL 1 HOUR) where ticketnumber = '".$ticketnumber."'";
          }
          else
          {
              $querystring = "update withdrawals set authorizeddate = '".$taken."', takendate = DATE_ADD(NOW(), INTERVAL 1 HOUR) where ticketnumber = '".$ticketnumber."'";
              
          }
        //   echo $querystring;
          $this->m_masters->querying($querystring);
          
          $querystring = str_replace("'","''",$querystring);
          $querylog = "insert into logs values('".$querystring."',now())";
          $this->m_masters->querying($querylog);
          
          $response['result'] = "Ticket Successfully Updated!";
        
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
    
      }
      
    }
  }
  
  
  public function getdeposits_permata()
  {
   
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          
          $jumlah = $fromjsondata['jumlah'];
          $brand = $fromjsondata['brand'];
          $brand2 = $fromjsondata['brand2'];
          $brand3 = $fromjsondata['brand3'];
          $brand4 = $fromjsondata['brand4'];
          $brand5 = $fromjsondata['brand5'];
          $brand6 = $fromjsondata['brand6'];
          $brand7 = $fromjsondata['brand7'];
          $brand8 = $fromjsondata['brand8'];
          $brand9 = $fromjsondata['brand9'];
          $brand10 = $fromjsondata['brand10'];
         
          
        


          $querystring = "call panelvstrx ('".$jumlah. "',
          '".$brand."',
          '".$brand2."',
          '".$brand3."',
          '".$brand4."',
          '".$brand5."',
          '".$brand6."',
          '".$brand7."',
          '".$brand8."',
          '".$brand9."',
          '".$brand10."');";
          $outstandingdepos = $this->m_masters->querying($querystring)->result();
          $this->db->close();
          
        //   echo $querystring;
        //   print_r($outstandingdepos);
            $querystring = str_replace("'","''",$querystring);
              $querylog = "insert into logs values('".$querystring."',now())";
              $this->m_masters->querying($querylog);
            
            
          if(count($outstandingdepos) == 0)
          {
            $response['result'][0]->ticketnumber = '';
              
          }
          else
          {
            $response['result'] = $outstandingdepos;
          }

        
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
    
      }
      
    }
  }
  
  
  
  public function getwithdrawals()
  {
   
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $sourceaccount = $fromjsondata['sourceaccount'];
          $bank = $fromjsondata['bank'];
          $wdcount = $fromjsondata['wdcount'];
          $maxamount = $fromjsondata['maxamount'];
          $brand = $fromjsondata['brand'];
          
          if($sourceaccount == '' || $wdcount == '' || $wdcount == '0' || $maxamount == '' || $maxamount == 0)
          {
              $response['result'][0]->ticketnumber = "sourceaccount,  wdaccount, maxamount cannot be empty";
          }
          
          else
          {
            if($bank <> '')
            {
                $querystring = "select a.*, b.copartno, c.userid, c.pinlayar, c.pintoken, c.nomorrekening, c.namarekening, c.balance,c.useable from withdrawals a left join coparts b on a.bank = b.copart left join sourceaccountsindividu c on a.takeraccount = c.nomorrekening where takeraccount = '".$sourceaccount."' and (authorizeddate = '') and ticketnumber not like '(%' and a.bank = '".$bank."' and amount <= ".$maxamount . " order by requestdate limit ".$wdcount;
            }
            else
            {
                $querystring = "select a.*, b.copartno, c.userid, c.pinlayar, c.pintoken, c.nomorrekening, c.namarekening, c.balance,c.useable from withdrawals a left join coparts b on a.bank = b.copart left join sourceaccountsindividu c on a.takeraccount = c.nomorrekening where takeraccount = '".$sourceaccount."' and (authorizeddate = '') and ticketnumber not like '(%' and a.bank like '%%' and amount <= ".$maxamount . "  order by requestdate limit ".$wdcount;
                
            }
            // echo $querystring;
            $outstandingwithdrawals = $this->m_masters->querying($querystring)->result();
            
            $querystring = str_replace("'","''",$querystring);
              $querylog = "insert into logs values('".$querystring."',now())";
              $this->m_masters->querying($querylog);
            
            
          if(count($outstandingwithdrawals) == 0)
          {
              $response['result'][0]->ticketnumber = '';
              
          }
          else
          {
            $response['result'] = $outstandingwithdrawals;
          }

        }
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
    
      }
      
    }
  }
  

  public function insertdepositpermatapanelform()
  {
    
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    
    
    if ($this->form_validation->run() == FALSE)
    {
    
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }

    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
    //   echo $querystring;
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          
          $package = $fromjsondata['data'];
          $jumlahrow = count($package);

            $nx_brand  = $package[0]->nx_brand ;
            $nx_requested_date = $package[0]->nx_requested_date;
            $nx_requested_time = $package[0]->nx_requested_time;
            $nx_ticket_no = $package[0]->nx_ticket_no;
            $nx_username = $package[0]->nx_username;
            $nx_player_group = $package[0]->nx_player_group;
            $nx_depo_amt = $package[0]->nx_depo_amt;
            $nx_depo_info_type = $package[0]->nx_depo_info_type;
            $nx_depo_info_method = $package[0]->nx_depo_info_method;
            $nx_depo_info_ref = $package[0]->nx_depo_info_ref;
            $nx_payment_from_name = $package[0]->nx_payment_from_name;
            $nx_payment_from_bank = $package[0]->nx_payment_from_bank;
            $nx_payment_from_account = $package[0]->nx_payment_from_account;
            $nx_payment_to_name = $package[0]->nx_payment_to_name;
            $nx_payment_to_bank = $package[0]->nx_payment_to_bank;
            $nx_payment_to_account = $package[0]->nx_payment_to_account;
              
              
              
              $nx_depo_amt = str_replace(",","",$nx_depo_amt);
              $nx_depo_amt = str_replace("-","",$nx_depo_amt);
              $nx_requested_date = $this->tidythisdate($nx_requested_date . " ".$nx_requested_time);
              
              $querystring = "insert into deposits_permata_panelform values ("
              ."'".$nx_brand ."',"
              ."'".$nx_requested_date ."',"
              ."'".$nx_ticket_no ."',"
              ."'".$nx_username ."',"
              ."'".$nx_player_group ."',"
              ."'".$nx_depo_amt ."',"
              ."'".$nx_depo_info_type ."',"
              ."'".$nx_depo_info_method ."',"
              ."'".$nx_depo_info_ref ."',"
              ."'".$nx_payment_from_name ."',"
              ."'".$nx_payment_from_bank ."',"
              ."'".$nx_payment_from_account ."',"
              ."'".$nx_payment_to_name ."',"
              ."'".$nx_payment_to_bank ."',"
              ."'".$nx_payment_to_account ."',"
              ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
              ."'',"
              ."'',"
              ."'')";
            
            // echo $querystring;              
              $this->m_masters->querying($querystring);
            
              $querystring = str_replace("'","''",$querystring);
              $querylog = "insert into logs values('".$querystring."',now())";
              $this->m_masters->querying($querylog);
              

          }
          
          
          $response['result'] = "Data Successfully Submitted!" ;
          
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
    }
  }
  
  
  
  public function insertdepositpermatatrxform()
  {
    
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    
    
    if ($this->form_validation->run() == FALSE)
    {
    
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }

    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
    //   echo $querystring;
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          
          $package = $fromjsondata['data'];
          $jumlahrow = count($package);
            
            $permata_mid = $package[0]->permata_mid;
            $permata_nama_merchant = $package[0]->permata_nama_merchant;
            $permata_jenis_qr = $package[0]->permata_jenis_qr;
            $permata_tanggal_transaksi = $package[0]->permata_tanggal_transaksi;
            $permata_tipe_transaksi = $package[0]->permata_tipe_transaksi;
            $permata_status = $package[0]->permata_status;
            $permata_customer_pan = $package[0]->permata_customer_pan;
            $permata_nama_customer = $package[0]->permata_nama_customer;
            $permata_nama_psjp = $package[0]->permata_nama_psjp;
            $permata_jenis_pembayaran = $package[0]->permata_jenis_pembayaran;
            $permata_id_transaksi = $package[0]->permata_id_transaksi;
            $permata_bill_number = $package[0]->permata_bill_number;
            $permata_ref_pembayaran = $package[0]->permata_ref_pembayaran;
            $permata_ref_pengembalian = $package[0]->permata_ref_pengembalian;
            $permata_jumlah_transaksi = $package[0]->permata_jumlah_transaksi;
            $permata_jumlah_pengembalian = $package[0]->permata_jumlah_pengembalian;
            $permata_jumlah_mdr = $package[0]->permata_jumlah_mdr;
            $permata_biaya_layanan = $package[0]->permata_biaya_layanan;
            $permata_jumlah_bersih = $package[0]->permata_jumlah_bersih;
              
              
              
              $permata_jumlah_transaksi = str_replace(",00","",$permata_jumlah_transaksi);
              $permata_jumlah_transaksi = str_replace(",","",$permata_jumlah_transaksi);
              $permata_jumlah_transaksi = str_replace("Rp. ","",$permata_jumlah_transaksi);
              $permata_jumlah_transaksi = str_replace(".","",$permata_jumlah_transaksi);
              
              
              $permata_jumlah_pengembalian = str_replace(",00","",$permata_jumlah_pengembalian);
              $permata_jumlah_pengembalian = str_replace(",","",$permata_jumlah_pengembalian);
              $permata_jumlah_pengembalian = str_replace("Rp. ","",$permata_jumlah_pengembalian);
              $permata_jumlah_pengembalian = str_replace(".","",$permata_jumlah_pengembalian);
              
              $permata_jumlah_mdr = str_replace(",00","",$permata_jumlah_mdr);
              $permata_jumlah_mdr = str_replace(",","",$permata_jumlah_mdr);
              $permata_jumlah_mdr = str_replace("Rp. ","",$permata_jumlah_mdr);
              $permata_jumlah_mdr = str_replace(".","",$permata_jumlah_mdr);
              
              $permata_biaya_layanan = str_replace(",00","",$permata_biaya_layanan);
              $permata_biaya_layanan = str_replace(",","",$permata_biaya_layanan);
              $permata_biaya_layanan = str_replace("Rp. ","",$permata_biaya_layanan);
              $permata_biaya_layanan = str_replace(".","",$permata_biaya_layanan);
              
              
              $permata_jumlah_bersih = str_replace(",00","",$permata_jumlah_bersih);
              $permata_jumlah_bersih = str_replace(",","",$permata_jumlah_bersih);
              $permata_jumlah_bersih = str_replace("Rp. ","",$permata_jumlah_bersih);
              $permata_jumlah_bersih = str_replace(".","",$permata_jumlah_bersih);

              
              $querystring = "insert into deposits_permata_trxform values ("
              ."'".$permata_mid ."',"
              ."'".$permata_nama_merchant ."',"
              ."'".$permata_jenis_qr ."',"
              ."'".$permata_tanggal_transaksi ."',"
              ."'".$permata_tipe_transaksi ."',"
              ."'".$permata_status ."',"
              ."'".$permata_customer_pan  ."',"
              ."'".$permata_nama_customer  ."',"
              ."'".$permata_nama_psjp  ."',"
              ."'".$permata_jenis_pembayaran  ."',"
              ."'".$permata_id_transaksi  ."',"
              ."'".$permata_bill_number  ."',"
              ."'".$permata_ref_pembayaran  ."',"
              ."'".$permata_ref_pengembalian  ."',"
              ."'".$permata_jumlah_transaksi  ."',"
              ."'".$permata_jumlah_pengembalian   ."',"
              ."'".$permata_jumlah_mdr   ."',"
              ."'".$permata_biaya_layanan   ."',"
              ."'".$permata_jumlah_bersih   ."',"
              ."'',"
              ."'',"
              ."DATE_ADD(NOW(), INTERVAL 1 HOUR))";
            
            //   echo $querystring;              
              $this->m_masters->querying($querystring);
            
              $querystring = str_replace("'","''",$querystring);
              $querylog = "insert into logs values('".$querystring."',now())";
              $this->m_masters->querying($querylog);
              

          }
          
          
          $response['result'] = "Data Successfully Submitted!" ;
          
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
    }
  }
  
  
  
//   public function insertwithdrawals()
//   {
    
//     $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    
//     $this->form_validation->set_data($fromjsondata);
//     $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    
    
//     if ($this->form_validation->run() == FALSE)
//     {
    
//       $response['Info'] = validation_errors();
//       $this->output
//       ->set_status_header(400)
//       ->set_content_type('application/json', 'utf-8')
//       ->set_output(json_encode($response, JSON_PRETTY_PRINT))
//       ->_display();
//       exit;
//     }

//     else
//     {
//       $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
//     //   echo $querystring;
//       $authentication = $this->m_masters->querying($querystring)->result();
//       if (count($authentication) < 1)
//       {
//           $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
//           $this->output
//           ->set_status_header(400)
//           ->set_content_type('application/json', 'utf-8')
//           ->set_output(json_encode($response, JSON_PRETTY_PRINT))
//           ->_display();
//           exit;
          
//       }
//       else
//       {
          
//           $package = $fromjsondata['data'];
//           $jumlahrow = count($package);
          

//         //   print_r($package);
//           if($package[0]->we_brand <> '')
//           {
//               $querystring = "select brandgroup from brands where brand = '".$package[0]->we_brand ."'";
//               $brandgroup = $this->m_masters->querying($querystring)->result()[0]->brandgroup;
//           }
          
//           else if ($package[0]->nx_brand <> '')
//           {
//               $querystring = "select brandgroup from brands where brand = '".$package[0]->nx_brand ."'";
//               $brandgroup = $this->m_masters->querying($querystring)->result()[0]->brandgroup;
//           }
          
          
          
//         //   echo $brandgroup;
          
          
//           if($brandgroup == 'NS88'||$brandgroup == 'CR88' || $brandgroup == 'LIONG88')
//           {
//               $querystring = "call transactionaccountpicker('".$package[0]->we_brand."','".$package[0]->we_bank_name."');";
        
//               $resultnya = $this->m_masters->querying($querystring)->result();
          
//               $this->db->close();
              
//             //   print_r($resultnya);
          
//               $amountcorrection = str_replace(",","",$package[0]->we_amount);
//               $amountcorrection = str_replace("-","",$amountcorrection);
              
//               $requestdatecorrection = $this->tidythisdate($package[0]->we_request_date);
              
//               $querystring = "insert into withdrawals values ("
//               ."'".$package[0]->we_txn_code ."',"
//               ."'".$package[0]->we_brand ."',"
//               .$amountcorrection .","
//               ."'".$requestdatecorrection ."',"
//               ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
//               ."'".$package[0]->we_username ."',"
//               ."'".$package[0]->we_account_name ."',"
//               ."'".$package[0]->we_bank_name."',"
//               ."'".$package[0]->we_bank_account ."',"
//               ."'".$resultnya[0]->nomorrekening."',"
//               ."DATE_ADD(NOW(), INTERVAL 1 HOUR),'','')";
//               $this->m_masters->querying($querystring);
              
//               $querystring = str_replace("'","''",$querystring);
//               $querylog = "insert into logs values('".$querystring."',now())";
//               $this->m_masters->querying($querylog);
              
//             //   echo $querystring;
              
//             //   echo $resultnya[0]->bank;
//             //   echo $package[0]->we_bank_name;
              
//               if ($resultnya[0]->bank <> $package[0]->we_bank_name)
//               {
                  
//                   $querybrandgroup = "select brandgroup from brands where brand = '".$package[0]->we_brand ."'";
//                   $brandgroup = $this->m_masters->querying($querybrandgroup)->result()[0]->brandgroup;
//                 //   echo $querybrandgroup;
//                 //   echo $brandgroup;
                
//                   $querystring = "select fee from adminfee where sourcebank = '".$resultnya[0]->bank."' and destbank = '".$package[0]->we_bank_name."' and brandgroup = '".$brandgroup."'";
                  
//                   $fee = $this->m_masters->querying($querystring)->result()[0]->fee;
//                 //   echo $fee;
//                   if($fee == '')
//                   {
//                       $querystring = "select fee from adminfee where sourcebank = '".$resultnya[0]->bank."' and destbank = 'OTHERBANK' and brandgroup = '".$brandgroup."'";
//                       $fee = $this->m_masters->querying($querystring)->result()[0]->fee;
                  
//                     //   echo $fee;
//                   }
                  
//                   $querystring = "insert into withdrawals values ("
//                       ."'(".$package[0]->we_txn_code .")',"
//                       ."'".$package[0]->we_brand ."',"
//                       .$fee .","
//                       ."'".$requestdatecorrection ."',"
//                       ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
//                       ."'BIAYA',"
//                       ."'BIAYA ADM',"
//                       ."'".$package[0]->we_bank_name."',"
//                       ."'".$package[0]->we_bank_account ."',"
//                       ."'".$resultnya[0]->nomorrekening."',"
//                       ."DATE_ADD(NOW(), INTERVAL 1 HOUR),'','2')";
                      

//                 $this->m_masters->querying($querystring);
                
//                 // echo $querystring;
                
//                 $querystring = str_replace("'","''",$querystring);
//               $querylog = "insert into logs values('".$querystring."',now())";
//               $this->m_masters->querying($querylog);
//               }
//             // echo $querystring;
//           }
          
//           else if($brandgroup == 'P70' || $brandgroup == 'Africa_A' || $brandgroup == 'Africa_B' || $brandgroup = 'Ausie' || $brandgroup == 'NONA')
//           {
//             //   echo $brandgroup;
             
//               $querystring = "call transactionaccountpicker('".$package[0]->nx_brand."','".$package[0]->nx_bank_title."');";
        
//               $resultnya = $this->m_masters->querying($querystring)->result();
          
//               $this->db->close();
              
              
              
              
//               $amountcorrection = str_replace(",","",$package[0]->nx_wd_amount);
//               $amountcorrection = str_replace("-","",$amountcorrection);
//               $requestdatecorrection = $this->tidythisdate($package[0]->nx_requested_date . " ".$package[0]->nx_requested_time);
              
//               $querystring = "insert into withdrawals values ("
//               ."'".$package[0]->nx_ticket_no ."',"
//               ."'".$package[0]->nx_brand ."',"
//               .$amountcorrection .","
//               ."'".$requestdatecorrection ."',"
//               ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
//               ."'".$package[0]->nx_username ."',"
//               ."'".$package[0]->nx_bank_acc_name ."',"
//               ."'".$package[0]->nx_bank_title ."',"
//               ."'".$package[0]->nx_bank_account ."',"
//               ."'".$resultnya[0]->nomorrekening."',"
//               ."DATE_ADD(NOW(), INTERVAL 1 HOUR),'','')";
//               $this->m_masters->querying($querystring);
//             //   echo $querystring;
            
//             $querystring = str_replace("'","''",$querystring);
//               $querylog = "insert into logs values('".$querystring."',now())";
//               $this->m_masters->querying($querylog);
              
//               if ($resultnya[0]->bank <> $package[0]->nx_bank_title)
//               {
                  
//                   $querybrandgroup = "select brandgroup from brands where brand = '".$package[0]->nx_brand ."'";
//                   $brandgroup = $this->m_masters->querying($querybrandgroup)->result()[0]->brandgroup;
                  
                  
//                   $querystring = "select fee from adminfee where sourcebank = '".$resultnya[0]->bank."' and destbank = '".$package[0]->nx_bank_title."' and brandgroup = '".$brandgroup."'";
//                   $fee = $this->m_masters->querying($querystring)->result()[0]->fee;
    
//                   if($fee == '')
//                   {
//                       $querystring = "select fee from adminfee where sourcebank = '".$resultnya[0]->bank."' and destbank = 'OTHERBANK' and brandgroup = '".$brandgroup."'";
//                       $fee = $this->m_masters->querying($querystring)->result()[0]->fee;
//                   }
                  
//                   $querystring = "insert into withdrawals values ("
//                   ."'(".$package[0]->nx_ticket_no .")',"
//                   ."'".$package[0]->nx_brand ."',"
//                   .$fee.","
//                   ."'".$requestdatecorrection ."',"
//                   ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
//                   ."'BIAYA',"
//                   ."'BIAYA ADM',"
//                   ."'".$package[0]->nx_bank_title."',"
//                   ."'".$package[0]->nx_bank_account ."',"
//                   ."'".$resultnya[0]->nomorrekening."',"
//                   ."DATE_ADD(NOW(), INTERVAL 1 HOUR),'','2')";
                      

//                 $this->m_masters->querying($querystring);
                
//                 $querystring = str_replace("'","''",$querystring);
//               $querylog = "insert into logs values('".$querystring."',now())";
//               $this->m_masters->querying($querylog);
//               }

//           }
          
          

          
//           $response['result'] = "Data Successfully Submitted!" ;
//           $response['takeraccount'] = $resultnya[0]->nomorrekening;
//           $response['takerbank'] = $resultnya[0]->bank;
          
          
//           $this->output
//           ->set_status_header(400)
//           ->set_content_type('application/json', 'utf-8')
//           ->set_output(json_encode($response, JSON_PRETTY_PRINT))
//           ->_display();
//           exit;
//       }
    
      
//     }
//   }
  
  
  
  
  public function insertwithdrawals()
  {
    
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    
    
    if ($this->form_validation->run() == FALSE)
    {
    
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(200)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }

    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
    //   echo $querystring;
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          
          $package = $fromjsondata['data'];
          $jumlahrow = count($package);
          

        //   print_r($package);
          if($package[0]->we_brand <> '')
          {
              $querystring = "select brandgroup from brands where brand = '".$package[0]->we_brand ."'";
              $brandgroup = $this->m_masters->querying($querystring)->result()[0]->brandgroup;
          }
          
          else if ($package[0]->nx_brand <> '')
          {
              $querystring = "select brandgroup from brands where brand = '".$package[0]->nx_brand ."'";
              $brandgroup = $this->m_masters->querying($querystring)->result()[0]->brandgroup;
          }
          
          
          

          
          
          if($brandgroup == 'NS88'||$brandgroup == 'CR88' || $brandgroup == 'LIONG88' || $brandgroup == 'Plaza88' || $brandgroup == 'Konoha88' || $brandgroup == 'Star88' || $brandgroup == 'Grab88')
          {
            
        
            if($brandgroup == 'NS88'  || $brandgroup == 'CR88' )
            {
                $querystring = "select walletname from ewallet where walletname = '".$package[0]->we_bank_name."'";
                $walletexist = count($this->m_masters->querying($querystring)->result());
    
             
                if ($walletexist >= 1)
                {
                  $querystring = "call transactionaccountpicker_ewallet('".$package[0]->we_brand."');";
                  $resultnya = $this->m_masters->querying($querystring)->result();
                  $this->db->close();
                }
    
                else
                {
                  $querystring = "call transactionaccountpicker('".$package[0]->we_brand."','".$package[0]->we_bank_name."');";
                  $resultnya = $this->m_masters->querying($querystring)->result();          
                  $this->db->close();
                }
            }
            else
            {
                
                $querystring = "call transactionaccountpicker('".$package[0]->we_brand."','".$package[0]->we_bank_name."');";
                  $resultnya = $this->m_masters->querying($querystring)->result();          
                  $this->db->close();
            }
            
            // echo $querystring;
            // print_r($resultnya);
              
            //   print_r($resultnya);
          
              $amountcorrection = str_replace(",","",$package[0]->we_amount);
              $amountcorrection = str_replace("-","",$amountcorrection);
              
              $requestdatecorrection = $this->tidythisdate($package[0]->we_request_date);
              
              $querystring = "insert into withdrawals values ("
              ."'".$package[0]->we_txn_code ."',"
              ."'".$package[0]->we_brand ."',"
              .$amountcorrection .","
              ."'".$requestdatecorrection ."',"
              ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
              ."'".$package[0]->we_username ."',"
              ."'".$package[0]->we_account_name ."',"
              ."'".$package[0]->we_bank_name."',"
              ."'".$package[0]->we_bank_account ."',"
              ."'".$resultnya[0]->nomorrekening."',"
              ."DATE_ADD(NOW(), INTERVAL 1 HOUR),'','')";
              $this->m_masters->querying($querystring);
              
              $querystring = str_replace("'","''",$querystring);
              $querylog = "insert into logs values('".$querystring."',now())";
              $this->m_masters->querying($querylog);
              
            //   echo $querystring;
              
            //   echo $resultnya[0]->bank;
            //   echo $package[0]->we_bank_name;
              
              if ($resultnya[0]->bank <> $package[0]->we_bank_name)
              {
                  
                  $querybrandgroup = "select brandgroup from brands where brand = '".$package[0]->we_brand ."'";
                  $brandgroup = $this->m_masters->querying($querybrandgroup)->result()[0]->brandgroup;
                //   echo $querybrandgroup;
                //   echo $brandgroup;
                
                  $querystring = "select fee from adminfee where sourcebank = '".$resultnya[0]->bank."' and destbank = '".$package[0]->we_bank_name."' and brandgroup = '".$brandgroup."'";
                  
                  $fee = $this->m_masters->querying($querystring)->result()[0]->fee;
                //   echo $fee;
                  if($fee == '')
                  {
                      $querystring = "select fee from adminfee where sourcebank = '".$resultnya[0]->bank."' and destbank = 'OTHERBANK' and brandgroup = '".$brandgroup."'";
                      $fee = $this->m_masters->querying($querystring)->result()[0]->fee;
                  
                    //   echo $fee;
                  }
                  
                  $querystring = "insert into withdrawals values ("
                      ."'(".$package[0]->we_txn_code .")',"
                      ."'".$package[0]->we_brand ."',"
                      .$fee .","
                      ."'".$requestdatecorrection ."',"
                      ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
                      ."'BIAYA',"
                      ."'BIAYA ADM',"
                      ."'".$package[0]->we_bank_name."',"
                      ."'".$package[0]->we_bank_account ."',"
                      ."'".$resultnya[0]->nomorrekening."',"
                      ."DATE_ADD(NOW(), INTERVAL 1 HOUR),'','2')";
                      

                $this->m_masters->querying($querystring);
                
                // echo $querystring;
                
                $querystring = str_replace("'","''",$querystring);
              $querylog = "insert into logs values('".$querystring."',now())";
              $this->m_masters->querying($querylog);
              }
              
              $response['result'] = "Data Successfully Submitted!" ;
              
            $response['brand'] = $package[0]->we_brand;
            $response['ticketnumber'] = $package[0]->we_txn_code;
            $response['takeraccount'] = $resultnya[0]->nomorrekening;
            $response['takerbank'] = $resultnya[0]->bank;
            // echo $querystring;
          }
          
          else if($brandgroup == 'P70' || $brandgroup == 'Africa_A' || $brandgroup == 'Africa_B' || $brandgroup == 'Ausie' || $brandgroup == 'NONA')
          {
            //   echo "asdf";
            //   echo $brandgroup;
                    
          
                //   $querystring = "select walletname from ewallet where walletname = '".$package[0]->nx_bank_title."'";
                //   $walletexist = count($this->m_masters->querying($querystring)->result());
                  

                //   if ($walletexist >= 1)
                //   {
    //               	$querystring = "select a.nomorrekening, a.bank from sourceaccountsindividu a inner join sourceaccounts_brandgroup b
				// 	on a.nomorrekening = b.sourceaccount where a.ewallet_only_flag = '1' and b.brandgroup = '".$brandgroup."'";
					
					
				// 	$querystring = "call transactionaccountpicker_ewallet('".$package[0]->nx_brand."');";
				// 	$resultnya = $this->m_masters->querying($querystring)->result();
				//     $this->db->close();
					
				// 	echo $brandgroup;
                    // echo $querystring;
                //   }

                //   else
                //   {
                  	$querystring = "call transactionaccountpicker('".$package[0]->nx_brand."','".$package[0]->nx_bank_title."');";
        
              		$resultnya = $this->m_masters->querying($querystring)->result();
              		
              		$this->db->close();

                //   }
              
             
                // echo $brandgroup;
              
              
              
              $amountcorrection = str_replace(",","",$package[0]->nx_wd_amount);
              $amountcorrection = str_replace("-","",$amountcorrection);
              $requestdatecorrection = $this->tidythisdate($package[0]->nx_requested_date . " ".$package[0]->nx_requested_time);
              
              $querystring = "insert into withdrawals values ("
              ."'".$package[0]->nx_ticket_no ."',"
              ."'".$package[0]->nx_brand ."',"
              .$amountcorrection .","
              ."'".$requestdatecorrection ."',"
              ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
              ."'".$package[0]->nx_username ."',"
              ."'".$package[0]->nx_bank_acc_name ."',"
              ."'".$package[0]->nx_bank_title ."',"
              ."'".$package[0]->nx_bank_account ."',"
              ."'".$resultnya[0]->nomorrekening."',"
              ."DATE_ADD(NOW(), INTERVAL 1 HOUR),'','')";
              $this->m_masters->querying($querystring);
            //   echo $querystring;
            
            $querystring = str_replace("'","''",$querystring);
              $querylog = "insert into logs values('".$querystring."',now())";
              $this->m_masters->querying($querylog);
              
              
              if ( ($resultnya[0]->bank <> $package[0]->nx_bank_title) && ($package[0]->nx_bank_title <> 'DANA') && ($package[0]->nx_bank_title <> 'OVO' ) )
              {
                  
                  $querybrandgroup = "select brandgroup from brands where brand = '".$package[0]->nx_brand ."'";
                  $brandgroup = $this->m_masters->querying($querybrandgroup)->result()[0]->brandgroup;
                  
                  
                  $querystring = "select fee from adminfee where sourcebank = '".$resultnya[0]->bank."' and destbank = '".$package[0]->nx_bank_title."' and brandgroup = '".$brandgroup."'";
                  $this->db->close();
                  $fee = $this->m_masters->querying($querystring)->result()[0]->fee;
                  
                //   echo 'asdf';
                //   echo $fee;
                  if($fee == '')
                  {
                      $querystring = "select fee from adminfee where sourcebank = '".$resultnya[0]->bank."' and destbank = 'OTHERBANK' and brandgroup = '".$brandgroup."'";
                      $fee = $this->m_masters->querying($querystring)->result()[0]->fee;
                    // echo $querystring;
                    //   echo $fee;
                  }
                  
                  $querystring = "insert into withdrawals values ("
                  ."'(".$package[0]->nx_ticket_no .")',"
                  ."'".$package[0]->nx_brand ."',"
                  .$fee.","
                  ."'".$requestdatecorrection ."',"
                  ."DATE_ADD(NOW(), INTERVAL 1 HOUR),"
                  ."'BIAYA',"
                  ."'BIAYA ADM',"
                  ."'".$package[0]->nx_bank_title."',"
                  ."'".$package[0]->nx_bank_account ."',"
                  ."'".$resultnya[0]->nomorrekening."',"
                  ."DATE_ADD(NOW(), INTERVAL 1 HOUR),'','BIAYA')";
                      
                
                $this->m_masters->querying($querystring);
                
                $querystring = str_replace("'","''",$querystring);
              $querylog = "insert into logs values('".$querystring."',now())";
              $this->m_masters->querying($querylog);
              
              
              }
              $response['result'] = "Data Successfully Submitted!" ;
              
            $response['brand'] = $package[0]->nx_brand;
            $response['ticketnumber'] = $package[0]->nx_ticket_no;
            
            // $response['brand'] = 'luffy';
            // $response['ticketnumber'] = 'doffy';
            
            $response['takeraccount'] = $resultnya[0]->nomorrekening;
            $response['takerbank'] = $resultnya[0]->bank;
              
          }
          
          
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    
      
    }
  }
  
  public function updateregistrationsstatus()
  {
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
         $querystring = "update registrations set status = '".$fromjsondata['status']."' where ticketnumber = '" .$fromjsondata['ticketnumber']."'";
        //  echo $querystring;
         $querythis = $this->m_masters->querying($querystring);
            $queryresult = $this->db->affected_rows();
            if($queryresult <= 0)
            {
                $response['queryresult'] = "No Such Ticket Number in Registration Exist!";
            }
            else
            {
                $response['queryresult'] = "Success updating ticket number!";
            }
            
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
      
  }
  
  public function updatedataregis()
  {
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $querystring = "select * from sourcetodestaccounts where sourceaccount = '".$fromjsondata['nomorrekening'] . "' and destaccount = '".$fromjsondata['norektujuan']. "'";

          $adakah = count($this->m_masters->querying($querystring)->result());
        
          if($adakah == 0)
          {
            $querystring = "insert into sourcetodestaccounts values ('','".$fromjsondata['nomorrekening']."','".$fromjsondata['norektujuan']."','"
                .$fromjsondata['namarekening']."','','System',now());";
            
            $result = $this->m_masters->querying($querystring);
            
            $queryresult = $this->db->affected_rows();
            if($queryresult <= 0)
            {
                $response['queryresult'] = "Registration Fail!";
            }
            else
            {
                $response['queryresult'] = "Registration Successful!";
            }
          }
          else
          {
              $response['queryresult'] = "These accounts has already been mapped before!";
              
          }
            
            
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
     
  }
  
  public function dosignout()
  {
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $querystring = "insert into loginrequests values ('','logout','".$fromjsondata['nomorrekening']. "',now(),'2','','','','','','System',now());";
          $result = $this->m_masters->querying($querystring);
          $queryresult = $this->db->affected_rows();
            if($queryresult <= 0)
            {
                $response['queryresult'] = "Logout Request Fail!";
            }
            else
            {
                $response['queryresult'] = "Logout Request Success!";
            }
            
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
     
  }
  
  
  
  
  
  public function updatebalance()
  {
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $querystring = "update sourceaccounts set balance = ".$fromjsondata['newbalance']. " where nomorrekening = '".$fromjsondata['accountno']. "'";
        //   echo $querystring;
          $querythis = $this->m_masters->querying($querystring);
            $queryresult = $this->db->affected_rows();
            if($queryresult <= 0)
            {
                $response['queryresult'] = "No Change Made!";
            }
            else
            {
                $response['queryresult'] = "Success!";
            }
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
  }
  
  
  
  public function setloginrequeststatus()
  {
    //   echo 'asdf';
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $querystring = "update loginrequests set `status` = '".$fromjsondata['newstatus']. "', time = now() where requestid = '".$fromjsondata['requestid']. "'";
        //   echo $querystring;
          $querythis = $this->m_masters->querying($querystring);
          $queryresult = $this->db->affected_rows();
        //   echo $queryresult;
            if($queryresult == 0)
            {
                $response['queryresult'] = "No Such Request Exist!";
            }
            else
            {
                // echo $fromjsondata['newstatus'];
                if($fromjsondata['newstatus'] == '3')
                {
                    // echo 'halo';
                    $querystring = "select rekening from loginrequests where requestid = '".$fromjsondata['requestid']. "'";
                    $nomorrekening = $this->m_masters->querying($querystring)->result()[0]->rekening;
                    // echo $querystring;
                    
                    // echo $nomorrekening;
                    $querystring = "select status from loginrequests where rekening = '".$nomorrekening. "' and requestid < ".$fromjsondata['requestid']."  order by requestid desc limit 1";
                    // echo $querystring;
                    $previousstatus = $this->m_masters->querying($querystring)->result()[0]->status;
                    
                    // echo $previousstatus;
                    if($previousstatus == '3')
                    {
                        $response['additional'] = 'blokirdulusementara';
                        $querystring = "insert into loginrequests values ('','login','".$nomorrekening."',now(),'4','','','','','','System',now())";
                        $this->m_masters->querying($querystring);
                        // echo $querystring;
                        
                        
                        $querystring = "update sourceaccounts set useable = '0' where nomorrekening = '".$nomorrekening."'";
                        // echo $querystring;
                        $this->m_masters->querying($querystring);
                    }
                    
                }
                
                else if ($fromjsondata['newstatus'] == '2')
                {
                    
                    
                    $querystring = "select * from sourceaccounts where nomorrekening = '".$fromjsondata['nomorrekening']."'";
                    $detailrekening = $this->m_masters->querying($querystring)->result();
                    
                    
                    $querystring ="insert into requests values ('','withdrawal',now(),'0','".$detailrekening[0]->nomorrekening."','".$detailrekening[0]->corpid."','".$detailrekening[0]->userid."','".$detailrekening[0]->stickernumber."','','','System',now());";
                    
             
                  
                    $this->m_masters->querying($querystring);
                }
                $response['queryresult'] = "Success!";
            }
    
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
  }
  
  public function getlatestlogin()
  {
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $querystring = "select * from loginrequests where type = 'login' and `status` = 0 order by createddate limit 1";
          $response['result'] = $this->m_masters->querying($querystring)->result();
            
            if(count($response['result']) <= 0)
            {
                $response['result'][0]->requestid = "No Request!";
            }
            
            //$$$
          $querystring = "update loginrequests set `status` = '1' where requestid = '".$response['result'][0]->requestid. "'";
          $this->m_masters->querying($querystring);        
            
        //   echo $querystring;    
                
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
  }
  
  
  

  public function getlatestrequest()
  {
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
    //   echo $querystring;
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {
          $querystring = "select * from requests where nomorrekening = '".$fromjsondata['nomorrekening']."' order by requestid desc limit 1";
        //   echo $querystring;
        
          $response['request'] = $this->m_masters->querying($querystring)->result();
          
          if($response['request'][0]->status == '5')
          {
              $querystring = "select * from registrations where requestrequestid = '".$response['request'][0]->requestid. "' order by requestid ";
              
              $response['registrations'] = $this->m_masters->querying($querystring)->result();
              
              $jumlahaktual = count($response['registrations']);
              
              $querystring = "select * from accountperrequest";
              $accountperrequest = $this->m_masters->querying($querystring)->result()[0]->accountperrequest;
              
              
              for($i = $jumlahaktual ; $i< $accountperrequest ;$i++)
              {
                  
                  $response['registrations'][$i] = '';
              }
          }
          
          else
          {
              $response['registrations'] = '';
          }
          
          
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
  }
  
  
  public function setrequeststatus()
  {
    $fromjsondata = (array)json_decode(file_get_contents('php://input'));
    $this->form_validation->set_data($fromjsondata);
    $this->form_validation->set_rules('password', 'password', 'required|xss_clean');
    
    if ($this->form_validation->run() == FALSE)
    {
        // print_r($fromjsondata);
      $response['Info'] = validation_errors();
      $this->output
      ->set_status_header(400)
      ->set_content_type('application/json', 'utf-8')
      ->set_output(json_encode($response, JSON_PRETTY_PRINT))
      ->_display();
      exit;
    }
    else
    {
      $querystring = "select * from apipassword where `password` = '".$fromjsondata['password']. "'";
      $authentication = $this->m_masters->querying($querystring)->result();
      if (count($authentication) < 1)
      {
          $response['errmsg'] = "We would like you to leave! You doesnt pass the authentication phase!";
          $response['input'] = $fromjsondata;
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
          
      }
      else
      {

          if($fromjsondata['newstatus'] == '1'||$fromjsondata['newstatus']=='11'|| $fromjsondata['newstatus'] == '6' || $fromjsondata['newstatus'] == '71')
          {
              $querystring = "update requests set status = '".$fromjsondata['newstatus']."', appli2request = '".$fromjsondata['appli2request']."', appli2submit = '' , time=now() where requestid = ".$fromjsondata['requestid'];
            //   echo $querystring;
              $this->m_masters->querying($querystring);
              $response['result'] = "success!";
          }
          
          if($fromjsondata['newstatus'] == '3' || $fromjsondata['newstatus'] == '73')
          {
              $querystring = "update requests set status = '".$fromjsondata['newstatus']."', appli2request = '".$fromjsondata['appli2request']."', appli2submit = '' , time=now() where requestid = ".$fromjsondata['requestid'];
            //   echo $querystring;
              $this->m_masters->querying($querystring);
              
              $querystring = "select nomorrekening from requests where requestid = '".$fromjsondata['requestid']."'";
              $nomorrekeningnya = $this->m_masters->querying($querystring)->result();
              
              $querystring = "insert into loginrequests values ('','logout','".$nomorrekeningnya[0]->nomorrekening. "',now(),'2','','','','','','System',now());";
              $this->m_masters->querying($querystring);
              
              $querystring = "update sourceaccounts set useable = 0 where nomorrekening = '".$nomorrekeningnya[0]->nomorrekening."'";
              $this->m_masters->querying($querystring);
              
              $response['result'] = "success!";
          }
          
          if($fromjsondata['newstatus'] == '8' || $fromjsondata['newstatus'] == '74')
          {
              $querystring = "update requests set status = '".$fromjsondata['newstatus']."', appli2request = '".$fromjsondata['appli2request']."', appli2submit = '' , time=now() where requestid = ".$fromjsondata['requestid'];
            //   echo $querystring;
              $this->m_masters->querying($querystring);
              
              $querystring = "select nomorrekening from requests where requestid = '".$fromjsondata['requestid']."'";
              $nomorrekeningnya = $this->m_masters->querying($querystring)->result();
              
              $querystring = "insert into loginrequests values ('','logout','".$nomorrekeningnya[0]->nomorrekening. "',now(),'2','','','','','','System',now());";
              $this->m_masters->querying($querystring);
              
              $querystring = "update sourceaccounts set useable = 0 where nomorrekening = '".$nomorrekeningnya[0]->nomorrekening."'";
              $this->m_masters->querying($querystring);
              
              $querystring = "update withdrawals set authorizeddate = now() where ticketnumber in (
                    select ticketnumber from registrations where requestrequestid = '".$fromjsondata['requestid']."'
                  )";
              $this->m_masters->querying($querystring);
              
              $response['result'] = "success!";
          }
          

          
          if($fromjsondata['newstatus'] == '4')
          {
              $querystring = "update requests set status = '".$fromjsondata['newstatus']."', time=now() where requestid = ".$fromjsondata['requestid'];
            //   echo $querystring;
              $this->m_masters->querying($querystring);
              $response['result'] = "success!";
          }
          
          if($fromjsondata['newstatus'] == '6')
          {
              $querystring = "update requests set status = '".$fromjsondata['newstatus']."', appli2request = '".$fromjsondata['appli2request']."', appli2submit = '' , time=now() where requestid = ".$fromjsondata['requestid'];
            //   echo $querystring;
              $this->m_masters->querying($querystring);
              $response['result'] = "success!";
          }
          
          
          
          
          $this->output
          ->set_status_header(400)
          ->set_content_type('application/json', 'utf-8')
          ->set_output(json_encode($response, JSON_PRETTY_PRINT))
          ->_display();
          exit;
      }
    }
  }
  
  
  
  
}