<?php
class Orders extends CI_Model {

 		public function add_dc($post) {
          $result = $this->db->insert('dc_record',$post);
          if($result){return $this->db->insert_id();}else{return false;} 
		 }

    
		public function add_dc_qc_check_raw_material($post) {
          $result = $this->db->insert('qc_check_dc_raw_material',$post);
          if($result){return $this->db->insert_id();}else{return false;} 
		 }

		 public function add_dc_qc_check_packing_material($post) {
          $result = $this->db->insert('qc_check_dc_packing_material',$post);
          if($result){return true;}else{return false;} 
		 }

		 
		 public function add_timing_qc($save_data) {
          $result = $this->db->insert('add_timing_qc',$save_data);
          if($result){return true;}else{return false;} 
		 }


		 public function add_supervisor_qcs($save_data) {
			$result = $this->db->insert('supervisor_ranges_qcs',$save_data);
			if($result){return true;}else{return false;} 
		   }

		   
		   public function add_supervisor1_qcs($save_data) {
			$result = $this->db->insert('supervisor_1_qc',$save_data);
			if($result){return true;}else{return false;} 
		   }
		 
		 
		 public function add_supervisor_qc($save_data) {
          $result = $this->db->insert('supervisor_qc',$save_data);
          if($result){return true;}else{return false;} 
		 }

		  public function add_complain_hour_check_qc($save_data) {
          $result = $this->db->insert('complain_hour_check_qc',$save_data);
          if($result){return true;}else{return false;} 
		 }

		 
		  public function add_randomlycheck_qc($save_data) {
          $result = $this->db->insert('randomly_check_qc',$save_data);
          if($result){return true;}else{return false;} 
		 }


		 public function add_dc_product($post) {
          $result = $this->db->insert('qc_check_dc_product',$post);
          if($result){return true;}else{return false;} 
		 }

		 
		 public function add_qc_machine($dataarray) {
          $result = $this->db->insert('qc_machine',$dataarray);
          if($result){return true;}else{return false;} 
		 }


		 
		 public function update_machine_qc($id) {
		$result = $this->db->where('id',$id);
		$result = $this->db->update('qc_machine',$_POST);
		if($result){return true;}else{return false;}
	}


		public function get_machine_qc_data($id)
			{
				
				$sql = $this->db->where('id',$id);
				$sql = $this->db->get('qc_machine');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}


			

			public function get_product_id($id)
			{
				
				
				$this->db->select('products.id');    
				$this->db->from('products');
				$this->db->where("products.name",$id); 
				$sql = $this->db->get();
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}

     public function get_dc_qty($id)
		{
			$this->db->select('SUM(dc_record.qty) AS qty,dc_record.qc_status');    
			$this->db->from('dc_record');
			$this->db->where("dc_record.so_id",$id); 
			$this->db->where("dc_record.qc_status !=",3); 
			$sql = $this->db->get();

			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}

		


		public function get_all_po()
		{
			$sql = $this->db->get_where('purchase_order', array('status'=>1));
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}

		
		public function get_complainhours($id)
		{
			$sql = $this->db->get_where('complain_hour_check_qc', array('id'=>$id));
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}

		public function get_dc_order(){

			$sql = $this->db->get_where('dc_record', array('qc_status !='=>1));
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}	
		}
		

		
		public function get_Orders_code(){

		$this->db->select('dc_record.grn_no');  
		  
		$this->db->from('dc_record');
		//$this->db->where('dc_record.id',$id);
		$this->db->order_by('dc_record.grn_no','DESC');
		
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

		
		public function get_Orders_status(){

		$this->db->select('dc_record.qc_status');  
		  
		$this->db->from('dc_record');
		$this->db->where('dc_record.qc_status',0);
		$this->db->group_by('dc_record.qc_status');
		
		
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

		
		public function get_qc_machine(){

		$this->db->select('qc_machine.day_night,qc_machine.machine_id,qc_machine.id,qc_machine.product_id,qc_machine.material_id,qc_machine.mr_mould,qc_machine.pcs_cavity,qc_machine.weight,qc_machine.finishing,qc_machine.fitting,qc_machine.color,qc_machine.remarks,qc_machine.created');  
		  
		$this->db->from('qc_machine');
		$this->db->where('qc_machine.status',0);
		$this->db->where('qc_machine.report_date >=','2023-04-17');
		
		
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	public function get_machine_data()
	{
		$sql = $this->db->get('machines');
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}



		

		public function quality_checked_print($id)
		{
		$this->db->select('dc_record_po.lot_no,dc_record_po.grn_no,dc_record_po.qc_status,dc_record_po.created,dc_record_po.dc_no,dc_record_po.qty,dc_record_po.id as dc_id,purchase_order_materials_details.po_id,purchase_order_materials_details.material_id,purchase_order_materials_details.product_id,purchase_order.vendor_id,dc_record_po.release_no,purchase_order_materials_details.unit');
		$this->db->from('dc_record_po');
		$this->db->join('purchase_order_materials_details', 'dc_record_po.po_id = purchase_order_materials_details.po_id', 'inner');
		$this->db->join('purchase_order', 'purchase_order_materials_details.po_id = purchase_order.id', 'inner');
		$this->db->where('dc_record_po.id',$id );
		//$this->db->where('dc_record.qc_status',1);
		$sql = $this->db->get();
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}
		
		

		public function get_release(){

		$this->db->select('dc_record_po.release_no');  
		  
		$this->db->from('dc_record_po');
		//$this->db->where('type',1);
		$this->db->order_by('dc_record_po.id','DESC');
		
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

		public function update_dc_status($id) {
		$result = $this->db->where('id',$id);
		$result = $this->db->update('purchase_order','dc_status',1);
		if($result){return true;}else{return false;}
	}


	

		public function update_qc_timings($id) {
		$result = $this->db->where('id',$id);
		$result = $this->db->update('complain_hour_check_qc',$_POST);
		if($result){return true;}else{return false;}
	}


		public function get_all_po1()
		{
			
			$this->db->select('purchase_order.id,purchase_order_materials_details.po_id,purchase_order_materials_details.weight,SUM(dc_record.qty) AS qty');$this->db->from('purchase_order');
			$this->db->join('purchase_order_materials_details', 'purchase_order.id = purchase_order_materials_details.po_id', 'inner');
			$this->db->join('dc_record', 'purchase_order_materials_details.po_id = dc_record.po_id', 'inner');
		$sql = $this->db->get();
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		public function get_all_po22()
			{
			$this->db->select('dc_record.created,dc_record.grn_no,dc_record.dc_no,SUM(dc_record.qty) AS qtys,dc_record.id as dc_id,purchase_order_materials_details.po_id,purchase_order_materials_details.material_id,purchase_order_materials_details.product_id,purchase_order.vendor_id,purchase_order_materials_details.order_qty,purchase_order.dc_status,purchase_order.approve_status,purchase_order.po_date,purchase_order.id');
			$this->db->from('dc_record');
			$this->db->join('purchase_order_materials_details', 'dc_record.po_id = purchase_order_materials_details.po_id', 'inner');
			$this->db->join('purchase_order', 'purchase_order_materials_details.po_id = purchase_order.id', 'inner');
			$this->db->where('purchase_order.status',1 );
			$sql = $this->db->get();
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
			}



		public function get_dc_data()
		{
			$this->db->select('dc_record_po.qc_status,dc_record_po.created,dc_record_po.dc_no,dc_record_po.qty,dc_record_po.id as dc_id,dc_record_po.po_id,purchase_order_materials_details.po_id,purchase_order_materials_details.material_id,purchase_order_materials_details.product_id,purchase_order.vendor_id,purchase_order.quot_ref');
			$this->db->from('dc_record_po');
			$this->db->join('purchase_order_materials_details', 'dc_record_po.po_id = purchase_order_materials_details.po_id', 'inner');
			$this->db->join('purchase_order', 'purchase_order_materials_details.po_id = purchase_order.id', 'inner');
			//$this->db->where('dc_record_po.qc_status',0);

		$sql = $this->db->get();
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}

		
		public function get_qc_data($id)
		{
		$this->db->select('dc_record_po.created,dc_record_po.dc_no,dc_record_po.qty,dc_record_po.id as dc_id,dc_record_po.po_id,purchase_order_materials_details.po_id,purchase_order_materials_details.material_id,purchase_order_materials_details.product_id,purchase_order.vendor_id');
			$this->db->from('dc_record_po');
			$this->db->join('purchase_order_materials_details', 'dc_record_po.po_id = purchase_order_materials_details.po_id', 'inner');
			$this->db->join('purchase_order', 'purchase_order_materials_details.po_id = purchase_order.id', 'inner');
		$this->db->where('dc_record_po.id',$id );
		$sql = $this->db->get();
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		/*public function get_hold_qc_raw($id)
		{
			$sql = $this->db->get_where('qc_check_dc_raw_material', array('dc_id'=>$id));
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		public function get_hold_qc_pack($id)
		{
			$sql = $this->db->get_where('qc_check_dc_packing_material', array('dc_id'=>$id));
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}*/
		
		

		public function update_so_status($id) {
		$result = $this->db->where('id',$id);
		$result = $this->db->update('sales_order',array('status' =>1));
		if($result){return true;}else{return false;}
		}

	public function update_dc_qc_status($id,$consignment) {
		$result = $this->db->where('id',$id);
		$result = $this->db->update('dc_record_po',array('qc_status' =>$consignment));
		if($result){return true;}else{return false;}
		}


		public function get_machine_mold()
	{
		
		$this->db->select('molds.machine_type');
		$this->db->from('molds');
		//$this->db->group_by('machine_id');
		$sql = $this->db->get();
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}



	public function get_materials()
		{
	
		$sql = $this->db->get('materials');


		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
		}


		public function get_products_mould()
		{
		
		$sql = $this->db->get('molds');
		
		
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

		}


		

		public function get_products_mold($id)
		{
			$sql = $this->db->get_where('molds', array('machine_type'=>$id));
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		
		public function get_machine($id)
		{
			$sql = $this->db->get_where('machines', array('machine_type'=>$id));
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		

		public function get_machine_data_production()
		{
		    $this->db->select('production_machine_remarks.machine,production_machine_remarks.so_id');
			$this->db->from('production_machine_remarks');
			$this->db->join('production_plan', 'production_machine_remarks.so_id = production_plan.so_id', 'inner');
			
			$this->db->where('production_machine_remarks.status',0);
			$this->db->group_by('production_machine_remarks.machine');
			$sql = $this->db->get();
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}

		

		public function get_machine_data_production_ibm()
		{
		    $this->db->select('production_machine_remarks.machine,production_machine_remarks.so_id');
			$this->db->from('production_machine_remarks');
			$this->db->join('production_plan', 'production_machine_remarks.so_id = production_plan.so_id', 'inner');
			
			$this->db->where('production_machine_remarks.status',0);
			$this->db->group_by('production_machine_remarks.machine');
			$sql = $this->db->get();
			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		public function get_item_for_check_timing_qc($p_name,$so_id)
		{
			
			$this->db->select('production_plan.product_name,production_plan.job,production_plan.material_id');
			$this->db->from('production_plan');
			$this->db->where('production_plan.product_name',$p_name );
            $this->db->where('production_plan.so_id',$so_id );
            $this->db->where('production_plan.complete_status',0 );
			$sql = $this->db->get();

			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		

		public function get_config($id)
		{
			
			$this->db->select('*');
			$this->db->from('machine_config');
			$this->db->where('machine_config.product',$id );
            $this->db->where('machine_config.status',0 );
			$sql = $this->db->get();

			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		

		public function get_config_ibm($id)
		{
			
			$this->db->select('*');
			$this->db->from('machine_config_ibm');
			$this->db->where('machine_config_ibm.product',$id );
            $this->db->where('machine_config_ibm.status',0 );
			$sql = $this->db->get();

			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}

		public function get_machine_data_production_operator()
		{
			
			$this->db->select('empolyees.fullname,empolyees.id');
			$this->db->from('empolyees');
			$this->db->where('empolyees.depart',7);
			$this->db->where('empolyees.status',1);
			$sql = $this->db->get();

			if($sql->num_rows() >0)
			{return $sql->result_array();
			}else
			{return false;}
		}


		public function get_qc_timing()
			{	

				$date = date('Y-m-d');
				
				$sql = $this->db->where('insert_date',$date);
				$sql = $this->db->get('add_timing_qc');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}

		

		public function get_randomly_qc()
			{	

				$date = date('Y-m-d');
				
				$sql = $this->db->where('insert_date',$date);
				$sql = $this->db->get('randomly_check_qc');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}

			


			public function get_qc_complain_hour()
			{	

				$date = date('Y-m-d');
				
				$sql = $this->db->where('insert_date',$date);
				$sql = $this->db->get('complain_hour_check_qc');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}



			public function get_supervisor_data()
			{	

				$date = date('Y-m-d');
				
				$sql = $this->db->where('insert_date',$date);
				$sql = $this->db->get('supervisor_qc');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}

			

			public function get_supervisor1_data1()
			{	

				$date = date('Y-m-d');
				
				$sql = $this->db->where('insert_date',$date);
				$sql = $this->db->get('supervisor_1_qc');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}

			

			public function get_supervisor_data_range()
			{	

				$date = date('Y-m-d');
				
				$sql = $this->db->where('insert_date',$date);
				$sql = $this->db->get('supervisor_ranges_qcs');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}


			    public function delete_complian($id)
				{
					$sql = $this->db->delete('complain_hour_check_qc', array('id'=>$id));
					
					return true;
				}
            
            
            
            
             public function delete_qc_machine_entry($id)
				{
					$sql = $this->db->delete('qc_machine', array('id'=>$id));
					
					return true;
				}

				


			    public function delete_timing_qc($id)
				{
					$sql = $this->db->delete('add_timing_qc', array('id'=>$id));
					
					return true;
				}

				

				  public function delete_randomlydelete($id)
				{
					$sql = $this->db->delete('randomly_check_qc', array('id'=>$id));
					
					return true;
				}


}