﻿-- =============================================
-- Author:		Steamware
-- Mod. date:   2014.05.29
-- Description:	cancellazione Cost List per data
-- =============================================
CREATE PROCEDURE [dbo].[stp_SC2C_deleteForDate]
    @Date SMALLDATETIME
AS
BEGIN

    SET NOCOUNT ON;

    DECLARE @CodQuoteDateMin BIGINT,
            @CodQuoteDateMax BIGINT;

    SET @Date            = CONVERT(SMALLDATETIME,CONVERT(CHAR(8), @Date, 112))
    SET @CodQuoteDateMin = CONVERT( BIGINT, ( RIGHT(YEAR(@Date),2) + dbo.f_padLeft(MONTH(@Date), 2,'0') + dbo.f_padLeft(DAY(@Date), 2,'0') + dbo.f_padLeft(0, 8,'0') ) )
    SET @CodQuoteDateMax = CONVERT( BIGINT, ( RIGHT(YEAR(@Date),2) + dbo.f_padLeft(MONTH(@Date), 2,'0') + dbo.f_padLeft(DAY(@Date), 2,'0') + dbo.f_padLeft(9, 8,'9') ) )

    -- cancello i dati di carico
    DELETE FROM dbo.CostList  WHERE DateList = @Date AND QuoteType = 'C'

    DELETE FROM dbo.QuoteOC      WHERE QuoteType = 'C' AND CodQuote >= @CodQuoteDateMin AND CodQuote <= @CodQuoteDateMax
    DELETE FROM dbo.QuoteParam   WHERE QuoteType = 'C' AND CodQuote >= @CodQuoteDateMin AND CodQuote <= @CodQuoteDateMax
    DELETE FROM dbo.QuoteWorkExt WHERE QuoteType = 'C' AND CodQuote >= @CodQuoteDateMin AND CodQuote <= @CodQuoteDateMax
    DELETE FROM dbo.QuoteWorkInt WHERE QuoteType = 'C' AND CodQuote >= @CodQuoteDateMin AND CodQuote <= @CodQuoteDateMax
    DELETE FROM dbo.QuoteRM      WHERE QuoteType = 'C' AND CodQuote >= @CodQuoteDateMin AND CodQuote <= @CodQuoteDateMax
    DELETE FROM dbo.QuoteList    WHERE QuoteType = 'C' AND CodQuote >= @CodQuoteDateMin AND CodQuote <= @CodQuoteDateMax   
    DELETE FROM dbo.QuoteFull_C  WHERE QuoteType = 'C' AND CodQuote >= @CodQuoteDateMin AND CodQuote <= @CodQuoteDateMax

    -- RIPULISCO TUTTO 
    -- TRUNCATE TABLE CostList
    -- TRUNCATE TABLE dbo.QuoteFull_C

RETURN

END