<?php 
include 'session.php';
extract($_REQUEST);
$act=$action;


switch($act)
{		
	case 'get_product_det_by_pdtsku':
		if(isset($_REQUEST['product_sku']) && !empty($_REQUEST['product_sku'])){
			$pdt_prev_img='NoImageAvailable.png';			
			$rslt_pdt_det = $db->get_a_line("select * from t_purchase_entry_details where p_invoice_det_id='".$_REQUEST['pdt_invDet_id']."' ");
			
			if (!empty($rslt_pdt_det['upload_img'])) { 
				if(file_exists(CAAROOT_DIR."/uploads/product_image/".$rslt_pdt_det['upload_img']))
				{
					$pdt_prev_img = $rslt_pdt_det['upload_img'];	
				}											
			}
			//image url	
			$img_url =BASE_URL.'uploads/product_image/'.$pdt_prev_img;

			$rslt_sales_det = $db->get_a_line("select * from t_sales_entry_details where pdt_sku='".$_REQUEST['product_sku']."' ");

			if(!empty($rslt_sales_det)) $sa = 'Sold';
			else $sa = 'Available';
						
			echo json_encode(array("rslt"=>"1", "pdt_desc"=>$rslt_pdt_det['saree_desc'], "pdt_img_url"=>$img_url, "pdt_price"=>$rslt_pdt_det['purchase_rate'],'pdt_sold' => $sa));
		}
		else{
			echo json_encode(array("rslt"=>"-1")); // error
		}
	
	break;	

	case 'get_product_det_by_purchase' :
		$rslt_pdt_list = $db->get_rsltset("select * from t_purchase_entry_details where 1=1 and IsActive=1 and p_invoice_no='".$_REQUEST['pdt_inv_no']."' ");
		$html = "<table style='border:1px solid #eceeef; margin:5px;'>";
		if(count($rslt_pdt_list) > 0) {
			foreach($rslt_pdt_list as $r){
				$pdt_prev_img='NoImage.png';			
					if($r['upload_img'] != '' && file_exists(CAAROOT_DIR."/uploads/product_image/".$r['upload_img']))
					{
						$pdt_prev_img = $r['upload_img'];	
					}														
				$html .= "<tr ><td style='padding:10px;'>".$r['pdt_sku']."</td><td style='padding:10px;'>".$r['saree_desc']."</td>";
				$html .= '<td style="padding:10px;"><img src="'.BASE_URL.'uploads/product_image/'.$pdt_prev_img.'" width="50px" height="50px" align="absmiddle"/>	</td>';
				$html .='<td style="padding:10px;">'.$r['purchase_rate'].'</td>';
				$html .="</tr>";
			}
		}else $html .="<tr>No Product Found</tr>";

		$html .= "</table>";
		echo $html;
	break;
	
	case 'get_supplier_det_by_sup_id':
				
		if(isset($_REQUEST['supplier_id']) && !empty($_REQUEST['supplier_id'])){
			$rslt_cus_det = $db->get_a_line("select * from m_supplier where sup_id='".$_REQUEST['supplier_id']."' ");
									
			echo json_encode(array("rslt"=>"1", "sup_state"=>$rslt_cus_det['sup_state'],"sup_type" =>$rslt_cus_det['sup_type']));
		}
		else{
			echo json_encode(array("rslt"=>"-1")); // error
		}
		
	break;
	
	case 'get_customer_det_by_cus_id':
				
		if(isset($_REQUEST['customer_id']) && !empty($_REQUEST['customer_id'])){
			$rslt_cus_det = $db->get_a_line("select * from m_customer where cus_id='".$_REQUEST['customer_id']."' ");
									
			echo json_encode(array("rslt"=>"1", "cus_state"=>$rslt_cus_det['cus_state']));
		}
		else{
			echo json_encode(array("rslt"=>"-1")); // error
		}
		
	break;

	
	case 'get_inv_no_by_company_id':
		$rslt_data='';
		
		if(isset($company_id) && !empty($company_id) && !empty($financial_id)){			
			$res_AI = $db->get_a_line("SELECT IFNULL(MAX(`actu_inv_no`), 0) + 1 as disp_inv_no FROM t_purchase_entry_main where 1=1 and company_id='".$company_id."' and financial_id = '".$financial_id."'");				
			$rslt_data = $res_AI['disp_inv_no'];

			$res_pe = $db->get_a_line("SELECT * FROM t_purchase_entry_details as t left join t_purchase_entry_main as t1 on t.p_invoice_no = t1.p_invoice_no  WHERE t1.company_id = '".$company_id."' and t.financial_id = '".$financial_id."' and pdt_sku != '' order by p_invoice_det_id desc");				
			$rslt_data_sku = $res_pe['pdt_sku'];
			if($rslt_data_sku == ""){
				if($company_id == '2' && $financial_id == '3')
					$rslt_data_sku = 'PB20-000';
				else
					$rslt_data_sku = 'HM20-000';	
			}
			
							
		}else if(!isset($financial_id)){
			$res_AI = $db->get_a_line("SELECT IFNULL(MAX(`actu_inv_no`), 0) + 1 as disp_inv_no FROM t_purchase_entry_main where 1=1 and company_id='".$company_id."'");				
			$rslt_data = $res_AI['disp_inv_no'];	
		}
		
		echo json_encode(array("rslt"=>$rslt_data,"pdt_sku"=>$rslt_data_sku)); //success
	break;

	case 'get_bill_no_by_company_id':
		$rslt_data='';
		
		if(isset($company_id) && !empty($company_id) && !empty($financial_id)){	
			$res_AI = $db->get_a_line("SELECT IFNULL(MAX(`actu_bill_no`), 0) + 1 as disp_inv_no FROM t_sales_entry_main where 1=1 and company_id='".$company_id."' and financial_id = '".$financial_id."' ");				
			$rslt_data = $res_AI['disp_inv_no'];				
		}else if(!isset($financial_id)){
			$res_AI = $db->get_a_line("SELECT IFNULL(MAX(`actu_bill_no`), 0) + 1 as disp_inv_no FROM t_sales_entry_main where 1=1 and company_id='".$company_id."' ");				
			$rslt_data = $res_AI['disp_inv_no'];	
		}
		
		echo json_encode(array("rslt"=>$rslt_data)); //success
	break;
	
		
}

?>