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
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;


-- View for Custom Folio Description per Day

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,
  -- Quantity makes no sense in this Style decode((select zpos.ZPOS_PAIDOUT from zpos where zpos.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(select zpos.ZPOS_QUANTITY from zpos where zpos.ZPOS_ID=ZPOS_ID_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,
  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((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=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))),
                                                         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_ID from ZPOS where ZPOS.ZPOS_ID=ZPOS_ID_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=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=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=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=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,                         
  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
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;



-- View for Custom Folio Description per Stay

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((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=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))),
                                                         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_ID from ZPOS where ZPOS.ZPOS_ID=ZPOS_ID_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=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=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZPOS_ID_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=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=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
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
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;


-- View for Voiding Invoices, detailed Folio Style showing all figures as negative values

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(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_ORG_NAME,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_COMMENT_ORG,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      DDESC_LF_COM_ORG_ALL,
  ZPOS.ZPOS_PHONENR                                                                   PHONE,
  (case when nvl(ZPOS.ZPOS_PHONENR,'0')<>'0' then SUBSTR(ZPOS.ZPOS_PHONENR,1,LENGTH(ZPOS.ZPOS_PHONENR)-3)||'XXX' else ZPOS.ZPOS_PHONENR END) PHONE_XXX,
  ZDCO.ZDCO_ID                                                                        ZDCO_ID,
  ZDCO.ZDCO_NUMERICDESC                                                            NDESC,
  (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
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));


-- View for Folio Style grouped by Price per Day

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('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,   
    ZWIN_ID_S ZWIN_ID
FROM (
    SELECT
        zpos.zpos_postdate zpos_postdate_s,
        min(zpos.zpos_id) zpos_id_s,
        MIN(ZDCO_PAYM_TYPE) ZDCO_PAYM_TYPE_S,
        min(zpos.zpos_zdco_id) zdco_id_s,
        sum(decode(zpos.zpos_paidout,1,-1,1)*(zpos.zpos_quantity)) qty,
        decode(zpos.zpos_cdt,5,-1,1)*zpos.zpos_sumupamount/zpos.zpos_quantity unit,
        sum(decode(zpos.zpos_cdt,5,decode(zpos.zpos_paidout,1,(-zpos.zpos_unitprice*zpos.zpos_quantity),-(zpos.zpos_unitprice*zpos.zpos_quantity)),decode(zpos.zpos_cdt,5,0,zpos.zpos_sumupamount))) gross_s,
        sum(decode(zpos.zpos_cdt,5,decode(zpos.zpos_paidout,1,null,(zpos.zpos_unitprice*zpos.zpos_quantity)), null)) pay_s,
        sum(decode(zpos.zpos_cdt,5,decode(zpos.zpos_paidout,1,-zpos.zpos_unitprice*zpos.zpos_quantity,null),decode(zpos.zpos_cdt,5,0,zpos.zpos_sumupamount))) rev_s,
        zwin.zwin_id zwin_id_s,
        ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
        decode(nvl(zpos.zpos_zarr_id,0),0,1,0) indicate_zpos_id,
        decode(nvl(zpos.zpos_zarr_id,0),0,zpos.zpos_id,zpos.zpos_zarr_id) zarr_or_zpos_s
    FROM
    zorg,
        zpos
        INNER JOIN zdco ON zpos.zpos_zdco_id = zdco.zdco_id
        LEFT JOIN zwin ON zpos.zpos_zwin_id = zwin.zwin_id
    WHERE
    ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
        (zpos.zpos_zall_id IS NULL OR zpos.zpos_zall_id=0) AND
        zpos.zpos_id = zpos.zpos_internalsplit_zpos_id AND
        nvl(zpos.zpos_hidden,0)=0
        AND nvl(zpos.zpos_noreprint,0)<>1
    GROUP BY
        zwin.zwin_id,
        ZWIN.ZWIN_INVOICE_TO_XCMS_ID,
        zpos.zpos_postdate,
        decode(nvl(zpos.zpos_zarr_id,0),0,zpos.zpos_id,zpos.zpos_zarr_id),
        decode(nvl(zpos.zpos_zarr_id,0),0,1,0),
        decode(zpos.zpos_cdt,5,-1,1)*zpos.zpos_sumupamount/zpos.zpos_quantity
    )
WHERE
    abs(nvl(rev_s,pay_s))<>(decode((SELECT wuss.wuss_value FROM wuss WHERE wuss.wuss_xcms_id=0 AND wuss.wuss_name='Show 0 Postings on Billing Screen'),1,-1,0))
ORDER BY
 CDT ASC;



-- Views to show Tax Details

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_TAX_DET
AS 
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,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S) 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
--and Z1.ZPOS_ZWIN_ID=1480
);


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,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S) 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,
(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,
(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,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(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_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
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,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAC_TAX,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(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_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
from
zpos Z1
where 
Z1.ZPOS_CDT=2
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
);


-- Views to create Folios sorted by originated Reservation or FA

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,
  -- Quantity makes no sense in this Style decode((select zpos.ZPOS_PAIDOUT from zpos where zpos.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(select zpos.ZPOS_QUANTITY from zpos where zpos.ZPOS_ID=ZPOS_ID_S) 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,
  --(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) RES_GUEST_NAME,
  --(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) FA_GUEST_NAME,
  (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((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,
  ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
  INDICATE_ZPOS_ID, 
  ZPOS_DEPOSIT_ZINV_ID, --new field 10.07.2012
  ZPOS_DEPOSIT, --new field 10.07.2012
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  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, -- New Field 10.07.2012
  nvl(ntd.ZPOS_DEPOSIT_ZINV_ID,0) ZPOS_DEPOSIT_ZINV_ID -- New Field for DEPOSIT FLAG 10.07.2012
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_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
--  distinct
  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,-- New Field 10.07.2012
  nvl(ntd.ZPOS_DEPOSIT_ZINV_ID,0) ZPOS_DEPOSIT_ZINV_ID -- New Field for DEPOSIT FLAG 10.07.2012
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_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;

--Recommended statement to use in Folio
-- select * from V8_ED_FOLIO_REV_ORI_ARR_PD where V8_ED_FOLIO_REV_ORI_ARR_PD.ZWIN_ID=:ZWIN_ID order by V8_ED_FOLIO_REV_ORI_ARR_PD.DAT,V8_ED_FOLIO_REV_ORI_ARR_PD.IS_FA,V8_ED_FOLIO_REV_ORI_ARR_PD.ORI_RES_FA_DESC, V8_ED_FOLIO_REV_ORI_ARR_PD.CDT,V8_ED_FOLIO_REV_ORI_ARR_PD.ZDCO_NUMERICDESC


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,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID,
(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,
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
) 
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,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID,
(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,
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
);



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,
  -- Quantity makes no sense in this Style decode((select zpos.ZPOS_PAIDOUT from zpos where zpos.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(select zpos.ZPOS_QUANTITY from zpos where zpos.ZPOS_ID=ZPOS_ID_S) 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,
  --(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) RES_GUEST_NAME,
  --(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) FA_GUEST_NAME,
  (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((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,
   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 (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC,
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 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
--  distinct
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  1 IS_RES,
  0 IS_FA,
  ntd.ZPOS_ORIGINATED_YRES_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  (SELECT YRES.YRES_EXPARRTIME FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) ARRIVAL,
  (SELECT YRES.YRES_EXPDEPTIME FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  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
--  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,
  (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_S2 ZWIN_ID,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAX_CODE,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(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,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_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
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,
SPLITID_S2 SPLITID,
TAX_CODE_S2 TAC_TAX,
GROSS_REV_S2 GROSS_REV,
S_NET NET_REV,
S_TAX TAX,
(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,
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, 
--(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 nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
);


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((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=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_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=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_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=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_FOLIOTEXT 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 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,
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,
(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,
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,
(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,
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,
(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,
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,
 --decode((select zpos.ZPOS_PAIDOUT from zpos where zpos.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(select zpos.ZPOS_QUANTITY from zpos where zpos.ZPOS_ID=ZPOS_ID_S) QTY,
 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,
  --(nvl2(decode(DEPARTURE,0,null, DEPARTURE),1,'-')) MINUS1,
  (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,
  --(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) RES_GUEST_NAME,
  --(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) FA_GUEST_NAME,
  (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((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZDCO_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=ZDCO_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZDCO_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZDCO_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=ZDCO_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZDCO_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZDCO_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=ZDCO_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZDCO_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZDCO_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=ZDCO_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=ZDCO_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZDCO_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZDCO_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZDCO_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZDCO_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=ZDCO_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZDCO_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZDCO_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=ZDCO_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZDCO_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZDCO_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZDCO_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=ZDCO_OR_ZPOS_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 1 else 0 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 1 else 0 end)) MULTI_TAXCODE_EXISTS,
  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)--,decode(nvl(ntd.ZPOS_ZDCO_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZDCO_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,
 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))--,decode(nvl(ntd.ZPOS_ZDCO_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_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_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,
decode (TAXCOUNT,3,0,S_NET) NET_REV,
S_TAX TAX,
(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,
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,
decode (TAXCOUNT,3,0,S_NET) NET_REV,
S_TAX TAX,
(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,
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 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,
Sum(Net_rev) NET_REV,
sum(TAX) TAX,
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,
(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,
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,
(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,
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,
(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,
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 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
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,
(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,
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,
(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,
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
);



--View to summarize all postings per guest per stay, description is hardcoded (required for special tour operator invoice)

CREATE OR REPLACE FORCE VIEW V8_ED_FOLIO_REV_ORI_TOUR_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,
  -- Quantity makes no sense in this Style decode((select zpos.ZPOS_PAIDOUT from zpos where zpos.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(select zpos.ZPOS_QUANTITY from zpos where zpos.ZPOS_ID=ZPOS_ID_S) 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,
  --(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) RES_GUEST_NAME,
  --(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) FA_GUEST_NAME,
  (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,(Wtrl_Translate('ZARR',(DECODE(INDICATE_ZPOS_ID,0,(SELECT ZARR.ZARR_ID  FROM ZARR WHERE ZARR.ZARR_ID=ZARR_OR_ZPOS_S),0)),'ZARR_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)))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))),
  --(CASE WHEN (((SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))))=(SELECT ZDCO.ZDCO_LONGDESC FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))))
  --OR (SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=TRUNC(ZPOS_POSTDATE_S) AND YDET.YDET_YRES_ID=(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))) AND ROWNUM=1))
  --THEN
  --(DECODE(
  --(SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))),
  --(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=TRUNC(ZPOS_POSTDATE_S) AND YDET.YDET_YRES_ID=(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))) AND ROWNUM=1)),
  --DECODE(NVL((SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=TRUNC(ZPOS_POSTDATE_S) AND YDET.YDET_YRES_ID=(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))) AND ROWNUM=1)),0),0,'',Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=TRUNC(ZPOS_POSTDATE_S) AND YDET.YDET_YRES_ID=(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))) AND ROWNUM=1)),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(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)))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))),
  --(DECODE(NVL((SELECT ZDCO.ZDCO_FOLIOTEXT FROM ZDCO WHERE ZDCO.ZDCO_ID=(SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))),' '),' ',
  --DECODE(NVL((SELECT ZDCO.ZDCO_ID FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))),0),0,' ',(Wtrl_Translate('ZDCO',(SELECT ZDCO.ZDCO_ID FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_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)))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))),
  --DECODE(NVL((SELECT ZDCO.ZDCO_ID FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))),0),0,' ',(Wtrl_Translate('ZDCO',(SELECT ZDCO.ZDCO_ID FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_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)))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))))
  --))))
  --ELSE (SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))) END)
  --)) DDESC,
  'Total' 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 (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC,
  --decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 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
--  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,
  (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
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, 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
--  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,
  (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, 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_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_FOLIOTEXT 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 
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 
--  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_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_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC 
FROM 
  ZPOS, 
  ZWIN, 
  ZDCO,
  ZORG 
WHERE 
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND 
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND 
  zdco_id=ZPOS.zpos_zdco_id AND 
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND 
  NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND 
  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));


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((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_FOLIOTEXT 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 
--  distinct 
-- statement to get all Postings with ZDCO_SUBFUNCTION=1 and ZPOS_CDT=1 sum per amount 
  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 
--  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,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(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC,  
   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((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_FOLIOTEXT 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 
--  distinct 
-- statement to get all Postings with ZDCO_SUBFUNCTION=1 and ZPOS_CDT=1 sum per amount 
  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 
--  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,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(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC, 
   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,
  --(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) RES_GUEST_NAME,
  --(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) FA_GUEST_NAME,
  (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((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,
  ZCHK_OR_ZPOS,
  ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
  INDICATE_ZPOS_ID,
  GUEST_CHECK_NUM, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  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
SELECT
--  distinct
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  0 IS_RES,
  1 IS_FA,
  ntd.ZPOS_ORIGINATED_ZFAC_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
    ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  (SELECT ZFAC.ZFAC_EXPVALIDFROM FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) ARRIVAL,
  (SELECT ZFAC.ZFAC_EXPVALIDUNTIL FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  ntd.ZPOS_POSCHECK_NR GUEST_CHECK_NUM,
  to_number(null) ZCHK_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
  --AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,  ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSCHECK_NR,  ntd.ZPOS_POSTDATE,ntd.ZPOS_ORIGINATED_ZFAC_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID),DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
) WHERE  GUEST_CHECK_NUM is null
union all
SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  nvl2((to_char( DEPARTURE,'J')),'-',NULL) MINUSI,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZPOS_INTERNALGROUP_ID_S ZPOS_INTERNALGROUP_ID,
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  IS_RES,
  IS_FA,
  ORI_RES_FA_ID,
  ARRIVAL,
  DEPARTURE,
  ZFAC_OR_YRES,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S) ZDCO_NUMERICDESC,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
    (DECODE(IS_FA,0,(SELECT V8_REP_NAME.NAME6 FROM YRES,V8_REP_NAME WHERE YRES.YRES_ID=ORI_RES_FA_ID AND YRES.YRES_XCMS_ID=V8_REP_NAME.XCMS_ID),(SELECT V8_REP_NAME.NAME6 FROM ZFAC,V8_REP_NAME WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID AND ZFAC.ZFAC_XCMS_ID=V8_REP_NAME.XCMS_ID))) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  (DECODE(IS_FA,0,(NVL((SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID),(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE+1=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID))),(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ORI_RES_FA_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID))))) ORI_RES_FA_DESC,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
  decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=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_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=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_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=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_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S))))
          ) DDESC,
  to_number(ZCHK_OR_ZPOS_S) ZCHK_OR_ZPOS, 
  ZARR_OR_ZPOS,
  INDICATE_ZPOS_ID, 
  (DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) GUEST_CHECK_NUM,
 decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC, 
  --decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=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,
 -- 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 and Z5.ZPOS_INTERNALGROUP_ID = Z3.ZPOS_INTERNALGROUP_ID)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  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,
to_number(null) ZARR_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=NTD.ZPOS_ID and
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)=0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,  ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_ORIGINATED_YRES_ID, to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0),DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) --,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
UNION ALL
SELECT
--  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,
 to_number(null) ZARR_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
 -- AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,   ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_ORIGINATED_ZFAC_ID, to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0),DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) --,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
)
where 
(DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) is not null
)order by CDT ASC, DAT;



CREATE OR REPLACE VIEW V8_ED_FOL_ORI_GCHK_ARR_PS
AS 
(SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  nvl2((to_char( DEPARTURE,'J')),'-',NULL) MINUSI,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZPOS_INTERNALGROUP_ID_S ZPOS_INTERNALGROUP_ID,
  -- Quantity makes no sense in this Style decode((select zpos.ZPOS_PAIDOUT from zpos where zpos.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(select zpos.ZPOS_QUANTITY from zpos where zpos.ZPOS_ID=ZPOS_ID_S) 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,
  --(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) RES_GUEST_NAME,
  --(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) FA_GUEST_NAME,
  (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((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select YPAC_FOLIOTEXT from YPAC where YPAC_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,
  ZCHK_OR_ZPOS,
  ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
  INDICATE_ZPOS_ID,
  GUEST_CHECK_NUM, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  1 IS_RES,
  0 IS_FA,
  ntd.ZPOS_ORIGINATED_YRES_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  (SELECT YRES.YRES_EXPARRTIME FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) ARRIVAL,
  (SELECT YRES.YRES_EXPDEPTIME FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
   ntd.ZPOS_POSCHECK_NR GUEST_CHECK_NUM,
  to_number(null) ZCHK_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)=0
  --AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSCHECK_NR,  ntd.ZPOS_ORIGINATED_YRES_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
UNION ALL
SELECT
--  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,
  (SELECT ZFAC.ZFAC_EXPVALIDFROM FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) ARRIVAL,
  (SELECT ZFAC.ZFAC_EXPVALIDUNTIL FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  ntd.ZPOS_POSCHECK_NR GUEST_CHECK_NUM,
  to_number(null) ZCHK_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
--  AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID,ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSCHECK_NR,  ntd.ZPOS_ORIGINATED_ZFAC_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID),DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
) WHERE  GUEST_CHECK_NUM is null
union all
SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  nvl2((to_char( DEPARTURE,'J')),'-',NULL) MINUSI,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZPOS_INTERNALGROUP_ID_S ZPOS_INTERNALGROUP_ID,
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  IS_RES,
  IS_FA,
  ORI_RES_FA_ID,
  ARRIVAL,
  DEPARTURE,
  ZFAC_OR_YRES,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S) ZDCO_NUMERICDESC,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
    (DECODE(IS_FA,0,(SELECT V8_REP_NAME.NAME6 FROM YRES,V8_REP_NAME WHERE YRES.YRES_ID=ORI_RES_FA_ID AND YRES.YRES_XCMS_ID=V8_REP_NAME.XCMS_ID),(SELECT V8_REP_NAME.NAME6 FROM ZFAC,V8_REP_NAME WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID AND ZFAC.ZFAC_XCMS_ID=V8_REP_NAME.XCMS_ID))) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  (DECODE(IS_FA,0,(NVL((SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID),(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE+1=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID))),(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ORI_RES_FA_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID))))) ORI_RES_FA_DESC,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
  decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=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_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select yrch_LONGDESC from yrch where yrch_id=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('YRCH',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YRCH',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_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=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_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select YPAC_LONGDESC from YPAC where YPAC_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('YPAC',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=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 S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YPAC',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=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_FOLIOTEXT from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S))))
          ) DDESC,
  to_number(ZCHK_OR_ZPOS_S) ZCHK_OR_ZPOS, 
  ZARR_OR_ZPOS,
  INDICATE_ZPOS_ID, 
  (DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) GUEST_CHECK_NUM,
 decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZCHK_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_POSCHECK_NR=ZCHK_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC, 
  --decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=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,
 -- 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 and Z5.ZPOS_INTERNALGROUP_ID = Z3.ZPOS_INTERNALGROUP_ID)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  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,
to_number(null) ZARR_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)=0
--AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_ORIGINATED_YRES_ID, to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0),DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) --,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
UNION ALL
SELECT
--  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,
 to_number(null) ZARR_OR_ZPOS,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
 -- AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_ORIGINATED_ZFAC_ID, to_char(DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR)), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0),DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) --,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
)
where 
(DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) is not null
)
order by CDT ASC, DAT;



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


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

  
