﻿-- =============================================
-- Author:		Steamware - S.E.L.
-- Create date: 2014.05.16
-- Description:	update di un record in QL (Full) TIPO SIMULAZIONE e poi aggiorna a cascata
-- =============================================
CREATE PROCEDURE [dbo].[stp_QLS_UpdExtrPar]
(
	@Original_QuoteType CHAR(1) = 'S',
	@Original_CodQuote	BIGINT = 0,
	@Original_QuoteRev	INT = 0,
	@TipoDies			NVARCHAR(50) = '',	
	@DiamDies			INT = 1,	
	@HoleNumDies		INT = 1,	
	@CodPlant	        NVARCHAR(50),
	@DiesPrice_sim      DECIMAL(18, 6),
	@DiesTecLife        DECIMAL(18, 6),
	@WSR01_sim          DECIMAL(9, 8),
	@MSR01_sim          DECIMAL(9, 8),
	@NetProd01_sim      DECIMAL(9, 3)
)
AS

SET XACT_ABORT ON;
BEGIN TRAN

	-- dichiaro e calcolo valore complessivo...
	DECLARE	@CodDiesGroup	NVARCHAR(50) = ''

	SELECT	@CodDiesGroup	=  @TipoDies + '-' + CAST(@DiamDies AS VARCHAR(50)) + '-' + CAST(@HoleNumDies AS VARCHAR(50)) + '-' + CodItemGroup
	FROM	QuoteList
	WHERE	QuoteType	= @Original_QuoteType 
		AND	CodQuote	= @Original_CodQuote	
		AND	QuoteRev	= @Original_QuoteRev	


	-- @CodDiesGroup
	EXEC stp_QSP_upsert @Original_QuoteType, @Original_CodQuote, @Original_QuoteRev, 'CodDiesGroup', @CodDiesGroup
	
	-- @CodPlant
	EXEC stp_QSP_upsert @Original_QuoteType, @Original_CodQuote, @Original_QuoteRev, 'CodPlant', @CodPlant
	
	-- @DiesPrice_sim
	EXEC stp_QSP_upsert @Original_QuoteType, @Original_CodQuote, @Original_QuoteRev, 'DiesPrice_sim', @DiesPrice_sim
	
	-- @DiesTecLife
	EXEC stp_QSP_upsert @Original_QuoteType, @Original_CodQuote, @Original_QuoteRev, 'DiesTecLife', @DiesTecLife
	
	-- @WSR01_sim
	EXEC stp_QSP_upsert @Original_QuoteType, @Original_CodQuote, @Original_QuoteRev, 'WSR01_sim', @WSR01_sim
	
	-- @MSR01_sim
	EXEC stp_QSP_upsert @Original_QuoteType, @Original_CodQuote, @Original_QuoteRev, 'MSR01_sim', @MSR01_sim
	
	-- @NetProd01_sim
	EXEC stp_QSP_upsert @Original_QuoteType, @Original_CodQuote, @Original_QuoteRev, 'NetProd01_sim', @NetProd01_sim
	

	-- update dei valori calcolati!
	EXEC stp_QL_fullDataUpdate @Original_QuoteType
		,@Original_CodQuote
		,@Original_QuoteRev
		,'Y'

	-- 2014.10.12 modifica sam: chiamo 2 volte perché è simulazione e i valori _sim sono salvati alla fine ed usati all'inizio...
    EXEC stp_QL_fullDataUpdate @Original_QuoteType
        ,@Original_CodQuote
        ,@Original_QuoteRev
        ,'Y'
			
COMMIT TRAN


-- seleziono intera riga!
	
SELECT	* 
FROM	v_QuoteFull_Q
WHERE	QuoteType = @Original_QuoteType
	AND CodQuote = @Original_CodQuote
	AND QuoteRev = @Original_QuoteRev


RETURN