SELECT
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  0 IS_RES,
  1 IS_FA,
  ntd.ZPOS_ORIGINATED_ZFAC_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
    ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  (SELECT ZFAC.ZFAC_EXPVALIDFROM FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) ARRIVAL,
  (SELECT ZFAC.ZFAC_EXPVALIDUNTIL FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  ntd.ZPOS_POSCHECK_NR GUEST_CHECK_NUM,
  to_number(null) ZCHK_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,  ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSCHECK_NR,  ntd.ZPOS_POSTDATE,ntd.ZPOS_ORIGINATED_ZFAC_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID),DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
) WHERE  GUEST_CHECK_NUM is null
union all
SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  nvl2((to_char( DEPARTURE,'J')),'-',NULL) MINUSI,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZPOS_INTERNALGROUP_ID_S ZPOS_INTERNALGROUP_ID,
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  IS_RES,
  IS_FA,
  ORI_RES_FA_ID,
  ARRIVAL,
  DEPARTURE,
  ZFAC_OR_YRES,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S) ZDCO_NUMERICDESC,
  (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,
    (DECODE(IS_FA,0,(SELECT V8_REP_NAME.NAME6 FROM YRES,V8_REP_NAME WHERE YRES.YRES_ID=ORI_RES_FA_ID AND YRES.YRES_XCMS_ID=V8_REP_NAME.XCMS_ID),(SELECT V8_REP_NAME.NAME6 FROM ZFAC,V8_REP_NAME WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID AND ZFAC.ZFAC_XCMS_ID=V8_REP_NAME.XCMS_ID))) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  (DECODE(IS_FA,0,(NVL((SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID),(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE+1=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID))),(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ORI_RES_FA_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID))))) ORI_RES_FA_DESC,
  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('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(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZCHK_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=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_S)  and ZCHK_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_s),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_S))),
                                                       2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_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=ZCHK_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=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_S)  and ZCHK_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_s),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_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=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=ZCHK_OR_ZPOS_S))))
          ) DDESC,
  to_number(ZCHK_OR_ZPOS_S) ZCHK_OR_ZPOS, 
  ZARR_OR_ZPOS,
  INDICATE_ZPOS_ID, 
  (DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) GUEST_CHECK_NUM,
decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ZTXC, ZTCD where ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)), 'ZTXC_LONGDESC', (SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end),(case when (select count(distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' '  else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=zpos_postdate_s and Z3.ZPOS_ZWIN_ID=Zwin_id_s and Z4.ZPOS_ZTCD_ID is not null and rownum=1)), 'ZTXC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end)) TAXCODE_DESC, 
decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3, ZPOS Z5 where Z5.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S )>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS,
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  1 IS_RES,
  0 IS_FA,
  ntd.ZPOS_ORIGINATED_YRES_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  (SELECT TRUNC(YRES.YRES_EXPARRTIME) FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) ARRIVAL,
(SELECT trunc(YRES.YRES_EXPDEPTIME) FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) DEPARTURE,
    DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) INDICATE_ZPOS_ID,
  to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)) ZCHK_OR_ZPOS_S,
to_number(null) ZARR_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID(+)=NTD.ZPOS_ID and
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)=0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,  ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_ORIGINATED_YRES_ID, to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0),DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) --,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
UNION ALL
SELECT
 MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  0 IS_RES,
  1 IS_FA,
  ntd.ZPOS_ORIGINATED_ZFAC_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  NULL ARRIVAL,
  NULL DEPARTURE,
  DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) INDICATE_ZPOS_ID,
  to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)) ZCHK_OR_ZPOS_S,
to_number(null) ZARR_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,   ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_ORIGINATED_ZFAC_ID, to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0),DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) --,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
)
where 
(DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) is not null
)order by CDT ASC, DAT;

CREATE OR REPLACE VIEW V8_ED_FOL_ORI_GCHK_ARR_PS
AS 
(SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  nvl2((to_char( DEPARTURE,'J')),'-',NULL) MINUSI,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZPOS_INTERNALGROUP_ID_S ZPOS_INTERNALGROUP_ID,
   GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  IS_RES,
  IS_FA,
  ORI_RES_FA_ID,
  ARRIVAL,
  DEPARTURE,
  ZFAC_OR_YRES,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S) ZDCO_NUMERICDESC,
  (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,
    (DECODE(IS_FA,0,(SELECT V8_REP_NAME.NAME6 FROM YRES,V8_REP_NAME WHERE YRES.YRES_ID=ORI_RES_FA_ID AND YRES.YRES_XCMS_ID=V8_REP_NAME.XCMS_ID),(SELECT V8_REP_NAME.NAME6 FROM ZFAC,V8_REP_NAME WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID AND ZFAC.ZFAC_XCMS_ID=V8_REP_NAME.XCMS_ID))) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  (DECODE(IS_FA,0,(NVL((SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID),(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE+1=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID))),(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ORI_RES_FA_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID))))) ORI_RES_FA_DESC,
  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(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         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_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                         wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.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 zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where 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_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.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 zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where 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_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_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,
  ZCHK_OR_ZPOS,
  ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
  INDICATE_ZPOS_ID,
  GUEST_CHECK_NUM, 
 -- decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC,
 decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ZTXC, ZTCD where ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)), 'ZTXC_LONGDESC', (SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end),(case when (select count(distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' '  else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=zpos_postdate_s and Z3.ZPOS_ZWIN_ID=Zwin_id_s and Z4.ZPOS_ZTCD_ID is not null and rownum=1)), 'ZTXC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end)) TAXCODE_DESC, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  1 IS_RES,
  0 IS_FA,
  ntd.ZPOS_ORIGINATED_YRES_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  (SELECT YRES.YRES_EXPARRTIME FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) ARRIVAL,
  (SELECT YRES.YRES_EXPDEPTIME FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
   ntd.ZPOS_POSCHECK_NR GUEST_CHECK_NUM,
  to_number(null) ZCHK_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID(+)=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)=0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSCHECK_NR,  ntd.ZPOS_ORIGINATED_YRES_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
UNION ALL
SELECT
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  0 IS_RES,
  1 IS_FA,
  ntd.ZPOS_ORIGINATED_ZFAC_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  (SELECT ZFAC.ZFAC_EXPVALIDFROM FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) ARRIVAL,
  (SELECT ZFAC.ZFAC_EXPVALIDUNTIL FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  ntd.ZPOS_POSCHECK_NR GUEST_CHECK_NUM,
  to_number(null) ZCHK_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSCHECK_NR,  ntd.ZPOS_ORIGINATED_ZFAC_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID),DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
) WHERE  GUEST_CHECK_NUM is null
union all
SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  nvl2((to_char( DEPARTURE,'J')),'-',NULL) MINUSI,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZPOS_INTERNALGROUP_ID_S ZPOS_INTERNALGROUP_ID,
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  IS_RES,
  IS_FA,
  ORI_RES_FA_ID,
  ARRIVAL,
  DEPARTURE,
  ZFAC_OR_YRES,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S) ZDCO_NUMERICDESC,
  (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,
    (DECODE(IS_FA,0,(SELECT V8_REP_NAME.NAME6 FROM YRES,V8_REP_NAME WHERE YRES.YRES_ID=ORI_RES_FA_ID AND YRES.YRES_XCMS_ID=V8_REP_NAME.XCMS_ID),(SELECT V8_REP_NAME.NAME6 FROM ZFAC,V8_REP_NAME WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID AND ZFAC.ZFAC_XCMS_ID=V8_REP_NAME.XCMS_ID))) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  (DECODE(IS_FA,0,(NVL((SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID),(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE+1=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID))),(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ORI_RES_FA_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID))))) ORI_RES_FA_DESC,
  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('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(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZCHK_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=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_S)  and ZCHK_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_s),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_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=ZCHK_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=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_S)  and ZCHK_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_s),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_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=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=ZCHK_OR_ZPOS_S))))
          ) DDESC,
  to_number(ZCHK_OR_ZPOS_S) ZCHK_OR_ZPOS, 
  ZARR_OR_ZPOS,
  INDICATE_ZPOS_ID, 
  (DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) GUEST_CHECK_NUM,
   --decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC,
   decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ZTXC, ZTCD where ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)), 'ZTXC_LONGDESC', (SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end),(case when (select count(distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' '  else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=zpos_postdate_s and Z3.ZPOS_ZWIN_ID=Zwin_id_s and Z4.ZPOS_ZTCD_ID is not null and rownum=1)), 'ZTXC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end)) TAXCODE_DESC, 
   decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3, ZPOS Z5 where Z5.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S )>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS,
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  1 IS_RES,
  0 IS_FA,
  ntd.ZPOS_ORIGINATED_YRES_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  (SELECT TRUNC(YRES.YRES_EXPARRTIME) FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) ARRIVAL,
(SELECT trunc(YRES.YRES_EXPDEPTIME) FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) DEPARTURE,
    DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) INDICATE_ZPOS_ID,
  to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)) ZCHK_OR_ZPOS_S,
to_number(null) ZARR_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID(+)=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)=0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_ORIGINATED_YRES_ID, to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0),DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) --,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
UNION ALL
SELECT
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  0 IS_RES,
  1 IS_FA,
  ntd.ZPOS_ORIGINATED_ZFAC_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  NULL ARRIVAL,
  NULL DEPARTURE,
  DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) INDICATE_ZPOS_ID,
  to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)) ZCHK_OR_ZPOS_S,
to_number(null) ZARR_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_ORIGINATED_ZFAC_ID, to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0),DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) --,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
)
where 
(DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) is not null
)
order by CDT ASC, DAT;

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('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(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         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_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                        wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.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 zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where 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_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.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 zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where 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_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_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
--ZWIN.ZWIN_ID=:zwin_id and
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
--ZWIN.ZWIN_ID=:zwin_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.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

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('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(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         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_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                        wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.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 zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where 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_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.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 zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where 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_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_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
--zwin_id=:zwin_id and
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
--zwin_id=:zwin_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.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_DCO_PS
AS 
SELECT
  MIN_ZPOS_POSTDATE_S MIN_DAT,
  MAX_ZPOS_POSTDATE_S MAX_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,
  TAXLINK_ID_S TAXLINK_ID,
  COUNTER_S QTY,
  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,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO WHERE ZDCO.ZDCO_ID=ZDCO_ID_S) ZDCO_NUMERICDESC,
  Wtrl_Translate('ZDCO',ZDCO_ID_S,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) ZDCO_LONGDESC,
  Wtrl_Translate('ZDCO',ZDCO_ID_S,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) ZDCO_FOLIOTEXT,
(decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZPOS_ID_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=ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S)  and ZPOS_ID_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZPOS_ID_S)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZPOS_ID_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZPOS_ID_S))),
                                                        2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZPOS_ID_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZPOS_ID_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=ZPOS_ID_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=ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=zpos_id_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S)  and ZPOS_ID_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZPOS_ID_S)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZPOS_ID_S),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZPOS_ID_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=ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZPOS_ID_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_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=ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZPOS_ID_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_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=ZPOS_ID_S))))
                                   ) DDESC, 
  ZWIN_ID_S ZWIN_ID,
  (CASE WHEN (SELECT max(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND (max_zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime OR zdtl.zdtl_fromtime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s OR zdtl.zdtl_totime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s)) = (SELECT min(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND (max_zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime OR zdtl.zdtl_fromtime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s OR zdtl.zdtl_totime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s)) THEN (SELECT max(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND (max_zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime OR zdtl.zdtl_fromtime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s OR zdtl.zdtl_totime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s)) ELSE 0 END) taxcode, 
  (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ) MULTI_TAXCODE_EXISTS
FROM
(
SELECT
--  distinct
  MIN(ntd.ZPOS_POSTDATE) MIN_ZPOS_POSTDATE_S,
  MAX(ntd.ZPOS_POSTDATE) MAX_ZPOS_POSTDATE_S,  
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(ZDCO_PAYM_TYPE) ZDCO_PAYM_TYPE_S,
  COUNT(zdco_id) COUNTER_S,
  MIN(ntd.ZPOS_TAXLINK_ID) TAXLINK_ID_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  zdco_id ZDCO_ID_S
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
--ZWIN.ZWIN_ID=:ZWIN_ID and
ZORG.ZORG_ZPOS_ID(+)=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
GROUP BY ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID, zdco_id 
) 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 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)),
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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=:ZWIN_ID
;

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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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=:ZWIN_ID
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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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=:ZWIN_ID 
  GROUP BY ZPOS.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;
  
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(nvl(ZORG.ZORG_ROLE,0),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)),
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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
--zwin_id=:zwin_id and
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
--zwin_id=:zwin_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.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

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(nvl(ZORG.ZORG_ROLE,0),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)),
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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(nvl(ZORG.ZORG_ROLE,0),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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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 zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_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 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
--zwin_id=:zwin_id and
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
--zwin_id=:zwin_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.ZPOS_CDT,ZWIN.ZWIN_ID,ZDCO.ZDCO_NUMERICDESC,zpos_zcur_id;

  
  DROP VIEW V8_ED_FOLIO_TAX_CURR;

--
-- V8_ED_FOLIO_TAX_CURR  (View) 
--
CREATE OR REPLACE VIEW V8_ED_FOLIO_TAX_CURR
(ZWIN_ID, ZTCD_ID, ZDCO_ID, ZTCD_ZTXC_ID, ZTCD_NAME, 
 ZTXC_SHORTDESC, LOCAL_GROSS, FOREIGN_GROSS, LOCAL_NET, FOREIGN_NET, 
 LOCAL_TAX, FOREIGN_TAX, TAX_CODE, RES_FOREIGN_ZCUR_ID, LOCAL_ZCUR_ID)
AS 
SELECT
/* 
View to get VAT 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. 
*/  
 S_ZWIN_ID ZWIN_ID,
 S_ZTCD_ID ZTCD_ID,
 (SELECT ZTCD.ZTCD_POSTTO_ZDCO_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=S_ZTCD_ID) ZDCO_ID,
 (SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=S_ZTCD_ID) ZTCD_ZTXC_ID,
 Wtrl_Translate('ZTXC',(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=S_ZTCD_ID),'ZTXC_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=S_ZWIN_ID)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=S_ZWIN_ID)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=S_ZWIN_ID)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=S_ZWIN_ID)))) ZTCD_NAME,
 (SELECT ZTXC.ZTXC_SHORTDESC FROM ZTXC WHERE ZTXC.ZTXC_ID=(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=S_ZTCD_ID)) ZTXC_SHORTDESC,
 GROSS_AMOUNT_L             LOCAL_GROSS,
 GROSS_AMOUNT_F             FOREIGN_GROSS,
 NET_AMOUNT_L               LOCAL_NET,
 ROUND(NET_AMOUNT_F,2)      FOREIGN_NET,
 TAX_AMOUNT_L               LOCAL_TAX,
 ROUND(TAX_AMOUNT_F,2)      FOREIGN_TAX,
 (SELECT ZTXC.ZTXC_DISPLORDER FROM ZTXC WHERE ZTXC.ZTXC_ID = (SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID = S_ZTCD_ID)) TAX_CODE,
 S_RES_FOREIGN_ZCUR_ID RES_FOREIGN_ZCUR_ID,
 (select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) LOCAL_ZCUR_ID
FROM
(
SELECT
 S_ZWIN_ID S_ZWIN_ID,
 S_ZTCD_ID S_ZTCD_ID,
 SUM(S_GROSS_L) GROSS_AMOUNT_L,
 SUM(S_GROSS_F) GROSS_AMOUNT_F,
 SUM(S_NET_L) NET_AMOUNT_L,
 SUM(S_NET_F) NET_AMOUNT_F,
 SUM(S_GROSS_L-S_NET_L) TAX_AMOUNT_L,
 SUM(S_GROSS_F-S_NET_F) TAX_AMOUNT_F,
 MAX(S_RES_FOREIGN_ZCUR_ID) S_RES_FOREIGN_ZCUR_ID
FROM
(
SELECT
  DISTINCT
  z.ZPOS_ZWIN_ID S_ZWIN_ID,
  z.ZPOS_ZTCD_ID S_ZTCD_ID,
  z.ZPOS_TAXLINK_ID,
  NVL((SELECT SUM(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS_L,
  NVL((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 (ZPOS.ZPOS_GROSSFOREIGNPRICE*ZPOS.ZPOS_QUANTITY) ELSE /*here comes the exchange calc */ (DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),(ZPOS.ZPOS_GROSSUNITPRICE*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))) END) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS_F,
  NVL((SELECT SUM(ZPOS.zpos_unitprice*ZPOS.zpos_quantity) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET_L,
  NVL((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 (ZPOS.zpos_foreignprice*ZPOS.zpos_quantity) ELSE /*here comes the exchange calc */ (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))) END) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET_F,
  (CASE WHEN NVL(z.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_id FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=z.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=z.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.*/ S_RES_FOREIGN_ZCUR_ID
FROM
  ZPOS z
WHERE
  NVL(z.ZPOS_ZTCD_ID,0)<>0 AND NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)<>1
  AND NOT EXISTS (SELECT 1 FROM ZALL WHERE zall.ZALL_ID=z.ZPOS_ZALL_ID AND NVL(zall.ZALL_INTERNALSPLIT_ZPOS_ID,0)=0)
  AND NOT EXISTS (SELECT 1 FROM ZPOS WHERE zpos.ZPOS_ID=z.ZPOS_INTERNALSPLIT_ZPOS_ID AND NVL(zpos.ZPOS_HIDDEN,0)=3)
--  and ZPOS_ZWIN_ID=5344
)
GROUP BY S_ZWIN_ID,S_ZTCD_ID
)
UNION ALL
SELECT
 S_ZWIN_ID ZWIN_ID,
 TO_NUMBER(-1) ZTCD_ID,
 TO_NUMBER(-1) ZDCO_ID,
 TO_NUMBER(-1) ZTCD_ZTXC_ID,
 'Paid Out'   ZTCD_NAME,
 'P/O'   ZTXC_SHORTDESC,
 GROSS_AMOUNT_L             LOCAL_GROSS,
 GROSS_AMOUNT_F             FOREIGN_GROSS,
 NET_AMOUNT_L               LOCAL_NET,
 ROUND(NET_AMOUNT_F,2)      FOREIGN_NET,
 TAX_AMOUNT_L               LOCAL_TAX,
 ROUND(TAX_AMOUNT_F,2)      FOREIGN_TAX,
 0 TAX_CODE,
 S_RES_FOREIGN_ZCUR_ID RES_FOREIGN_ZCUR_ID,
 (select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) LOCAL_ZCUR_ID
FROM
(
SELECT
 S_ZWIN_ID S_ZWIN_ID,
 SUM(S_GROSS_L) GROSS_AMOUNT_L,
 SUM(S_GROSS_F) GROSS_AMOUNT_F,
 SUM(S_NET_L) NET_AMOUNT_L,
 SUM(S_NET_F) NET_AMOUNT_F,
 SUM(S_GROSS_L-S_NET_L) TAX_AMOUNT_L,
 SUM(S_GROSS_F-S_NET_F) TAX_AMOUNT_F,
 MAX(S_RES_FOREIGN_ZCUR_ID) S_RES_FOREIGN_ZCUR_ID
FROM
(
SELECT
  z.ZPOS_ZWIN_ID S_ZWIN_ID,
  NVL(-(SELECT SUM(ZPOS.zpos_sumupamount) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS_L,
  NVL(-(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 (ZPOS.zpos_foreignsumupamount) ELSE /*here comes the exchange calc */ (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.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS_F,
  NVL(-(SELECT SUM(ZPOS.zpos_sumupamount) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET_L,
  NVL(-(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 (ZPOS.zpos_foreignsumupamount) ELSE /*here comes the exchange calc */ (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.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET_F,
  (CASE WHEN NVL(z.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_id FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=z.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=z.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.*/ S_RES_FOREIGN_ZCUR_ID
  FROM
  ZPOS z
WHERE
  z.ZPOS_ZDCO_ID IN (SELECT ZDCO.ZDCO_ID FROM ZDCO WHERE ZDCO.ZDCO_PAIDOUT=1) AND NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)<>1
  AND NOT EXISTS (SELECT 1 FROM ZALL WHERE zall.ZALL_ID=z.ZPOS_ZALL_ID AND NVL(zall.ZALL_INTERNALSPLIT_ZPOS_ID,0)=0)
  AND NOT EXISTS (SELECT 1 FROM ZPOS WHERE zpos.ZPOS_ID=z.ZPOS_INTERNALSPLIT_ZPOS_ID AND NVL(zpos.ZPOS_HIDDEN,0)=3)
--  and ZPOS_ZWIN_ID=5344
)
GROUP BY S_ZWIN_ID
)
UNION ALL
SELECT
 S_ZWIN_ID ZWIN_ID,
 TO_NUMBER(-2) ZTCD_ID,
 TO_NUMBER(-2) ZDCO_ID,
 TO_NUMBER(-2) ZTCD_ZTXC_ID,
 'No Tax Linked'   ZTCD_NAME,
 'No Link'   ZTXC_SHORDESC,
 SUM(GROSS_AMOUNT_L)             LOCAL_GROSS,
 SUM(GROSS_AMOUNT_F)             FOREIGN_GROSS,
 SUM(NET_AMOUNT_L)               LOCAL_NET,
 SUM(ROUND(NET_AMOUNT_F,2))      FOREIGN_NET,
 SUM(TAX_AMOUNT_L)               LOCAL_TAX,
 SUM(ROUND(TAX_AMOUNT_F,2))      FOREIGN_TAX,
 0 TAX_CODE,
 MAX(S_RES_FOREIGN_ZCUR_ID) RES_FOREIGN_ZCUR_ID,
 (select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency')) LOCAL_ZCUR_ID
FROM
(
SELECT
 S_ZWIN_ID S_ZWIN_ID,
 SUM(S_GROSS_L) GROSS_AMOUNT_L,
 SUM(S_GROSS_F) GROSS_AMOUNT_F,
 SUM(S_NET_L) NET_AMOUNT_L,
 SUM(S_NET_F) NET_AMOUNT_F,
 SUM(S_GROSS_L-S_NET_L) TAX_AMOUNT_L,
 SUM(S_GROSS_F-S_NET_F) TAX_AMOUNT_F,
 MAX(S_RES_FOREIGN_ZCUR_ID) S_RES_FOREIGN_ZCUR_ID
FROM
(
SELECT
  z.ZPOS_ZWIN_ID S_ZWIN_ID,
  NVL((SELECT SUM(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS_L,
  NVL((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 (ZPOS.ZPOS_GROSSFOREIGNPRICE*ZPOS.ZPOS_QUANTITY) ELSE /*here comes the exchange calc */ (DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),(ZPOS.ZPOS_GROSSUNITPRICE*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))) END) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS_F,
  NVL((SELECT SUM(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET_L,
  NVL((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 (ZPOS.ZPOS_GROSSFOREIGNPRICE*ZPOS.ZPOS_QUANTITY) ELSE /*here comes the exchange calc */ (DECODE(ZPOS_ZDCO_ID,NVL((select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('PostForeignCurrencyRateDiffonCODept')),0),to_char(null),(ZPOS.ZPOS_GROSSUNITPRICE*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))) END) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET_F,
  (CASE WHEN NVL(z.ZPOS_YRES_ID,0)<>0 THEN (SELECT zcur_id FROM ZCUR,YRES WHERE zcur_id = YRES.YRES_CONTRACT_ZCUR_ID AND YRES.YRES_ID=z.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=z.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.*/ S_RES_FOREIGN_ZCUR_ID
  FROM
  ZPOS z
WHERE
  NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)<>1 AND z.ZPOS_CDT=1
  AND NOT EXISTS (SELECT 1 FROM ZDTL WHERE ZDTL.ZDTL_ZDCO_ID=z.ZPOS_ZDCO_ID AND z.ZPOS_POSTDATE BETWEEN ZDTL.ZDTL_FROMTIME AND ZDTL.ZDTL_TOTIME)
  AND NOT EXISTS (SELECT 1 FROM ZALL WHERE zall.ZALL_ID=z.ZPOS_ZALL_ID AND NVL(zall.ZALL_INTERNALSPLIT_ZPOS_ID,0)=0)
  AND NOT EXISTS (SELECT 1 FROM ZPOS WHERE zpos.ZPOS_ID=z.ZPOS_INTERNALSPLIT_ZPOS_ID AND NVL(zpos.ZPOS_HIDDEN,0)=3)
--  and ZPOS_ZWIN_ID=5344
)
GROUP BY S_ZWIN_ID
) GROUP BY S_ZWIN_ID ORDER BY TAX_CODE;


CREATE OR REPLACE VIEW V8_ED_FOLIO_ZXCH_INFO
AS 
SELECT 
/* 
View to get Foreign Currency Exchange Rates to be used in e.g. Foreign Currency Folio Styles.
*/  
ZXCH.ZXCH_ZCUR_ID ZCUR_ID,
(SELECT ZCUR.ZCUR_SHORTDESC FROM ZCUR WHERE ZCUR.ZCUR_ID=ZXCH.ZXCH_ZCUR_ID) CURRENCY,
DECODE(NVL(ZXCH.ZXCH_PRIMARY,0),0,ZXCH.ZXCH_RATE,1/ZXCH.ZXCH_RATE) RATE,
DECODE(NVL(ZXCH.ZXCH_PRIMARY,0),0,ZXCH.ZXCH_CASH_RATE,1/ZXCH.ZXCH_CASH_RATE) CASH_RATE,
DECODE(NVL(ZXCH.ZXCH_PRIMARY,0),0,ZXCH.ZXCH_RATECODE_RATE,1/ZXCH.ZXCH_RATECODE_RATE) RATECODE_RATE,
NVL(ZXCH.ZXCH_PRIMARY,0) IS_BACKRATE
FROM ZXCH 
WHERE ZXCH_CURRENT=1
UNION ALL
SELECT
ZCUR.ZCUR_ID ZCUR_ID,
ZCUR.ZCUR_SHORTDESC CURRENCY,
1 RATE,
1 CASH_RATE,
1 RATECODE_RATE,
0 IS_BACKRATE
FROM ZCUR
where ZCUR.ZCUR_ID=(select wuss_value from wuss where upper(WUSS.WUSS_NAME) like upper('basecurrency'))
ORDER BY ZCUR_ID ASC;

CREATE OR REPLACE VIEW V8_IFC_PROFILESEARCH AS
SELECT
  DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1)) GUEST_FORMATTEDNAME,
  XCMS.XCMS_NAME1 GUEST_NAME,
  XCMS.XCMS_NAME3 GUEST_FIRSTNAME,
  (SELECT XPDA.XPDA_SHORTDESC FROM XPDA,XPAL WHERE XPDA.XPDA_ID=XPAL.XPAL_XPDA_ID AND XPAL.XPAL_XPAC_ID=11 AND XPAL.XPAL_XCMS_ID=XCMS.XCMS_ID and rownum = 1) GUEST_VIP,
  (SELECT WLAN_SHORTDESC FROM WLAN WHERE XCMS.XCMS_WLAN_ID=WLAN.WLAN_ID) GUEST_LANGUAGE,
  (SELECT XCID_ADDRGREET FROM XCID WHERE XCID.XCID_ID=XCMS.XCMS_ID) GUEST_TITLE,      
  XCMS.XCMS_ID GUEST_XCMS_ID,
  XADR_STREET1 GUEST_ADDRESS1,
  XADR_STREET2 GUEST_ADDRESS2,
  XADR_ZIP GUEST_ZIP,
  XADR_CITY GUEST_CITY,
  XSTA_LONGDESC GUEST_STATE,
  XCOU_LONGDESC GUEST_COUNTRY,
  XCOU_ISO2 GUEST_COUNTRY_ISO,
  (SELECT XCOU.XCOU_ISO2 from XCOU WHERE XCOU.XCOU_ID=XCMS.XCMS_XCOU_ID_NATION) GUEST_NATIONALITY,
  (SELECT XCOM_VALUE FROM XCOM, XCMT WHERE  XCMT.XCMT_ID = XCOM.XCOM_XCMT_ID AND XCOM.XCOM_PRIMARY(+)=1 AND XCMT.XCMT_TYPE(+)=4 AND  XCOM.XCOM_XCMS_ID=XCMS.XCMS_ID AND ROWNUM=1 ) GUEST_PHONE,
  (SELECT XCOM_VALUE FROM XCOM, XCMT WHERE XCMT.XCMT_ID = XCOM.XCOM_XCMT_ID AND   XCOM.XCOM_PRIMARY(+)=1 AND   XCMT.XCMT_TYPE(+)=1 AND  XCOM.XCOM_XCMS_ID=XCMS.XCMS_ID AND ROWNUM=1 ) GUEST_EMAIL,
  (SELECT XCOM_VALUE FROM XCOM, XCMT WHERE XCMT.XCMT_ID = XCOM.XCOM_XCMT_ID AND   XCOM.XCOM_PRIMARY(+)=1 AND   XCMT.XCMT_TYPE(+)=3 AND  XCOM.XCOM_XCMS_ID=XCMS.XCMS_ID AND ROWNUM=1 ) GUEST_FAX,
  (SELECT XCID.XCID_BIRTHTIME FROM XCID WHERE XCID.XCID_ID = XCMS.XCMS_ID) GUEST_BIRTHDAY,
  (SELECT XMNR.XMNR_VALUE FROM XMNR, XMTY WHERE  XMTY.XMTY_ROLE=6 AND XMTY.XMTY_ID=XMNR.XMNR_XMTY_ID AND XMNR.XMNR_XCMS_ID=XCMS.XCMS_ID AND ROWNUM=1) GUEST_MEMBER_ID_6,
  (SELECT DECODE(A.XCMS_TYPE,1,A.XCMS_NAME1||' '||A.XCMS_NAME2||' '||A.XCMS_NAME3,TRIM(DECODE(NVL(A.XCMS_NAME3,''),'','',A.XCMS_NAME3||' ')||DECODE(NVL(A.XCMS_NAME2,''),'','',A.XCMS_NAME2||' ')||A.XCMS_NAME1))  FROM XCMS A, XCIL, XCCA WHERE XCCA.XCCA_ID=A.XCMS_XCCA_ID AND XCIL.XCIL_PRIMARY(+)=1 AND ROWNUM = 1 AND XCCA.XCCA_INTERNALCATEGORY=1 AND XCIL.XCIL_COMPANY_XCMS_ID=A.XCMS_ID AND XCIL.XCIL_INDIVIDUAL_XCMS_ID=XCMS.XCMS_ID) GUEST_COMPANY,
  (SELECT DECODE(NVL(XCID.XCID_SEX,0),0,' ',1,'M',2,'F',3,' ') FROM XCID WHERE XCID.XCID_ID=XCMS.XCMS_ID) GUEST_GENDER,
  (SELECT XPDC.XPDC_VALUE FROM XPDC, XPDG WHERE XPDC.XPDC_XCMS_ID=XCMS.XCMS_ID AND XPDC.XPDC_XPDG_ID=XPDG.XPDG_ID AND XPDG.XPDG_ROLE=1 AND XPDC.XPDC_PRIMARY=1 AND ROWNUM=1) GUEST_PASSPORT
FROM XADR,XSTA,XCOU, XCMS
  WHERE XADR_PRIMARY(+)=1 AND
  XCMS.XCMS_ID=XADR.XADR_XCMS_ID(+) AND
  XADR_XSTA_ID=XSTA_ID(+) AND
  XADR_XCOU_ID=XCOU_ID(+);

-- Best Western specfic view

create or replace view V8_REP_BW_XMNR_INFOS
as
select xmnr.*, xmty.*
from xmnr, xmty
where xmnr_xmty_id=xmty_id and xmnr.xmnr_xmty_role=11;

CREATE OR REPLACE VIEW V8_EDITOR_ADR
AS 
SELECT 
       XCMS_ID GUEST_ID,
       XATY_LONGDESC ADDRESSTYPE_DESC,
       XADR_ID ADDRESS_ID, 
       XADR.XADR_STREET1 STREET1,
       XADR.XADR_STREET2 STREET2,
       XADR.XADR_STREET3 STREET3,
       XADR.XADR_ZIP     ZIP,       
       XADR.XADR_CITY    CITY,
	   XADR.XADR_PRIMARY,
       XCOU.XCOU_CUSTSHORTDESC COUNTRY,
       XCOU.XCOU_ISO2           COUNTRY_ISO2,
       XCOU.XCOU_ISO3           COUNTRY_ISO3,
       XCOU.XCOU_LONGDESC       COUNTRY_NAME,              
DECODE(XCOU_ADDR_TYPE,
             1,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||', ')||DECODE(XSTA_LONGDESC,NULL,'',XSTA_LONGDESC||' ')||XADR_ZIP),
             2,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XSTA_LONGDESC,NULL,DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')||DECODE(XADR_CITY,NULL,'',XADR_CITY||' '),XSTA_LONGDESC||CHR(10)||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' '||DECODE(XADR_CITY,NULL,'',XADR_CITY||' ')))),
             3,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||CHR(10)
              ||DECODE(XCOU_CUSTSHORTDESC,NULL,'',XCOU_CUSTSHORTDESC||' ')
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY),
             4,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||' ')||XADR_ZIP),
             5,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||', ')||DECODE(XSTA_LONGDESC,NULL,'',XSTA_LONGDESC||' ')||XADR_ZIP)
              ||CHR(10)
              ||UPPER(XCOU_LONGDESC) ,
             6,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY),
             7,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||' ')||XADR_ZIP)
              ||CHR(10)
              ||UPPER(XCOU_LONGDESC),
             8,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XCOU_CUSTSHORTDESC,NULL,'',XCOU_CUSTSHORTDESC||'-')
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY),
             9,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XCOU_CUSTSHORTDESC,NULL,'',XCOU_CUSTSHORTDESC||' ')
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY),
             10,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY)
              ||CHR(10)
              ||XCOU_LONGDESC ,
             11,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||' ')||XADR_ZIP)
              ||CHR(10)
              ||CHR(10)
              ||XCOU_LONGDESC ,
             12,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||', ')||DECODE(XSTA_LONGDESC,NULL,'',XSTA_LONGDESC||' ')||XADR_ZIP)
              ||CHR(10)
              ||CHR(10)
              ||XCOU_LONGDESC ,
             13,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY)
              ||CHR(10)
              ||UPPER(XCOU_LONGDESC) ,
             14,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',UPPER(XADR_ZIP)||' ')||UPPER(XADR_CITY)),
             15,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY)
              ||DECODE(XADR_ZIP,NULL,'',CHR(10)||XADR_ZIP)
              ||DECODE(XSTA_LONGDESC,NULL,'',CHR(10)||XSTA_LONGDESC)),
             16,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||(XADR_ZIP||' '||XADR_CITY)),
             17,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(xadr_zip,NULL,'',xadr_zip||' ')||DECODE(xadr_city,NULL,'',xadr_city||' ')||xsta_shortdesc),
             18,(DECODE(xcms_type,1,xcms_name1||' '||xcms_name2||' '||xcms_name3,xcid_addrgreet||CHR(10)||trim(DECODE(NVL(xcms_name3,''),'','',xcms_name3||' ')||DECODE(NVL(xcms_name2,''),'','',xcms_name2||' ')||xcms_name1))
              ||CHR(13)||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
               ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
               ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
               ||CHR(10)
               ||(xadr_zip||' '||xadr_city)
               ||DECODE(xsta_longdesc,NULL,'',CHR(10)||xsta_longdesc)
              ||DECODE(xcou_longdesc,NULL,'',CHR(10)||UPPER(xcou_longdesc))),
             19,(DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',UPPER(XADR_ZIP)||' ')||UPPER(XADR_CITY))
              ||CHR(10)
              ||UPPER(XCOU_LONGDESC),
             --HERE IS THE DEFAULT
             DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))
              ||CHR(10)
              ||DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||(XADR_ZIP||' '||XADR_CITY)) NAME_ADDRESS,
--HERE COMES ONLY THE NAME
                          (DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,XCID_ADDRGREET||CHR(10)||TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1))) NAME ,
--HERE COMES ONLY THE ADDRESS
DECODE(XCOU_ADDR_TYPE,
             1,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||', ')||DECODE(XSTA_LONGDESC,NULL,'',XSTA_LONGDESC||' ')||XADR_ZIP),
             2,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XSTA_LONGDESC,NULL,DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')||DECODE(XADR_CITY,NULL,'',XADR_CITY||' '),XSTA_LONGDESC||CHR(10)||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' '||DECODE(XADR_CITY,NULL,'',XADR_CITY||' ')))),
             3,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||CHR(10)
              ||DECODE(XCOU_CUSTSHORTDESC,NULL,'',XCOU_CUSTSHORTDESC||' ')
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY),
                4,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||' ')||XADR_ZIP),
             5,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||', ')||DECODE(XSTA_LONGDESC,NULL,'',XSTA_LONGDESC||' ')||XADR_ZIP)
              ||CHR(10)
              ||UPPER(XCOU_LONGDESC) ,
             6,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY),
                7,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||' ')||XADR_ZIP)
              ||CHR(10)
              ||UPPER(XCOU_LONGDESC) ,
             8,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XCOU_CUSTSHORTDESC,NULL,'',XCOU_CUSTSHORTDESC||'-')
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY),
             9,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XCOU_CUSTSHORTDESC,NULL,'',XCOU_CUSTSHORTDESC||' ')
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY),
             10,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY)
              ||CHR(10)
              ||XCOU_LONGDESC,
                11,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||' ')||XADR_ZIP)
              ||CHR(10)
              ||CHR(10)
              ||XCOU_LONGDESC ,
             12,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY||', ')||DECODE(XSTA_LONGDESC,NULL,'',XSTA_LONGDESC||' ')||XADR_ZIP)
              ||CHR(10)
              ||CHR(10)
              ||XCOU_LONGDESC ,
             13,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',XADR_ZIP||' ')
              ||XADR_CITY)
              ||CHR(10)
              ||UPPER(XCOU_LONGDESC),
                14,(DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',UPPER(XADR_ZIP)||' ')||UPPER(XADR_CITY)),
             15,DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_CITY,NULL,'',XADR_CITY)
              ||DECODE(XADR_ZIP,NULL,'',CHR(10)||XADR_ZIP)
              ||DECODE(XSTA_LONGDESC,NULL,'',CHR(10)||XSTA_LONGDESC),
             16,DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||(XADR_ZIP||' '||XADR_CITY),
             17,DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(xadr_zip,NULL,'',xadr_zip||' ')||DECODE(xadr_city,NULL,'',xadr_city||' ')||xsta_shortdesc,
             18,DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
               ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
               ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
               ||CHR(10)
               ||(xadr_zip||' '||xadr_city)
               ||DECODE(xsta_longdesc,NULL,'',CHR(10)||xsta_longdesc)
              ||DECODE(xcou_longdesc,NULL,'',CHR(10)||UPPER(xcou_longdesc)),
             19,DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||DECODE(XADR_ZIP,NULL,'',UPPER(XADR_ZIP)||' ')||UPPER(XADR_CITY)
              ||CHR(10)
              ||UPPER(XCOU_LONGDESC),
               --HERE IS THE DEFAULT
             DECODE(XADR_STREET1,NULL,'',XADR_STREET1)
              ||DECODE(XADR_STREET2,NULL,'',CHR(10)||XADR_STREET2)
              ||DECODE(XADR_STREET3,NULL,'',CHR(10)||XADR_STREET3)
              ||CHR(10)
              ||(XADR_ZIP||' '||XADR_CITY)) ADDRESS
FROM XCOU,XSTA,XADR,XATY, XCMS, XCID
WHERE XCOU_ID(+)=XADR_XCOU_ID
AND XSTA_ID(+)=XADR_XSTA_ID
AND XATY_ID(+)=XADR_XATY_ID
AND XCMS.XCMS_ID=XADR.XADR_XCMS_ID
AND XCMS.XCMS_ID=XCID.XCID_ID(+);

CREATE OR REPLACE VIEW V8_EDT_CALEVENT_PARTICIPANTS
AS 
SELECT YCEP.YCEP_ID,
          YCEP.YCEP_YCEV_ID,
          YCEP.YCEP_XCMS_ID,
          YCEP.YCEP_NOOFADULTS,
          YCEP.YCEP_TICKETPRICE,
          YCEP.YCEP_AMOUNT,
          YCEP.YCEP_ZFAC_ID,
          YCEP.YCEP_VOID_ZFAC_ID,
          YCEP.YCEP_FULLYPAID,
          YCEP.YCEP_PRINTED,
          YCEP.YCEP_NOTES,
          YCEP.YCEP_CANCELLED,
          YCEV.YCEV_LONGDESC,
          YCEV.YCEV_SHORTWEBINFO,
          (SELECT XCMS.XCMS_NAME1
             FROM XCMS
            WHERE XCMS.XCMS_ID = YCEP.YCEP_XCMS_ID)
             XCMS_NAME1,
          (SELECT XCMS.XCMS_NAME2
             FROM XCMS
            WHERE XCMS.XCMS_ID = YCEP.YCEP_XCMS_ID)
             XCMS_NAME2,
          (SELECT XCMS.XCMS_NAME3
             FROM XCMS
            WHERE XCMS.XCMS_ID = YCEP.YCEP_XCMS_ID)
             XCMS_NAME3
     FROM YCEP, YCEV
    WHERE YCEP_YCEV_ID = YCEV_ID;



 






