<?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();
	      	
				$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);
     	
     	$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;

		

		$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 semi_add_stock(){
		$condition  = $this->Stocks->get_product_for_condition($product_id);
		if($condition ==""){
			$product_id = product_id($this->input->post('product_id'));
			$qty =  str_replace(',', '', $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');
			$batch = $this->input->post('batch');
			$insert_date = $this->input->post('insert_date');
			$so_id = $this->input->post('so_id');
			$emp_id = $this->input->post('emp_id');
			$m_weight = $this->input->post('m_weight');
			$job = $this->input->post('job');
			$w_weight = $this->input->post('w_weight');
			$moving_to = $this->input->post('moving_to');
			$shift = $this->input->post('shift');
			$qty_b = $this->input->post('qty_b');
			$p_name = product_name($product_id);
			$qty_production = qty_production($p_name,$so_id);
			$qty_in_stock_log = qty_in_stock_log($product_id,$so_id);
			$all_spare = get_all_spare($so_id,$product_id);
				if($all_spare >= 100){
				$result = $this->Stocks->update_stock_status($product_id,$so_id);
				}
				$fg_log_qty = $qty_in_stock_log+$qty;
				$explo = explode('.', $qty_b);
				$spare_pcs= @$explo[1];
				$save_data =  array(
					'product_id' => $product_id,
					'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' => $product_id,
							'type' => $type,
							'batch' => $batch,
							'emp_id' => $emp_id, 
							'm_weight' => $m_weight,
							'w_weight' => $w_weight,
							'qty_b' => $qty_b,
							'pcs_status' => 1,
							'qty' => $qty,
							'spare' => $spare_pcs,
							'shift'=> $shift,
							'moving_to' => $moving_to,
							'job' => $job,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );


		
     	

     	if($qty_production == $fg_log_qty){

		

			$result = $this->Stocks->update_machine_status($product_id,$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);*/

		}

		$result = $this->Stocks->add_fg($save_data);
     	
     	$result1 = $this->Stocks->add_fg_log($save_log);
     	
     /*	print_r($result1);
     	die;*/
     	
       if($result){$add_fg=array('data'=>$result1);}else{$add_fg=array('status'=>'false');}
        echo json_encode($add_fg);

	}


	public function fg_stock1(){
	check_login();


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

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

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

	$qty =  str_replace(',', '', $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 = product_id($this->input->post('product_id'));
			$qty =  str_replace(',', '', $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');
			$batch = $this->input->post('batch');
			$insert_date = $this->input->post('insert_date');
			$so_id = $this->input->post('so_id');
		    $emp_id = $this->input->post('emp_id');
			$m_weight = $this->input->post('m_weight');
            $job = $this->input->post('job');
			$w_weight = $this->input->post('w_weight');
			$moving_to = $this->input->post('moving_to');
			$shift = $this->input->post('shift');
			$qty_b = $this->input->post('qty_b');
		   $p_name = product_name($product_id);
		   $qty_production = qty_production($p_name,$so_id);
		   $qty_in_stock_log = qty_in_stock_log($product_id,$so_id);


				   
					$all_spare = get_all_spare($so_id,$product_id);

					if($all_spare >= 100){

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

				 $fg_log_qty = $qty_in_stock_log+$qty;

				 	$explo = explode('.', $qty_b);
					$spare_pcs= @$explo[1];

					/*$bags =intval($qty);
*/
					

		$save_data =  array(
							'product_id' => $product_id,
							'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' => $product_id,
							'type' => $type,
							'batch' => $batch,
							'emp_id' => $emp_id, 
							'm_weight' => $m_weight,
							'w_weight' => $w_weight,
							'qty_b' => $qty_b,
							'pcs_status' => 1,
							'qty' => $qty,
							'spare' => $spare_pcs,
							'shift'=> $shift,
							'moving_to' => $moving_to,
							'job' => $job,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );


		
     	

     	if($qty_production == $fg_log_qty){

		

			$result = $this->Stocks->update_machine_status($product_id,$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);*/

		}

		$result = $this->Stocks->add_fg($save_data);
     	
     	$result1 = $this->Stocks->add_fg_log($save_log);
     	
     /*	print_r($result1);
     	die;*/
     	
       if($result){$add_fg=array('data'=>$result1);}else{$add_fg=array('status'=>'false');}
        echo json_encode($add_fg);


		}else {




			$qty_add = $this->input->post('total_qty');
			$qty =  str_replace(',', '', $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');
			$batch = $this->input->post('batch');
			$so_id = $this->input->post('so_id');
			$emp_id = $this->input->post('emp_id');
			$m_weight = $this->input->post('m_weight');
			$w_weight = $this->input->post('w_weight');
			$moving_to = $this->input->post('moving_to');
			$shift = $this->input->post('shift');
			$qty_b = $this->input->post('qty_b');
			$product_id = product_id($this->input->post('product_id'));
			$p_name = product_name($product_id);
            $job = $this->input->post('job');
			
			$qty_production = qty_production($p_name,$so_id);
			$qty_in_stock_log = qty_in_stock_log($product_id,$so_id);


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


		    $fg_log_qty = $qty_in_stock_log+$qty;

		    $all_spare = get_all_spare($so_id,$product_id);

		     if($all_spare >= 100){

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

		    $explo = explode('.', $qty_b);
					$spare_pcs= @$explo[1];

					//$bags =intval($qty);

			

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

		$save_log =  array(
							'so_id' => $so_id,
							'product_id' => $product_id,
							'type' => $type,
							'batch' => $batch,
							'emp_id' => $emp_id, 
							'm_weight' => $m_weight,
							'w_weight' => $w_weight,
							'pcs_status' => 1,
							'qty_b' => $qty_b,
							'qty' => $qty,
							'spare' => $spare_pcs,
							'shift' => $shift,
							'moving_to'=>$moving_to,
							'job' => $job,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );
	 	
		//


		if($qty_production == $fg_log_qty){

			

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

			

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

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

		}

		$result = $this->Stocks->update_stock($product_id,$dataarray);
		$result1 = $this->Stocks->add_fg_log($save_log);

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

		}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 fg_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 == ""){
	$condition  = $this->Stocks->get_product_for_condition($product_id);
	
	 if($condition ==""){

	 		$product_id = $this->input->post('product_id');
			$qty = $this->input->post('qty');
			$type = 5;
			$remarks = $this->input->post('remarks');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');
			$batch = $this->input->post('batch');
			$qc_ref = $this->input->post('qc_ref');
			$carton_weight = $this->input->post('carton_weight');
			$insert_date = $this->input->post('insert_date');
			

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

		$save_log =  array('product_id' => $product_id,
							'type' => $type,
							'batch' => $batch,
							'qc_ref'=> $qc_ref,
							'carton_weight'=>$carton_weight,
							'qty' => $qty,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );

     	$result = $this->Stocks->add_fg($save_data);
     	
     	$result1 = $this->Stocks->add_fg_log($save_log);
     	
       if($result){$add_fg=array('data'=>$result1);}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');
			$batch = $this->input->post('batch');
			$qc_ref = $this->input->post('qc_ref');
			
			$carton_weight = $this->input->post('carton_weight');
			$all_qty = $condition[0]['qty']+$qty;

		

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

		$save_log =  array('product_id' => $product_id,
							'type' => $type,
							'batch' => $batch,
							'qc_ref' => $qc_ref,
							'carton_weight'=> $carton_weight,
							'qty' => $qty,
							'remarks' => $remarks,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created,
							'modified'=> $modified
							 );
	 	
		$result = $this->Stocks->update_stock($product_id,$dataarray);
		$result1 = $this->Stocks->add_fg_log($save_log);

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

		}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);
     
     	$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;

		

		$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);
     	
     	$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;

			

		$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');	

		$job = $this->input->post('job');
		$so_id = production_get_so_id($job);	
		$production = production_get_id($job);
		
		$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');
		$qty = $this->input->post('qty');
		$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(
		 						'production_id' => $production,
		 						'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
								 );

		 	

	     	$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(
		 						'production_id' => $production,
		 						'so_id' => $so_id,
		 						'product_id' => $product_id,
		 						'material_id' => $material_id,
								'machine_id' => $machine_id,
								'insert_date' => $insert_date,
								'qcr' => $qcr,
								'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(
	     					'production_id' => $production,
	     					'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' => $qty,
						
							'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_semi(){
	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');

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

	 	$dataarray =  array('product_id' => $product_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 out_stock1(){
	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');

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

	 	$dataarray =  array('product_id' => $product_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($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 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[]');
	
	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){

						


							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);



							}
							
						}


						
						
					}
					
						$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);

				

	 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);
     	
     	$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;

		
		$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');
				
				$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'];

				}
			
				$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');
				
				//$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'];

				}
			
				//$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');
				
				$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'];

				}
				
				$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');
				
				$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'];

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

		public function get_sfgprinted_code(){

			check_login();
			if($this->input->post())
				{
					$product_id = $this->input->post('product_id');
					
				
				$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'];

				

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

				

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


			

			public function get_sfgassembled_code(){

				check_login();
				if($this->input->post())
					{
						$product_id = $this->input->post('product_id');
						
					
					$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'];
	
					
	
						$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];
	
					
	
							 $data['type'] = $data1[0]['product_type'];
							  echo json_encode ($data);
					}
				}

				

				public function get_sfgsealed_code(){

					check_login();
					if($this->input->post())
						{
							$product_id = $this->input->post('product_id');
							
						
						$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'];
		
						
		
							$data['total_qty'] = $data2[0]['total_qty'] - $data3[0]['total_qty_out'];
		
						
		
								 $data['type'] = $data1[0]['product_type'];
								  echo json_encode ($data);
						}
					}

		
		public function get_wast_qty(){

		check_login();
		if($this->input->post())
			{
				$material_id = $this->input->post('material_id');
				
				$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;
				}


				
				//$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);

			
				
				$data['all_data'] = $this->Stocks->get_data_from_production_job($product_id);

				
				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');
				
				$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');
				
				$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');
			
				$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'] = round($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);

				}
			
				
			}
		}
		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();
                    $data['empolyee']=$this->System->get_empolyee_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)));

				  }if($in_out == 2){


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

					echo json_encode (array('html'=>$this->load->view('Stock/semi_out',$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);
            
            /*print_r($data21);
			 		die;*/
			 
			//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);

			 	
			 	 $product_id =  product_id($data211[0]['product_name']);

			 	 
				 $data1 = $this->Stocks->get_product_details($product_id);
				/* print_r($product_id);
			 			die;*/
				 $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'];


			 		

					$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'];
					
					@$emp_id = get_ramdomlyemp($data211[0]['job'],$data211['product_name']);

					@$data['emp_id'] = fullname($emp_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);
					$emp_id = get_ramdomlyemp($data211[0]['job'],$data211[0]['product_name']);
					$data['emp_id'] = fullname($emp_id);

         		   echo json_encode ($data);

				}

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



	

	public function calculation_recived_qty(){

	    check_login();
		if($this->input->post())
		{
		    $machine = $this->input->post('machine');
		  	$m_weight = $this->input->post('m_weight');
			$job = $this->input->post('job');
			$so_id = $this->input->post('so_id');
			$Product = $this->input->post('Product');

		  
				$pc_weight = get_pc_weight($job,$machine);	

				if($pc_weight ==''){
					$data = 'false';
					echo json_encode ($data);

				}else{

					$product_id = product_id($Product);
					$all_spare = get_all_spare($so_id,$product_id);

					

					if($all_spare >= 100){

						$alltotal = $m_weight*1000/$pc_weight;
						$all_total_explod = explode('.', $alltotal);

						$bags = $all_total_explod[0];
						$resultbag =  $bags;
						$all_spare_total = $all_spare-100;

						$recived_qty = $resultbag.'.'.$all_spare_total; 

						//$results = number_format($recived_qty);
						$results =  str_replace(',', '', $recived_qty);
						@$data['recived_qty'] = $results;


						$standerd_per = get_production_semi_standrd($job,$so_id);

						$total_spare_bag = $results/$standerd_per;
						$data['semi_standard'] = $total_spare_bag+1;
						/*print_r($data['semi_standard']);
						die;*/

					}else{

						$recived_qty= $m_weight*1000/$pc_weight;
						$results = number_format($recived_qty,2);
						@$data['recived_qty'] =$results;
						$standerd_per = get_production_semi_standrd($job,$so_id);
						if($standerd_per ==''){
								$data = 'semi_standered';
								echo json_encode ($data);
						}
						@$data['semi_standard'] = $recived_qty/$standerd_per;
						
					}

									
				
					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']=$this->Stocks->get_products_semi();
			echo json_encode (array('html'=>$this->load->view('Stock/semi_get_mode_entry',$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']==11){
			
			$data['get_products']=$this->Stocks->get_products_fg();
			echo json_encode (array('html'=>$this->load->view('Stock/onlyprint',$data,true)));
		}else if($data['id']==12){
			
			$data['get_products']=$this->Stocks->get_products_printed();
			echo json_encode (array('html'=>$this->load->view('Stock/sfg_printed',$data,true)));
		}else if($data['id']==13){
			
			$data['get_products']=$this->Stocks->get_products_assembled();
			echo json_encode (array('html'=>$this->load->view('Stock/assembled_sfg',$data,true)));
		}else if($data['id']==14){
			
			$data['get_products']=$this->Stocks->get_products_sealed();
			echo json_encode (array('html'=>$this->load->view('Stock/sealed_sfg',$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();
		//print_r($data['get_report_fg']);
		//die;
		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();
			
			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 onlyprintslip(){

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

		
		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 release_batch_line_clerence_printing(){

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

			$printed_id = $this->input->post('printed_id');
			
			$result = $this->Stocks->update_release_batch_lineclerence($printed_id);

			$result = $this->Stocks->release_batch_lineclerence($_POST);

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

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

	}

	
	
	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 printing_line_machine(){

		check_login();

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

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

	}

	public function sealed_line_machine(){

		check_login();

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

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

	}

	

	public function add_machine(){

		check_login();
		$id = $this->uri->segment('3');
		$data['machine']=$this->Stocks->other_machine();
		$data['machines']=$this->Stocks->update_printing_machine($id);
			
		$this->load->view('Stock/add_machine',$data);

	}


	public function add_machine_sealing(){

		check_login();
		$id = $this->uri->segment('3');
		$data['machine']=$this->Stocks->other_machine_sealing();
		$data['machines']=$this->Stocks->update_sealing_machine($id);
			
		$this->load->view('Stock/add_machine_sealing',$data);

	}

	
	public function assign_machine_printing(){

		check_login();
		$machine = $this->input->post('machine');
		@$check = $this->Stocks->check_machine_status($machine);
		
		if(@$check[0]['status'] == 2){
		
			if($check){$add_mixing=array('status'=>'machine_inprogress');}
	            echo json_encode($add_mixing);

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

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

			$user_id = $this->input->post('user_id');
			$insert_date = $this->input->post('insert_date');
			$printed_id = $this->input->post('id');
			$dataarray1 =  array(
				
			   'machine' => $machine,
			   'insert_date' => $insert_date,
				'status' => 2,
			   'user_id'=> $user_id
			 
				);
			
			$result = $this->Stocks->update_assign_machine($printed_id,$dataarray1);

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

	}
}


public function assign_machine_sealing(){

	check_login();
	$machine = $this->input->post('machine');
	@$check = $this->Stocks->check_machine_status($machine);
	
	if(@$check[0]['status'] == 2){
	
		if($check){$add_mixing=array('status'=>'machine_inprogress');}
			echo json_encode($add_mixing);

	}else{
		

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

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

		$user_id = $this->input->post('user_id');
		$insert_date = $this->input->post('insert_date');
		$sealing_id = $this->input->post('id');
		$dataarray1 =  array(
			
		   'machine' => $machine,
		   'insert_date' => $insert_date,
			'status' => 2,
		   'user_id'=> $user_id
		 
			);
		
		$result = $this->Stocks->update_assign_machine_sealing($sealing_id,$dataarray1);

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

}
}



public function assign_line_assembled(){

	check_login();
	$line = $this->input->post('line');
	@$check = $this->Stocks->check_line_status($machine);
	
	if(@$check[0]['status'] == 2){
	
		if($check){$add_mixing=array('status'=>'machine_inprogress');}
			echo json_encode($add_mixing);

	}else{
		

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

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

		$user_id = $this->input->post('user_id');
		$insert_date = $this->input->post('insert_date');
		$batch = $this->input->post('batch');
		$pro = $this->input->post('pro');
		
		$dataarray1 =  array(
			
		   'line' => $line,
		   'insert_date' => $insert_date,
			'status' => 2,
		   'user_id'=> $user_id
		 
			);

			$batch_no = batch_no($batch);
		
		$result = $this->Stocks->update_assign_assembled_status($batch_no,$dataarray1);

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

}
}


public function transform_page_printing(){

	check_login();

	$id = $this->uri->segment('3');
	if($this->input->post())
		{
			$batch_id = $this->input->post('batch_id');
			$created = $this->input->post('created');
			$user_id = $this->input->post('user_id');
			$transform = $this->input->post('transform');
			$rej_qty = $this->input->post('rej_qty');
			$transform_qty = $this->input->post('transform_qty');
			$item = $this->input->post('item');
			$depart = $this->input->post('depart');
			$total  = get_transform_qty_printing($batch_id,$transform);
			$batch_qty = batch_qty($batch_id);
			$result_qty = $total+$transform_qty;
			
			$batch = batch_no($batch_id);
			
			if($result_qty > $batch_qty){

				$add_mixing=array('status'=>'validate');
				 echo json_encode($add_mixing);
			}else{

				if($result_qty == $batch_qty)
			{

				$dataarray1 =  array(
					'batch' => $batch_id,
					'batch_no' => $batch,
					'product'=> $transform,
					'qty' => $transform_qty,
					
					'status' => 2,
					'user_id'=> $user_id,
					'created'=> $created
					 );

					 $dataarray2 =  array(
						'batch' => $batch_id,
						'transform'=> $transform,
						'type'=> $depart,
						'qty' => $transform_qty,
						'rej_qty' => $rej_qty,
						'created'=> $created
						 );

						 $save_log =  array('product_id' => $transform,
						 'type' => $depart,
						 'batch' => $batch_id,
						 'qty' => $transform_qty,
						 
						 'user_id'=> $user_id,
						 'created'=> $created
						  );

					 
				 $result1 = $this->Stocks->add_fg_log($save_log);	
				 $result = $this->Stocks->add_logs_tranform($dataarray2);
				 $result = $this->Stocks->release_batch_assembled($dataarray1);
				 $result = $this->Stocks->update_transform_machine($batch_id);
				
				if($result){$add_mixing=array('status'=>'true');}else{$add_mixing=array('status'=>'false');}
				 echo json_encode($add_mixing);

			}else{
				if($depart == 3){
					$dataarray1 =  array(
						'batch' => $batch_id,
						'batch_no' => $batch,
						'product'=> $transform,
						'qty' => $transform_qty,
						'status' => 2,
						'user_id'=> $user_id,
						'created'=> $created
						 );
	
						 $dataarray2 =  array(
							'batch' => $batch_id,
							'transform'=> $transform,
							'type'=> $depart,
							'qty' => $transform_qty,
							'rej_qty' => $rej_qty,
							'created'=> $created
							 );
							
							 $dataarray =  array(
								'printing_product'=> $transform,
								
								 );

						 $save_log =  array('product_id' => $transform,
							'type' => $depart,
							'batch' => $batch_id,
							'qty' => $transform_qty,
							
							'user_id'=> $user_id,
							'created'=> $created
							 );

						
					 $result1 = $this->Stocks->add_fg_log($save_log);
					 $result = $this->Stocks->update_transform_product_batch($batch_id,$dataarray);
					 $result = $this->Stocks->add_logs_tranform($dataarray2);
					 $result = $this->Stocks->release_batch_assembled($dataarray1);

				}else if($depart == 4){
					
					
					$dataarray1 =  array(
						'batch' => $batch_id,

						'user_id'=> $user_id,
						'created'=> $created
						 );
	
						 $dataarray2 =  array(
							'batch' => $batch_id,
							'transform'=> $transform,
							'type'=> $depart,
							'qty' => $transform_qty,
							'rej_qty' => $rej_qty,
							'created'=> $created
							 );

							 $dataarray =  array(
								'assembled_product'=> $transform,
								 );

								 $save_log =  array('product_id' => $transform,
								 'type' => $depart,
								 'batch' => $batch_id,
								 'qty' => $transform_qty,
								 
								 'user_id'=> $user_id,
								 'created'=> $created
								  );
	 
							 
					 $result1 = $this->Stocks->add_fg_log($save_log);
					 $result = $this->Stocks->update_transform_product_batch($batch_id,$dataarray);
					 $result = $this->Stocks->add_logs_tranform($dataarray2);
					 $result = $this->Stocks->add_new_batch_sealingmachine($dataarray1);

				}else{
					

				}

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

			}
			}

			
				
					 
		}else{
			$data['transform_data_printing']=$this->Stocks->transform_data_printing($id);
			$data['get_semi']=$this->Stocks->get_semi_product_printing();
			$data['dep']=$this->Master_list->printing_transform_dep();
			$this->load->view('Stock/transform_page_printing',$data);
			}
}








public function transform_page_fg(){

	check_login();

	$id = $this->uri->segment('3');
	if($this->input->post())
		{

			$batch_id = $this->input->post('batch_id');
			$created = $this->input->post('created');
			$user_id = $this->input->post('user_id');
			$transform = $this->input->post('transform');
			$transform_qty = $this->input->post('transform_qty');
			$item = $this->input->post('item');
			$depart = $this->input->post('depart');
			$total  = get_transform_qty($batch_id);
			$batch_qty = batch_qty($batch_id);
			$result_qty = $total+$transform_qty;

			if($result_qty > $batch_qty){

				$add_mixing=array('status'=>'validate');
				 echo json_encode($add_mixing);
				}else{

				if($result_qty == $batch_qty)
			{

				

					 $dataarray2 =  array(
						'batch' => $batch_id,
						'transform'=> $transform,
						'type'=> $depart,
						'qty' => $transform_qty,
						
						'created'=> $created
						 );

				 $result = $this->Stocks->add_logs_tranform($dataarray2);
				 $result = $this->Stocks->update_transform_machine_sealing($batch_id);
				if($result){$add_mixing=array('status'=>'true');}else{$add_mixing=array('status'=>'false');}
				 echo json_encode($add_mixing);

			}else{
				 if($depart == 5){
					
	
						 $dataarray2 =  array(
							'batch' => $batch_id,
							'transform'=> $transform,
							'type'=> $depart,
							'qty' => $transform_qty,
							
							'created'=> $created
							 );
					 $result = $this->Stocks->add_logs_tranform($dataarray2);

				}

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

			}
			}
		
					 
		}else{
			$data['transform_data_fg']=$this->Stocks->transform_data_fg($id);
			$data['get_semi']=$this->Stocks->get_semi_product_fg();
			
			$this->load->view('Stock/transform_page_fg',$data);
			}
}




public function add_fg_stock(){
	check_login();


	if($this->input->post())
	{
	$batch_id = $this->input->post('batch_id');
	$transform_qty = $this->input->post('transform_qty');
	$total  = get_transform_qty($batch_id);
	$batch_qty = batch_qty($batch_id);
	$result_qty = $total+$transform_qty;
	$product_id = $this->input->post('transform');
	$pack  = $this->Stocks->packing_batch();
	
	if($pack[0]['qty'] < $transform_qty){

		$add_mixing=array('status'=>'validate');
		 echo json_encode($add_mixing);
		}else{
		$condition  = $this->Stocks->get_product_for_condition($product_id);
		
		if($condition ==""){

	 		$product_id = $this->input->post('transform');
			$qty = $this->input->post('transform_qty');
			$types = 5;
			$batch_id = $this->input->post('batch_id');
			$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_id,
							'type' => $types,
							'qty' => $qty,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created
							 );

		$save_log =  array('product_id' => $product_id,
							'type' => $types,
							'batch' => $batch_id,
							'qty' => $qty,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created
							 );

     	$result = $this->Stocks->add_fg($save_data);
     	
     	$result1 = $this->Stocks->add_fg_log($save_log);
     	
       if($result){$add_fg=array('data'=>$result1);}else{$add_fg=array('status'=>'false');}
        echo json_encode($add_fg);


		}else{
			$product_id = $this->input->post('transform');
			$qty = $this->input->post('transform_qty');
			$type = 5;
			$batch_id = $this->input->post('batch_id');
			$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_log =  array('product_id' => $product_id,
							'type' => $type,
							'batch' => $batch_id,
							'qty' => $qty,
							'insert_date'=>$insert_date,
							'user_id'=> $user_id,
							'created'=> $created
							 );

     
     	
     	$result1 = $this->Stocks->add_fg_log($save_log);

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



public function transform_page_sealing(){

	check_login();

	$id = $this->uri->segment('3');
	if($this->input->post())
		{

			$batch_id = $this->input->post('batch_id');
			$created = $this->input->post('created');
			$user_id = $this->input->post('user_id');
			$transform = $this->input->post('transform');
			$transform_qty = $this->input->post('transform_qty');
			$item = $this->input->post('item');
			$depart = $this->input->post('depart');
			$rej_qty = $this->input->post('rej_qty');
			// $total  = get_transform_qty($batch_id);
			// $batch_qty = batch_qty($batch_id);
			// $result_qty = $total+$transform_qty;

			$total  = get_transform_qty_sealing($batch_id,$transform);
			$batch_qty = batch_qty($batch_id);
			$result_qty = $total+$transform_qty;

			

			if($result_qty > $batch_qty){

				$add_mixing=array('status'=>'validate');
				 echo json_encode($add_mixing);
				}else{

				if($result_qty == $batch_qty)
			{
				

					 $dataarray2 =  array(
						'batch' => $batch_id,
						'transform'=> $transform,
						'type'=> $depart,
						'qty' => $transform_qty,
						'rej_qty'=>$rej_qty,
						'created'=> $created
						 );

				 $result = $this->Stocks->add_logs_tranform($dataarray2);
				 $result = $this->Stocks->update_transform_machine_sealing($batch_id);
				if($result){$add_mixing=array('status'=>'true');}else{$add_mixing=array('status'=>'false');}
				 echo json_encode($add_mixing);

			}else{
				 if($depart == 5){
					
	
						 $dataarray2 =  array(
							'batch' => $batch_id,
							'transform'=> $transform,
							'type'=> $depart,
							'qty' => $transform_qty,
							'rej_qty'=>$rej_qty,
							'created'=> $created
							 );

							 $dataarray =  array(
								'packing_product'=> $transform,
								
								 );
					
					 $result = $this->Stocks->update_transform_product_batch($batch_id,$dataarray);
					 $result = $this->Stocks->add_logs_tranform($dataarray2);

				}

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

			}
			}
		
					 
		}else{
			$data['transform_data_sealing']=$this->Stocks->transform_data_sealing($id);
			$data['get_semi']=$this->Stocks->sealed_transform_dep();
			$data['dep']=$this->Master_list->sealed_transform_dep();
			// print_r($data['dep']);
			// die;
			$this->load->view('Stock/transform_page_sealing',$data);
			}
}



public function packing_batch(){

	check_login();

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

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

}


public function transform_page_assembled(){

	check_login();

	$id = $this->uri->segment('3');
	if($this->input->post())
		{

			$batch_id = $this->input->post('batch_id');
			$batch_no = $this->input->post('batch_no');
			$created = $this->input->post('created');
			$user_id = $this->input->post('user_id');
			$transform = $this->input->post('transform');
			$pro = $this->input->post('transform');
			$transform_qty = $this->input->post('transform_qty');
			$item = $this->input->post('item');
			$depart = $this->input->post('depart');
			$total  = get_transform_qty($batch_id);
			$batch_qty = get_transform_qty_ass($batch_id,$pro);
			$result_qty = $transform_qty;
			$rej_qty =  $this->input->post('rej_qty');
			$total  = get_transform_qty_sealing($batch_id,$transform);
			$batch_qty = batch_qty($batch_id);

			$result_qty = $total+$transform_qty;

		
				if($result_qty == $batch_qty)
			{

				

				$dataarray1 =  array(
					'batch' => $batch_id,
					'product'=> $transform,
					'qty' => $transform_qty,
					'user_id'=> $user_id,
					'created'=> $created
					 );

					 $dataarray2 =  array(
						'batch' => $batch_id,
						'transform'=> $transform,
						'type'=> $depart,
						'qty' => $transform_qty,
						'rej_qty' => $rej_qty,
						'created'=> $created
						 );


				 $result = $this->Stocks->add_logs_tranform($dataarray2);
				 //$result = $this->Stocks->release_batch_assembled($dataarray1);
				 $result = $this->Stocks->update_transform_machine_assembled($batch_id);

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

			}else{
				if($depart == 4){


					$dataarray1 =  array(
						'batch' => $batch_id,
						
						
						'user_id'=> $user_id,
						'created'=> $created
						 );
	
						 $dataarray2 =  array(
							'batch' => $batch_id,
							'transform'=> $transform,
							'type'=> $depart,
							'qty' => $transform_qty,
							'rej_qty' => $rej_qty,
							'created'=> $created
							 );

								$dataarray =  array(
									'sealed_product'=> $transform,
									
									 );
								 
					 $result = $this->Stocks->add_logs_tranform($dataarray2);
					 $result = $this->Stocks->add_new_batch_sealingmachine($dataarray1);
					 $result = $this->Stocks->update_transform_product_release($batch_no,$dataarray);
					 if($result){$add_mixing=array('status'=>'true');}else{$add_mixing=array('status'=>'false');}
					 echo json_encode($add_mixing);

				}else if($depart==5){

					$dataarray2 =  array(
						'batch' => $batch_id,
						'transform'=> $transform,
						'type'=> $depart,
						'qty' => $transform_qty,
						'rej_qty' => $rej_qty,
						'created'=> $created
						 );
				 $result = $this->Stocks->add_logs_tranform($dataarray2);

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

					 
		}else{
			$data['transform_data_printing']=$this->Stocks->transform_data_assembled($id);
			$data['get_semi']=$this->Stocks->assembled_transform_dep();
			$data['dep']=$this->Master_list->assembled_transform_dep();
			$this->load->view('Stock/transform_page_assembled',$data);
			}
}


public function batch_printing_line(){

	check_login();

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

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

}

public function batch_sealed_line(){

	check_login();

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

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

}

public function batch_assembled_line(){

	check_login();

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

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

}


public function assign_assemble_line_area(){

	check_login();

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

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

}

public function add_line(){

	check_login();

	

	$data['lines']=$this->Stocks->lines();
	
	$this->load->view('Stock/add_line',$data);

}




public function assembled_line_area(){

	check_login();
			if($this->input->post())
			{
					$description = $this->input->post('description');
					$created = $this->input->post('created');
					$user_id = $this->input->post('user_id');
				
				   $dataarray =  array(
		 			'description'=>$description,
				   'created'=>$created,
				   'user_id'=>$user_id
				 
					);
					

					$result = $this->Stocks->add_line_clerence_area($dataarray);
					if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
					echo json_encode($add_out);
					}else{
					$data['line_assembled']=$this->Stocks->line_assembled_description();

					$this->load->view('Stock/assembled_line_area',$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);

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

			
			}
		}


		
		public function lineclerence_print_machine(){

			check_login();
			if($this->input->post())
			{
		
		
				$printing_machine_id = $this->input->post('id');	
				$insert_date = $this->input->post('insert_date');
				
				$clear1 = $this->input->post('clear1');
				$clear2 = $this->input->post('clear2');
				$clear3 = $this->input->post('clear3');
				$clear4 = $this->input->post('clear4');
				$clear5 = $this->input->post('clear5');
				$clear6 = $this->input->post('clear6');
				$clear7 = $this->input->post('clear7');
				$clear8 = $this->input->post('clear8');
				$clear9 = $this->input->post('clear9');
				$clear10 = $this->input->post('clear10');
				$clear11 = $this->input->post('clear11');
				$clear12 = $this->input->post('clear12');
				$clear13 = $this->input->post('clear13');
				$remarks = $this->input->post('remarks');
				$created = $this->input->post('created');
				$user_id = $this->input->post('user_id');

				$dataarray =  array(
		 						
				   'printing_machine_id' => $printing_machine_id,
				   'insert_date' => $insert_date,
				   'clear1'=>$clear1,
				   'clear2'=>$clear2,
				   'clear3'=>$clear3,
				   'clear4'=>$clear4,
				   'clear5'=>$clear5,
				   'clear6'=>$clear6,
				   'clear7'=>$clear7,
				   'clear8'=>$clear8,
				   'clear9'=>$clear9,
				   'clear10'=>$clear10,
				   'clear11'=>$clear11,
				   'clear12'=>$clear12,
				   'clear13'=>$clear13,
				   'remarks'=> $remarks,
				   'user_id'=> $user_id,
				   'created'=> $created
					);
					$dataarray1 =  array(
		 						
						'id' => $printing_machine_id,
						'insert_date' => $insert_date,
						
						'status'=> 1,
						'user_id'=> $user_id,
						'created'=> $created
						 );

					$result = $this->Stocks->update_assign_machine_printing_status($printing_machine_id,$dataarray1);
					$result = $this->Stocks->add_line_clerence_print($dataarray);
					if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
					echo json_encode($add_out);
				
		}else{
		$this->load->view('Stock/lineclerence_print_machine');
		}
	}


	public function lineclerence_sealing_machine(){

		

		check_login();
		if($this->input->post())
		{
	
	
			$printing_machine_id = $this->input->post('id');	
			$insert_date = $this->input->post('insert_date');
			
			$clear1 = $this->input->post('clear1');
			$clear2 = $this->input->post('clear2');
			$clear3 = $this->input->post('clear3');
			$clear4 = $this->input->post('clear4');
			$clear5 = $this->input->post('clear5');
			$clear6 = $this->input->post('clear6');
			$clear7 = $this->input->post('clear7');
			$clear8 = $this->input->post('clear8');
			$clear9 = $this->input->post('clear9');
			$clear10 = $this->input->post('clear10');
			$clear11 = $this->input->post('clear11');
			$clear12 = $this->input->post('clear12');
			$clear13 = $this->input->post('clear13');
			$remarks = $this->input->post('remarks');
			$created = $this->input->post('created');
			$user_id = $this->input->post('user_id');

			$dataarray =  array(
							 
			   'sealing_machine_id' => $printing_machine_id,
			   'insert_date' => $insert_date,
			   'clear1'=>$clear1,
			   'clear2'=>$clear2,
			   'clear3'=>$clear3,
			   'clear4'=>$clear4,
			   'clear5'=>$clear5,
			   'clear6'=>$clear6,
			   'clear7'=>$clear7,
			   'clear8'=>$clear8,
			   'clear9'=>$clear9,
			   'clear10'=>$clear10,
			   'clear11'=>$clear11,
			   'clear12'=>$clear12,
			   'clear13'=>$clear13,
			   'remarks'=> $remarks,
			   'user_id'=> $user_id,
			   'created'=> $created
				);
				$dataarray1 =  array(
							 
					'id' => $printing_machine_id,
					'insert_date' => $insert_date,
					
					'status'=> 1,
					'user_id'=> $user_id,
					'created'=> $created
					 );

				$result = $this->Stocks->update_assign_machine_sealed_status($printing_machine_id,$dataarray1);
				$result = $this->Stocks->add_line_clerence_sealed($dataarray);
				if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
				echo json_encode($add_out);
			
	}else{
	$this->load->view('Stock/lineclerence_sealing_line');
	}
}


	public function lineclerence_assembled_line(){

		check_login();
		if($this->input->post())
		{
			$batch = $this->input->post('batch');
			$pro = $this->input->post('pro');	
			$insert_date = $this->input->post('insert_date');
			$clear1 = $this->input->post('clear1');
			$clear2 = $this->input->post('clear2');
			$clear3 = $this->input->post('clear3');
			$clear4 = $this->input->post('clear4');
			$clear5 = $this->input->post('clear5');
			$clear6 = $this->input->post('clear6');
			$clear7 = $this->input->post('clear7');
			$clear8 = $this->input->post('clear8');
			$clear9 = $this->input->post('clear9');
			$clear10 = $this->input->post('clear10');
			$clear11 = $this->input->post('clear11');
			$clear12 = $this->input->post('clear12');
			$clear13 = $this->input->post('clear13');
			$remarks = $this->input->post('remarks');
			$created = $this->input->post('created');
			$user_id = $this->input->post('user_id');

		
			$dataarray =  array(
			   'assembled_id' => $batch,
			   'insert_date' => $insert_date,
			   'clear1'=>$clear1,
			   'clear2'=>$clear2,
			   'clear3'=>$clear3,
			   'clear4'=>$clear4,
			   'clear5'=>$clear5,
			   'clear6'=>$clear6,
			   'clear7'=>$clear7,
			   'clear8'=>$clear8,
			   'clear9'=>$clear9,
			   'clear10'=>$clear10,
			   'clear11'=>$clear11,
			   'clear12'=>$clear12,
			   'clear13'=>$clear13,
			   'remarks'=> $remarks,
			   'user_id'=> $user_id,
			   'created'=> $created
				);

				$batch_no = batch_no($batch);
				$dataarray1 =  array(
							 
					
					'insert_date' => $insert_date,
					
					'status'=> 1,
					'user_id'=> $user_id,
					'created'=> $created
					 );


					

				$result = $this->Stocks->update_assign_assembled_status($batch_no,$dataarray1);
				$result = $this->Stocks->add_line_clerence_assembled($dataarray);
				if($result){$add_out=array('status'=>'true');}else{$add_out=array('status'=>'false');}
				echo json_encode($add_out);
			
	}else{
		$this->load->view('Stock/lineclerence_assembled_line');
	}
}


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
								 );

		 

	     	$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 fg_print(){
		check_login();
   
		$id = $this->uri->segment('3');
		$data['fg_print']=$this->Stocks->fg_print($id);

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

		}
		
		
		public function semi_in_print(){
		check_login();
   
		$id = $this->uri->segment('3');
		
		$data['semi_print']=$this->Stocks->fg_print($id);

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

		}



	public function out_stock_entry(){
	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');

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

	 	$dataarray =  array('product_id' => $product_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($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 fg_stock_entry(){
	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 == ""){
	$condition  = $this->Stocks->get_product_for_condition($product_id);
	
	 if($condition ==""){

	 		$product_id = $this->input->post('product_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');
			$batch = $this->input->post('batch');
			$insert_date = $this->input->post('insert_date');
			

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

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

     	$result = $this->Stocks->add_fg($save_data);
     	
     	$result1 = $this->Stocks->add_fg_log($save_log);
     	
       if($result){$add_fg=array('data'=>$result1);}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');
			$batch = $this->input->post('batch');
			
		
			$all_qty = $condition[0]['qty']+$qty;

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

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

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

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

		}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 batch_release(){
		check_login();
   
		$data['sales_order_open_order'] = $this->Stocks->sales_order_open_order();
		$this->load->view('Stock/batch_release',$data);

		}

		

		public function tansform_check(){


			check_login();

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

			$data['tansform_check'] = $this->Stocks->tansform_check($id);
			
			$this->load->view('Stock/tansform_check',$data);
	
			
		}


		
		public function release_batch_form(){


			check_login();

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

			if($this->input->post()){
				
				$batch = $this->input->post('batch');
				$main_product = $this->input->post('main_product');
				$product = $this->input->post('product');
				$insert_date = $this->input->post('insert_date');
				$company = $this->input->post('company');
				$req_qty = $this->input->post('req_qty');
				$qty = $this->input->post('out_qty');
				$depart = $this->input->post('depart');
				$so_id = $this->input->post('so_id');
				$jobs = $this->input->post('jobs');
				$created = $this->input->post('created');
				$user_id = $this->input->post('user_id');
				$assignto = $this->input->post('assign_to');
				$batch_size = $this->input->post('batch_size');

			    $subject_count = count(array_filter($product));

				@$batch_size_chek = batch_size($so_id);
				$total_qty = $batch_size+$batch_size_chek;
				
				if($req_qty[0] == $total_qty){
					
					

					$save_data_batch_size =  array(
						'batch' => $batch,
						'assign_to' => $assignto,
						'batch_size' => $batch_size,
						'so_id' => $so_id,
						'product' => $main_product
					 );

				$batch_size = $this->Stocks->batch_assign_to_batch_size($save_data_batch_size);		
				
				for($i=0; $i < $subject_count; $i++){
					
					$dataarray =  array(
						'so_id' =>  $so_id,
						'jobs' => $jobs[$i],
						'batch' =>  $batch,
						'main_product' =>  $main_product,
						'product' =>  $product[$i],
						'insert_date' =>  $insert_date,
						'company' =>  $company,
						'req_qty' =>  $req_qty[$i],
						'qty' =>  $qty[$i],
						'depart' =>  $depart[$i],
						
						'created' =>  $created,
						'user_id' =>  $user_id
					
					);

					
					

					$save_data =  array(
						'product_id' => product_id($product[$i]),
						'type' => $depart[$i],
						'qty' => $qty[$i],
						'insert_date'=>$insert_date,
						'user_id'=> $user_id,
						'created'=> $created
						);

					

					$result1 = $this->Stocks->add_new_batch($dataarray);
					
					$add_log_semi = $this->Stocks->add_out_stock($save_data);
				
		  if($depart[$i]==2)
			{

				$machine =  array(
					'batch' =>  $result1,
					'created' =>  $created,
					'user_id' =>  $user_id
				
				 );

				   $result = $this->Stocks->add_new_batch_machine($machine);
			}else if($depart[$i]==3)
			{

				$line =  array(
					'batch' =>  $result1,
					'batch_no' => $batch,
					'created' =>  $created,
					'user_id' =>  $user_id
				
				 );

				   $result = $this->Stocks->add_new_batch_line($line);
			}else if($depart[$i]==4)
			{

				$line =  array(
					'batch' =>  $result1,
					'created' =>  $created,
					'user_id' =>  $user_id
				
				 );

				   $result = $this->Stocks->add_new_batch_sealingmachine($line);
			}
		}
				$update_status =  array(
					'status' => 1
					);
				$update_status = $this->Stocks->update_status($so_id,$update_status);


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


		}else{

					$save_data_batch_size =  array(
						'batch' => $batch,
						'assign_to' => $assignto,
						'batch_size' => $batch_size,
						'so_id' => $so_id,
						'product' => $main_product
					 );

		 $batch_size = $this->Stocks->batch_assign_to_batch_size($save_data_batch_size);			 

		for($i=0; $i < $subject_count; $i++){
			
			$dataarray =  array(
				'so_id' =>  $so_id,
				'jobs' => $jobs[$i],
				'batch' =>  $batch,
				'main_product' =>  $main_product,
				'product' =>  $product[$i],
				'insert_date' =>  $insert_date,
				'company' =>  $company,
				'req_qty' =>  $req_qty[$i],
				'qty' =>  $qty[$i],
				'depart' =>  $depart[$i],
				
				'created' =>  $created,
				'user_id' =>  $user_id
			
			 );

			 $save_data =  array(
				'product_id' => product_id($product[$i]),
				'type' => $depart[$i],
				'qty' => $qty[$i],
				'insert_date'=>$insert_date,
				'user_id'=> $user_id,
				'created'=> $created
				 );

			 $result1 = $this->Stocks->add_new_batch($dataarray);
			// $add_log_semi = $this->Stocks->add_fg_log($save_data);
			 $add_log_semi = $this->Stocks->add_out_stock($save_data);
		  
		  if($depart[$i]==2)
			{

				$machine =  array(
					'batch' =>  $result1,
					'created' =>  $created,
					'user_id' =>  $user_id
				
				 );

				   $result = $this->Stocks->add_new_batch_machine($machine);
			}else if($depart[$i]==3)
			{

				$line =  array(
					'batch' =>  $result1,
					'batch_no' => $batch,
					'created' =>  $created,
					'user_id' =>  $user_id
				
				 );

				   $result = $this->Stocks->add_new_batch_line($line);
			}else if($depart[$i]==4)
			{

				$line =  array(
					'batch' =>  $result1,
					'created' =>  $created,
					'user_id' =>  $user_id
				
				 );

				   $result = $this->Stocks->add_new_batch_sealingmachine($line);
			}
		}


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

				}


				
				

			 
		   }else{

			$code['batch'] = $this->Stocks->get_batch_number();
			$data['sales_order_open_order_id'] = $this->Stocks->sales_order_open_order_id($id);
			$xx = @$code['batch'][0]['batch'];
			if($xx == ''){
				for ($dc_no = 0; $dc_no <= 10000; $dc_no++) {
			   
				}
			}else{
				for ($dc_no = 0; $dc_no <= $xx; $dc_no++) {
			   
				}
			}
			
				$data['batch'] = $dc_no;

				$fg = $data['sales_order_open_order_id'][0]['product_id'];
			    $data['get_bom_data']=$this->Master_list->get_bom_data_batch($fg);
				$data['dep']=$this->Master_list->departments();
				$data['parties']=$this->System->get_parties_assembling();
				// print_r($data['get_bom_data']);
				// die;
			$this->load->view('Stock/release_batch_form',$data);
	
			}
		}

			


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

	
}
