<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Purchase_order extends CI_Controller {

	public function purchase_order_all()
	{
		check_login();
		
	

		if($this->input->post()){
	     	
			     	$id = $this->input->post('id');
			     	//$sale_pro_status = $this->input->post('sale_pro_status');
			     	//$this->Purchase_Orders->update_status($sale_pro_status);
			     	$so_id = $this->input->post('so_id');

			     	$check_so = $this->Purchase_Orders->check_so($so_id);
			     	
			     	if($check_so != 1){


			     		
			     	$get_materials_boms =$this->Sales_Orders->get_materials_boms($id);
			     	
			     	$all_data=$this->Sales_Orders->get_destroy_bom_po($so_id);
			     	$qtys = $all_data[0]['quantity'];

			     	$main = $all_data[0]['main_product']; 
                    $main_qty = $all_data[0]['quantity'];
                    
                   
                     $created = date('Y-m-d H:i:s');
                   
                    $so_po_boms = array(

                    'so_id' => $so_id,
                    'main_product' => $main,
                    'quantity' => $main_qty,
                     'created' => $created        
  						 );
                    $so_po_bom = $this->Purchase_Orders->add_bom_salesorder_details($so_po_boms);


			     	if(!empty($get_materials_boms))
                    {
			     	foreach ($get_materials_boms as  $values) {
			     		$aa = $values['material_id'];
			     		$material_code =  material_codess($aa);
			     		//print_r($aa);


			     		  $weight =  $values['weight'];

			     		  //$condition = $weight * $qty;
			     		  $condition = $weight * $qtys;

                    if($condition >= 1000)
                    {
                    	if($values['unit']== 10){
                            $total = $condition/1000;
                           $res = $total/1000;
                           //echo $total; 

                            
                                      $material = $values['material_id'];
                                      $units =  6;
                                      $t_qty =  $res;
                                      $data = array(
											'so_po_id'=>$so_po_bom,
											'material_id' => $material,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );  
                                     
                                   $this->Purchase_Orders->add_pur_details($data);
                           			 }else if($values['unit']== 4){

                           			 	/*if($aa != 8 && $aa != 10){*/

                           			 		
                           			 			
                           			 		  $total = $condition/1000/25;
		                                      $main = $all_data[0]['main_product']; 
		                                      $main_qty = $all_data[0]['quantity'];
		                                      $material = $values['material_id'];
		                                      $units =  1;
		                                      $t_qty =  $total;

		                                      $data = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => $material,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );  
                                     
                                		   $this->Purchase_Orders->add_pur_details($data);

                           			 
                                        
                                }else{

                                	
                                       
	                                   $units = 6;

	                                   $main = $all_data[0]['main_product']; 
                                      $main_qty = $all_data[0]['quantity'];
                                      $material = $values['material_id'];
                                      //$units =  1;
                                      $t_qty =  $condition/1000;
                                      $data = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => $material,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );  
                                     
                                   $this->Purchase_Orders->add_pur_details($data);

                                       }
	     		
						     	}else if($condition < 1000)
                    {
                    	if($values['unit']== 10){
                            $total = $condition/1000;
                            //$res = $total/1000;
                           //echo $total; 

                            
                                      $material = $values['material_id'];
                                      $units =  4;
                                      $t_qty =  $total;
                                      $data = array(
											'so_po_id'=>$so_po_bom,
											'material_id' => $material,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );  
                                     
                                   $this->Purchase_Orders->add_pur_details($data);
                           			 }else if($values['unit']== 4){

                           			 	/*if($aa != 8 && $aa != 10){*/

                           			 		
                           			 			
                           			 		  $total = $condition/1000;
		                                      $main = $all_data[0]['main_product']; 
		                                      $main_qty = $all_data[0]['quantity'];
		                                      $material = $values['material_id'];
		                                      $units =  6;
		                                      $t_qty =  $total;

		                                      $data = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => $material,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );  
                                     
                                		   $this->Purchase_Orders->add_pur_details($data);

                           			 
                                        
                                }else{

                                	
                                       
	                                   $units = 6;

	                                   $main = $all_data[0]['main_product']; 
                                      $main_qty = $all_data[0]['quantity'];
                                      $material = $values['material_id'];
                                      //$units =  1;
                                      $t_qty =  $condition/1000;
                                      $data = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => $material,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );  
                                     
                                   $this->Purchase_Orders->add_pur_details($data);

                                       }
	     		
						     	}
						     	
						     }
						 }


						     	$get_materials_packing_boms=$this->Sales_Orders->get_materials_packing_boms($id);	

						     	/*print_r($get_materials_packing_boms);
						     	die;*/

								 //$condition =  $get_materials_packing_boms['weight']*$qty;
						     	if(!empty($get_materials_packing_boms)){
								 foreach ($get_materials_packing_boms as $values) {

								 		$weight =  $values['weight'];
								 		  $names = material_name($values['material_id']);
						     		  //$condition = $weight * $qty;
						     		  $condition = $weight * $qtys;

					            if($condition >= 1000)
					            {
					                if($values['unit']== 10){
					                $total = $condition/1000;
					                $t_qunatity = $total/1000;
					                $main = $all_data[0]['main_product']; 
						              $main_qty = $all_data[0]['quantity'];
						              $material = $values['material_id'];
						              $units =  6;
						              $t_qty =  $t_qunatity;
						              $datas = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => $material,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );  
					                                     
					                $this->Purchase_Orders->add_pur_details($datas);
					               //echo "$res Kg";
					                }else if($values['unit']== 4){
					                     $total = $condition/1000;
					                      $main = $all_data[0]['main_product']; 
					                      $main_qty = $all_data[0]['quantity'];
					                      $material = $values['material_id'];
					                      $units =  6;
					                      $t_qty =  $total;
					                      $data = array(
												'so_po_id'=>$so_po_bom,												
												'material_id' => $material,
										        'unit' => $units,
										        'total' => $t_qty			        
										        );  
					                     
					                   $this->Purchase_Orders->add_pur_details($data);
					                }
					           }else{
					             $condition; 
					             if($names == "POLY BAG 24'' X 36''")
					             {
					             	$units = 10;
					            
					             

					             //$total = $condition/1000;
					                      $main = $all_data[0]['main_product']; 
					                      $main_qty = $all_data[0]['quantity'];
					                      $material = $values['material_id'];
					                      //$units =  6;
					                      $t_qty =  $condition;
					                      $data = array(
												'so_po_id'=>$so_po_bom,
												
												'material_id' => $material,
										        'unit' => $units,
										        'total' => $t_qty			        
										        );  
					                     
					                   $this->Purchase_Orders->add_pur_details($data);
					                    }else{
					             		$units = 8;	

					             		 $main = $all_data[0]['main_product']; 
					                      $main_qty = $all_data[0]['quantity'];
					                      $material = $values['material_id'];
					                      //$units =  6;
					                      $t_qty =  $condition;
					                      $data = array(
												'so_po_id'=>$so_po_bom,
												
												'material_id' => $material,
										        'unit' => $units,
										        'total' => $t_qty			        
										        );  
					                     
					                   $this->Purchase_Orders->add_pur_details($data);
					             }
					           }
					       }

					   }


					   $get_moldedchild_boms=$this->Sales_Orders->get_moldedchild_boms($id);
					   if(!empty($get_moldedchild_boms)){

					   	 foreach ($get_moldedchild_boms as $values) {

					   	 	$product_id = $values['child_product'];
					   	 	$units = $values['unit'];
					   	 	//$t_qty = $values['weight'];
 
					   	 	  $t_qty = $values['weight']*$qtys;

					      			  $data = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => 0,
											'child_product' => $product_id,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );   
					      			 	 $this->Purchase_Orders->add_pur_details($data);
					      			 	}

					      			 	}


					     $get_printedchild_boms=$this->Sales_Orders->get_printed_boms($id);
					   if(!empty($get_printedchild_boms)){

					   	 foreach ($get_printedchild_boms as $values) {

					   	 	$product_id = $values['product_id'];
					   	 	$units = $values['unit'];
					   	 	//$t_qty = $values['weight'];

					   	 	  $t_qty = $values['weight']*$qtys;

					      			  $data = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => 0,
											'child_product' => $product_id,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );   
					      			 	 $this->Purchase_Orders->add_pur_details($data);
					      			 	}

					      			 	}

					     $get_assembled_boms=$this->Sales_Orders->get_assembled_boms($id);
					   if(!empty($get_assembled_boms)){

					   	 foreach ($get_assembled_boms as $values) {

					   	 	$product_id = $values['product_id'];
					   	 	$units = $values['unit'];
					   	 	//$t_qty = $values['weight'];

					   	 	  $t_qty = $values['weight']*$qtys;

					      			  $data = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => 0,
											'child_product' => $product_id,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );   
					      			 	 $this->Purchase_Orders->add_pur_details($data);
					      			 	}

					      			 	}


					     $get_sealed_boms=$this->Sales_Orders->get_sealed_boms($id);

					   if(!empty($get_sealed_boms)){

					   	 foreach ($get_sealed_boms as $values) {

					   	 	$product_id = $values['product_id'];
					   	 	$units = $values['unit'];
					   	 	//$t_qty = $values['weight'];

					   	 	  $t_qty = $values['weight']*$qtys;

					      			  $data = array(
											'so_po_id'=>$so_po_bom,
											
											'material_id' => 0,
											'child_product' => $product_id,
									        'unit' => $units,
									        'total' => $t_qty			        
									        );   
					      			 	 $this->Purchase_Orders->add_pur_details($data);
					      			 	}

					      			 	}
						      
					      			 $validate = "False";

							     	   echo json_encode($validate);
					
							}else if($check_so == 1)
							     	{
							     		
							       		$validate = "True";

							     	   echo json_encode($validate);
							     	}
							     		
	        
							     	
							     	
		}else{
		      		
		      		//$data['parties']=$this->Sales_Orders->get_party();
		      		$data['vendors']=$this->Purchase_Orders->get_vendors();
		      		$data['get_po']=$this->Purchase_Orders->get_all_po();
					$this->load->view('purchase/purchase_order',$data);
		      }
	}


	public function get_code(){
	
				if($this->input->post())
					{
						 $param = $this->input->post('id');
			 			 $data = $this->Purchase_Orders->get_code($param);
						
							
						echo json_encode($data);
					}

				}

		public function	add_purchase_order(){

			if($this->input->post()){

						$vendor_id = $this->input->post('vendor_id');
						$po_no = $this->input->post('po_no');
						
						$po_date = $this->input->post('po_date');
						$gst = $this->input->post('gst');
						$quot_date = $this->input->post('quot_date');
						$quot_ref = $this->input->post('quot_ref');
						$inst = $this->input->post('inst');
						$created = $this->input->post('created');
						$modified = $this->input->post('modified');


						$material_id = $this->input->post('material_id');
						$product_id = $this->input->post('product_id');
						$order_qty = $this->input->post('order_qty');
						$unit = $this->input->post('unit');
						$rate = $this->input->post('rate');
						//$total = $this->input->post('total');
						$total= $order_qty*$rate;
						$modified = $this->input->post('modified');
						$created = $this->input->post('created');


						 $save_po = array(

					                    'vendor_id' => $vendor_id,
					                    'po_no' => $po_no,
					                     'po_date' => $po_date,
					                     'gst' => $gst,
					                     'quot_date' => $quot_date,
					                     'quot_ref' => $quot_ref,
					                     'inst' => $inst, 
					                      'modified'  => $modified,
					                      'created'  => $created
					  						 );
						

						 $result_id = $this->Purchase_Orders->add_purchase($save_po);

	          	 if($product_id == NULL){

	          	 	
	          	 	

	          	 	 $all_data = array(

					                    'material_id' => $material_id,
					                    
					                    'order_qty' => $order_qty,
					                     'unit' => $unit,
					                     'rate' => $rate,
					                     'total' => $total,
					                     'modified' => $modified,
					                     'created' => $created, 
					                      'po_id'  => $result_id
					  						 );
	          	 	 $result = $this->Purchase_Orders->details_add_materials($all_data);

	          	  
	          	   if($result){$add_purchase=array('status'=>'true');}else{$add_purchase=array('status'=>'false');}
	          		echo json_encode($add_purchase);

	          	 }else{

	          	 	 //$result_id = $this->Purchase_Orders->add_purchase($_POST);

	          	 	 $all_data1 = array(

					                    
					                    'product_id' => $product_id,
					                    'order_qty' => $order_qty,
					                     'unit' => $unit,
					                     'rate' => $rate,
					                     'total' => $total,
					                     'modified' => $modified,
					                     'created' => $created, 
					                      'po_id'  => $result_id
					  						 );
	          		
	          	    $result = $this->Purchase_Orders->details_add_products($all_data1);

	          	     if($result){$add_purchase=array('status'=>'true');}else{$add_purchase=array('status'=>'false');}
	          			  echo json_encode($add_purchase);
	      				}

	          	 }


	       

			}

			
			public function details_add_materials()
				{
					check_login();

					if($this->input->post()){

						
						$created = $this->input->post('created');
						$modified = $this->input->post('modified');
						
						$weight = $this->input->post('weight');
						$order_qty = $this->input->post('order_qty');
						$unit = $this->input->post('unit');
						$rate = $this->input->post('rate');
						$id = $this->input->post('po_id');
						$material_id = $this->input->post('material_id');
						$total= $order_qty*$rate;
						$match_po=$this->Purchase_Orders->get_match_po($id);
						if($match_po == NULL){
			      	    $result = $this->Purchase_Orders->update_po_materials_status($id);
							 
							  $all_data = array(

					                    'material_id' => $material_id,
					                    'product_id' => 0,
					                    'weight' => $weight,
					                     'unit' => $unit,
					                     'rate' => $rate,
					                     'total' => $total,
					                     'order_qty' => $order_qty,
					                     'created' => $created,
					                     'modified' => $modified, 
					                     'po_id' => $id	     
					  						 );

						  $result = $this->Purchase_Orders->details_add_materials($all_data);

						 

						$condition = $this->Purchase_Orders->total_purchase_material($material_id);
						$condition1 = $this->Purchase_Orders->puchase_order_details_total($material_id);

						
						
						if($condition1[0]['alltotal'] == $condition[0]['order_qty']){
						 $result = $this->Purchase_Orders->update_status_so_p_o_d($material_id);
						
				        if($result){$add_details_add=array('status'=>'true');}else{$add_details_add=array('status'=>'false');}
				            echo json_encode($add_details_add);
				        }else{

				        	if($result){$add_details_add=array('status'=>'true');}else{$add_details_add=array('status'=>'false');}
				            echo json_encode($add_details_add);
				        }
				        }else{
				        	//$message = "One Entry Only...";
				        	if($match_po){$message_show=array('status'=>'error');}else{$message_show=array('status'=>'notworking');}
				        	echo json_encode($message_show);
				        }
				      }
					
				}

			public function details_add_products()
				{
					check_login();				
					if($this->input->post()){
					$id = $this->input->post('po_id');
					$product_id = $this->input->post('product_id');
					$match_po=$this->Purchase_Orders->get_match_po($id);
						if($match_po == NULL){
			         $result = $this->Purchase_Orders->update_po_products_status($id);
					 $result = $this->Purchase_Orders->details_add_products($_POST);
					  $result = $this->Purchase_Orders->update_status_pro_so_p_o_d($product_id);
				        if($result){$add_details_add=array('status'=>'true');}else{$add_details_add=array('status'=>'false');}
				            echo json_encode($add_details_add);
				             }else{
				        	//$message = "One Entry Only...";
				        	if($match_po){$message_show=array('status'=>'error');}else{$message_show=array('status'=>'notworking');}
				        	echo json_encode($message_show);
				        }
				      }
					
				}

				

			public function view_design_pro_mat_packing(){

			check_login();
			if($this->input->post())
			{
					
					
					
					$data['id'] = $this->input->post('id');
					$data['po_id'] = $this->input->post('po_id');
					$data['get_units']=$this->Master_list->get_units();
					//$data['get_materials_requsition']=$this->Purchase_Orders->get_materials_requsition();
					//$data['get_products']=$this->Purchase_Orders->get_products_requsition();
					$data['get_units']=$this->Purchase_Orders->get_units();
					$data['get_materials']=$this->Purchase_Orders->get_materials();
					$data['get_products']=$this->Purchase_Orders->get_products();
					echo json_encode (array('html'=>$this->load->view('purchase/get_material_design_packing',$data,true)));

				
				}	
			}


			/*public function get_materils_details(){

				check_login();
				if($this->input->post())
				{
				$material = $this->input->post('material');

				$data1 = $this->Purchase_Orders->get_materils_details($material);
				$data2=$this->Purchase_Orders->remaining_requisitions($material);
				
				if($data1 !="" && $data2 !=""){
				$total = $data1[0]['total']-$data2[0]['order_qty'];
				}else{

					$total = $data1[0]['total'];
				}
				
			
				$data['codes'] = material_codess($data1[0]['material_id']);
				$data['grade'] = material_garde($data1[0]['material_id']);
				$data['total'] = $total;
				$data['unit'] = get_unit_name($data1[0]['unit']);
				$data['units_value'] = $data1[0]['unit'];
				
				echo json_encode ($data);

				
				}
			}*/

			public function get_materils_details(){

				check_login();
				if($this->input->post())
				{
				$material = $this->input->post('material');

				$data1 = $this->Purchase_Orders->get_materils_details($material);
				
			
				$data['codes'] = $data1[0]['material_code'];
				
				
				echo json_encode ($data);

				
				}
			}


			public function get_product_details(){

				check_login();
				if($this->input->post())
				{
				$product_id = $this->input->post('product_id');
				//print_r($product_id);
				//die;
				//$data1 = $this->Purchase_Orders->get_product_details($product_id);
				$data1 = $this->Purchase_Orders->get_product_details($product_id);
				//$data['codes'] = product_code($data1[0]['child_product']);

				$data['codes'] = $data1[0]['pro_code'];
				
				//$data['grade'] = product_remark($data1[0]['child_product']);
				//$data['total'] = $data1[0]['total'];
				//$data['unit'] = get_unit_name($data1[0]['unit']);
				//$data['units_value'] = $data1[0]['unit'];
				
				echo json_encode ($data);

				
				}
			}


			

			

	public function view_purchase_order_items()
	{
		check_login();
		$id = $this->uri->segment('3');
		if($this->input->post())
		{
			 $param = $this->input->post('id');
 			$data = $this->Master_list->get_products_data($param);
			
			//print_r($data);
			echo json_encode($data);
		}else{

			$data['get_units']=$this->Master_list->get_units();
			$data['get_materials_data']=$this->Purchase_Orders->get_materials_data($id);
			$data['get_data_products']=$this->Purchase_Orders->get_data_products($id);
			
			//$data['get_products']=$this->Master_list->get_products();

			$this->load->view('purchase/view_purchase_order_items',$data);
		}
		
	}
	

	public function delete_data_pro($id)
	{
		 check_login();

			$pro_id = pro_id($id);
			$result=$this->Purchase_Orders->update_req_status_pro($pro_id);
			$result=$this->Purchase_Orders->delete_data_pro($id);
	
			if($result){$pro=array('status'=>'true');}else{$pro=array('status'=>'false');}
			echo json_encode($pro);
		
				
	}


	public function delete_data_mat($id)
	{
		 check_login();

			
			 //$id =  $this->input->post('id');

				 $material_id = mat_id($id);
				 
			$result=$this->Purchase_Orders->delete_data_mat($id);
			$result=$this->Purchase_Orders->update_req_status($material_id);
			if($result){$material=array('status'=>'true');}else{$material=array('status'=>'false');}
			echo json_encode($material);
		
				
	}

	public function update_data_mat($po_id)
	{
		 check_login();

		 	

			
			$result=$this->Purchase_Orders->update_data_mat_status($po_id);
			
			if($result){$material=array('status'=>'true');}else{$material=array('status'=>'false');}
			echo json_encode($material);
		
				
	}

	
	public function update_data_pro($po_id)
	{
		 check_login();

		 	

			
			$result=$this->Purchase_Orders->update_data_pro_status($po_id);
			
			if($result){$material=array('status'=>'true');}else{$material=array('status'=>'false');}
			echo json_encode($material);
		
				
	}


	

	public function view_invoice_details(){

		check_login();
		
		$id = $this->uri->segment('3');

		
		$data['get_invoice_data']=$this->Purchase_Orders->get_invoice_data($id);

		$this->load->view('purchase/view_po_invoice',$data);
		//echo json_encode (array('html'=>$this->load->view('/purchase/view_po_invoice',$data,true)));
	
	}
	
	
public function email_to_vendor($ids){
		

    $po_id = $ids;
				 
		$vendor_id=$this->Purchase_Orders->po_id_vendor($po_id);
		$get_invoice_data=$this->Purchase_Orders->get_invoice_data($po_id);
		$email = email_vendor($vendor_id[0]['vendor_id']);	
		$po_number = po_number($po_id);
			$to = $email;
			$subject = "Rizwan Associates P.O#".$po_number;

			$date = $get_invoice_data[0]['po_date'];
			$po_date = date('d-m-Y', strtotime($date));
			$vendorname = vendor($get_invoice_data[0]['vendor_id']);
			$attn = attn($get_invoice_data[0]['vendor_id']);
			$addres_vendor = addres_vendor($get_invoice_data[0]['vendor_id']);
			$email_vendor = email_vendor($get_invoice_data[0]['vendor_id']);
			$phone_vendor = phone_vendor($get_invoice_data[0]['vendor_id']);
			

			$htmlContent = '
		
				<html lang="en">
				

			<body>


			<div class="row">
                    <div class="col-lg-12">

                        <div class="card mt-3">
                            
                <div class="card-header"><img src="http://rizwanassociates.com/new_portal/assets/logo.png" style="width:105px;"/><br/>
                                <span class="float-right">
                                    <strong>P.O #:</strong>'."$po_number".'<br/>
                                   <strong>Date: </strong>'."$po_date".'</span>
                                  
                                </div>
                            <div class="card-body">
                                <div class="row mb-5">
                                    <div class="mt-4 col-xl-6 col-lg-6 col-md-6 col-sm-12">
                                        <h6>From:</h6>
                                        <div> <strong>Rizwan Associates</strong> </div>
                                        <div>201-20C,</div>
                                        <div>Ittehad Comercial Lane 8, DHA Karachi, Pakistan</div>
                                        <div>Email: info@rizwanassociates.com</div>
                                        <div>Phone: +92 21 32551101</div>
                                    </div>

                                    <div class="mt-4 col-xl-6 text-right col-lg-6 col-md-6 col-sm-12">
                                        <h6>To:</h6>
                                        <div> <strong>'."$vendorname".' </strong> </div>
                                        <div>'."$attn".'</div>
                                        <div>'."$addres_vendor".'</div>
                                        <div>Email:'."$email_vendor".' </div>
                                        <div>Phone: +'."$phone_vendor".'</div>
                                    </div>
                                </div>
                                   <div class="table-responsive">
                                    <table class="table table-striped" border=1px>
                                        <thead>
                                            <tr>
                                                <th class="center">Code</th>
                                                <th>Description</th>
                                                <th class="right">Quantity</th>
                                                <th class="center">Unit</th>
                                                <th class="right">Price</th>
                                                <th class="right">Total(GST)</th>
                                            </tr>
                                        </thead>
                                        <tbody>';
                                            
                                            $sum =0;
                                            if(!empty($get_invoice_data)) {?>
                                                <?php foreach ($get_invoice_data as  $value) {
                                                    if($value['material_id'] !=0){
                                                  $mat_code = material_codess($value['material_id']);
                                                  $mat_name = material_name($value['material_id']);
                                                  $qty = $value['order_qty']; 
                                                  $unit = get_unit_name($value['unit']);
                                                  $rate = $value['rate'];
                                                  $total =  $value['order_qty']*$value['rate'];

                                                  
                                           		$htmlContent .= '<tr>
                                                <td class="center">'."$mat_code".'</td>
                                                <td class="left strong">'."$mat_name".'</td>
                                                <td class="left">'."$qty".'</td>
                                                <td class="right">'."$unit".'</td>
                                                
                                                <td class="right">'."$rate".'</td>
                                                <td class="right">
                                                    
                                                   '."$total".'
                                                   
                                                </td>
                                            </tr>';
                                             }else{ 

                                             	$pro_code = product_code($value['product_id']);
                                                  $pro_name = product_name($value['product_id']);
                                                  $pro_weight =  $value['weight'];
                                                  $unit_pro = get_unit_name($value['unit']);
                                                  $rate_pro = $value['rate'];
                                                  $totalpro =  $value['order_qty']*$value['rate'];
                                                  $tot1 = $sum+= $totalpro;
                                                  $tot2 = $sum+= $totalpro;

												$htmlContent .=' <tr>
                                                <td class="center">'."$pro_code".'</td>
                                                <td class="left strong">'."$pro_name".'</td>
                                                <td class="left">'."$pro_weight".'</td>
                                                <td class="right">'."$unit_pro".'</td>
                                                
                                                <td class="right">'."$rate_pro".'</td>
                                                <td class="right">
                                                    
                                                      '."$totalpro".' 
                                                    
                                                </td>
                                            </tr>';


                                            } } }
                                            
                                        $htmlContent .= '</tbody>
                                    </table>
                                </div>
                    </div>
                </div>
                 </div>
                  </div>
              </body>

			</html> ';

		

			// Set content-type header for sending HTML email
			$headers = "MIME-Version: 1.0" . "\r\n";
			$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

			// Additional headers
			$headers .= 'From: info@rizwanassociates.com' . "\r\n";
			//$headers .= 'Cc: welcome@example.com' . "\r\n";
			//$headers .= 'Bcc: welcome2@example.com' . "\r\n";

			// Send email
			if(mail($to,$subject,$htmlContent,$headers)):
			   $this->load->view('purchase_order/purchase_order_all');
			else:
			    $errorMsg = 'Email sending fail.';
			endif;
			        
			    }


		
	public function approved_po($id)
	{
		 check_login();

			$id = $id;
			$result=$this->Purchase_Orders->approved_po($id);
			//$result=$this->Purchase_Orders->delete_data_pro($id);
	
			if($result){$pro=array('status'=>'true');}else{$pro=array('status'=>'false');}
			echo json_encode($pro);
		
				
	}

	public function purchase_order_delete_data($id){

		 check_login();
		    $result = $this->Purchase_Orders->delete_data_purchase_order($id);

			$result = $this->Purchase_Orders->delete_data_purchase_order_data($id);
			
			
			if($result){$pro=array('status'=>'true');}else{$pro=array('status'=>'false');}
			echo json_encode($pro);
		}



		public function edit_po_order(){


		 check_login();

	     $id = $this->uri->segment('3');

	     
		if($this->input->post())
		{	

			$vendor_id = $this->input->post('vendor_id');
			$po_no = $this->input->post('po_no');
			$po_date = $this->input->post('po_date');
			$quot_date = $this->input->post('quot_date');
			$quot_ref = $this->input->post('quot_ref');
			$gst = $this->input->post('gst');
			$inst = $this->input->post('inst');
			$created =  $this->input->post('created');
			$modified =  $this->input->post('modified');
			
			$save_po = array(

		                    'vendor_id' => $vendor_id,
		                    'po_no' => $po_no,
		                     'po_date' => $po_date,
		                     'gst' => $gst,
		                     'quot_date' => $quot_date,
		                     'quot_ref' => $quot_ref,
		                     'inst' => $inst,
		                     'created'=> $created,
		                     'modified' => $modified
		                     
		  						 );
	     	
	           $result1=$this->Purchase_Orders->update_po($id,$save_po);

	         
	           		$material_id = $this->input->post('material_id');
		            $product_id = $this->input->post('product_id');
					$order_qty = $this->input->post('order_qty');
					$unit = $this->input->post('unit');
					$rate = $this->input->post('rate');
					
				    $modified = $this->input->post('modified');

				   if($product_id!=""){

				   
				    $save_po1 = array(
       
			                    'product_id' => $product_id,
			                     'material_id' => 0,
			                     'order_qty' => $order_qty,
			                     'unit' => $unit,
			                     'rate' => $rate,
			                     
			                     'modified' => $modified,
			                   
			                     
			  						 );

				  
					 $result=$this->Purchase_Orders->update_po_products($id,$save_po1);

			
					if($result){$material=array('status'=>'true');}else{$party=array('status'=>'false');}
					echo json_encode($material);
				}else if($material_id !=""){

				
					
			    $save_po1 = array(

		                   
		                    'material_id' => $material_id,
		                    'product_id' => 0,
		                     'order_qty' => $order_qty,
		                     'unit' => $unit,
		                     'rate' => $rate,
		                   	  'modified' => $modified,
		                     
		  						 );

			  
		
				$result=$this->Purchase_Orders->update_po_products($id,$save_po1);

			
			if($result){$material=array('status'=>'true');}else{$party=array('status'=>'false');}
			echo json_encode($material);


		}


		}else{
				
	      		$data['get_edit_po_order'] = $this->Purchase_Orders->get_edit_po_order($id);
	      		$data['vendors']=$this->Purchase_Orders->get_vendors();
		      		
				$this->load->view('purchase/po_edit',$data);
	      }
				
	
	}



	public function logout()
	{
		check_login();
		$this->session->sess_destroy();
		redirect(base_url(),'refresh'); 
	}

	
}
