CREATE PROC [dbo].[xp_report_for_1c_new]
@operday datetime
AS
set ANSI_NULLS ON
set QUOTED_IDENTIFIER OFF
GO
ALTER procedure [dbo].[xp_report_for_1c_new]
@operday datetime = null
as
set nocount on
declare @operdaystart int
set @operdaystart = convert(int,convert(varchar(8),@operday,112))
--берем первые 5000 строк
SELECT top 5000
h.id cheqid
INTO
#cheque
FROM
chequehead h
WHERE
operday = ISNULL(@operdaystart,operday)
IF EXISTS(SELECT * FROM #cheque)
BEGIN
select '<?xml version="1.0" encoding="windows-1251"?>' XML_HEADER
select
1 TAG,
null parent,
'Отчет' [REPORTHEAD!1!Desc],
convert(int, null) [DAY!2!OPERDAY],
convert(int, null) [CASH!3!CASHNUMBER],
convert(varchar(20), null) [GOOD!4!GOODSITEM],
convert(decimal(19,2), null) [GOOD!4!SUMMA],
convert(decimal(19,2), null) [GOOD!4!QUANTITY],
convert(decimal(19,2), null) [GOOD!4!PRICE],
convert(decimal(19,2), null) [GOOD!4!PRICEWITHOUTDISC],
convert(decimal(19,2), null) [GOOD!4!NDS],
convert(varchar(10), null) [GOOD!4!GOODSUNIT]
union all
select distinct 2, 1,null,
operday,
null,
null,null,null,null,null,null,null--,null
from
chequehead h (nolock)
join chequepos p (nolock) on p.chequeid=h.id
JOIN #cheque h1 ON h1.cheqid = h.id
union all
select distinct 3, 2,null,
operday,
cash_code ,
null,null,null,null,null,null,null--,null
from
chequehead h (nolock)
join chequepos p (nolock) on p.chequeid=h.id
JOIN #cheque h1 ON h1.cheqid = h.id
union all
select 4, 3,null,
operday,
cash_code ,
code goodsitem,
-- cash_code ,
convert(decimal(19,2),sum(p.summa)) summa,
convert(decimal(19,3),sum(quant)) quantity,
convert(decimal(19,2),p.Price) PRICE,
convert(decimal(19,2),p.PriceWithoutDisc) PRICEWITHOUTDISC,
convert(decimal(19,2), NDSx1) NSD,
convert(varchar(10), GoodsUnit) GoodsUnit
from
chequehead h (nolock)
join chequepos p (nolock) on p.chequeid=h.id
JOIN #cheque h1 ON h1.cheqid = h.id
left JOIN goodsindepart gd (nolock) ON gd.goodscode = p.Code
left JOIN depart d (nolock) ON d.departnumber = GrCode AND d.id = gd.departid
group by
cash_code,
operday,
code,
p.Price,
PriceWithoutDisc,
NDSx1,
GoodsUnit
order BY [DAY!2!OPERDAY], [CASH!3!CASHNUMBER], [GOOD!4!GOODSITEM]
for xml explicit
END