<?php
class Checkout_model extends CI_model{
function get_session_id(){
        $query = "SELECT * FROM ci_sessions WHERE ip_address = '".$_SERVER['SERVER_ADDR']."'";
        
        $rec = mysql_query($query);
        
        return $rec;
    }
function add_checkout($res){
        
        $ses_id = $res['session_id'];        
        $ip_address = $res['ip_address']; 
        $add_date = $res['add_date'];
       
       /* $user_data = $res['user_data'];        
        $user_name = $res['name'];        
        $user_email = $res['email'];       
        $user_phone = $res['phone'];
        $user_address1 = $res['address1'];
        $user_address2 = $res['address2'];
        $user_city = $res['city'];
        $user_state = $res['state'];
        $user_zip = $res['zip'];
        $user_country = $res['country'];
        $add_date = $res['add_date'];
        
        $query = "INSERT INTO contact_us (id, topic, session_id, ip_address, user_data, user_name, user_email, user_phone, user_address1, user_address2, user_city, user_state, user_zip, user_country,add_date) VALUES ('null', 'shopping shipping request', '".$ses_id."', '".$ip_address."', '".$user_data."', '".$user_name."', '".$user_email."', '".$user_phone."', '".$user_address1."', '".$user_address2."', '".$user_city."', '".$user_state."', '".$user_zip."', '".$user_country."', '".$add_date."')";
        
        $res = mysql_query($query);
          $uid=mysql_insert_id();
 if($uid){   
            $this->load->library('email');
            $this->email->set_mailtype('html');
             $msg="Dear ".$res['name'].",<br> We received your order and will contact you soon <br> Thank you <br> creationegypt team";
			$this->email->from('noreply@creationegyptco.com');
			$this->email->to($res['email']);
			$this->email->subject('New Message from creationegyptco.com');
			$this->email->message($msg);
			$this->email->send();
			
			 $msg2="Dear Admin,<br> new order sent to your site Admin panel pls check this link to see it <a href='".base_url()."AdminCp/orders/view_order/".$uid."' >check new order by clicking here</> <br> Thank you <br> creationegypt team";
			$this->email->from('noreply@creationegyptco.com');
			$to= $this->fun->get_email();
			$this->email->subject('New Order Sent to your site -creationegyptco.com');
			$this->email->message($msg2);
			$this->email->send();

  }*/
      $rand=$ses_id.time();

$cart = $this->cart->contents();
foreach($cart as $item):
$p_name=$item['name'];
$p_id=$item['id'];
$p_q=$item['qty'];
$p_price=$item['price'];
 $query2 = "INSERT INTO `orders` (session_id,uid, pid, pname, qty, price,add_date) VALUES ('".$rand."','".$this->session->userdata('userid')."', '".$p_id."', '".$p_name."', '".$p_q."', '".$p_price."','".$add_date."')";     
  $res2 = mysql_query($query2);

endforeach; 
 $this->cart->destroy();
   }
 
  // $query = "DELETE FROM ci_sessions WHERE session_id = '".$ses_id."'";
        
       // $rec = mysql_query($query);

             // mysql_query("delete FROM ci_sessions WHERE session_id= '$ses_id'");

        
       // $_SESSION['ses_msg'] = "Your shopping shipping request has been done, Thanks.";
       // redirect('temp');
   
}