Alternatively, replace all the date calc code with:
select @qdt=dateadd(m,-1,dateadd(d,datediff(d,'1 Jan 1900',getdate()),'1 Jan 1900')-datepart(d,getdate())+1)
This returns the first day of the previous month, 12:00 am, as does the given script.
An explanation of what the code was doing would have been nice, rather than having to work it out. However, the majority is not needed as SQL provides all the necessary functions for doing date calcs...