<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Purchases_Order extends CI_Controller {

    var $main_menu_name = "purchases_order";
	var $sub_menu_name = "purchases_order";

	public function __construct()
	{
		parent::__construct();

		
		$this->load->model('Purchases_Order_Model');
		$this->load->model('Supplier_Model');
		$this->load->model('Warehouse_Model');
		$this->load->model('Common_Model');
		$this->load->model('Tax_Rates_Model');
		$this->load->model('Customer_Model');
		$this->load->model('Sequerty_Model');
		$this->load->model('Product_Models');
		$this->load->model('Purchases_Order_Model');
		//$this->load->model('Requisition_Model');
		$this->load->model('User_Model');
		
	}
	
	//Requisition list page load
	public function index()
	{
		$data['purchases_order'] = $this->Purchases_Order_Model->get_all_purchases_order();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
        $this->load->view('purchases_order',$data);
	}	
	

	public function itemlist()
	{
		$data['purchases_order'] = $this->Purchases_Order_Model->get_all_purchases_order();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
        $this->load->view('purchases_order',$data);
	}
		
	public function purchases_order_add_row()
	{
		$product_id=$this->input->post('id');
		
		$rowCount=$this->input->post('rowCount');
		$pro_dlts=$this->Product_Models->get_product_by_id($product_id);
		//print_r($pro_dlts);
		$row_details='';
		
		$selbox='';
		//test 
		//$rowCount_e=1;
		$tmp_th_e=$rowCount;
		$pymnt_cheque_date='';
		$required_date=date('m/d/Y');
		
		//
		$poitm_qty=1;
		
		//get pr item qty by id
		//$poitm_qty=
		
		$sub_total_item=$poitm_qty*$pro_dlts->product_cost;
		
		
		//validate
		$this->load->library('form_validation');
		//$this->form_validation->set_rules('bank_id_'.$tmp_th_e, 'Bank', 'required');
		//if ($this->form_validation->run() != FALSE)
        {
             
			$row_details="
			<tr id=\"row_e_$tmp_th_e\">
			<td class=\"text-left\">
			$pro_dlts->product_name
			<input type=\"hidden\" style=\"width:100%; text-align:left\" name=\"row_e[$tmp_th_e][product_name][]\" id=\"product_name_$tmp_th_e\" value=\"$pro_dlts->product_name\" class=\"pymnt_amount\">
			<input type=\"hidden\" style=\"width:100%; text-align:left\" name=\"row_e[$tmp_th_e][product_id][]\" id=\"product_id_$tmp_th_e\" value=\"$pro_dlts->product_id\" class=\"product_id\">
			</td>
			<td>$pro_dlts->unit_code
			<input type=\"hidden\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][product_unit][]\" id=\"product_unit_$tmp_th_e\" value=\"$pro_dlts->product_unit\" class=\"product_unit\">
			</td>
			<td><input type=\"text\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][poitm_qty][]\" id=\"poitm_qty_$tmp_th_e\" value=\"$poitm_qty\" class=\"poitm_qty\" onchange=\"changeQtyByProductID(this.value,$tmp_th_e);\" onclick=\"this.select(); setTmpVal(this.value);\"></td>
			<td><input onchange=\"calculateTotal()\";	type=\"text\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][product_cost][]\" id=\"product_cost_$tmp_th_e\" value=\"$pro_dlts->product_cost\" class=\"product_cost\"></td>
			<td><input type=\"text\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][poitm_dis][]\" id=\"poitm_dis_$tmp_th_e\" value=\"0\" class=\"poitm_dis\" onchange=\"changeDiscountByProductID(this.value,1);\" onclick=\"this.select(); setTmpVal(this.value);\">
			<input type=\"hidden\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][poitm_dis_val][]\" id=\"poitm_dis_val_$tmp_th_e\" value=\"0\" class=\"poitm_dis_val\">
			</td>
			<td><input type=\"text\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][sub_total_item][]\" id=\"sub_total_item_$tmp_th_e\" value=\"$sub_total_item\" class=\"\" readonly></td>
			<td><a onclick=\"deletePurchase_OrderItem($tmp_th_e)\"><i style=\"cursor:pointer;\" title=\"Remove\" id=\"1446800197032\" class=\"fa fa-times tip podel\"></i></a></td>
			</tr>
			";
			
		 $e = array('status' =>1,'row_details' =>$row_details);
		 echo json_encode($e);
		
		}
		
	}
	
	//Requisition details view
	public function view()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = '';
		
		//get sale id
		$po_id=$this->uri->segment('3');
		$data['po_item_list']= $this->Purchases_Order_Model->get_po_item_list_by_po_id($po_id);
		$data['po_details']= $this->Purchases_Order_Model->get_po_info($po_id);
		
	
		$data['customer_details']= $this->Customer_Model->get_customer_info($data['po_details']['customer_id']);
		$data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info($data['po_details']['warehouse_id']);		
		
		$data['po_id']=$po_id;
        $this->load->view('purchases_order_view',$data);
	}

	//Requisition add page
	public function add_po_payments()
	{
		$po_pymnt_amount=$this->input->post('po_pymnt_amount');
		$po_id=$this->input->post('po_id');
		$po_pymnt_ref_no=$this->input->post('po_pymnt_ref_no');
		$po_pymnt_paying_by=$this->input->post('po_pymnt_paying_by');
		$po_pymnt_date_time=$this->input->post('po_pymnt_date_time');
		$po_pymnt_date_time_send=date('Y-m-d H:i:s', strtotime($po_pymnt_date_time));
		$po_pymnt_cheque_no=$this->input->post('po_pymnt_cheque_no');
		$po_pymnt_crdt_card_no=$this->input->post('po_pymnt_crdt_card_no');
		$po_pymnt_crdt_card_holder_name=$this->input->post('po_pymnt_crdt_card_holder_name');
		$po_pymnt_crdt_card_month=$this->input->post('po_pymnt_crdt_card_month');
		$po_pymnt_crdt_card_year=$this->input->post('po_pymnt_crdt_card_year');
		$po_pymnt_crdt_card_type=$this->input->post('po_pymnt_crdt_card_type');
		$po_type = $this->input->post('po_type');

		$po_pymnt_note=$this->input->post('po_pymnt_note');
		$user_id=$this->session->userdata('ss_user_id');
		$po_pymnt_added_date_time=date("Y-m-d H:i:s");
		$po_pymnt_id='';
		
        $this->load->library('form_validation'); //form validation lib
        $this->form_validation->set_rules('po_pymnt_amount', 'Amount', 'required');
		if($po_pymnt_paying_by=='Credit Card'){
			$this->form_validation->set_rules('po_pymnt_crdt_card_type', 'Card Type', 'required');
			$this->form_validation->set_rules('po_pymnt_crdt_card_no', 'Credit Card No', 'required');
			$this->form_validation->set_rules('po_pymnt_crdt_card_holder_name', 'Holder Name', 'required');
			$this->form_validation->set_rules('po_pymnt_crdt_card_month', 'Month', 'required');
			$this->form_validation->set_rules('po_pymnt_crdt_card_year', 'Year', 'required');
		}
		if($po_pymnt_paying_by=='Cheque'){
			$this->form_validation->set_rules('po_pymnt_cheque_no', 'Cheque No', 'required');
		}
		$this->form_validation->set_rules('po_id', 'System Error', 'required');


        if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
			$data=array(
				'po_pymnt_amount'=>$po_pymnt_amount,	
				'po_pymnt_ref_no'=>$po_pymnt_ref_no,
				'po_pymnt_paying_by'=>$po_pymnt_paying_by,
				'po_pymnt_date_time'=>$po_pymnt_date_time_send,
				'po_pymnt_note'=>$po_pymnt_note,
				'user_id'=>$user_id,
				'po_id'=>$po_id,
				'po_pymnt_added_date_time'=>$po_pymnt_added_date_time,
				'po_pymnt_cheque_no'=>$po_pymnt_cheque_no,
				'po_pymnt_crdt_card_no'=>$po_pymnt_crdt_card_no,
				'po_pymnt_crdt_card_holder_name'=>$po_pymnt_crdt_card_holder_name,
				'po_pymnt_crdt_card_type'=>$po_pymnt_crdt_card_type,
				'po_pymnt_crdt_card_month'=>$po_pymnt_crdt_card_month,
				'po_pymnt_crdt_card_year'=>$po_pymnt_crdt_card_year,
				'po_payment_type' => $po_type
			);
			
               if ($this->Purchases_Order_Model->save_po_payments($data,$po_pymnt_id)) {
                    $st = array('status' =>1,'validation' =>'Done!');
                    echo json_encode($st);
               } else {
                    $st = array('status' =>0,'validation' =>'error occurred please contact your system administrator');
                    echo json_encode($st);
               }
		}
	}	
	
	//Requisition payment page 
	public function payments()
	{
        $data['po_id'] = $this->input->get('id');
        $data['po_type'] = $this->input->get('po_type');
        $this->load->view('models/purchases_order_payment',$data);	
	}
	
	
	public function update_po_status()
	{
        $po_id = $this->input->post('po_id');
		 $type = $this->input->get('type');
		$disMsg='';
		$prepared_user=0;
		$checked_user=0;
		$approved_user=0;
		$authorized_user=0;
		
		$printed_date='';
		$printed_user=0;
		$printed_status='';
		
		
		if($this->input->post('po_prepared_status')=='Prepared')  $prepared_user=$this->session->userdata('ss_user_id');
		if($this->input->post('po_checked_status')=='Checked')  $checked_user=$this->session->userdata('ss_user_id');
		if($this->input->post('po_approved_status')=='Approved')  $approved_user=$this->session->userdata('ss_user_id');
		if($this->input->post('po_authorized_status')=='Authorized')  $authorized_user=$this->session->userdata('ss_user_id');
		if($this->input->post('printed_status')=='Printed')  $printed_user=$this->session->userdata('ss_user_id');
		
		//echo $prepared_user;
		$data_edit=array();
		if($type=='prepared'){
			$data_edit=array(
				'po_prepared_status'=>$this->input->post('po_prepared_status'),
				'prepared_user'=>$prepared_user,
				'prepared_date'=>date("Y-m-d H:i:s"),
			);
		}
		
		if($type=='checked'){
			$data_edit=array(
				 'po_checked_status'=>$this->input->post('po_checked_status'),
				'checked_user'=>$checked_user,
				'checked_date'=>date("Y-m-d H:i:s"),
			);
		}
		
		if($type=='authorized'){
			$data_edit=array(
				 'po_authorized_status'=>$this->input->post('po_authorized_status'),
				'authorized_user'=>$authorized_user,
				'authorized_date'=>date("Y-m-d H:i:s"),
			);
		}
		
		if($type=='approved'){
			$data_edit=array(
				 'po_approved_status'=>$this->input->post('po_approved_status'),
				'approved_user'=>$approved_user,
				'approved_date'=>date("Y-m-d H:i:s"),
			);
		}
		
		if($type=='printed'){
			$data_edit=array(
				 'printed_status'=>$this->input->post('printed_status'),
				'printed_user'=>$printed_user,
				'printed_date'=>date("Y-m-d H:i:s"),
			);
		}
			
			$_insert=$this->Purchases_Order_Model->save_purchases_order($data_edit,$po_id);
			
			//echo $this->db->last_query();
        
        echo json_encode(array('po_id'=>$po_id,'error'=>'','disMsg'=>$disMsg,));
	}
	
	
	//Requisition save 
	//Requisition item save
	//Add purchases_order items to 54 table
	public function save_purchases_order()
	{
		$warehouse_id=$this->input->post('warehouse_id');
		$req_id=$this->input->post('req_id');
		$rowCount=$this->input->post('rowCount');
		$po_id=$this->input->post('po_id');
		$po_datetime=date('Y-m-d H:i:s', strtotime($this->input->post('po_datetime')));
		$po_delivery_date=date('Y-m-d H:i:s', strtotime($this->input->post('po_delivery_date')));
		$project_id=$this->session->userdata('ss_warehouse_id');
		$supp_id=$this->input->post('supp_id');
		//$po_payment_term=$this->input->post('po_payment_term');
		$po_total=$this->input->post('po_total');
		$po_net_total=$this->input->post('po_net_total');
		$po_vat=$this->input->post('po_vat');
		$po_vat_val=$this->input->post('po_vat_val');
		$po_discount=$this->input->post('po_discount');
		$po_discount_amt=$this->input->post('po_discount_amt');
		$po_reference_no=$this->input->post('po_reference_no');
		$po_exepences=$this->input->post('po_exepences');
		$po_exepences_dtls=$this->input->post('po_exepences_dtls');
		$nbt_fld_val=$this->input->post('nbt_fld_val');
		$nbt_amount=$this->input->post('nbt_amount');
		$warehouse_po_id=$this->input->post('warehouse_po_id');

		//if(!$po_id){
			//	$po_reference_no=$this->Common_Model->gen_ref_number('po_id','purchases_order','REF/');
		//}else {
			//$po_reference_no=$this->input->post('po_reference_no');
	//	}
		
		$error='';
		$disMsg='';
		$lastid='';
		//$po_id='';
		
		if(!$error){
			$data_save=array(
			    'req_id'=>$req_id,
				'po_datetime'=>$po_datetime,
				'po_delivery_date'=>$po_delivery_date,
				'supp_id'=>$supp_id,
				//'po_payment_term'=>$po_payment_term,
				'po_total'=>$po_total,
				'po_net_total'=>$po_net_total,
				'po_vat'=>$po_vat,
				'po_vat_val'=>$po_vat_val,
				'po_discount'=>$po_discount,
				'po_discount_amt'=>$po_discount_amt,
				 'warehouse_id'=>$warehouse_id,
				 'po_reference_no'=>$po_reference_no,
				 'po_exepences'=>$po_exepences,
				 'nbt_amount'=>$nbt_amount,
				 'nbt_fld_val'=>$nbt_fld_val,
				 'po_exepences_dtls'=>$po_exepences_dtls,
				 'warehouse_po_id'=>$warehouse_po_id,
				 'rep_id'=>$this->input->post('rep_id')
				);
			

			if($po_id){
				//echo '111111111111';
				
				$po_id=$po_id;
				$this->Purchases_Order_Model->save_purchases_order($data_save,$po_id);
				//delete old req items
				$this->Purchases_Order_Model->delete_old_purchases_order_items($po_id);
				$disMsg='PO successfully updated';
				
			}else {
				
				
			$_insert=$this->Purchases_Order_Model->save_purchases_order($data_save,'');
			$lastid=$this->db->insert_id();
			$po_id=$lastid;
			$disMsg='PO successfully added';
			}
			
			
			
			//insert sale item data
			$row=$this->input->post('row_e');
			$rowCount=$this->input->post('rowCount');
			//echo 'test:'.$rowCount;
			$data_item=array();
			for($i=1; $i<=$rowCount; $i++){
				if(isset($row[$i]['product_name'][0]))
				{
					
				$data_item=array(
					'po_id'=>$po_id,
					'product_id'=>$row[$i]['product_id'][0],
					'product_name'=>$row[$i]['product_name'][0],
					'poitm_qty'=>$row[$i]['poitm_qty'][0],
					'product_cost'=>$row[$i]['product_cost'][0],
					'poitm_dis'=>$row[$i]['poitm_dis'][0],
					'poitm_dis_val'=>$row[$i]['poitm_dis_val'][0],
					'sub_total_item'=>$row[$i]['sub_total_item'][0],
					'product_unit'=>$row[$i]['product_unit'][0],					
				);
				$this->Purchases_Order_Model->save_purchases_order_item($data_item);
				
				//add reford for f4 table
				/*
				$type='purchases_order';
				$ref_id=$po_id;
				$product=$row[$i]['product_name'][0];
				$quantity=$row[$i]['product_name'][0];
				$unit_cost=$row[$i]['product_name'][0];
				$this->Common_Model->add_fi_table($type,$ref_id,$product,$quantity,$unit_cost);*/
				}
			}
		
		}else {
			
			$disMsg='Please select these before adding any product:'.$disMsg;
		}	
		
	//	echo "Test:".$po_id;;
		
		$this->session->set_flashdata('message', 'PO successfully added!');
		
		echo json_encode(array('po_id'=>$po_id,'error'=>$error,'disMsg'=>$disMsg,));
	}

	//Requisition reference no jenarate	
	public function get_next_ref_no(){
		$warehouse_id=$this->input->get('val');
		//$req_des=$this->Requisition_Model->get_req_info($pr_id);
		//print_r($req_des);
		//$warehouse_id=$req_des[0]->warehouse_id;
		
		$query=$this->Purchases_Order_Model->get_next_ref_no($warehouse_id);
		//echo $this->db->last_query();
		$result = $query->row();
		//print_r($result);
		$warehouse_po_id=$result->warehouse_po_id;
		
		$po_reference_no=sprintf("%04d", $result->warehouse_po_id+1);
		$po_reference_no=$po_reference_no;
		//get warehouse code
		$warehouse_dtls=$this->Warehouse_Model->get_warehouse_info($warehouse_id);
		//print_r($warehouse_dtls);
		$warehouse_code=$warehouse_dtls['code'];

		$po_reference_no_new=$warehouse_code.'/PO/'.$po_reference_no;
		//echo "ref:$po_reference_no_new";
		echo json_encode(array('po_reference_no'=>$po_reference_no_new,'warehouse_po_id'=>($warehouse_po_id+1),'warehouse_id'=>$warehouse_id
	));
	}
	
	//Requisition ger avalable product qty
	public function get_avalable_product_qty(){
		$product_id=$this->input->get('product_id');
		$warehouse_id=$this->input->get('warehouse_id');
		
		$data['total']=$this->Purchases_Order_Model->get_avalable_product_qty($product_id,$warehouse_id);
		echo json_encode(array('remmnaingQty'=>$data['total']));
	}
	
	//equisition details view
	public function purchases_order_details()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = '';
		
		//get sale id
		$po_id=$this->uri->segment('3');
		if(isset($po_id)){
			$data['po_details']=$this->Purchases_Order_Model->get_purchases_order_details_by_id($po_id);
			$data['reqitm_list']=$this->Purchases_Order_Model->get_purchases_order_items_by_id($po_id);
		}
		$data['po_id'] =$po_id;
		
        $this->load->view('purchases_order_details',$data);
		
	}

	//Requisition add form
    public function purchases_order_add()
    {
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'purchases_order_add';
		
		$po_id=$this->uri->segment('3');
		$data['poitm_list']=array();
		if(isset($po_id)){
			$data['po_details']=$this->Purchases_Order_Model->get_purchases_order_details_by_id($po_id);
			$data['poitm_list']=$this->Purchases_Order_Model->get_purchases_order_items_by_id($po_id);
		}
		
		$data['po_id'] =$po_id;
		//get suppliers list
		$data['suppliers'] = $this->Supplier_Model->get_all_supplier_obj();
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['tax_rates_list'] = $this->Tax_Rates_Model->get_all_tax_rates();
		$data['customer_list'] = $this->Customer_Model->get_all_customers();
		$data['status_list'] = $this->Common_Model->get_all_status();
		//$data['pr_list'] = $this->Requisition_Model->get_all_requisition_names();
		$data['rep_list'] = $this->User_Model->getSalesRep();
		
        $this->load->view('purchases_order_add',$data);
    }
	
	
	
	//Requisition product items get
	 public function suggestions($value='')
    {
		$term=$this->input->get('term');
		$data['purchases_order'] = $this->Purchases_Order_Model->get_products_suggestions($term);
		$json = array();
		//echo "Count:".count($data['purchases_order']);
		//print_r($data['purchases_order']);
		foreach ($data['purchases_order'] as $row)
		{
			$product_name=$row['product_name'];
			$product_code=$row['product_code'];
			$product_part_no=$row['product_part_no'];
			$product_oem_part_number=$row['product_oem_part_number'];
			$product_id=$row['product_id'];
			$product_price=$row['product_price'];
			$product_cost=$row['product_cost'];
			$sendParameters="'$product_id','$product_name','$product_code','$product_price'";
			$sendParameters="$product_id,$product_name,$product_code,$product_price";
			$extraName='';
			$extraName.=", Cost: ".number_format($product_cost, 2, '.', ',');
			if($product_part_no) $extraName.=", SAP Code: $product_part_no";
			if($product_oem_part_number) $extraName.=", OEM Part No: $product_oem_part_number";
			
			 $json_itm=array(
			 		'id'=> $row['product_id'],
					'product_id'=> $row['product_id'],
					'product_code'=> $row['product_code'],
					'product_name'=> $row['product_name'],
					'product_price'=> $row['product_price'],
					'product_part_no'=> $row['product_part_no'],
					'product_oem_part_number'=> $row['product_oem_part_number'],
                    'value'=> $row['product_name']." (".$row['product_code'].")",
                    'label'=> $row['product_name']." (".$row['product_code'].")$extraName"
                    );
					array_push($json,$json_itm);
		}		
		echo json_encode($json);		
    }
	
	//Sale details page
	public function po_details()
	{
		
		$po_id=$this->uri->segment('3');
		//$data['po_details']= $this->Purchases_Order_Model->get_po_info($po_id);
		//print_r($data['po_details']);
		$po_details=$this->Purchases_Order_Model->get_po_info($po_id);
		$data['po_details']= $po_details;
		
			//print_r($po_details[0]);
			
			//echo "whe:".$po_details[0]->warehouse_id;
		//echo 'test:'.$po_id;
		
		//get sale item list
		$data['po_item_list']= $this->Purchases_Order_Model->get_po_item_list_by_po_id($po_id);
		
		//$data['customer_details']= $this->Customer_Model->get_customer_info($data['po_details']['customer_id']);
		$data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info($po_details[0]->warehouse_id);
		
		
		
		$data['cr_limit_list'] = $this->Common_Model->get_all_cr_limit();
        $this->load->view('models/purchases_order_print',$data);
	}	
	
	//Purchase Order list
	public function list_purchases_order()
	{
	$requestData= $_REQUEST;
	
	$columns = array( 
		0 =>'po_id', 
		1 => 'po_id',
		2=> 'po_id',
		3 =>'po_id', 
		4 => 'po_id',
		5=> 'po_id'
	);
	
	$data = array();
	//$warehouse_id=0;
	//$usr_permtn_pubvr =$this->Common_Model->is_avalable_for_use_this_link_for_user($this->session->userdata('ss_group_id'),'users');
	//if(!in_multiarray("usrgp_permission_page","all_site", $usr_permtn_pubvr,"usrgp_permission_view",1))
	//{
	//	$warehouse_id=$this->session->userdata('ss_warehouse_id');
	//}
	$purchases_order = $this->Purchases_Order_Model->get_all_purchases_order();
	$totalData = count($purchases_order);
	$totalFiltered = $totalData;  
	
	foreach ($purchases_order as $row){
		$warehouse_id=$row['id'];
	 	$user_id=$this->session->userdata('ss_user_id');
		//$is_avalabel=$this->Warehouse_Model->check_is_user_assigned_to_warehouses($user_id, $warehouse_id);
		//if($is_avalabel)
		{
			
			
		$nestedData=array(); 
		$po_id=$row['po_id'];
		$rep_name='';
		if(!empty($row['rep_id'])){
		$rep_info=$this->User_Model->get_user_info($row['rep_id']);
		//echo $this->db->last_query();
		//print_r($rep_info);
		$rep_name=$rep_info['user_first_name'];
		}
		
		$total_paid_amount='';
		$nestedData[] = $row['po_id'];
		$nestedData[] = $row['po_reference_no'];
		$nestedData[] =$row['name'];
		
		$nestedData[] = date('d/M/Y', strtotime($row['po_datetime']));
		$nestedData[] =$rep_name;
		$nestedData[] = date('d/M/Y', strtotime($row['po_delivery_date']));
		
		$prepared_status_dis='';
		$checked_status_dis='';
		$approved_status_dis='';
		$authorized_status_dis='';
		//check is grn addeble
		$isgrnaddedble=true;
		$printed_status_dis ='';
		
		if (empty($row['po_prepared_status'])) {
		  $prepared_status_dis = '<span class="label label-warning">Pending</span>';
		  $isgrnaddedble=false;
		}else if($row['po_prepared_status']=='Prepared'){
			$prepared_status_dis = '<span class="label label-success">Prepared</span>';
			
		}
		if (empty($row['po_checked_status'])) {
		  $checked_status_dis = '<span class="label label-warning">Pending</span>';
		   $isgrnaddedble=false;
		}else if($row['po_checked_status']=='Checked'){
			$checked_status_dis = '<span class="label label-success">Checked</span>';
		}
		if (empty($row['po_approved_status'])) {
		  $approved_status_dis = '<span class="label label-warning">Pending</span>';
		   $isgrnaddedble=false;
		}else if($row['po_approved_status']=='Approved'){
			$approved_status_dis = '<span class="label label-success">Approved</span>';
		}
		
		if (empty($row['po_authorized_status'])) {
		  $authorized_status_dis = '<span class="label label-warning">Pending</span>';
		  
		}else if($row['po_authorized_status']=='Authorized'){
			$authorized_status_dis = '<span class="label label-success">Authorized</span>';
		}
		
		if (empty($row['printed_status'])) {
		  $printed_status_dis = '<span class="label label-warning">Pending</span>';
		}else if($row['printed_status']=='Printed'){
			$printed_status_dis = '<span class="label label-success">Printed</span>';
		}
		
		$nestedData[] = $prepared_status_dis;
		$nestedData[] = $checked_status_dis;
		$nestedData[] = $approved_status_dis;
		$nestedData[]=$authorized_status_dis;
		$nestedData[] = $printed_status_dis;
		
		
		//$nestedData[] = $row['po_id'];
		$actionTxtDisble='';
		$actionTxtEnable='';
		$actionTxtUpdate='';
		$actionTxtDelete='';
		
		
		
		$url=base_url("purchases_order/po_details?po_id=$po_id");
		
		$actionTxtUpdate='<a onClick="fbs_click('.$row['po_id'].')" data-toggle="modal" href="#" class="btn btn-xs btn-blue tooltips" data-placement="top" data-original-title="Edit suppliers"><i class="clip-zoom-in-2"></i></a> &nbsp;';
		
		$actionTxtViewDetails='<a href="'.base_url().'purchases_order/details/'.$po_id.'" class="btn btn-xs btn-blue tooltips" data-placement="top" data-original-title="Edit suppliers"><i class="fa fa-file-text-o"></i></a> &nbsp;';
		
		
		$des_action='';
	
	$des_action = '<div class="btn-group text-left">
                            <button data-toggle="dropdown" class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                            <li><a href="'.base_url().'purchases_order/details/'.$po_id.'"><i class="fa fa-file-text-o"></i> Purchase Order Details</a></li>';
		
		if ($row['po_prepared_status']) {
			
		}else if ($row['po_checked_status']) {
			
		}else if ($row['po_approved_status']) {
			
		}else if ($row['po_authorized_status']) {
			
		}
		else{		
						$des_action.= '	 <li><a href="'.base_url().'purchases_order/manage/'.$po_id.'"><i class="fa fa-file-text-o"></i> Update Purchase Order Details</a></li>
                            <li><a onClick="fbs_click('.$row['po_id'].')" data-toggle="modal" href="#" data-placement="top" data-original-title="Edit suppliers"><i class="fa fa-print"></i> Print Purchase Order</a></li>';
		}
		
		
		$des_action.= '<li><a href="'.base_url().'purchases/manage/0/'.$po_id.'"><i class="fa fa-file-text-o"></i> Add GRN</a></li>
							
                            </ul></div>';
							
		$nestedData[] = $des_action;
	
	$data[] = $nestedData;
	}//end is avalable
}

	$json_data = array(
			//"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}
}