CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_ARR_PDPP
AS 
SELECT
    ZPOS_POSTDATE_S DAT,
    (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
    ZPOS_ID_S ZPOS_ID,
    DECODE(ZDCO_PAYM_TYPE_S,2,1,0) IS_CL,
    ZDCO_ID_S ZDCO_ID,
    QTY QTY,
    UNIT UNIT,
    GROSS_S GROSS,
    PAY_S PAY,
    REV_S REV,
    (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
    (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
    (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
    (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
    (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
    TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 
    AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) 
    AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL, 
    decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZARR',(select ZARR_ID from ZARR where ZARR_ID=(select ZPOS_ZARR_ID from ZPOS where ZPOS.ZPOS_ZARR_ID=ZARR_or_zpos_S and ZPOS.ZPOS_ID=ZPOS_ID_S)),'ZARR_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,   
    ZWIN_ID_S ZWIN_ID
FROM (
    SELECT
        zpos.zpos_postdate zpos_postdate_s,
        min(zpos.zpos_id) zpos_id_s,
        MIN(ZDCO_PAYM_TYPE) ZDCO_PAYM_TYPE_S,
        min(zpos.zpos_zdco_id) zdco_id_s,
        sum(decode(zpos.zpos_paidout,1,-1,1)*(zpos.zpos_quantity)) qty,
        decode(zpos.zpos_cdt,5,-1,1)*zpos.zpos_sumupamount/zpos.zpos_quantity unit,
        sum(decode(zpos.zpos_cdt,5,decode(zpos.zpos_paidout,1,(-zpos.zpos_unitprice*zpos.zpos_quantity),-(zpos.zpos_unitprice*zpos.zpos_quantity)),decode(zpos.zpos_cdt,5,0,zpos.zpos_sumupamount))) gross_s,
        sum(decode(zpos.zpos_cdt,5,decode(zpos.zpos_paidout,1,null,(zpos.zpos_unitprice*zpos.zpos_quantity)), null)) pay_s,
        sum(decode(zpos.zpos_cdt,5,decode(zpos.zpos_paidout,1,-zpos.zpos_unitprice*zpos.zpos_quantity,null),decode(zpos.zpos_cdt,5,0,zpos.zpos_sumupamount))) rev_s,
        zwin.zwin_id zwin_id_s,
        ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
        decode(nvl(zpos.zpos_zarr_id,0),0,1,0) indicate_zpos_id,
        decode(nvl(zpos.zpos_zarr_id,0),0,zpos.zpos_id,zpos.zpos_zarr_id) zarr_or_zpos_s
    FROM
    zorg,
        zpos
        INNER JOIN zdco ON zpos.zpos_zdco_id = zdco.zdco_id
        LEFT JOIN zwin ON zpos.zpos_zwin_id = zwin.zwin_id
    WHERE
    ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
        (zpos.zpos_zall_id IS NULL OR zpos.zpos_zall_id=0) AND
        zpos.zpos_id = zpos.zpos_internalsplit_zpos_id AND
        nvl(zpos.zpos_hidden,0)=0
        AND nvl(zpos.zpos_noreprint,0)<>1
    GROUP BY
        zwin.zwin_id,
        ZWIN.ZWIN_INVOICE_TO_XCMS_ID,
        zpos.zpos_postdate,
        decode(nvl(zpos.zpos_zarr_id,0),0,zpos.zpos_id,zpos.zpos_zarr_id),
        decode(nvl(zpos.zpos_zarr_id,0),0,1,0),
        decode(zpos.zpos_cdt,5,-1,1)*zpos.zpos_sumupamount/zpos.zpos_quantity
    )
WHERE
    abs(nvl(rev_s,pay_s))<>(decode((SELECT wuss.wuss_value FROM wuss WHERE wuss.wuss_xcms_id=0 AND wuss.wuss_name='Show 0 Postings on Billing Screen'),1,-1,0))
ORDER BY
 CDT ASC;

 
CREATE OR REPLACE FORCE VIEW V8_ED_CCM_YBOM_YEVM
AS
     SELECT YEVM.YEVM_ID YEVM_ID,
            YEVM.YEVM_YBOM_ID YEVM_YBOM_ID,
            TO_NUMBER (TO_CHAR (YEVM.YEVM_STARTDATE, 'J')) DATE_NUMBER,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_fs v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               FS_RATE_PER_DAY,
            NVL (
               (SELECT SUM (v.net)
                  FROM v8_stat_ccm_res_forrev_fs v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               FS_NET_PER_DAY,
            NVL (
               (SELECT SUM (v.vat)
                  FROM v8_stat_ccm_res_forrev_fs v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               FS_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_fs v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = yevm.yevm_startdate),
                  0),
               ybom.zcur_shortdesc)
               FFS_RATE_PER_DAY,
            (SELECT SUM (YEBS.YEBS_COSTPRICE)
               FROM YEBS, YEVM yevm_2, YFSR
              WHERE     YEBS.yebs_yfsr_id = YFSR.yfsr_id(+)
                    AND YEBS.YEBS_YEVM_ID = yevm_2.YEVM_ID
                    AND YEBS.YEBS_PKGINCL = 0
                    AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                    AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE)
               FS_COSTPRICE,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_ci v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               ITEMS_RATE_PER_DAY,
            NVL (
               (SELECT SUM (v.NET)
                  FROM v8_stat_ccm_res_forrev_ci v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               ITEMS_NET_PER_DAY,
            NVL (
               (SELECT SUM (v.VAT)
                  FROM v8_stat_ccm_res_forrev_ci v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               ITEMS_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_ci v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = yevm.yevm_startdate),
                  0),
               ybom.zcur_shortdesc)
               FITEMS_RATE_PER_DAY,
            (SELECT SUM (YEBR.YEBR_COSTPRICE * YEBR.YEBR_QUANTITY)
               FROM YEBR, YEVM yevm_2, YCIR
              WHERE     YEBR.yebr_ycir_id = YCIR.ycir_id(+)
                    AND YEBR.YEBR_YEVM_ID = yevm_2.YEVM_ID
                    AND YEBR.YEBR_PKGINCL = 0
                    AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                    AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE)
               ITEMS_COSTPRICE,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_sm v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               BOOKED_MENU_RATE_PER_DAY,
            NVL (
               (SELECT SUM (v.NET)
                  FROM v8_stat_ccm_res_forrev_sm v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               BOOKED_MENU_NET_PER_DAY,
            NVL (
               (SELECT SUM (v.VAT)
                  FROM v8_stat_ccm_res_forrev_SM v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               BOOKED_MENU_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_sm v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = yevm.yevm_startdate),
                  0),
               ybom.zcur_shortdesc)
               FBOOKED_MENU_RATE_PER_DAY,
            (SELECT SUM (
                         YEMN.YEMN_COSTPRICE
                       * DECODE (
                            NVL (yemn.YEMN_POSTGUARANTEED, 0),
                            0, yemn.YEMN_QUANTITY,
                            DECODE (
                               SIGN (
                                    yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                  - yemn.YEMN_QUANTITY),
                               1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                               yemn.YEMN_QUANTITY)))
               FROM YEMN, YEVM yevm_2
              WHERE     YEMN.YEMN_YEVM_ID = yevm_2.YEVM_ID
                    AND YEMN.YEMN_PKGINCL = 0
                    AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                    AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE)
               BOOKED_MENU_COSTPRICE,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_mi v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               SUB_MENU_RATE_PER_DAY,
            NVL (
               (SELECT SUM (v.NET)
                  FROM v8_stat_ccm_res_forrev_mi v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               SUB_MENU_NET_PER_DAY,
            NVL (
               (SELECT SUM (v.VAT)
                  FROM v8_stat_ccm_res_forrev_mi v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               SUB_MENU_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_mi v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = yevm.yevm_startdate),
                  0),
               ybom.zcur_shortdesc)
               FSUB_MENU_RATE_PER_DAY,
              NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
               TOT_REV_PER_DAY_NO_PKG,
              NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
               TOT_NET_REV_PER_DAY_NO_PKG,
              NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
               TOT_VAT_PER_DAY_NO_PKG,
            V8_FNC_VALCURR_FORMATING (
                 NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_fs v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_ci v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_sm v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_mi v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0),
               ybom.zcur_shortdesc)
               FTOT_REV_PER_DAY_NO_PKG,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_pk v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               PACKAGE_RATE,
            NVL (
               (SELECT SUM (v.NET)
                  FROM v8_stat_ccm_res_forrev_pk v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               PACKAGE_NET,
            NVL (
               (SELECT SUM (v.VAT)
                  FROM v8_stat_ccm_res_forrev_pk v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = yevm.yevm_startdate),
               0)
               PACKAGE_VAT,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_pk v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = yevm.yevm_startdate),
                  0),
               ybom.zcur_shortdesc)
               FPACKAGE_RATE,
              NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_pk v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
               TOT_REV_PER_DAY,
              NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_pk v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
               TOT_NET_REV_PER_DAY,
              NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_pk v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = yevm.yevm_startdate),
                 0)
               TOT_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
                 NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_fs v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_ci v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_sm v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_mi v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_pk v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = yevm.yevm_startdate),
                    0),
               ybom.zcur_shortdesc)
               FTOT_REV_PER_DAY,
            YEVM.YEVM_NAME YEVM_NAME,
            YEVM.YEVM_NOOFATTENDEES YEVM_NOOFATTENDEES,
            YEVM.YEVM_DOORCARD YEVM_DOORCARD,
            YEVM.YEVM_YPKD_ID YEVM_YPKD_ID,
            YEVM.YEVM_PKGINCL YEVM_PKGINCL,
            yevm_startdate YEVM_STARTTIME,
            TO_CHAR (YEVM.YEVM_STARTTIME, 'HH24:MI') FYEVM_STARTTIME,
            TRUNC (YEVM.YEVM_ENDTIME) YEVM_ENDTIME,
            TO_CHAR (YEVM.YEVM_ENDTIME, 'HH24:MI') FYEVM_ENDTIME,
            YEVM.YEVM_YETY_ID YEVM_YETY_ID,
            YEVM.YEVM_DISPLORDER,
            (SELECT YETY.YETY_SHORTDESC
               FROM YETY
              WHERE YETY.YETY_ID = YEVM.YEVM_YETY_ID)
               YETY_SHORTDESC,
            (SELECT YETY.YETY_LONGDESC
               FROM YETY
              WHERE YETY.YETY_ID = YEVM.YEVM_YETY_ID)
               YETY_LONGDESC,
            YEVM.YEVM_YEST_ID YEVM_YEST_ID,
            (SELECT YEST.YEST_SHORTDESC
               FROM YEST
              WHERE YEST.YEST_ID = YEVM.YEVM_YEST_ID)
               YEST_SHORTDESC,
            (SELECT YEST.YEST_LONGDESC
               FROM YEST
              WHERE YEST.YEST_ID = YEVM.YEVM_YEST_ID)
               YEST_LONGDESC,
            (SELECT YEST.YEST_CANCELSTATUS
               FROM YEST
              WHERE YEST.YEST_ID = YEVM.YEVM_YEST_ID)
               YEST_CANCELSTATUS,
            V8_FNC_YEBS_YFSC_REP_INFOS (YEVM.YEVM_ID, 3, 0) FUNCTION_SPACE,
            V8_FNC_YEBS_YFSC_REP_INFOS (YEVM.YEVM_ID, 3, 2) FUNCTION_SPACE_GENERIC,
            V8_FNC_YEBS_YFSC_REP_INFOS (YEVM.YEVM_ID, 3, 1)
               FUNCTION_SPACE_ALTINFO,
            V8_FNC_YEBS_YSSC_REP_INFOS (YEVM.YEVM_ID, 3) SEATING,
            V8_FNC_YEBS_YFSR_REP_INFOS (YEVM.YEVM_ID, 3, 0) RATES,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   1,
                                   0)
               NOTES_SHORT_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   1,
                                   1)
               NOTES_SHORT_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   2,
                                   0)
               NOTES_LONG_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   2,
                                   1)
               NOTES_LONG_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   3,
                                   0)
               NOTES_ND_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   3,
                                   1)
               NOTES_ND_VIEWABLE,
            --(select yebs.YEBS_ID from yebs where yebs.YEBS_YEVM_ID=yevm.YEVM_ID and rownum=1) T1
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',1,0) FS_NOTES_SHORT_NON_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',1,1) FS_NOTES_SHORT_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',2,0) FS_NOTES_LONG_NON_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',2,1) FS_NOTES_LONG_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',3,0) FS_NOTES_ND_NON_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',3,1) FS_NOTES_ND_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   1,
                                   0,
                                   NULL,
                                   1)
               FS_NOTES_SHORT_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   1,
                                   1,
                                   NULL,
                                   1)
               FS_NOTES_SHORT_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   2,
                                   0,
                                   NULL,
                                   1)
               FS_NOTES_LONG_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   2,
                                   1,
                                   NULL,
                                   1)
               FS_NOTES_LONG_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   3,
                                   0,
                                   NULL,
                                   1)
               FS_NOTES_ND_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   3,
                                   1,
                                   NULL,
                                   1)
               FS_NOTES_ND_VIEWABLE,
            DECODE ( (SELECT COUNT (yemn_id)
                        FROM YEMN
                       WHERE yemn_yevm_id = yevm_id),
                    0, 0,
                    1)
               StdMenusFlag,
            DECODE ( (SELECT COUNT (yebr_id)
                        FROM YEBR
                       WHERE yebr_yevm_id = yevm_id),
                    0, 0,
                    1)
               MiscItemsFlag,
            NVL (
               (SELECT 1
                  FROM xcno
                 WHERE     xcno.XCNO_TABLE_ID = yevm.yevm_id
                       AND xcno.XCNO_TABLELINKEDTO = 'YEVM'
                       AND ROWNUM = 1),
               0)
               yevm_notes_flag,
            NVL (
               (SELECT 1
                  FROM yebs, xcno
                 WHERE     YEBS.YEBS_YEVM_ID = yevm.yevm_id
                       AND xcno.XCNO_TABLE_ID = yebs.yebs_id
                       AND xcno.XCNO_TABLELINKEDTO = 'YEBS'
                       AND ROWNUM = 1),
               0)
               yebs_notes_flag,
            DECODE (
               (SELECT COUNT (a.yevm_id)
                  FROM yevm a
                 WHERE a.yevm_id = yevm.yevm_id AND a.yevm_doorcard <> ' '),
               0, 0,
               1)
               DoorCardFlag,
            YEVM.yevm_doorcard_prof,
              NVL (
                 (SELECT SUM (
                            DECODE (
                               NVL (YEBS.yebs_perhour, 0),
                               0, YEBS.YEBS_ORG_SPECIALRATE,
                               1,   (YEBS.yebs_endtime - YEBS.yebs_starttime)
                                  * 24
                                  * YEBS.YEBS_ORG_SPECIALRATE))
                    FROM YEBS, YEVM yevm_2, YFSR
                   WHERE     YEBS.yebs_yfsr_id = YFSR.yfsr_id(+)
                         AND YEBS.YEBS_YEVM_ID = yevm_2.YEVM_ID
                         AND YEBS.YEBS_PKGINCL = 0
                         AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE),
                 0)
            + NVL (
                 (SELECT SUM (
                              DECODE (
                                 NVL (YEBR.yebr_perhour, 0),
                                 0, YEBR.YEBR_ORG_MANUALPRICE,
                                 1,   (YEBR.yebr_endtime - YEBR.yebr_starttime)
                                    * 24
                                    * YEBR.YEBR_ORG_MANUALPRICE)
                            * YEBR.YEBR_QUANTITY)
                    FROM YEBR, YEVM yevm_2, YCIR
                   WHERE     YEBR.yebr_ycir_id = YCIR.ycir_id(+)
                         AND YEBR.YEBR_YEVM_ID = yevm_2.YEVM_ID
                         AND YEBR.YEBR_PKGINCL = 0
                         AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE),
                 0)
            + NVL (
                 (SELECT SUM (
                              YEMN.YEMN_MENUPRICE
                            * DECODE (
                                 NVL (yemn.YEMN_POSTGUARANTEED, 0),
                                 0, yemn.YEMN_QUANTITY,
                                 DECODE (
                                    SIGN (
                                         yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                       - yemn.YEMN_QUANTITY),
                                    1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                                    yemn.YEMN_QUANTITY)))
                    FROM YEMN, YEVM yevm_2
                   WHERE     YEMN.YEMN_YEVM_ID = yevm_2.YEVM_ID
                         AND YEMN.YEMN_PKGINCL = 0
                         AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE),
                 0)
            + NVL (
                 (SELECT SUM (YEMI.YEMI_ORG_PRICE * YEMI.YEMI_QUANTITY)
                    FROM YEMI, YEVM yevm_2
                   WHERE     YEMI.YEMI_YEVM_ID = yevm_2.YEVM_ID
                         AND YEMI.YEMI_INCLUDEDINTHEMENU = 0
                         AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE),
                 0)
            + NVL (
                 (SELECT SUM (YPKD.YPKD_PRICE * YPKD.YPKD_NOOFATTENDEES)
                    FROM YPKD
                   WHERE     YPKD.YPKD_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND TRUNC (YPKD.YPKD_STARTTIME) = YEVM.YEVM_STARTDATE),
                 0)
               TOT_ORGREV_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
                 NVL (
                    (SELECT SUM (
                               DECODE (
                                  NVL (YEBS.yebs_perhour, 0),
                                  0, YEBS.YEBS_ORG_SPECIALRATE,
                                  1,   (YEBS.yebs_endtime - YEBS.yebs_starttime)
                                     * 24
                                     * YEBS.YEBS_ORG_SPECIALRATE))
                       FROM YEBS, YEVM yevm_2, YFSR
                      WHERE     YEBS.yebs_yfsr_id = YFSR.yfsr_id(+)
                            AND YEBS.YEBS_YEVM_ID = yevm_2.YEVM_ID
                            AND YEBS.YEBS_PKGINCL = 0
                            AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE),
                    0)
               + NVL (
                    (SELECT SUM (
                                 DECODE (
                                    NVL (YEBR.yebr_perhour, 0),
                                    0, YEBR.YEBR_ORG_MANUALPRICE,
                                    1,   (  YEBR.yebr_endtime
                                          - YEBR.yebr_starttime)
                                       * 24
                                       * YEBR.YEBR_ORG_MANUALPRICE)
                               * YEBR.YEBR_QUANTITY)
                       FROM YEBR, YEVM yevm_2, YCIR
                      WHERE     YEBR.yebr_ycir_id = YCIR.ycir_id(+)
                            AND YEBR.YEBR_YEVM_ID = yevm_2.YEVM_ID
                            AND YEBR.YEBR_PKGINCL = 0
                            AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE),
                    0)
               + NVL (
                    (SELECT SUM (
                                 YEMN.YEMN_MENUPRICE
                               * DECODE (
                                    NVL (yemn.YEMN_POSTGUARANTEED, 0),
                                    0, yemn.YEMN_QUANTITY,
                                    DECODE (
                                       SIGN (
                                            yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                          - yemn.YEMN_QUANTITY),
                                       1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                                       yemn.YEMN_QUANTITY)))
                       FROM YEMN, YEVM yevm_2
                      WHERE     YEMN.YEMN_YEVM_ID = yevm_2.YEVM_ID
                            AND YEMN.YEMN_PKGINCL = 0
                            AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE),
                    0)
               + NVL (
                    (SELECT SUM (YEMI.YEMI_ORG_PRICE * YEMI.YEMI_QUANTITY)
                       FROM YEMI, YEVM yevm_2
                      WHERE     YEMI.YEMI_YEVM_ID = yevm_2.YEVM_ID
                            AND YEMI.YEMI_INCLUDEDINTHEMENU = 0
                            AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND yevm_2.YEVM_STARTDATE = YEVM.YEVM_STARTDATE),
                    0)
               + NVL (
                    (SELECT SUM (YPKD.YPKD_PRICE * YPKD.YPKD_NOOFATTENDEES)
                       FROM YPKD
                      WHERE     YPKD.YPKD_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND TRUNC (YPKD.YPKD_STARTTIME) = YEVM.YEVM_STARTDATE),
                    0),
               ybom.zcur_shortdesc)
               FTOT_ORGREV_PER_DAY,
            V8_FNC_YEBS_YFSR_REP_INFOS (YEVM.YEVM_ID, 3, 1) ORG_RATES,
            V8_FNC_YEBS_YFSR_REP_INFOS (YEVM.YEVM_ID, 3, 2) REBATE_PERC,
            V8_FNC_YEBS_YFSR_REP_INFOS (YEVM.YEVM_ID, 3, 3) REBATE_FLAT,
            yevm_alternative AlternativeFlag,
            v8_fnc_ccm_alternatives ('YEVM',
                                     yevm.yevm_id,
                                     1,
                                     1)
               Alternatives,
            ybom.zcur_shortdesc zcur_shortdesc
       FROM YEVM,
            (SELECT ybom_id,
                    (SELECT zcur_shortdesc
                       FROM zcur
                      WHERE zcur_id = ybom_base_zcur_id)
                       zcur_shortdesc
               FROM ybom) ybom
      WHERE yevm_ybom_id = ybom.ybom_id AND NVL (yevm_mergedto_yevm_id, 0) = 0
   ORDER BY yevm.yevm_startdate, yevm.yevm_displorder;


-- View to get Revenue Information, this is for the detailed Folio Style to be used in Multi Currency environment (also showing print sep. VAT as one amount per tax code grouped by currency)

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_MULTICURR
AS 
SELECT
  ZPOS.ZPOS_POSTDATE DAT,
  ZPOS.ZPOS_CDT CDT,
  ZPOS.ZPOS_ID ZPOS_ID,
  DECODE (ZDCO.ZDCO_PAYM_TYPE,2,1,0) IS_CL,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY)) QTY,
  (CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR  NVL(zpos_zcur_id,0) =0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity)   ,-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity))   ,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END) GROSS, 
  (CASE WHEN(NVL(zpos_zcur_id,0)) =NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END) FOREIGN_GROSS,
  (CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount))) ELSE NULL END)/*used to show foreign exchange rate amount on invoice*/ FOREIGN_EXCHANGE, 
  (CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))) ELSE NULL END)/*used to show local exchange rate amount on invoice*/ LOCAL_EXCHANGE,
  (CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN ltrim((decode(zpos.ZPOS_REVERSEEXCHRATE,1,to_char(zpos.ZPOS_EXCHANGERATE,'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,'''),to_char((1/zpos.ZPOS_EXCHANGERATE),'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,''')))) ELSE NULL END) EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY,      
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND (NVL(zpos_zcur_id,0)<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_CL,
  decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_ORG_NAME,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_COMMENT_ORG,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      DDESC_LF_COM_ORG_ALL,
  ZPOS.ZPOS_PHONENR                                                                   PHONE,
  (case when nvl(ZPOS.ZPOS_PHONENR,'0')<>'0' then SUBSTR(ZPOS.ZPOS_PHONENR,1,LENGTH(ZPOS.ZPOS_PHONENR)-3)||'XXX' else ZPOS.ZPOS_PHONENR END) PHONE_XXX,
  ZDCO.ZDCO_ID                                                                        ZDCO_ID,
  ZDCO.ZDCO_NUMERICDESC                                                            NDESC,
  ZPOS.ZPOS_TAXLINK_ID                                                                   TAXLINK_ID,
  (DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))) LDESC,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) ORG_NAME,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) ORG_NAME_ROOM,
  (DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) ORG_NAME_ROOM_IF,
  (DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))) ORG_ROOM,
  DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID)))) ORG_ROOM_IF,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID,
  (SELECT XCCS.XCCS_NUMBER FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) CreditCardNumber,
  (SELECT 'XXXX XXXX XXXX '||SUBSTR(XCCS.XCCS_NUMBER,LENGTH(XCCS.XCCS_NUMBER)-3,4) FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) HiddenCreditCardNumber,
  (SELECT YRES.YRES_CRSRESNR FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) CRSRESNR,
  (CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_shortdesc FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END) /*returns the contracted curr. in case it is <> local curr.*/ CONTRACTED_CURR,      
  (CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_id FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_id FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END) /*returns the contracted curr. ID in case it is <> local curr.*/ CONTRACTED_CURR_ID
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)<>2 AND
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND 
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
--  AND ZWIN_ID=4785
UNION ALL
SELECT
  null DAT,
  ZPOS.ZPOS_CDT CDT,
  null ZPOS_ID,
  null IS_CL,
  null QTY,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity)   ,-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity))   ,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS, 
  sum((CASE WHEN(NVL(zpos_zcur_id,0)) =NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS,
  null FOREIGN_EXCHANGE,
  null LOCAL_EXCHANGE,
  null EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY,   
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND (NVL(zpos_zcur_id,0)<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_CL,
 MAX(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ))DDESC,
 MAX(RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                      ) DDESC_LF_ORG_NAME,
 MAX(RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                      ) DDESC_LF_COMMENT_ORG,
  MAX(RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      )DDESC_LF_COM_ORG_ALL,
  null PHONE,
  null PHONE_XXX,
  null ZDCO_ID,
  9999 NDESC,
  null TAXLINK_ID,
  max((DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))))) LDESC,
  null ORG_NAME,
  null ORG_NAME_ROOM,
  null ORG_NAME_ROOM_IF,
  null ORG_ROOM,
  null ORG_ROOM_IF,
  ZWIN.ZWIN_ID ZWIN_ID,
  null CreditCardNumber,
  null HiddenCreditCardNumber,
  null CRSRESNR,
  max((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_shortdesc FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END)) CONTRACTED_CURR, 
  max((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_id FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_id FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END)) CONTRACTED_CURR_ID
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)=2 AND 
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
--  AND ZWIN_ID=4785  
  GROUP BY ZPOS.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

  
-- View to get Revenue Information grouped by Rate Amount, Currency and Department Codes, this is for the Group Folio Style per Day to be used in Multi Currency environment (also showing print sep. VAT as one amount per tax code grouped by currency)

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_MULTI_GRP_PD
AS 
SELECT
 DAT_S DAT,
 max(CDT_S) CDT,
 max(ZPOS_ID_S) ZPOS_ID,
 max(IS_CL_S) IS_CL,
 count(1) QTY,
 sum(GROSS_S) GROSS,
 GROSS_S GROSS_SGL,
 sum(FOREIGN_GROSS_S) FOREIGN_GROSS,
 FOREIGN_GROSS_S FOREIGN_GROSS_SGL,
 sum(FOREIGN_EXCHANGE_S) FOREIGN_EXCHANGE,
 sum(LOCAL_EXCHANGE_S) LOCAL_EXCHANGE,
 max(EXCHANGERATE_S) EXCHANGERATE,
 CURRENCY_S CURRENCY,
 max(SGROSS_S) SGROSS,
 max(SFOREIGN_GROSS_S) SFOREIGN_GROSS,
 max(OPENA_S) OPENA,
 max(DDESC_S) DDESC,
 max(DDESC_LF_ORG_NAME_S) DDESC_LF_ORG_NAME,
 max(DDESC_LF_COMMENT_ORG_S) DDESC_LF_COMMENT_ORG,
 max(DDESC_LF_COM_ORG_ALL_S) DDESC_LF_COM_ORG_ALL,
 max(PHONE_S) PHONE,
 max(PHONE_XXX_S) PHONE_XXX,
 ZDCO_ID_S ZDCO_ID,
 max(NDESC_S) NDESC,
 max(TAXLINK_ID_S) TAXLINK_ID,
 max(LDESC_S) LDESC,
 max(ORG_NAME_S) ORG_NAME,
 max(ORG_NAME_ROOM_S) ORG_NAME_ROOM,
 max(ORG_NAME_ROOM_IF_S) ORG_NAME_ROOM_IF,
 max(ORG_ROOM_S)ORG_ROOM,
 max(ORG_ROOM_IF_S) ORG_ROOM_IF,
 ZWIN_ID_S ZWIN_ID,
 max(CREDITCARDNUMBER_S) CREDITCARDNUMBER,
 max(HIDDENCREDITCARDNUMBER_S) HIDDENCREDITCARDNUMBER,
 max(CRSRESNR_S) CRSRESNR,
 max(CONTRACTED_CURR_S) CONTRACTED_CURR
 FROM      
(
SELECT
  ZPOS.ZPOS_POSTDATE DAT_S,
  ZPOS.ZPOS_CDT CDT_S,
  ZPOS.ZPOS_ID ZPOS_ID_S,
  DECODE (ZDCO.ZDCO_PAYM_TYPE,2,1,0) IS_CL_S,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY)) QTY_S,
  ((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS_S, 
  ((CASE WHEN(NVL(zpos_zcur_id,0))=NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS_S,
  ((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount))) ELSE NULL END))/*used to show foreign exchange rate amount on invoice*/ FOREIGN_EXCHANGE_S, 
  ((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))) ELSE NULL END)) /*used to show local exchange rate amount on invoice*/ LOCAL_EXCHANGE_S,
  (CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN ltrim((decode(zpos.ZPOS_REVERSEEXCHRATE,1,to_char(zpos.ZPOS_EXCHANGERATE,'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,'''),to_char((1/zpos.ZPOS_EXCHANGERATE),'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,''')))) ELSE NULL END) EXCHANGERATE_s, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY_S,      
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND (NVL(zpos_zcur_id,0)<>NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS_S,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS_S,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA_S,
   decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC_S,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_ORG_NAME_S,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_COMMENT_ORG_S,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      DDESC_LF_COM_ORG_ALL_S,
  ZPOS.ZPOS_PHONENR                                                                   PHONE_S,
  (case when nvl(ZPOS.ZPOS_PHONENR,'0')<>'0' then SUBSTR(ZPOS.ZPOS_PHONENR,1,LENGTH(ZPOS.ZPOS_PHONENR)-3)||'XXX' else ZPOS.ZPOS_PHONENR END) PHONE_XXX_S,
  ZDCO.ZDCO_ID                                                                        ZDCO_ID_S,
  ZDCO.ZDCO_NUMERICDESC                                                            NDESC_S,
  ZPOS.ZPOS_TAXLINK_ID                                                                   TAXLINK_ID_S,
  ((DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))))) LDESC_S,
  ((DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID)))) ORG_NAME_S,
  ((DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID)))) ORG_NAME_ROOM_S,
  ((DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID)))))) ORG_NAME_ROOM_IF_S,
  ((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID)))) ORG_ROOM_S,
  (DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))) ORG_ROOM_IF_S,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID   ZWIN_INVOICE_TO_XCMS_ID,
  (SELECT XCCS.XCCS_NUMBER FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) CreditCardNumber_S,
  (SELECT 'XXXX XXXX XXXX '||SUBSTR(XCCS.XCCS_NUMBER,LENGTH(XCCS.XCCS_NUMBER)-3,4) FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) HiddenCreditCardNumber_S,
  (SELECT YRES.YRES_CRSRESNR FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) CRSRESNR_S,
  (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns the contracted curr. in case it is <> local curr.*/ CONTRACTED_CURR_S      
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)<>2 AND
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND 
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
) 
GROUP BY DAT_S, DECODE(CDT_S,1,CDT_S,ZPOS_ID_S), ZDCO_ID_S, GROSS_S, FOREIGN_GROSS_S, CURRENCY_S, ZWIN_ID_S
UNION ALL
SELECT
  null DAT,
  ZPOS.ZPOS_CDT CDT,
  null ZPOS_ID,
  null IS_CL,
  null QTY,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS,
  null GROSS_SGL, 
  sum((CASE WHEN(NVL(zpos_zcur_id,0))=NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS,
  null FOREIGN_GROSS,
  null FOREIGN_EXCHANGE,
  null LOCAL_EXCHANGE,
  null EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY,   
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND (NVL(zpos_zcur_id,0)<>NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS_S,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS_S,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA_S,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC_LF_ORG_NAME,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC_LF_COMMENT_ORG,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC_LF_COM_ORG_ALL,
  null PHONE,
  null PHONE_XXX,
  null ZDCO_ID,
  9999 NDESC,
  null TAXLINK_ID,
  max((DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))))) LDESC,
  null ORG_NAME,
  null ORG_NAME_ROOM,
  null ORG_NAME_ROOM_IF,
  null ORG_ROOM,
  null ORG_ROOM_IF,
  ZWIN.ZWIN_ID ZWIN_ID,
  null CreditCardNumber,
  null HiddenCreditCardNumber,
  null CRSRESNR,
  max((SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))) CONTRACTED_CURR 
FROM
  ZPOS,
  ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)=2 AND 
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
  GROUP BY ZPOS.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

  
-- View to get Revenue Information grouped by Rate Amount, Currency and Department Codes, this is for the Group Folio Style per Stay to be used in Multi Currency environment (also showing print sep. VAT as one amount per tax code grouped by currency)

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_MULTI_GRP_PS
AS 
SELECT
 max(DAT_S) DAT,
 max(CDT_S) CDT,
 max(ZPOS_ID_S) ZPOS_ID,
 max(IS_CL_S) IS_CL,
 count(1) QTY,
 sum(GROSS_S) GROSS,
 GROSS_S GROSS_SGL,
 sum(FOREIGN_GROSS_S) FOREIGN_GROSS,
 FOREIGN_GROSS_S FOREIGN_GROSS_SGL,
 sum(FOREIGN_EXCHANGE_S) FOREIGN_EXCHANGE,
 sum(LOCAL_EXCHANGE_S) LOCAL_EXCHANGE,
 max(EXCHANGERATE_S) EXCHANGERATE,
 CURRENCY_S CURRENCY,
 max(SGROSS_S) SGROSS,
 max(SFOREIGN_GROSS_S) SFOREIGN_GROSS,
 max(OPENA_S) OPENA,
 max(DDESC_S) DDESC,
 max(DDESC_LF_ORG_NAME_S) DDESC_LF_ORG_NAME,
 max(DDESC_LF_COMMENT_ORG_S) DDESC_LF_COMMENT_ORG,
 max(DDESC_LF_COM_ORG_ALL_S) DDESC_LF_COM_ORG_ALL,
 max(PHONE_S) PHONE,
 max(PHONE_XXX_S) PHONE_XXX,
 ZDCO_ID_S ZDCO_ID,
 max(NDESC_S) NDESC,
 max(TAXLINK_ID_S) TAXLINK_ID,
 max(LDESC_S) LDESC,
 max(ORG_NAME_S) ORG_NAME,
 max(ORG_NAME_ROOM_S) ORG_NAME_ROOM,
 max(ORG_NAME_ROOM_IF_S) ORG_NAME_ROOM_IF,
 max(ORG_ROOM_S)ORG_ROOM,
 max(ORG_ROOM_IF_S) ORG_ROOM_IF,
 ZWIN_ID_S ZWIN_ID,
 max(CREDITCARDNUMBER_S) CREDITCARDNUMBER,
 max(HIDDENCREDITCARDNUMBER_S) HIDDENCREDITCARDNUMBER,
 max(CRSRESNR_S) CRSRESNR,
 max(CONTRACTED_CURR_S) CONTRACTED_CURR
 FROM      
(
SELECT
  ZPOS.ZPOS_POSTDATE DAT_S,
  ZPOS.ZPOS_CDT CDT_S,
  ZPOS.ZPOS_ID ZPOS_ID_S,
  DECODE (ZDCO.ZDCO_PAYM_TYPE,2,1,0) IS_CL_S,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY)) QTY_S,
  ((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS_S, 
  ((CASE WHEN(NVL(zpos_zcur_id,0))=NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS_s,
  ((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount))) ELSE NULL END))/*used to show foreign exchange rate amount on invoice*/ FOREIGN_EXCHANGE_S, 
  ((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))) ELSE NULL END)) /*used to show local exchange rate amount on invoice*/ LOCAL_EXCHANGE_S,
  (CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN ltrim((decode(zpos.ZPOS_REVERSEEXCHRATE,1,to_char(zpos.ZPOS_EXCHANGERATE,'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,'''),to_char((1/zpos.ZPOS_EXCHANGERATE),'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,''')))) ELSE NULL END) EXCHANGERATE_s, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY_S,      
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND (NVL(zpos_zcur_id,0)<>NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS_S,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS_S,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA_S,
  decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC_S,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_ORG_NAME_S,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_COMMENT_ORG_S,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      DDESC_LF_COM_ORG_ALL_S,
  ZPOS.ZPOS_PHONENR                                                                   PHONE_S,
  (case when nvl(ZPOS.ZPOS_PHONENR,'0')<>'0' then SUBSTR(ZPOS.ZPOS_PHONENR,1,LENGTH(ZPOS.ZPOS_PHONENR)-3)||'XXX' else ZPOS.ZPOS_PHONENR END) PHONE_XXX_S,
  ZDCO.ZDCO_ID                                                                        ZDCO_ID_S,
  ZDCO.ZDCO_NUMERICDESC                                                            NDESC_S,
  ZPOS.ZPOS_TAXLINK_ID                                                                   TAXLINK_ID_S,
  ((DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))))) LDESC_S,
  ((DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID)))) ORG_NAME_S,
  ((DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID)))) ORG_NAME_ROOM_S,
  ((DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID)))))) ORG_NAME_ROOM_IF_S,
  ((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID)))) ORG_ROOM_S,
  (DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))) ORG_ROOM_IF_S,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID                ZWIN_INVOICE_TO_XCMS_ID,
  (SELECT XCCS.XCCS_NUMBER FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) CreditCardNumber_S,
  (SELECT 'XXXX XXXX XXXX '||SUBSTR(XCCS.XCCS_NUMBER,LENGTH(XCCS.XCCS_NUMBER)-3,4) FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) HiddenCreditCardNumber_S,
  (SELECT YRES.YRES_CRSRESNR FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) CRSRESNR_S,
  (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns the contracted curr. in case it is <> local curr.*/ CONTRACTED_CURR_S      
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)<>2 AND
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND 
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
) 
GROUP BY DECODE(CDT_S,1,CDT_S,ZPOS_ID_S), ZDCO_ID_S, GROSS_S, FOREIGN_GROSS_S, CURRENCY_S, ZWIN_ID_S
UNION ALL
SELECT
  null DAT,
  ZPOS.ZPOS_CDT CDT,
  null ZPOS_ID,
  null IS_CL,
  null QTY,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS,
  null GROSS_SGL, 
  sum((CASE WHEN(NVL(zpos_zcur_id,0))=NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS,
  null FOREIGN_GROSS,
  null FOREIGN_EXCHANGE,
  null LOCAL_EXCHANGE,
  null EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY,   
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND (NVL(zpos_zcur_id,0)<>NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS_S,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS_S,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA_S,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC_LF_ORG_NAME,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC_LF_COMMENT_ORG,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC_LF_COM_ORG_ALL,
  null PHONE,
  null PHONE_XXX,
  null ZDCO_ID,
  9999 NDESC,
  null TAXLINK_ID,
  max((DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))))) LDESC,
  null ORG_NAME,
  null ORG_NAME_ROOM,
  null ORG_NAME_ROOM_IF,
  null ORG_ROOM,
  null ORG_ROOM_IF,
  ZWIN.ZWIN_ID ZWIN_ID,
  null CreditCardNumber,
  null HiddenCreditCardNumber,
  null CRSRESNR,
  max((SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))) CONTRACTED_CURR 
FROM
  ZPOS,
  ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)=2 AND 
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
  GROUP BY ZPOS.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

  
-- View to get Revenue Information grouped by Arrangement Codes, this is for the Folio Style Arrangement Code per Day to be used in Multi Currency environment (also showing print sep. VAT as one amount per tax code grouped by currency)

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_MULTI_ARR_PD
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZDCO_ID_S ZDCO_ID,
  REC_COUNT_S QTY,
  GROSS_S GROSS,
  FOREIGN_GROSS_S FOREIGN_GROSS,
  FOREIGN_EXCHANGE_S FOREIGN_EXCHANGE, 
  LOCAL_EXCHANGE_S LOCAL_EXCHANGE,
  EXCHANGERATE_S  EXCHANGERATE, 
  CURRENCY_S CURRENCY,      
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S AND (NVL(zpos_zcur_id,0)<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
 decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,
  ZWIN_ID_S ZWIN_ID,
  CONTRACTED_CURR_S CONTRACTED_CURR  
FROM
(
SELECT
  zpos.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MAX(zpos.ZPOS_ID) ZPOS_ID_S,
  MAX(zpos.ZPOS_ZDCO_ID) ZDCO_ID_S,
  COUNT(1) REC_COUNT_S,   
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity)   ,-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity))   ,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS_S, 
  sum((CASE WHEN(NVL(zpos_zcur_id,0)) =NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS_S,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount))) ELSE NULL END))/*used to show foreign exchange rate amount on invoice*/ FOREIGN_EXCHANGE_S,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))) ELSE NULL END))/*used to show local exchange rate amount on invoice*/ LOCAL_EXCHANGE_S,
  MAX((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN ltrim((decode(zpos.ZPOS_REVERSEEXCHRATE,1,to_char(zpos.ZPOS_EXCHANGERATE,'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,'''),to_char((1/zpos.ZPOS_EXCHANGERATE),'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,''')))) ELSE NULL END)) EXCHANGERATE_S,
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY_S,
  MAX((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_shortdesc FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END))/*returns the contracted curr. in case it is <> local curr.*/ CONTRACTED_CURR_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  DECODE(NVL(zpos.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(zpos.ZPOS_ZARR_ID,0),0,zpos.ZPOS_ID,zpos.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)<>2 AND
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND 
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
GROUP BY ZPOS_ZCUR_ID, ZWIN.ZWIN_ID, ZWIN_INVOICE_TO_XCMS_ID, zpos.ZPOS_POSTDATE, DECODE(NVL(zpos.ZPOS_ZARR_ID,0),0,zpos.ZPOS_ID,zpos.ZPOS_ZARR_ID), DECODE(NVL(zpos.ZPOS_ZARR_ID,0),0,1,0)
)
UNION ALL
SELECT
  null DAT,
  ZPOS.ZPOS_CDT CDT,
  null ZPOS_ID,
  null ZDCO_ID,
  null QTY,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity)   ,-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity))   ,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS, 
  sum((CASE WHEN(NVL(zpos_zcur_id,0)) =NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS,
  null FOREIGN_EXCHANGE,
  null LOCAL_EXCHANGE,
  null EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY,   
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND (NVL(zpos_zcur_id,0)<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC,
  ZWIN.ZWIN_ID ZWIN_ID,
  max((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_shortdesc FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END)) CONTRACTED_CURR 
FROM
  ZPOS,
  ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)=2 AND 
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
  GROUP BY ZPOS.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

  
-- View to get Revenue Information grouped by Arrangement Codes, this is for the Folio Style Arrangement Code per Stay to be used in Multi Currency environment (also showing print sep. VAT as one amount per tax code grouped by currency)

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_MULTI_ARR_PS
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZDCO_ID_S ZDCO_ID,
  REC_COUNT_S QTY,
  GROSS_S GROSS,
  FOREIGN_GROSS_S FOREIGN_GROSS,
  FOREIGN_EXCHANGE_S FOREIGN_EXCHANGE, 
  LOCAL_EXCHANGE_S LOCAL_EXCHANGE,
  EXCHANGERATE_S  EXCHANGERATE, 
  CURRENCY_S CURRENCY,      
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S AND (NVL(zpos_zcur_id,0)<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
  decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,
  ZWIN_ID_S ZWIN_ID,
  CONTRACTED_CURR_S CONTRACTED_CURR  
FROM
(
SELECT
  MAX(zpos.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MAX(zpos.ZPOS_ID) ZPOS_ID_S,
  MAX(zpos.ZPOS_ZDCO_ID) ZDCO_ID_S,
  COUNT(1) REC_COUNT_S,   
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity)   ,-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity))   ,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS_S, 
  sum((CASE WHEN(NVL(zpos_zcur_id,0)) =NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS_S,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount))) ELSE NULL END))/*used to show foreign exchange rate amount on invoice*/ FOREIGN_EXCHANGE_S,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))) ELSE NULL END))/*used to show local exchange rate amount on invoice*/ LOCAL_EXCHANGE_S,
  MAX((CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN ltrim((decode(zpos.ZPOS_REVERSEEXCHRATE,1,to_char(zpos.ZPOS_EXCHANGERATE,'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,'''),to_char((1/zpos.ZPOS_EXCHANGERATE),'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,''')))) ELSE NULL END)) EXCHANGERATE_S,
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY_S,
  MAX((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_shortdesc FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END))/*returns the contracted curr. in case it is <> local curr.*/ CONTRACTED_CURR_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
    ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  DECODE(NVL(zpos.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(zpos.ZPOS_ZARR_ID,0),0,zpos.ZPOS_ID,zpos.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)<>2 AND
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND 
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
GROUP BY ZPOS_ZCUR_ID, ZWIN.ZWIN_ID,  ZWIN.ZWIN_INVOICE_TO_XCMS_ID, DECODE(NVL(zpos.ZPOS_ZARR_ID,0),0,zpos.ZPOS_ID,zpos.ZPOS_ZARR_ID), DECODE(NVL(zpos.ZPOS_ZARR_ID,0),0,1,0)
)
UNION ALL
SELECT
  null DAT,
  ZPOS.ZPOS_CDT CDT,
  null ZPOS_ID,
  null ZDCO_ID,
  null QTY,
  sum((CASE WHEN(NVL(zpos_zcur_id,0))<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL((zpos_zcur_id),0)=0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity)   ,-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity))   ,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)) ELSE NULL END)) GROSS, 
  sum((CASE WHEN(NVL(zpos_zcur_id,0)) =NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) AND NVL(zpos_zcur_id,0)<>0 THEN DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)) ELSE NULL END)) FOREIGN_GROSS,
  null FOREIGN_EXCHANGE,
  null LOCAL_EXCHANGE,
  null EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,0) AND NVL(zpos_zcur_id,0)<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')))/*returns curr. of the posting*/ CURRENCY,   
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND (NVL(zpos_zcur_id,0)<>NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) OR NVL(zpos_zcur_id,0)=0) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SGROSS,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),-(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID AND NVL(zpos_zcur_id,0)<>0 AND (NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) AND ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0)) SFOREIGN_GROSS,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
  max((CASE WHEN ((ZPOS.ZPOS_DESCRIPT=ZDCO.ZDCO_LONGDESC) OR ZPOS.ZPOS_DESCRIPT=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1))  THEN (DECODE(ZPOS.ZPOS_DESCRIPT,(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=ZPOS.ZPOS_POSTDATE AND YDET.YDET_YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ROWNUM=1),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))))) ELSE ZPOS.ZPOS_DESCRIPT END)) DDESC,
  ZWIN.ZWIN_ID ZWIN_ID,
  max((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_shortdesc FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END)) CONTRACTED_CURR 
FROM
  ZPOS,
  ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND NVL(ZPOS.ZPOS_CDT,0)=2 AND 
  ZPOS_ZDCO_ID<>NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0) AND
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
  GROUP BY ZPOS.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

  
CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_CURR
AS 
SELECT
/* 
View to get Revenue Information in local Currency (LOCAL) and Res. contracted Currency (FOREIGN). This is for the Folio Style 'Foreign Currency Folio Detailed' (using Global Setting 'Show invoice in foreign currency' to be able to show Invoice in Currency other than local Currency, e.g. Reservation contracted foreign Currency).
Also works in case Global Setting 'Show invoice in foreign currency' is disabled, therefore can be always used. 
*/  
  ZPOS.ZPOS_POSTDATE DAT,
  ZPOS.ZPOS_CDT CDT,
  ZPOS.ZPOS_ID ZPOS_ID,
  (DECODE (ZDCO.ZDCO_PAYM_TYPE,2,1,0)) IS_CL,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY)) QTY,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) LOCAL_DEBIT, 
  (CASE WHEN NVL(zpos_zcur_id,0)<>0 AND NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_foreignprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) ELSE /*here comes the exchange calc */ (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -(DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),ZPOS.zpos_unitprice)*ZPOS.zpos_quantity) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)), NULL), DECODE(ZPOS.ZPOS_CDT,5,0, DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),ZPOS.zpos_sumupamount) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) ) ))) END) FOREIGN_DEBIT,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) LOCAL_CREDIT,
  (CASE WHEN NVL(zpos_zcur_id,0)<>0 AND NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) ELSE /*here comes the exchange calc */ (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) ),TO_NUMBER(NULL))) END) FOREIGN_CREDIT, 
  (CASE WHEN(NVL(zpos_zcur_id,0))<>0 AND (NVL(zpos_zcur_id,0))<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) THEN ltrim((decode(zpos.ZPOS_REVERSEEXCHRATE,1,to_char((1/zpos.ZPOS_EXCHANGERATE),'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,'''),to_char(zpos.ZPOS_EXCHANGERATE,'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,''')))) ELSE ltrim(to_char((SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)),'99999990D999999', 'NLS_NUMERIC_CHARACTERS = ''.,''')) END) EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))))/*returns curr. of the posting*/ POSTING_CURRENCY,      
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) LOCAL_SDEBIT,
  (SELECT SUM(CASE WHEN NVL(zpos_zcur_id,0)<>0 AND NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_foreignprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) ELSE /*here comes the exchange calc */ (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -(DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),ZPOS.zpos_unitprice)*ZPOS.zpos_quantity) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)), NULL), DECODE(ZPOS.ZPOS_CDT,5,0, DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),ZPOS.zpos_sumupamount) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) ) ))) END) 
   FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) FOREIGN_SDEBIT,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) LOCAL_SCREDIT,
  (SELECT SUM(CASE WHEN NVL(zpos_zcur_id,0)<>0 AND NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) ELSE /*here comes the exchange calc */ (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) ),TO_NUMBER(NULL))) END) 
   FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) FOREIGN_SCREDIT,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) LOCAL_OPENA,
  ((SELECT NVL(SUM(CASE WHEN NVL(zpos_zcur_id,0)<>0 AND NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_foreignprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_foreignsumupamount))) ELSE /*here comes the exchange calc */ (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -(DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),ZPOS.zpos_unitprice)*ZPOS.zpos_quantity) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)), NULL), DECODE(ZPOS.ZPOS_CDT,5,0, DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),ZPOS.zpos_sumupamount) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) ) ))) END),0) 
   FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)
   -(SELECT NVL(SUM(CASE WHEN NVL(zpos_zcur_id,0)<>0 AND NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) ELSE /*here comes the exchange calc */ (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) ),TO_NUMBER(NULL))) END),0) 
   FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)) FOREIGN_OPENA,  
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     LOCAL_SPAY_CL,
  (SELECT SUM(CASE WHEN NVL(zpos_zcur_id,0)<>0 AND NVL(zpos_zcur_id,0)=NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0) THEN (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) ELSE /*here comes the exchange calc */ (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity) * (SELECT (DECODE(ZXCH.ZXCH_PRIMARY,0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE)) FROM ZXCH WHERE ZXCH.ZXCH_CURRENT=1 AND ZXCH.ZXCH_ZCUR_ID = NVL((CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT YRES.YRES_CONTRACT_ZCUR_ID FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND YRES.YRES_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT ZFAC.ZFAC_CONTRACT_ZCUR_ID FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND ZFAC.ZFAC_CONTRACT_ZCUR_ID<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END),0)) ),TO_NUMBER(NULL))) END) 
   FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) FOREIGN_SPAY_CL,
  decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_ORG_NAME,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_COMMENT_ORG,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      DDESC_LF_COM_ORG_ALL,
  ZPOS.ZPOS_PHONENR                                                                   PHONE,
  (case when nvl(ZPOS.ZPOS_PHONENR,'0')<>'0' then SUBSTR(ZPOS.ZPOS_PHONENR,1,LENGTH(ZPOS.ZPOS_PHONENR)-3)||'XXX' else ZPOS.ZPOS_PHONENR END) PHONE_XXX,
  ZDCO.ZDCO_ID                                                                        ZDCO_ID,
  ZDCO.ZDCO_NUMERICDESC                                                            NDESC,
  ZPOS.ZPOS_TAXLINK_ID                                                                   TAXLINK_ID,
  (DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))) LDESC,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID and rownum=1),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID and rownum=1))) ORG_NAME,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID and rownum=1),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID and rownum=1))) ORG_NAME_ROOM,
  (DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) ORG_NAME_ROOM_IF,
  (DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))) ORG_ROOM,
  DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID)))) ORG_ROOM_IF,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID,
  (SELECT XCCS.XCCS_NUMBER FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID and rownum = 1) CreditCardNumber,
  (SELECT 'XXXX XXXX XXXX '||SUBSTR(XCCS.XCCS_NUMBER,LENGTH(XCCS.XCCS_NUMBER)-3,4) FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID and rownum = 1) HiddenCreditCardNumber,
  (SELECT YRES.YRES_CRSRESNR FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) CRSRESNR,
  (CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_shortdesc FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_shortdesc FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END) /*returns the contracted curr. in case it is <> local curr.*/ RES_FOREIGN_CURR,      
  (CASE WHEN NVL(ZPOS.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_id FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=ZPOS.ZPOS_YRES_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) ELSE (SELECT zcur_id FROM ZCUR,ZFAC WHERE zcur_id = ZFAC.ZFAC_CONTRACT_ZCUR_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ZFAC_ID AND zcur_id<>(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) END) /*returns the contracted curr. ID in case it is <> local curr.*/ RES_FOREIGN_ZCUR_ID,
  (SELECT zcur_shortdesc FROM ZCUR where zcur_id=(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))) LOCAL_CURR,
  (select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) LOCAL_ZCUR_ID,
  (select wuss_value  from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')) EXCH_DIFF_ZDCO_ID
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
--  AND ZWIN_ID=5291
;


--MHoffmann 04.03.2014 new view to get revenue by srp2 type (like srpd)

CREATE OR REPLACE VIEW V8_REP_STAT_SRP2_LY
AS 
select 
   srp2_date  srp2_date
  ,srp2_zdco_id srp2_zdco_id
  ,srp2_type srp2_type
  ,nvl(sum(netrevd),0) netrevd
  ,nvl(sum(netrevdly),0) netrevdly
  ,nvl(sum(netrevm),0) netrevm
  ,nvl(sum(netrevmly),0) netrevmly
  ,nvl(sum(netrevy),0) netrevy
  ,nvl(sum(netrevyly),0) netrevyly 
  ,nvl(sum(grossrevd),0) grossrevd
  ,nvl(sum(grossrevdly),0) grossrevdly
  ,nvl(sum(grossrevm),0) grossrevm
  ,nvl(sum(grossrevmly),0) grossrevmly
  ,nvl(sum(grossrevy),0) grossrevy
  ,nvl(sum(grossrevyly),0) grossrevyly 
from (
select 
   srp2_date
  ,srp2_zdco_id
  ,srp2_type
  ,srp2_netrev_D netrevd,0 netrevdly
  ,srp2_netrev_M netrevm,0 netrevmly
  ,srp2_netrev_y netrevy,0 netrevyly
  ,srp2_grossrev_D grossrevd,0 grossrevdly
  ,srp2_grossrev_M grossrevm,0 grossrevmly
  ,srp2_grossrev_y grossrevy,0 grossrevyly
from srp2,wdat where wdat_date=srp2_date
union all
select 
   wdat_date
  ,srp2_zdco_id
  ,srp2_type
  ,0 netrevd,srp2_netrev_d netrevdly
  ,0 netrevm,srp2_netrev_m netrevmly
  ,0 netrevy,srp2_netrev_y netrevyly
  ,0 grossrevd,srp2_grossrev_d grossrevdly
  ,0 grossrevm,srp2_grossrev_m grossrevmly
  ,0 grossrevy,srp2_grossrev_y grossrevyly
from srp2, v8_sys_wdat_next_prev_year wdat
where (select wdat_PrevYear from wdat w1 where w1.wdat_date=wdat.wdat_date and rownum=1) = srp2_date
) 
group by srp2_date,srp2_zdco_id,srp2_type;


create or replace view v8_stat_ccm_rooms_det_ind as
select
  yres_id,
  yres_ybom_id ybom_id,
  wdat.wdat_date,
  (SELECT SUM (YPOS.YPOS_QUANTITY * YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID = YRES.YRES_ID AND YPOS.YPOS_TYPE <> 1 and ypos_date = wdat_date) CONF_RATE_STAY,
  (SELECT SUM (YPOS.YPOS_QUANTITY * YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID = YRES.YRES_ID AND YPOS.YPOS_DT <> 2 and ypos_date = wdat_date) CONF_RATE_NET_STAY
from yres, ydet, wdat
where yres_current_ydet_id=ydet.ydet_id and yres_resstatus <>3  and nvl(ydet_ybld_id, 0) = 0 and nvl(yres_ygrp_id, 0) = 0 and nvl(ydet_ygrp_id, 0)=0 and wdat_date between yres_exparrtime and yres_expdeptime;

create or replace view v8_stat_ccm_rooms_det_grp as
select
  yres_id,
  yres_ybom_id ybom_id,
  wdat.wdat_date,
  (SELECT SUM (YPOS.YPOS_QUANTITY * YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID = YRES.YRES_ID AND YPOS.YPOS_TYPE <> 1 and ypos_date = wdat_date)  CONF_RATE_STAY,
  (SELECT SUM (YPOS.YPOS_QUANTITY * YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID = YRES.YRES_ID AND YPOS.YPOS_DT <> 2 and ypos_date = wdat_date) CONF_RATE_NET_STAY
from yres, ydet, wdat
where yres_current_ydet_id=ydet.ydet_id and yres_resstatus <>3 and wdat_date between yres_exparrtime and yres_expdeptime and
( 
  ( 
    (nvl (yres.yres_xcms_id, 0) = 0 and nvl (yres.yres_ygrp_id, 0) <> 0) or ( nvl (ydet.ydet_ygrp_id, 0) <> 0 and ydet.ydet_noofrooms > 1 )
   ) or
  (
    (nvl (yres.yres_xcms_id, 0) <> 0 and nvl (yres.yres_ygrp_id, 0) = 0)
          and nvl (ydet.ydet_ygrp_id, 0) <> 0
          and ydet.ydet_noofrooms  < 2
  )
);

create or replace view v8_stat_ccm_rooms_det_blk as
select
  0 yres_id,
  yblh_ybom_id ybom_id,
  v.ybld_date wdat_date,
  (select sum (ybos.ybos_quantity * ybos.ybos_unitprice) * nvl (decode (v.ybst_openforpickup, 1, v.available, v.ybld_initialnoofrooms), 0) from ybos where ybos.ybos_ybld_id = v.ybld_id and ybos.ybos_date=v.ybld_date) CONF_RATE_STAY,
  (select sum (ybos.ybos_quantity * ybos.ybos_unitprice) * nvl (decode (v.ybst_openforpickup, 1, v.available, v.ybld_initialnoofrooms), 0) from ybos where ybos.ybos_ybld_id = v.ybld_id and ybos.ybos_dt = 1  and ybos.ybos_date=v.ybld_date) CONF_RATE_NET_STAY 
from V8_ED_BLOCK_DETAILS v  
where nvl (v.ybst_cancelstatus, 0) <> 1 and ( (v.ybst_openforpickup = 1) and (nvl (v.available, 0) <> 0) or (v.ybst_openforpickup = 0) and (nvl (v.ybld_initialnoofrooms, 0) <> 0))
union all --block picked
select 
  yres_id,
  yblh_ybom_id,
  ydet_date,
  getYDetDiscountedAmountForeign (ydet_rateamount, ydet_ratemanual, ydet_ratediff, ydet_ratediscountperc, ydet_nodiscountpackage, ydet_foreignrateamount, ydet_foreignratemanual, ydet_foreignratediff, ydet_foreignnodiscountpackage, ydet_finalAmount, ydet_finalForeignAmount) *
  nvl ( (decode ( nvl (ydet.ydet_sharenum, 0), 0, ydet.ydet_noofrooms, (case when ydet.ydet_id = (select min (ydet_2.ydet_id) from ydet ydet_2 where ydet_2.ydet_sharenum = ydet.ydet_sharenum) then (select avg (ydet_2.ydet_noofrooms) from ydet ydet_2 where ydet_2.ydet_sharenum = ydet.ydet_sharenum)  else 0 end))), 0) ,
  (select sum (ypos.ypos_unitprice) from ypos where ypos.ypos_yres_id = ydet.ydet_yres_id and ydet.ydet_date = ypos.ypos_date and ypos.ypos_dt <> 2) *
  nvl ( (decode ( nvl (ydet.ydet_sharenum, 0), 0, ydet.ydet_noofrooms, (case when ydet.ydet_id = (select min (ydet_2.ydet_id) from ydet ydet_2 where ydet_2.ydet_sharenum = ydet.ydet_sharenum) then (select avg (ydet_2.ydet_noofrooms) from ydet ydet_2 where ydet_2.ydet_sharenum = ydet.ydet_sharenum)  else 0 end))), 0)
from
  yres, ydet, ybld, yblh
where yres.yres_id=ydet.ydet_yres_id and yres_resstatus <>3 and ydet.ydet_ybld_id=ybld.ybld_id and ybld.ybld_yblh_id=yblh.yblh_id;


create or replace view v8_stat_ccm_rooms_tot_bydate as
select 
  v2.ybom_id, 
  wdat.wdat_date,
  DECODE (v2.flag_indres, 1, NVL ( (SELECT SUM (v.CONF_RATE_STAY) FROM v8_stat_ccm_rooms_det_ind v WHERE v.ybom_id = v2.ybom_id and v.wdat_date=wdat.wdat_date), 0), 0) + 
  DECODE (v2.flag_ygrp, 1, NVL ( (SELECT SUM (v.CONF_RATE_STAY) FROM v8_stat_ccm_rooms_det_grp v WHERE v.ybom_id = v2.ybom_id and v.wdat_date=wdat.wdat_date), 0), 0) + 
  DECODE (v2.flag_yblh, 1, NVL ( (SELECT SUM (v.CONF_RATE_STAY) FROM v8_stat_ccm_rooms_det_blk v WHERE v.ybom_id = v2.ybom_id and v.wdat_date=wdat.wdat_date), 0), 0) ROOM_REV,         
  DECODE (v2.flag_indres, 1, NVL ( (SELECT SUM (v.CONF_RATE_NET_STAY) FROM v8_stat_ccm_rooms_det_ind v WHERE v.ybom_id = v2.ybom_id and v.wdat_date=wdat.wdat_date), 0), 0) + 
  DECODE (v2.flag_ygrp, 1, NVL ( (SELECT SUM (v.CONF_RATE_NET_STAY) FROM v8_stat_ccm_rooms_det_grp v WHERE v.ybom_id = v2.ybom_id and v.wdat_date=wdat.wdat_date), 0), 0) + 
  DECODE (v2.flag_yblh, 1, NVL ( (SELECT SUM (v.CONF_RATE_NET_STAY) FROM v8_stat_ccm_rooms_det_blk v WHERE v.ybom_id = v2.ybom_id and v.wdat_date=wdat.wdat_date), 0), 0) ROOM_NET_REV
from V8_CCM_FLAGS v2, wdat;


create or replace view v8_stat_ccm_rooms_det_blk as
select
  0 yres_id,
  yblh_ybom_id ybom_id,
  (select sum (ybos.ybos_quantity * ybos.ybos_unitprice) * nvl (decode (v.ybst_openforpickup, 1, v.available, v.ybld_initialnoofrooms), 0) from ybos where ybos.ybos_ybld_id = v.ybld_id) CONF_RATE_STAY,
  (select sum (ybos.ybos_quantity * ybos.ybos_unitprice) * nvl (decode (v.ybst_openforpickup, 1, v.available, v.ybld_initialnoofrooms), 0) from ybos where ybos.ybos_ybld_id = v.ybld_id and ybos.ybos_dt = 1) CONF_RATE_NET_STAY 
from V8_ED_BLOCK_DETAILS v  
where nvl (v.ybst_cancelstatus, 0) <> 1 and ( (v.ybst_openforpickup = 1) and (nvl (v.available, 0) <> 0) or (v.ybst_openforpickup = 0) and (nvl (v.ybld_initialnoofrooms, 0) <> 0))
union all --block picked
select 
  yres_id,
  yblh_ybom_id,
  getYDetDiscountedAmountForeign (ydet_rateamount, ydet_ratemanual, ydet_ratediff, ydet_ratediscountperc, ydet_nodiscountpackage, ydet_foreignrateamount, ydet_foreignratemanual, ydet_foreignratediff, ydet_foreignnodiscountpackage, ydet_finalAmount, ydet_finalForeignAmount) *
  nvl ( (decode ( nvl (ydet.ydet_sharenum, 0), 0, ydet.ydet_noofrooms, (case when ydet.ydet_id = (select min (ydet_2.ydet_id) from ydet ydet_2 where ydet_2.ydet_sharenum = ydet.ydet_sharenum) then (select avg (ydet_2.ydet_noofrooms) from ydet ydet_2 where ydet_2.ydet_sharenum = ydet.ydet_sharenum)  else 0 end))), 0) ,
  (select sum (ypos.ypos_unitprice) from ypos where ypos.ypos_yres_id = ydet.ydet_yres_id and ydet.ydet_date = ypos.ypos_date and ypos.ypos_dt <> 2) *
  nvl ( (decode ( nvl (ydet.ydet_sharenum, 0), 0, ydet.ydet_noofrooms, (case when ydet.ydet_id = (select min (ydet_2.ydet_id) from ydet ydet_2 where ydet_2.ydet_sharenum = ydet.ydet_sharenum) then (select avg (ydet_2.ydet_noofrooms) from ydet ydet_2 where ydet_2.ydet_sharenum = ydet.ydet_sharenum)  else 0 end))), 0)
from
  yres, ydet, ybld, yblh
where yres.yres_id=ydet.ydet_yres_id and yres_resstatus <>3 and ydet.ydet_ybld_id=ybld.ybld_id and ybld.ybld_yblh_id=yblh.yblh_id;

create or replace view v8_stat_ccm_rooms_totals as
select 
v2.ybom_id, 
  DECODE (v2.flag_indres, 1, NVL ( (SELECT SUM (v.CONF_RATE_STAY) FROM v8_stat_ccm_rooms_det_ind v WHERE v.ybom_id = v2.ybom_id), 0), 0) + 
  DECODE (v2.flag_ygrp, 1, NVL ( (SELECT SUM (v.CONF_RATE_STAY) FROM v8_stat_ccm_rooms_det_grp v WHERE v.ybom_id = v2.ybom_id), 0), 0) + 
  DECODE (v2.flag_yblh, 1, NVL ( (SELECT SUM (v.CONF_RATE_STAY) FROM v8_stat_ccm_rooms_det_blk v WHERE v.ybom_id = v2.ybom_id), 0), 0) ROOM_REV,         
  DECODE (v2.flag_indres, 1, NVL ( (SELECT SUM (v.CONF_RATE_NET_STAY) FROM v8_stat_ccm_rooms_det_ind v WHERE v.ybom_id = v2.ybom_id), 0), 0) + 
  DECODE (v2.flag_ygrp, 1, NVL ( (SELECT SUM (v.CONF_RATE_NET_STAY) FROM v8_stat_ccm_rooms_det_grp v WHERE v.ybom_id = v2.ybom_id), 0), 0) + 
  DECODE (v2.flag_yblh, 1, NVL ( (SELECT SUM (v.CONF_RATE_NET_STAY) FROM v8_stat_ccm_rooms_det_blk v WHERE v.ybom_id = v2.ybom_id), 0), 0) ROOM_NET_REV
from V8_CCM_FLAGS v2;



-- Changed FB_REV; FB_REV , FB_VAT looking to v8_stat_ccm_res_forrev_fb
CREATE OR REPLACE FORCE VIEW V8_STAT_CCM_RES_FORREV_TOTALS
AS
   SELECT ybom.ybom_id,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_fs v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FS_REV,
          NVL (
             (SELECT SUM (v.ORGREV)
                FROM v8_stat_ccm_res_forrev_fs v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FS_ORGREV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_fs v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FS_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_fs v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FS_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_ci v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             ITEMS_REV,
          NVL (
             (SELECT SUM (v.ORGREV)
                FROM v8_stat_ccm_res_forrev_ci v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             ITEMS_ORGREV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_ci v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             ITEMS_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_ci v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             ITEMS_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_sm v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             BOOKED_MENU_REV,
          NVL (
             (SELECT SUM (v.ORGREV)
                FROM v8_stat_ccm_res_forrev_sm v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             BOOKED_MENU_ORGREV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_sm v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             BOOKED_MENU_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_sm v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             BOOKED_MENU_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_mi v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             MI_REV,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_mi v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             MI_ORGREV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_mi v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             MI_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_mi v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             MI_VAT,
          NVL (
             ROUND (
                (SELECT SUM (v.GROSSREV)
                   FROM v8_stat_ccm_res_forrev_pk v
                  WHERE     v.YBOM_ID = ybom.YBOM_ID
                        AND NVL (v.ALTERNATIVE, 0) = 0),
                2),
             0)
             PACKAGE_REV,
          NVL (
             ROUND (
                (SELECT SUM (v.ORGREV)
                   FROM v8_stat_ccm_res_forrev_pk v
                  WHERE     v.YBOM_ID = ybom.YBOM_ID
                        AND NVL (v.ALTERNATIVE, 0) = 0),
                2),
             0)
             PACKAGE_ORGREV,
          NVL (
             ROUND (
                (SELECT SUM (v.NET)
                   FROM v8_stat_ccm_res_forrev_pk v
                  WHERE     v.YBOM_ID = ybom.YBOM_ID
                        AND NVL (v.ALTERNATIVE, 0) = 0),
                2),
             0)
             PACKAGE_NET_REV,
          NVL (
             ROUND (
                (SELECT SUM (v.VAT)
                   FROM v8_stat_ccm_res_forrev_pk v
                  WHERE     v.YBOM_ID = ybom.YBOM_ID
                        AND NVL (v.ALTERNATIVE, 0) = 0),
                2),
             0)
             PACKAGE_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_fb v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FB_REV,
          NVL (
             (SELECT SUM (v.ORGREV)
                FROM v8_stat_ccm_res_forrev_fb v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FB_ORGREV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_fb v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FB_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_fb v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FB_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_tot v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             TOT_REV,
          NVL (
             (SELECT SUM (v.ORGREV)
                FROM v8_stat_ccm_res_forrev_tot v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             TOT_ORGREV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_tot v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             TOT_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_tot v
               WHERE v.YBOM_ID = ybom.YBOM_ID AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             TOT_VAT,
          v.ROOM_REV,         
          v.ROOM_NET_REV,
          v.ROOM_REV - v.ROOM_NET_REV ROOM_VAT,
         ybom2.zcur_shortdesc ZCUR_SHORTDESC
     FROM ybom,
          v8_stat_ccm_rooms_totals v,
          (SELECT ybom_id,
                  (SELECT zcur_shortdesc
                     FROM zcur
                    WHERE zcur_id = ybom_base_zcur_id)
                     zcur_shortdesc
             FROM ybom) ybom2
    WHERE ybom.ybom_id = ybom2.ybom_id and ybom.ybom_id=v.ybom_id;


CREATE OR REPLACE FORCE VIEW V8_STAT_CCM_RES_FORREV_TOT_DAY
AS
   SELECT ybom.ybom_id,
          wdat.wdat_date wdat_date,
          TO_NUMBER (TO_CHAR (TRUNC (wdat.wdat_date), 'J')) DATE_NUMBER,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_fs v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FS_REV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_fs v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FS_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_fs v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FS_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_ci v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             ITEMS_REV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_ci v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             ITEMS_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_ci v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             ITEMS_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_sm v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             BOOKED_MENU_REV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_sm v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             BOOKED_MENU_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_sm v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             BOOKED_MENU_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_mi v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             MI_REV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_mi v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             MI_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_mi v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             MI_VAT,
          NVL (
             ROUND (
                (SELECT SUM (v.GROSSREV)
                   FROM v8_stat_ccm_res_forrev_pk v
                  WHERE     v.YBOM_ID = ybom.YBOM_ID
                        AND V.WDAT_DATE = wdat.wdat_date
                        AND NVL (v.ALTERNATIVE, 0) = 0),
                2),
             0)
             PACKAGE_REV,
          NVL (
             ROUND (
                (SELECT SUM (v.NET)
                   FROM v8_stat_ccm_res_forrev_pk v
                  WHERE     v.YBOM_ID = ybom.YBOM_ID
                        AND V.WDAT_DATE = wdat.wdat_date
                        AND NVL (v.ALTERNATIVE, 0) = 0),
                2),
             0)
             PACKAGE_NET_REV,
          NVL (
             ROUND (
                (SELECT SUM (v.VAT)
                   FROM v8_stat_ccm_res_forrev_pk v
                  WHERE     v.YBOM_ID = ybom.YBOM_ID
                        AND V.WDAT_DATE = wdat.wdat_date
                        AND NVL (v.ALTERNATIVE, 0) = 0),
                2),
             0)
             PACKAGE_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_fb v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FB_REV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_fb v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FB_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_fb v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             FB_VAT,
          NVL (
             (SELECT SUM (v.GROSSREV)
                FROM v8_stat_ccm_res_forrev_tot v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             TOT_REV,
          NVL (
             (SELECT SUM (v.NET)
                FROM v8_stat_ccm_res_forrev_tot v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             TOT_NET_REV,
          NVL (
             (SELECT SUM (v.VAT)
                FROM v8_stat_ccm_res_forrev_tot v
               WHERE     v.YBOM_ID = ybom.YBOM_ID
                     AND V.WDAT_DATE = wdat.wdat_date
                     AND NVL (v.ALTERNATIVE, 0) = 0),
             0)
             TOT_VAT,      
          v.ROOM_REV,         
          v.ROOM_NET_REV,
          v.ROOM_REV - v.ROOM_NET_REV ROOM_VAT,
          ybom2.zcur_shortdesc ZCUR_SHORTDESC
     FROM ybom,
          wdat,
          v8_stat_ccm_rooms_tot_bydate v,
          (SELECT ybom_id,
                  (SELECT zcur_shortdesc
                     FROM zcur
                    WHERE zcur_id = ybom_base_zcur_id)
                     zcur_shortdesc
             FROM ybom) ybom2
    WHERE     ybom.ybom_id = ybom2.ybom_id and
              ybom.ybom_id=v.YBOM_ID AND wdat.wdat_date=v.wdat_date and 
              wdat.wdat_date BETWEEN YBOM.YBOM_ARRIVAL AND YBOM.YBOM_DEPARTURE;
