﻿
-- =============================================
-- STORED PROCEDURE stp_QL_fullDataUpdate
--
-- Author:		Steamware
-- Create date: 2014.01.27
-- Description:	Recupera tutti i dati dalle tab QuoteList x avere schema x report/gestioen web
-- =============================================
CREATE PROCEDURE [dbo].[stp_QL_fullDataUpdate] (
	@QuoteType CHAR(1) = 'Q'
	, @CodQuote BIGINT = 0
	, @QuoteRev INT = 0
	, @DoUpdate CHAR(1) = 'N' -- se si debba fare ricalcolo Y/N
	)
AS
SET NOCOUNT ON

/* va rifatta struttura:
*
* tab temporanea
* calcolo parametri
* salvataggio in QuoteList dei 3 valori di sunto
* calcolo dei valori aggregati e restituzione riga risultati finali
*
*  ATTENZIONE NON CONTROLLA IL CAMPO VALID DEVE ESSERE SEMPRE 1 PER ESSERE VALIDO
*
*/




--DECLARE   @FullCost DECIMAL(9,6)
--        , @MinPrice DECIMAL(9,6)
IF (@DoUpdate = 'Y')
BEGIN
	EXEC stp_QL_update    @QuoteType
		                , @CodQuote
		                , @QuoteRev
END


-- inserire in tabelle: QuoteFull_Q (quotazioni/preventivi)
-- inserire in tabelle: QuoteFull_R (resoconto di commessa)
-- inserire in tabelle: QuoteFull_S (simulazioni)

-- elimino eventuale temp table...
IF OBJECT_ID('tempdb..#TempFullData') IS NOT NULL
    DROP TABLE #TempFullData;


-- Costruisco la "superselect" di tutti i valori necessari...

-- =================================================
-- PIVOT TABELLA QuoteParameter
-- =================================================
;WITH ctePivotQuoteP 
AS (
    SELECT *
    FROM (
            SELECT QuoteType
	            , CodQuote
	            , QuoteRev
	            , Descr
	            , Parameter
            FROM dbo.QuoteParam
            WHERE CodQuote = @CodQuote
	            AND QuoteType = @QuoteType
	            AND QuoteRev = @QuoteRev
        ) AS SourceTable

    PIVOT(MIN(Parameter) FOR Descr IN (
		  ItemDescr
		, CodifProfilo
		, ClasseMerc
		, CodClientAssoc
		, ClientNameAssoc
		, CodDiesGroup
		, TipoDies
		, DiamDies
		, HoleNumDies
		, NumDiesInList
		, NumDiesExaust
		, CodPlant
		, KeyAM
		, KeyAMIL
		, NumSMED
		, KgTeo
		, KgProdotti
		, QuotaMan
		, DiesPrice
		, DiesExpLife
		, DiesEOLife
		, PkgDesc
		, PRMWeight
		, PWeight
		, PTare
		, TareStRatio
        , LivPackage
        , LivPackageDett
	    )
  ) AS PivotData
),
-- =================================================
-- PIVOT TABELLA QuoteOC
-- =================================================
ctePivotQuoteOC AS (   
    SELECT *
    FROM (
            SELECT    QuoteType
	                , CodQuote
	                , QuoteRev
	                , Class01 + ProcessNum AS ClassProc
	                , C4UG
            FROM dbo.QuoteOC
            WHERE CodQuote = @CodQuote
	            AND QuoteType = @QuoteType
	            AND QuoteRev = @QuoteRev
            ) AS SourceTable
    PIVOT(SUM(C4UG) FOR ClassProc IN 
              (   EE01
		        , GAS01
		        , MATRICE01
		        , VENDITA01
		        , ROTTAME01
		        , VENDITA04
		        , ROTTAME04
		        , IMBALLO04
		        , TRASPORTI04
		        , PROVVIGIONI04
	          ) 
       ) AS PivotData
)
-- =================================================
-- MAIN Select Tabella con INTO temp table
-- =================================================
SELECT 'V14.1' AS Vers
	, QL.QuoteType
	, QL.CodQuote
	, QL.QuoteRev
	, QL.CodClient
	, CD.ClientName
	, QL.CodItem
	, QL.CodItemGroup
	, QL.RawMat
	, QL.UnitWeight
	, QL.BatchQty
	, QL.CodInco
	, QL.KeyAM
	, QL.Note
	, QL.PriceOff
	, pQP.CodClientAssoc
	, pQP.ClientNameAssoc
	, pQP.ItemDescr
	, pQP.CodifProfilo
	, pQP.ClasseMerc
	, pQP.TipoDies
	, pQP.CodDiesGroup
	, pQP.DiamDies
	, pQP.HoleNumDies
	, pQP.DiesPrice
	, pQP.DiesExpLife
	, pQP.DiesEOLife
	, pQP.NumDiesInList
	, pQP.NumDiesExaust
	, pQP.CodPlant
	, pQP.KeyAMIL
	, pQP.KgTeo
	, pQP.NumSMED
	, pQP.QuotaMan
	, pQP.PRMWeight
	, pQP.PWeight
	, pQP.PTare
    , pQP.LivPackage
    , pQP.LivPackageDett
	, QRM.RawMatCost
	, QRM.RawMatExtraCost
	, QRM.RawMatCost + QRM.RawMatExtraCost AS RawMatFullCost -- fare campo calcolato ?!?
	, QRM.CodPackag
    , pQP.PkgDesc
	, QRM.FSR
	, ISNULL(pQOC.PROVVIGIONI04, 0) AS ProvvCost
	, ISNULL(pQOC.EE01, 0)          AS EECost
	, ISNULL(pQOC.GAS01, 0)         AS GasCost
	, ISNULL(pQOC.MATRICE01, 0)     AS DiesCost
	, ISNULL(pQOC.IMBALLO04, 0)     AS PackCost
	, ISNULL(pQOC.TRASPORTI04, 0)   AS TranspCost
	, ISNULL(pQOC.VENDITA01, 0)     AS SellCost01
	, ISNULL(pQOC.ROTTAME01, 0)     AS RefCost01
	, ISNULL(pQOC.VENDITA04, 0)     AS SellCost04
	, ISNULL(pQOC.ROTTAME04, 0)     AS RefCost04
	, QWI01.WSR AS WSR01
	, QWI01.MSR AS MSR01
	, QWI01.NetProd AS NetProd01
	, QWI01.FC4UG AS FC4UG01
	, QWI01.VC4UG AS VC4UG01
	, QWI01.OH4UG AS OH4UG01
	, QWI01.SC4UG AS SC4UG01
	, 1 - (QWI01.DYR * QWI02.DYR * QWI03.DYR * QWI04.DYR) AS DSR
	, QWI04.NetProd AS NetProd04
	, QWI04.FC4UG AS FC4UG04
	, QWI04.VC4UG AS VC4UG04
	, ISNULL(pQOC.VENDITA01, 0) + ISNULL(pQOC.ROTTAME01, 0) + ISNULL(pQOC.VENDITA04, 0) + ISNULL(pQOC.ROTTAME04, 0) + ISNULL(pQOC.EE01, 0) + ISNULL(pQOC.GAS01, 0) + ISNULL(pQOC.MATRICE01, 0) + ISNULL(pQOC.IMBALLO04, 0) + ISNULL(pQOC.PROVVIGIONI04, 0) + ISNULL(pQOC.TRASPORTI04, 0) + QWI01.VC4UG + QWI04.VC4UG + QWI01.SC4UG AS FVC
	, PD.Charge

INTO #TempFullData        -- carico in temp table
FROM dbo.QuoteList AS QL

INNER JOIN dbo.ClientDet CD
	ON QL.CodClient = CD.CodClient

INNER JOIN ctePivotQuoteP AS pQP
	ON      QL.QuoteType = pQP.QuoteType
	    AND QL.CodQuote  = pQP.CodQuote
	    AND QL.QuoteRev  = pQP.QuoteRev

INNER JOIN dbo.QuoteRM AS QRM
	ON      QL.QuoteType = QRM.QuoteType
		AND QL.CodQuote  = QRM.CodQuote
		AND QL.QuoteRev  = QRM.QuoteRev
		AND QL.rawmat    = QRM.RawMat

INNER JOIN  ctePivotQuoteOC AS pQOC
	    ON      QL.QuoteType = pQOC.QuoteType
		    AND QL.CodQuote  = pQOC.CodQuote
		    AND QL.QuoteRev  = pQOC.QuoteRev

INNER JOIN dbo.QuoteWorkInt AS QWI01
	ON      QL.QuoteType = QWI01.QuoteType
		AND QL.CodQuote  = QWI01.CodQuote
		AND QL.QuoteRev  = QWI01.QuoteRev
		AND QWI01.ProcessNum = '01'
INNER JOIN dbo.QuoteWorkInt AS QWI02
	ON      QL.QuoteType = QWI02.QuoteType
		AND QL.CodQuote  = QWI02.CodQuote
		AND QL.QuoteRev  = QWI02.QuoteRev
		AND QWI02.ProcessNum = '02'
INNER JOIN dbo.QuoteWorkInt AS QWI03
	ON      QL.QuoteType = QWI03.QuoteType
		AND QL.CodQuote  = QWI03.CodQuote
		AND QL.QuoteRev  = QWI03.QuoteRev
		AND QWI03.ProcessNum = '03'
INNER JOIN dbo.QuoteWorkInt AS QWI04
	ON      QL.QuoteType = QWI04.QuoteType
		AND QL.CodQuote  = QWI04.CodQuote
		AND QL.QuoteRev  = QWI04.QuoteRev
		AND QWI04.ProcessNum = '04'
INNER JOIN dbo.PlantsDet AS PD
	ON PD.CodPlant = pQP.CodPlant

---- salvo i dati dell'ordine: FullCost, MinPrice, PriceOff (se zero lo pongo uguale a MinPrice)
--UPDATE  QuoteList
--SET     FullCost    = @FullCost
--        , MinPrice  = @MinPrice
--        , PriceOff  = CASE WHEN ISNULL(PriceOff,0) = 0 THEN @MinPrice ELSE PriceOff END
--WHERE   QuoteType = @QuoteType
--	AND CodQuote = @CodQuote
--	AND QuoteRev = @QuoteRev

IF @QuoteType = 'Q' 
 BEGIN 
    IF OBJECT_ID('QuoteFull_Q') IS NULL             -- DROP TABLE QuoteFull_Q
        SELECT * INTO dbo.QuoteFull_Q FROM #TempFullData
    ELSE
     BEGIN
        --DELETE dbo.QuoteFull_Q WHERE QuoteType = @QuoteType AND CodQuote = @CodQuote AND QuoteRev = @QuoteRev
        DELETE dbo.QuoteFull_Q WHERE QuoteType = @QuoteType AND CodQuote = @CodQuote AND QuoteRev = @QuoteRev
        INSERT dbo.QuoteFull_Q 
        SELECT * FROM #TempFullData
     END
 END

ELSE IF @QuoteType = 'R'
 BEGIN 
    IF OBJECT_ID('QuoteFull_R') IS NULL             -- DROP TABLE QuoteFull_R
        SELECT * INTO dbo.QuoteFull_R FROM #TempFullData
    ELSE
     BEGIN
        INSERT dbo.QuoteFull_R
        SELECT * FROM #TempFullData
     END
 END

ELSE IF @QuoteType = 'S'
 BEGIN 
    IF OBJECT_ID('QuoteFull_S') IS NULL             -- DROP TABLE QuoteFull_S
        SELECT * INTO dbo.QuoteFull_S FROM #TempFullData
    ELSE
     BEGIN     
        INSERT dbo.QuoteFull_S
        SELECT * FROM #TempFullData
     END
 END

-- elimino temp table...
IF OBJECT_ID('tempdb..#TempFullData') IS NOT NULL
    DROP TABLE #TempFullData

RETURN









