select top 10 * from [dbo].[v_order_details_status]
View:
CREATE view [dbo].[v_order_details_status]
as
select id,status,trackers,
case
when status='Cancelled' Then 0
when _sdays > _days then 1
when _sdays <= _days then 0
when dbo.f_get_business_days(_ts,GETDATE()) > _days then 1
else 0
end late_shipment,
case when status='Cancelled' Then 0 else dbo.f_max(0, _sdays - _days) end late_shipment_days,
case when status='Cancelled' then null else turnaround end turnover,
_sd shipdate,
_snd shipdate_notified,
expected_shipdate
from (
select
d.id,
case
when MAX(t.id) is null then d.status
else 'Shipped'
end status,
dbo.Conc(tn.tracker) trackers,
d._days,
d._ts,
min(isnull(tn.created,status_date)) _sd,
min(isnull(tn.notified_customer_date,status_date)) _snd,
min(dbo.f_get_business_days(d._ts,coalesce(tn.created,d.ship_status_date,GETDATE()))) _sdays,
MIN(
case
when coalesce(tn.created,d.ship_status_date,GETDATE())<_exp then dbo.f_get_business_days(d._ts,coalesce(tn.created,d.ship_status_date,GETDATE()))
else d._days + datediff(day,_exp,coalesce(tn.created,d.ship_status_date,GETDATE()))
end
) turnaround,
expected_shipdate
from(
select
od.id,
case
when od.status='SHIPPED' then 'Shipped'
--when not poi.poid is null then 'Invoiced'
when os.closed=1 then os.text
when os.shippable = 0 then os.text
when not od.status is null then od.status
when od.poid>0 then 'PO Created'
else 'Not Shipped'
end status,
isnull(po.ts,o.ts) _ts,
op.original_leadtime_days _days,
dbo.f_get_business_day(isnull(po.ts,o.ts),op.original_leadtime_days) _exp,
od.status_date,
case when od.status='SHIPPED' then od.status_date else null end ship_status_date,
case
--when op.expected_shipdate is not null then
-- case
-- when op.expected_shipdate> getdate() then op.expected_shipdate
-- else getdate()
-- end
when op.expected_shipdate> getdate() then op.expected_shipdate
else
dbo.f_get_business_day(
case
when pd.expected> isnull(po.ts,o.ts) then pd.expected
else isnull(po.ts,o.ts)
end
,
dbo.f_max(2,lt.days)
)
end expected_shipdate
from orders_details od
join orders_products op on op.id = od.opid
join orders o on o.id = op.orderid
join orders_status os on os.id = ISNULL(o.status,0)
join product_details pd on pd.id = od.itemid
join product p on p.id = pd.pid
join vendor v on v.id = p.vendor_id
left join leadtime lt on lt.id = ISNULL(pd.leadtime,v.default_leadtime_id)
left join purchase_order po on po.id = od.poid
--join v_product_status ps on ps.id = od.itemid
--left join purchase_order po on po.id = od.poid
--left join (
-- select distinct poid from purchase_order_invoice
--) poi on poi.poid = od.poid
) d
left join orders_details_trackers t on t.odid = d.id
left join tracking_numbers tn on tn.id = t.tracker
group by d.id,d.status,d._days,d._ts,d.expected_shipdate
) t
-------------
Fun 1 used:
CREATE function [dbo].[f_get_business_days](@f date, @t date) returns int
as
begin
/*
return(
select SUM(cast(dbo.f_is_business_day(dateadd(day,i,@f)) as int))
from f_get_nums(DATEDIFF(day,@f,@t))
)
*/
if @f is null or @t is null return null;
declare @n int = 0;
declare @i int = 0;
declare @j int = datediff(day,@f,@t);
while(@i<@j)
begin
set @i = @i + 1;
if dbo.f_is_business_day(DATEADD(day,@i,@f))=1 set @n = @n + 1;
end
return @n;
end
-----------
Fun 2 used:
CREATE function [dbo].[f_is_business_day](@ts date) returns bit --with schemabinding
as
begin
if @ts is null return 0;
if DATEPART(weekday,@ts) in(1,7) return 0 --SATURDAY & SUNDAY
if MONTH(@ts) = 5 and DATEPART(weekday,@ts) = 2
AND DAY(@TS)>=25 return 0 --MEMORIAL DAY
if MONTH(@ts) = 7 and DAY(@ts) = 4 return 0 --JULY 4
if MONTH(@ts) = 7 and DAY(@ts) = 5
and DATEPART(WEEKDAY,@ts) = 2 return 0 --JULY 4 SUNDAY
if MONTH(@ts) = 9 and DATEPART(weekday,@ts) = 2
AND DAY(@TS)<7 return 0 --LABOR DAY
if MONTH(@ts) = 11 and DATEPART(weekday,@ts) = 5
and DAY(@ts) between 22 and 28 return 0 --THANKSGIVING
/*
if MONTH(@ts) = 11 and DATEPART(weekday,@ts) = 6
and DAY(@ts) between 23 and 29 return 0 --BLACK FRIDAY
*/
if MONTH(@ts) = 12 and DAY(@ts) = 25 return 0 --CHRISTMAS
if MONTH(@ts) = 12 and DAY(@ts) = 31 return 0 --NEW YEARS EVE
if MONTH(@ts) = 1 and DAY(@ts) = 1 return 0 --NEW YEARS
return 1
end