export const cycleCountCheck = `
-- Define the structure of the result set from the stored procedure
DECLARE @results TABLE (    
IdLocation INT,
LastMoveDate Date

)
-- insert into the temp table
insert into @results
select IdLagerAbteilung, MAX(CaSE WHEN CONVERT(char(10), Buchungsdatum, 120) IS NULL THEN '1900-01-01' ELSE CONVERT(char(10), Buchungsdatum, 120) END) AS LastLocMov
from AlplaPROD_test1.dbo.V_LagerBuchungen x(nolock)

group by IdLagerAbteilung

select * from (
select x.IdLagerAbteilung as laneID, 
x.IdWarenLager as warehouseID, 
w.Bezeichnung as warehouseName,
w.LagerTyp as warehouseIDTyp,
w.Standort as warehouseLocation,
x.Bezeichnung as Description, 
LastMoveDate, 
CASE WHEN CONVERT(char(10), i.Datum, 120) is null then getdate() - 365 else CONVERT(char(10), i.Datum, 120) end as LastInv,
--create the types of warehouses to choose from
case
-- empty
when (sum(EinlagerungsMengeSum) is null and  Datum < LastMoveDate) or (
(sum(EinlagerungsMengeSum) is null and  Datum < DATEADD(day, -[ageOfRow], getdate()))
) then 'EMPTY' 
-- finished goods
when w.LagerTyp = 2  and w.Standort = 10 then 'FG' 
-- external finished goods
when w.LagerTyp = 2  and w.Standort = 20 then 'EXTERNAL' 
-- silos
when w.LagerTyp in (3) and x.MaterialSilo = 1 then 'BULK'
-- MATERIALS
when w.LagerTyp = 3 and x.MaterialSilo = 0 then 'MATERIALS'

-- MATERIALS
when w.LagerTyp = 11 then 'WASTE'
-- MATERIALS
when w.LagerTyp = 9 then 'PACKAGING'

end as rowType,
CASE  WHEN DateDiff(DAY,i.Datum,getDate()) is null then 1000 else DateDiff(DAY,i.Datum,getDate()) end  as DaysSinceLast

from AlplaPROD_test1.dbo.T_LagerAbteilungen as x (NOLOCK)

-- last move
left join
@results as b on
x.IdLagerAbteilung = b.IdLocation

-- last inv
left join
(select * from [AlplaPROD_test1].[dbo].[T_LagerAbteilungenInventuren] (nolock)) as i on x.IdLagerAbteilung = 
i.IdLagerAbteilung

-- useing this to determin only if the lane is empty
left join
(select * from [AlplaPROD_test1].dbo.V_LagerPositionenBarcodes (nolock)) as y on x.IdLagerAbteilung =
y.IdLagerAbteilung

-- get the warehosue type
left join
(select * from [AlplaPROD_test1].dbo.T_WarenLager (nolock)) as w on x.IdWarenLager = w.IdWarenLager

where x.aktiv = 1 and x.IdWarenLager not in (1,5,6)

group by x.IdLagerAbteilung, 
x.IdWarenLager, 
w.LagerTyp,
w.Standort,
x.Bezeichnung, 
LastMoveDate,
i.Datum,
x.MaterialSilo,
w.Bezeichnung
) xb
`;
