USE [shmg]
GO
/****** Object:  StoredProcedure [dbo].[sm_attendnew_jr_month]    Script Date: 05/24/2013 09:58:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER procedure [dbo].[sm_attendnew_jr_month]as 
declare @upd_Date datetime
declare  @S_WORKA datetime
declare @E_WORKa DATETIME
declare @S_WORKb DATETIME
declare @E_WORKb DATETIME
declare @S_WORK DATETIME
declare @E_WORK DATETIME
declare  @a  int
declare  @b  int
declare  @cmp_nm4  char(10)
 
declare @fact_no char(6),@jban  decimal(4,2),@guojia  decimal(4,2)
declare @subject varchar(100),@bth_date  datetime
declare @s_noon datetime
declare @e_noon datetime
declare @s_evening datetime
declare @e_evening datetime
declare @c_Date2 char(10)
declare @c_date1 char(10)select @upd_date=convert(char(10),getdate()+10,111)
if datepart(month,@upd_date)>2
begin
select @c_date2=convert(char(4),datepart(year,@upd_date))+'/'+substring(convert(char(3),(datepart(month,@upd_date)-1)+100),2,2)+'/25'
select @c_date1=convert(char(4),datepart(year,@upd_date))+'/'+substring(convert(char(3),(datepart(month,@upd_date)-2)+100),2,2)+'/26'
end
if datepart(month,@upd_date)=1
begin
select @c_date2=convert(char(4),datepart(year,@upd_date)-1)+'/12/25'
select @c_date1=convert(char(4),datepart(year,@upd_date)-1)+'/11/26'
end
if datepart(month,@upd_date)=2
begin
select @c_date2=convert(char(4),datepart(year,@upd_date))+'/01/25'
select @c_date1=convert(char(4),datepart(year,@upd_date)-1)+'/12/26'
endselect @upd_date=@c_date1--抓出未成年工的人員
select hrnum into #yhlee from hr..hr005 where  convert(char(10),birthday,111)>dateadd(year,-18,@c_date1)   and hrnum like '0%' order by birthday
--抓國假週六日的資料
select guojia into #yhlee1 from sm_guojia_jr where guojia>=@c_date1 and guojia<=@c_date2 and DATEPART(DW,guojia) in (7,1) order by upd_date---5~8八月份考勤使用
---select guojia into #yhlee1 from sm_guojia_jr where guojia>=@c_date1 and guojia<=@c_date2 and DATEPART(DW,guojia)<>7 order by upd_date
--抓部門
select * into #wprg_tmp from prod..wprg_tmp where enab>0--更新外發洗衣部門的欄位
update #wprg_tmp set cmp_nm4=cmp_nm3,cmp_nm3=cmp_nm3 where sno1=2119  delete from cy_attend_jr where tx_yn='a'   and convert(char(10),upd_date,111)>=@c_Date1 and  convert(char(10),upd_date,111)<=@c_Date2 and hr003atsn=21select  a.tsn,a.hrnum as fact_no,a.chName as psn_name,a.birthday ,a.jobcode,a.startDay,a.leaveday,c.tsn as hr003tsn 
into #m from shmg..hr005 a join shmg..hr006 b on a.tsn=b.hr005tsn join shmg..hr003 c on b.hr003tsn=c.tsn where b.defau=1 and ((leaveday>@c_Date1  and startday<=@c_Date2)  or (startday<=@c_Date2  and leaveday='')) and c.hr003atsn=21
and a.hrnum like '0%' while  convert(char(10),@upd_date,111)<=convert(char(10),@c_date2,111)
begin
declare   temp_prsn  cursor
for
select distinct a.fact_no  
,jban=case  when  datediff(mm,a.birthday,@upd_date )>217  then    isnull(c.jban,0)
else 
  0
end
 
,guojia=case  when  datediff(mm,a.birthday,@upd_date)>217  then    isnull(c.guojia,0)
else
0
end
,b.cmp_nm5
,a.birthday,d.s_noon,d.e_noon,d.s_evening,d.e_evening
from   #m a,#wprg_tmp b ,card_jban1_jr c,cy_eat d 
where a.hr003tsn=b.sno1 and  (b.cmp_nm5=c.cmp_nm5 or b.cmp_nm4=c.cmp_nm5) 
and (convert(char(10),a.leaveday,111)>convert(char(10),@upd_date,111) or  convert(char(10),a.leaveday,111)='1900/01/01')
and  convert(char(10),a.startDay,111)<=convert(char(10),@upd_date,111)
and c.factory=b.cmp_nm6 and c.dept_no=b.dept_name   
and convert(char(10),d.s_noon,111)=c.date_1 and  d.factory=c.cmp_nm5
and   c.date_1=@upd_date
open temp_prsn
fetch next  from temp_prsn into @fact_no,@jban,@guojia,@cmp_nm4,@bth_date,@s_noon,@e_noon,@s_evening,@e_evening
WHILE (@@FETCH_STATUS <> -1)
BEGIN
select   @s_work=dateadd(second,rand()*60,dateadd(minute,rand()*14+46,dateadd(hour,7,@upd_Date)))
--SELECT @s_worka=dateadd(second,rand()*60,dateadd(minute,rand()*15,dateadd(hour,12,@upd_Date)))
--SELECT @e_worka=dateadd(second,rand()*60,dateadd(minute,rand()*20+40,dateadd(hour,12,@upd_Date)))
SELECT @s_worka=dateadd(second,rand()*60,dateadd(minute,rand()*5+5,@s_noon))
SELECT @e_worka=dateadd(second,rand()*60,dateadd(minute,rand()*20,@e_noon)) --select   @s_workb=dateadd(second,rand()*60,dateadd(minute,rand()*15,dateadd(hour,17,@upd_Date))) 
--SELECT @e_workb=dateadd(second,rand()*60,dateadd(minute,rand()*15+45,dateadd(hour,17,@upd_Date)))
if @jban=0  --如果沒設定加班
begin

               begin
select   @s_workb=dateadd(second,rand()*60,dateadd(minute,rand()*10,dateadd(hour,17,@upd_Date))) 
select   @e_work=@s_workb
select  @s_workb='1900/01/01'
select  @e_workb='1900/01/01'
end

end
else
begin
select @b=@jban*3600
select   @s_workb=dateadd(second,rand()*60,dateadd(minute,rand()*5+5,@s_evening)) 
SELECT @e_workb=dateadd(second,rand()*60,dateadd(minute,rand()*10,@e_evening))  select @e_work=dateadd(second,  convert(int ,rand()*900+@b),dateadd(hour,18,@upd_date))


end

insert into cy_attend_jr(hr003atsn,fact_no,s_work,e_work,upd_date,tx_yn,S_WORK1,E_WORK1,s_work2,e_work2)
values(21,@fact_no,@s_work,@e_work,@upd_Date,'a',@s_worka,@e_worka,@s_workb,@e_workb)


fetch next  from temp_prsn into @fact_no,@jban,@guojia,@cmp_nm4,@bth_date,@s_noon,@e_noon,@s_evening,@e_evening
end
           deallocate temp_prsndelete from cy_attend_jr where upd_date=@upd_date and tx_yn='a'
and fact_no in (select hrnum from hr..hr015  where convert(char(10),workday,111)=convert(char(10),@upd_Date,111)
and hrnum  in (select fact_no from cy_attend_jr where tx_yn='a' and  upd_date=@upd_Date and hr003atsn=21)
and leavekinds not like '%出差%'
and leavekinds not like '%旅遊%'
and leavekinds<>'無'
and hour5=8.00
) and hr003atsn=21--刪除未成年工的平常加班跟週日加班
update cy_attend_jr 
set e_work=dateadd(second,rand()*60,dateadd(minute,rand()*10,dateadd(hour,17,@upd_Date))),s_work2='',e_work2=''
where upd_date=@upd_date and fact_no in (select hrnum from #yhlee) and  s_work2<>'' and tx_yn='a' and hr003atsn=21if datepart(month,@c_date2)>=5 and  datepart(month,@c_date2)<=8
begin
delete from cy_attend_jr where upd_date=@upd_date and fact_no in (select hrnum from #yhlee) and datepart(dw,upd_date) in (1) and tx_yn='a' and hr003atsn=21
end
else
begin
delete from cy_attend_jr where upd_date=@upd_date and fact_no in (select hrnum from #yhlee) and datepart(dw,upd_date) in (1,7) and tx_yn='a' and hr003atsn=21
end
----delete from cy_attend_jr where upd_date=@upd_date and fact_no in (select hrnum from #yhlee) and datepart(dw,upd_date) in (1,7) and tx_yn='a' and hr003atsn=21delete from cy_attend_jr where upd_date=@upd_date and fact_no in (select hrnum from #yhlee) and upd_date in (select guojia from #yhlee1) and tx_yn='a' and hr003atsn=21update cy_attend_jr 
set e_work=dateadd(second,rand()*60,dateadd(minute,rand()*15,dateadd(hour,16,@upd_date))),s_work2='1900-01-01',e_work2='1900-01-01'
where fact_no in (select fact_no from baby_vn where e_date>=@upd_Date and s_date<=@upd_date and jobcode='a') and tx_yn='a' and convert(char(10),upd_date,111)=convert(char(10),@upd_date,111)
select @upd_Date=@upd_date+1 delete from cy_attend_jr where upd_date>=@c_date1 and upd_date<=@c_date2 and tx_yn<>'d' and  upd_date in (select guojia from #yhlee1)
end SQLselect