<?php
require_once('config.php');

$error_message;
$valid = true;

$json_return = array();
$json_img_link = array();


$Folder=$_REQUEST['Folder'];
$dvc_id=$_REQUEST['dvc_id'];
$MobileURL=$_REQUEST['MobileURL'];
$MobileURL=$_REQUEST['MobileURL'];
$LOGINID=$_REQUEST['LOGINID'];


$uploaddir = "C:\\wamp64\\www\\".$Folder."\\mobile_image_temp\\temp_".$LOGINID."_".$dvc_id."\\";

	if (!is_dir($uploaddir)) {
		mkdir($uploaddir, 0777, true);
		mkdir($uploaddir."\\thumbnail\\", 0777, true);
	}

$MobileURLFullSize = $MobileURL . "/mobile_image_temp/temp_".$LOGINID."_".$dvc_id."/";
$MobileURLTnSize = $MobileURL . "/mobile_image_temp/temp_".$LOGINID."_".$dvc_id."/thumbnail/";

$exportdir = "C:\\wamp64\\www\\". $Folder ."\\mobile_image_temp\\temp_".$LOGINID."_".$dvc_id."\\";
$exportdirtn = "C:\\wamp64\\www\\". $Folder ."\\mobile_image_temp\\temp_".$LOGINID."_".$dvc_id."\\thumbnail\\";	


$sql = "SELECT 		

itm_mst.itm_mst_stockno,
itm_mst.itm_mst_costcenter,
itm_mst.itm_mst_account,
itm_mst.itm_mst_mstr_locn,			
itm_mst.itm_mst_desc,	
itm_mst.itm_mst_issue_uom,			
itm_mst.itm_mst_issue_price,
itm_mst.itm_mst_ttl_oh,
itm_mst_ext_desc = COALESCE(cast(itm_mst.itm_mst_ext_desc as varchar (255)),''),	
itm_det.itm_det_part_deac_status,
itm_det.itm_det_order_pt,			
itm_det_ttl_oh= COALESCE(itm_det_ttl_oh,0),itm_mst_mstr_locn, 					
itm_mst_order_rule, 					
itm_mst.site_cd, 					
itm_mst.RowID,
attachment	= COALESCE(itm_ref.attachment,''),
file_name	= COALESCE(itm_ref.file_name,'')

FROM 		itm_mst 	


INNER JOIN itm_det 	
ON 			( itm_mst.site_cd = itm_det.site_cd ) 	
AND			( itm_mst.RowID = itm_det.mst_RowID ) 	

INNER JOIN itm_sts 	
ON			(itm_mst.site_cd = itm_sts.site_cd ) 	
AND			( itm_det.itm_det_part_deac_status = itm_sts.itm_sts_status ) 

LEFT OUTER 
JOIN		itm_loc
ON			itm_mst.site_cd = itm_loc.site_cd
AND			itm_mst.RowID = itm_loc.mst_RowID

AND			itm_mst.itm_mst_mstr_locn = itm_loc.itm_loc_stk_loc

LEFT OUTER 
JOIN		itm_ref (NOLOCK)
ON			itm_mst.site_cd = itm_ref.site_cd
AND			itm_mst.rowid = itm_ref.mst_rowid
AND 		itm_ref.type  IN ('P')


LEFT OUTER JOIN itm_sup 	
ON			( itm_mst.site_cd = itm_sup.site_cd ) 	
AND			( itm_mst.RowID = itm_sup.mst_RowID ) 	
AND			( itm_mst.itm_mst_rec_supplier = itm_sup.itm_sup_supplier ) 

WHERE		( (itm_mst_ttl_oh - itm_det_ttl_hard_resrv - itm_det_ttl_short + itm_det_pr_due_in + itm_det_due_in) < itm_det_order_pt ) 
AND 		( (itm_mst_ttl_oh - itm_det_ttl_hard_resrv - itm_det_ttl_short + itm_det_pr_due_in + itm_det_due_in) < itm_det_maximum ) 
AND			( itm_det_maximum  > 0 ) 
AND			( itm_det_order_pt  > 0 )
AND			( itm_sts_typ_cd = 'ACTIVE' ) 
AND			( itm_mst_order_rule = 'T' ) 
AND 		( itm_mst.site_cd = '".$_REQUEST['site_cd']."' )   

AND (((itm_loc_oh_qty - itm_loc_hard_resrv - itm_loc_short_qty + itm_loc_pr_due_in + itm_loc_due_in) < itm_loc_order_pt )
			AND         ((itm_loc_oh_qty - itm_loc_hard_resrv - itm_loc_short_qty + itm_loc_pr_due_in + itm_loc_due_in) < itm_loc_maximum )
			AND         ( itm_det_maximum  > 0 )
			AND         ( itm_det_order_pt  > 0 )
			AND         ( itm_mst_order_rule = 'L' )  OR ((itm_mst_ttl_oh - itm_det_ttl_hard_resrv - itm_det_ttl_short + itm_det_pr_due_in + itm_det_due_in) < itm_det_order_pt )
			AND         ((itm_mst_ttl_oh - itm_det_ttl_hard_resrv - itm_det_ttl_short + itm_det_pr_due_in + itm_det_due_in) < itm_det_maximum )
			AND         ( itm_det_maximum  > 0 )
			AND         ( itm_det_order_pt  > 0 )
			AND         ( itm_mst_order_rule = 'T' ))

UNION ALL SELECT 	
itm_mst_b.itm_mst_stockno,
itm_mst_b.itm_mst_costcenter,
itm_mst_b.itm_mst_account,
itm_mst_b.itm_mst_mstr_locn,			
itm_mst_b.itm_mst_desc,	
itm_mst_b.itm_mst_issue_uom,			
itm_mst_b.itm_mst_issue_price,
itm_mst_b.itm_mst_ttl_oh,
itm_mst_ext_desc = COALESCE(cast(itm_mst_b.itm_mst_ext_desc as varchar (255)),''),	
itm_det_b.itm_det_part_deac_status,
itm_det_b.itm_det_order_pt,			
itm_det_b= COALESCE(itm_det_ttl_oh,0),itm_mst_mstr_locn, 					
itm_mst_order_rule, 					
itm_mst_b.site_cd, 					
itm_mst_b.RowID,
attachment	= COALESCE(itm_ref.attachment,''),
file_name	= COALESCE(itm_ref.file_name,''),
itm_ref.RowID AS file_RowID	

FROM 		itm_mst itm_mst_b 	

INNER JOIN itm_det itm_det_b 	
ON			( itm_mst_b.site_cd = itm_det_b.site_cd ) 	
AND			( itm_mst_b.RowID = itm_det_b.mst_RowID ) 	




LEFT OUTER 
JOIN		itm_ref (NOLOCK)
ON			itm_mst_b.site_cd = itm_ref.site_cd
AND			itm_mst_b.rowid = itm_ref.mst_rowid
AND 		itm_ref.type  IN ('P')


INNER JOIN itm_loc 	
ON 			( itm_mst_b.site_cd = itm_loc.site_cd ) 	
AND			( itm_mst_b.RowID = itm_loc.mst_RowID ) 	

INNER JOIN itm_sts itm_sts_b 	
ON			( itm_det_b.site_cd = itm_sts_b.site_cd ) 	
AND			( itm_det_b.itm_det_part_deac_status = itm_sts_b.itm_sts_status ) 	


LEFT OUTER JOIN itm_sup itm_sup_b 	
ON			( itm_mst_b.site_cd = itm_sup_b.site_cd ) 	
AND			( itm_mst_b.RowID = itm_sup_b.mst_RowID ) 	
AND			( itm_mst_b.itm_mst_rec_supplier = itm_sup_b.itm_sup_supplier ) 

WHERE		( (itm_loc_oh_qty - itm_loc_hard_resrv - itm_det_ttl_short + itm_loc_pr_due_in + itm_loc_due_in) < itm_loc_order_pt ) 
AND 		( (itm_loc_oh_qty - itm_loc_hard_resrv - itm_det_ttl_short + itm_loc_pr_due_in + itm_loc_due_in) < itm_loc_maximum )
AND			( itm_det_b.itm_det_maximum  > 0 ) 
AND			( itm_det_b.itm_det_order_pt  > 0 ) 
AND			( itm_sts_b.itm_sts_typ_cd = 'ACTIVE' )
AND			( itm_mst_b.itm_mst_order_rule = 'L' ) 
AND 		( itm_mst_b.site_cd = '".$_REQUEST['site_cd']."' ) 
AND (((itm_loc_oh_qty - itm_loc_hard_resrv - itm_loc_short_qty + itm_loc_pr_due_in + itm_loc_due_in) < itm_loc_order_pt )
			AND         ((itm_loc_oh_qty - itm_loc_hard_resrv - itm_loc_short_qty + itm_loc_pr_due_in + itm_loc_due_in) < itm_loc_maximum )
			AND         ( itm_det_maximum  > 0 )
			AND         ( itm_det_order_pt  > 0 )
			AND         ( itm_mst_order_rule = 'L' )  OR ((itm_mst_ttl_oh - itm_det_ttl_hard_resrv - itm_det_ttl_short + itm_det_pr_due_in + itm_det_due_in) < itm_det_order_pt )
			AND         ((itm_mst_ttl_oh - itm_det_ttl_hard_resrv - itm_det_ttl_short + itm_det_pr_due_in + itm_det_due_in) < itm_det_maximum )
			AND         ( itm_det_maximum  > 0 )
			AND         ( itm_det_order_pt  > 0 )
			AND         ( itm_mst_order_rule = 'T' ))";
						
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (dft_mst)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
}

$json = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
		
		$site_cd = $row['site_cd'];
		$RowID = $row['RowID'];
		$itm_mst_stockno = $row['itm_mst_stockno'];
		$itm_mst_costcenter = $row['itm_mst_costcenter'];
		$itm_mst_account = $row['itm_mst_account'];
		$itm_mst_mstr_locn = $row['itm_mst_mstr_locn'];
		$itm_mst_desc = $row['itm_mst_desc'];
		$itm_mst_issue_uom = $row['itm_mst_issue_uom'];
		$itm_mst_issue_price = $row['itm_mst_issue_price'];
		$itm_mst_ttl_oh = $row['itm_mst_ttl_oh'];
		$itm_mst_ext_desc = $row['itm_mst_ext_desc'];
		$itm_det_part_deac_status = $row['itm_det_part_deac_status'];
		$itm_det_order_pt = $row['itm_det_order_pt'];
		$itm_det_ttl_oh = $row['itm_det_ttl_oh'];
		
		
		
		$file_name = $row['file_name'];

		$attachment;
		$fileName;
		
		if(empty($file_name)){
			
			$attachment = Null;
			$fileName =Null;
			
		}else{
			
			$Photo = $row['attachment'];
			$file_Name = $row['file_name'];
			
			$file_RowID = $row['file_RowID'];
						
			$file_Name = generateUniqueName($file_Name,$file_RowID);
			
			$exportFullPathFileName = $exportdir.$file_Name;
			$exportFullPathTNFileName = $exportdirtn.$file_Name;
				  
			$publicFullPathFileName = $MobileURLFullSize.$file_Name;		  
			$publicFullPathTNFileName = $MobileURLTnSize.$file_Name;
				  
			$publicFullPathFileName = $MobileURLFullSize.$file_Name;		  
			$publicFullPathTNFileName = $MobileURLTnSize.$file_Name;	 
				   
				   
			If ($file = fopen($exportFullPathFileName,"w")) {
				if (fwrite($file, $Photo)==false) {
				echo "Error Write Files";
				}
				fclose($file);
			}	

			If ($file = fopen($exportFullPathTNFileName,"w")) {
				if (fwrite($file, $Photo)==false) {
				echo "Error Write Files";
				}
				fclose($file);
			}		  

			$attachment = $publicFullPathFileName;
			$fileName =$file_Name;
		}
		
		
		$json_img_link['site_cd'] = $site_cd;
		$json_img_link['RowID'] = $RowID;
		$json_img_link['itm_mst_stockno'] = $itm_mst_stockno;
		$json_img_link['itm_mst_costcenter'] = $itm_mst_costcenter;
		$json_img_link['itm_mst_account'] = $itm_mst_account;
		$json_img_link['itm_mst_mstr_locn'] = $itm_mst_mstr_locn;
		$json_img_link['itm_mst_desc'] = $itm_mst_desc;
		$json_img_link['itm_mst_issue_uom'] = $itm_mst_issue_uom;
		$json_img_link['itm_mst_issue_price'] = $itm_mst_issue_price;
		$json_img_link['itm_mst_ttl_oh'] = $itm_mst_ttl_oh;
		$json_img_link['itm_mst_ext_desc'] = $itm_mst_ext_desc;
		$json_img_link['itm_det_part_deac_status'] = $itm_det_part_deac_status;
		$json_img_link['itm_det_order_pt'] = $itm_det_order_pt;
		$json_img_link['itm_det_ttl_oh'] = $itm_det_ttl_oh;
		
		$json_img_link['attachment'] = $attachment;
		$json_img_link['fileName'] = $fileName;
		array_push($json_return, $json_img_link);
						
     }
} while ( sqlsrv_next_result($stmt) );

if ($valid) {
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	
	returnData($json_return);	
}

function generateUniqueName($file_name,$image_count) {

    $unique_name = "CMMS_IMAGE_" . $image_count . ".jpeg"; 
    return $unique_name;
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',	
	'data' => $json);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',	
	'data' => $json);
	
	echo json_encode($returnData);
	
	
		
	exit();
}
	


?>