<?php 
Class Transaction extends AdminController
{
 function __construct(){
  parent::__construct();
}

function edit_entry($entry_id=false)
{
  if($entry_id)
    return false;

  if($this->input->server('REQUEST_METHOD') === 'POST'){
   $post = $this->input->post();

   if(!empty($post['entry_id']))
    return false;

  $save=array();
  // echo "<pre>";print_r($post);exit;
   // $entry_date = str_replace("/","-", $save['entry_date']);
   // list($dy, $mn, $yr) = explode('-', $entry_date);
   // $save['entry_date']= mktime(0,0,0,$mn,$dy,$yr); 

  if(!empty($_FILES['upload1']['name']) || !empty($_FILES['upload2']['name'])){
   $upload_path ='./uploads/entry/';

   if(!file_exists($upload_path))
    mkdir($upload_path, 0777, true);

  $config['upload_path'] = $upload_path;
  $config['allowed_types'] = '*';
            // $config['max_size'] = '100';
            // $config['max_width']  = '700';
            // $config['max_height']  = '700';
  $this->load->library('upload', $config);
}

if(!empty($_FILES['upload1']['name'])){ 
  if(!empty($post['entry_id'])){
    $condition = array();
    $condition[] = array('name'=>'entry_id','value'=>$post['entry_id']);
    $select=array('upload1');
    $res = $this->setting_model->get_setting('entry',$condition,false,false,$select);
    if(!empty($res['upload1'])){
      unlink('./uploads/entry/'.$res['upload1']);
    }
  } 

  if ( ! $this->upload->do_upload('upload1')){
    $this->session->set_flashdata("alert_error",$this->upload->display_errors());
  }else{
    $image_info = $this->upload->data();
    $save['upload1'] = $image_info['file_name'];
  }
}

if(!empty($_FILES['upload2']['name'])){
  $upload_path ='./uploads/entry/';

  if(!file_exists($upload_path))
    mkdir($upload_path, 0777, true);

  if(!empty($post['entry_id'])){
    $condition = array();
    $condition[] = array('name'=>'entry_id','value'=>$post['entry_id']);
    $select=array('upload2');
    $res = $this->setting_model->get_setting('entry',$condition,false,false,$select);
    if(!empty($res['upload2'])){
      unlink('./uploads/entry/'.$res['upload2']);
    }
  } 

  if (!$this->upload->do_upload('upload2')){
    $this->session->set_flashdata("alert_error",$this->upload->display_errors());
  }else{
    $image_info = $this->upload->data();
    $save['upload2'] = $image_info['file_name'];
  }
} 

$save['veh_no']=$post['veh_no'];

$condition = array();
$condition[] = array('name'=>'vehicle_type_id','value'=>$post['vt_id']); 
$res = $this->setting_model->get_setting('vehicle_type',$condition);
if(empty($res)){
  $save1=array();
  $save1['name'] =$post['vt_id'];  
  $save1['status'] = '1'; 
  $save1['deleted'] = '0'; 
  $save1['created_by'] = $this->session->userdata('admin_id');
  $save1['created_date'] = time();   
  $post['vt_id'] = $this->setting_model->save_setting('vehicle_type',$save1,true); 
}

$save['vt_id']=$post['vt_id'];

$condition = array();
$condition[] = array('name'=>'material_type_id','value'=>$post['mt_id']); 
$res = $this->setting_model->get_setting('material_type',$condition);
if(empty($res)){
  $save1=array();
  $save1['name'] =$post['mt_id']; 
  $save1['status'] = '1'; 
  $save1['deleted'] = '0'; 
  $save1['created_by'] = $this->session->userdata('admin_id');
  $save1['created_date'] = time();   
  $post['mt_id'] = $this->setting_model->save_setting('material_type',$save1,true); 
}

$save['mt_id']=$post['mt_id'];

$save['container_no']=$post['container_no'];
$save['mobile1']=$post['mobile1'];
$save['mobile2']=$post['mobile2'];
$save['load_type']=$post['load_type'];

if($post['load_type']=='E'){
  $save['empty_wt']=$post['empty_wt1'];
  $save['tot_wt']=0;
  $save['net_wt']=0;
  $save['net_amt']=$post['net_amt1']; 
}
else{
  $save['tot_wt']=$post['tot_wt'];
  $save['empty_wt']=$post['empty_wt2']; 
  $save['net_wt']=$post['net_wt'];
  $save['net_amt']=$post['net_amt2']; 
}  

$save['remarks']=$post['remarks'];

if(empty($post['entry_id'])){
 $ret=$this->get_next_sno(); 

 $save['entry_no']=$ret[0];
 $save['entry_sno']=$ret[1];
 $save['entry_date']=time(); 

 $save['deleted'] = '0'; 
 $save['created_by'] = $this->session->userdata('admin_id');
 $save['created_date'] = time();   
 $id= $this->setting_model->save_setting('entry',$save,true);   
}
else{ 
  $save['entry_id']=$post['entry_id'];

  $save['modified_by'] = $this->session->userdata('admin_id');
  $save['modified_date'] = time(); 
  $this->setting_model->save_setting('entry',$save);   

  $id=$post['entry_id'];   
}     

// $msg='Welcome%20to%20SPS%20Weigh%20Bridge,%20Salem,%20Ph%20:%209944117090,%209944448764\n';
// $msg.='Date/Time:'.date('d-m-Y, h:i A').'\n';
// $msg.='Vehicle%20No:'.$save['veh_no'].', G.Wt:'.$save['tot_wt'].', E.Wt:'.$save['empty_wt'].'\n'; 
// $msg.='Net.Wt:'.$save['net_wt'].',\n\n'; 
// $msg.='Thank%20you%20Visit%20Again'; 

$res_sms = $this->setting_model->get_setting('wb_sms'); 

$url = 'http://sms.4mdesigner.com/vendorsms/pushsms.aspx?apikey='.$res_sms['api_key'].'&clientId='.$res_sms['client_id'].'&msisdn='.$post['mobile1'].'&sid='.$res_sms['sender_id'].'&msg=Welcome%20to%20SPS%20Weigh%20Bridge,%20Salem,%20Ph%20:%209944117090,%209944448764Date/Time%20:'.date('d-m-Y, h:i A').'Vehicle%20No%20:'.$save['veh_no'].',%20E.Wt:'.$save['empty_wt'].'Net.Wt%20:'.$save['net_wt'].',Thank%20you%20Visit%20Again&fl=0&gwid=2';

//echo $url; exit;

// $url='http://sms.4mdesigner.com/vendorsms/pushsms.aspx?apikey=8fc1b0fc-b450-4fd1-bc3b-2cba41e2ceb6&clientId=055bf75b-7863-4bfb-acea-0ab28a31df6a&msisdn=9894217795&sid=SPSCWB&msg=Welcome%20to%20SPS%20Weigh%20Bridge,%20Salem,%20Ph%20:%209944117090,%209944448764Date/Time%20:08-07-2022,%209.30amVehicle%20No%20:TN30bg5896,%20G.Wt:853,%20E.Wt:899Net.Wt%20:89544,Thank%20you%20Visit%20Again&fl=0&gwid=2';

// echo $url;exit; 

$header = array("Accept: application/json"); 

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
$resp = json_decode($resp);    
curl_close($ch);

$sms_resp='';
if(!empty($resp)){
  if($resp->ErrorCode=='000')
    $sms_resp='S';
  else
    $sms_resp=substr($resp->ErrorMessage,0,50);
}
else
  $sms_resp='Failed';

$save=array();
$save['entry_id']=$id; 
$save['sms_resp']=$sms_resp;

$this->setting_model->save_setting('entry',$save);

$this->session->set_flashdata("alert_success","Entry saved successfully!");      
redirect('entry/'.$id);    
}

if($entry_id){
  $data['page_title'] =$data['meta_title']= 'Edit Entry';    

  $condition = array();
  $condition[] = array('name'=>'entry_id','value'=>$entry_id); 
  $data['entry'] = $this->setting_model->get_setting('entry',$condition);
  $entry_no=$data['entry']['entry_no']; 
}
else{
  $data['page_title'] =$data['meta_title']= 'Add Entry';  

  $ret=$this->get_next_sno(); 
  $entry_no=$ret[0];

  $data['entry']=false;  
}

$data['entry_id']=$entry_id;
$data['entry_no']=$entry_no;
$data['breadcrumbs'][] = array('title'=>$data['meta_title'], 'href'=> "");  

$order=array();  
$order[] = array('name'=>'name','value'=>'asc'); 
$condition=array();
$condition[] = array('name'=>'deleted','value'=>'0');
$select = array('material_type_id','name');
$data['mts']= $this->setting_model->get_setting('wb_material_type',$condition,$order,true,$select);          
$order=array();  
$order[] = array('name'=>'name','value'=>'asc');  
$select = array('vehicle_type_id','name');
$data['vts']= $this->setting_model->get_setting('wb_vehicle_type',$condition,$order,true,$select);

$order=array();  
$group[] = array('value'=>'veh_no');
$order[] = array('name'=>'veh_no','value'=>'asc'); 
$select = array('veh_no');
$data['vnos']= $this->setting_model->get_setting('wb_entry',false,$order,false,$select,false,$group);

//echo "<pre>";print_r($data);exit;

$this->load->view('common/header',$data);
$this->load->view('transaction/edit_entry',$data);
$this->load->view('common/footer',$data);
}

function get_next_sno(){
  $order[] = array('name'=>'entry_id','value'=>'desc'); 
  $select = array('entry_no','entry_sno');
  $res = $this->setting_model->get_setting('entry',false,$order,false,$select);
  // echo "<pre>aaa";print_r($res);exit;
  if(!empty($res) && !empty($res[0]['entry_no'])) {
    $entry_no=$res[0]['entry_no']; 
    $entry_sno=$res[0]['entry_sno']; 

    $pre=str_replace($entry_sno,"",$entry_no);

    $entry_sno = (double)$entry_sno;

    if(!$pre){
     if($entry_sno>=9999){
      $entry_sno='0001';
      $entry_no='A'.$entry_sno;  
    }
    else{

      $entry_sno++;
      $entry_sno=$entry_no=str_pad($entry_sno, 4, '0', STR_PAD_LEFT);  
    } 
  }
  else{ 
    if($entry_sno>=9999){
      $ar1=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

      $ar2=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

      $pre_ar=$ar1;
      foreach ($ar1 as $key => $a1) { 
        foreach ($ar2 as $key => $a2) { 
          $pre_ar[]=$a1.$a2;
        } 
      }  

      $exists=false;
      foreach ($pre_ar as $key => $pr) {
        if($pre==$pr){
          $exists=true;
          $pre=$pre_ar[$key+1];
          break;
        }
      }

      if(!$exists){
        echo "All S.No. ends! Contact developer!";
        return false;
      }

      $entry_sno='0001';
      $entry_no=$pre.$entry_sno;  
    }
    else{
      $entry_sno++;
      $entry_sno=str_pad($entry_sno, 4, '0', STR_PAD_LEFT);  
      $entry_no=$pre.$entry_sno;  
    } 

  }
}
else{
  $entry_no=$entry_sno='0001'; 
}

return array($entry_no,$entry_sno);
}

function entry_list($entry_id=false){
  $data['entry_id']=$entry_id;
  $data['admin_perms']= get_admin_perms();
  if(strpos($data['admin_perms']['view_perm'],',4,')=== false && strpos($data['admin_perms']['edit_perm'],',4,')=== false && strpos($data['admin_perms']['del_perm'],',4,')=== false){    
    redirect('login');  
  }

  $data['meta_title'] =$data['page_title'] = 'Entry List';  

  $condition=array();
  if(!empty($_POST['vt_id'])){
    $condition[] = array('name'=>'vt_id','value'=>$_POST['vt_id']);
    $data['vt_id']=$_POST['vt_id'];
  }
  else
    $data['vt_id']=0; 

  if(!empty($_POST['mt_id'])){
    $condition[] = array('name'=>'mt_id','value'=>$_POST['mt_id']);
    $data['mt_id']=$_POST['mt_id'];
  }
  else
    $data['mt_id']=0;

  if(!empty($_POST['veh_no'])){
    $condition[] = array('name'=>'veh_no','value'=>$_POST['veh_no']);
    $data['veh_no']=$_POST['veh_no'];
  }
  else
    $data['veh_no']=0;

  $select = array('entry.*','wb_vehicle_type.name as vt_name','wb_material_type.name as mt_name');
  $join[] = array('table_name' => 'wb_material_type' ,'condition' => 'entry.mt_id =wb_material_type.material_type_id' ,'join_type' => 'left');
  $join[] = array('table_name' => 'wb_vehicle_type' ,'condition' => 'entry.vt_id =wb_vehicle_type.vehicle_type_id' ,'join_type' => 'left');
  $group[] = array('value'=>'entry.entry_id');

  $order[] = array('name'=>'entry_id','value'=>'desc');  
  $data['recs'] = $this->setting_model->get_setting('entry',$condition,$order,false,$select,$join,$group);

  $order=array();  
  $order[] = array('name'=>'material_type_id','value'=>'asc'); 
  $select = array('material_type_id','name');
  $data['mts']= $this->setting_model->get_setting('wb_material_type',false,$order,true,$select);          
  $order=array();  
  $order[] = array('name'=>'vehicle_type_id','value'=>'asc'); 
  $select = array('vehicle_type_id','name');
  $data['vts']= $this->setting_model->get_setting('wb_vehicle_type',false,$order,true,$select);

  $order=$group=array();  
  $group[] = array('value'=>'veh_no');
  $order[] = array('name'=>'veh_no','value'=>'asc'); 
  $select = array('veh_no');
  $data['vnos']= $this->setting_model->get_setting('wb_entry',false,$order,false,$select,false,$group);

  //echo $this->db->last_query();exit;
         //    echo "<pre>";print_r($data['vnos']);exit;
  $this->load->view('common/header',$data);
  $this->load->view('transaction/entry_list',$data);
  $this->load->view('common/footer',$data);

}



}
?>