<?php
require_once('../Db/dbconfig.php');

if($_POST['type'] == 'stockSend')
{
	$adminIds=$_POST['boxescat'];
	$buyrname=$_POST['buyrname'];
	$isinternational=$_POST['isinternational'];
	
	$inv_no=$_POST['inv_no'];
	$invdat=$_POST['invdat'];
	$noofbox=$_POST['noofbox'];
	$userId=$_POST['userId'];

	$insertedCount=0;
	$dubplicateInv=array();
	
	$sqls="SELECT * FROM stock_to_export where invoice_no='".$inv_no."';";
    $ress=mysqli_query($con,$sqls);

    if (mysqli_num_rows($ress) > 0) {
    	$dubplicateInv[] = $inv_no;
	}else{
		$adId = explode(",",$adminIds);

		for($a=0; $a < count($adId); $a++)
		{	
			if($noofbox !=0){
				$adminId = $adId[$a];
				$sql="SELECT * FROM package_to_stock where id=".$adminId.";";
		        $res=mysqli_query($con,$sql);

			    if (mysqli_num_rows($res) > 0) {
				    $row = mysqli_fetch_assoc($res);
					$totalBoxs = $row["totalBoxs"];
					$lotnumber = $row["lotnumber"];
					if($totalBoxs > $noofbox){
						$tqbox=$totalBoxs-$noofbox;
						/*if($tqbox < $noofbox){
							$noofbox = $noofbox - $tqbox;
						}*/
						$query = "INSERT INTO stock_to_export
					(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,author)
					VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.", ".$noofbox.", CURRENT_TIMESTAMP,".$userId.");";
						
						$queryrun = mysqli_query($con, $query);
						if($queryrun > 0)
						{ 
							$last_id = mysqli_insert_id($con);
				
							$query3 = "INSERT INTO stock_to_export_history
						(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,parent_id,author)
						VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.",".$noofbox.", CURRENT_TIMESTAMP,".$last_id.",".$userId.");";
							
							$queryrun3 = mysqli_query($con, $query3);
							if($queryrun3 > 0)
							{
								if($tqbox == 0){
									$query2 = "update package_to_stock set totalBoxs = 0,send_to_quantity=0 where id=".$adminId;
								}else{
									$query2 = "update package_to_stock set totalBoxs = ".$tqbox." where id=".$adminId;
								}
								
								$querysrun2 = mysqli_query($con, $query2);
								if($querysrun2 >0)
								{
									$insertedCount++;
									$noofbox = 0;
								}
							}
						}

						
					}else{
						$noofbox=$noofbox-$totalBoxs;

						$query = "INSERT INTO stock_to_export
					(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,author)
					VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.", ".$totalBoxs.", CURRENT_TIMESTAMP,".$userId.");";
						
						$queryrun = mysqli_query($con, $query);
						if($queryrun > 0)
						{ 
							$last_id = mysqli_insert_id($con);
				
							$query3 = "INSERT INTO stock_to_export_history
						(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,parent_id,author)
						VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.",".$totalBoxs.", CURRENT_TIMESTAMP,".$last_id.",".$userId.");";
							
							$queryrun3 = mysqli_query($con, $query3);
							if($queryrun3 > 0)
							{
								
								$query2 = "update package_to_stock set totalBoxs = 0,send_to_quantity=0 where id=".$adminId;
								
								$querysrun2 = mysqli_query($con, $query2);
								if($querysrun2 >0)
								{
									$insertedCount++;
								}
							}
						}

					}
								    	
				}
			}
			

		}
	}
	
	if($insertedCount == 0){
		$myObj = [ 'status' => false,'msg'=>'Failed to Export products','duplicateInv'=>$dubplicateInv];
	}else{
		$myObj = [ 'status' => true];
	}

	mysqli_close($con);
	header('Content-type: application/json');
	echo json_encode($myObj);
}else if($_POST['type'] == 'stockBoxDelete')
{
	$stadminId=$_POST['editadminId'];
	$noofboxes=$_POST['noofboxes'];
	$userId=$_POST['userId'];
	
	$status = false;

	$sql="select * from package_to_stock where id in (".$stadminId.");";
	$res=mysqli_query($con,$sql);
	$numofRows = mysqli_num_rows($res);
	if ($numofRows > 0) 
	{
		while($rows=mysqli_fetch_assoc($res)){

			if($noofboxes !=0){
				$adDet = explode(",",$rows['admindpt_parent_details']);
				$totalBoxs = $rows['totalBoxs'];
				$updatedCount=0;
				for($a=0; $a < count($adDet); $a++){
					
					$adminSplit = explode("-",$adDet[$a]);
					$insertedBoxes = 0;
					if($totalBoxs < $noofboxes){
						$updateQty = $adminSplit[1] * $totalBoxs;
						$noofboxes = $noofboxes - $totalBoxs;
						$totalBoxsup = 0;
						$insertedBoxes = $totalBoxs;
					}else{
						$updateQty = $adminSplit[1] * $noofboxes;
						$totalBoxsup = $totalBoxs -  $noofboxes;
						$insertedBoxes = $noofboxes;
						$noofboxes = 0;
					}
					$query = "UPDATE admin_dpts set send_to_quantity=send_to_quantity+".$updateQty." WHERE id=".$adminSplit[0].";";

					$queryrun = mysqli_query($con, $query);
					
					if($queryrun >0){
						
						$query = "INSERT INTO package_to_stock_history
						(final_product, send_to_quantity, send_to_qtny_type, status, totalBoxs, admindpt_parent_details,deleted_by, storageId, author, parent_id)
						VALUES('".$rows['final_product']."', ".$rows['send_to_quantity'].",  ".$rows['send_to_qtny_type'].", 1, ".$insertedBoxes.", '".$rows['admindpt_parent_details']."', CURRENT_TIMESTAMP, 0,'".$userId."',".$rows['id']." );";

						$queryrun = mysqli_query($con, $query);
						
						if($queryrun >0){
							
							if($totalBoxsup == 0){
								$query = "DELETE from package_to_stock WHERE id=".$rows['id'].";";
								$queryrun = mysqli_query($con, $query);
								
							}else{
								$query = "UPDATE package_to_stock set totalBoxs = ".$totalBoxsup." WHERE id=".$rows['id'].";";
								$queryrun = mysqli_query($con, $query);
								
							}
						}
					}
				}
				
					
					
				
			}
				    										
		}
	}

	if($noofboxes == 0){
		$status = true;
	}else{
		$status = false;
	}
	
	$myObj = [ 'status' => $status];
	mysqli_close($con);
	header('Content-type: application/json');
	
	echo json_encode($myObj);
}else if($_POST['type'] == 'stockDelete')
{
	$stadminId=$_POST['adminId'];
	$userId=$_POST['userId'];
	
	$status = false;

	$sql="select * from package_to_stock where id in (".$stadminId.");";
	$res=mysqli_query($con,$sql);
	$numofRows = mysqli_num_rows($res);
	if ($numofRows > 0) 
	{
		while($rows=mysqli_fetch_assoc($res)){

			$adDet = explode(",",$rows['admindpt_parent_details']);
			$totalBoxs = $rows['totalBoxs'];
			$lotnumber = $rows["lotnumber"];
			$updatedCount=0;
			for($a=0; $a < count($adDet); $a++){
				
				$adminSplit = explode("-",$adDet[$a]);

				$updateQty = $adminSplit[1] * $totalBoxs;

				$query = "UPDATE admin_dpts set send_to_quantity=send_to_quantity+".$updateQty." WHERE id=".$adminSplit[0].";";

				$queryrun = mysqli_query($con, $query);
				
				if($queryrun >0){
					$updatedCount++;
				}
			}
			if($updatedCount == count($adDet)){
				$query = "INSERT INTO package_to_stock_history
				(lotnumber,final_product, send_to_quantity, send_to_qtny_type, status, totalBoxs, admindpt_parent_details,deleted_by, storageId, author, parent_id)
				VALUES('".$lotnumber."','".$rows['final_product']."', ".$rows['send_to_quantity'].",  ".$rows['send_to_qtny_type'].", 1, ".$rows['totalBoxs'].", '".$rows['admindpt_parent_details']."', CURRENT_TIMESTAMP, '".$rows['storageId']."','".$userId."',".$rows['id']." );";

				$queryrun = mysqli_query($con, $query);
				
				if($queryrun >0){
					$query = "DELETE from package_to_stock WHERE id=".$rows['id'].";";
					$queryrun = mysqli_query($con, $query);
					
					if($queryrun >0){
						$numofRows--;
					}
				}
			}
			
				    										
		}
	}

	if($numofRows == 0){
		$status = true;
	}else{
		$status = false;
	}
	
	$myObj = [ 'status' => $status];
	mysqli_close($con);
	header('Content-type: application/json');
	
	echo json_encode($myObj);
}else if($_POST['type'] == 'exportDelete')
{
	$exportId=$_POST['exportId'];
	$userId=$_POST['userId'];
			
	$status = false;

	$sql="select * from stock_to_export where id in (".$exportId.");";
	$res=mysqli_query($con,$sql);
	$numofRows = mysqli_num_rows($res);
	if ($numofRows > 0) 
	{
		while($rows=mysqli_fetch_assoc($res)){
			$columnnm = 'storage_id';
			if($rows['storage_id'] == null or $rows['storage_id'] == 0){
				$columnnm = 'storageIdml';
			}

			$lotnumber = $rows["lotnumber"];

			$query = "UPDATE package_to_stock set send_to_quantity=".$rows['quantity'].",totalBoxs=totalBoxs+".$rows['no_of_boxes']." WHERE id=".$rows['adminId'].";";

			$queryrun = mysqli_query($con, $query);
			
			if($queryrun >0){
				$query = "INSERT INTO stock_to_export_history
						(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, ".$columnnm.", adminId, final_productId, is_international, no_of_boxes, delete_on,parent_id,author)
						VALUES('".$lotnumber."','".$rows['purchaser_name']."', '".$rows['quantity_type']."', ".$rows['quantity'].", '".$rows['invoice_no']."', '".$rows['invoice_dt']."', '".$rows[$columnnm]."',".$rows['adminId'].", ".$rows['final_productId'].", ".$rows['is_international'].", ".$rows['no_of_boxes'].", CURRENT_TIMESTAMP,".$rows['id'].",".$userId.");";

				$queryrun = mysqli_query($con, $query);
				
				if($queryrun >0){
					$query = "DELETE from stock_to_export WHERE id=".$rows['id'].";";
					$queryrun = mysqli_query($con, $query);
					
					if($queryrun >0){
						$numofRows--;
					}
				}
			}
				

		}
	    										
	}
	
	if($numofRows == 0){
		$status = true;
	}else{
		$status = false;
	}

	$myObj = [ 'status' => $status];
	mysqli_close($con);
	header('Content-type: application/json');
	
	echo json_encode($myObj);
}else if($_POST['type'] == 'validateProductIsExported')
{
	$adminId =$_POST["adminId"];
	
	$sql1="select * from stock_to_export ad where adminId in (".$adminId.");";
	$result = $con->query($sql1);

	if ($result->num_rows > 0) {
		$myObj = [ 'status' => true];
	}else{
		$myObj = [ 'status' => false];
	}
	
	mysqli_close($con);
	header('Content-type: application/json');
	echo json_encode($myObj);
}else if($_POST['type'] == 'multiStockSend')
{	
	$inv_no=$_POST['inv_no'];
	$invdat=$_POST['invdat'];
	$buyrname=$_POST['buyrname'];
	$isinternational=$_POST['isinternational'];

	$adminIdml=$_POST['boxescat'];
	
	$noofbox=$_POST['totalBoxes'];
	$userId=$_POST['userId'];

	$number= count($_POST["boxescat"]);

	$insertedCount =0;
	if($number > 0)
	{
		$sqls="SELECT * FROM stock_to_export where invoice_no='".$inv_no."';";
        $ress=mysqli_query($con,$sqls);

	    if (mysqli_num_rows($ress) == 0) {
	    	for($i=0; $i < $number; $i++)
			{
				$adminIdml=mysqli_real_escape_string($con,$_POST["boxescat"][$i]);
				$noofboxsml=mysqli_real_escape_string($con,$_POST["totalBoxes"][$i]);
				$is_single=mysqli_real_escape_string($con,$_POST["is_single"][$i]);

				$adId = explode(",",$adminIdml);
				if($is_single == '1'){
					
					for($a=0; $a < count($adId); $a++)
					{	
						if($noofboxsml !=0){
							$adminId = $adId[$a];
							$sql="SELECT * FROM package_to_stock where id=".$adminId.";";
							$res=mysqli_query($con,$sql);

							if (mysqli_num_rows($res) > 0) {
								$row = mysqli_fetch_assoc($res);
								$totalBoxs = $row["totalBoxs"];
								$lotnumber = $row["lotnumber"];

								if($totalBoxs > $noofboxsml){
									$tqbox=$totalBoxs-$noofboxsml;

									/*if($tqbox < $noofboxsml){
										$noofboxsml = $noofboxsml - $tqbox;
									}*/
									
									$query = "INSERT INTO stock_to_export
								(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,author)
								VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.", ".$noofboxsml.", CURRENT_TIMESTAMP,".$userId.");";
									
									$queryrun = mysqli_query($con, $query);
									if($queryrun > 0)
									{ 
										$last_id = mysqli_insert_id($con);
							
										$query3 = "INSERT INTO stock_to_export_history
									(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,parent_id,author)
									VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.",".$noofboxsml.", CURRENT_TIMESTAMP,".$last_id.",".$userId.");";
										
										$queryrun3 = mysqli_query($con, $query3);
										if($queryrun3 > 0)
										{
											if($tqbox == 0){
												$query2 = "update package_to_stock set totalBoxs = 0,send_to_quantity=0 where id=".$adminId;
											}else{
												$query2 = "update package_to_stock set totalBoxs = ".$tqbox." where id=".$adminId;
											}
											
											$querysrun2 = mysqli_query($con, $query2);

											if($querysrun2 >0)
											{
												$insertedCount++;
												$noofboxsml = 0;
											}
											
										}
									}
			
									
								}else{
									$noofboxsml=$noofboxsml-$totalBoxs;
			
									$query = "INSERT INTO stock_to_export
								(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,author)
								VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.", ".$totalBoxs.", CURRENT_TIMESTAMP,".$userId.");";
									
									$queryrun = mysqli_query($con, $query);
									if($queryrun > 0)
									{ 
										$last_id = mysqli_insert_id($con);
							
										$query3 = "INSERT INTO stock_to_export_history
									(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,parent_id,author)
									VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.",".$totalBoxs.", CURRENT_TIMESTAMP,".$last_id.",".$userId.");";
										
										$queryrun3 = mysqli_query($con, $query3);
										if($queryrun3 > 0)
										{
											
											$query2 = "update package_to_stock set totalBoxs = 0,send_to_quantity=0 where id=".$adminId;
											
											$querysrun2 = mysqli_query($con, $query2);

											if($querysrun2 >0)
											{
												$insertedCount++;
											}
											
										}
									}
			
								}
								
							}
						}
						
					}
				}else{
					for($a=0; $a < count($adId); $a++)
					{	
						if($noofboxsml !=0){
							$adminId = $adId[$a];
							$sql="SELECT * FROM package_to_stock where id=".$adminId.";";
							$res=mysqli_query($con,$sql);

							if (mysqli_num_rows($res) > 0) {
								$row = mysqli_fetch_assoc($res);
								$totalBoxs = $row["totalBoxs"];
								$lotnumber = $row["lotnumber"];

								if($totalBoxs > $noofboxsml){
									$tqbox=$totalBoxs-$noofboxsml;
									/*if($tqbox < $noofboxsml){
										$noofboxsml = $noofboxsml - $tqbox;
									}*/
									$query = "INSERT INTO stock_to_export
									(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storageIdml, adminId, final_productId, is_international, no_of_boxes, created_at,author)
									VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageID_ml']."',".$adminId.", ".$row['final_product'].", ".$isinternational.", ".$noofboxsml.", CURRENT_TIMESTAMP,".$userId.");";
									
									$queryrun = mysqli_query($con, $query);
									if($queryrun > 0)
									{ 
										$last_id = mysqli_insert_id($con);
							
										$query3 = "INSERT INTO stock_to_export_history
										(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,parent_id,author)
										VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.",".$noofboxsml.", CURRENT_TIMESTAMP,".$last_id.",".$userId.");";
										
										$queryrun3 = mysqli_query($con, $query3);
										if($queryrun3 > 0)
										{
											if($tqbox == 0){
												$query2 = "update package_to_stock set totalBoxs = 0,send_to_quantity=0 where id=".$adminId;
											}else{
												$query2 = "update package_to_stock set totalBoxs = ".$tqbox." where id=".$adminId;
											}
											
											$querysrun2 = mysqli_query($con, $query2);
											if($querysrun2 >0)
											{
												$insertedCount++;
												$noofboxsml = 0;
											}
											
										}
									}
			
									
								}else{
									$noofboxsml=$noofboxsml-$totalBoxs;
			
									$query = "INSERT INTO stock_to_export
									(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storageIdml, adminId, final_productId, is_international, no_of_boxes, created_at,author)
									VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageID_ml']."',".$adminId.", ".$row['final_product'].", ".$isinternational.", ".$totalBoxs.", CURRENT_TIMESTAMP,".$userId.");";
									
									$queryrun = mysqli_query($con, $query);
									if($queryrun > 0)
									{ 
										$last_id = mysqli_insert_id($con);
							
										$query3 = "INSERT INTO stock_to_export_history
										(lotnumber,purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, storage_id, adminId, final_productId, is_international, no_of_boxes, created_at,parent_id,author)
										VALUES('".$lotnumber."','".$buyrname."', '".$row['send_to_qtny_type']."', ".$row['send_to_quantity'].", '".$inv_no."', '".$invdat."', '".$row['storageId']."',".$adminId.", ".$row['final_product'].", ".$isinternational.",".$totalBoxs.", CURRENT_TIMESTAMP,".$last_id.",".$userId.");";
										
										$queryrun3 = mysqli_query($con, $query3);
										if($queryrun3 > 0)
										{
											
											$query2 = "update package_to_stock set totalBoxs = 0,send_to_quantity=0 where id=".$adminId;
											
											$querysrun2 = mysqli_query($con, $query2);
											
											if($querysrun2 >0)
											{
												$insertedCount++;
											}
											
										}
									}
			
								}
								
							}
						}
						
					}
				}
			    
			}
			if($insertedCount == 0){
				$myObj = [ 'status' => false,'msg'=>'Failed to Export products'];
			}else{
				$myObj = [ 'status' => true,'msg'=>$number." Products Exported"];
			}
		}else{
			$myObj = [ 'status' => false,'msg'=>'Invoice No Already Exist!'];
		}
		
	}else{
		$myObj = [ 'status' => false,'msg'=>'No Rows Found'];
	}

	mysqli_close($con);
	header('Content-type: application/json');
	echo json_encode($myObj);
}else if($_POST['type'] =='viewStock'){
	$p_id = $_POST['id'];
 	$q = "select ad.id,group_concat(ad.id order by ad.id asc) as ids ,sum(ad.totalBoxs) as totalBoxs,frmd.depart as frmdpt,p.item as finalProdname,p.id as finalprodID,ad.send_to_quantity ,sendqt.q_name,sendqt.id as qttypeId,ad.created_at as senddttime
		from package_to_stock ad ,department frmd,final_product p ,quantity_type sendqt 
		where frmd.id = 2 and ad.final_product = p.id and ad.send_to_qtny_type = sendqt.id and ad.send_to_quantity !=0 and ad.id in (".$p_id.") group by ad.send_to_quantity order by ad.created_at desc";
 
   	$result=mysqli_query($con,$q);
		
	if(mysqli_num_rows($result) > 0)
    { 
		while($row = mysqli_fetch_assoc($result)) 
		{
			$date = new DateTime($row['senddttime'], new DateTimeZone('MST'));
			$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
			$row['senddttime']= $date->format('Y-m-d H:i:s');
			$data[]=$row;
			
		}
		$myObj = [ 'status' => true,'data'=>$data];
	}
	else
	{
		$myObj = [ 'status' => false];
	}
	mysqli_close($con);
	header('Content-type: application/json');
	echo json_encode($myObj);
}else if($_POST['type'] =='getTotalQtyFromPackage'){
	$p_id = $_POST['id'];

	$status = false;
	$totaltQty = 0;

 	$q = "select send_to_quantity,admindpt_parent_details from package_to_stock where id =".$p_id.";";

   	$result=mysqli_query($con,$q);
		
	if (mysqli_num_rows($result) > 0) {
		$row = mysqli_fetch_assoc($result);
		$sendQty = $row['send_to_quantity'];

		$adminDt =  explode(",",$row['admindpt_parent_details']);

		$packageSendQty = 0;

		for($i=0; $i < count($adminDt); $i++){
			$adminId = explode("-",$adminDt[$i]);
			$q1 = "select send_to_quantity from admin_dpts where id =".$adminId[0];

		   	$result1=mysqli_query($con,$q1);
				
			if(mysqli_num_rows($result1) > 0)
		    { 
				$row1 = mysqli_fetch_assoc($result1);
				
				$packageSendQty += $row1['send_to_quantity'];
				
			}
		}

		$totaltQty = $packageSendQty+$sendQty;
		$status = true;
		
	}

	$myObj = [ 'status' => $status,'data'=>$totaltQty];
	mysqli_close($con);
	header('Content-type: application/json');
	echo json_encode($myObj);
}else if($_POST['type'] =='viewExport'){
	$p_id = $_POST['id'];
 	$q = "select group_concat(ste.id order by ste.id asc) as ids,ste.id,pm.name as purchaser_name ,qt.q_name ,qt.id as qtytypeId,ste.quantity ,DATE(ste.invoice_dt) as invoice_dt ,ste.invoice_no ,
		fp.item,sum(ste.no_of_boxes) as no_of_boxes,ste.created_at 
		from stock_to_export ste,quantity_type qt ,final_product fp,purchaser_master pm where ste.quantity_type = qt.id and ste.final_productId = fp.id and ste.purchaser_name=pm.id and ste.id in (".$p_id.")
		group by ste.quantity order by ste.created_at desc";
 
   	$result=mysqli_query($con,$q);
		
	if(mysqli_num_rows($result) > 0)
    { 
		while($row = mysqli_fetch_assoc($result)) 
		{
			$data[]=$row;
			
		}
		$myObj = [ 'status' => true,'data'=>$data];
	}
	else
	{
		$myObj = [ 'status' => false];
	}
	mysqli_close($con);
	header('Content-type: application/json');
	echo json_encode($myObj);
}else if($_POST['type'] == 'exportBoxDelete')
{
	$stadminId=$_POST['editadminId'];
	$noofboxes=$_POST['noofboxes'];
	$userId=$_POST['userId'];
	
	$status = false;

	$sql="select * from stock_to_export where id in (".$stadminId.");";
	$res=mysqli_query($con,$sql);
	if (mysqli_num_rows($res) > 0) 
	{
		while($rows=mysqli_fetch_assoc($res)){

			if($noofboxes !=0){
				$columnnm = 'storage_id';
				if($rows['storage_id'] == null or $rows['storage_id'] == 0){
					$columnnm = 'storageIdml';
				}
				$totalBoxs = $rows['no_of_boxes'];
				$insertedBoxes = 0;
				if($totalBoxs < $noofboxes){
					$noofboxes = $noofboxes - $totalBoxs;
					$totalBoxsup = 0;
					$insertedBoxes = $totalBoxs;
				}else{
					$totalBoxsup = $totalBoxs -  $noofboxes;
					$insertedBoxes = $noofboxes;
					$noofboxes = 0;
				}

				$query = "INSERT INTO stock_to_export_history
						(purchaser_name, quantity_type, quantity, invoice_no, invoice_dt, ".$columnnm.", adminId, final_productId, is_international, no_of_boxes, delete_on,parent_id,author)
						VALUES('".$rows['purchaser_name']."', '".$rows['quantity_type']."', ".$rows['quantity'].", '".$rows['invoice_no']."', '".$rows['invoice_dt']."', '".$rows[$columnnm]."',".$rows['adminId'].", ".$rows['final_productId'].", ".$rows['is_international'].", ".$insertedBoxes.", CURRENT_TIMESTAMP,".$rows['id'].",".$userId.");";

				$queryrun = mysqli_query($con, $query);
				
				if($queryrun >0){
					
					$query = "UPDATE package_to_stock set send_to_quantity=".$rows['quantity'].",totalBoxs = totalBoxs+".$insertedBoxes." WHERE id=".$rows['adminId'].";";
					$queryrun = mysqli_query($con, $query);

					if($queryrun >0){
						if($totalBoxsup == 0){
							$query = "DELETE from stock_to_export WHERE id=".$rows['id'].";";
							$queryrun = mysqli_query($con, $query);
							
						}else{
							$query = "UPDATE stock_to_export set no_of_boxes=".$totalBoxsup." WHERE id=".$rows['id'].";";
							$queryrun = mysqli_query($con, $query);
							
						}
					}
					
				}
				
			}
				    										
		}
	}

	if($noofboxes == 0){
		$status = true;
	}else{
		$status = false;
	}
	
	$myObj = [ 'status' => $status,'$query'=>$query];
	mysqli_close($con);
	header('Content-type: application/json');
	
	echo json_encode($myObj);
}


?>