TRUNCATE TABLE DY2022.DBO.PENALTY_MH;

DECLARE @DAYS_LATE AS INT = 1;

DROP TABLE DY2022.DBO.#PEN_MH ;

        WITH VDEF2 AS 
        ( 
        SELECT 2022 AS DIGYR, * FROM DY2022.DBO.DEFAULTS2 
        UNION 
        SELECT 2021 AS DIGYR, * FROM DY2021.DBO.DEFAULTS2 
        UNION 
        SELECT 2020 AS DIGYR, * FROM DY2020.DBO.DEFAULTS2 
        UNION 
        SELECT 2019 AS DIGYR, * FROM DY2019.DBO.DEFAULTS2 
        UNION 
        SELECT 2018 AS DIGYR, * FROM DY2018.DBO.DEFAULTS2 
        UNION 
        SELECT 2017 AS DIGYR, * FROM DY2017.DBO.DEFAULTS2 
        UNION 
        SELECT 2016 AS DIGYR, * FROM DY2016.DBO.DEFAULTS2 
        UNION 
        SELECT 2015 AS DIGYR, * FROM DY2015.DBO.DEFAULTS2 
        UNION 
        SELECT 2014 AS DIGYR, * FROM DY2014.DBO.DEFAULTS2 
        ) 
 
 		SELECT *
        INTO DY2022.DBO.#PEN_MH 
 		FROM (
        SELECT B.BILL_NO 
            , B.BILL_YEAR 
            , B.DUE_DATE + @DAYS_LATE AS P_DATE 
            , D.PENALTY_MH_RATE1 AS P_RATE 
            , CASE WHEN B.DUE_DATE + @DAYS_LATE < CONVERT(DATE,GETDATE()) 
                THEN ROUND((V7.ADV_TAX_BALANCE + V7.FEES_BALANCE + V7.FEES_OTHER_BALANCE) * D.PENALTY_MH_RATE1,2) 
                ELSE 0 
                END AS P_AMT 
            , CASE WHEN P.BANKRUPT=1 THEN NULL ELSE GETDATE() END AS P_APPLYDATE 
 			, B.BILL_TYPE 
 
        FROM DY2022.DBO.VBILL_BALANCE_7 V7 
        INNER JOIN DY2022.DBO.VBILL B ON B.BILL_NO=V7.BILL_NO AND B.BILL_YEAR=V7.BILL_YEAR AND B.BALANCE_DUE>0 AND B.BILL_TYPE=V7.BILL_TYPE  and b.PENALTY=0 and b.bill_type='m' 
        INNER JOIN DY2022.DBO.VPROPERTY P ON P.PROPERTYKEY=B.PROPERTYKEY AND P.DIGYR=B.BILL_YEAR AND B.BILL_TYPE=P.PROPTYPE  AND P.PROPTYPE='M'  
        LEFT JOIN VDEF2 D ON D.DIGYR+1=B.BILL_YEAR 
        WHERE ISNULL(V7.ADV_TAX_BALANCE,0) + ISNULL(V7.FEES_BALANCE,0) + ISNULL(V7.FEES_OTHER_BALANCE,0) > ISNULL(D.WRITEOFF_MAX,0) 
            AND B.DUE_DATE + @DAYS_LATE < CONVERT(DATE,GETDATE()) 
            AND B.DUE_DATE + @DAYS_LATE > ISNULL((SELECT MAX(P_DATE) FROM DY2022.DBO.PENALTY_MH),0) 
            AND D.PENALTY_MH_RATE1 > 0.0 
			--AND V7.BILL_NO=350016

        ) X ;

 INSERT INTO DY2022.DBO.PENALTY_MH ( 
        BILL_NO 
        , BILL_YEAR 
        , P_DATE 
        , P_RATE 
        , P_AMT 
        , P_APPLYDATE 
        ) 
 
        SELECT 
            I.BILL_NO 
            , I.BILL_YEAR 
            , I.P_DATE 
            , I.P_RATE 
            , I.P_AMT 
            , I.P_APPLYDATE 
        FROM DY2022.DBO.#PEN_MH I 
        INNER JOIN DY2022.DBO.VBILL B ON B.BILL_NO=I.BILL_NO AND B.BILL_YEAR=I.BILL_YEAR AND I.BILL_TYPE=B.BILL_TYPE 
 
     ; 
-- UPDATE BILL TO SHOW PENALTY AMOUNTS 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2022.DBO.BILL BU 
    INNER JOIN  (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2023  AND BU.BILL_TYPE IN ('M') ; 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2021.DBO.BILL BU 
    INNER JOIN (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2022  AND BU.BILL_TYPE IN ('M') ; 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2020.DBO.BILL BU 
    INNER JOIN (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2021  AND BU.BILL_TYPE IN ('M') ; 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2019.DBO.BILL BU 
    INNER JOIN (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2020  AND BU.BILL_TYPE IN ('M') ; 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2018.DBO.BILL BU 
    INNER JOIN (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2019  AND BU.BILL_TYPE IN ('M') ; 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2017.DBO.BILL BU 
    INNER JOIN (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2018  AND BU.BILL_TYPE IN ('M') ; 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2016.DBO.BILL BU 
    INNER JOIN (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2017  AND BU.BILL_TYPE IN ('M') ; 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2015.DBO.BILL BU 
    INNER JOIN (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2016  AND BU.BILL_TYPE IN ('M') ; 
 
    UPDATE BU SET BU.PENALTY= BU.PENALTY + I.P_AMT, BU.TOTAL= BU.TOTAL + I.P_AMT, BU.BALANCE_DUE= BU.BALANCE_DUE + I.P_AMT 
    FROM DY2014.DBO.BILL BU 
    INNER JOIN (SELECT BILL_NO, BILL_YEAR, SUM(P_AMT) AS P_AMT FROM DY2022.DBO.#PEN_MH WHERE P_APPLYDATE IS NOT NULL GROUP BY BILL_NO, BILL_YEAR) I ON I.BILL_NO=BU.BILL_NO AND I.BILL_YEAR=BU.BILL_YEAR AND BU.BILL_YEAR=2015  AND BU.BILL_TYPE IN ('M') ; 
 
 
 
INSERT INTO DY2022.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2022.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
INSERT INTO DY2021.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2021.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
INSERT INTO DY2020.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2020.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
INSERT INTO DY2019.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2019.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
INSERT INTO DY2018.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2018.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
INSERT INTO DY2017.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2017.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
INSERT INTO DY2016.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2016.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
INSERT INTO DY2015.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2015.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
INSERT INTO DY2014.DBO.BILL_TRANSACTIONS (BILL_NO, BILL_YEAR, T_AMOUNT, T_DATE, T_TYPE, COMMENTS, LOGINID, COMPNAME) 
SELECT P.BILL_NO, P.BILL_YEAR, P_AMT, P_DATE, 'PENALTY_MH', 'PROCESSED ON '+CONVERT(VARCHAR(30),P_APPLYDATE), 'SYSGEN', HOST_NAME() 
FROM DY2022.DBO.#PEN_MH P 
INNER JOIN DY2014.DBO.BILL B ON B.BILL_NO=P.BILL_NO AND B.BILL_YEAR=P.BILL_YEAR AND P.BILL_TYPE='M' ; 
 
