USE [audioitems-test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[usp_Orders_GetRecordsByOrderStatus_NewSystem]
(
@status varchar(2),
@invoice varchar(20),
@top varchar(20)
)
as
begin
declare @qry as varchar(8000);
declare @pre as varchar(10);
set @pre=(select [dbo].[fun_GetOrderPrefix]())
set @qry='SELECT '+@top+'orders.shipmentDetails,case orders.Printed when ''y'' then ''Yes'' else ''No'' end as PrintStatus,PrintedDate,'''+@pre+'''+CONVERT(varchar(10),orders.idorder) as idorder,orderstatus as odstatus, orders.invoiceNo, customers.idcustomer,
orders.ShippingName+'''+' '+'''+orders.ShippingLastName+'',''+orders.shippingStateCode as shipto,orders.shippingStateCode, name+'''+' '+'''+lastName as Name, account, customerCompany,phone, email, orderDate, isnull(total,0) as total, case when orderstatus=1
then ''Pending''
when orderstatus=2 then ''Delivered''
when orderstatus=3 then ''Cancelled''
when orderstatus=4 then ''Paid''
when orderstatus=5 then ''Chargeback''
when orderstatus=6 then ''Refunded''
when orderstatus=7 then ''Payment pending''
end as Status, shipmentTracking,case when CHARINDEX(''-'', transactionResults)<>0 and CHARINDEX(''-'', transactionResults) is not null then
substring(transactionResults,CHARINDEX(''-'', transactionResults)+1,len(transactionResults)-CHARINDEX(''-'', transactionResults)+1) else '''' end as pdate ,
isnull( ( select top 1 Isnull(s.supplierName ,wh.wname) as name from OrderDetail od, Suppliers s ,warehouse wh,vendorOrderDetail vod
where od.idvendoralloc *= s.idSupplier
and od.idWarehouseAlloc *= wh.idWarehouse and od.idorder = orders.idOrder and orders.idorder *= vod.idorder and od.idproduct = vod.idproduct),
( select top 1 Isnull(s.supplierName ,wh.wname) as name from OrderDetail od, Suppliers s ,warehouse wh
where od.idvendoralloc *= s.idSupplier
and od.idWarehouseAlloc *= wh.idWarehouse and od.idorder = orders.idOrder)
)
as ShippedBy
FROM orders, customers where orders.idCustomer=customers.idCustomer';
if @status!=0
begin
if @status=8
begin
set @qry=@qry+' and (orders.viewed is null or orders.viewed=0)'
end
else
begin
set @qry=@qry+' and orderstatus='+@status
end
end
if @invoice!=''
begin
set @qry=@qry+' and invoiceNo='+@invoice
end
set @qry=@qry+' order by orderdate desc';
print @qry
execute (@qry)
end