SELECT
--  distinct
  ZPOS.ZPOS_POSTDATE DAT,
  ZPOS.ZPOS_CDT CDT,
  ZPOS.ZPOS_ID ZPOS_ID,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY))                                                                                                                                                                                                   QTY,
  NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)+NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)                                       GROSS,
  TO_NUMBER(0)                                                                                                  PAY,
  TO_CHAR(TO_NUMBER(0)*100,'999999990,09')                                                                   FPAY,
  NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)+NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)                                                                         REV,
  TO_CHAR((NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)+NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0))*100,'999999990,09')                                             FREV,
  ((NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)+NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0))/(DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY)))                                                                                                                UNIT,
  (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)                                              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.ZWIN_ID)     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.ZWIN_ID)     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.ZWIN_ID)                     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.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,
  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.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)=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.ZWIN_ID),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.ZWIN_ID),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.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) END)*100,'999999990,09') FSPAY_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_longdesc 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_longdesc 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_longdesc 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_longdesc 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,
  (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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))))) 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
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
--ZWIN.ZWIN_ID=:zwin_id and
ZORG.ZORG_ZPOS_ID(+)=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(ZDCO.ZDCO_SUBFUNCTION,0)=1 AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1;  

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_DCO_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,
  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 zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime) = (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 zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime) 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 zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime) 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
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_TAXLINK_ID) TAXLINK_ID_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(ZDCO_PAYM_TYPE) ZDCO_PAYM_TYPE_S,
  COUNT(zdco_id) COUNTER_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, ntd.ZPOS_POSTDATE, 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_XCMS_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,
    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,
  (SELECT v8_rep_name.NAME6 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ORI_XCMS_ID) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  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, 
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  ntd.ZPOS_ORIGINATED_XCMS_ID ORI_XCMS_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,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_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, ntd.ZPOS_POSTDATE,ntd.ZPOS_ORIGINATED_XCMS_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), 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 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_XCMS_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,
  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,
  (SELECT v8_rep_name.NAME6 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ORI_XCMS_ID) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  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, 
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  MAX(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  ntd.ZPOS_ORIGINATED_XCMS_ID ORI_XCMS_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,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_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, ntd.ZPOS_ORIGINATED_XCMS_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,ntd.ZPOS_POSTDATE
) 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 ZPOS_POSTDATE_S, CDT ASC;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_ZUDD_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,
    GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
  decode(INDICATE_ZPOS_ID,
                                     0, WTRL_TRANSLATE('ZUDD',(select ZUDD_ID from ZUDD where ZUDD_ID=ZUDD_OR_ZPOS_S),'ZUDD_DESCRIPTION',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_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=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=ZUDD_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZUDD_OR_ZPOS_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZUDD_OR_ZPOS_S)  and ZUDD_OR_ZPOS_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZUDD_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=ZUDD_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZUDD_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZUDD_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_OR_ZPOS_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_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=ZUDD_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZUDD_OR_ZPOS_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=ZUDD_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZUDD_OR_ZPOS_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZUDD_OR_ZPOS_S)  and ZUDD_OR_ZPOS_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_OR_ZPOS_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZUDD_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=ZUDD_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZUDD_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZUDD_OR_ZPOS_S),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_OR_ZPOS_S)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_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=ZUDD_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZUDD_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=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_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_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,               
  INDICATE_ZPOS_ID,                             
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(ntd.ZPOS_ZDCO_ID) ZDCO_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,
  DECODE(NVL(ntd.ZPOS_ZUDD_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZUDD_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZUDD_ID) ZUDD_OR_ZPOS_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, ntd.ZPOS_POSTDATE, DECODE(NVL(ntd.ZPOS_ZUDD_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZUDD_ID), DECODE(NVL(ntd.ZPOS_ZUDD_ID,0),0,1,0)
) 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, DDESC ASC;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_ZUDD_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, 
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
  decode(INDICATE_ZPOS_ID,
                                     0, WTRL_TRANSLATE('ZUDD',(select ZUDD_ID from ZUDD where ZUDD_ID=ZUDD_OR_ZPOS_S),'ZUDD_DESCRIPTION',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_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=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_ID from ZPOS where ZPOS.ZPOS_ID=ZPOS_ID_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S)),
                                                       --  wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=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=ZUDD_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZUDD_OR_ZPOS_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZUDD_OR_ZPOS_S)  and ZUDD_OR_ZPOS_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZUDD_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=ZUDD_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZUDD_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZUDD_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_OR_ZPOS_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_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=ZUDD_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZUDD_OR_ZPOS_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=ZUDD_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZUDD_OR_ZPOS_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZUDD_OR_ZPOS_S)  and ZUDD_OR_ZPOS_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_OR_ZPOS_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZUDD_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=ZUDD_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZUDD_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZUDD_OR_ZPOS_S),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_OR_ZPOS_S)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZUDD_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=ZUDD_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZUDD_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=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_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_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
FROM
(
SELECT
  MAX(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(ntd.ZPOS_ZDCO_ID) ZDCO_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,
  DECODE(NVL(ntd.ZPOS_ZUDD_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZUDD_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZUDD_ID) ZUDD_OR_ZPOS_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, DECODE(NVL(ntd.ZPOS_ZUDD_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZUDD_ID), DECODE(NVL(ntd.ZPOS_ZUDD_ID,0),0,1,0)
) 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 ZPOS_POSTDATE_S, CDT, DDESC ASC;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_VOID
AS 
SELECT
--  distinct
  ZPOS.ZPOS_POSTDATE DAT,
  ZPOS.ZPOS_CDT CDT,
  ZPOS.ZPOS_ID ZPOS_ID,
  (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),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))*-1 GROSS,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))*-1 PAY,
  (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)))*-1 REV,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*((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)))/ZPOS.ZPOS_QUANTITY))*-1 UNIT,
  (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)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)*-1 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)=2 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)*-1  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)=2 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)*-1 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)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)*-1 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)=2 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)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
  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_longdesc 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_longdesc 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_longdesc 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_longdesc 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,
  (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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))))) 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,
  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)=2 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)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=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)=2 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),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)=2 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),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)=2 
  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)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)*-100,'999999990,09') FSPAY_CL,
  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   
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)=2
  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));

CREATE OR REPLACE VIEW V8_ED_FOLIO_TAX_INFO_VOID
AS 
SELECT 
 ZWIN_ID_S ZWIN_ID, 
 ZTCD_ID_S ZTCD_ID, 
 (SELECT ZTCD.ZTCD_POSTTO_ZDCO_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S) ZDCO_ID, 
 (SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S) ZTCD_ZTXC_ID, 
 Wtrl_Translate('ZTXC',(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S),'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=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)))) ZTCD_NAME, 
 (SELECT ZTXC.ZTXC_SHORTDESC FROM ZTXC WHERE ZTXC.ZTXC_ID=(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S)) ZTXC_SHORTDESC, 
 -1*GROSS_AMOUNT                        GROSS, 
 -1*ROUND(GROSS_AMOUNT,2)                        GROSS2,
 -1*NET_AMOUNT                 NET, 
 -1*ROUND(NET_AMOUNT,2)        NET2, 
 -1*TAX_AMOUNT                 TAX, 
 -1*ROUND(TAX_AMOUNT,2)        TAX2 
FROM 
( 
SELECT 
 ZWIN_ID_S ZWIN_ID_S, 
 ZTCD_ID_S ZTCD_ID_S, 
 SUM(S_GROSS) GROSS_AMOUNT, 
 SUM(S_NET) NET_AMOUNT, 
 SUM(S_GROSS-S_NET) TAX_AMOUNT 
FROM 
( 
SELECT 
  DISTINCT 
  z.ZPOS_ZWIN_ID ZWIN_ID_S, 
  z.ZPOS_ZTCD_ID ZTCD_ID_S, 
  z.ZPOS_TAXLINK_ID, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) 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, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX 
FROM 
  ZPOS z 
WHERE 
  NVL(z.ZPOS_ZTCD_ID,0)<>0 AND NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)=2 
  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) 
) 
GROUP BY ZWIN_ID_S,ZTCD_ID_S 
) 
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, 
 -1*GROSS_AMOUNT             GROSS, 
 -1*ROUND(GROSS_AMOUNT,2)             GROSS2,
 -1*NET_AMOUNT                 NET, 
 -1*ROUND(NET_AMOUNT,2)        NET2, 
 -1*TAX_AMOUNT                 TAX, 
 -1*ROUND(TAX_AMOUNT,2)        TAX2 
FROM 
( 
SELECT 
 S_ZWIN_ID S_ZWIN_ID, 
 SUM(S_GROSS) GROSS_AMOUNT, 
 SUM(S_NET) NET_AMOUNT, 
 SUM(S_TAX) TAX_AMOUNT 
FROM 
( 
SELECT 
  z.ZPOS_ZWIN_ID S_ZWIN_ID, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) 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, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX 
  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)=2 
  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) 
) 
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, 
 -1*SUM(GROSS_AMOUNT)          GROSS, 
 -1*ROUND(SUM(GROSS_AMOUNT),2)          GROSS2, 
 -1*SUM(NET_AMOUNT)              NET, 
 -1*ROUND(SUM(NET_AMOUNT),2)     NET2, 
 -1*SUM(TAX_AMOUNT)              TAX, 
 -1*ROUND(SUM(TAX_AMOUNT),2)     TAX2 
FROM 
( 
SELECT 
 S_ZWIN_ID S_ZWIN_ID, 
 S_ZPOS_POSTDATE S_ZPOS_POSTDATE, 
 SUM(S_GROSS) GROSS_AMOUNT, 
 SUM(S_NET) NET_AMOUNT, 
 SUM(S_TAX) TAX_AMOUNT 
FROM 
( 
SELECT 
  z.ZPOS_ZWIN_ID S_ZWIN_ID, 
  z.ZPOS_POSTDATE S_ZPOS_POSTDATE, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) 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, 
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX 
  FROM 
  ZPOS z 
WHERE 
  NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)=2 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) 
) 
GROUP BY S_ZWIN_ID,S_ZPOS_POSTDATE 
) GROUP BY S_ZWIN_ID;

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

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_TAX_DET
AS 
select
ZWIN_ID_S,
TAC_TAX,
TAXCOUNT,
SPLITID,
SUM(GROSS_REV) GROSS_REV,
SUM(NET_REV) NET_REV,
sum(TAX) TAX,
TAX_CODE_DESC,
round(SUM(GROSS_REV),2) GROSS_REV2,
round(SUM(NET_REV),2) NET_REV2,
round(sum(TAX),2) TAX2
--TAXLINK_ID_S2
from
(select
ZWIN_ID_S2 ZWIN_ID_S,
SPLITID_S2 SPLITID, 
TAX_CODE_S TAC_TAX,
TAXCOUNT,
decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0) GROSS_REV,
decode (TAXCOUNT,3,0,S_NET) NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S),'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_S2))))TAX_CODE_DESC,
TAXLINK_ID_S2
from(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
nvl((select count(ZPOS.ZPOS_TAXLINK_ID) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID),0)TAXCOUNT,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S
from zpos Z1
where Z1.ZPOS_CDT=2
))group by ZWIN_ID_S, SPLITID, TAC_TAX, TAXCOUNT, TAX_CODE_DESC
order by TAX_CODE_DESC;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_TAX_DESC_DET
AS 
select
ZWIN_ID_S2 ZWIN_ID_S,
SPLITID_S2 SPLITID, 
DESCRIPT_S2 DESCRIPT,
TAXCOUNT,
decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0) GROSS_REV,
decode (TAXCOUNT,3,0,S_NET) NET_REV,
S_TAX TAX,
round(decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0),2) GROSS_REV2,
round(decode (TAXCOUNT,3,0,S_NET),2) NET_REV2,
round(S_TAX,2) TAX2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S),'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_S2))))TAX_CODE_DESC,
TAXLINK_ID_S2
from(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
Z1.ZPOS_DESCRIPT DESCRIPT_S2,
nvl((select count(ZPOS.ZPOS_TAXLINK_ID) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID),0)TAXCOUNT,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S
from zpos Z1
where Z1.ZPOS_CDT=2
--and Z1.ZPOS_ZWIN_ID=1480
);

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_APD_TAX_DET
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
ZPOS_POSTDATE_S2 ZPOS_POSTDATE,
to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAX_CODE,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
round(GROSS_REV_S2,2) GROSS_REV2,
round(S_NET,2) NET_REV2,
round(S_TAX,2) TAX2,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2) TAX_CODE_DESC
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2, 
Z1.ZPOS_ZARR_ID SPLITID_S2, 
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_ZARR_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) 
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
ZPOS_POSTDATE_S2 ZPOS_POSTDATE,
to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAC_TAX,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
round(GROSS_REV_S2,2) GROSS_REV2,
round(S_NET,2) NET_REV2,
round(S_TAX,2) TAX2,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2) TAX_CODE_DESC
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
);

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_APS_TAX_DET
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAX_CODE,
SUM(GROSS_REV_S2) GROSS_REV,
SUM(S_NET) NET_REV,
SUM(S_TAX) TAX,
round(SUM(GROSS_REV_S2),2) GROSS_REV2,
round(SUM(S_NET),2) NET_RE2V,
round(SUM(S_TAX),2) TAX2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_ZARR_ID SPLITID_S2, 
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_ZARR_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)
group by ZWIN_ID_S2, SPLITID_S2,TAX_CODE_S2
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAC_TAX,
SUM(GROSS_REV_S2) GROSS_REV,
SUM(S_NET) NET_REV,
SUM(S_TAX) TAX,
round(SUM(GROSS_REV_S2),2) GROSS_REV2,
round(SUM(S_NET),2) NET_RE2V,
round(SUM(S_TAX),2) TAX2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)group by ZWIN_ID_S2, SPLITID_S2, TAX_CODE_S2;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_ORI_ARR_PD
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_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                       --  wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         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,
  ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
  INDICATE_ZPOS_ID, 
  ZPOS_DEPOSIT_ZINV_ID, 
  ZPOS_DEPOSIT, 
 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
  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,
  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,
  ntd.ZPOS_DEPOSIT, 
  nvl(ntd.ZPOS_DEPOSIT_ZINV_ID,0) ZPOS_DEPOSIT_ZINV_ID 
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
  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_POSTDATE,ntd.ZPOS_DEPOSIT_ZINV_ID, ntd.ZPOS_DEPOSIT, 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
  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,
  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,
  ntd.ZPOS_DEPOSIT,
  nvl(ntd.ZPOS_DEPOSIT_ZINV_ID,0) ZPOS_DEPOSIT_ZINV_ID 
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
  WHERE
  --ZWIN.ZWIN_ID=:ZWIN_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_POSTDATE,ntd.ZPOS_DEPOSIT_ZINV_ID,ntd.ZPOS_DEPOSIT, 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 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_APD_G_TAX_DET
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
ZPOS_POSTDATE_S2 ZPOS_POSTDATE,
to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAX_CODE,
SUM(GROSS_REV_S2) GROSS_REV,
SUM(S_NET) NET_REV,
SUM(S_TAX) TAX,
round(SUM(GROSS_REV_S2),2) GROSS_REV2,
round(SUM(S_NET),2) NET_RE2V,
round(SUM(S_TAX),2) TAX2,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2, 
Z1.ZPOS_ZARR_ID SPLITID_S2, 
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_ZARR_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2, ZPOS_POSTDATE_S2, SPLITID_S2, TAX_CODE_S2, ORI_RES_FA_ID_S2
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
ZPOS_POSTDATE_S2 ZPOS_POSTDATE,
to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAC_TAX,
SUM(GROSS_REV_S2) GROSS_REV,
SUM(S_NET) NET_REV,
SUM(S_TAX) TAX,
round(SUM(GROSS_REV_S2),2) GROSS_REV2,
round(SUM(S_NET),2) NET_RE2V,
round(SUM(S_TAX),2) TAX2,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2, ZPOS_POSTDATE_S2, SPLITID_S2, TAX_CODE_S2, ORI_RES_FA_ID_S2;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_ORI_ARR_PS
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  (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,
   ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
  INDICATE_ZPOS_ID, 
   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_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 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,
  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_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,
  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_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 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_APS_G_TAX_DET
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
SPLITID_S SPLITID,
TAX_CODE_S TAX_CODE,
GROSS_REV_S GROSS_REV,
round(GROSS_REV_S,2) GROSS_REV2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S),'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))))TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
SPLITID_S2 SPLITID_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_ZARR_ID SPLITID_S2, 
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
SPLITID_S SPLITID,
TAX_CODE_S TAC_TAX,
GROSS_REV_S GROSS_REV,
round(GROSS_REV_S,2) GROSS_REV2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S),'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))))TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
SPLITID_S2 SPLITID_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
);

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_ORI_GCHK
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('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,
  ZCHK_OR_ZPOS_S ZCHK_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 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)), '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
--  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 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,
  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
--  distinct
  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,
  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 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_GCHK_G_TAX_DET
AS 
select
INDICATE_ZPOS_ID,
ZWIN_ID,
SPLITID,
ZCHK_OR_ZPOS,
TAX_CODE,
sum(gross_rev) GROSS_REV,
Sum(Net_rev) NET_REV,
sum(TAX) TAX,
round(sum(gross_rev),2) GROSS_REV2,
round(Sum(Net_rev),2) NET_REV2,
round(sum(TAX),2) TAX2,
TAX_CODE_DESC,
ORI_RES_FA_ID
from
 (
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
ZCHK_OR_ZPOS_S2 ZCHK_OR_ZPOS,
--DESCRIPT_S DESCRIPT,
TAX_CODE_S2 TAX_CODE,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
case when nvl((select z2.zpos_poscheck_nr from zpos z2 where z2.zpos_id=z1.zpos_internalsplit_zpos_id),'0')='0' then to_char(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) else to_char(Z1.ZPOS_POSCHECK_NR) end   SPLITID_S2,
to_char(DECODE(NVL(Z1.ZPOS_POSCHECK_NR,' '),' ',to_char(Z1.ZPOS_ID),Z1.ZPOS_POSCHECK_NR)) ZCHK_OR_ZPOS_S2,
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and (Z1.ZPOS_POSCHECK_NR) is not null
and Z1.ZPOS_ID=Z1.ZPOS_INTERNALSPLIT_ZPOS_ID
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
ZCHK_OR_ZPOS_S2 ZCHK_OR_ZPOS,
--DESCRIPT_S DESCRIP,
TAX_CODE_S2 TAC_TAX,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) SPLITID_S2,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) ZCHK_OR_ZPOS_S2,
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and (Z1.ZPOS_POSCHECK_NR) is Null
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
ZCHK_OR_ZPOS_S2 ZCHK_OR_ZPOS,
--DESCRIPT_S DESCRIP,
TAX_CODE_S2 TAC_TAX,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) SPLITID_S2,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) ZCHK_OR_ZPOS_S2,
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT<>2
and (Z1.ZPOS_POSCHECK_NR) is not Null
and Z1.ZPOS_ID<>Z1.ZPOS_INTERNALSPLIT_ZPOS_ID
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
))
group by
INDICATE_ZPOS_ID,
ZWIN_ID,
SPLITID,
ZCHK_OR_ZPOS,
TAX_CODE,
TAX_CODE_DESC,
ORI_RES_FA_ID;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_ORI_ZDCO_PS
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  (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,
nvl2((to_char( DEPARTURE,'J')),'-',NULL) MINUSI,
QTY,
GROSS_QTY,
  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(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, 
   ZDCO_OR_ZPOS_S ZDCO_OR_ZPOS, 
  INDICATE_ZPOS_ID, 
    --decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZDCO_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=ZDCO_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_ZDCO_ID=ZDCO_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_ZDCO_ID=ZDCO_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=ZDCO_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=ZDCO_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_ZDCO_ID=ZDCO_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_ZDCO_ID=ZDCO_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=ZDCO_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_ZDCO_ID=ZDCO_OR_ZPOS_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z5.ZPOS_INTERNALGROUP_ID = Z3.ZPOS_INTERNALGROUP_ID)>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,
sum(NTD.ZPOS_QUANTITY) QTY,
  1 IS_RES,
  0 IS_FA,
  ntd.ZPOS_ORIGINATED_YRES_ID ORI_RES_FA_ID,
  NTD.ZPOS_SUMUPAMOUNT GROSS_QTY,
  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_ZDCO_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZDCO_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZDCO_ID) ZDCO_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES,
  ntd.ZPOS_ZDCO_ID ZDCO_ID_S
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, ntd.ZPOS_ZDCO_ID, NTD.ZPOS_SUMUPAMOUNT,
DECODE(NVL(ntd.ZPOS_ZDCO_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZDCO_ID), 
DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZDCO_ID,0),0,1,0)
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,
sum(NTD.ZPOS_QUANTITY) QTY,
  0 IS_RES,
  1 IS_FA,
  ntd.ZPOS_ORIGINATED_ZFAC_ID ORI_RES_FA_ID,
  NTD.ZPOS_SUMUPAMOUNT GROSS_QTY,
  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_ZDCO_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZDCO_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZDCO_ID) ZDCO_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES,
  ntd.ZPOS_ZDCO_ID ZDCO_ID_S
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, ntd.ZPOS_ZDCO_ID, NTD.ZPOS_SUMUPAMOUNT,
DECODE(NVL(ntd.ZPOS_ZDCO_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZDCO_ID),
DECODE(NVL(ntd.ZPOS_ZDCO_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZDCO_ID), 
DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZDCO_ID,0),0,1,0))
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_ZDCO_G_TAX_DET
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAX_CODE,
TAXCOUNT,
decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0) GROSS_REV,
round(decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0),2) GROSS_REV2,
decode (TAXCOUNT,3,0,S_NET) NET_REV,
round(decode (TAXCOUNT,3,0,S_NET),2) NET_REV2, -- NS 28.06.NEW FIELD
S_TAX TAX,
round(S_TAX ,2) TAX2, -- NS 28.06.NEW FIELD
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_INTERNALGROUP_ID SPLITID_S2, 
nvl((select count(ZPOS.ZPOS_TAXLINK_ID) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID),0)TAXCOUNT,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_INTERNALGROUP_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAC_TAX,
TAXCOUNT,
decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0) GROSS_REV,
round(decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0),2) GROSS_REV2,
decode (TAXCOUNT,3,0,S_NET) NET_REV,
round(decode (TAXCOUNT,3,0,S_NET),2) NET_REV2, -- NS 28.06.NEW FIELD
S_TAX TAX,
round(S_TAX ,2) TAX2, -- NS 28.06.NEW FIELD
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2,
nvl((select count(ZPOS.ZPOS_TAXLINK_ID) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID),0)TAXCOUNT,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_INTERNALGROUP_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
);

CREATE OR REPLACE FORCE VIEW V8_ED_FOLIO_REV_APD_G_TAX_DESC
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
ZPOS_POSTDATE_S ZPOS_POSTDATE,
to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S SPLITID,
--DESCRIPT_S DESCRIPT,
TAX_CODE_S TAX_CODE,
GROSS_REV_S GROSS_REV,
round((GROSS_REV_S),2) GROSS_REV2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S),'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))))TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
ZPOS_POSTDATE_S2 ZPOS_POSTDATE_S,
SPLITID_S2 SPLITID_S,
--DESCRIPT_S2 DESCRIPT_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2, 
Z1.ZPOS_ZARR_ID SPLITID_S2, 
--Z1.ZPOS_DESCRIPT DESCRIPT_S2,
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,ZPOS_POSTDATE_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
ZPOS_POSTDATE_S ZPOS_POSTDATE,
to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S SPLITID,
--DESCRIPT_S DESCRIP,
TAX_CODE_S TAC_TAX,
GROSS_REV_S GROSS_REV,
round((GROSS_REV_S),2) GROSS_REV2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S),'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))))TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
ZPOS_POSTDATE_S2 ZPOS_POSTDATE_S,
SPLITID_S2 SPLITID_S,
--DESCRIPT_S2 DESCRIPT_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
--Z1.ZPOS_DESCRIPT DESCRIPT_S2, 
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,ZPOS_POSTDATE_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
);

CREATE OR REPLACE VIEW V8_ED_FOL_REV_GCHK_G_TAX_DESC
AS 
select
INDICATE_ZPOS_ID,
ZWIN_ID,
SPLITID,
ZCHK_OR_ZPOS,
TAX_CODE,
DESCRIPT,
sum(gross_rev) GROSS_REV,
round(sum(gross_rev),2) GROSS_REV2,
Sum(Net_rev) NET_REV,
round(Sum(Net_rev),2) NET_REV2,
sum(TAX) TAX,
round(sum(TAX),2) TAX2,
TAX_CODE_DESC,
ORI_RES_FA_ID
from
 (
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
ZCHK_OR_ZPOS_S2 ZCHK_OR_ZPOS,
DESCRIPT_S DESCRIPT,
TAX_CODE_S2 TAX_CODE,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_DESCRIPT DESCRIPT_S,
case when nvl((select z2.zpos_poscheck_nr from zpos z2 where z2.zpos_id=z1.zpos_internalsplit_zpos_id),'0')='0' then to_char(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) else to_char(Z1.ZPOS_POSCHECK_NR) end   SPLITID_S2,
to_char(DECODE(NVL(Z1.ZPOS_POSCHECK_NR,' '),' ',to_char(Z1.ZPOS_ID),Z1.ZPOS_POSCHECK_NR)) ZCHK_OR_ZPOS_S2,
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and (Z1.ZPOS_POSCHECK_NR) is not null
and Z1.ZPOS_ID=Z1.ZPOS_INTERNALSPLIT_ZPOS_ID
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
ZCHK_OR_ZPOS_S2 ZCHK_OR_ZPOS,
DESCRIPT_S DESCRIP,
TAX_CODE_S2 TAC_TAX,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_DESCRIPT DESCRIPT_S,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) SPLITID_S2,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) ZCHK_OR_ZPOS_S2,
--(select sum(zpos_grossunitprice*zpos_quantity) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID) GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and (Z1.ZPOS_POSCHECK_NR) is Null
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
ZCHK_OR_ZPOS_S2 ZCHK_OR_ZPOS,
DESCRIPT_S DESCRIP,
TAX_CODE_S2 TAC_TAX,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_DESCRIPT DESCRIPT_S,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) SPLITID_S2,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) ZCHK_OR_ZPOS_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT<>2
and (Z1.ZPOS_POSCHECK_NR) is not Null
and Z1.ZPOS_ID<>Z1.ZPOS_INTERNALSPLIT_ZPOS_ID
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
))
group by
INDICATE_ZPOS_ID,
ZWIN_ID,
DESCRIPT,
SPLITID,
ZCHK_OR_ZPOS,
TAX_CODE,
TAX_CODE_DESC,
ORI_RES_FA_ID;

CREATE OR REPLACE VIEW V8_ED_FOL_REV_ZDCO_G_TAX_DESC
AS 
select
INDICATE_ZPOS_ID,
ZWIN_ID,
SPLITID,
TAX_CODE,
sum(GROSS_REV) GROSS_REV,
sum( NET_REV) NET_REV,
sum(TAX) TAX,
round(sum(GROSS_REV),2) GROSS_REV2,
round(sum( NET_REV),2) NET_REV2,
round(sum(TAX),2) TAX2,
TAX_CODE_DESC,
ORI_RES_FA_ID
from
(select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAX_CODE,
DESCRIPT_S DESCRIPT,
TAXCOUNT,
decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0) GROSS_REV,
decode (TAXCOUNT,3,0,S_NET) NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_DESCRIPT DESCRIPT_S,
Z1.ZPOS_INTERNALGROUP_ID SPLITID_S2, 
nvl((select count(ZPOS.ZPOS_TAXLINK_ID) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID),0)TAXCOUNT,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_INTERNALGROUP_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAC_TAX,
DESCRIPT_S DESCRIPT,
TAXCOUNT,
decode(TAXCOUNT,0,GROSS_REV_S2,1,GROSS_REV_S2,2,GROSS_REV_S2,0) GROSS_REV,
decode (TAXCOUNT,3,0,S_NET) NET_REV,
S_TAX TAX,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S2),'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_S2))))TAX_CODE_DESC,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_DESCRIPT DESCRIPT_S,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2,
nvl((select count(ZPOS.ZPOS_TAXLINK_ID) from zpos where ZPOS.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID),0)TAXCOUNT,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0)  GROSS_REV_S2,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z1.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z1.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX,
Z1.ZPOS_TAXLINK_ID TAXLINK_ID_S2,
Z1.ZPOS_ZTCD_ID TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_INTERNALGROUP_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
)) group by INDICATE_ZPOS_ID,
ZWIN_ID,
SPLITID,
TAX_CODE,TAX_CODE_DESC,
ORI_RES_FA_ID;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_APS_G_TAX_DESC
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
SPLITID_S SPLITID,
TAX_CODE_S TAX_CODE,
GROSS_REV_S GROSS_REV,
round(GROSS_REV_S,2) GROSS_REV2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S),'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))))TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
SPLITID_S2 SPLITID_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_ZARR_ID SPLITID_S2, 
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
SPLITID_S SPLITID,
TAX_CODE_S TAC_TAX,
GROSS_REV_S GROSS_REV,
round(GROSS_REV_S,2) GROSS_REV2,
(wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S),'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))))TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
SPLITID_S2 SPLITID_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
);

CREATE OR REPLACE VIEW V8_ED_FOLIO_TAX_INFO_DEPO
AS 
SELECT
 ZWIN_ID_S ZWIN_ID,
 ZTCD_ID_S ZTCD_ID,
 (SELECT ZTCD.ZTCD_POSTTO_ZDCO_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S) ZDCO_ID,
 (SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S) ZTCD_ZTXC_ID,
 Wtrl_Translate('ZTXC',(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S),'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=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)))) ZTCD_NAME,
 (SELECT ZTXC.ZTXC_SHORTDESC FROM ZTXC WHERE ZTXC.ZTXC_ID=(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S)) ZTXC_SHORTDESC,
 (SELECT ZTXC.ZTXC_LONGDESC FROM ZTXC WHERE ZTXC.ZTXC_ID=(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S)) ZTXC_LONGDESC,
 GROSS_AMOUNT                        GROSS,
  round(GROSS_AMOUNT,2)                        GROSS2,
 NET_AMOUNT                 NET,
 ROUND(NET_AMOUNT,2)        NET2,
 TAX_AMOUNT                 TAX,
 ROUND(TAX_AMOUNT,2)        TAX2,
 MULTI_TAX_AMOUNT           MULTITAX,
 ROUND(MULTI_TAX_AMOUNT,2)  MULTITAX2,
 (SELECT ztxc.ztxc_displorder FROM ztxc WHERE ztxc.ztxc_id = (SELECT ztcd.ztcd_ztxc_id FROM ztcd WHERE ztcd.ztcd_id = ztcd_id_s)) tax_code
 FROM
(
SELECT
 ZWIN_ID_S ZWIN_ID_S,
 ZTCD_ID_S ZTCD_ID_S,
 SUM(S_GROSS) GROSS_AMOUNT,
 SUM(S_NET) NET_AMOUNT,
 SUM(S_GROSS-S_NET) TAX_AMOUNT,
 SUM(S_TAX) MULTI_TAX_AMOUNT
FROM
(
SELECT
  DISTINCT
  z.ZPOS_ZWIN_ID ZWIN_ID_S,
  z.ZPOS_ZTCD_ID ZTCD_ID_S,
  z.ZPOS_TAXLINK_ID,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) 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,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX
FROM
  ZPOS z
WHERE
  NVL(z.ZPOS_ZTCD_ID,0)<>0 
  and z.zpos_deposit=1 --NEW FIELD
  --AND NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)<>1 -- Needed for DEP VIEW
  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)
)
GROUP BY ZWIN_ID_S,ZTCD_ID_S
)
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,
 'Paid Out' ZTXC_LONGDESC,
 GROSS_AMOUNT             GROSS,
  round(GROSS_AMOUNT,2)             GROSS2,
 NET_AMOUNT                 NET,
 ROUND(NET_AMOUNT,2)        NET2,
 TAX_AMOUNT                 TAX,
 ROUND(TAX_AMOUNT,2)        TAX2,
 0 MULTITAX,
 0 MULTITAX2,
 0 tax_code
FROM
(
SELECT
 S_ZWIN_ID S_ZWIN_ID,
 SUM(S_GROSS) GROSS_AMOUNT,
 SUM(S_NET) NET_AMOUNT,
 SUM(S_TAX) TAX_AMOUNT
FROM
(
SELECT
  z.ZPOS_ZWIN_ID S_ZWIN_ID,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) 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,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX
  FROM
  ZPOS z
WHERE
  z.zpos_deposit=1 --NEW FIELD
 --AND NVL(z.ZPOS_NOREPRINT,0)<>1 Needed for DEP VIEW
   and  z.ZPOS_ZDCO_ID IN (SELECT ZDCO.ZDCO_ID FROM ZDCO WHERE ZDCO.ZDCO_PAIDOUT=1) AND NVL(z.ZPOS_HIDDEN,0)=0 
  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)
)
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,
 'No Tax Linked' ZTXC_LONGDESC,
 SUM(GROSS_AMOUNT)          GROSS,
 round(sum(gross_amount),2) GROSS2,
 SUM(NET_AMOUNT)              NET,
 ROUND(SUM(NET_AMOUNT),2)     NET2,
 SUM(TAX_AMOUNT)              TAX,
 ROUND(SUM(TAX_AMOUNT),2)     TAX2,
 0 MULTITAX,
 0 MULTITAX2,
 0 tax_code
FROM
(
SELECT
 S_ZWIN_ID S_ZWIN_ID,
 S_ZPOS_POSTDATE S_ZPOS_POSTDATE,
 SUM(S_GROSS) GROSS_AMOUNT,
 SUM(S_NET) NET_AMOUNT,
 SUM(S_TAX) TAX_AMOUNT
FROM
(
SELECT
  z.ZPOS_ZWIN_ID S_ZWIN_ID,
  z.ZPOS_POSTDATE S_ZPOS_POSTDATE,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) 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,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX
  FROM
  ZPOS z
WHERE
 z.zpos_deposit=1 --NEW FIELD
 --AND NVL(z.ZPOS_NOREPRINT,0)<>1 Needed for DEP VIEW
  and NVL(z.ZPOS_HIDDEN,0)=0 
  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)
)
GROUP BY S_ZWIN_ID,S_ZPOS_POSTDATE
) GROUP BY S_ZWIN_ID;

CREATE OR REPLACE VIEW V8_ED_FOLIO_TAX_ALL_DEP_ALL
AS 
select 
ZWIN_ID,
SUM(NVL(GROSS,0)) GROSS,
SUM(NVL(GROSS2,0)) GROSS2,
SUM(NVL(NET,0)) NET,
SUM(NVL(NET2,0)) NET2,-- Field was missing in the first select Statement
SUM(NVL(TAX,0)) TAX,
SUM(NVL(TAX2,0)) TAX2,
SUM(NVL(MULTITAX,0)) MULTITAX,
SUM(NVL(MULTITAX2,0)) MULTITAX2,
Check_DEPO
from (select
zwin_id,
ztcd_id,
ZDCO_ID,
ZTCD_ZTXC_ID,
ZTCD_NAME,
ZTXC_SHORTDESC,
ZTXC_LONGDESC,
SUM(GROSS) GROSS,
SUM(GROSS2) GROSS2,
SUM(NET) NET,
SUM(NET2) NET2,
SUM(TAX) TAX,
SUM(TAX2) TAX2,
SUM(MULTITAX) MULTITAX,
SUM(MULTITAX2) MULTITAX2,
CHECK_DEPO
from
(select 
zwin_id,
ztcd_id,
ZDCO_ID,
ZTCD_ZTXC_ID,
ZTCD_NAME,
ZTXC_SHORTDESC,
ZTXC_LONGDESC,
nvl(GROSS,0)GROSS,
nvl(GROSS2,0)GROSS2,
nvl(NET,0)NET,
nvl(NET2,0)NET2,
nvl(TAX,0)TAX,
nvl(TAX2,0)TAX2,
nvl(MULTITAX,0)MULTITAX,
nvl(MULTITAX2,0)MULTITAX2,
nvl((select 1 from zpos where zpos.zpos_zwin_id=v8_ed_folio_tax_info.zwin_id  and zpos.zpos_deposit=1 and rownum=1),0) CHECK_DEPO
from
V8_ED_FOLIO_TAX_INFO
union all
select 
zwin_id,
ztcd_id,
ZDCO_ID,
ZTCD_ZTXC_ID,
ZTCD_NAME,
ZTXC_SHORTDESC,
ZTXC_LONGDESC,
nvl(GROSS,0)GROSS,
nvl(GROSS2,0)GROSS2,
nvl(NET,0)NET,
nvl(NET2,0)NET2,
nvl(TAX,0)TAX,
nvl(TAX2,0)TAX2,
nvl(MULTITAX,0)MULTITAX,
nvl(MULTITAX2,0)MULTITAX2,
nvl((select 1 from zpos where zpos.zpos_zwin_id=v8_ed_folio_tax_info_DEPO.zwin_id  and zpos.zpos_deposit=1 and rownum=1),0) CHECK_DEPO
from
V8_ED_FOLIO_TAX_INFO_DEPO)
group by zwin_id,
ztcd_id,
ZDCO_ID,
ZTCD_ZTXC_ID,
ZTCD_NAME,
ZTXC_SHORTDESC,
ZTXC_LONGDESC,
CHECK_DEPO)
group by ZWIN_ID, CHECK_DEPO;

CREATE OR REPLACE VIEW V8_ED_FOLIO_TAX_ALL_WITH_DEP
AS 
select
zwin_id,
ztcd_id,
ZDCO_ID,
ZTCD_ZTXC_ID,
ZTCD_NAME,
ZTXC_SHORTDESC,
ZTXC_LONGDESC,
tax_code,
SUM(GROSS) GROSS,
round(SUM(GROSS),2) GROSS2,
SUM(NET) NET,
SUM(NET2) NET2,
SUM(TAX) TAX,
SUM(TAX2) TAX2,
SUM(MULTITAX) MULTITAX,
SUM(MULTITAX2) MULTITAX2,
CHECK_DEPO
from
(select 
zwin_id,
ztcd_id,
ZDCO_ID,
ZTCD_ZTXC_ID,
ZTCD_NAME,
ZTXC_SHORTDESC,
ZTXC_LONGDESC,
tax_code,
nvl(GROSS,0)GROSS,
round(nvl(GROSS,0),2)GROSS2,
nvl(NET,0)NET,
nvl(NET2,0)NET2,
nvl(TAX,0)TAX,
nvl(TAX2,0)TAX2,
nvl(MULTITAX,0)MULTITAX,
nvl(MULTITAX2,0)MULTITAX2,
nvl((select 1 from zpos where zpos.zpos_zwin_id=v8_ed_folio_tax_info.zwin_id  and zpos.zpos_deposit=1 and rownum=1),0) CHECK_DEPO
from
V8_ED_FOLIO_TAX_INFO
union all
select 
zwin_id,
ztcd_id,
ZDCO_ID,
ZTCD_ZTXC_ID,
ZTCD_NAME,
ZTXC_SHORTDESC,
ZTXC_LONGDESC,
tax_code,
nvl(GROSS,0)GROSS,
round(nvl(GROSS,0),2)GROSS2,
nvl(NET,0)NET,
nvl(NET2,0)NET2,
nvl(TAX,0)TAX,
nvl(TAX2,0)TAX2,
nvl(MULTITAX,0)MULTITAX,
nvl(MULTITAX2,0)MULTITAX2,
nvl((select 1 from zpos where zpos.zpos_zwin_id=v8_ed_folio_tax_info_DEPO.zwin_id  and zpos.zpos_deposit=1 and rownum=1),0) CHECK_DEPO
from
V8_ED_FOLIO_TAX_INFO_DEPO)
group by zwin_id,
ztcd_id,
ZDCO_ID,
ZTCD_ZTXC_ID,
ZTCD_NAME,
ZTXC_SHORTDESC,
ZTXC_LONGDESC,
tax_code,
CHECK_DEPO
order by tax_code;

CREATE OR REPLACE VIEW V8_ED_FOL_GRP_AMOUNT_ALL
AS 
SELECT 
MIN_DAT_S DAT, 
1 CDT, 
MIN_ZPOS_ID_S ZPOS_ID, 
COUNTER_S QTY, 
GROSS_S GROSS, 
SUM_GROSS_S SUM_GROSS, 
NVL(PAY_S,0) PAY, 
NVL(REV_S,0) 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 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 (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 (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 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 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 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 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 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 (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 (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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)) FSPAY_CL, 
ZWIN_ID_S ZWIN_ID, 
MIN_ZPOS_ID_S MIN_ZPOS_ID, 
(SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ZDCO_ID, 
(SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO WHERE ZDCO.ZDCO_ID=(SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=MIN_ZPOS_ID_S)) ZDCO_NUMERICDESC, 
(decode((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=MIN_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=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S))))
                                   ) DDESC
                                   FROM 
( 
SELECT 
--  distinct 
  MIN(ZPOS.ZPOS_POSTDATE) MIN_DAT_S, 
  MAX(ZPOS.ZPOS_POSTDATE) MAX_DAT_S, 
  MIN(ZPOS.ZPOS_ID) MIN_ZPOS_ID_S, 
  COUNT(*) COUNTER_S, 
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))) GROSS_S, 
  COUNT(*)*(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))) SUM_GROSS_S, 
  SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,NULL,(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)), NULL)) PAY_S, 
  SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) REV_S, 
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S 
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 
  ABS(ZPOS.ZPOS_QUANTITY)=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)) 
  GROUP BY 
  ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID,
  (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))) 
 ) 
     UNION ALL 
     ( select
 min(DAT) Dat,
CDT,
min (ZPOS_ID) ZPOS_ID,
SUM(QTY) QTY,
 GROSS,
SUM(SUM_GROSS) SUM_GROSS,
SUM(PAY) PAY,
SUM(REV) REV,
SUM(SPAY) SPAY,
SUM(SPAY_CL) SPAY_CL,
SUM(SPAY_NOCL) SPAY_NOCL,
SUM(SREV) SREV,
SUM(OPENA) OPENA,
FSPAY_CL,
ZWIN_ID,
min(MIN_ZPOS_ID) MIN_ZPOS_ID,
 MIN(ZDCO_ID) ZDCO_ID,
 MIN(ZDCO_NUMERICDESC) ZDCO_NUMERICDESC,
 MIN(DDESC) DDESC
  from  
       (SELECT 
 ZPOS.ZPOS_POSTDATE DAT, 
  ZPOS.ZPOS_CDT CDT, 
  ZPOS.ZPOS_ID ZPOS_ID, 
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY)) QTY, 
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*((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)))/ZPOS.ZPOS_QUANTITY)) GROSS, 
  (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))) SUM_GROSS, 
  NVL((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) PAY, 
  NVL((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) 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)             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 (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, 
  (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 (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_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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)      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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) 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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=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 (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),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 (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)) FSPAY_CL, 
  ZWIN.ZWIN_ID ZWIN_ID, 
  ZPOS.ZPOS_ID MIN_ZPOS_ID, 
  ZDCO.ZDCO_ID ZDCO_ID, 
  ZDCO.ZDCO_NUMERICDESC ZDCO_NUMERICDESC, 
   decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                      4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc 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 
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 
  ABS(ZPOS.ZPOS_QUANTITY)<>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)))
group by ZWIN_ID, GROSS, CDT, FSPAY_CL);

CREATE OR REPLACE VIEW V8_ED_FOL_GRP_AMOUNT_ROOMREV
AS 
SELECT 
MIN_DAT_S DAT, 
1 CDT, 
MIN_ZPOS_ID_S ZPOS_ID, 
COUNTER_S QTY, 
GROSS_S GROSS, 
SUM_GROSS_S SUM_GROSS, 
NVL(PAY_S,0) PAY, 
(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 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 (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 (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 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 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 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 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 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 (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 (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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)) FSPAY_CL, 
ZWIN_ID_S ZWIN_ID, 
(decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S)  and MIN_ZPOS_ID_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                         wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=MIN_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=MIN_ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=MIN_ZPOS_ID_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=MIN_ZPOS_ID_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=MIN_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=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S)  and MIN_ZPOS_ID_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=MIN_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=MIN_ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=MIN_ZPOS_ID_S),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S))))
                                   ) DDESC, 
(SELECT ZPOS.ZPOS_COMMENT FROM ZPOS WHERE ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ZPOS_COMMENT, 
(SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ZPOS_DESCRIPT 
FROM 
( 
SELECT 
  MIN(ZPOS.ZPOS_POSTDATE) MIN_DAT_S, 
  MAX(ZPOS.ZPOS_POSTDATE) MAX_DAT_S, 
  MIN(ZPOS.ZPOS_ID) MIN_ZPOS_ID_S, 
  COUNT(*) COUNTER_S, 
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))) GROSS_S, 
  COUNT(*)*(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))) SUM_GROSS_S, 
  SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,NULL,(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)), NULL)) PAY_S, 
  SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) REV_S, 
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S
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(ZDCO.ZDCO_SUBFUNCTION,0)=1 AND 
  NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND 
  ZPOS.ZPOS_CDT=1 
--AND ZWIN.ZWIN_ID=:paraw 
  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 
  ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID,
  (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))) 
 ) 
UNION ALL 
SELECT 
  ZPOS.ZPOS_POSTDATE DAT, 
  ZPOS.ZPOS_CDT CDT, 
  ZPOS.ZPOS_ID ZPOS_ID, 
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY))                                                                     QTY, 
  (DECODE(ZPOS.zpos_cdt,5,0,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))               GROSS, 
  (DECODE(ZPOS.zpos_cdt,5,0,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))               SUM_GROSS, 
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))                          PAY, 
  (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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)             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 (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, 
  (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 (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_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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)      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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) 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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=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 (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),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 (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)) FSPAY_CL, 
   ZWIN.ZWIN_ID ZWIN_ID, 
    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_longdesc 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,  
   ZPOS.ZPOS_COMMENT ZPOS_COMMENT, 
   ZPOS.ZPOS_DESCRIPT ZPOS_DESCRIPT 
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(ZDCO.ZDCO_SUBFUNCTION,0)<>1 AND 
  NVL(ZPOS.ZPOS_HIDDEN,0)=0 
  --AND ZWIN.ZWIN_ID=:paraw 
  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));

CREATE OR REPLACE VIEW V8_ED_FOL_GRP_AMT_ROOMREV_DAY
AS 
SELECT 
MIN_DAT_S DAT, 
1 CDT, 
MIN_ZPOS_ID_S ZPOS_ID, 
COUNTER_S QTY, 
GROSS_S GROSS, 
SUM_GROSS_S SUM_GROSS, 
NVL(PAY_S,0) PAY, 
(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 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 (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 (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 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 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 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 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 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 (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 (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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)) FSPAY_CL, 
ZWIN_ID_S ZWIN_ID, 
(decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S)  and MIN_ZPOS_ID_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=MIN_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=MIN_ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=MIN_ZPOS_ID_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=MIN_ZPOS_ID_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=MIN_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=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S)  and MIN_ZPOS_ID_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=MIN_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=MIN_ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=MIN_ZPOS_ID_S),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=MIN_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=MIN_ZPOS_ID_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=MIN_ZPOS_ID_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=MIN_ZPOS_ID_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=MIN_ZPOS_ID_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=MIN_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=MIN_ZPOS_ID_S))))
                                   ) DDESC, 
(SELECT ZPOS.ZPOS_COMMENT FROM ZPOS WHERE ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ZPOS_COMMENT, 
(SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=MIN_ZPOS_ID_S) ZPOS_DESCRIPT 
FROM 
( 
SELECT 
  MIN(ZPOS.ZPOS_POSTDATE) MIN_DAT_S, 
  MAX(ZPOS.ZPOS_POSTDATE) MAX_DAT_S, 
  MIN(ZPOS.ZPOS_ID) MIN_ZPOS_ID_S, 
  COUNT(*) COUNTER_S, 
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))) GROSS_S, 
  COUNT(*)*(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))) SUM_GROSS_S, 
  SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,NULL,(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)), NULL)) PAY_S, 
  SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) REV_S, 
  ZWIN.ZWIN_ID ZWIN_ID_S ,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S
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(ZDCO.ZDCO_SUBFUNCTION,0)=1 AND 
  NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND 
  ZPOS.ZPOS_CDT=1 
 -- AND ZWIN.ZWIN_ID=:paraw 
  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 
  ZWIN.ZWIN_ID,ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ZPOS.ZPOS_POSTDATE, 
  (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))) 
 ) 
UNION ALL 
SELECT 
  ZPOS.ZPOS_POSTDATE DAT, 
  ZPOS.ZPOS_CDT CDT, 
  ZPOS.ZPOS_ID ZPOS_ID, 
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY))                                                                     QTY, 
  (DECODE(ZPOS.zpos_cdt,5,0,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))               GROSS, 
  (DECODE(ZPOS.zpos_cdt,5,0,DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))               SUM_GROSS, 
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))                          PAY, 
  (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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)             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 (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, 
  (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 (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_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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)      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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) 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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=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 (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),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 (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),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 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 ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)) FSPAY_CL, 
   ZWIN.ZWIN_ID ZWIN_ID, 
  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_longdesc 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, 
   ZPOS.ZPOS_COMMENT ZPOS_COMMENT, 
   ZPOS.ZPOS_DESCRIPT ZPOS_DESCRIPT 
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(ZDCO.ZDCO_SUBFUNCTION,0)<>1 AND 
  NVL(ZPOS.ZPOS_HIDDEN,0)=0 
 -- AND ZWIN.ZWIN_ID=:paraw 
  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));

CREATE OR REPLACE VIEW V8_ED_FOL_ORI_GCHK_ARR_PD
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 (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
  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_POSTDATE,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
