<?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.invoicedate) between '$lower' and '$upper' and invtype='i' ORDER BY invoicemid desc";
		}
		else{
		$eve = "select * from res_invoice_master where date(res_invoice_master.invoicedate) between '$lower' and '$upper' and invoicestatus='$status' and invtype='i' ORDER BY invoicemid 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_deliverychallan_master.deliverydate) between '$lower' and '$upper'";
	}
	
	if(isset($_SESSION['qtstatus']))
	{
		$status = $_SESSION['qtstatus'];
		if($_SESSION['qtstatus']=='All')
		{
			$tt2="";
		}
		else
		{
			$tt2=" and deliverystatus='$status'";
		}
	}
	
		
	 $eve = "select * from res_deliverychallan_master where deliverytype='i' $tt1 $tt2 and cmpmid=$cmpmid ORDER BY deliverymid 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++;
				$invoicemid=$rtgby2['deliverymid'];


					
					$qedit="<a href='deliverygenerate.php?id=".$rtgby2['deliverymid']."'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='deliverydelete.php?id=".$rtgby2['deliverymid']."' 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='deliverystatus.php?invoicemid=".$rtgby2['deliverymid']."'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;

					$invoicefordisplay=$rtgby2['deliveryfordisplay'];
					$invoicestatus=$rtgby2['deliverystatus'];
					$invoicenetamount=$rtgby2['deliverynetamount'];

					$invoicedate=date("d-m-Y", strtotime($rtgby2['deliverydate']));
					$invoicestatus=$rtgby2['deliverystatus'];
					$customermid=$rtgby2['customermid'];

					if($invoicestatus=="In Review")
					{
						$invoicestatus = "<span class='label label-sm label-warning'>In Review</span>";
					}
					else if($invoicestatus=="Cancelled")
					{
						$invoicestatus = "<span class='label label-sm label label-danger '>Cancelled</span>";
					}
					else if($qstatus=="Converted To Sales")
					{
						$invoicestatus = "<span class='label label-sm label label-info '>Converted</span>";
					}
					else if($invoicestatus=="Previous")
					{
						$invoicestatus = "<span class='label label-sm label label-info '>Previous</span>";
					}
					else
					{
						$invoicestatus = "<span class='label label-sm label label-info '>$qstatus</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,'invoicemid'=> $invoicemid, 'invoicefordisplay'=> $invoicefordisplay, 'invoicedate'=> $invoicedate, 'invoicestatus'=> $invoicestatus,'cname'=> $cname,'cmobile'=> $cmobile,'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);


?>
