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


	/*public function __construct()
		{
		 parent::__construct();
		 $this->load->library('pdf');
		}

		public function get_pdf()
		{
		    $this->load->library('pdf');
		    $this->pdf->loadHtml('html code or variable');
		    // $customPaper = array(0,0,570,570);
		    //$this->pdf->set_paper($customPaper);
		    $this->pdf->setPaper('A4','portrait');//landscape
		    $this->pdf->render();
		    $this->pdf->stream("abc.pdf", array('Attachment'=>0));
		    //'Attachment'=>0 for view and 'Attachment'=>1 for download file        
		}*/

	public function active_stock()
	{
		check_login();
		
				
	      		//$data['parties']=$this->Sales_Orders->get_party();
	      		
	      		//$data['get_po']=$this->Orders->get_all_po();
	      		$data['get_active_stock']=$this->Stocks->get_all_active_stock();
	      		//print_r($data['get_active_stock']);
	      		//die;
				$this->load->view('Stock/active_stock',$data);
	   
	}


	public function raw_material_mixing(){

		check_login();

		$data['get_active_stock']=$this->Stocks->get_raw_material_mixing();

		$this->load->view('Stock/raw_material_mixing',$data);


	}

	public function add_raw_material_mixing(){
		check_login();

		if($this->input->post()){
	     	
	           $result = $this->Stock->add_raw_material_mixing($_POST);
	        if($result){$add_mixing=array('status'=>'true');}else{$add_mixing=array('status'=>'false');}
	            echo json_encode($add_mixing);
	      }else{
	           $this->load->view('Stock/raw_material_mixing_add');
	      }

	}


	public function production_wast_in(){
	check_login();

	

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

		$material_id = $this->input->post('material_id');

		
	$condition  = $this->Stocks->get_material_wast_for_condition($material_id);
	
	 if($condition ==""){

	 		$material_id = $this->input->post('material_id');
			$qty = $this->input->post('qty');
			$wast_lot = $this->input->post('wast_lot');
			//$type = $this->input->post('type');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			
			$insert_date = $this->input->post('insert_date');
			

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

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

     	$result = $this->Stocks->add_wast($save_data);
     	//print_r($save_log);
     	//die;
     	$result = $this->Stocks->add_wast_log($save_log);
     	
       if($result){$add_fg=array('status'=>'true');}else{$add_fg=array('status'=>'false');}
        echo json_encode($add_fg);
		}else {

			$qty_add = $this->input->post('total_qty');
			$qty = $this->input->post('qty');
			$wast_lot = $this->input->post('wast_lot');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			$insert_date = $this->input->post('insert_date');

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

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

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

		$save_log =  array('material_id' => $material_id,
							//'type' => $type,
							'qty' => $qty,
							'wast_lot' => $wast_lot,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );
	 	
		$result = $this->Stocks->update_stock_wast($material_id,$dataarray);
		$data = $this->Stocks->add_wast_log($save_log);
		echo json_encode($data);
				}
			}
	}
	
	
	public function waste_printer(){

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

	      		//$data['get_all_so_products']=$this->Sales_Orders->get_all_so_products($id);
	      		$data['all_waste']=$this->Stocks->get_waste_data($id);
				$this->load->view('Stock/waste_printer',$data);

	}


	

public function production_balance(){
	check_login();


	if($this->input->post())
	{
		$product_id = $this->input->post('product_id');	
		$material_id = $this->input->post('material_id');	
		$machine_id = $this->input->post('machine_id');
		$insert_date = $this->input->post('insert_date');
		$p_ml_kg = $this->input->post('p_ml_kg');
		$w_ml_kg = $this->input->post('w_ml_kg');
		$m_weight = $this->input->post('m_weight');
		$w_weight = $this->input->post('w_weight');
		$last_blance = $this->input->post('last_blance');
		$balance = $this->input->post('balance');

		$user_id = $this->input->post('user_id');
		$created = $this->input->post('created');
		$modified = $this->input->post('modified');

		$condition  = $this->Stocks->getproduction_stock($material_id,$machine_id,$product_id);
		
		if($condition ==""){

		 	$dataarray =  array(
		 						'product_id' => $product_id,
		 						'material_id' => $material_id,

								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								'm_weight'=>$m_weight,
								'w_weight'=> $w_weight,
								'p_ml_kg' => $p_ml_kg,
								'w_ml_kg' => $w_ml_kg,
								'balance' => $balance,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );


		 	$dataarray1 =  array(
		 						'product_id' => $product_id,
		 						'material_id' => $material_id,

								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								'm_weight'=>$m_weight,
								'w_weight'=> $w_weight,
								'p_ml_kg' => $p_ml_kg,
								'w_ml_kg' => $w_ml_kg,
								'last_blance' => $last_blance,
								'balance' => $balance,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

	     	$result = $this->Stocks->add_production_mat($dataarray);

	     	$result = $this->Stocks->log_production_mat($dataarray1);

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


				//$all_qty = $condition[0]['balance']+$balance;
				
				$dataarray =  array(
		 						'product_id' => $product_id,
		 						'material_id' => $material_id,

								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								'm_weight'=>$m_weight,
								'w_weight'=> $w_weight,
								'p_ml_kg' => $p_ml_kg,
								'w_ml_kg' => $w_ml_kg,
								'last_blance' => $last_blance,
								'balance' => $balance,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );


		 	$dataarray1 =  array(
		 						'product_id' => $product_id,
		 						'material_id' => $material_id,

								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								'm_weight'=>$m_weight,
								'w_weight'=> $w_weight,
								'p_ml_kg' => $p_ml_kg,
								'w_ml_kg' => $w_ml_kg,
								'balance' => $balance,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

	     	$result = $this->Stocks->log_production_mat($dataarray);

	     	$result = $this->Stocks->update_production_mat($material_id,$machine_id,$product_id,$dataarray1);

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


	}

	}



	public function fg_stock(){
	check_login();


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

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

	$color_id = $this->input->post('color_id');

	$machine = $this->input->post('machine');
	$job = $this->input->post('job');
	$product_ids = $this->input->post('product_id');

	$product_id = $product_ids;
	$p_qty = $this->input->post('p_qty');

	$qty = $this->input->post('qty');

	if($p_qty==0 || $qty > $p_qty ){

			$data=array('status'=>'complete');
      		  echo json_encode($data);
      		  die;

      		}

	if($product_id !="" && $color_id == ""){
	$condition  = $this->Stocks->get_product_for_condition($product_id);

	
	 if($condition ==""){



	 		$product_id = $this->input->post('product_id');
	 		$production_id = $this->input->post('machine');
			
			$qty = $this->input->post('qty');
			$type = $this->input->post('type');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			$so_id = $this->input->post('so_id');
			$insert_date = $this->input->post('insert_date');
				
			      $productid = product_id($product_id);

				 $p_name = product_name($productid);
				 $qty_production = qty_production($p_name,$so_id);
				



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

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

			/*print_r($productid);
			print_r($so_id);
			die;*/

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

     	 $qty_in_stock_log = qty_in_stock_log($productid,$so_id);

				 $fg_log_qty = $qty_in_stock_log+$qty;

				

     	if($qty_production == $fg_log_qty){

		        


			$result = $this->Stocks->update_machine_status($productid,$so_id);

			
			$result = $this->Stocks->update_production_plan_status($p_name,$so_id);

			$result11 = $this->Stocks->production_plan_complete_status($so_id);
				$check_status = 1;
				
					foreach ($result11 as $key => $value) {


						/*print_r($value['complete_status']);
*/
						
						if($value['complete_status'] == 0){
							$check_status = 'no';
							//break;
						}

					}
					
						if($check_status== 1){
							
							

							$result=$this->Requisitions->update_so_po_finish($so_id);


						}

			/*$result = $this->Stocks->update_production_plan_complete($production_id);*/

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

		}else if($qty_production == $qty){

				

			$result = $this->Stocks->update_machine_status($so_id,$productid);

			
			$result = $this->Stocks->update_production_plan_status($p_name,$so_id);

			$result11 = $this->Stocks->production_plan_complete_status($so_id);
				$check_status = 1;
				
					foreach ($result11 as $key => $value) {


						/*print_r($value['complete_status']);
*/
						
						if($value['complete_status'] == 0){
							$check_status = 'no';
							//break;
						}

					}
					
						if($check_status== 1){
							
							

							$result=$this->Requisitions->update_so_po_finish($so_id);


						}

			/*$result = $this->Stocks->update_production_plan_complete($production_id);*/

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

		}
     	
      
		}else {
			$product_id = $this->input->post('product_id');
			$productid = product_id($product_id);
			$so_id = $this->input->post('so_id');
				 $p_name = product_name($productid);
			 $qty_production = qty_production($p_name,$so_id);

			$qty_add = $this->input->post('total_qty');
			$qty = $this->input->post('qty');
			$type = $this->input->post('type');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			$insert_date = $this->input->post('insert_date');
			$so_id = $this->input->post('so_id');

			
			 $qty_production = qty_production($p_name,$so_id);
				 $qty_in_stock_log = qty_in_stock_log($productid,$so_id);
			$all_qty = $condition[0]['qty']+$qty;

			 $fg_log_qty = $qty_in_stock_log+$qty;

		/*if($p_qty==0 || $qty > $p_qty){

			$data=array('status'=>'complete');
      		  echo json_encode($data);
      		  

		}else{*/

		$dataarray =  array('product_id' => $productid,
							'type' => $type,
							'qty' => $all_qty,
							'remarks' => $remarks,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

		$save_log =  array(
							'so_id' => $so_id,
							'product_id' => $productid,
							'type' => $type,
							'qty' => $qty,
							'remarks' => $remarks,
							'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($qty_production == $fg_log_qty){

		

			$result = $this->Stocks->update_machine_status($product_id,$so_id);

			$p_name = $product_id;

			$result = $this->Stocks->update_production_plan_status($p_name,$so_id);

			/*$result = $this->Stocks->update_production_plan_complete($production_id);*/

		}

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

		}
       

		}else if($product_id == "" && $color_id == ""){

			$material_id = $this->input->post('material_id');

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

	 

	 		$material_id = $this->input->post('material_id');
			$qty = $this->input->post('qty');
			$type = $this->input->post('type');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			
			$insert_date = $this->input->post('insert_date');
			

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

		$save_log =  array('material_id' => $material_id,
							'type' => $type,
							'qty' => $qty,
							'remarks' => $remarks,
							'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);
     	
       if($result){$add_fg=array('status'=>'true');}else{$add_fg=array('status'=>'false');}
        echo json_encode($add_fg);
		}else {

			$qty_add = $this->input->post('total_qty');
			$qty = $this->input->post('qty');
			$type = $this->input->post('type');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			$insert_date = $this->input->post('insert_date');

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

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

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

		$save_log =  array('material_id' => $material_id,
							'type' => $type,
							'qty' => $qty,
							'remarks' => $remarks,
							'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){$edit_fg=array('status'=>'true');}else{$edit_fg=array('status'=>'false');}
        echo json_encode($edit_fg);
		}


		}else if($color_id != "" ){

			

			$color_id = $this->input->post('color_id');

			$condition  = $this->Stocks->get_color_for_condition($color_id);
	
	 if($condition ==""){



	 		$color_id = $this->input->post('color_id');
			$qty = $this->input->post('qty');
			//$type = $this->input->post('type');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			
			$insert_date = $this->input->post('insert_date');
			

		$save_data =  array('color_id' => $color_id,
							//'type' => $type,
							'qty' => $qty,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

		$save_log =  array('color_id' => $color_id,
							//'type' => $type,
							'qty' => $qty,
							'remarks' => $remarks,
							'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);
     	
       if($result){$add_fg=array('status'=>'true');}else{$add_fg=array('status'=>'false');}
        echo json_encode($add_fg);
		}else {

			$qty_add = $this->input->post('total_qty');
			$qty = $this->input->post('qty');
			//$type = $this->input->post('type');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			$insert_date = $this->input->post('insert_date');

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

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

		$dataarray =  array('color_id' => $color_id,
							//'type' => $type,
							'qty' => $all_qty,
							'remarks' => $remarks,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

		$save_log =  array('color_id' => $color_id,
							//'type' => $type,
							'qty' => $qty,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );
	 	
		$result = $this->Stocks->update_stock_color($color_id,$dataarray);
		$result = $this->Stocks->add_fg_log($save_log);




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

		}


	}else{
	
		$this->load->view('Stock/fg_stock');
		}
	}

	
	public function out_stock_production(){
	check_login();


	if($this->input->post())
	{
		$product_id = $this->input->post('product_id');
		
		$material_id = $this->input->post('material_id');	

		$so_id = production_get_so_id($material_id);

		$machine_id = $this->input->post('machine_id');	
		//$machine_id = $this->input->post('machine_id');
		$insert_date = $this->input->post('insert_date');
		$qcr = $this->input->post('qcr');
		$color_code = $this->input->post('color_code');
		$color_ratio = $this->input->post('color_ratio');
		$quantity = $this->input->post('quantity');
		$wlot = $this->input->post('wlot');
		$weight_wast = $this->input->post('weight_wast');
		$remarks = $this->input->post('remarks');
		$user_id = $this->input->post('user_id');
		$created = $this->input->post('created');
		$modified = $this->input->post('modified');
		$color_id = $this->input->post('color_id');

		
		$condition  = $this->Stocks->get_material_for_condition($material_id);
		
		 if($condition !="" && $color_id ==""){

		 	

		 	$dataarray =  array(
		 						'so_id' => $so_id,
		 						'product_id' => $product_id,
		 						'material_id' => $material_id,
								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								'qcr' => $qcr,
								'color_code' => $color_code,
								'color_ratio' => $color_ratio,
								'qty'=>$quantity,
								'wlot'=> $wlot,
								'weight_wast'=> $weight_wast,
								'remarks'=> $remarks,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

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

	     	$result = $this->Stocks->add_out_stock_production($dataarray);

	     	$result = $this->Stocks->update_waste_status($machine_id);


	       if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
	        echo json_encode($add_out);
			}else if( $condition !="" && $color_id !=""){

				


				$dataarray =  array(
		 						'so_id' => $so_id,
		 						'product_id' => $product_id,
		 						'material_id' => $material_id,
								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								
								'qty'=>$quantity,
								'wlot'=> $wlot,
								'weight_wast'=> $weight_wast,
								'remarks'=> $remarks,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

		 

	     	$result = $this->Stocks->add_out_stock_production($dataarray);

	     	$result = $this->Stocks->update_waste_status($machine_id);


				$dataarray1 =  array(
	     					'so_id' => $so_id,
	     					'product_id' => $product_id,
	     					'material_id' => $color_id,
							'machine_id' => $machine_id,
							'insert_date'=>$insert_date,
							'qcr' => $qcr,
							'color_code' => $color_code,
							'color_ratio' => $color_ratio,
							'qty' => $quantity,
						
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );




     	$result = $this->Stocks->add_out_stock_production($dataarray1);

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

				
			}else{


				echo "Stock Not Available..!";
			}


	}

	}

	
public function out_stock(){

	check_login();

	if($this->input->post())
	{
	$product_id = $this->input->post('product_id');
	$color_id = $this->input->post('color_id');

	if($product_id !="" && $color_id == ""){

	$product_id = $this->input->post('product_id[]');	
	$qty = $this->input->post('qty');
	$type = $this->input->post('type');
	$insert_date = $this->input->post('insert_date');
	$remarks = $this->input->post('remarks');
	$user_id = $this->input->post('user_id');
	$created = $this->input->post('created');
	$modified = $this->input->post('modified');
	$product_fg = $this->input->post('fg_product');
	$batch = $this->input->post('batch');
	$product_name = $this->input->post('product_id[]');

	$in_stock = $this->input->post('in_stock[]');

	/*print_r($in_stock);
*/
	foreach ($in_stock as  $in_stocka) {
		

		if($in_stocka < $qty){

			$add_out=array('status'=>'qty_not_match');
	        echo json_encode($add_out);

			exit;

			}
		}

		$fg_types=$this->Stocks->fg_get_ids_boms($product_fg);
			//$pp_name = product_name($fg_types);
		foreach ($fg_types as $values) {

						$semi_products = $values;


						$semi_type = $this->Stocks->semi_type_products($semi_products);

						
						if(@$semi_type){

							$fg_line =  array(
		 						'batch' => $batch,
		 						'fg_product' => $product_fg,
		 						
								'qty' => $qty,
								'insert_date'=>$insert_date,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

     						$result = $this->Stocks->add_fg_stock($fg_line);


							if($semi_type[0]['product_type'] == '3'){

								$assembled_line =  array(
		 						'batch' => $batch,
		 						'fg_product' => $product_fg,
		 						
								'qty' => $qty,
								'insert_date'=>$insert_date,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

     
       	 				$result = $this->Stocks->add_assembled_stock($assembled_line);


							} else if($semi_type[0]['product_type'] == '2'){
								
								$printed_line =  array(
		 						'batch' => $batch,
		 						'fg_product' => $product_fg,
		 						
								'qty' => $qty,
								'insert_date'=>$insert_date,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

     					$result = $this->Stocks->add_printed_stock($printed_line);


							}else if($semi_type[0]['product_type'] == '4'){

								$sealed_line =  array(
		 						'batch' => $batch,
		 						'fg_product' => $product_fg,
		 						
								'qty' => $qty,
								'insert_date'=>$insert_date,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

					     	$result = $this->Stocks->add_sealed_stock($sealed_line);



							}
							
						}


						
						
					}

				

	 foreach ($product_name as  $value) {
		
		$ppp = product_id($value);

		$condition  = $this->Stocks->get_product_for_condition($ppp);
		
		if($condition !=""){

 	    $condition_batch = $this->Stocks->match_batch($batch);

 	    if($condition_batch !="" ){

 	 	 if($condition_batch){$add_out=array('status'=>'true1');}
         echo json_encode($add_out);

		 }else{
					$dataarray =  array('product_id' => $ppp,
							'type' => $type,
							'qty' => $qty,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

		     	$result = $this->Stocks->add_out_stock($dataarray);
		    	} 
		       
		      }else{

			echo "Stock Not Available..!";
			}

		}

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

	}else  if($product_id == "" && $color_id == ""){


		$material_id = $this->input->post('material_id');	
		$qty = $this->input->post('qty');
		$type = $this->input->post('type');
		$insert_date = $this->input->post('insert_date');
		$remarks = $this->input->post('remarks');
		$user_id = $this->input->post('user_id');
		$created = $this->input->post('created');
		$modified = $this->input->post('modified');

		$condition  = $this->Stocks->get_material_for_condition($material_id);
		
		 if($condition !=""){

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

	     	$result = $this->Stocks->add_out_stock($dataarray);
	       if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
	        echo json_encode($add_out);
			}else{

				echo "Stock Not Available..!";
			}


	}else if($color_id != ""){

			$color_id = $this->input->post('color_id');	
			$qty = $this->input->post('qty');
			//$type = $this->input->post('type');
			$insert_date = $this->input->post('insert_date');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');

			$condition  = $this->Stocks->get_color_for_condition($color_id);
			
			 if($condition !=""){

			 	$dataarray =  array('color_id' => $color_id,
									//'type' => $type,
									'qty' => $qty,
									'remarks' => $remarks,
									'insert_date'=>$insert_date,
									'user_id'=> $user_id,
									'created'=> $created,
									'modified'=> $modified
									 );

		     	$result = $this->Stocks->add_out_stock($dataarray);
		       if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
		        echo json_encode($add_out);
				}else{

					echo "Stock Not Available..!";
				}
	}


	}else{
	
		$this->load->view('Stock/fg_stock');
		}
	}


    public function waste_out_stock(){
	check_login();


	if($this->input->post())
	{
		$material_id = $this->input->post('material_id');	
		$machine_id = $this->input->post('machine_id');
		$insert_date = $this->input->post('insert_date');
		//$qcr = $this->input->post('qcr');
		//$color_code = $this->input->post('color_code');
		//$color_ratio = $this->input->post('color_ratio');
		$qty = $this->input->post('qty');
		$wast_lot = $this->input->post('wast_lot');
		//$weight_wast = $this->input->post('weight_wast');
		$remarks = $this->input->post('remarks');
		$user_id = $this->input->post('user_id');
		$created = $this->input->post('created');
		$modified = $this->input->post('modified');

		$condition  = $this->Stocks->get_material_for_condition_waste($material_id);
		
		 if($condition !=""){

		 	$dataarray =  array('material_id' => $material_id,
								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								'wast_lot'=>$wast_lot,
								'qty'=>$qty,
								
								'remarks'=> $remarks,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

	     	$result = $this->Stocks->add_out_waste_Stock($dataarray);
	       if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
	        echo json_encode($add_out);
			}else{

				echo "Stock Not Available..!";
			}

		}

	}

public function return_production_balance(){
	check_login();

	

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

		$material_ids = $this->input->post('material_id');

		$material_id = material_id($material_ids);
		
		$condition  = $this->Stocks->get_material_production_for_condition($material_id);
	
	 if($condition ==""){

	 		$product_id = $this->input->post('product_id');
	 		$product = product_id($product_id);


			$qty = $this->input->post('qty');
			$production_id = $this->input->post('machine_id');
			$unit = $this->input->post('unit');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			
			$insert_date = $this->input->post('insert_date');
			

		$save_data =  array('product_id' => $product,
							
							'material_id' => $material_id,
							'production_id' => $production_id,
							'insert_date' => $insert_date,
							'qty'=>$qty,
							'unit' => $unit,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

		$save_log =  array('product_id' => $product,
							'material_id' => $material_id,
							'production_id' => $production_id,
							'insert_date' => $insert_date,
							'qty'=>$qty,
							'unit' => $unit,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

     	$result = $this->Stocks->add_production_mat($save_data);
     	//print_r($save_log);
     	//die;
     	$result = $this->Stocks->log_production_mat($save_log);
     	
       if($result){$add_fg=array('status'=>'true');}else{$add_fg=array('status'=>'false');}
        echo json_encode($add_fg);
		}else {

			$product_id = $this->input->post('product_id');
	 		$product = product_id($product_id);
	 		$material_id = material_id($material_ids);

			$qty = $this->input->post('qty');
			$production_id = $this->input->post('machine_id');
			$unit = $this->input->post('unit');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			
			$insert_date = $this->input->post('insert_date');

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

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

		$dataarray =  array('product_id' => $product,
							'material_id' => $material_id,
							'production_id' => $production_id,
							'insert_date' => $insert_date,
							'qty'=>$qty,
							'unit' => $unit,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

		$save_log =  array('product_id' => $product,
							'material_id' => $material_id,
							'production_id' => $production_id,
							'insert_date' => $insert_date,
							'qty'=>$qty,
							'unit' => $unit,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );
	 	
		$result = $this->Stocks->update_stock_return($material_id,$dataarray);
		$data = $this->Stocks->log_production_mat($save_log);
		echo json_encode($data);
				}
			}
	}


	public function production_out_stock(){
	check_login();


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

			$material_ids = $this->input->post('material_id');
		    $product_id = $this->input->post('product_id');
	 		$product = product_id($product_id);
	 		$material_id = material_id($material_ids);

			$qty = $this->input->post('qty');
			$production_id = $this->input->post('machine_id');
			$unit = $this->input->post('unit');
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			
			$insert_date = $this->input->post('insert_date');

		$condition  = $this->Stocks->get_material_production_for_condition($material_id);
		
		 if($condition !=""){

		 	$dataarray =  array('product_id' => $product,
							'material_id' => $material_id,
							'production_id' => $production_id,
							'insert_date' => $insert_date,
							'qty'=>$qty,
							'unit' => $unit,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

	     	$result = $this->Stocks->add_production_mat_out($dataarray);
	       if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
	        echo json_encode($add_out);
			}else{

				echo "Stock Not Available..!";
			}

		}

	}

	
	/*public function return_production_balance(){
	check_login();


	if($this->input->post())
	{
		$product_id = $this->input->post('product_id');
		$material_id = $this->input->post('material_id');	
		$machine_id = $this->input->post('machine_id');
		$insert_date = $this->input->post('insert_date');
	
		$r_qty = $this->input->post('r_qty');
		$balance = $this->input->post('balance');
		$last_blance = $this->input->post('last_blance');
		
		$user_id = $this->input->post('user_id');
		$created = $this->input->post('created');
		$modified = $this->input->post('modified');

		$condition  = $this->Stocks->getproduction_stock($material_id,$machine_id,$product_id);
		
		 if($condition !="" && $condition[0]['balance'] !=0){

		 

	     	$condi  = $this->Stocks->get_lastqty_production_stock($material_id,$machine_id,$product_id);

			$total = $condi[0]['balance']-$r_qty;
			
		 	$updateqty = array('balance' => $total);

		 	$updateqty_log = array(

		 						'product_id' => $product_id,
		 						'material_id' => $material_id,
								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								'last_blance' => $last_blance,
								'balance' => $total,
		 						'return'=>$r_qty,
		 						'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
		 							);

	     	$result = $this->Stocks->update_qty_production($updateqty,$material_id,$machine_id,$product_id);


	     	$result = $this->Stocks->add_qty_production_log($updateqty_log);


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

				echo "Stock Not Available..!";
			}

		}

	}*/


public function get_material_code(){

		check_login();
		if($this->input->post())
			{
				$material_id = $this->input->post('material_id');
				//print_r($product_id);
				//die;
				$data1 = $this->Stocks->get_material_id_details($material_id);
				$data2 = $this->Stocks->get_total_material($material_id);
				$data3 = $this->Stocks->get_total_out_material($material_id);
				$data['codes'] = $data1[0]['material_code'];
				if($data2 =="")
				{	
				    $data['total_qty'] = 0;
				}else{

					$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];

				}
				/*print_r($data['total_qty']);
				die;*/
				$data['type'] = $data1[0]['type'];
				echo json_encode ($data);
			}
		}


		
		public function get_product_from_production(){

		check_login();
		if($this->input->post())
			{
				$production_id = $this->input->post('machine_id');
				
				$data1 = $this->Stocks->get_product_details_productions($production_id);

				$data['product'] = $data1[0]['product_name'];

				$data['job'] = $data1[0]['job'];

				$data['material_id'] = material_name($data1[0]['material_id']);
				$material_id = material_id($data['material_id']);
				$data2 = $this->Stocks->production_stock($material_id);
				$data3 = $this->Stocks->production_stock_return($material_id);

				
				if($data2 =="")
				{	
				    $data['total_qty'] = 0;

				}else{

					$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];

				}

				echo json_encode ($data);
			}
		}
		
	
		public function get_product_code_production(){

		check_login();
		if($this->input->post())
			{
				$material_id = $this->input->post('material_id');
				$machine_id = $this->input->post('machine_id');
				$product_id = $this->input->post('product_id');
				//print_r($product_id);
				//die;
				//$data1 = $this->Stocks->get_material_id_details($material_id);
				$data2 = $this->Stocks->get_total_material_last_balance($material_id,$machine_id,$product_id);
				//$data3 = $this->Stocks->get_total_out_material($material_id);
				//$data['codes'] = $data1[0]['material_code'];
				if($data2 =="" )
				{	

					
				    $data['last_blance'] = 0;
				}else{

					$data['last_blance'] = $data2[0]['last_blance'];

				}
				/*print_r($data['total_qty']);
				die;*/
				//$data['type'] = $data1[0]['type'];
				echo json_encode ($data);
			}
		}
		
		public function get_material_code_packing(){

		check_login();
		if($this->input->post())
			{
				$material_id = $this->input->post('material_id');
				//print_r($product_id);
				//die;
				$data1 = $this->Stocks->get_material_id_details($material_id);
				$data2 = $this->Stocks->get_total_material($material_id);
				$data3 = $this->Stocks->get_total_out_packing($material_id);
				$data['codes'] = $data1[0]['material_code'];
				if($data2 =="")
				{	
				    $data['total_qty'] = 0;
				}else{

					$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];

				}
				/*print_r($data['total_qty']);
				die;*/
				$data['type'] = $data1[0]['type'];
				echo json_encode ($data);
			}
		}


		public function get_color_code(){

		check_login();
		if($this->input->post())
			{
				$color_id = $this->input->post('color_id');
				//print_r($product_id);
				//die;
				$data1 = $this->Stocks->get_color_id_details($color_id);
				$data2 = $this->Stocks->get_total_color($color_id);
				$data3 = $this->Stocks->get_total_out_color($color_id);
				$data['codes'] = $data1[0]['code'];
				if($data2 =="")
				{	
				    $data['total_qty'] = 0;
				}else{

					$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];

				}
				/*print_r($data['total_qty']);
				die;*/
				//$data['type'] = $data1[0]['type'];
				echo json_encode ($data);
			}
		}

		
		public function get_wast_qty(){

		check_login();
		if($this->input->post())
			{
				$material_id = $this->input->post('material_id');
				//print_r($product_id);
				//die;
				$data1 = $this->Stocks->get_material_id_details($material_id);
				$data2 = $this->Stocks->get_total_material_wast($material_id);
				$data3 = $this->Stocks->get_total_out_material_wast($material_id);
				$data['codes'] = $data1[0]['material_code'];
				if($data2 =="")
				{	
				    $data['total_qty'] = 0;
				}else{

					$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];
					$data['bags'] = $data['total_qty']/10;
				}


				/*print_r($data['total_qty']);
				die;*/
				//$data['type'] = $data1[0]['type'];
				echo json_encode ($data);
			}
		}


		

		public function get_machine_code_production(){

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

				
				/*$machine_id = machine_from_production($production_id);*/
				
				$data4 = $this->Stocks->get_total_machine_wast($machine_id);
				
				$data['total_qty'] = $data4[0]['total_qty'];
				$data['wast_lot'] = $data4[0]['wast_lot'];
				
				echo json_encode ($data);
			}
		}



		public function get_waste_out_production(){

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

				$data3 = $this->Stocks->getfgstock_record_waste($material_id);
				
				$data4 = $this->Stocks->get_total_production_wast($material_id);

				
				
				$data['total_qty'] = $data3[0]['qty']-$data4[0]['total_qty'];
  

				$data['wlot'] = $data3[0]['wast_lot'];
	
				echo json_encode ($data);
			}
		}



		public function get_material_job_production(){

		check_login();
		if($this->input->post())
			{
				$product_id = $this->input->post('product_id');
				//$p_name = product_name($product_id);

				/*print_r($p_name);
				die;*/
				
				$data['all_data'] = $this->Stocks->get_data_from_production_job($product_id);

				/*print_r($data['all_data'][0]['machine']);
				die;*/
				echo json_encode (array('html'=>$this->load->view('Stock/job_details_compund',$data,true)));

				/*$data['material_id'] = $data3[0]['material_id'];

				$data['bom_qty'] = $data3[0]['bom_qty'];

				$data['bom_unit'] = $data3[0]['bom_unit'];

				$data['machine'] = $data3[0]['machine'];*/
			/*	echo json_encode ($data);*/
			}
		}


		public function get_material_code_production(){

		check_login();
		if($this->input->post())
			{
				$material_id = $this->input->post('material_id');
				//print_r($product_id);
				//die;
				$data1 = $this->Stocks->get_material_id_details($material_id);
				$data2 = $this->Stocks->get_total_material($material_id);
				$data3 = $this->Stocks->get_material_code_production($material_id);

				$data['codes'] = $data1[0]['material_code'];
				if($data2 =="")
				{	
				    $data['total_qty'] = 0;
				}else{

					$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];

				}

				$data['type'] = $data1[0]['type'];
				echo json_encode ($data);
			}
		}

		

		public function get_color_code_production(){

		check_login();
		if($this->input->post())
			{
				$material_id = $this->input->post('color_code');
				
				$data2 = $this->Stocks->get_total_color_stock($material_id);
				$data3 = $this->Stocks->get_material_code_production($material_id);

				
				if($data2 =="")
				{	
				    $data['total_qty_color'] = 0;
				}else{

					$data['total_qty_color'] = $data2[0]['total_qty_color'] - $data3[0]['total_qty_out'];

				}

				
				echo json_encode ($data);
			}
		}



		public function get_material_stock_production(){

		check_login();
		if($this->input->post())
			{
				$material_id = $this->input->post('material_id');
				//print_r($product_id);
				//die;
				$data1 = $this->Stocks->get_material_id_details($material_id);
				$data2 = $this->Stocks->get_total_material($material_id);
				$data3 = $this->Stocks->get_total_out_material($material_id);
				$data4 = $this->Stocks->production_compund_stock_out($material_id);


				$data['codes'] = $data1[0]['material_code'];
				if($data2 =="")
				{	
				    $data['total_qty'] = 0;
				}else{
						
					$data['total_qty'] = $data2[0]['total_qty']-$data4[0]['pro_comp_stockout'];

				}

				$data['type'] = $data1[0]['type'];
				echo json_encode ($data);
			}
		}




	public function get_product_code(){

		check_login();
		if($this->input->post())
			{
				$product_id = $this->input->post('product_id');
				/*print_r($pro);
				die;*/
				$data1 = $this->Stocks->get_product_details($product_id);
				$data2 = $this->Stocks->get_total11($product_id);
				$data3 = $this->Stocks->get_total_out($product_id);
				$data['codes'] = $data1[0]['pro_code'];
				if($data2 =="" )
				{	
				    $data['total_qty'] = 0;
				}else{

					$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];

				}

				if($data['total_qty'] <= 1){

						 $data['total_qty'] = 0;
					 	 echo json_encode ($data);
				}else{



					$data['type'] = $data1[0]['product_type'];
				   echo json_encode ($data);

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

		public function get_inout_production(){

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

				  if($in_out == 1){

				  	$data['get_machine']=$this->Stocks->get_products_semi_production();

					echo json_encode (array('html'=>$this->load->view('Stock/semi_in_production_data',$data,true)));

				  }else if($in_out == 0){


				  	$data['get_products']=$this->Stocks->get_products_semi_production_product();

				 
				  	$data['type']=$this->Stocks->get_products_semi_type();

				  	$data['fg_product']=$this->Stocks->fg_product();
					echo json_encode (array('html'=>$this->load->view('Stock/semi_out_production_data',$data,true)));

				  }
				  
				}

			}


	public function get_semi_production_production_id(){

	    check_login();
		if($this->input->post())
		{
		    $machine = $this->input->post('machine');
		  
		    $data21=$this->Stocks->get_so_production_production_id($machine);

			 
			//echo json_encode (array('html'=>$this->load->view('Stock/get_so_production_production_id',$data,true)));
				 $p_name =  product_name($data21[0]['product_id']);
			 	 $so_id = $data21[0]['so_id'];


			 	 $data211=$this->Stocks->get_so_production_production_data($so_id,$p_name);

			 	 /*print_r($data211[0]['product_name']);
			 	 die;*/
			 	 $product_id =  product_id($data211[0]['product_name']);


				 $data1 = $this->Stocks->get_product_details($product_id);
				 $data2 = $this->Stocks->get_total($product_id,$so_id);
				 $data2112 = $this->Stocks->get_total11($product_id); 
				 $data3 = $this->Stocks->get_total_out($product_id);
			     $data['codes'] = $data1[0]['pro_code'];


				if($data2 =="" )
				{	
				    $data['total_qty'] = 0;
				}else{

					$data['total_qty'] = $data2112[0]['total_qty'] - $data3[0]['total_qty_out'];

				}

				if($data['total_qty'] <= 1){

					 $data['total_qty'] = 0;

					 $product_ids =  product_id($data211[0]['product_name']);
			 		$data121=$this->Stocks->get_complete_semi_productions($product_ids);
			 		$data['p_qty'] = $data211[0]['p_qty']-$data2[0]['total_qty'];

			 		/*print_r($data2[0]['total_qty']);
			     	die;*/

					$data['product_name'] = $data211[0]['product_name'];
					$data['job'] = $data211[0]['job'];

					
					
					$data['type'] = get_child_pro_type($product_ids);

					$data['so_id'] = $data21[0]['so_id'];
					


					 	 echo json_encode ($data);
				}else{

					$product_ids =  product_id($data211[0]['product_name']);
			 		$data121=$this->Stocks->get_complete_semi_productions($product_ids);
			 		$data['p_qty'] = $data211[0]['p_qty']-$data2[0]['total_qty'];

			 		/*if( $data211[0]['p_qty'] < $data121[0]['total']){

			 			$data['p_qty']=0;

			 		}else{

			 			$data['p_qty'];
			 		}*/

			 		
					$data['product_name'] = $data211[0]['product_name'];
					$data['job'] = $data211[0]['job'];

					$data['total_qty'] = $data2112[0]['total_qty'] - $data3[0]['total_qty_out'];

					$data['so_id'] = $data21[0]['so_id'];
					
					$data['type'] = get_child_pro_type($product_ids);
         		   echo json_encode ($data);

				}

/*
			 echo json_encode ($data);
		*/
		}
	}


	public function get_design(){
			check_login();
		$data['id'] = $this->input->post('mode');
		if($data['id']==1){
		$data['get_products']=$this->Stocks->get_products_fg();
		echo json_encode (array('html'=>$this->load->view('Stock/fg_get_mode',$data,true)));
		}else if($data['id']==2){
			
			echo json_encode (array('html'=>$this->load->view('Stock/semi_get_mode',$data,true)));
		}else if($data['id']==3){
			$data['get_packing']=$this->Stocks->get_products_packing();
			echo json_encode (array('html'=>$this->load->view('Stock/packing_get_mode',$data,true)));
		}else if($data['id']==4){
			$data['get_raw']=$this->Stocks->get_products_raw_material();
			echo json_encode (array('html'=>$this->load->view('Stock/raw_material_get_mode',$data,true)));
		}else if($data['id']==7){
			$so_id = $this->input->post('so_id');
			$data['get_production']=$this->Stocks->get_products_raw_material();
			$data['get_machine']=$this->Stocks->get_products_machine();
			$data['get_production_plan']=$this->Stocks->get_plan_data($so_id);

			$data['get_production_job']=$this->Stocks->get_production_job();

			echo json_encode (array('html'=>$this->load->view('Stock/compound_production_get_mode',$data,true)));
		}else if($data['id']==5){
			
			$data['get_color']=$this->Stocks->get_color();
			echo json_encode (array('html'=>$this->load->view('Stock/color_get_mode',$data,true)));
		}else if($data['id']==6){
			
			$data['get_raw_wast']=$this->Stocks->get_products_raw_material();
			$data['get_machine']=$this->Stocks->get_products_machine();

			echo json_encode (array('html'=>$this->load->view('Stock/wast_get_mode',$data,true)));
		}else if($data['id']==8){
			
			//$data['get_products_semi']=$this->Stocks->get_products_semi();
			//$data['get_material']=$this->Stocks->get_material();
			$data['get_machine']=$this->Stocks->get_products_machine();
			$data['get_raw']=$this->Stocks->get_products_raw_material();
			echo json_encode (array('html'=>$this->load->view('Stock/production_report_mode',$data,true)));
		}else if($data['id']==10){
			
			//$data['get_products_semi']=$this->Stocks->get_products_semi();
			//$data['get_material']=$this->Stocks->get_material();
			$data['get_machine']=$this->Stocks->get_production_job();
			$data['get_unit']=$this->Stocks->get_unit();
			$data['get_raw']=$this->Stocks->get_products_raw_material_production();
			echo json_encode (array('html'=>$this->load->view('Stock/return_production_report_mode1',$data,true)));
		}

		/*else if($data['id']==9){
			
			$data['get_products_semi']=$this->Stocks->get_products_semi();
			//$data['get_material']=$this->Stocks->get_material();
			$data['get_machine']=$this->Stocks->get_products_machine();
			$data['get_raw']=$this->Stocks->get_products_raw_material();
			echo json_encode (array('html'=>$this->load->view('Stock/return_production_report_mode',$data,true)));
		}*/
		
	}



	public function stock_report(){

	check_login();
	if($this->input->post())
		{
		  $mode = $this->input->post('mode');
		  
		if($mode==1){
		$data['get_report_fg']=$this->Stocks->get_products_fg_reports();
		echo json_encode (array('html'=>$this->load->view('Stock/fg_get_report',$data,true)));
		}else if($mode==2){
			$data['get_products_semi']=$this->Stocks->get_products_semi_reports();
			//$data['get_products_semi1']=$this->Stocks->get_products_semi_reports1();
			echo json_encode (array('html'=>$this->load->view('Stock/semi_get_report',$data,true)));
		}else if($mode==3){
			$data['get_products_packing']=$this->Stocks->get_products_packing_reports();
			//$data['get_products_semi1']=$this->Stocks->get_products_semi_reports1();
			echo json_encode (array('html'=>$this->load->view('Stock/get_products_packing_report',$data,true)));
		}else if($mode==4){
			$data['get_products_raw']=$this->Stocks->get_products_raw_reports();
			//$data['get_products_semi1']=$this->Stocks->get_products_semi_reports1();
			echo json_encode (array('html'=>$this->load->view('Stock/get_products_raw_report',$data,true)));
		}else if($mode==6){
			$data['get_products_production']=$this->Stocks->get_products_production_reports();
			//$data['get_products_semi1']=$this->Stocks->get_products_semi_reports1();
			/*print_r($data['get_products_production']);
			die;*/
			echo json_encode (array('html'=>$this->load->view('Stock/get_products_production_reports',$data,true)));
		}else if($mode==5){
			$data['get_products_color']=$this->Stocks->get_products_color_reports();
			//$data['get_products_semi1']=$this->Stocks->get_products_semi_reports1();
			echo json_encode (array('html'=>$this->load->view('Stock/get_products_color_reports',$data,true)));
		}else if($mode==7){
			$data['get_products_wast']=$this->Stocks->get_products_wast();

			//$data['get_products_semi1']=$this->Stocks->get_products_semi_reports1();
			echo json_encode (array('html'=>$this->load->view('Stock/get_products_wast_reports',$data,true)));
		}else if($mode==8){
			$data['get_material_production_reports']=$this->Stocks->get_material_production_reports();

			//$data['get_products_semi1']=$this->Stocks->get_products_semi_reports1();
			echo json_encode (array('html'=>$this->load->view('Stock/get_material_production_reports',$data,true)));
		}


		}else{

		$this->load->view('Stock/stock_report');
		}
	}


    public function report_semi_fg(){

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

		if($mode==1){
		$data['get_report_fg']=$this->Stocks->get_products_fg_reports();
		echo json_encode (array('html'=>$this->load->view('Stock/fg_get_report',$data,true)));
		}else if($mode==2){
			
			$data['get_products_semi']=$this->Stocks->get_products_semi_reports();
			//$data['get_products_semi1']=$this->Stocks->get_products_semi_reports1();
			//echo json_encode (array('html'=>$this->load->view('Stock/report_semi_fg',$data,true)));
			$this->load->view('Stock/report_semi_fg',$data);
		}else if($mode==7){

			$data['get_products_production']=$this->Stocks->get_products_production_reports();
			$this->load->view('Stock/report_production_report',$data);

		}
	
	}


	
	

public function update_stock()
	{


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

				 $mode = $this->input->post('mode');
			  	 $in_out = $this->input->post('in_out');
			  	 if($mode == 2 && $in_out == 1){
				$data['get_recods_update']=$this->Stocks->get_recods_update($in_out);
				echo json_encode (array('html'=>$this->load->view('Stock/get_recods_update',$data,true)));
				}if($mode == 2 && $in_out == 2){

					$data['get_recods_update_out']=$this->Stocks->get_recods_update_out();
				echo json_encode (array('html'=>$this->load->view('Stock/get_recods_update_out',$data,true)));

			}if($mode == 3 && $in_out == 1){

					$data['get_recods_packing_in']=$this->Stocks->get_recods_packing_in();
				echo json_encode (array('html'=>$this->load->view('Stock/get_recods_packing_in',$data,true)));

			}if($mode == 3 && $in_out == 2){

					$data['get_recods_packing_out']=$this->Stocks->get_recods_packing_out();
				echo json_encode (array('html'=>$this->load->view('Stock/get_recods_packing_out',$data,true)));

			}if($mode == 4 && $in_out == 1){

					$data['get_recods_packing_in']=$this->Stocks->get_recods_packing_in();
				echo json_encode (array('html'=>$this->load->view('Stock/get_recods_packing_in',$data,true)));

			}if($mode == 5 && $in_out == 1){

					$data['get_recods_color_in']=$this->Stocks->get_recods_color_in();
				echo json_encode (array('html'=>$this->load->view('Stock/get_recods_color_in',$data,true)));

			}if($mode == 5 && $in_out == 2){

					$data['get_recods_color_out']=$this->Stocks->get_recods_color_out();
				echo json_encode (array('html'=>$this->load->view('Stock/get_recods_color_out',$data,true)));

			}if($mode == 6 && $in_out == 1){

					$data['get_recods_waste_in']=$this->Stocks->get_recods_waste_in();
				echo json_encode (array('html'=>$this->load->view('Stock/get_recods_waste_in',$data,true)));

			}if($mode == 6 && $in_out == 2){

					$data['get_recods_waste_out']=$this->Stocks->get_recods_waste_out();
				    echo json_encode (array('html'=>$this->load->view('Stock/get_recods_waste_out',$data,true)));

			}if($mode == 7 && $in_out == 2){

					$data['get_recods_production']=$this->Stocks->get_recods_production();
				    echo json_encode (array('html'=>$this->load->view('Stock/get_recods_production',$data,true)));

			}


		}else{

		$this->load->view('Stock/update_stock');
		}
	}


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

		  
		 	$waste  = get_log_stock_wast($id);

		 	$get_log_qty = get_log_stock_wast($id);

		 	$get_qty_fgstock = $this->Stocks->getfgstock_record_waste($waste);

		 	$total = $get_qty_fgstock[0]['qty']-$get_log_qty;


		 	$dataarray =  array('qty' => $total);
		 	
		 	$result=$this->Stocks->update_fgstock_waste($waste,$dataarray);
		
			$result=$this->Stocks->delete_record_waste($id);
				
			if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
			echo json_encode($empolyee);
		
				
	}


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

		  
		 	$pro  = get_log_stock_pro($id);

		 	$get_log_qty = log_qty($id);

		 	$get_qty_fgstock = $this->Stocks->getfgstock_record_semi($pro);

		 	$total = $get_qty_fgstock[0]['qty']-$get_log_qty;

		 	

		 	$dataarray =  array('qty' => $total);
		 	
		 	$result=$this->Stocks->update_fgstock_semi($pro,$dataarray);
		
			$result=$this->Stocks->delete_record_semi($id);
				
			if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
			echo json_encode($empolyee);
		
				
	}

	

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

		  
		 	$mat  = get_log_stock_mat($id);

		 	$get_log_qty = log_qty($id);

		 	$get_qty_fgstock = $this->Stocks->getfgstock_record_packing($mat);

		 	$total = $get_qty_fgstock[0]['qty']-$get_log_qty;

		 	

		 	$dataarray =  array('qty' => $total);
		 	
		 	$result=$this->Stocks->update_fgstock_packing($mat,$dataarray);
		
			$result=$this->Stocks->delete_record_packing($id);
				
			if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
			echo json_encode($empolyee);
		
				
	}



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

		  
		 	$color  = get_log_stock_color($id);

		 	$get_log_qty = log_qty($id);

		 	$get_qty_fgstock = $this->Stocks->getfgstock_record_color($color);

		 	$total = $get_qty_fgstock[0]['qty']-$get_log_qty;

		 	

		 	$dataarray =  array('qty' => $total);
		 	
		 	$result=$this->Stocks->update_fgstock_color($color,$dataarray);
		
			$result=$this->Stocks->delete_record_color($id);
				
			if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
			echo json_encode($empolyee);
		
				
	}
	

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

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


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

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


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

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


	

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

		  
		
			$result=$this->Stocks->delete_record_production($id);
				
			if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
			echo json_encode($empolyee);
		
				
	}
	
	public function delete_record_color_out($id)
	{
		 check_login();

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

	

		public function match_batch()
		{
			 check_login();

			  $batch = $this->input->post('batch');
			
				$result=$this->Stocks->match_batch($batch);
					
				
				if($result){$empolyee=array('status'=>'true');}else{$empolyee=array('status'=>'false');}
				echo json_encode($empolyee);
			
					
		}


	

	public function reporting(){

	    check_login();
		if($this->input->post())
		{
		    $start = $this->input->post('start');
		    $end = $this->input->post('end');
			$in_out = $this->input->post('in_out');
		   if($in_out==1){

		    $data['reporting_data']=$this->Stocks->reporting_data($start,$end);


			echo json_encode (array('html'=>$this->load->view('Stock/reporting_data',$data,true)));

		}else if($in_out==2){

			$data['reporting_data']=$this->Stocks->reporting_data_out($start,$end);


			echo json_encode (array('html'=>$this->load->view('Stock/reporting_data_out',$data,true)));

		}else if($in_out==""){

			echo json_encode (array('Select option'));
		}

		}else{
		$this->load->view('Stock/reporting');

			}
		}


		
		public function get_so_production_products(){

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

			$data['get_so_production_products']=$this->Stocks->get_so_production_products($so_id);


			echo json_encode (array('html'=>$this->load->view('Stock/get_so_production_products',$data,true)));

		
		}
	}


	

	public function get_so_production_production_id(){

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

			$data['get_so_production_production_id']=$this->Stocks->get_so_production_production_id($production_id);


			echo json_encode (array('html'=>$this->load->view('Stock/get_so_production_production_id',$data,true)));

		
		}
	}

		
		public function stock_fg_delete_data($id){

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

	}


	public function assembled_line(){

		check_login();

		$data['assembled_line']=$this->Stocks->get_assembled_line();

		$this->load->view('Stock/assembled_line',$data);


	}


	
	public function sealing_line(){

		check_login();

		$data['sealing_line']=$this->Stocks->get_sealing_line();

		$this->load->view('Stock/sealing_line',$data);


	}


	
	public function packing_fg_line(){

		check_login();

		$data['packing_fg_line']=$this->Stocks->packing_fg_line();

		$this->load->view('Stock/packing_fg_line',$data);


	}


	public function printing_line(){

		check_login();

		$data['printing_line']=$this->Stocks->printing_line();

		$this->load->view('Stock/printing_line',$data);


	}


	public function get_product_bom_molded_items(){

		check_login();
		if($this->input->post())
			{
				$fg_product = $this->input->post('fg_product');
				
				
				$data['all_data'] = $this->Stocks->get_molded_items_bom($fg_product);

				/*print_r($data['all_data']);
				die;*/
				echo json_encode (array('html'=>$this->load->view('Stock/get_boms_semi_data',$data,true)));

			
			}
		}


public function out_stock_from_form(){
	check_login();


	if($this->input->post())
	{
		
		
		$material_id = $this->input->post('material_id');	
		$insert_date = $this->input->post('insert_date');
		
		$quantity = $this->input->post('qty');
	
		$remarks = $this->input->post('remarks');
		$user_id = $this->input->post('user_id');
		$created = $this->input->post('created');
		$modified = $this->input->post('modified');
	
		$condition  = $this->Stocks->get_material_for_condition($material_id);
		
		 if($condition !=""){

		 	

		 	$dataarray =  array(
		 						
		 						'material_id' => $material_id,
								
								'insert_date' => $insert_date,
								
								'qty'=>$quantity,
								
								'remarks'=> $remarks,
								'user_id'=> $user_id,
								'created'=> $created,
								'modified'=> $modified
								 );

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

	     	$result = $this->Stocks->add_out_stock_production($dataarray);

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


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

	
}
