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

	public function pending_orders()
	{
		check_login();
		if ($this->input->post()) {
			$id = $this->input->post('so_id');
			$product_id = $this->input->post('product_id');
			$insert_date = date('Y-m-d');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			$dc_no = $this->input->post('dc_no');
			$fgqty = $this->input->post('fgqty');
			$gst = $this->input->post('gst');
			$batch = $this->input->post('batch');
			$ref_code = $this->input->post('ref_code');
			$remarks = $this->input->post('remarks');
			$insert_date = date('Y-m-d');
			$set_qty = $this->input->post('set_qty');
			@$set_product_id = $this->input->post('set_product_id');
			@$set_product_qty = $this->input->post('qty_semi');
			@$subject_count = count($set_product_id);
			$val = '';
			if($subject_count == 0){}
			for ($i = 0; $i < $subject_count; $i++) {
				$get_id = $this->Orders->get_product_id($set_product_id[$i]);
				$get_qty = $set_product_qty[$i];
				$get_set_qty = $set_qty[$i];
				// print_r($get_set_qty);
				// die;
				foreach ($get_id as $get_ids) {
					if ($get_set_qty < $get_qty) {
						//$add_dc = array('validate' => 'true');
						$val = 'valtrue';
					} else {

						$val = 'valfalse';
						//$add_dc = array('validate' => 'false');
					}
				}

			}
			if ($val == 'valtrue') {
				// print_r('validate');
				// die;
				$add_dc = array('validate' => 'validate');

			} else if ($val == 'valfalse') {

				// print_r('not validate');
				// die;
				$dataarray1 = array(
					'so_id' => $id,
					'dc_no' => $dc_no,
					'qty' => $fgqty,
					'insert_date' => $insert_date,
					'gst' => $gst,
					'batch' => $batch,
					'ref_code' => $ref_code,
					'remarks' => $remarks,
					'user_id' => $user_id,
					'created' => $created,
					'modified' => $modified
				);
				$result_id = $this->Orders->add_dc($dataarray1);
				$dc_qty = $this->Orders->get_dc_qty($id);
				$so_qty = $this->Sales_Orders->get_invoice_data($id);
				$soqty = $so_qty[0]['quantity'];
				$dcqty = $dc_qty[0]['qty'];
				if ($dcqty >= $soqty) {
					$result = $this->Orders->update_so_status($id);
				}
				@$product_id = $this->input->post('product_id');
				@$set_product_qty = $this->input->post('qty_semi');
				@$subject_count = count($set_product_id);
				$dataarray = array(
					'product_id' => $product_id,
					'type' => 5,
					'dc_id' => $result_id,
					'qty' => $fgqty,
					'insert_date' => $insert_date,
					'user_id' => $user_id,
					'created' => $created,
					'modified' => $modified
				);
				$dataarray11 = array(
					'product_id' => $product_id,
					'type' => 5,
					'qty' => $fgqty,
					'insert_date' => $insert_date,
					'user_id' => $user_id,
					'created' => $created,
					'modified' => $modified
				);
				$result = $this->Stocks->add_fg_log($dataarray11);
				$result = $this->Stocks->add_out_stock($dataarray);
				for ($i = 0; $i < $subject_count; $i++) {
					$get_id = $this->Orders->get_product_id($set_product_id[$i]);
					$get_qty = $set_product_qty[$i];
					foreach ($get_id as $get_ids) {

						$dataarray = array(
							'product_id' => $get_ids['id'],
							'type' => 5,
							'dc_id' => $result_id,
							'qty' => $get_qty,
							'insert_date' => $insert_date,
							'user_id' => $user_id,
							'created' => $created,
							'modified' => $modified
						);
						// $dataarray11 = array(
						// 	'product_id' => $product_id,
						// 	'type' => 5,
						// 	'qty' => $fgqty,
						// 	'insert_date' => $insert_date,
						// 	'user_id' => $user_id,
						// 	'created' => $created,
						// 	'modified' => $modified
						// );
						// $result = $this->Stocks->add_fg_log($dataarray11);
						$result = $this->Stocks->add_out_stock($dataarray);
						if ($result) {
							$add_dc = array('status' => 'true');
						} else {
							$add_dc = array('status' => 'false');
						}
					}
				}

			}else{
				$dataarray1 = array(
					'so_id' => $id,
					'dc_no' => $dc_no,
					'qty' => $fgqty,
					'insert_date' => $insert_date,
					'gst' => $gst,
					'batch' => $batch,
					'ref_code' => $ref_code,
					'remarks' => $remarks,
					'user_id' => $user_id,
					'created' => $created,
					'modified' => $modified
				);
				$result_id = $this->Orders->add_dc($dataarray1);
				$dc_qty = $this->Orders->get_dc_qty($id);
				$so_qty = $this->Sales_Orders->get_invoice_data($id);
				$soqty = $so_qty[0]['quantity'];
				$dcqty = $dc_qty[0]['qty'];
				if ($dcqty >= $soqty) {
					$result = $this->Orders->update_so_status($id);
				}		
					$product_id = $this->input->post('product_id');
						$dataarray = array(
							'product_id' => $product_id,
							'type' => 5,
							'dc_id' => $result_id,
							'qty' => $fgqty,
							'insert_date' => $insert_date,
							'user_id' => $user_id,
							'created' => $created,
							'modified' => $modified
						);
						$dataarray11 = array(
							'product_id' => $product_id,
							'type' => 5,
							'qty' => $fgqty,
							'insert_date' => $insert_date,
							'user_id' => $user_id,
							'created' => $created,
							'modified' => $modified
						);
						$result = $this->Stocks->add_fg_log($dataarray11);
						$result = $this->Stocks->add_out_stock($dataarray);
						if ($result) {
							$add_dc = array('status' => 'true');
						} else {
							$add_dc = array('status' => 'false');
						}
			}
			echo json_encode($add_dc);
		} else {
			//$data['get_po']=$this->Orders->get_all_po();
			$data['get_dc_order'] = $this->Orders->get_dc_order();
			$data['get_all_po1'] = $this->Orders->get_all_po1();
			/*print_r($data['get_all_po1']);
			die;*/
			$data['get_Orders_status'] = $this->Orders->get_Orders_status();
			$this->load->view('orders_qc/orders_pending', $data);
		}
	}





	

	public function reciving_po(){

		check_login();
		
		
		$data['id'] = $this->input->post('id');
		$id = $data['id'];
		$data['get_invoice_data']=$this->Purchase_Orders->get_invoice_data($id);
		$data['get_dc_qty']=$this->Orders->get_dc_qty($id);

	

		

		$data['grn_no']=$this->Orders->get_Orders_code($id);
		$xx = $data['grn_no'][0]['grn_no'];

		for ($grn_no = 0; $grn_no <= $xx; $grn_no++) {
		             
		 }

		 $data['grn_code'] = $grn_no;
		

		 //$data['grn_no'] = $pro_code;
		echo json_encode (array('html'=>$this->load->view('orders_qc/add_recive_po',$data,true)));
	
	
	}


	public function qc_view(){

		check_login();
		
		
		$data['id'] = $this->input->post('id');
		$id = $data['id'];
		$data['get_qc_view']=$this->Orders->get_qc_data($id);

		//$data['get_dc_qty']=$this->Orders->get_dc_qty($id);
		echo json_encode (array('html'=>$this->load->view('orders_qc/qc_view_form',$data,true)));
	
	
	}



	public function add_qc_dc_check_raw_material(){
		check_login();
		if($this->input->post()){
	     		$id = $this->input->post('dc_id');
	     		$consignment = $this->input->post('consignment');

	     		$material_id = $this->input->post('material_id');
            	$qty = $this->input->post('qty');
            	$insert_date = date('Y-m-d');
            	$user_id = $this->input->post('user_id');
				$created = $this->input->post('created');
				$modified = $this->input->post('modified');

				
	     		
	            $result = $this->Orders->add_dc_qc_check_raw_material($_POST);
	            $result = $this->Orders->update_dc_qc_status($id,$consignment);

	            $condition  = $this->Stocks->get_material_for_condition($material_id);

	             if($consignment == '1'){

	             	 if($condition ==""){

	             	 	$save_data =  array('material_id' => $material_id,
										'type' => 1,
										'qty' => $qty,
										
										'insert_date'=>$insert_date,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );

					$save_log =  array('material_id' => $material_id,
										'type' => 1,
										'qty' => $qty,
										
										'insert_date'=>$insert_date,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );

			     	$result = $this->Stocks->add_fg($save_data);
			     	//print_r($save_log);
			     	//die;
			     	$result = $this->Stocks->add_fg_log($save_log);

	             	 }else{

	             	 		$all_qty = $condition[0]['qty']+$qty;

							//print_r($all_qty);
							//die;

						$dataarray =  array('material_id' => $material_id,
											'type' => 1,
											'qty' => $all_qty,
											
											'user_id'=> $user_id,
											'created'=> $created,
											'modified'=> $modified
											 );

						$save_log =  array('material_id' => $material_id,
											'type' =>1,
											'qty' => $qty,
											
											'insert_date'=>$insert_date,
											'user_id'=> $user_id,
											'created'=> $created,
											'modified'=> $modified
											 );
					 	
						$result = $this->Stocks->update_stock_material($material_id,$dataarray);
						$result = $this->Stocks->add_fg_log($save_log);


	             	 }

	             }

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

	}

	public function add_dc_packing_material(){
		check_login();
		if($this->input->post()){
	     		$id = $this->input->post('dc_id');
	     		$consignment = $this->input->post('consignment');

	     		    $material_id = $this->input->post('material_id');
	            	$qty = $this->input->post('qty');
	            	$insert_date = date('Y-m-d');
	            	$user_id = $this->input->post('user_id');
					$created = $this->input->post('created');
					$modified = $this->input->post('modified');
	     		
	            $result = $this->Orders->add_dc_qc_check_packing_material($_POST);
	            $result = $this->Orders->update_dc_qc_status($id,$consignment);


	            $condition  = $this->Stocks->get_material_for_condition($material_id);

		             if($consignment == '1'){

	             	 if($condition ==""){

				 		
					$save_data =  array('material_id' => $material_id,
										'type' => 2,
										'qty' => $qty,
										
										'insert_date'=>$insert_date,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );

					$save_log =  array('material_id' => $material_id,
										'type' => 2,
										'qty' => $qty,
										
										'insert_date'=>$insert_date,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );

			     	$result = $this->Stocks->add_fg($save_data);
			     	//print_r($save_log);
			     	//die;
			     	$result = $this->Stocks->add_fg_log($save_log);

			     }else{


			     		$all_qty = $condition[0]['qty']+$qty;

							//print_r($all_qty);
							//die;

						$dataarray =  array('material_id' => $material_id,
											'type' => 2,
											'qty' => $all_qty,
											
											'user_id'=> $user_id,
											'created'=> $created,
											'modified'=> $modified
											 );

						$save_log =  array('material_id' => $material_id,
											'type' =>2,
											'qty' => $qty,
											
											'insert_date'=>$insert_date,
											'user_id'=> $user_id,
											'created'=> $created,
											'modified'=> $modified
											 );
					 	
						$result = $this->Stocks->update_stock_material($material_id,$dataarray);
						$result = $this->Stocks->add_fg_log($save_log);

			     }
	             	

	            	
	             }

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

	}

	public function add_dc_product(){
		check_login();
		if($this->input->post()){
	     		$id = $this->input->post('dc_id');
	     		$consignment = $this->input->post('consignment');


	     			$product_id = $this->input->post('product_id');
	            	$qty = $this->input->post('qty');
	            	$insert_date = date('Y-m-d');
	            	$user_id = $this->input->post('user_id');
					$created = $this->input->post('created');
					$modified = $this->input->post('modified');
	     		
	            $result = $this->Orders->add_dc_product($_POST);
	            $result = $this->Orders->update_dc_qc_status($id,$consignment);
	            if($consignment == '1'){
	            	
					$condition  = $this->Stocks->get_product_for_condition($product_id);

			if($condition ==""){

				$save_data =  array('product_id' => $product_id,
							'type' =>1,
							'qty' => $qty,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

				$save_log =  array('product_id' => $product_id,
							'type' => 1,
							'qty' => $qty,
							
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

					     	$result = $this->Stocks->add_fg($save_data);
					     	//print_r($save_log);
					     	//die;
					     	$result = $this->Stocks->add_fg_log($save_log);

	            

	            }else{
						
						$all_qty = $condition[0]['qty']+$qty;
						$dataarray =  array('product_id' => $product_id,
										'type' => 1,
										'qty' => $all_qty,
										
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );

					$save_log =  array('product_id' => $product_id,
										'type' => 1,
										'qty' => $qty,
										
										'insert_date'=>$insert_date,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );
				 	
					$result = $this->Stocks->update_stock($product_id,$dataarray);
					$result = $this->Stocks->add_fg_log($save_log);
	      	      }

	            }

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

		}

	}

	

		public function active_in_store_print(){
		check_login();

		$id = $this->uri->segment('3');
		$data['quality_checked_print']=$this->Orders->quality_checked_print($id);
		


		$this->load->view('orders_qc/active_in_store_print1',$data);

		}

	public function dc_qc_check_list(){
		check_login();



		$data['get_dc_data']=$this->Orders->get_dc_data();

		$this->load->view('orders_qc/dc_qc_check_list',$data);

		}

		
public function add_qc_machine(){
		check_login();
		if($this->input->post()){


			$day_night = $this->input->post('day_night');
			//$machine_type = $this->input->post('machine_type');
			$machine_id = $this->input->post('machine');
			$job = $this->input->post('job');
			$product_id = $this->input->post('product_id');
			$material_id = $this->input->post('material_id');
			$report_date = $this->input->post('report_date');
			$mr_mould = $this->input->post('mr_mould');
			$pcs_cavity = $this->input->post('pcs_cavity');
			$weight = $this->input->post('weight');
			$finishing = $this->input->post('finishing');
			$fitting = $this->input->post('fitting');
			$engriving = $this->input->post('engriving');
			$color = $this->input->post('color');
			$remarks = $this->input->post('remarks');
			$insert_date = $this->input->post('insert_date');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			$user_id = $this->input->post('user_id');
			 	$product= product_id($product_id);
			 	$material= material_id($material_id);
				
			$dataarray =  array(
							'day_night' => $day_night,
							//'machine_type' => $machine_type,
							'machine_id' => $machine_id,
							'job' => $job,
							'product_id' => $product,
							'material_id' => $material,
							'report_date' => $report_date,
							'mr_mould' => $mr_mould,
							'pcs_cavity' => $pcs_cavity,
							'weight'=> $weight,
							'finishing'=> $finishing,
							'fitting'=> $fitting,
							'engriving'=> $engriving,
							'color'=> $color,
							'remarks'=> $remarks,
							'insert_date'=> $insert_date,
							'created'=> $created,
							'modified'=> $modified,
							'user_id'=> $user_id
							 );

	     	$result = $this->Orders->add_qc_machine($dataarray);
            if($result){$add_dc=array('status'=>'true');}else{$add_dc=array('status'=>'false');}
            echo json_encode($add_dc);
	      	}else{

	      	//$data['get_product']=$this->Orders->get_products_mould();
	      	//$data['get_materials']=$this->Orders->get_materials();
	      	$data['get_machine_mold']=$this->Orders->get_machine_mold();
	      	$data['get_machine']=$this->Stocks->get_products_semi_production();
	      	//$today_date = date('Y-m-d');

	      	//$today_date = '2022-10-01';

	      	$data['get_qc_machine']=$this->Orders->get_qc_machine();
			$this->load->view('orders_qc/add_qc_machine',$data);

	      }

	}


	
	public function get_mould_product(){

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

		$data['get_products_mold']=$this->Orders->get_products_mold($id);
		$data['get_products_machine']=$this->Orders->get_machine($id);
		echo json_encode (array('html'=>$this->load->view('orders_qc/get_products_mold',$data,true)));
	}



		public function get_machine(){

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

	
		$data['get_products_machine']=$this->Orders->get_machine($id);


		echo json_encode (array('html'=>$this->load->view('orders_qc/get_products_machine',$data,true)));
	}
	
	public function edit_machine_qc($id)
	{
		 check_login();

	    $id = $this->uri->segment('3');
		if($this->input->post())
		{
		
			$result=$this->Orders->update_machine_qc($id);
			
			if($result){$material=array('status'=>'true');}else{$party=array('status'=>'false');}
			echo json_encode($material);
		}else{
					$data['get_machine_qc']=$this->Orders->get_machine_qc_data($id);
					$data['get_product']=$this->Orders->get_products_mould();
			      	$data['get_materials']=$this->Orders->get_materials();
			      	$data['get_machine']=$this->Orders->get_machine_mold();
			      	$today_date = date('Y-m-d');
			      	$data['get_qc_machine']=$this->Orders->get_qc_machine($today_date);
			      	$data['get_machine_data']=$this->Orders->get_machine_data();
			      		 	
	          		$this->load->view('orders_qc/edit_machine_qc',$data);
	      }
				
	}



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

	    $id = $this->uri->segment('3');
		if($this->input->post())
		{
		
			$result=$this->Orders->update_qc_timings($id);
			
			if($result){$material=array('status'=>'true');}else{$party=array('status'=>'false');}
			echo json_encode($material);
		}else{
					$data['get_complainhours']=$this->Orders->get_complainhours($id);
					
			      		 	
	          		$this->load->view('orders_qc/edit_qc_timings',$data);
	      }
				
	}
	

	public function qc_production_on_machine()
	{
		 check_login();

	    $this->load->view('orders_qc/qc_production_on_machine');
	     
				
	}


	


	public function delete_timing_qc($id){

			check_login();
	
			$result=$this->Orders->delete_timing_qc($id);
	
			if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
			echo json_encode($empolyee);

	}


	
public function delete_randomlydelete($id){

		
			check_login();
	
			$result=$this->Orders->delete_randomlydelete($id);
	
			if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
			echo json_encode($empolyee);

	}


	public function delete_complian($id){

			check_login();
	
			$result=$this->Orders->delete_complian($id);
	
			if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
			echo json_encode($empolyee);

	}


	

	public function get_design_qc_production(){
			check_login();

			$data['id'] = $this->input->post('mode');
			if($data['id']==1){

			$data['get_machine_data']=$this->Orders->get_machine_data_production();

			/*print_r($data['get_machine_data']);
			die;*/

			$data['get_qc_timing']=$this->Orders->get_qc_timing();
			/*print_r($data['get_machine_data']);
			die;*/
			echo json_encode (array('html'=>$this->load->view('orders_qc/timing_machine_qc',$data,true)));

			}else if($data['id']==2){
				$data['get_machine_data']=$this->Orders->get_machine_data_production();
				/*print_r($data['get_machine_data']);
				die;*/

				$data['get_machine_data_production_operator']=$this->Orders->get_machine_data_production_operator();
				$data['get_randomly_qc']=$this->Orders->get_randomly_qc();
				echo json_encode (array('html'=>$this->load->view('orders_qc/randomly_check',$data,true)));
			}else if($data['id']==3){
				$data['get_supervisor_data']=$this->Orders->get_supervisor_data();
				$data['get_machine_data']=$this->Orders->get_machine_data_production();
				echo json_encode (array('html'=>$this->load->view('orders_qc/supervisor_check',$data,true)));
			}else if($data['id']==4){
					$data['get_machine_data']=$this->Orders->get_machine_data_production();

					$data['get_qc_complain_hour']=$this->Orders->get_qc_complain_hour();
				echo json_encode (array('html'=>$this->load->view('orders_qc/complain_hour',$data,true)));
			}else if($data['id']==5){
					$data['get_supervisor_data']=$this->Orders->get_supervisor1_data1();
					$data['get_machine_data']=$this->Orders->get_machine_data_production();
					
				echo json_encode (array('html'=>$this->load->view('orders_qc/supervisor_check_sheet_1',$data,true)));
			}else if($data['id']==6){
					$data['get_supervisor_data']=$this->Orders->get_supervisor_data_range();
					$data['get_machine_data']=$this->Orders->get_machine_data_production();
					
				echo json_encode (array('html'=>$this->load->view('orders_qc/supervisor_check_sheet_2',$data,true)));
			}
		}


		public function get_item_production_supervisor_1(){

			

			$id = production_machine_id_so_id_supervisor1($this->input->post('machine'));
			
			$p_name = product_name($id);
			
			
			$data2=$this->Orders->get_item_for_check_timing_qc($p_name);
			
	
			////Config Data////////////
	
			$data['product_name'] = @$data2[0]['product_name'];
	
			$data['job'] = @$data2[0]['job'];
			$data['material'] = material_name(@$data2[0]['material_id']);
			$data['material_id'] = @$data2[0]['material_id'];
	
			echo json_encode ($data);
		}


		public function get_item_production(){

			

		$id = production_machine_id_so_id($this->input->post('machine'));
		$so_id = production_so_id($this->input->post('machine'));
		$p_name = product_name($id);
		
		
		$data2=$this->Orders->get_item_for_check_timing_qc($p_name,$so_id);
		

		$data5=$this->Orders->get_config($id);

		
		////Config Data////////////
		$data['range1_max'] = @$data5[0]['range1_max'];
		$data['range1_min'] = @$data5[0]['range1_min'];
		$data['range2_max'] = @$data5[0]['range2_max'];
		$data['range2_min'] = @$data5[0]['range2_min'];
		$data['range3_max'] = @$data5[0]['range3_max'];
		$data['range3_min'] = @$data5[0]['range3_min'];
		$data['range4_max'] = @$data5[0]['range4_max'];
		$data['range4_min'] = @$data5[0]['range4_min'];
		$data['range5_max'] = @$data5[0]['range5_max'];
		$data['range5_min'] = @$data5[0]['range5_min'];
		$data['range1_injp_max'] = @$data5[0]['range1_injp_max'];
		$data['range1_injp_min'] = @$data5[0]['range1_injp_min'];
		$data['range2_injs_max'] = @$data5[0]['range2_injs_max'];
		$data['range2_injs_min'] = @$data5[0]['range2_injs_min'];
		$data['range3_injt_sec_max'] = @$data5[0]['range3_injt_sec_max'];
		$data['range3_injt_sec_min'] = @$data5[0]['range3_injt_sec_min'];
		$data['range4_ct_sec_max'] = @$data5[0]['range4_ct_sec_max'];
		$data['range4_ct_sec_min'] = @$data5[0]['range4_ct_sec_min'];
		$data['range5_doz_mm_max'] = @$data5[0]['range5_doz_mm_max'];
		$data['range5_doz_mm_min'] = @$data5[0]['range5_doz_mm_min'];
		////Config Data////////////

		$data['product_name'] = @$data2[0]['product_name'];

		$data['job'] = @$data2[0]['job'];
		$data['material'] = material_name(@$data2[0]['material_id']);
		$data['material_id'] = @$data2[0]['material_id'];

		echo json_encode ($data);
	}


	public function get_item_production1(){

			

		$id = production_machine_id_so_id($this->input->post('machine'));
		
		$p_name = product_name($id);
		
		
		$data2=$this->Orders->get_item_for_check_timing_qc($p_name,$id);
		

		$data5=$this->Orders->get_config_ibm($id);

		
		////Config Data////////////
		$data['range1_max'] = @$data5[0]['range1_max'];
		$data['range1_min'] = @$data5[0]['range1_min'];
		$data['range2_max'] = @$data5[0]['range2_max'];
		$data['range2_min'] = @$data5[0]['range2_min'];
		$data['range3_max'] = @$data5[0]['range3_max'];
		$data['range3_min'] = @$data5[0]['range3_min'];
		$data['range4_max'] = @$data5[0]['range4_max'];
		$data['range4_min'] = @$data5[0]['range4_min'];
		$data['range5_max'] = @$data5[0]['range5_max'];
		$data['range5_min'] = @$data5[0]['range5_min'];
		$data['inj_pressure_max'] = @$data5[0]['inj_pressure_max'];
		$data['inj_pressure_min'] = @$data5[0]['inj_pressure_min'];
		$data['inj_speed_max'] = @$data5[0]['inj_speed_max'];
		$data['inj_speed_min'] = @$data5[0]['inj_speed_min'];
		$data['doz_pressure_max'] = @$data5[0]['doz_pressure_max'];
		$data['doz_pressure_min'] = @$data5[0]['doz_pressure_min'];
		$data['doz_speed_max'] = @$data5[0]['doz_speed_max'];
		$data['doz_speed_min'] = @$data5[0]['doz_speed_min'];
		$data['blast_time_max'] = @$data5[0]['blast_time_max'];
		$data['blast_time_min'] = @$data5[0]['blast_time_min'];
		$data['expect_max'] = @$data5[0]['expect_max'];
		$data['expect_min'] = @$data5[0]['expect_min'];
		$data['m_o_t_max'] = @$data5[0]['m_o_t_max'];
		$data['m_o_t_min'] = @$data5[0]['m_o_t_min'];
		////Config Data////////////

		$data['product_name'] = @$data2[0]['product_name'];

		$data['job'] = @$data2[0]['job'];
		$data['material'] = material_name(@$data2[0]['material_id']);
		$data['material_id'] = @$data2[0]['material_id'];

		echo json_encode ($data);
	}


	

	public function timing_machine_qc(){
		check_login();
		if($this->input->post()){


	     		$machine = $this->input->post('machine');
	     		$product_name = $this->input->post('product_name');

	     		$job = $this->input->post('job');
            	$power_lot = $this->input->post('power_lot');
            	$bag_no = $this->input->post('bag_no');
            	$black_spot = $this->input->post('black_spot');
            	$short_pcs = $this->input->post('short_pcs');
            	$remarks = $this->input->post('remarks');
            	$insert_date = date('Y-m-d');
            	$user_id = $this->input->post('user_id');
				$created = $this->input->post('created');
				$modified = $this->input->post('modified');
	     		
	            
	            
	            $save_data =  array(	
	            						'machine' => $machine,
										'product_name' => $product_name,
										'job' => $job,
										'power_lot'=>$power_lot,
										'bag_no'=>$bag_no,
										'black_spot'=>$black_spot,
										'short_pcs'=>$short_pcs,
										'remarks'=>$remarks,
										'insert_date'=>$insert_date,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );
	            $result = $this->Orders->add_timing_qc($save_data);

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

	}


	

	public function add_supervisor_range_qcs(){
		check_login();
		if($this->input->post()){


	     		$machine = $this->input->post('machine');
	     		$product_name = $this->input->post('product_name');
				$job = $this->input->post('job');
				$insert_date = date('Y-m-d');
            	$die_condition = $this->input->post('die_condition');
            	$rang_1 = $this->input->post('rang_1');
            	$rang_2 = $this->input->post('rang_2');
            	$rang_3 = $this->input->post('rang_3');
            	$rang_4 = $this->input->post('rang_4');
            	$rang_5 = $this->input->post('rang_5');
				$rang_6 = $this->input->post('rang_6');
				$rang_7 = $this->input->post('rang_7');
				$rang_8 = $this->input->post('rang_8');
				$rang_9 = $this->input->post('rang_9');
				$rang_10 = $this->input->post('rang_10');
				$pieces_quality = $this->input->post('pieces_quality');
				$mould_cooling = $this->input->post('mould_cooling');
				$m_o_t = $this->input->post('m_o_t');
				$shift = $this->input->post('shift');
				$remarks = $this->input->post('remarks');
            	$user_id = $this->input->post('user_id');
				$created = $this->input->post('created');
				
	            $save_data =  array(	
									'machine' => $machine,
									'product_name' => $product_name,
									'job' => $job,
									'insert_date'=>$insert_date,
									'die_condition'=>$die_condition,
									'rang_1'=>$rang_1,
									'rang_2'=>$rang_2,
									'rang_3'=>$rang_3,
									'rang_4'=>$rang_4,
									'rang_5'=>$rang_5,
									'rang_6'=>$rang_6,
									'rang_7'=>$rang_7,
									'rang_8'=>$rang_8,
									'rang_9'=>$rang_9,
									'rang_10'=>$rang_10,
									'pieces_quality'=>$pieces_quality,
									'mould_cooling'=>$mould_cooling,
									'm_o_t'=>$m_o_t,
									'shift'=>$shift,
									'remarks'=>$remarks,
									'user_id'=> $user_id,
									'created'=> $created
										
										 );
										 
	            $result = $this->Orders->add_supervisor_qcs($save_data);

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

	}


	public function add_supervisor1(){
		check_login();
		if($this->input->post()){


	     		$machine = $this->input->post('machine');
	     		$product_name = $this->input->post('product_name');
				$job = $this->input->post('job');
				$insert_date = date('Y-m-d');
            	$die_condition = $this->input->post('die_condition');
            	$range1_max = $this->input->post('range1_max');
				$range1_min = $this->input->post('range1_min');
				$range2_max = $this->input->post('range2_max');
				$range2_min = $this->input->post('range2_min');

				$range3_max = $this->input->post('range3_max');
				$range3_min = $this->input->post('range3_min');
				$range4_max = $this->input->post('range4_max');
				$range4_min = $this->input->post('range4_min');
				$range5_max = $this->input->post('range5_max');
				$range5_min = $this->input->post('range5_min');
				$inj_pressure_max = $this->input->post('inj_pressure_max');
				$inj_pressure_min = $this->input->post('inj_pressure_min');
				$inj_speed_max = $this->input->post('inj_speed_max');
				$inj_speed_min = $this->input->post('inj_speed_min');
				$doz_pressure_max = $this->input->post('doz_pressure_max');
				$doz_pressure_min = $this->input->post('doz_pressure_min');
				$doz_speed_max = $this->input->post('doz_speed_max');
				$doz_speed_min = $this->input->post('doz_speed_min');
				$blast_time_max = $this->input->post('blast_time_max');
				$blast_time_min = $this->input->post('blast_time_min');
				$expect_max = $this->input->post('expect_max');
				$expect_min = $this->input->post('expect_min');
				$rang_1 = $this->input->post('rang_1');
				$rang_2 = $this->input->post('rang_2');
				$rang_3 = $this->input->post('rang_3');
				$rang_4 = $this->input->post('rang_4');
				$rang_5 = $this->input->post('rang_5');
				$inject_pressure = $this->input->post('inject_pressure');
				$inject_speed = $this->input->post('inject_speed');
				$doz_pressure = $this->input->post('doz_pressure');
				$doz_speed = $this->input->post('doz_speed');
				$air_blast = $this->input->post('air_blast');
				$expect = $this->input->post('expect');
				$m_t_o = $this->input->post('m_t_o');
				$pieces_quality = $this->input->post('pieces_quality');
				$mould_cooling  = $this->input->post('mould_cooling');
				$m_o_t_max = $this->input->post('m_o_t_max');
				$m_o_t_min = $this->input->post('m_o_t_min');

				
				$shift = $this->input->post('shift');
				$remarks = $this->input->post('remarks');
            	$user_id = $this->input->post('user_id');
				$created = $this->input->post('created');
				
	            $save_data =  array(	
									'machine' => $machine,
									'product_name' => $product_name,
									'job' => $job,
									'insert_date'=>$insert_date,
									'die_condition'=>$die_condition,
									'range1_max'=>$range1_max,
									'range1_min'=>$range1_min,
									'range2_max'=>$range2_max,
									'range2_min'=>$range2_min,

									'range3_max'=>$range3_max,
									'range3_min'=>$range3_min,
									'range4_max'=>$range4_max,
									'range4_min'=>$range4_min,
									'range5_max'=>$range5_max,
									'range5_min'=>$range5_min,
									
									'inj_pressure_max'=>$inj_pressure_max,
									'inj_pressure_min'=>$inj_pressure_min,
									'inj_speed_max'=>$inj_speed_max,
									'inj_speed_min'=>$inj_speed_min,
									'doz_pressure_max'=>$doz_pressure_max,
									'doz_pressure_min'=>$doz_pressure_min,
									'doz_speed_max'=>$doz_speed_max,
									'doz_speed_min'=>$doz_speed_min,
									'blast_time_max'=>$blast_time_max,
									'blast_time_min'=>$blast_time_min,
									'expect_max'=>$expect_max,
									'expect_min'=>$expect_min,
									'rang_1'=>$rang_1,
									'rang_2'=>$rang_2,
									'rang_3'=>$rang_3,
									'rang_4'=>$rang_4,
									'rang_5'=>$rang_5,
									'inject_pressure'=>$inject_pressure,
									'inject_speed'=>$inject_speed,
									'doz_pressure'=>$doz_pressure,
									'doz_speed'=>$doz_speed,
									'air_blast'=>$air_blast,
									'expect'=>$expect,
									'm_t_o'=>$m_t_o,
									'pieces_quality'=>$pieces_quality,
									'mould_cooling'=>$mould_cooling,
									'm_o_t_max'=>$m_o_t_max,
									'm_o_t_min'=>$m_o_t_min,
									'shift'=>$shift,
									'remarks'=>$remarks,
									'created'=> $created,
									'user_id'=> $user_id
									
										
										 );
										 
	            $result = $this->Orders->add_supervisor1_qcs($save_data);

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

	}



	public function supervisormachine_qc(){
		check_login();
		if($this->input->post()){


	     		$machine = $this->input->post('machine');
	     		$product_name = $this->input->post('product_name');

	     		$job = $this->input->post('job');
            	$machine_cooling = $this->input->post('machine_cooling');
            	$die_cooling = $this->input->post('die_cooling');
            	$heater1 = $this->input->post('heater1');
            	$heater2 = $this->input->post('heater2');
            	$heater3 = $this->input->post('heater3');
            	$heater4 = $this->input->post('heater4');
            	$heater5 = $this->input->post('heater5');
            	$heater6 = $this->input->post('heater6');
            	$timers_reading1 = $this->input->post('timers_reading1');
            	$timers_reading2 = $this->input->post('timers_reading2');
            	$timers_reading3 = $this->input->post('timers_reading3');
            	$timers_reading4 = $this->input->post('timers_reading4');
            	$timers_reading5 = $this->input->post('timers_reading5');
            	$timers_reading6 = $this->input->post('timers_reading6');
            	$pcs_codition = $this->input->post('pcs_codition');
            	$machine_condition = $this->input->post('machine_condition');

            	$remarks = $this->input->post('remarks');
            	$insert_date = date('Y-m-d');
            	$user_id = $this->input->post('user_id');
				$created = $this->input->post('created');
				$modified = $this->input->post('modified');
	     		
	            
	            
	            $save_data =  array(	
	            						'machine' => $machine,
										'product_name' => $product_name,
										'job' => $job,
										'machine_cooling'=>$machine_cooling,
										'die_cooling'=>$die_cooling,
										'heater1'=>$heater1,
										'heater2'=>$heater2,
										'heater3'=>$heater2,
										'heater4'=>$heater2,
										'heater5'=>$heater2,
										'heater6'=>$heater2,
										'timers_reading1'=>$timers_reading1,
										'timers_reading2'=>$timers_reading2,
										'timers_reading3'=>$timers_reading3,
										'timers_reading4'=>$timers_reading4,
										'timers_reading5'=>$timers_reading5,
										'timers_reading6'=>$timers_reading6,
										'pcs_codition'=>$pcs_codition,
										'machine_condition'=>$machine_condition,
										
										'remarks'=>$remarks,
										'insert_date'=>$insert_date,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );
	            $result = $this->Orders->add_supervisor_qc($save_data);

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

	}
	

	public function randomly_check(){
		check_login();
		if($this->input->post()){

			$dateTime = new DateTime('now', new DateTimeZone('Asia/Karachi'));

	     		$machine = $this->input->post('machine');
	     		$job = $this->input->post('job');
            	$product_name = $this->input->post('product_name');
            	$material = $this->input->post('material');
            	$time_in = $this->input->post('time_in');

            	$new_tapin = date('h:i:s A', strtotime($time_in));

            	$total_cavities = $this->input->post('total_cavities');
            	$production_cavities = $this->input->post('production_cavities');
            	$operator = $this->input->post('operator');
            	$short_pcs = $this->input->post('short_pcs');
            	$condition = $this->input->post('condition');
            	$fitting = $this->input->post('fitting');
            	$pcs_weight_range = $this->input->post('pcs_weight_range');
            	$check_pcs = $this->input->post('check_pcs');
            	$time_out = $this->input->post('time_out');
            	$new_tapout = date('h:i:s A', strtotime($time_out));
            	$remarks = $this->input->post('remarks');

            	$insert_date =  $this->input->post('insert_date');
            	$shift = $this->input->post('shift');
            	$user_id = $this->input->post('user_id');
				$created = $this->input->post('created');
				$modified = $this->input->post('modified');
	     		
	            
	            
	            $save_data =  array(	
	            						'machine' => $machine,
										'job' => $job,
										'product_name' => $product_name,
										'material'=>$material,
										'time_in'=>$new_tapin,
										'total_cavities'=>$total_cavities,
										'production_cavities'=>$production_cavities,
										'operator'=>$operator,
										'short_pcs'=>$short_pcs,
										'condition'=>$condition,
										'fitting'=>$fitting,
										'pcs_weight_range'=>$pcs_weight_range,
										'check_pcs'=>$check_pcs,
										'time_out'=>$new_tapout,
										'remarks'=>$remarks,
										'insert_date'=>$insert_date,
										'shift' => $shift,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );
	            $result = $this->Orders->add_randomlycheck_qc($save_data);

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

	}


	

	public function complain_hour(){
		check_login();
		if($this->input->post()){

			//$dateTime = new DateTime('now', new DateTimeZone('Asia/Karachi'));

	     		$machine = $this->input->post('machine');
	     		$job = $this->input->post('job');
            	$product_name = $this->input->post('product_name');
            	//$material = $this->input->post('material');
            	$work_stop = $this->input->post('work_stop');

            	$production_time = $this->input->post('production_time');
            	$new_tapout = date('h:i:s A', strtotime($production_time));
            	$production_time_start = $this->input->post('production_time_start');
            	$new_tapout1 = date('h:i:s A', strtotime($production_time_start));
            	$production_stoping_time = $this->input->post('production_stoping_time');
            	$condition = $this->input->post('condition');
            	$remarks = $this->input->post('remarks');
            	
            	$insert_date = date('Y-m-d');
            	$user_id = $this->input->post('user_id');
				$created = $this->input->post('created');
				$modified = $this->input->post('modified');
	     		
	            
	            
	            $save_data =  array(	
	            						'machine' => $machine,
										'job' => $job,
										'product_name' => $product_name,
										//'material'=>$material,
										'work_stop'=>$work_stop,
										'production_time'=>$new_tapout,
										'production_time_start'=>$new_tapout1,
										'production_stoping_time'=>$production_stoping_time,
										'condition'=>$condition,
										
										'remarks'=>$remarks,
										'insert_date'=>$insert_date,
										'user_id'=> $user_id,
										'created'=> $created,
										'modified'=> $modified
										 );
	            $result = $this->Orders->add_complain_hour_check_qc($save_data);

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

	}

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

	
}
