$myServer = "FALCOA-PC\SQL2005";
$myUser = "sa";
$myPass = "taweesak";
$myDB = "ONE";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//mysql_db_query($db,"SET NAMES TIS620");
$codeline = -1;
$x=O1;
$cc=Cancelled;
$strSQL ="select i.ReceiptNum,i.DocNum,i.DocTotal,i.CardName,convert(char,i.DocDueDate,101) as docduedate,v.ItemCode,
v.Dscription,v.Quantity,v.Price,v.VatGroup,v.SlpCode,(((v.Price*v.Quantity)*100)/107)
as Amt,v.DocDate,v.DocEntry,c.DocNum,convert(char,c.DocDate,101) as date,s.SlpName,v.ObjType,i.DiscSum,i.DpmAmnt,v.WtLiable
from OINV i inner join INV1 v
on i.DocEntry = v.DocEntry
inner join ORCT c
on i.ReceiptNum=c.DocNum
inner join OSLP s
on i.SlpCode=s.SlpCode
where c.DocDate BETWEEN '".$d1."' and '".$d2."' and s.SlpCode='".$sl."' and c.JrnlMemo!='".$cc."'
union
select o.ReceiptNum,o.DocNum,o.DocTotal,o.CardName,convert(char,o.DocDueDate,101) as docduedate,n.ItemCode,
n.Dscription,n.Quantity,n.Price,n.VatGroup,n.SlpCode,n.Price,n.DocDate,n.DocEntry,c.DocNum,convert(char,c.DocDate,101) as date,s.SlpName,n.ObjType,o.DiscSum,o.DpmAmnt,n.WtLiable
from ORIN o inner join RIN1 n
on o.DocEntry = n.DocEntry
inner join ORCT c
on o.ReceiptNum=c.DocNum
inner join OSLP s
on o.SlpCode=s.SlpCode
where c.DocDate BETWEEN '".$d1."' and '".$d2."' and s.SlpCode='".$sl."' and c.JrnlMemo!='".$cc."'
union
select p.ReceiptNum,p.DocNum,p.DocTotal,p.CardName,convert(char,p.DocDueDate,101) as docduedate,d.ItemCode,
d.Dscription,d.Quantity,d.Price,d.VatGroup,d.SlpCode,(((d.Price*d.Quantity)*100)/107)
as Amt,d.DocDate,d.DocEntry,c.DocNum,convert(char,c.DocDate,101) as date,s.SlpName,d.ObjType,p.DiscSum,p.DpmAmnt,d.WtLiable
from ODPI p inner join DPI1 d
on p.DocEntry = d.DocEntry
inner join ORCT c
on p.DocNum=c.DocNum
inner join OSLP s on
p.SlpCode=s.SlpCode
where c.DocDate BETWEEN '".$d1."' and '".$d2."' and s.SlpCode='".$sl."' and c.JrnlMemo!='".$cc."'order by i.ReceiptNum";
//echo $strSQL;
$result = mssql_query($strSQL);