<?php
class Quotation_model extends CI_Model {
function __construct()
	{
		parent::__construct();

	}
	public function get_quotation_list()
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM quotation where IsActive=0";
        $record = $this->db->query($sql);
        return $record->result_array();
	}	

	public function get_tender_details($tender_id)
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM quotation where IsActive=0 and tender_id='$tender_id' order by sort_order";
	//	echo $sql;
        $record = $this->db->query($sql);
        return $record->result_array();
	}	
	
	public function get_product_list()
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM product where IsActive=0";
        $record = $this->db->query($sql);
        return $record->result_array();
	}	
	public function get_tender_list()
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM quotation where IsActive=0 group by tender_no,tender_id order by createdAt DESC";
        $record = $this->db->query($sql);
        return $record->result_array();
	}	

	public function save_quotation($post_data,$path)
	{
			
				$sql="select max(tender_id) as tender_id from quotation";
				$r=$this->db->query($sql);
				$rr=$r->row('tender_id');
				$tender_id=$rr+1;
				$tender_no=$post_data['tender_no'];
			$count2 = count($post_data['category']);
			//echo $count2;
				$query_values2 = array();

				for($i=0; $i<$count2; $i++)
				{
				//	echo $i;
				$db = get_instance()->db->conn_id;
                $pieces=$post_data['pieces'];
				$location=$post_data['location'];
				$tender_no=$post_data['tender_no'];
				$tender_date1=$post_data['tender_date'];
				$tender_date=date("Y-m-d", strtotime($tender_date1));
			//	$tender_id=$post_data['tender_id'];
				$sort_order=mysqli_real_escape_string($db,$post_data['sort_order'][$i]);
				$category=mysqli_real_escape_string($db,$post_data['category'][$i]);
				$component=mysqli_real_escape_string($db,$post_data['component'][$i]);
				$price=mysqli_real_escape_string($db,$post_data['price'][$i]);
				$quantity=mysqli_real_escape_string($db,$post_data['quantity'][$i]);
				$amount=mysqli_real_escape_string($db,$post_data['amount'][$i]);
				$product=$post_data['product'];
				$valor=mysqli_real_escape_string($db,$post_data['valor'][$i]);
				$sorder=mysqli_real_escape_string($db,$post_data['sorder'][$i]);
				$component_no=mysqli_real_escape_string($db,$post_data['component_no'][$i]);
				$tender_due_date1=$post_data['tender_due_date'];
				$tender_due_date=date("Y-m-d", strtotime($tender_due_date1));
			    $cstatus=$post_data['cstatus'];
				
				$data=array('tender_no'=>$tender_no,'tender_date'=>$tender_date,'tender_id'=>$tender_id,
				'category'=>$valor,'component'=>$component_no,'price'=>$price,'quantity'=>$quantity,
				'amount'=>$amount,'product'=>$product,'tender_due_date'=>$tender_due_date,
				'sort_order'=>$sorder,'pieces'=>$pieces,'location'=>$location,'cstatus'=>$cstatus,'path'=>$path);
				$in=$this->db->insert('quotation',$data);
				}
/*		
		$sql = "SELECT * FROM quotation where IsActive=0 and tender_id='$tender_id' order by sort_order asc";
	//	echo $sql;
        $record = $this->db->query($sql);
        $quotation_info=$record->result_array();
	//	$product_id=$quotation_info[0]['product'];
		
		$product_id=$quotation_info[0]['product'];
 $sql = "SELECT product,tax FROM product where IsActive=0 and id='$product_id'";
        $record = $this->db->query($sql);
        $product=$record->row('product');
        $tax=$record->row('tax');
		
		$tender_no=$quotation_info[0]['tender_no'];
		$tender_date1=$quotation_info[0]['tender_date'];
		$tender_date=date("d-m-Y", strtotime($tender_date1));
		$tender_due_date1=$quotation_info[0]['tender_due_date'];
		$tender_due_date=date("d-m-Y", strtotime($tender_due_date1));

//echo $product_id;
$invoice= '';
?>
<link  href="<?php echo site_url();?>assets/css/bootstrap1.css" rel="stylesheet">
 <link  href="<?php echo site_url();?>assets/css/admin.css" rel="stylesheet">
        <link rel='stylesheet' type='text/css' href="<?php echo site_url();?>assets/css/invoice-style.css">
<?php
foreach($quotation_info as $r)
{
$invoice.="

<tr style='font-size:13px;'>
    <td style='border-top:1px solid #111;' scope='row'>".$r['category']."</td>
    <td style='border-top:1px solid #111;' scope='row'>".$r['component']."</td>
	<td style='border-top:1px solid #111;'>".$r['price']."</td>
    <td style='border-top:1px solid #111;'>".$r['quantity']."</td>
    <td style='border-top:1px solid #111;'>".$r['amount']."</td>
</tr> ";
	}
	
	
$tender_id=$quotation_info[0]['tender_id'];
 $sql1 = "SELECT sum(amount) as amount FROM quotation where IsActive=0 and tender_id='$tender_id'";
        $record1 = $this->db->query($sql1);
        $amount=$record1->row('amount');
		
		$tamount=(($amount*$tax)/100);
		$tamount1=$tamount+$amount;
		
		$from ='Tender'; 
	//	$to=$Email_id;
		$to='yadkiinfotech@gmail.com';//$email;
		$subject = "Tender";

			$invoice="
			<html>
		<head>
		<title>Tender</title>
		</head>
		<body>
 
    <nav class='navbar navbar-default'>
  <div class='container-fluid' style='width:500px; margin:0 auto;'>
    <div class='navbar-header'>    


<style>
.table thead > tr > th, .table tbody > tr > th, .table tfoot > tr > th, .table thead > tr > td, .table tbody > tr > td, .table tfoot > tr > td {
    padding: 3px;
    line-height: 1.428571429;
    vertical-align: top;
    border-top: 1px solid #2c2c2c;
}
#main td, th {
    font-size: 13px;
    border: 1px solid #2c2c2c;
    padding: 3px 8px;
}

.css-serial {
  counter-reset: serial-number;  
}

.css-serial td:first-child:before {
  counter-increment: serial-number;  
  content: counter(serial-number); 
}


</style>
    </div>
  </div>

</nav><div class='container' style='width:100%;'>
        <body>
<style>
body {
    font-family: Arial;
    color: #111;
}
</style>
		
		<table style='width:100%'>
		<tr>
			<td colspan='10' style='border-bottom: 3px solid #eee;width:100%'>
				<h1><b>Tender</b></h1>
			</td>
		</tr>
		

		<tr>
		<td colspan='5' style='width:50%;border-bottom:1px solid #111'>
		
            
               <b>Tender no.: </b>$tender_no<br />
			
			   <b>Product :</b>&nbsp;$product

		</td>
		<td colspan='5' style='width:50%;border-bottom:1px solid #111'>
		
            
			   <b>Tender Date :</b>&nbsp;$tender_date<br />
			   <b>Tender Due Date : </b>&nbsp;$tender_due_date<br />

		</td>
		</tr>
		<table style='width:100%'>
			<tr>
    <th style='width:20%;border:none;text-align:left'>&nbsp;</th>
    <th style='width:20%;border:none;text-align:left'>Descr.</th>
     <th style='width:20%;border:none;text-align:left'>Unit Price</th>
    <th style='width:20%;border:none;text-align:left'>Quantity</th>
    <th style='width:20%;border:none;text-align:left'>Price</th>
    
</tr>
$invoice
 <tr style='font-size:13px;'>
    <td style='border-top:1px solid #111;' scope='row'>&nbsp;</td>
    <td style='border-top:1px solid #111;' scope='row'>&nbsp;</td>
	<td style='border-top:1px solid #111;'>&nbsp;</td>
    <td style='border-top:1px solid #111;'><b>TOTAL</b></td>
    <td style='border-top:1px solid #111;'><b>$amount</b></td>
	
  </tr>   
  
  <tr style='font-size:13px;'>
    <td style='border-top:1px solid #111;' scope='row'>&nbsp;</td>
    <td style='border-top:1px solid #111;' scope='row'>&nbsp;</td>
	<td style='border-top:1px solid #111;'>&nbsp;</td>
    <td style='border-top:1px solid #111;'>TAX $tax%</td>
    <td style='border-top:1px solid #111;'>$tamount</td>
	
  </tr> 
  
  <tr style='font-size:13px;'>
    <td style='border-top:1px solid #111;' scope='row'>&nbsp;</td>
    <td style='border-top:1px solid #111;' scope='row'>&nbsp;</td>
	<td style='border-top:1px solid #111;'>&nbsp;</td>
    <td style='border-top:1px solid #111;'><b>GT</b></td>
    <td style='border-top:1px solid #111;'><b>$tamount1</b></td>
	
  </tr> 

</table>
<style>
body {
	margin:0;
	background-color:#f2f2f2;	
}

h1 {
	font-size:28px;	
}
</style>
	<style>
	        .floatedTable {
				float:left;
        }
        .inlineTable {
            display: inline-block;
        }
	</style>

	  
</head>
<body>
</body>

 
 ";
//echo $invoice;
			/* 
			require_once "./pdfmaster/dompdf_config.inc.php";
    $dompdf1 = new DOMPDF();
    $invoice = preg_replace('/>\s+</', "><", $invoice);
    $dompdf1->load_html($invoice);
    $dompdf1->render();
    $file_to_save1 = "./pdfmaster/tender.pdf";
    //save the pdf file on the server
    file_put_contents($file_to_save1, $dompdf1->output());
		
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= "From: $from\n";
 */
//mail($to,$subject,$Message,$headers);	
//mail($to,$subject,$invoice,$headers);
		return TRUE;
				
	}	



	public function delete_quotation($id)
	{	
	
				$data=array('IsActive'=>'1');
				$this->db->where('id',$id);
				 return $this->db->update('quotation',$data);
			
				
	}	
	
		
	public function get_tender_info($tender_id)
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM quotation where IsActive=0 and tender_id='$tender_id'";
        $record = $this->db->query($sql);
        return $record->result_array();
	}	

	public function update_quotation($post_data)
	{	
				
			$count2 = count($post_data['category']);
			//echo $count2;
				$query_values2 = array();

				for($i=0; $i<$count2; $i++)
				{
				//	echo $i;
				$db = get_instance()->db->conn_id;
$pieces=$post_data['pieces'];
				$location=$post_data['location'];
				$tender_no=$post_data['tender_no'];
				$tender_date1=$post_data['tender_date'];
				$tender_date=date("Y-m-d", strtotime($tender_date1));
				$tender_id=$post_data['tender_id'];
				$sort_order=mysqli_real_escape_string($db,$post_data['sort_order'][$i]);
				$category=mysqli_real_escape_string($db,$post_data['category'][$i]);
				$component=mysqli_real_escape_string($db,$post_data['component'][$i]);
				$price=mysqli_real_escape_string($db,$post_data['price'][$i]);;
				$quantity=mysqli_real_escape_string($db,$post_data['quantity'][$i]);
				$amount=mysqli_real_escape_string($db,$post_data['amount'][$i]);
				$product=$post_data['product'];
				$tender_due_date1=$post_data['tender_due_date'];
				$tender_due_date=date("Y-m-d", strtotime($tender_due_date1));
			
				$valor=mysqli_real_escape_string($db,$post_data['valor'][$i]);
				//echo $valor;
				$sorder=mysqli_real_escape_string($db,$post_data['sorder'][$i]);
				$component_no=mysqli_real_escape_string($db,$post_data['component_no'][$i]);
				//echo $component_no;
				$cstatus=$post_data['cstatus'];
				$data=array('tender_no'=>$tender_no,'tender_date'=>$tender_date,'category'=>$valor,'component'=>$component_no,
				'price'=>$price,'quantity'=>$quantity,'amount'=>$amount,'product'=>$product,
				'tender_due_date'=>$tender_due_date,'sort_order'=>$sorder,'pieces'=>$pieces,'location'=>$location,
				'cstatus'=>$cstatus);
				$this->db->where('tender_id',$tender_id);
				$this->db->where('category',$valor);
				// return $this->db->update('quotation',$data);
				$in=$this->db->update('quotation',$data);
				}
		//	exit;
				return TRUE;
				
	}	
public function save_ask_price($post_data)
	{
				$ask_price=$post_data['ask_price'];
				$tender_id=$post_data['tender_id'];
				$data=array('ask_price'=>$ask_price);
				$this->db->where('tender_id',$tender_id);
				 return $this->db->update('quotation',$data);
			
				
	}
	
		public function save_status($post_data)
	{
				$status=$post_data['status'];
				$loss_price=$post_data['loss_price'];
				$brand=$post_data['brand'];
				$tender_id=$post_data['tender_id'];
				
				$data=array('status'=>$status,'loss_price'=>$loss_price,'brand'=>$brand);
				$this->db->where('tender_id',$tender_id);
				 return $this->db->update('quotation',$data);
			
				
	}	
}?>