<?php
session_start();
	include("connect.php");	
	header("Access-Control-Allow-Origin: *");
$cmpmid=$_SESSION['linkarisecmpmid'];
	//$lower = $_SESSION['qtdtlower'];
	//$upper = $_SESSION['qtdtupper'];
	//$status = $_SESSION['qtstatus'];
		/* if($status=='All'){
		$eve = "select * from res_invoice_master where date(res_invoice_master.orderdate) between '$lower' and '$upper' and invtype='i' ORDER BY ordermid desc";
		}
		else{
		$eve = "select * from res_invoice_master where date(res_invoice_master.orderdate) between '$lower' and '$upper' and orderstatus='$status' and invtype='i' ORDER BY ordermid desc";
		}
	
	$re = mysqli_query($conn, $eve); */

	$tt1="";
	$tt2="";
	
	if(isset($_SESSION['qtdtlower']) && isset($_SESSION['qtdtupper']))
	{
		$lower = $_SESSION['qtdtlower'];
		$upper = $_SESSION['qtdtupper'];
	
		$tt1=" and date(res_salesorder_master.orderdate) between '$lower' and '$upper'";
	}
	
	if(isset($_SESSION['qtstatus']))
	{
		$status = $_SESSION['qtstatus'];
		if($_SESSION['qtstatus']=='All')
		{
			$tt2="";
		}
		else
		{
			$tt2=" and orderstatus='$status'";
		}
	}
	
		
	 $eve = "select * from res_salesorder_master where ordertype='i' $tt1 $tt2 and cmpmid=$cmpmid ORDER BY ordermid desc";
	$re = mysqli_query($conn, $eve) or die("Query Failed"); 

	$response = array();
	$posts = array();

	$result="0";

	if(mysqli_num_rows($re) > 0)
	{
		$sno=0;
		 while($rtgby2 = mysqli_fetch_assoc($re))
		 {

				$sno++;
				$ordermid=$rtgby2['ordermid'];


					
					$qedit="<a href='salesordergenerate.php?id=".$rtgby2['ordermid']."'data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit fa-lg' >  </a>";
					
					//$tdelete="<a href='typedelete.php?id=".$rt['tmid']."' > Delete </a>";
				
					$qdelete="<a href='salesorderdelete.php?id=".$rtgby2['ordermid']."' onclick='return confirm(&#39;Are you sure you want to delete ?&#39;);' data-toggle='tooltip' data-placement='top' title='Delete' class='far fa-trash-alt fa-lg'>  </a>";
					$view="<a href='salesorderstatus.php?ordermid=".$rtgby2['ordermid']."'data-toggle='tooltip' data-placement='top' title='View' class='fa fa-eye fa-lg' >  </a>";
					
			
					
					$qedit1="";
					$op=$view."&nbsp;&nbsp;&nbsp; ".$qedit."&nbsp;&nbsp;&nbsp;".$qdelete;

					$orderfordisplay=$rtgby2['orderfordisplay'];
					$orderstatus=$rtgby2['orderstatus'];
					$ordernetamount=$rtgby2['ordernetamount'];

					$orderdate=date("d-m-Y", strtotime($rtgby2['orderdate']));
					$orderstatus=$rtgby2['orderstatus'];
					$customermid=$rtgby2['customermid'];

					if($orderstatus=="In Review")
					{
						$orderstatus = "<span class='label label-sm label-warning'>In Review</span>";
					}
					else if($orderstatus=="Cancelled")
					{
						$orderstatus = "<span class='label label-sm label label-danger '>Cancelled</span>";
					}
					else if($orderstatus=="Converted To Sales")
					{
						$orderstatus = "<span class='label label-sm label label-info '>Converted</span>";
					}
					else if($orderstatus=="Previous")
					{
						$orderstatus = "<span class='label label-sm label label-info '>Previous</span>";
					}
					else
					{
						$orderstatus = "<span class='label label-sm label label-info '>$orderstatus</span>";
					}
					$cname="";
					$cmobile="";
					$eve7="select * from res_party_master where partymid=$customermid";
					$re7 = mysqli_query($conn, $eve7);
					while($rt7 = mysqli_fetch_assoc($re7))
					{
							$cname=$rt7['partyname'];
							$cmobile=$rt7['partymobile'];
					}

				$posts[] = array('sno'=> $sno,'ordermid'=> $ordermid, 'orderfordisplay'=> $orderfordisplay, 'orderdate'=> $orderdate, 'orderstatus'=> $orderstatus,'cname'=> $cname,'cmobile'=> $cmobile,'ordernetamount'=> $ordernetamount,'op'=>$op);
		 }

		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('data' => $posts)));
		mysqli_close($conn);
		return;
	}

   else
   {
		$result="0";
		$msg="Invalid Error";

		$posts[] = array('p1'=> $result,'p2'=> $msg);

		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('item' => $posts)));
   }

	mysqli_close($conn);


?>
