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

class c_modules 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', 3600);    
    ini_set('memory_limit', '20000M');
    date_default_timezone_set("Asia/Jakarta");

    $this->data = array(
      'intervalinactnotpickedup' => '3',
      'intervalinterested' => '30'
    );

     // if(empty($this->session->userdata("username")))
   //       {
   //         redirect(base_url(),'refresh');
   //       }
  }
  
  
  public function index()
  {
    if (empty($this->session->userdata('username')))
    { 
      redirect('c_login');
    }
    else
    {
      $group = $this->session->userdata('privilege');
      $username  = $this->session->userdata('username');
      $data['title'] = "Main Modules";
       
      $this->load->view('v_modules',$data); 
    }
  }
  
  public function inquireadminfee()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $sourcebank = $this->input->post("sourcebank");
      $destbank = $this->input->post("destbank");
      $fee = $this->input->post("fee");
      
      $querystring = "select * from adminfee where sourcebank like '%".$sourcebank."%' and destbank like '%".$destbank."%' and brandgroup = '".$this->session->userdata('brandgroup')."'";
      $data['adminfeelist'] = $this->m_masters->querying($querystring)->result();
    
      $ada = count($data['adminfeelist']);
      
      if($ada == 0)
      {
          echo '<div class="alert alert-danger alert-dismissible">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                          <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                          Tidak ada admin fee yang sesuai dengan kriteria yang diisi
                        </div>';
      }
      else
      {
          $this->load->view("v_eaf_dg",$data);
      }
    }
  }
  
  public function updateadminfee()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $sourcebank = $this->input->post("sourcebank");
      $destbank = $this->input->post("destbank");
      $fee = $this->input->post("fee");
      
      $querystring = "select * from adminfee where sourcebank = '".$sourcebank."' and destbank = '".$destbank."' and brandgroup = '".$this->session->userdata('brandgroup')."'";
      $data['adminfeelist'] = $this->m_masters->querying($querystring)->result();
    
      $ada = count($data['adminfeelist']);
      
      if($ada == 0)
      {
          echo '<div class="alert alert-danger alert-dismissible">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                          <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                          Tidak ada admin fee yang sesuai dengan kriteria yang diisi
                        </div>';
      }
      else
      {
          if($fee == "" || is_numeric($fee) == 0)
          {
              echo '<div class="alert alert-danger alert-dismissible">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                          <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                          Fee tidak boleh kosong dan harus merupakan nominal!
                        </div>';
          }
          
          else
          {
              $querystring = "update adminfee set fee = '".$fee."' where sourcebank = '".$sourcebank."' and destbank = '".$destbank."' and brandgroup = '".$this->session->userdata('brandgroup')."'";
            // echo $querystring;
              $this->m_masters->querying($querystring);
              echo '<div class="alert alert-success alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Success!</h5>
                              Admin Fee berhasil diupdate!
                            </div>';
              
          }
      }
    }
  }
  
  public function inquirerekening()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $rekening = $this->input->post("rekening");
      $namarekening = $this->input->post("namarekening");
      $userid = $this->input->post("userid");
      $pinlayar = $this->input->post("pinlayar");
      $pintoken = $this->input->post("pintoken");
      $balance = $this->input->post("balance");
      $useable = $this->input->post("useable");
      $bank = $this->input->post("bank");
      $useable = $this->input->post("useable");
      $ewalletonly = $this->input->post("ewalletonly");
      
      if ($this->session->userdata('privilege') == 'Admin')
      {
          $querystring = "select * from sourceaccountsindividu a inner join sourceaccounts_brandgroup b on a.nomorrekening = b.sourceaccount where b.brandgroup = '".$this->session->userdata('brandgroup'). "' and ";
      }
      else
      {
          $querystring = "select * from sourceaccountsindividu where ";
      }
      
      if($rekening == '')
      {
          $querystring = $querystring ." nomorrekening like '%%' and";
      }
      else
      {
          $querystring = $querystring ." nomorrekening = '".$rekening."' and";
      }
      
     
      
      $querystring = $querystring ." namarekening like '%".$namarekening."%' and";
      $querystring = $querystring ." userid like '%".$userid."%' and";
      $querystring = $querystring ." pinlayar like '%".$pinlayar."%' and";
      $querystring = $querystring ." pintoken like '%".$pintoken."%' and";
      $querystring = $querystring ." balance like '%".$balance."%' and";
      
      if($useable == '')
      {
          $querystring = $querystring ." useable like '%%' and";
      }
      else
      {
          $querystring = $querystring ." useable = '".$useable."' and";
      }
      
      if($ewalletonly == '')
      {
          $querystring = $querystring ." ewallet_only_flag like '%%' and";
      }
      else
      {
          $querystring = $querystring ." ewallet_only_flag = '".$ewalletonly."' and";
      }

      if($bank == '')
      {
          $querystring = $querystring ." bank like '%%'";
      }
      else
      {
          $querystring = $querystring ." bank = '".$bank."'";
      }

      
      
    //   echo $querystring;
      
      
      $data['rekeninglist'] = $this->m_masters->querying($querystring)->result();
      $ada = count($data['rekeninglist']);
      
      if($ada == 0)
      {
          echo '<div class="alert alert-danger alert-dismissible">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                          <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                          Tidak ada rekening yang sesuai dengan kriteria yang diisi
                        </div>';
      }
      else
      {
          $this->load->view("v_rekeninglist_dg",$data);
      }
    }
  }
  
  
  public function updatestatusrekening()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
        // echo "a";
      $rekening = $this->input->post("rekening");
      $useable = $this->input->post("useable");
      $brandgroup = $this->input->post("brandgroup");
      $ewalletonly = $this->input->post("ewalletonly");
      
      
      if($this->session->userdata('privilege') == 'SuperAdmin')
      {
        $querystring = "select * from sourceaccounts_brandgroup where sourceaccount = '".$rekening."'";
      }
      else if($this->session->userdata('privilege') == 'Admin')
      {
        $querystring = "select * from sourceaccounts_brandgroup where sourceaccount = '".$rekening."' and brandgroup = '".$this->session->userdata('brandgroup')."'";
      }
      
      $pernahada = count($this->m_masters->querying($querystring)->result());
      
    //   echo $pernahada;
      if($pernahada == 1)
      {
         
         

         
         if($this->session->userdata('privilege') == 'SuperAdmin')
         {
            $querystring = "update sourceaccountsindividu set useable = '".$useable. "' where nomorrekening = '".$rekening."'";
            $this->m_masters->querying($querystring);
         
            $querystring = "update sourceaccounts_brandgroup set brandgroup = '".$brandgroup."' where sourceaccount = '".$rekening."'";
            $this->m_masters->querying($querystring);
         }
         
         else if($this->session->userdata('privilege') == 'Admin')
         {
            if($ewalletonly == 1)
            {
                // $querystring = "update sourceaccountsindividu a inner join sourceaccounts_brandgroup b set a.ewallet_only_flag = '0' where b.brandgroup = '".$this->session->userdata('brandgroup')."'";
                // $this->m_masters->querying($querystring);
                // echo $querystring;
                
                $querystring = "update sourceaccountsindividu set useable = '0', ewallet_only_flag = '1' where nomorrekening = '".$rekening."'";
                $this->m_masters->querying($querystring);

                
            }
            else if ($ewalletonly == 0)
            {
                $querystring = "update sourceaccountsindividu set useable = '".$useable. "', ewallet_only_flag = '0' where nomorrekening = '".$rekening."'";
                $this->m_masters->querying($querystring);
                // echo $querystring;
            }
         }
         
         $querystring = "call brandgroupsewallettakerreorder()";
                $this->m_masters->querying($querystring);
                $this->db->close();
                
                
         $querystring = "call brandgroupstakerreorder()";
         $this->m_masters->querying($querystring);
         $this->db->close();
    
         
          echo '<div class="alert alert-success alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Success!</h5>
                              Status Rekening berhasil diupdate!
                            </div>';
      }
      else
      {
         echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Nomor Rekening tidak ditemukan!
                            </div>';
      }
    }
  }
  
  public function deleteticketnumberdeposits()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $ticketnumber = $this->input->post("ticketnumber");
      $brand = $this->input->post("brand");
      $keterangan = $this->input->post("keterangan");

      $querystring = "select * from deposits_permata_panelform where ticketnumber = '".$ticketnumber."' and length(approveddate)  < 10 and brand = '".$brand."'";
      $pernahada = count($this->m_masters->querying($querystring)->result());
    //   echo $querystring;
      
      if($pernahada == 1)
      {
         $querystring = "update deposits_permata_panelform set approveddate = '100' where ticketnumber = '".$ticketnumber."' and length(approveddate)  < 10 and brand = '".$brand."'";
         $this->m_masters->querying($querystring);
        //  echo $querystring;
         
         
         $querystring = "insert into manualin_deposits_logs values ('".$ticketnumber."','".$brand."','".$keterangan."',now())";
         $this->m_masters->querying($querystring);
         
          echo '<div class="alert alert-success alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Success!</h5>
                              Ticket Number berhasil disingkirkan!
                            </div>';
      }
      else
      {
         echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Ticket Number tidak ditemukan!
                            </div>';
      }
    }
  }
  
  
  public function deleteticketnumber()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $ticketnumber = $this->input->post("ticketnumber");
      $brand = $this->input->post("brand");
      $keterangan = $this->input->post("keterangan");
      
      
    //   if ($this->session->userdata('privilege') == 'SuperAdmin')
    //   {
            $querystring = "select * from withdrawals where ticketnumber = '".$ticketnumber."' and length(authorizeddate)  < 10 and brand = '".$brand."'";
          
    //   }
    //   else
    //   {
    //       $querystring = "select * from withdrawals a inner join sourceaccounts_brandgroup b on a.takeraccount = b.sourceaccount  where a.ticketnumber = '".$ticketnumber."' and length(a.authorizeddate) < 10 and b.brandgroup = '". $this->session->userdata('brandgroup') . "'";
    //   }
      
    //   echo $querystring;
      $pernahada = count($this->m_masters->querying($querystring)->result());
      
      if($pernahada == 1)
      {
        // $querystring = "delete from withdrawals where ticketnumber = '".$ticketnumber."' and length(authorizeddate)  < 10";

         $querystring = "update withdrawals set authorizeddate = '100' where ticketnumber = '".$ticketnumber."' and length(authorizeddate)  < 10 and brand = '".$brand."'";
         $this->m_masters->querying($querystring);
        
         $querystring = "insert into manualin_logs values ('".$ticketnumber."','".$brand."','".$keterangan."',now())";
         $this->m_masters->querying($querystring);
        // echo $querystring;
         
          echo '<div class="alert alert-success alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Success!</h5>
                              Ticket Number berhasil disingkirkan!
                            </div>';
      }
      else
      {
         echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Ticket Number tidak ditemukan!
                            </div>';
      }
    }
  }
  
  public function deleterekening()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $rekening = $this->input->post("rekening");
      
      if($this->session->userdata('privilege') == 'SuperAdmin')
      {
        $querystring = "select * from sourceaccounts_brandgroup where sourceaccount = '".$rekening."'";
      }
      else if($this->session->userdata('privilege') == 'Admin')
      {
        $querystring = "select * from sourceaccounts_brandgroup where sourceaccount = '".$rekening."' and brandgroup = '".$this->session->userdata('brandgroup')."'";
      }
      
      $pernahada = count($this->m_masters->querying($querystring)->result());
      
      if($pernahada == 1)
      {
         $querystring = "select ewallet_only_flag from sourceaccountsindividu where nomorrekening = '".$rekening."'";
         $ewalletonly = $this->m_masters->querying($querystring)->result()[0]->ewallet_only_flag;
         
         $querystring = "delete from sourceaccountsindividu where nomorrekening = '".$rekening."'";
         $this->m_masters->querying($querystring);
         
         $querystring = "delete from sourceaccounts_brandgroup where sourceaccount = '".$rekening."'";
         $this->m_masters->querying($querystring);
         

            $querystring = "call brandgroupsewallettakerreorder()";
            $this->m_masters->querying($querystring);
            $this->db->close();
         
         
         $querystring = "call brandgroupstakerreorder()";
         $this->m_masters->querying($querystring);
         $this->db->close();
         
          echo '<div class="alert alert-success alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Success!</h5>
                              Rekening berhasil didelete!
                            </div>';
      }
      else
      {
         echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Nomor Rekening tidak ditemukan!
                            </div>';
      }
    }
  }
  
  
  
  public function submitrekening()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $rekening = $this->input->post("rekening");
      $namarekening = $this->input->post("namarekening");
      $userid = $this->input->post("userid");
      $pinlayar = $this->input->post("pinlayar");
      $pintoken = $this->input->post("pintoken");
      $balance = $this->input->post("balance");
      $useable = $this->input->post("useable");
      $bank = $this->input->post("bank");
      $brandgroup = $this->input->post("brandgroup");
      $ewalletonly = $this->input->post("ewalletonly");

      $querystring = "select * from sourceaccounts_brandgroup where sourceaccount = '".$rekening."'";
    //   echo $querystring;
      
      $pernahada = count($this->m_masters->querying($querystring)->result());
      
      if($pernahada == 1)
      {
          echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Nomor Rekening ini sudah pernah ada sebelumnya!
                            </div>';
      }
      else if ( $rekening == '' || $namarekening == '' || $userid == '' || $pinlayar == '' || $pintoken == '' || $balance == '' || $useable == ''|| $bank == ''|| $ewalletonly == '')
      {
          

          echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Data harus semua diinput!
                            </div>';
          
      }
      
      else
      { $add_message = '';
         if ($ewalletonly == 1)
         {
             $useable = 0;
             $add_message = "Useable dijadiin 0 karena ewallet diset 1";
         }
         
         if($this->session->userdata("privilege") == 'Admin')
         {
             $brandgroup = $this->session->userdata('brandgroup');
         }

        //  $querystring = "update sourceaccountsindividu a inner join sourceaccounts_brandgroup b set a.ewallet_only_flag = '0' where b.brandgroup = '".$brandgroup."'";
        //  $this->m_masters->querying($querystring);
        //  echo $querystring;
         
         $querystring = "insert into sourceaccountsindividu values ("
         ."''"
         .",'".$userid . "'"
         .",'".$pinlayar."'"
         .",'".$pintoken."'"
         .",'".$bank."'"
         .",'".$rekening."'"
         .",'".$namarekening."'"
         .",'".$balance."'"
         .",'".$useable."'"
         .",now()"
         .",'".$this->session->userdata('username')."','".$ewalletonly . "')";
         $this->m_masters->querying($querystring);
         
        //  echo $querystring;
         $querystring = "insert into sourceaccounts_brandgroup values ("
         ."'".$rekening . "'";
    
         
         if($this->session->userdata("privilege") == 'Admin')
         {
             $querystring = $querystring . ",'".$this->session->userdata('brandgroup') . "','".$this->session->userdata('username'). "',now())";
         }
         else if($this->session->userdata("privilege") == 'SuperAdmin')
         {
             $querystring = $querystring . ",'".$brandgroup . "','".$this->session->userdata('username'). "',now())";
         }
         $this->m_masters->querying($querystring);
        //  echo $querystring;
        
         $querystring = "call brandgroupstakerreorder()";
         $this->m_masters->querying($querystring);
         $this->db->close();
        //  echo $querystring;
         
         if ($ewalletonly == 1)
         {
            $querystring = "call brandgroupsewallettakerreorder()";
            $this->m_masters->querying($querystring);
            $this->db->close();
            // echo $querystring;
         }
         
         echo '<div class="alert alert-success alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Success!</h5>
                              Rekening berhasil diinput! ' . $add_message . '
                            </div>';
                            
                            
      }
      
    }
  }
  
  
  public function preview()
  {
      $datefrom = $this->input->post("datefrom");
      $dateto = $this->input->post("dateto");
      $reporttype = $this->input->post("reporttype");
      $rekening = $this->input->post("rekening");
      $ticketnumber = $this->input->post("ticketnumber");
      $brand = $this->input->post("brand");
      
      if($reporttype == "withdrawals")
      { 
          $datefrom = $datefrom . " 00:00:00";
          $dateto = $dateto . " 23:59:59";

        //   $querystring = "select a.*, b.keterangan,c.reportbrand from withdrawals a left join manualin_logs b on a.ticketnumber = b.ticketnumber and a.brand = b.brand left join brandrealname c on a.brand = c.brand where a.authorizeddate <> '100' and a.requestdate >= '".$datefrom . "' and a.requestdate <= '".$dateto . "' and a.takeraccount = '" .$rekening."' and a.brand in (select brand from brands where brandgroup = '".$this->session->userdata('brandgroup')."') order by a.requestdate,a.ticketnumber desc";
          
          $querystring = "select a.*, b.keterangan,c.reportbrand from withdrawals a left join manualin_logs b on a.ticketnumber = b.ticketnumber and a.brand = b.brand left join brandrealname c on a.brand = c.brand where a.authorizeddate <> '100' and a.requestdate >= '".$datefrom . "' and a.requestdate <= '".$dateto . "' and a.takeraccount = '" .$rekening."' order by a.requestdate,a.ticketnumber desc";


         
          $data['listwd'] = $this->m_masters->querying($querystring)->result();
          
          $querystring = str_replace("'","''",$querystring);
          $querylog = "insert into logs_report values('".$querystring."','".$this->session->userdata('username')."',now())";
          $this->m_masters->querying($querylog);
          
        //   echo $querylog;
          $querystring = "select sum(Amount) as totalwd from withdrawals where requestdate >= '".$datefrom . "' and requestdate <= '".$dateto . "' and takeraccount = '" .$rekening."'";
          $data['totalwd'] = $this->m_masters->querying($querystring)->result();
        //   echo $querystring;
        
        
        
          
          if(count($data['listwd']) == 0)
          {
            echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              No Records Found!
                            </div>';
          }
          else
          {
            $this->load->view("v_wdlist_dg",$data);
          }

       }
       
      if($reporttype == "depositspermata")
      { 
          $datefrom = $datefrom . " 00:00:00";
          $dateto = $dateto . " 23:59:59";

          $querystring = "select a.*, b.keterangan from deposits_permata_panelform a left join manualin_deposits_logs b on a.ticketnumber = b.ticketnumber and a.brand = b.brand where a.to_account = 'SCAN BARCODE' and a.approveddate <> '100' and a.requestdate >= '".$datefrom . "' and a.requestdate <= '".$dateto . "' order by a.requestdate,a.ticketnumber desc";

          
         
          $data['listdepo'] = $this->m_masters->querying($querystring)->result();
        //   echo $querystring;
          
          
          
          $querystring = str_replace("'","''",$querystring);
          $querylog = "insert into logs_report values('".$querystring."','".$this->session->userdata('username')."',now())";
          $this->m_masters->querying($querylog);
          
        //   echo $querylog;
        //   $querystring = "select sum(Amount) as totalwd from withdrawals where requestdate >= '".$datefrom . "' and requestdate <= '".$dateto . "' and takeraccount = '" .$rekening."'";
        //   $data['totalwd'] = $this->m_masters->querying($querystring)->result();
        //   echo $querystring;
        
        
        
          
          if(count($data['listdepo']) == 0)
          {
            echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              No Records Found!
                            </div>';
          }
          else
          {
            $this->load->view("v_depolist_dg",$data);
          }

       }
       
       
       
       
      if($reporttype == "ctn")
      { 
          
          $datefrom = $datefrom . " 00:00:00";
          $dateto = $dateto . " 23:59:59";
         $querystring = "select a.*, b.keterangan from withdrawals a left join manualin_logs b on a.ticketnumber = b.ticketnumber and a.brand = b.brand where a.ticketnumber = '".$ticketnumber."' and a.requestdate >= '".$datefrom . "' and a.requestdate <= '".$dateto . "' and  a.brand in (select brand from brands where brandgroup in( select brandgroup from users where user = '".$this->session->userdata('username')."')) order by a.requestdate";
         
        // echo $querystring;
        $data['listwd'] = $this->m_masters->querying($querystring)->result();
        $pernahada = count($data['listwd']);
        // echo $pernahada;
         if($pernahada > 0 )
         {
                $this->load->view("v_wdlist_dg",$data);
          }
          else
          {
             echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Ticket Number tidak ditemukan!
                            </div>';
          }

      }
      
      if($reporttype == "mtn")
      { 
          
          $datefrom = $datefrom . " 00:00:00";
          $dateto = $dateto . " 23:59:59";
         $querystring = "select a.*, b.keterangan from withdrawals a left join manualin_logs b on a.ticketnumber = b.ticketnumber and a.brand = b.brand where a.authorizeddate = '100' and a.requestdate >= '".$datefrom . "' and a.requestdate <= '".$dateto . "' and  a.brand in (select brand from brands where brandgroup in( select brandgroup from users where user = '".$this->session->userdata('username')."')) order by a.requestdate";
         
        // echo $querystring;
        $data['listwd'] = $this->m_masters->querying($querystring)->result();
        $pernahada = count($data['listwd']);
        // echo $pernahada;
         if($pernahada > 0 )
         {
                $this->load->view("v_wdlist_dg",$data);
          }
          else
          {
             echo '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Ticket Number tidak ditemukan!
                            </div>';
          }

      }
       
       
  }

  public function SignOut()
  { 
    session_unset();
    session_destroy();
    redirect('c_login');
  }

  public function main()
  {
      if(empty($this->session->userdata('username')))
      {
        echo "0";
      }
      else
      {
        
        $modulecode = $this->input->post('modulecode');
        $data['modulecode'] = $modulecode;
        
        
        if($modulecode == 'idr')
        {
            $querystring = "select bank from banks";
            $data['banklist'] = $this->m_masters->querying($querystring)->result();
            
            if ($this->session->userdata('privilege') == 'SuperAdmin')
            {
                $querystring = "select brandgroup from brandgroups";
            
                $data['brandgrouplist'] = $this->m_masters->querying($querystring)->result();
            }
            $this->load->view("v_".$modulecode,$data);
            // echo $querystring;
        }
        
        else if($modulecode == 'eaf')
        {
            $querystring = "select distinct sourcebank from adminfee where brandgroup = '".$this->session->userdata('brandgroup')."'";
            $data['sourcebanklist'] = $this->m_masters->querying($querystring)->result();
            
            $querystring = "select distinct destbank from adminfee where brandgroup = '".$this->session->userdata('brandgroup')."'";
            $data['destbanklist'] = $this->m_masters->querying($querystring)->result();
            
            $this->load->view("v_".$modulecode,$data);
        }
        
        else if($modulecode == 'usr')
        {
           if ($this->session->userdata('privilege') == 'SuperAdmin')
            {
                $querystring = "select brandgroup from brandgroups";
            
                $data['brandgrouplist'] = $this->m_masters->querying($querystring)->result();
            }
           $this->load->view("v_".$modulecode,$data); 
        }
        
        else if($modulecode == 'reports')
        {
            // echo $this->session->userdata('privilege');
            if ($this->session->userdata('privilege') == 'SuperAdmin')
            {
                $querystring = " select a.*, b.userid, b.namarekening, b.bank from ( select distinct sourceaccount from sourceaccounts_brandgroup )a left join sourceaccountsindividu b on a.sourceaccount = b.nomorrekening";
            }
            else if ($this->session->userdata('privilege') == 'Admin')
            {
                $querystring = "select a.sourceaccount,b.userid, b.namarekening, b.bank from sourceaccounts_brandgroup a left join sourceaccountsindividu b on a.sourceaccount = b.nomorrekening where a.brandgroup = '".$this->session->userdata('brandgroup')."'";
            }
            else if ($this->session->userdata('privilege') == 'Reporter')
            {
                $querystring = "select a.sourceaccount,b.userid, b.namarekening, b.bank from sourceaccounts_brandgroup a left join sourceaccountsindividu b on a.sourceaccount = b.nomorrekening where a.brandgroup in (select brandgroup from users where user = '".$this->session->userdata('username')."')";
                
            }
            
            
            $data['rekeninglist'] = $this->m_masters->querying($querystring)->result();


            // $querystring = "select brand from brands where brandgroup in( select brandgroup from users where user = '".$this->session->userdata('username')."')";
        
            // $data['brandlist'] = $this->m_masters->querying($querystring)->result();
            
            $this->load->view("v_".$modulecode,$data);
        }
        
        else if($modulecode == 'dtn' || $modulecode == 'dtnd' )
        {
            
            $querystring = "select brand from brands where brandgroup = '".$this->session->userdata('brandgroup')."'";
            $data['brandlist'] = $this->m_masters->querying($querystring)->result();
            $this->load->view("v_".$modulecode,$data);
        }
        

        
        else
        {
            $this->load->view("v_".$modulecode,$data);
        }
        
        // else if($modulecode == 'ML')
        // {
        //     if(empty($this->session->userdata('nomorrekening')) && empty($this->session->userdata('tempnomorrekening')))
        //     {
        //         $querystring = "select satu.* from sourceaccounts satu left join 
        //         (
        //         	select a.* from loginrequests a inner join
        //         	(
        //         		select rekening, max(time) as lastupdate from loginrequests group by rekening
        //         	)b on a.rekening = b.rekening and a.time = b.lastupdate
        //         )dua on satu.nomorrekening = dua.rekening 
        //         where  
        //             ( 
        //             (dua.rekening is null)
        //         	or (dua.type = 'logout')
        //         	or (dua.type = 'login' and dua.status = '3')
        //         	)
        //         	and
        //         	(satu.useable = 1)
        //         order by satu.balance desc limit 1";
        //         $data['utility'] = $this->m_masters->querying($querystring)->result();
        //         // print_r($data['utility']);
        //         if (count($data['utility']) == 0)
        //         {
        //             echo '<div class="alert alert-danger alert-dismissible">
        //                   <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        //                   <h5><i class="icon fas fa-check"></i> Danger!</h5>
        //                   Tidak ada rekening yang available sekarang!
        //                 </div>';
                    
        //         }
        //         else
        //         {
        //             $this->session->set_userdata('tempnomorrekening',$data['utility'][0]->nomorrekening);
        //             $this->session->set_userdata('tempcorpid',$data['utility'][0]->corpid);
        //             $this->session->set_userdata('tempuserid',$data['utility'][0]->userid);
        //             $this->session->set_userdata('tempstickernumber',$data['utility'][0]->stickernumber);
        //             $this->load->view("v_".$modulecode,$data);
            
                    
        //         }
        //     }
        //     else
        //     {
        //         echo '<div class="alert alert-danger alert-dismissible">
        //                   <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        //                   <h5><i class="icon fas fa-check"></i> Danger!</h5>
        //                   Sesi MBCA masih ada! jangan login MBCA lagi!
        //                 </div>';
                
        //     }
        // }
        
        // else if($modulecode == 'LK')
        // {
        //     $querystring = "select * from loginrequests where rekening = '".$this->session->userdata('tempnomorrekening'). "' order by time desc limit 1";
            
        //     // echo $querystring;
        //     $latestlogin = $this->m_masters->querying($querystring)->result();
        //     $latestloginstatus = $latestlogin[0];
        //     $latestlogincount = count($latestlogin);
            
            
        //     if($latestloginstatus->type == 'logout' || 
        //         ($latestloginstatus->type == 'login' && $latestloginstatus->status == '3') ||  ($latestloginstatus->type == 'login' && $latestloginstatus->status == '4') ||  
        //          ($latestlogincount == 0))
        //     {
        //         $this->session->unset_userdata('tempnomorrekening',$data['utility'][0]->nomorrekening);
        //         $this->session->unset_userdata('tempcorpid');
        //         $this->session->unset_userdata('tempuserid');
        //         $this->session->unset_userdata('tempstickernumber');
                
        //         echo '<div class="alert alert-danger alert-dismissible">
        //                   <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        //                   <h5><i class="icon fas fa-check"></i> Danger!</h5>
        //                   Mobile BCA telah terlog-out! Mohon untuk login MBCA lagi di menu MBCA Logins!
        //                 </div>';
      
        //     }
            
        //     // else if($latestloginstatus->type == 'login' && ($latestloginstatus->status == '0' || $latestloginstatus->status == '1'))
        //     // {
        //     //     echo '<div class="alert alert-danger alert-dismissible">
        //     //               <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        //     //               <h5><i class="icon fas fa-check"></i> Danger!</h5>
        //     //               Process login MBCA masih jalan dari sisi Core Payment! Mohon ditunggu
        //     //             </div>';
                
        //     // }
        //     else if($latestloginstatus->type == 'login' && $latestloginstatus->status == '2')
        //     {
        //         // echo 'saatnya regis';
                
        //         $this->session->set_userdata('nomorrekening',$this->session->userdata('tempnomorrekening'));
        //         $this->session->set_userdata('corpid',$this->session->userdata('tempcorpid'));
        //         $this->session->set_userdata('userid',$this->session->userdata('tempuserid'));
        //         $this->session->set_userdata('stickernumber',$this->session->userdata('tempstickernumber'));
                    
                    
        //         $querystring = "select * from requests where nomorrekening = '".$this->session->userdata['nomorrekening']."' order by requestid desc";
        //         $requestterakhir = $this->m_masters->querying($querystring)->result();
        //         // $jumlahrequestterakhir = count($requestterakhir);
                
        //         if( $requestterakhir[0]->status == '0' || $requestterakhir[0]->status == '3' ||$requestterakhir[0]->status == '41' ||$requestterakhir[0]->status == '73' || $requestterakhir[0]->status == '74' || $requestterakhir[0]->status == '8')
        //         {
                    
        //           $querystring ="insert into requests values ('','withdrawal',now(),'0','".$this->session->userdata('nomorrekening')."','".$this->session->userdata('corpid')."','".$this->session->userdata('userid')."','".$this->session->userdata('stickernumber')."','','','System',now());";
                  
        //           $this->m_masters->querying($querystring);
        //           $newid = $this->db->insert_id();
        //           $this->db->close();
                    
        //             echo '<div class="alert alert-success alert-dismissible">
        //                   <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        //                   <h5><i class="icon fas fa-check"></i> Success!</h5>
        //                   Request untuk Clear Form sudah diproses! Mohon terus lakukan Refresh lembar kerja!
        //                 </div>'; 
        //         }
                
        //         if($requestterakhir[0]->status == '1')
        //         {
        //             $data['requestterakhir'] = $requestterakhir;
        //             $modulecode = 'GRE';
                    
        //             $querystring = "select pintoken from sourceaccounts where nomorrekening = '".$this->session->userdata('nomorrekening')."'";
        //             $pintoken = $this->m_masters->querying($querystring)->result();
        //             $data['pintoken'] = $pintoken;
                    
        //             $data['newstatus'] = '2';
        //             // print_r($data['requestterakhir']);
        //             $this->load->view('v_'.$modulecode,$data);
        //         }
                
        //         if($requestterakhir[0]->status == '11')
        //         {
        //             $data['requestterakhir'] = $requestterakhir;
        //             $modulecode = 'GRE';
                    
        //             $querystring = "select pintoken from sourceaccounts where nomorrekening = '".$this->session->userdata('nomorrekening')."'";
        //             $pintoken = $this->m_masters->querying($querystring)->result();
        //             $data['pintoken'] = $pintoken;
                    
        //             $data['newstatus'] = '12';
        //             // print_r($data['requestterakhir']);
        //             $this->load->view('v_'.$modulecode,$data);
        //         }
        //         if($requestterakhir[0]->status == '4')
        //         {
        //             $querystring = "select * from accountperrequest";
        //             $accountperrequest = $this->m_masters->querying($querystring)->result()[0]->accountperrequest;
                
        //             $querystring = "select a.*, b.bankname, c.destaccount from withdrawals a inner join bankmapping b on a.bank = b.bank left join sourcetodestaccounts c on a.takeraccount = c.sourceaccount and a.playeraccount = c.destaccount where authorizeddate ='' and takeraccount = '".$this->session->userdata('nomorrekening')."' and a.bank in (select bank from bankincluded) order by approveddate limit ".$accountperrequest;
                
        //             // echo $querystring;
        //             $outstandingwithdrawals = $this->m_masters->querying($querystring)->result();
        //             $outstandingwithdrawalscount = count($outstandingwithdrawals);
        //             // print_r($outstandingwithdrawals);
                    
                    
        //             if($outstandingwithdrawalscount < $accountperrequest )
        //             {
        //                 $requirecount = $accountperrequest - $outstandingwithdrawalscount;
        //                 $querystring = "update withdrawals set takendate = now(),takeraccount = '".$this->session->userdata('nomorrekening'). "' where takendate = '' and bank in (select bank from bankincluded) order by approveddate limit ".$requirecount;
        //                 $this->m_masters->querying($querystring);
                        
                    
                        
        //                 $querystring = "select a.*, b.bankname, c.destaccount from withdrawals a inner join bankmapping b on a.bank = b.bank left join sourcetodestaccounts c on a.takeraccount = c.sourceaccount and a.playeraccount = c.destaccount where authorizeddate ='' and takeraccount = '".$this->session->userdata('nomorrekening')."' and a.bank in (select bank from bankincluded) order by approveddate limit ".$accountperrequest;
        //                 $alldata = $this->m_masters->querying($querystring)->result();
                        
        //             } 
        //             else
        //             {
        //                 $alldata = $outstandingwithdrawals;
        //             }
        //             // print_r($alldata);
                    
                    
        //             $saldoyangdibutuhin = 0;
        //             for($z = 0; $z < count($alldata) ; $z++)
        //             {
        //                 $saldoyangdibutuhin = $saldoyangdibutuhin  + $alldata[$z]->amount;
                        
        //             }
                    
        //             $querystring = "select balance from sourceaccounts where nomorrekening = '".$this->session->userdata('nomorrekening'). "'";
 
                    
        //             $saldoaktual = $this->m_masters->querying($querystring)->result()[0]->balance;
                    
        //             if($saldoaktual < $saldoyangdibutuhin)
        //             {
                        
        //                 $querystring = "insert into loginrequests values ('','logout','".$this->session->userdata('nomorrekening') . "',now(),'2','','','','','','System',now());";
        //                 $this->m_masters->querying($querystring);
                        
                        
        //                 $querystring = "update requests set status = '41', time=now() where requestid = ".$requestterakhir[0]->requestid;
        //                 $this->m_masters->querying($querystring);
                        
        //                 $querystring = "update withdrawals set takeraccount = '',takendate = '' where takeraccount = '".$this->session->userdata('nomorrekening')."'";
        //                 $this->m_masters->querying($querystring);
                        
        //                 $querystring = "update sourceaccounts set useable = 0 where nomorrekening = '".$this->session->userdata('nomorrekening')."'";
        //                 $this->m_masters->querying($querystring);
                        
                        
        //                 echo '<div class="alert alert-danger alert-dismissible">
        //                 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        //                 <h5><i class="icon fas fa-check"></i>Danger!</h5>
        //                 Rekening yang sekarang digunakan tidak dapat untuk melakukan transfer dana dikarenakan saldo tidak cukup! Anda harus login MBCA lagi di menu MBCA Login!
        //                 </div>';
        //             }
        //             else
        //             {
                       
                      
        //               for($i = 0; $i<count($alldata);$i++)
        //               {
        //                     $querystring  ="insert into registrations values ('','".$requestterakhir[0]->requestid."','".$alldata[$i]->ticketnumber. "','".$this->session->userdata('nomorrekening'). "','".$alldata[$i]->playeraccount . "','".$alldata[$i]->bank . "','".$alldata[$i]->amount."','".$alldata[$i]->destaccount."','0','System',now());";
                            
        //                     $this->m_masters->querying($querystring);
                            
        //               }
                      
        //               $querystring = "update requests set status = '5', time = now() where requestid = '".$requestterakhir[0]->requestid."'";
        //               $this->m_masters->querying($querystring);
        //               echo '<div class="alert alert-success alert-dismissible">
        //               <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        //               <h5><i class="icon fas fa-check"></i>Success!</h5>
        //                 Request sudah dikirim !
        //                 </div>';
                      
        //             }
                    
                    
                    
        //         }
                
        //         if($requestterakhir[0]->status == '6')
        //         {
        //             // echo 'asdf';
        //             $data['requestterakhir'] = $requestterakhir;
        //             $modulecode = 'GRE';
                    
        //             $querystring = "select pintoken from sourceaccounts where nomorrekening = '".$this->session->userdata('nomorrekening')."'";
        //             $pintoken = $this->m_masters->querying($querystring)->result();
        //             $data['pintoken'] = $pintoken;
                    
        //             $data['newstatus'] = '7';
        //             $this->load->view('v_'.$modulecode,$data);
                    
        //         }
                
        //         if($requestterakhir[0]->status == '71')
        //         {
        //             // echo 'asdf';
        //             $data['requestterakhir'] = $requestterakhir;
        //             $modulecode = 'GRE';
                    
        //             $querystring = "select pintoken from sourceaccounts where nomorrekening = '".$this->session->userdata('nomorrekening')."'";
        //             $pintoken = $this->m_masters->querying($querystring)->result();
        //             $data['pintoken'] = $pintoken;
                    
        //             $data['newstatus'] = '72';
        //             $this->load->view('v_'.$modulecode,$data);
                    
        //         }
        //     }
        // }
        
        
        
        
      }

  }
  
  public function lembarkerja()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
            $querystring = "select * from loginrequests where rekening = '".$this->session->userdata('nomorrekening'). "' order by time desc limit 1";
            $latestlogin = $this->m_masters->querying($querystring)->result();
            $latestloginstatus = $latestlogin[0];
            $latestlogincount = count($latestlogin);
        
            if($latestloginstatus->type == 'logout' || ($latestloginstatus->type == 'login' && $latestloginstatus->status == '3') || $latestlogincount == 0)
            {
                $this->session->unset_userdata('nomorrekening',$data['utility'][0]->nomorrekening);
                $this->session->unset_userdata('corpid');
                $this->session->unset_userdata('userid');
                $this->session->unset_userdata('stickernumber');
                
                echo '<div class="alert alert-danger alert-dismissible">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                          <h5><i class="icon fas fa-check"></i> Success!</h5>
                          Mobile BCA telah terlog-out! Mohon untuk login MBCA lagi di menu MBCA Logins!
                        </div>';
      
            }
            
            else
            {
                
                $querystring = "select * from accountperrequest";
                $accountperrequest = $this->m_masters->querying($querystring)->result()[0]->accountperrequest;
                
                
                $querystring = "select a.*, b.bankname, c.destaccount from withdrawals a inner join bankmapping b on a.bank = b.bank left join sourcetodestaccounts c on a.takeraccount = c.sourceaccount and a.playeraccount = c.destaccount where authorizeddate ='' and takeraccount = '".$this->session->userdata('nomorrekening')."' and a.bank in (select bank from bankincluded) order by approveddate limit ".$accountperrequest;
                
                
                $outstandingwithdrawals = $this->m_masters->querying($querystring)->result();
                $outstandingwithdrawalscount = count($outstandingwithdrawals);

                
                
                
                if($outstandingwithdrawalscount < $accountperrequest )
                {
                    
                    $requirecount = $accountperrequest - $outstandingwithdrawalscount;
                    $querystring = "update withdrawals set takendate = now(),takeraccount = '".$this->session->userdata('nomorrekening'). "' where takendate = '' and bank in (select bank from bankincluded) order by approveddate limit ".$requirecount;
                    $this->m_masters->querying($querystring);

                    
                    $querystring = "select a.*, b.bankname, c.destaccount from withdrawals a inner join bankmapping b on a.bank = b.bank left join sourcetodestaccounts c on a.takeraccount = c.sourceaccount and a.playeraccount = c.destaccount where authorizeddate ='' and takeraccount = '".$this->session->userdata('nomorrekening')."' and a.bank in (select bank from bankincluded) order by approveddate limit ".$accountperrequest;
                    $alldata = $this->m_masters->querying($querystring)->result();
                    
                }
                else
                {
                    $alldata = $outstandingwithdrawals;
                    
                }
                
                // echo $querystring;

                $saldoyangdibutuhin = 0;
                for($z = 0; $z < count($alldata) ; $z++)
                {
                    $saldoyangdibutuhin = $saldoyangdibutuhin  + $alldata[$z]->amount;
                    
                }
               
                
                
                $querystring = "select balance from sourceaccounts where nomorrekening = '".$this->session->userdata('nomorrekening'). "'";
                
                
                
                $saldoaktual = $this->m_masters->querying($querystring)->result()[0]->balance;
                
                if($saldoaktual < $saldoyangdibutuhin)
                {
                    
                    $querystring = "insert into loginrequests values ('','logout','".$this->session->userdata('nomorrekening'). "',now(),'2','','','','','','System',now())";
                    $this->m_masters->querying($querystring);
                    
                    
                    
                    $this->session->unset_userdata('nomorrekening',$data['utility'][0]->nomorrekening);
                    $this->session->unset_userdata('corpid');
                    $this->session->unset_userdata('userid');
                    $this->session->unset_userdata('stickernumber');
                    
                    echo '<div class="alert alert-danger alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    <h5><i class="icon fas fa-check"></i>Danger!</h5>
                    Rekening yang sekarang digunakan tidak dapat untuk melakukan transfer dana dikarenakan saldo tidak cukup! Anda harus login MBCA lagi di menu MBCA Login!
                    </div>';
                }
                else
                {
                    $querystring ="insert into requests values ('','withdrawal',now(),'0','".$this->session->userdata('nomorrekening')."','".$this->session->userdata('corpid')."','".$this->session->userdata('userid')."','".$this->session->userdata('stickernumber')."','','','System',now());
                    ";
                    
                  $this->m_masters->querying($querystring);
                  $newid = $this->db->insert_id();
                  $this->db->close();
                   
                  $querystring ="";
                  for($i = 0; $i<count($alldata);$i++)
                  {
                        $querystring  ="insert into registrations values ('','".$newid."','".$alldata[$i]->ticketnumber. "','".$this->session->userdata('nomorrekening'). "','".$alldata[$i]->playeraccount . "','".$alldata[$i]->bank . "','".$alldata[$i]->amount."','".$alldata[$i]->destaccount."','0','System',now());";
                        
                        $this->m_masters->querying($querystring);
                        
                  }
                }
                
                echo '<div class="alert alert-success alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-check"></i>Success!</h5>
                    Request sudah dikirim !
                </div>';
                
            }
    }  
  }
  
  public function submitloginticket()
  {
      
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $appli = $this->input->post("appli");
      $appli12 = $this->input->post("appli12");
      if($appli == '' || $appli12 == '')
      {
        $errmsg = $errmsg .'<div class="alert alert-danger alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-check"></i>Danger</h5>
                    Tolong Submit APPLI1 dan APPLI1 yang kedua dengan benar!
                </div>';
        echo $errmsg;
      }
      else
      { 
        $querystring = "select * from loginrequests where rekening = '".$this->session->userdata('nomorrekening'). "' order by time desc limit 1";
        $latestlogin = $this->m_masters->querying($querystring)->result();
        $latestloginstatus = $latestlogin[0];
        $latestlogincount = count($latestlogin);
        
        


        
        if($latestloginstatus->type == 'logout' || ($latestloginstatus->type == 'login' && $latestloginstatus->status == '3') || $latestlogincount == 0)
        {
        
        
        
        
            $queryinsert = "insert into loginrequests values ('','login','".$this->session->userdata('tempnomorrekening'). "',now(),0,'".$this->session->userdata('tempstickernumber')."','".$appli."','" .$this->session->userdata('tempcorpid')."','". $this->session->userdata('tempuserid') . "','".$appli12."','System',now())";
                
                $this->m_masters->querying($queryinsert);  
            // echo $queryinsert;   
            
            
       
            echo '<div class="alert alert-success alert-dismissible">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                          <h5><i class="icon fas fa-check"></i> Success!</h5>
                          Proses login ke MBCA sedang dijalankan oleh Core Payment! Silakan buka ke menu lembar kerja!
                        </div>';
      
        }
        else
        {
            echo '<div class="alert alert-danger alert-dismissible">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                          <h5><i class="icon fas fa-check"></i> Success!</h5>
                          Nomor Rekening is currently logged in!
                        </div>'; 
        }
      }

    }
  }
  
  public function updateappli2()
  {
      
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $appli2 = $this->input->post("appli2");
      $requestid = $this->input->post("requestid");
      $newstatus = $this->input->post("newstatus");
      if($appli2 == '')
      {
        $errmsg = $errmsg .'<div class="alert alert-danger alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-check"></i>Danger</h5>
                    Tolong Submit APPLI2!
                </div>';
        echo $errmsg;
      }
      else
      { 
        $querystring = "update requests set appli2submit = '".$appli2. "',time=now(), status = '".$newstatus."' where requestid = '".$requestid. "'";
        // echo $querystring;
        $this->m_masters->querying($querystring); 
        
        echo '<div class="alert alert-success alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-check"></i> Success!</h5>
                      Finished Updating appli2 ! Processed to Core Payment! Terus Refresh lembar kerja!
                    </div>'; 
      }    
    }
  }
  
  
  
  public function submitapplione()
  {
      
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $appli = $this->input->post("appli");
      $appli12 = $this->input->post("appli12");
    //   echo 'hehe';
      if($appli == '')
      {
        $errmsg = $errmsg .'<div class="alert alert-danger alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-check"></i>Danger</h5>
                    Please Submit APPLI 1 Correctly!
                </div>';
      
        echo $errmsg;
      }
      else
      { 
          
        $querystring = "select requestid from requests where type = 'login' and rekening = '".$this->session->userdata('nomorrekening')."' and status = 0 order by createddate limit 1";
        $idnya = $this->m_masters->querying($querystring)->result()[0]->requestid;

        // echo $idnya;
        
        $querystring = "update requests set param2 = '" .$appli . "', param5 = '".$appli12. "' where requestid =".$idnya;
        
        echo $querystring;
        $this->m_masters->querying($querystring);
   
        echo '<div class="alert alert-success alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-check"></i> Success!</h5>
                      Finished Logging In!
                    </div>';
      }
    }
  }
  
  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 uploadwithdrawals()
  {
    if (empty($_FILES['filemember']['name']) || $group == 'blank')
    {
      $errmsg = $errmsg .'<div class="alert alert-danger alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-check"></i>Danger</h5>
                    Please Upload File!
                </div>';
      
      echo $errmsg;
    }
    else
    {
      
      $path = './assets/excels/';
      require_once APPPATH . "/third_party/PHPExcel.php";
      $config['upload_path'] = $path;
      $config['allowed_types'] = 'xlsx|xls|csv';
      $config['remove_spaces'] = TRUE;
      $config['max_execution_time'] = 259200;
      $config['max_input_time'] = 259200;
      $this->load->library('upload', $config);
      $this->upload->initialize($config);

      if (!$this->upload->do_upload('filemember')) 
      {
          $error = array('error' => $this->upload->display_errors());
      }
      else
      { 
            try
            {
              
              $inputFileName = $path . $_FILES['filemember']['name'];   
              $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
              $objReader = PHPExcel_IOFactory::createReader($inputFileType);
              $objPHPExcel = $objReader->load($inputFileName);
              $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
              $i = 1 ;

              foreach ($allDataInSheet as $value)
              {
                    if($i > 2)
                    {
                      // echo $value['B'] . "<br>";
                      $ticketnumber[$i] = $value['B'];
                      $brand[$i] = $value['C'];
                      $amount[$i] = $value['D'];
                      $requestdate[$i] = $value['I'];
                      $approveddate[$i] = $value['J'];
                      $username[$i] = $value['L'];
                      $fullname[$i] = $value['M'];
                      $bankandnumber[$i] = $value['N'];
                    
                      $amount[$i] = str_replace('.00','',$amount[$i]);
                      $amount[$i] = str_replace(',','',$amount[$i]);
                      $amount[$i] = $amount[$i] . '000';
                      
                      
                      $requestdate[$i] = $this->tidythisdate($requestdate[$i]);
                      $approveddate[$i] = $this->tidythisdate($approveddate[$i]);
                      
                      $firstdelimiterlocation = strpos($bankandnumber[$i] , "|");
                      $bank = substr($bankandnumber[$i], 0 ,$firstdelimiterlocation);
                      
                      $newbankandnumber = substr($bankandnumber[$i], $firstdelimiterlocation + 1, strlen($bankandnumber[$i]) - $firstdelimiterlocation);
                      $seconddelimiterlocation = strpos($newbankandnumber, "|");
                      $playeraccount = substr($newbankandnumber,  $seconddelimiterlocation + 1, strlen($newbankandnumber) - ($seconddelimiterlocation + 1));
                      
                      
                      
                      if($ticketnumber[$i] <> '')
                      {
                        // $querystring = "select * from withdrawals where ticketnumber = '".$ticketnumber[$i]. "'";
                        // $exist = count($this->m_masters->querying($querystring)->result());
                        // if ($exist == 0 )        
                        // {
                            $querystring = "insert into withdrawals values ('".
                            $ticketnumber[$i]. "','".
                            $brand[$i]. "','".
                            $amount[$i]. "','".
                            $requestdate[$i]. "','".
                            $approveddate[$i]. "','".
                            $username[$i]. "','".
                            $fullname[$i]. "','".
                            $bank. "','".
                            $playeraccount. "','".
                            "',".
                            "now(),'".
                            "','".
                            "')";
                            $this->m_masters->querying($querystring);
                        // }
                            
                     }
                    }
                    $i = $i + 1;
                    // echo $querystring;
              }
            } catch (Exception $e)
            {
                die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                                . '": ' .$e->getMessage());
            }
            
            unlink($path . $_FILES['filemember']['name']);
            echo "Finish Uploading Withdrawals (".($i-4) . " rows)";
            


      }
    }
  }
  
}