--LAST UPDATE SCRIPT CONTAINING ONLY VIEWS THAT HAVE CHANGED SINCE PREVIOUS RELEASE

CREATE OR REPLACE FUNCTION V8_FNC_YEBS_YFSC_REP_INFOS
(
  YEVMID IN NUMBER,
  INCL_IN_YPKD in NUMBER,     -- 1 incl package 0  for not incl package,  3 for all
  FSOPTION in NUMBER          -- 0 original function space; 1 - alternatives;  2 - function space generic name 
) return varchar2 as
  ALL_YFSC varchar2(500);

  cursor YEBS_W_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       YFSC.YFSC_GENERIC_NAME,
       yebs.YEBS_ALTERNATIVE,
       v8_fnc_ccm_alternatives('YEBS', yebs.YEBS_ID, 0, 1) Alternatives,
       yevm.YEVM_ALTERNATIVE
     FROM
       yebs,yfsc,yevm
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID
       and nvl(yebs.YEBS_PKGINCL,0)=1
           and yevm.yevm_id=yebs.yebs_yevm_id
       and yevm.YEVM_ID=YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YFSC.YFSC_LONGDESC;


  cursor YEBS_WO_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       YFSC.YFSC_GENERIC_NAME,
       yebs.YEBS_ALTERNATIVE,
       v8_fnc_ccm_alternatives('YEBS', yebs.YEBS_ID, 0, 1) Alternatives,
       yevm.YEVM_ALTERNATIVE
     FROM
       yebs,yfsc,yevm
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID
       and nvl(yebs.YEBS_PKGINCL,0)=0
           and yevm.yevm_id=yebs.yebs_yevm_id
       and yevm.YEVM_ID=YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YFSC.YFSC_LONGDESC;


   cursor YEBS_ALL_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       YFSC.YFSC_GENERIC_NAME,
       yebs.YEBS_ALTERNATIVE,
       v8_fnc_ccm_alternatives('YEBS', yebs.YEBS_ID, 0, 1) Alternatives,
       yevm.YEVM_ALTERNATIVE
     FROM
       yebs,yfsc,yevm
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID
       and nvl(yebs.YEBS_PKGINCL,0) in (0,1)
           and yevm.yevm_id=yebs.yebs_yevm_id
       and yevm.YEVM_ID=YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YFSC.YFSC_LONGDESC;

       
       
  YEBS_W_P_VALUE YEBS_W_P_CURSOR%rowtype;

  YEBS_WO_P_VALUE YEBS_WO_P_CURSOR%rowtype;
  
  YEBS_ALL_VALUE YEBS_ALL_CURSOR%rowtype;

begin
    ALL_YFSC := '';
    IF INCL_IN_YPKD=1 THEN
    begin
        open YEBS_W_P_CURSOR;
        loop
              fetch YEBS_W_P_CURSOR
            into YEBS_W_P_VALUE;
        exit when YEBS_W_P_CURSOR%notfound;
            
            if FSOPTION=0 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_W_P_VALUE.YFSC_LONGDESC;
            end if;    
            
             if FSOPTION=2 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_W_P_VALUE.YFSC_GENERIC_NAME;
            end if;
            

            if (YEBS_W_P_VALUE.yevm_alternative=0) and (YEBS_W_P_VALUE.yebs_alternative=1) then
                if FSOPTION=0 then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                if YEBS_W_P_VALUE.alternatives IS NULL then
                    ALL_YFSC := ALL_YFSC||'(alternative)';
                else
                    ALL_YFSC := ALL_YFSC||'Alternative of'||' '||YEBS_W_P_VALUE.alternatives;
                end if;
            end if;

        end loop;
               exception
                when no_data_found then null;
                when others then null;
        close YEBS_W_P_CURSOR;
    end;
    END IF;

    IF INCL_IN_YPKD=0 THEN
    begin
        open YEBS_WO_P_CURSOR;
        loop
              fetch YEBS_WO_P_CURSOR
            into YEBS_WO_P_VALUE;
        exit when YEBS_WO_P_CURSOR%notfound;
            
            if FSOPTION=0 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_WO_P_VALUE.YFSC_LONGDESC;
            end if;
            
            if FSOPTION=2 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_WO_P_VALUE.YFSC_GENERIC_NAME;
            end if;
            
            if (YEBS_WO_P_VALUE.yevm_alternative=0) and (YEBS_WO_P_VALUE.yebs_alternative=1) then
                if FSOPTION=0 then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                if YEBS_WO_P_VALUE.alternatives IS NULL then
                    ALL_YFSC := ALL_YFSC||'(alternative)';
                else
                    ALL_YFSC := ALL_YFSC||'Alternative of'||' '||YEBS_WO_P_VALUE.alternatives;
                end if;
            end if;

        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_WO_P_CURSOR;
    end;
    END IF;
      
    IF INCL_IN_YPKD=3 THEN
    begin
        open YEBS_ALL_CURSOR;
        loop
              fetch YEBS_ALL_CURSOR
            into YEBS_ALL_VALUE;
        exit when YEBS_ALL_CURSOR%notfound;

            if FSOPTION=0 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_ALL_VALUE.YFSC_LONGDESC;
            end if;
            
            if FSOPTION=2 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_ALL_VALUE.YFSC_GENERIC_NAME;
            end if;


            if (YEBS_ALL_VALUE.yevm_alternative=0) and (YEBS_ALL_VALUE.yebs_alternative=1) then
                if FSOPTION=0 then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                if YEBS_ALL_VALUE.alternatives IS NULL then
                    ALL_YFSC := ALL_YFSC||'(alternative)';
                else
                    ALL_YFSC := ALL_YFSC||'Alternative of'||' '||YEBS_ALL_VALUE.alternatives;
                end if;
            end if;

        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_ALL_CURSOR;
    end;
    END IF;

     RETURN ALL_YFSC;

end V8_FNC_YEBS_YFSC_REP_INFOS;
/


CREATE OR REPLACE VIEW V8_REP_BILL_NOTAXDETAIL
AS 
SELECT 
  distinct 
  ZWIN.ZWIN_ID ZWIN_ID, 
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE, 
  ntd.ZPOS_CDT ZPOS_CDT, 
  nvl(ntd.ZPOS_YRES_ID,0) ZPOS_YRES_ID, 
  nvl(ntd.ZPOS_ZFAC_ID,0) ZPOS_ZFAC_ID, 
  ntd.ZPOS_XCMS_ID ZPOS_XCMS_ID,
  rpad((decode(nvl(zdco.ZDCO_FOLIOTEXT,' '),' ',WTRL_TRANSLATE('ZDCO',zdco.ZDCO_ID,'ZDCO_LONGDESC',nvl(nvl((decode((select xcca.XCCA_XCMSTYPE from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID),2,(select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID),(select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_CONTACT_ID))),(select wlan.WLAN_ID from wlan where wlan.WLAN_DEFAULT=1)),(select wlan.WLAN_ID from wlan where wlan.WLAN_DEFAULT=1))),WTRL_TRANSLATE('ZDCO',zdco.ZDCO_ID,'ZDCO_FOLIOTEXT',nvl((decode((select xcca.XCCA_XCMSTYPE from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID),2,(select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID),(select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_CONTACT_ID))),(select wlan.WLAN_ID from wlan where wlan.WLAN_DEFAULT=1))))),30,' ') ZDCO_LONGDESC,
   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(ntd.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)),ntd.ZPOS_DESCRIPT),decode(ntd.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(ntd.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)),ntd.ZPOS_DESCRIPT),decode(NTD.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(ntd.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)),NTD.ZPOS_DESCRIPT),decode(NTD.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)),NTD.ZPOS_DESCRIPT))
                                       )ZARR_LONGDESC,
  rpad((case when ((ntd.ZPOS_DESCRIPT=zdco.ZDCO_LONGDESC) or ntd.ZPOS_DESCRIPT=(select yrch.YRCH_FOLIOTEXT from yrch,ydet where yrch.YRCH_ID=ydet.YDET_YRCH_ID and ydet.ydet_date=(NTD.ZPOS_POSTDATE) and ydet.YDET_YRES_ID=ntd.ZPOS_ORIGINATED_YRES_ID and rownum=1))  then (decode(ntd.ZPOS_DESCRIPT,(select yrch.YRCH_FOLIOTEXT from yrch,ydet where yrch.YRCH_ID=ydet.YDET_YRCH_ID and ydet.ydet_date=(NTD.ZPOS_POSTDATE) and ydet.YDET_YRES_ID=ntd.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=(NTD.ZPOS_POSTDATE) and ydet.YDET_YRES_ID=ntd.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 ntd.ZPOS_DESCRIPT end),30) ZPOS_DESCRIPT,
  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)) GROSSAMOUNTJOINED, 
  DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL) PAYMENTS, 
  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)) NOTPAYMENTS, 
  zdco.ZDCO_ID ZDCO_ID, 
  zdco.ZDCO_NUMERICDESC ZDCO_NUMERICDESC, 
  zdco.ZDCO_PAYM_TYPE ZDCO_PAYM_TYPE, 
  zdco.ZDCO_STATS_TYPE ZDCO_STATS_TYPE, 
  ntd.ZPOS_PAIDOUT ZPOS_PAIDOUT, 
  zdco.ZDCO_CITYLEDGER ZDCO_CITYLEDGER, 
  zwin.ZWIN_WINDOW ZPOS_WINDOWNR, 
  zwin.ZWIN_WINDOW ZWIN_WINDOW, 
  zwin.ZWIN_FREETEXT1 ZWIN_FREETEXT1, 
  zwin.ZWIN_FREETEXT2 ZWIN_FREETEXT2, 
  zwin.ZWIN_FREETEXT3 ZWIN_FREETEXT3, 
  zwin.ZWIN_FREETEXT4 ZWIN_FREETEXT4, 
  decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, ntd.ZPOS_COMMENT,  getccmzposcommenttranslated(ntd.ZPOS_ID) ) ZPOS_COMMENT, 
  ntd.ZPOS_ID ZPOS_ID, 
  ntd.ZPOS_ORIGINATED_YRES_ID ZPOS_ORIGINATED_YRES_ID, 
  ntd.ZPOS_ORIGINATED_ZFAC_ID ZPOS_ORIGINATED_ZFAC_ID, 
  ntd.ZPOS_ORIGINATED_XCMS_ID ZPOS_ORIGINATED_XCMS_ID, 
  decode((select zpcf.ZPCF_CUSTOMORIGINATORNAME from zpcf where zpcf_zpos_id=ntd.ZPOS_ID),null,(case when(nvl(ntd.ZPOS_ORIGINATED_ZFAC_ID,0)=0) then (select trim(x.xcms_name1||', '||x.xcms_name3) from xcms x where x.xcms_id=ntd.ZPOS_ORIGINATED_XCMS_ID) else (select trim(x.xcms_name1||' '||x.xcms_name2) from xcms x where x.xcms_id=ntd.ZPOS_ORIGINATED_XCMS_ID) end)||', '||  (select yrms.YRMS_SHORTDESC from yrms,ydet where yrms.yrms_id=ydet.ydet_yrms_id and ydet.YDET_YRES_ID=ntd.ZPOS_ORIGINATED_YRES_ID and rownum=1),(select zpcf.ZPCF_CUSTOMORIGINATORNAME from zpcf where zpcf_zpos_id=ntd.ZPOS_ID)) ORIGINATED_GUEST_NAME, 
  (select yrms.YRMS_SHORTDESC from yrms,ydet where yrms.yrms_id=ydet.ydet_yrms_id and ydet.YDET_YRES_ID=ntd.ZPOS_ORIGINATED_YRES_ID and rownum=1) ORIGINATED_ROOM, 
  (select zfan.ZFAN_LONGDESC from zfan,zfac where zfan.ZFAN_ID=zfac.ZFAC_ZFAN_ID and zfac.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID and rownum=1) ORIGINATED_FA, 
  ntd.ZPOS_POSCHECK_NR ZPOS_POSCHECK_NR, 
  ntd.ZPOS_PHONENR ZPOS_PHONENR, 
  (select yres.YRES_EXPARRTIME from yres where yres.YRES_ID=ntd.ZPOS_YRES_ID) YRES_EXPARRTIME, 
  (select yres.YRES_EXPDEPTIME from yres where yres.YRES_ID=ntd.ZPOS_YRES_ID) YRES_EXPDEPTIME, 
  (select zfac.ZFAC_EXPVALIDFROM from zfac where zfac.ZFAC_ID=ntd.ZPOS_ZFAC_ID) ZFAC_EXPVALIDFROM, 
  (select zfac.ZFAC_EXPVALIDUNTIL from zfac where zfac.ZFAC_ID=ntd.ZPOS_ZFAC_ID) ZFAC_EXPVALIDUNTIL, 
  (select zfac.ZFAC_LONGDESC from zfac where zfac.ZFAC_ID=ntd.ZPOS_ZFAC_ID) ZFAN_LONGDESC, 
  zwin.ZWIN_INVOICE_TO_XCMS_ID XCMS_ID, 
  (select xcms.XCMS_NAME1 from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID) XCMS_NAME1, 
  (select xcms.XCMS_NAME2 from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID) XCMS_NAME2, 
  (select xcms.XCMS_NAME3 from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID) XCMS_NAME3, 
  (select xcms.XCMS_TYPE from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID) XCMS_TYPE, 
  (select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID) XCMS_WLAN_ID, 
  zwin.ZWIN_INVOICE_TO_CONTACT_ID CONTACT_XCMS_ID, 
  (select xcms.XCMS_NAME1 from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_CONTACT_ID) CONTACT_XCMS_NAME1, 
  (select xcms.XCMS_NAME2 from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_CONTACT_ID) CONTACT_XCMS_NAME2, 
  (select xcms.XCMS_NAME3 from xcms where xcms.XCMS_ID=zwin.ZWIN_INVOICE_TO_CONTACT_ID) CONTACT_XCMS_NAME3, 
  zwin.ZWIN_YRES_ID YRES_ID, 
  (select yres.YRES_XCMS_ID from yres where yres.YRES_ID=zwin.ZWIN_YRES_ID) YRES_XCMS_ID, 
  (select xcms.XCMS_NAME1 from xcms,yres where xcms.XCMS_ID=yres.YRES_XCMS_ID and yres.YRES_ID=zwin.ZWIN_YRES_ID) YRES_XCMS_NAME1, 
  (select xcms.XCMS_NAME3 from xcms,yres where xcms.XCMS_ID=yres.YRES_XCMS_ID and yres.YRES_ID=zwin.ZWIN_YRES_ID) YRES_XCMS_NAME3, 
  zwin.ZWIN_ZFAC_ID ZFAC_ID, 
  (select zfac.ZFAC_XCMS_ID from zfac where zfac.ZFAC_ID=zwin.ZWIN_ZFAC_ID) ZFAC_XCMS_ID, 
  (select xcms.XCMS_NAME1 from xcms,zfac where xcms.XCMS_ID=zfac.ZFAC_XCMS_ID and zfac.ZFAC_ID=zwin.ZWIN_ZFAC_ID) ZFAC_XCMS_NAME1, 
  (select xcms.XCMS_NAME3 from xcms,zfac where xcms.XCMS_ID=zfac.ZFAC_XCMS_ID and zfac.ZFAC_ID=zwin.ZWIN_ZFAC_ID) ZFAC_XCMS_NAME3, 
  (select xcms.XCMS_NAME1 from xcms where xcms.XCMS_ID=ntd.ZPOS_ORIGINATED_XCMS_ID) GROUP_XCMS_NAME1, 
  (select xcms.XCMS_NAME3 from xcms where xcms.XCMS_ID=ntd.ZPOS_ORIGINATED_XCMS_ID) GROUP_XCMS_NAME3, 
  zwin.ZWIN_INVOICE_TO_XADR_ID XADR_ID, 
  (select xadr.XADR_ZIP from xadr where xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XADR_ZIP, 
  (select xadr.XADR_STREET1 from xadr where xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XADR_STREET1, 
  (select xadr.XADR_STREET2 from xadr where xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XADR_STREET2, 
  (select xadr.XADR_STREET3 from xadr where xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XADR_STREET3, 
  (select xadr.XADR_CITY from xadr where xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XADR_CITY, 
  (select xsta.XSTA_SHORTDESC from xsta,xadr where xsta.XSTA_ID=xadr.XADR_XSTA_ID and xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XSTA_SHORTDESC, 
  (select xsta.XSTA_LONGDESC from xsta,xadr where xsta.XSTA_ID=xadr.XADR_XSTA_ID and xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XSTA_LONGDESC, 
  (select yrms.YRMS_SHORTDESC from yrms,ydet,yres where yrms.YRMS_ID=ydet.YDET_YRMS_ID and ydet.YDET_ID=yres.YRES_CURRENT_YDET_ID and yres.YRES_ID=zwin.ZWIN_YRES_ID) YRMS_SHORTDESC, 
  (select xcou.XCOU_ISO2 from xcou,xadr where xcou.XCOU_ID=xadr.XADR_XCOU_ID and xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XCOU_ISO2, 
  (select xcou.XCOU_LONGDESC from xcou,xadr where xcou.XCOU_ID=xadr.XADR_XCOU_ID and xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XCOU_LONGDESC, 
  (select xcou.XCOU_CUSTSHORTDESC from xcou,xadr where xcou.XCOU_ID=xadr.XADR_XCOU_ID and xadr.XADR_ID=zwin.ZWIN_INVOICE_TO_XADR_ID) XCOU_CUSTSHORTDESC, 
  (select XMNR_VALUE from xmnr where XMNR.XMNR_XCMS_ID = zwin.ZWIN_INVOICE_TO_XCMS_ID AND XMNR.XMNR_XMTY_ROLE = 1 and rownum = 1) AACC_NUMBER, 
  (select XMNR_VALUE from xmnr where XMNR.XMNR_XCMS_ID = zwin.ZWIN_YRES_ID AND XMNR.XMNR_XMTY_ROLE = 1 and rownum = 1) AACC_NUMBER_2, 
  (select XMNR_VALUE from xmnr where XMNR.XMNR_XCMS_ID = zwin.ZWIN_ZFAC_ID AND XMNR.XMNR_XMTY_ROLE = 1 and rownum = 1) AACC_NUMBER_3, 
  ntd.ZPOS_ZALL_ID, 
  (select yrms.YRMS_SHORTDESC from yrms,ydet,yres where yrms.YRMS_ID=ydet.YDET_YRMS_ID and ydet.YDET_ID=yres.YRES_CURRENT_YDET_ID and yres.YRES_ID=ntd.ZPOS_ORIGINATED_YRES_ID) GRP_YRMS_SHORTDESC, 
  (select yres.YRES_ACTARRTIME from yres where yres.YRES_ID=ntd.ZPOS_ORIGINATED_YRES_ID) GRP_YRES_ACTARRTIME, 
  (select yres.YRES_ACTDEPTIME from yres where yres.YRES_ID=ntd.ZPOS_ORIGINATED_YRES_ID) GRP_YRES_ACTDEPTIME, 
  (select yres.YRES_EXPARRTIME from yres where yres.YRES_ID=ntd.ZPOS_ORIGINATED_YRES_ID) GRP_YRES_EXPARRTIME, 
  (select yres.YRES_EXPDEPTIME from yres where yres.YRES_ID=ntd.ZPOS_ORIGINATED_YRES_ID) GRP_YRES_EXPDEPTIME, 
  (select xcid.XCID_ADDRGREET from xcid where xcid.XCID_ID=zwin.ZWIN_INVOICE_TO_XCMS_ID) XCID_ADDRGREET, 
  (select xcid.XCID_ADDRGREET from xcid where xcid.XCID_ID=zwin.ZWIN_INVOICE_TO_CONTACT_ID) CONTACT_XCID_ADDRGREET, 
--  nvl((select zpil.ZPIL_ZINV_ID from zpil where zpil.ZPIL_ZPOS_ID= ntd.zpos_Id and zpil_id= (select max(z.zpil_id) from zpil z where z.zpil_zpos_id=ntd.zpos_id)),0) ZPIL_ID, 
  nvl(ntd.ZPOS_NOREPRINT,0) ZPIL_ID,
  nvl((select max(zpil_zinv_id) from zpil, zinv where zpil_zpos_id=ntd.zpos_id and zpil_zinv_id=zinv_id and nvl(zinv_void_zinv_id,0)=0),0) ZINV_ID, 
  nvl((select max(zinv_id) from zpil, zinv where zpil_zpos_id=ntd.zpos_id and zpil_zinv_id=zinv_id and zinv_void_zinv_id<>0),0) VOID_PRINT , 
  zdco.zdco_udef_num_1 zdco_udef_num_1, 
  zdco.zdco_udef_num_2 zdco_udef_num_2, 
  zdco.zdco_udef_num_3 zdco_udef_num_3, 
  zdco.zdco_udef_amnt_1 zdco_udef_amnt_1, 
  zdco.zdco_udef_amnt_2 zdco_udef_amnt_2, 
  zdco.zdco_udef_amnt_3 zdco_udef_amnt_3, 
  zdco.zdco_udef_CHAR_1 zdco_udef_CHAR_1, 
  zdco.zdco_udef_CHAR_2 zdco_udef_CHAR_2, 
  zdco.zdco_udef_CHAR_3 zdco_udef_CHAR_3, 
  ntd.ZPOS_SUMUPAMOUNT ZPOS_SUMUPAMOUNT, 
  ntd.ZPOS_QUANTITY ZPOS_QUANTITY, 
  ntd.ZPOS_LEDGERSTATUS ZPOS_LEDGERSTATUS, 
  (select XMNR_VALUE from xmnr where XMNR.XMNR_XCMS_ID = zwin.ZWIN_INVOICE_TO_XCMS_ID AND XMNR.XMNR_XMTY_ROLE = 3 and rownum = 1) VAT_NUMBER, 
  (select XMNR_VALUE from xmnr where XMNR.XMNR_XCMS_ID = zwin.ZWIN_INVOICE_TO_XCMS_ID AND XMNR.XMNR_XMTY_ROLE = 9 and rownum = 1) FISCAL_CODE,
  (select ztxc.ZTXC_LONGDESC from ztxc, zdtl where ztxc.ZTXC_ID = zdtl.ZDTL_ZTXC_ID and zdtl.ZDTL_ZDCO_ID = zdco.ZDCO_ID and rownum = 1) ZTXC_LONGDESC, 
  nvl(ntd.ZPOS_ZARR_ID, 0) ZARR_ID, 
  nvl(ntd.ZPOS_ZART_ID, 0) ZART_ID, 
  (select yres.YRES_FREETEXT1 from yres where yres.YRES_ID=zwin.ZWIN_YRES_ID) BIL_REF, 
  (select yrch.YRCH_ID from yrch,ydet,yres where ydet.YDET_DATE=ntd.zpos_postdate and yrch.Yrch_ID=ydet.YDET_Yrch_ID and yres.YRES_ID(+)=ntd.ZPOS_ORIGINATED_YRES_ID and ydet.YDET_YRES_ID=zpos_originated_yres_id) YRCH_ID,
  nvl(ntd.zpos_zudd_id,0) ZUDD_ID, 
  zudd.ZUDD_DESCRIPTION ZUDD_DESCRIPTION 
FROM 
  ZPOS ntd, 
  ZWIN ZWIN, 
  ZDCO, 
  ZUDD ,
  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 
  zudd_id(+)=ntd.ZPOS_ZUDD_ID;

  
CREATE OR REPLACE FORCE VIEW V8_REP_QUEST_REP_SUM
AS 
SELECT
         NVL((ROUND(SUM(PercAnswerValue) / COUNT(1),2)),0) AVGReached,
         NVL(ROUND(SUM(MaxAnswerValue),2),0) MaxAnswerSum,
         NVL(ROUND(SUM(AnswerValue),2),0) AnswerSum,
         qnrf_id,
         QNRF_GUEST_XCMS_ID XCMS_ID,
         QNRF_YRES_ID YRES_ID
FROM (
SELECT  qnrf_id,
        WQAN.WQAN_VALUE AnswerValue,
        (SELECT MAX(WQAN.WQAN_VALUE) FROM wqan WHERE WQAN.WQAN_WQUE_ID = wque_id) MaxAnswerValue,
        ((WQAN.WQAN_VALUE * 100) / decode((SELECT max(WQAN.WQAN_VALUE) FROM wqan WHERE WQAN.WQAN_WQUE_ID = wque_id),0,1,(SELECT max(WQAN.WQAN_VALUE) FROM wqan WHERE WQAN.WQAN_WQUE_ID = wque_id))) PercAnswerValue,
         QNRF.QNRF_ENTERED EnteredDate,
         QNRF.QNRF_GUEST_XCMS_ID,
         QNRF.QNRF_YRES_ID
  --,qnra.*, wqan.*, wque.*, wqnl.*, wqug.*, wqnr.*, qnrf.*
  FROM   qnra,
         qnrf,
         wqan,
         wque,
         wqnl,
         wqug,
         wqnr
 WHERE       WQAN.WQAN_ID(+) = QNRA.QNRA_WQAN_ID
         AND WQUE.WQUE_ID = WQNL.WQNL_WQUE_ID
         AND WQNL.WQNL_ID = QNRA.QNRA_WQNL_ID
         AND WQUG.WQUG_ID(+) = WQNL.WQNL_WQUG_ID
         AND WQNR.WQNR_ID = WQNL.WQNL_WQNR_ID
         AND QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID
    UNION
SELECT   qnrf_id,
             0 AnswerValue,
             0 MaxAnswerValue,
             0 PercAnswerValue,
             QNRF_ENTERED EnteredDate,
             QNRF_GUEST_XCMS_ID,
             QNRF_YRES_ID FROM qnrf qnrf_1
    WHERE NOT EXISTS (SELECT 1 FROM qnra WHERE qnra_qnrf_id = qnrf_1.qnrf_id)
         )
         GROUP BY qnrf_id,QNRF_GUEST_XCMS_ID,QNRF_YRES_ID;

CREATE OR REPLACE VIEW V8_REP_WDAT_DATE_YRAS_SOURCE
AS 
SELECT 
wdat.WDAT_DATE WDAT_DATE, 
yras.YRAS_ID TABLE_ID,
yras.YRAS_SHORTDESC YRAS_SHORTDESC,
yras.YRAS_LONGDESC YRAS_LONGDESC,
YRAS.YRAS_YRAG_ID YRAS_YRAG_ID, -- new Field 04.12.2013 NSchmalenberg
(case when wdat.WDAT_DATE<V8_sys_fideliodate then 0 else 1 end) FUTURE 
 FROM 
wdat,yras
WHERE yras.YRAS_YRAC_ID=1;

CREATE OR REPLACE VIEW V8_REP_WDAT_DATE_YRAS_CHANNEL
AS 
SELECT 
wdat.WDAT_DATE WDAT_DATE, 
yras.YRAS_ID TABLE_ID,
yras.YRAS_SHORTDESC YRAS_SHORTDESC,
yras.YRAS_LONGDESC YRAS_LONGDESC,
YRAS.YRAS_YRAG_ID YRAS_YRAG_ID,  -- new Field 04.12.2013 NSchmalenberg
(case when wdat.WDAT_DATE<V8_sys_fideliodate then 0 else 1 end) FUTURE 
 FROM 
wdat,yras
WHERE yras.YRAS_YRAC_ID=2;

CREATE OR REPLACE VIEW V8_REP_YBLD_OCC_YRAS_SOURCE
AS 
select
   V8_REP_YBLH_WITH_YRAS.YRAS_SOURCE_ID TABLE_ID,
   ybld_date,
   nvl(sum(decode(ybst_resstatus,1,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms),0)),0) defnoofrooms,
   nvl(sum(decode(ybst_resstatus,1,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms*ybld_noofadults),0)),0) defnoofadults,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms),0)),0) tentnoofrooms,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms*ybld_noofadults),0)),0) tentnoofadults,
   sum(decode(ybst_resstatus,1,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms),nvl(ybld_expectednoofrooms,0))) defnoofroomsbookingpos,
   sum(decode(ybst_resstatus,1,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms*nvl(ybld_noofadults,0)),nvl(ybld_expectednoofrooms,0)*nvl(ybld_noofadults,0))) defnoofadultsBookinPos,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,case when nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)>=YBLD.YBLD_EXPECTEDNOOFROOMS then 0 else  YBLD.YBLD_EXPECTEDNOOFROOMS-nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)end))),0) defnoofroomsbookingposavail,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,case when nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)>=YBLD.YBLD_EXPECTEDNOOFROOMS then 0 else  YBLD.YBLD_EXPECTEDNOOFROOMS-nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)end * YBLD.YBLD_NOOFADULTS ))),0) defnoofadultsbookingposavail,
   sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms-nvl(ybld_expectednoofrooms,0)),0)) tentnoofroomsbookingpos,
   sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,(ybld_actualnoofrooms-nvl(ybld_expectednoofrooms,0))*nvl(ybld_noofadults,0)),0)) tentnoofadultsBookingPos,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,case when nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)>=YBLD.YBLD_EXPECTEDNOOFROOMS then YBLD.YBLD_ACTUALNOOFROOMS-nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0) else YBLD.YBLD_ACTUALNOOFROOMS-YBLD.YBLD_EXPECTEDNOOFROOMS  end))),0) tentnoofroomsbookingposavail,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,case when nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)>=YBLD.YBLD_EXPECTEDNOOFROOMS then YBLD.YBLD_ACTUALNOOFROOMS-nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0) else YBLD.YBLD_ACTUALNOOFROOMS-YBLD.YBLD_EXPECTEDNOOFROOMS  end * YBLD.YBLD_NOOFADULTS))),0) tentnoofadultsbookingposavail
   from ybld,V8_REP_YBLH_WITH_YRAS,ybst,fideliodate d
where
    ybld_yblh_id=yblh_id
and YBLH_YBST_ID=ybst_id
and YBST_RESSTATUS<>3
group by V8_REP_YBLH_WITH_YRAS.YRAS_SOURCE_ID,ybld_date;


CREATE OR REPLACE VIEW V8_REP_YBLD_OCC_YRAS_CHANNEL
AS 
select
   V8_REP_YBLH_WITH_YRAS.YRAS_CHANNEL_ID TABLE_ID,
   ybld_date,
   nvl(sum(decode(ybst_resstatus,1,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms),0)),0) defnoofrooms,
   nvl(sum(decode(ybst_resstatus,1,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms*ybld_noofadults),0)),0) defnoofadults,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms),0)),0) tentnoofrooms,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms*ybld_noofadults),0)),0) tentnoofadults,
   sum(decode(ybst_resstatus,1,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms),nvl(ybld_expectednoofrooms,0))) defnoofroomsbookingpos,
   sum(decode(ybst_resstatus,1,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms*nvl(ybld_noofadults,0)),nvl(ybld_expectednoofrooms,0)*nvl(ybld_noofadults,0))) defnoofadultsBookinPos,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,case when nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)>=YBLD.YBLD_EXPECTEDNOOFROOMS then 0 else  YBLD.YBLD_EXPECTEDNOOFROOMS-nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)end))),0) defnoofroomsbookingposavail,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,case when nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)>=YBLD.YBLD_EXPECTEDNOOFROOMS then 0 else  YBLD.YBLD_EXPECTEDNOOFROOMS-nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)end * YBLD.YBLD_NOOFADULTS ))),0) defnoofadultsbookingposavail,
   sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,ybld_actualnoofrooms-nvl(ybld_expectednoofrooms,0)),0)) tentnoofroomsbookingpos,
   sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,(ybld_actualnoofrooms-nvl(ybld_expectednoofrooms,0))*nvl(ybld_noofadults,0)),0)) tentnoofadultsBookingPos,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,case when nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)>=YBLD.YBLD_EXPECTEDNOOFROOMS then YBLD.YBLD_ACTUALNOOFROOMS-nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0) else YBLD.YBLD_ACTUALNOOFROOMS-YBLD.YBLD_EXPECTEDNOOFROOMS  end))),0) tentnoofroomsbookingposavail,
   nvl(sum(decode(ybst_resstatus,0,decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE),-1,0,case when nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0)>=YBLD.YBLD_EXPECTEDNOOFROOMS then YBLD.YBLD_ACTUALNOOFROOMS-nvl((select sum(YDET.YDET_NOOFROOMS) from ydet,yres where YDET.YDET_YBLD_ID=ybld.ybld_id and YDET.YDET_YRES_ID=yres.yres_id and yres.yres_resstatus=1),0) else YBLD.YBLD_ACTUALNOOFROOMS-YBLD.YBLD_EXPECTEDNOOFROOMS  end * YBLD.YBLD_NOOFADULTS))),0) tentnoofadultsbookingposavail
   from ybld,V8_REP_YBLH_WITH_YRAS,ybst,fideliodate d
where
    ybld_yblh_id=yblh_id
and YBLH_YBST_ID=ybst_id
and YBST_RESSTATUS<>3
group by V8_REP_YBLH_WITH_YRAS.YRAS_CHANNEL_ID,ybld_date;


CREATE OR REPLACE VIEW V8_REP_YPOS_FC_SOURCE
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_SOURCE_ID TABLE_ID,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET, 
sum(case when YPOS_DT=1 and TYPENUM in (0,2,4) and zdco_stats_type=1 then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) DEF_RM_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (1,3,5) and zdco_stats_type=1 then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) TENT_RM_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (0,2,4) and zdco_stats_type=2 then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) DEF_FB_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (1,3,5) and zdco_stats_type=3 then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) TENT_FB_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (0,2,4) and zdco_stats_type not in (1,2) then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) DEF_OTHER_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (1,3,5) and zdco_stats_type not in (1,2) then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) TENT_OTHER_REV_NET,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS, --NEW 04.12.13 NSchmalenberg
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, --NEW 04.12.13 NSchmalenberg
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, --NEW 04.12.13 NSchmalenberg
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  --NEW 04.12.13 NSchmalenberg
FROM V8_REP_REV_FORECAST 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_SOURCE_ID;


CREATE OR REPLACE VIEW V8_REP_YPOS_FC_CHANNEL
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_CHANNEL_ID TABLE_ID,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET, 
sum(case when YPOS_DT=1 and TYPENUM in (0,2,4) and zdco_stats_type=1 then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) DEF_RM_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (1,3,5) and zdco_stats_type=1 then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) TENT_RM_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (0,2,4) and zdco_stats_type=2 then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) DEF_FB_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (1,3,5) and zdco_stats_type=3 then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) TENT_FB_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (0,2,4) and zdco_stats_type not in (1,2) then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) DEF_OTHER_REV_NET,
sum(case when YPOS_DT=1 and TYPENUM in (1,3,5) and zdco_stats_type not in (1,2) then nvl(YPOS_QUANTITY*YPOS_UNITPRICE,0) else 0 end) TENT_OTHER_REV_NET,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS, --NEW 04.12.13 NSchmalenberg
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, --NEW 04.12.13 NSchmalenberg
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, --NEW 04.12.13 NSchmalenberg
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  --NEW 04.12.13 NSchmalenberg
FROM V8_REP_REV_FORECAST 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_CHANNEL_ID;


--History and Forecast by Source Code

CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_S_STATYPE1
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_SOURCE_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (1) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_SOURCE_ID;

CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_S_STATYPE2
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_SOURCE_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (2) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_SOURCE_ID;


CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_S_STATYPE3
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_SOURCE_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (3) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_SOURCE_ID;


CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_S_STATYPE4
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_SOURCE_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (4) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_SOURCE_ID;


CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_S_STATYPE5
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_SOURCE_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (5) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_SOURCE_ID;


CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_S_STATY345
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_SOURCE_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (3,3,5) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_SOURCE_ID;


CREATE OR REPLACE VIEW V8_REP_HS_ARR_ALLADULT_SOURCE
AS 
select wdat_date,yral_yras_id,sum(allexpadult) allexpadult,sum(defexpadult) defexpadult,sum(tentexpadult) tentexpadult  from (
select 
   wdat_date
   ,YRAL.YRAL_YRAS_ID
   ,sum(ydet_adultno*ydet_noofrooms) allexpadult
   ,decode(yres_resstatus,1,sum(ydet_adultno*ydet_noofrooms)) defexpadult
   ,decode(yres_resstatus,0,sum(ydet_adultno*ydet_noofrooms)) tentexpadult
from wdat,ydet,yres,ycat,YRAL
where
    ydet_date=wdat_date 
    and YRAL_YDET_ID=YDET_ID
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and YRAL.YRAL_YRAC_ID=1
group by wdat_date,yres_resstatus,YRAL.YRAL_YRAS_ID
) group by wdat_date,YRAL_YRAS_ID;


CREATE OR REPLACE VIEW V8_REP_HS_ARR_ALLCHILD_SOURCE
AS 
select wdat_date, YRAL_YRAS_ID YRAS_ID, nvl(sum(allarrchild),0) allarrchild,nvl(sum(defarrchild),0) defarrchild,nvl(sum(tentarrchild),0) tentarrchild 
   ,nvl(sum(allarrdomesticchild),0) allarrdomesticchild,nvl(sum(defarrdomesticchild),0) defarrdomesticchild,nvl(sum(tentarrdomesticchild),0) tentarrdomesticchild           
   ,nvl(sum(allarrfreeofchargechild),0) allarrfreeofchargechild,nvl(sum(defarrfreeofchargechild),0) defarrfreeofchargechild,nvl(sum(tentarrfreeofchargechild),0) tentarrfreeofchargechild    
from
 (
select 
   wdat_date
   ,YRAL_YRAS_ID
   ,sum(yrgl_number*ydet_noofrooms) allarrchild
   ,sum(decode(xcac_domestic,1,yrgl_number*ydet_noofrooms,0)) allarrdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*ydet_noofrooms))) defarrchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*ydet_noofrooms))) tentarrchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*ydet_noofrooms,0))) defarrdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*ydet_noofrooms,0))) tentarrdomesticchild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*ydet_noofrooms,0)) allarrfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*ydet_noofrooms,0))) defarrfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*ydet_noofrooms,0))) tentarrfreeofchargechild
from wdat,ydet,yres,yrgl,xcac,ycat, YRAL
where
    ydet_date=wdat_date and
    YRAL_YDET_ID=YDET_ID
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrgl_ydet_id=ydet_id
and yrgl_xcac_id=xcac_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and YRAL.YRAL_YRAC_ID=1
group by wdat_date,yres_resstatus,YRAL_YRAS_ID
) group by wdat_date,YRAL_YRAS_ID;


CREATE OR REPLACE VIEW V8_REP_SOURCE_HISTFOR
AS 
SELECT 
STAT_T.WDAT_DATE WDAT_DATE, 
STAT_T.WDAT_DATE WDAT_DATE2, 
STAT_T.WDAT_DATE WDAT_DATE3, 
STAT_T.TABLE_ID TABLE_ID,
STAT_T.YRAS_LONGDESC YRAS_LONGDESC,
STAT_T.YRAS_SHORTDESC YRAS_SHORTDESC, 
(SELECT YRAS.YRAS_DISPLORDER FROM YRAS WHERE YRAS.YRAS_ID =STAT_T.TABLE_ID) YRAS_DISPLORDER,
STAT_T.YRAS_YRAG_ID YRAS_YRAG_ID, 
(select YRAG.YRAG_DISPLORDER from YRAG where YRAG.YRAG_ID=STAT_T.YRAS_YRAG_ID) YRAG_DISPLAYORDER,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_SOURCE.DEFNOOFROOMS from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_OCC_WB_SOURCE.DEFNOOFROOMS from V8_REP_YDET_OCC_WB_SOURCE where V8_REP_YDET_OCC_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(decode(shis. SHIS_YRES_RESSTATUS,3,0,decode(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_NOOFROOMS))) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and SHIS.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
DEF_NOOFRROMS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_SOURCE.DEFNOOFROOMS from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl(( select V8_REP_YBLD_OCC_YRAS_SOURCE.DEFNOOFROOMSBOOKINGPOSAVAIL from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_OCC_WB_SOURCE.DEFNOOFROOMS from V8_REP_YDET_OCC_WB_SOURCE where V8_REP_YDET_OCC_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(decode(shis.SHIS_YRES_RESSTATUS,3,0,decode(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_NOOFROOMS))) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and SHIS.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
 DEF_NOOFRROMS_BOOKINGPOS, 
(decode(STAT_T.FUTURE,1, 
(nvl((select allexproom from V8_SYS_HS_ARR_ALLROOM_SOURCE V where V.SOURCE_ID=STAT_T.TABLE_ID and V.WDAT_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(decode(SHIS.SHIS_YRES_RESSTATUS,3,0, decode(shis.SHIS_ARR_ID,1,shis.SHIS_YDET_NOOFROOMS,0))) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
ARRIVAL_NOOFROOMS,
(decode(STAT_T.FUTURE,1, 
(nvl((select  defexproom from v8_sys_hs_arr_allroom_Source v where V.SOURCE_ID=STAT_T.TABLE_ID and V.WDAT_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(decode(shis.SHIS_YRES_RESSTATUS,1,decode(shis.SHIS_ARR_ID,1,shis.SHIS_YDET_NOOFROOMS,0),0)) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
ARRIVAL_NOOFROOMS_def,
(decode(STAT_T.FUTURE,1,
(nvl((select  alldeproom from V8_SYS_HS_DEP_ALLROOM_SOURCE v where V.SOURCE_ID=STAT_T.TABLE_ID and V.WDAT_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(det.YDET_NOOFROOMS) from v8_rep_ydet_infos det, yres where DET.SOURCE_ID=STAT_T.TABLE_ID and det.YDET_ID = yres.YRES_LAST_YDET_ID  and yres.YRES_EXPDEPTIME=STAT_T.WDAT_DATE and yres.YRES_RESSTATUS <> 3 and nvl(trunc(yres.YRES_ACTDEPTIME), yres.YRES_EXPDEPTIME) = STAT_T.WDAT_DATE),0)))) 
 DEPARTURE_NOOFROOMS,
(decode(STAT_T.FUTURE,1,
(nvl((select defdeproom from v8_sys_hs_dep_allroom_SOURCE v where v.SOURCE_id= STAT_T.TABLE_ID and v.wdat_date=STAT_T.WDAT_DATE),0)) ,
(nvl((select sum(det.YDET_NOOFROOMS) from v8_rep_ydet_infos det, yres where DET.SOURCE_ID = STAT_T.TABLE_ID AND det.YDET_ID = yres.YRES_LAST_YDET_ID and yres.YRES_EXPDEPTIME=STAT_T.WDAT_DATE and yres.YRES_RESSTATUS=1 and nvl(trunc(yres.YRES_ACTDEPTIME), yres.YRES_EXPDEPTIME) = STAT_T.WDAT_DATE),0))))
DEPARTURE_NOOFROOMS_def,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_SOURCE.TENTNOOFROOMS from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+(nvl((select V8_REP_YDET_OCC_WB_SOURCE.TENTNOOFROOMS from V8_REP_YDET_OCC_WB_SOURCE where V8_REP_YDET_OCC_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0)-nvl(( select V8_REP_YDET_OCC_WB_SOURCE.TENTBLOCKDEFRESROOM from V8_REP_YDET_OCC_WB_SOURCE where V8_REP_YDET_OCC_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0))),
0)) TENT_NOOFRROMS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_SOURCE.TENTNOOFROOMSBOOKINGPOSAVAIL from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+(nvl((select V8_REP_YDET_OCC_WB_SOURCE.TENTNOOFROOMS from V8_REP_YDET_OCC_WB_SOURCE where V8_REP_YDET_OCC_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0))), 
0)) TENT_NOOFRROMS_BOOKINGPOS,
nvl((select sbud.SBUD_VALUE from sbud where sbud.SBUD_DATE=STAT_T.WDAT_DATE and upper(sbud.SBUD_TABLENAME)='YRAS' and sbud.SBUD_TABLE_ID=STAT_T.TABLE_ID and sbud.SBUD_TYPE=3),0) BUD_NOOFROOMS, 
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_SOURCE.DEFNOOFADULTS from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_ADULT_WB_SOURCE.DEFADULTNO from V8_REP_YDET_ADULT_WB_SOURCE where V8_REP_YDET_ADULT_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_ADULT_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0)), 
(nvl((select sum(decode(shis.SHIS_YRES_RESSTATUS,3,0,decode(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_ADULTNO))) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
DEF_NOOFADULTS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_SOURCE.DEFNOOFROOMSBOOKINGPOSAVAIL from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_ADULT_WB_SOURCE.DEFADULTNO from V8_REP_YDET_ADULT_WB_SOURCE where V8_REP_YDET_ADULT_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_ADULT_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0)), 
(nvl((select sum(decode(shis.SHIS_YRES_RESSTATUS,3,0,decode(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_ADULTNO))) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID  and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
DEF_NOOFADULTS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_SOURCE.TENTNOOFADULTS from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_ADULT_WB_SOURCE.TENTADULTNO from V8_REP_YDET_ADULT_WB_SOURCE where V8_REP_YDET_ADULT_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_ADULT_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0)), 
0)) TENT_NOOFADULTS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_SOURCE.TENTNOOFADULTSBOOKINGPOSAVAIL from V8_REP_YBLD_OCC_YRAS_SOURCE where V8_REP_YBLD_OCC_YRAS_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_SOURCE.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_ADULT_WB_SOURCE.TENTADULTNO from V8_REP_YDET_ADULT_WB_SOURCE where V8_REP_YDET_ADULT_WB_SOURCE.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_ADULT_WB_SOURCE.YDET_DATE=STAT_T.WDAT_DATE),0)), 
0)) 
TENT_NOOFADULTS_BOOKINGPOS,
nvl((select sbud.SBUD_VALUE from sbud where sbud.SBUD_DATE=STAT_T.WDAT_DATE and upper(sbud.SBUD_TABLENAME)='YRAS' and sbud.SBUD_TABLE_ID=STAT_T.TABLE_ID and sbud.SBUD_TYPE=4),0) BUD_NOOFADULTS,
NVL((DECODE(STAT_T.FUTURE,1, 
(SELECT SUM(yrgl_number*ydet_noofrooms) FROM YDET,YRGL,YRES,XCAC, YRAL WHERE YRGL.yrgl_ydet_id=YDET.ydet_id  AND YRES.yres_id=YDET.ydet_yres_id AND XCAC.xcac_id=yrgl_xcac_id AND YRES.YRES_RESSTATUS=1 AND YDET.YDET_DATE=STAT_T.WDAT_DATE AND YDET.YDET_ID=YRAL.YRAL_YDET_ID and YRAL.YRAL_YRAS_ID=STAT_T.TABLE_ID), 
(NVL((SELECT SUM(DECODE(SHIS.SHIS_YRES_RESSTATUS,3,0,DECODE(SHIS.SHIS_DEP_ID,1,0,SHIS.SHIS_TOTALYRGL_NUMBER ))) FROM SHIS WHERE SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID AND SHIS.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))),0) 
DEF_NOOFKIDS,
NVL((DECODE(STAT_T.FUTURE,1, 
(SELECT SUM(yrgl_number*ydet_noofrooms) FROM YDET,YRGL,YRES,XCAC,YRAL WHERE YRGL.yrgl_ydet_id=YDET.ydet_id  AND YRES.yres_id=YDET.ydet_yres_id AND XCAC.xcac_id=yrgl_xcac_id AND YRES.YRES_RESSTATUS=0 AND YDET.YDET_DATE=STAT_T.WDAT_DATE AND YDET.YDET_ID=YRAL.YRAL_YDET_ID and YRAL.YRAL_YRAS_ID=STAT_T.TABLE_ID), 
0)),0) 
TENT_NOOFKIDS,
NVL((SELECT SUM(V8_REP_HS_ARR_ALLADULT_SOURCE.DEFEXPADULT) FROM V8_REP_HS_ARR_ALLADULT_SOURCE WHERE V8_REP_HS_ARR_ALLADULT_SOURCE.YRAL_YRAS_ID =STAT_T.TABLE_ID AND V8_REP_HS_ARR_ALLADULT_SOURCE.WDAT_DATE=STAT_T.WDAT_DATE),0) DEF_ARR_ADULTS,
NVL((SELECT SUM(V8_REP_HS_ARR_ALLADULT_SOURCE.TENTEXPADULT) FROM V8_REP_HS_ARR_ALLADULT_SOURCE WHERE V8_REP_HS_ARR_ALLADULT_SOURCE.YRAL_YRAS_ID=STAT_T.TABLE_ID AND V8_REP_HS_ARR_ALLADULT_SOURCE.WDAT_DATE=STAT_T.WDAT_DATE),0) TENT_ARR_ADULTS,
NVL((SELECT SUM(V8_REP_HS_ARR_ALLCHILD_SOURCE.DEFARRCHILD) FROM V8_REP_HS_ARR_ALLCHILD_SOURCE WHERE V8_REP_HS_ARR_ALLCHILD_SOURCE.YRAS_ID=STAT_T.TABLE_ID AND V8_REP_HS_ARR_ALLCHILD_SOURCE.WDAT_DATE=STAT_T.WDAT_DATE),0) DEF_ARR_KIDS, 
NVL((SELECT SUM(V8_REP_HS_ARR_ALLCHILD_SOURCE.TENTARRCHILD) FROM V8_REP_HS_ARR_ALLCHILD_SOURCE WHERE V8_REP_HS_ARR_ALLCHILD_SOURCE.YRAS_ID=STAT_T.TABLE_ID AND V8_REP_HS_ARR_ALLCHILD_SOURCE.WDAT_DATE=STAT_T.WDAT_DATE),0) TENT_ARR_KIDS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE1.DEF_REV_GROSS,0), 
(nvl((select sum(shis.SHIS_GROSSROOMREV) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_RM_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE1.DEF_REV_GROSS_BOOKINGPOS ,0), 
(nvl((select sum(shis.SHIS_GROSSROOMREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_RM_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE1.TENT_REV_GROSS,0),0)) TENT_RM_REV_GROSS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE1.TENT_REV_GROSS_BOOKINGPOS,0),0)) TENT_RM_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE2.DEF_REV_GROSS,0), 
(nvl((select sum(shis.SHIS_GROSSFBREV) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_FB_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE2.DEF_REV_GROSS_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_GROSSFBREV) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_FB_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE2.TENT_REV_GROSS,0),0)) TENT_FB_REV_GROSS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE2.TENT_REV_GROSS_BOOKINGPOS,0),0)) TENT_FB_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATY345.DEF_REV_GROSS,0), 
(nvl((select sum(shis.SHIS_GROSSMINIBARREV+shis.SHIS_GROSSMISCREV+shis.SHIS_GROSSTELEREV ) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_OTH_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATY345.DEF_REV_GROSS_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_GROSSMINIBARREV+shis.SHIS_GROSSMISCREV+shis.SHIS_GROSSTELEREV ) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_OTH_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATY345.TENT_REV_GROSS,0),0)) TENT_OTH_REV_GROSS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATY345.TENT_REV_GROSS_BOOKINGPOS,0),0)) TENT_OTH_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.DEF_REV_GROSS,0), 
(nvl((select sum(shis.SHIS_GROSSREV) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.DEF_REV_GROSS_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_GROSSREV) from shis where SHIS.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.TENT_REV_GROSS,0), 
0)) TENT_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.TENT_REV_GROSS_BOOKINGPOS,0), 
0)) TENT_REV_GROSS_BOOKINGPOS,
nvl((select sbud.SBUD_VALUE from sbud where sbud.SBUD_DATE=STAT_T.WDAT_DATE and upper(sbud.SBUD_TABLENAME)='YRAS' and sbud.SBUD_TABLE_ID=STAT_T.TABLE_ID and sbud.SBUD_TYPE=2),0) BUD_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE1.DEF_REV_NET,0), 
(nvl((select sum(shis.SHIS_NETROOMREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_RM_REV_NET,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE1.DEF_REV_NET_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_NETROOMREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_RM_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE1.TENT_REV_NET,0), 0)) TENT_RM_REV_NET,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE1.TENT_REV_NET_BOOKINGPOS,0), 0)) TENT_RM_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE2.DEF_REV_NET,0), 
(nvl((select sum(shis.SHIS_NETFBREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_FB_REV_NET,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE2.DEF_REV_NET_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_NETFBREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_FB_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE2.TENT_REV_NET,0), 0)) TENT_FB_REV_NET,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATYPE2.TENT_REV_NET_BOOKINGPOS,0), 0)) TENT_FB_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATY345.DEF_REV_NET,0), 
(nvl((select sum(shis.SHIS_NETMINIBARREV+shis.SHIS_NETMISCREV+shis.SHIS_NETTELEREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_OTH_REV_NET,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_S_STATY345.DEF_REV_NET_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_NETMINIBARREV+shis.SHIS_NETMISCREV+shis.SHIS_NETTELEREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_OTH_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATY345.TENT_REV_NET,0), 0)) TENT_OTH_REV_NET,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_S_STATY345.TENT_REV_NET_BOOKINGPOS,0), 0)) TENT_OTH_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.DEF_REV_NET,0), 
(nvl((select sum(shis.SHIS_NETREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_REV_NET,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.DEF_REV_NET_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_NETREV) from shis where shis.SHIS_SOURCE_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(STAT_R.TENT_REV_NET,0), 0)) TENT_REV_NET,
(decode(STAT_T.FUTURE,1,nvl(STAT_R.TENT_REV_NET_BOOKINGPOS,0), 0)) TENT_REV_NET_BOOKINGPOS,
nvl((select sbud.SBUD_VALUE from sbud where sbud.SBUD_DATE=STAT_T.WDAT_DATE and upper(sbud.SBUD_TABLENAME)='YRAS' and sbud.SBUD_TABLE_ID=STAT_T.TABLE_ID and sbud.SBUD_TYPE=1),0) BUD_REV_NET, 
STAT_T.FUTURE FUTURE 
FROM 
V8_REP_WDAT_DATE_YRAS_SOURCE STAT_T, 
V8_REP_YPOS_FC_SOURCE STAT_R, 
V8_REP_YPOS_FC_YRAS_S_STATYPE1, 
V8_REP_YPOS_FC_YRAS_S_STATYPE2, 
V8_REP_YPOS_FC_YRAS_S_STATY345 
where 
STAT_T.WDAT_DATE=STAT_R.YPOS_DATE(+) and 
STAT_T.TABLE_ID=STAT_R.TABLE_ID(+)and 
STAT_T.WDAT_DATE=V8_REP_YPOS_FC_YRAS_S_STATYPE1.YPOS_DATE(+)and   
STAT_T.TABLE_ID=V8_REP_YPOS_FC_YRAS_S_STATYPE1.YRAS_SOURCE_ID(+)and 
STAT_T.WDAT_DATE=V8_REP_YPOS_FC_YRAS_S_STATYPE2.YPOS_DATE(+)and 
STAT_T.TABLE_ID=V8_REP_YPOS_FC_YRAS_S_STATYPE2.YRAS_SOURCE_ID(+)and 
STAT_T.WDAT_DATE=V8_REP_YPOS_FC_YRAS_S_STATY345.YPOS_DATE(+)and
STAT_T.TABLE_ID=V8_REP_YPOS_FC_YRAS_S_STATY345.YRAS_SOURCE_ID(+)
order by STAT_T.WDAT_DATE;



-- History & Forecast by Channel Code

CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_C_STATYPE1
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_CHANNEL_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (1) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_CHANNEL_ID;



CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_C_STATYPE2
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_CHANNEL_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (2) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_CHANNEL_ID;



CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_C_STATYPE3
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_CHANNEL_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (3) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_CHANNEL_ID;



CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_C_STATYPE4
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_CHANNEL_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (4) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_CHANNEL_ID;


CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_C_STATYPE5
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_CHANNEL_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (5) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_CHANNEL_ID;

CREATE OR REPLACE VIEW V8_REP_YPOS_FC_YRAS_C_STATY345
AS 
SELECT 
V8_REP_REV_FORECAST.YPOS_DATE, 
V8_REP_REV_FORECAST.YRAS_CHANNEL_ID, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS, 
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((   ( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )    *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0))) DEF_REV_GROSS_BOOKINGPOS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS,
sum((decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((  V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0)))) TENT_REV_GROSS_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),1,0,2,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),3,0,4,nvl((( case when nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) > nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) then nvl(V8_REP_REV_FORECAST.YPOS_QUANTITY,0) else nvl(V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS,0) end  )  *V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),5,0)),0)) DEF_REV_NET_BOOKINGPOS, 
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.YPOS_QUANTITY*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET,
sum(decode(V8_REP_REV_FORECAST.YPOS_DT,1,(decode(V8_REP_REV_FORECAST.TYPENUM,0,0,1,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),2,0,3,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0),4,0,5,nvl((V8_REP_REV_FORECAST.QUANTITY_BOOKINGPOS*V8_REP_REV_FORECAST.YPOS_UNITPRICE),0))),0)) TENT_REV_NET_BOOKINGPOS  
FROM V8_REP_REV_FORECAST 
where V8_REP_REV_FORECAST.ZDCO_STATS_TYPE in (3,3,5) 
group by 
V8_REP_REV_FORECAST.YPOS_DATE,V8_REP_REV_FORECAST.YRAS_CHANNEL_ID;


CREATE OR REPLACE VIEW V8_REP_HS_ARR_ALLADULT_CHANNEL
AS 
select wdat_date,yral_yras_id,sum(allexpadult) allexpadult,sum(defexpadult) defexpadult,sum(tentexpadult) tentexpadult  from (
select 
   wdat_date
   ,YRAL.YRAL_YRAS_ID
   ,sum(ydet_adultno*ydet_noofrooms) allexpadult
   ,decode(yres_resstatus,1,sum(ydet_adultno*ydet_noofrooms)) defexpadult
   ,decode(yres_resstatus,0,sum(ydet_adultno*ydet_noofrooms)) tentexpadult
from wdat,ydet,yres,ycat,YRAL
where
    ydet_date=wdat_date 
    and YRAL_YDET_ID=YDET_ID
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and YRAL.YRAL_YRAC_ID=2
group by wdat_date,yres_resstatus,YRAL.YRAL_YRAS_ID
) group by wdat_date,YRAL_YRAS_ID;



CREATE OR REPLACE VIEW V8_REP_HS_ARR_ALLCHILD_CHANNEL
AS 
select wdat_date, YRAL_YRAS_ID YRAS_ID, nvl(sum(allarrchild),0) allarrchild,nvl(sum(defarrchild),0) defarrchild,nvl(sum(tentarrchild),0) tentarrchild 
   ,nvl(sum(allarrdomesticchild),0) allarrdomesticchild,nvl(sum(defarrdomesticchild),0) defarrdomesticchild,nvl(sum(tentarrdomesticchild),0) tentarrdomesticchild           
   ,nvl(sum(allarrfreeofchargechild),0) allarrfreeofchargechild,nvl(sum(defarrfreeofchargechild),0) defarrfreeofchargechild,nvl(sum(tentarrfreeofchargechild),0) tentarrfreeofchargechild    
from
 (
select 
   wdat_date
   ,YRAL_YRAS_ID
   ,sum(yrgl_number*ydet_noofrooms) allarrchild
   ,sum(decode(xcac_domestic,1,yrgl_number*ydet_noofrooms,0)) allarrdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*ydet_noofrooms))) defarrchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*ydet_noofrooms))) tentarrchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*ydet_noofrooms,0))) defarrdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*ydet_noofrooms,0))) tentarrdomesticchild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*ydet_noofrooms,0)) allarrfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*ydet_noofrooms,0))) defarrfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*ydet_noofrooms,0))) tentarrfreeofchargechild
from wdat,ydet,yres,yrgl,xcac,ycat, YRAL
where
    ydet_date=wdat_date and
    YRAL_YDET_ID=YDET_ID
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrgl_ydet_id=ydet_id
and yrgl_xcac_id=xcac_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and YRAL.YRAL_YRAC_ID=2
group by wdat_date,yres_resstatus,YRAL_YRAS_ID
) group by wdat_date,YRAL_YRAS_ID;


CREATE OR REPLACE VIEW V8_REP_HS_ARR_ALLROOM_CHANNEL
AS 
select wdat_date,channel_id
      ,sum(allexproom) allexproom,sum(defexproom) defexproom,sum(tentexproom) tentexproom  
      ,sum(allexproomDayUse) allexproomDayUse,sum(defexproomDayUse) defexproomDayUse,sum(tentexproomDayUse) tentexproomDayUse  
from (
select 
   wdat_date
   ,yral_yras_id channel_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) allexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) allexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproomDayUse
from wdat,ydet yd,yres y,ycat,yral
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yral_ydet_id=ydet_id
and yral_yrac_id=2
and (nvl(ydet_sharenum,0)=0
     or
     not exists (select d.ydet_sharenum 
                           from ydet d,yres r 
                where d.ydet_yres_id=r.yres_id
                             and d.ydet_id<>yd.ydet_id 
                             and (y.yres_exparrtime<>r.yres_exparrtime)
                             and d.ydet_sharenum=yd.ydet_sharenum
                )
    )
group by wdat_date,ydet_sharenum,yral_yras_id
) group by wdat_date,channel_id;


CREATE OR REPLACE VIEW V8_REP_HS_DEP_ALLROOM_CHANNEL
AS 
select wdat_date,channel_id   
   ,sum(alldeproom) alldeproom,sum(defdeproom) defdeproom ,sum(tentdeproom) tentdeproom  
   ,sum(alldeproomDayUse) alldeproomDayUse,sum(defdeproomDayUse) defdeproomDayUse,sum(tentdeproomDayUse) tentdeproomDayUse  
from (
select 
   wdat_date
   ,yral_yras_id channel_id
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) alldeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) alldeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproomDayUse
from wdat,ydet yd,yres y,ycat,yral
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date     
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yral_ydet_id=ydet_id
and yral_yrac_id=2
and (nvl(ydet_sharenum,0)= 0
     or
     not exists (select d.ydet_sharenum 
                           from ydet d,yres r 
                where d.ydet_yres_id=r.yres_id
                             and d.ydet_id<>yd.ydet_id 
                             and y.yres_expdeptime<>r.yres_expdeptime 
                             and d.ydet_sharenum=yd.ydet_sharenum
                )
    )
group by wdat_date,ydet_sharenum,yral_yras_id)
group by wdat_date,channel_id;


CREATE OR REPLACE VIEW V8_REP_CHANNEL_HISTFOR
AS 
SELECT 
STAT_T.WDAT_DATE WDAT_DATE, 
STAT_T.WDAT_DATE WDAT_DATE2, 
STAT_T.WDAT_DATE WDAT_DATE3, 
STAT_T.TABLE_ID TABLE_ID,
(SELECT YRAS.YRAS_DISPLORDER FROM YRAS WHERE YRAS.YRAS_ID =STAT_T.TABLE_ID) YRAS_DISPLORDER,
STAT_T.YRAS_YRAG_ID YRAS_YRAG_ID, 
STAT_T.YRAS_LONGDESC YRAS_LONGDESC, 
STAT_T.YRAS_SHORTDESC YRAS_SHORTDESC,
(select YRAG.YRAG_DISPLORDER from YRAG where YRAG.YRAG_ID=STAT_T.YRAS_YRAG_ID) YRAG_DISPLAYORDER,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_CHANNEL.DEFNOOFROOMS from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_OCC_WB_CHANNEL.DEFNOOFROOMS from V8_REP_YDET_OCC_WB_CHANNEL where V8_REP_YDET_OCC_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(decode(shis. SHIS_YRES_RESSTATUS,3,0,decode(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_NOOFROOMS))) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and SHIS.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
DEF_NOOFRROMS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_CHANNEL.DEFNOOFROOMS from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl(( select V8_REP_YBLD_OCC_YRAS_CHANNEL.DEFNOOFROOMSBOOKINGPOSAVAIL from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_OCC_WB_CHANNEL.DEFNOOFROOMS from V8_REP_YDET_OCC_WB_CHANNEL where V8_REP_YDET_OCC_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(decode(shis.SHIS_YRES_RESSTATUS,3,0,decode(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_NOOFROOMS))) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and SHIS.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
 DEF_NOOFRROMS_BOOKINGPOS, 
(decode(STAT_T.FUTURE,1, 
(nvl((select allexproom from V8_REP_HS_ARR_ALLROOM_CHANNEL V where v.channel_id=STAT_T.TABLE_ID and V.WDAT_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(decode(SHIS.SHIS_YRES_RESSTATUS,3,0, decode(shis.SHIS_ARR_ID,1,shis.SHIS_YDET_NOOFROOMS,0))) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
ARRIVAL_NOOFROOMS,
(decode(STAT_T.FUTURE,1, 
(nvl((select  defexproom from V8_REP_HS_ARR_ALLROOM_CHANNEL v where v.channel_id=STAT_T.TABLE_ID and V.WDAT_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(decode(shis.SHIS_YRES_RESSTATUS,1,decode(shis.SHIS_ARR_ID,1,shis.SHIS_YDET_NOOFROOMS,0),0)) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
ARRIVAL_NOOFROOMS_def,
(decode(STAT_T.FUTURE,1,
(nvl((select  alldeproom from V8_REP_HS_DEP_ALLROOM_CHANNEL v where v.channel_id=STAT_T.TABLE_ID and V.WDAT_DATE=STAT_T.WDAT_DATE),0)),
(nvl((select sum(det.YDET_NOOFROOMS) from v8_rep_ydet_infos det, yres where det.channel_id=STAT_T.TABLE_ID and det.YDET_ID = yres.YRES_LAST_YDET_ID  and yres.YRES_EXPDEPTIME=STAT_T.WDAT_DATE and yres.YRES_RESSTATUS <> 3 and nvl(trunc(yres.YRES_ACTDEPTIME), yres.YRES_EXPDEPTIME) = STAT_T.WDAT_DATE),0)))) 
 DEPARTURE_NOOFROOMS,
(decode(STAT_T.FUTURE,1,
(nvl((select defdeproom from V8_REP_HS_DEP_ALLROOM_CHANNEL v where v.channel_id= STAT_T.TABLE_ID and v.wdat_date=STAT_T.WDAT_DATE),0)) ,
(nvl((select sum(det.YDET_NOOFROOMS) from v8_rep_ydet_infos det, yres where det.channel_id = STAT_T.TABLE_ID AND det.YDET_ID = yres.YRES_LAST_YDET_ID and yres.YRES_EXPDEPTIME=STAT_T.WDAT_DATE and yres.YRES_RESSTATUS=1 and nvl(trunc(yres.YRES_ACTDEPTIME), yres.YRES_EXPDEPTIME) = STAT_T.WDAT_DATE),0))))
DEPARTURE_NOOFROOMS_def,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_CHANNEL.TENTNOOFROOMS from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+(nvl((select V8_REP_YDET_OCC_WB_CHANNEL.TENTNOOFROOMS from V8_REP_YDET_OCC_WB_CHANNEL where V8_REP_YDET_OCC_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0)-nvl(( select V8_REP_YDET_OCC_WB_CHANNEL.TENTBLOCKDEFRESROOM from V8_REP_YDET_OCC_WB_CHANNEL where V8_REP_YDET_OCC_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0))),
0)) TENT_NOOFRROMS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_CHANNEL.TENTNOOFROOMSBOOKINGPOSAVAIL from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+(nvl((select V8_REP_YDET_OCC_WB_CHANNEL.TENTNOOFROOMS from V8_REP_YDET_OCC_WB_CHANNEL where V8_REP_YDET_OCC_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_OCC_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0))), 
0)) TENT_NOOFRROMS_BOOKINGPOS,
--nvl((select sbud.SBUD_VALUE from sbud where sbud.SBUD_DATE=STAT_T.WDAT_DATE and upper(sbud.SBUD_TABLENAME)='YRAS' and sbud.SBUD_TABLE_ID=STAT_T.TABLE_ID and sbud.SBUD_TYPE=3),0) BUD_NOOFROOMS,  -- No Channal Budget available 
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_CHANNEL.DEFNOOFADULTS from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_ADULT_WB_CHANNEL.DEFADULTNO from V8_REP_YDET_ADULT_WB_CHANNEL where V8_REP_YDET_ADULT_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_ADULT_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0)), 
(nvl((select sum(decode(shis.SHIS_YRES_RESSTATUS,3,0,decode(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_ADULTNO))) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
DEF_NOOFADULTS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_CHANNEL.DEFNOOFROOMSBOOKINGPOSAVAIL from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_ADULT_WB_CHANNEL.DEFADULTNO from V8_REP_YDET_ADULT_WB_CHANNEL where V8_REP_YDET_ADULT_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_ADULT_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0)), 
(nvl((select sum(decode(shis.SHIS_YRES_RESSTATUS,3,0,decode(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_ADULTNO))) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID  and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0))))
DEF_NOOFADULTS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_CHANNEL.TENTNOOFADULTS from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_ADULT_WB_CHANNEL.TENTADULTNO from V8_REP_YDET_ADULT_WB_CHANNEL where V8_REP_YDET_ADULT_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_ADULT_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0)), 
0)) TENT_NOOFADULTS,
(decode(STAT_T.FUTURE,1, 
(nvl((select V8_REP_YBLD_OCC_YRAS_CHANNEL.TENTNOOFADULTSBOOKINGPOSAVAIL from V8_REP_YBLD_OCC_YRAS_CHANNEL where V8_REP_YBLD_OCC_YRAS_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YBLD_OCC_YRAS_CHANNEL.YBLD_DATE=STAT_T.WDAT_DATE),0)+nvl((select V8_REP_YDET_ADULT_WB_CHANNEL.TENTADULTNO from V8_REP_YDET_ADULT_WB_CHANNEL where V8_REP_YDET_ADULT_WB_CHANNEL.TABLE_ID=STAT_T.TABLE_ID and V8_REP_YDET_ADULT_WB_CHANNEL.YDET_DATE=STAT_T.WDAT_DATE),0)), 
0)) 
TENT_NOOFADULTS_BOOKINGPOS,
--nvl((select sbud.SBUD_VALUE from sbud where sbud.SBUD_DATE=STAT_T.WDAT_DATE and upper(sbud.SBUD_TABLENAME)='YRAS' and sbud.SBUD_TABLE_ID=STAT_T.TABLE_ID and sbud.SBUD_TYPE=4),0) BUD_NOOFADULTS, -- No Channal Budget available
NVL((DECODE(STAT_T.FUTURE,1, 
(SELECT SUM(yrgl_number*ydet_noofrooms) FROM YDET,YRGL,YRES,XCAC, YRAL WHERE YRGL.yrgl_ydet_id=YDET.ydet_id  AND YRES.yres_id=YDET.ydet_yres_id AND XCAC.xcac_id=yrgl_xcac_id AND YRES.YRES_RESSTATUS=1 AND YDET.YDET_DATE=STAT_T.WDAT_DATE AND YDET.YDET_ID=YRAL.YRAL_YDET_ID and YRAL.YRAL_YRAS_ID=STAT_T.TABLE_ID), 
(NVL((SELECT SUM(DECODE(SHIS.SHIS_YRES_RESSTATUS,3,0,DECODE(SHIS.SHIS_DEP_ID,1,0,SHIS.SHIS_TOTALYRGL_NUMBER ))) FROM SHIS WHERE SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID AND SHIS.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))),0) 
DEF_NOOFKIDS,
NVL((DECODE(STAT_T.FUTURE,1, 
(SELECT SUM(yrgl_number*ydet_noofrooms) FROM YDET,YRGL,YRES,XCAC,YRAL WHERE YRGL.yrgl_ydet_id=YDET.ydet_id  AND YRES.yres_id=YDET.ydet_yres_id AND XCAC.xcac_id=yrgl_xcac_id AND YRES.YRES_RESSTATUS=0 AND YDET.YDET_DATE=STAT_T.WDAT_DATE AND YDET.YDET_ID=YRAL.YRAL_YDET_ID and YRAL.YRAL_YRAS_ID=STAT_T.TABLE_ID), 
0)),0) 
TENT_NOOFKIDS,
NVL((SELECT SUM(V8_REP_HS_ARR_ALLADULT_CHANNEL.DEFEXPADULT) FROM V8_REP_HS_ARR_ALLADULT_CHANNEL WHERE V8_REP_HS_ARR_ALLADULT_CHANNEL.YRAL_YRAS_ID =STAT_T.TABLE_ID AND V8_REP_HS_ARR_ALLADULT_CHANNEL.WDAT_DATE=STAT_T.WDAT_DATE),0) DEF_ARR_ADULTS,
NVL((SELECT SUM(V8_REP_HS_ARR_ALLADULT_CHANNEL.TENTEXPADULT) FROM V8_REP_HS_ARR_ALLADULT_CHANNEL WHERE V8_REP_HS_ARR_ALLADULT_CHANNEL.YRAL_YRAS_ID=STAT_T.TABLE_ID AND V8_REP_HS_ARR_ALLADULT_CHANNEL.WDAT_DATE=STAT_T.WDAT_DATE),0) TENT_ARR_ADULTS,
NVL((SELECT SUM(V8_REP_HS_ARR_ALLCHILD_CHANNEL.DEFARRCHILD) FROM V8_REP_HS_ARR_ALLCHILD_CHANNEL WHERE V8_REP_HS_ARR_ALLCHILD_CHANNEL.YRAS_ID=STAT_T.TABLE_ID AND V8_REP_HS_ARR_ALLCHILD_CHANNEL.WDAT_DATE=STAT_T.WDAT_DATE),0) DEF_ARR_KIDS, 
NVL((SELECT SUM(V8_REP_HS_ARR_ALLCHILD_CHANNEL.TENTARRCHILD) FROM V8_REP_HS_ARR_ALLCHILD_CHANNEL WHERE V8_REP_HS_ARR_ALLCHILD_CHANNEL.YRAS_ID=STAT_T.TABLE_ID AND V8_REP_HS_ARR_ALLCHILD_CHANNEL.WDAT_DATE=STAT_T.WDAT_DATE),0) TENT_ARR_KIDS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE1.DEF_REV_GROSS,0), 
(nvl((select sum(shis.SHIS_GROSSROOMREV) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_RM_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE1.DEF_REV_GROSS_BOOKINGPOS ,0), 
(nvl((select sum(shis.SHIS_GROSSROOMREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_RM_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE1.TENT_REV_GROSS,0),0)) TENT_RM_REV_GROSS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE1.TENT_REV_GROSS_BOOKINGPOS,0),0)) TENT_RM_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE2.DEF_REV_GROSS,0), 
(nvl((select sum(shis.SHIS_GROSSFBREV) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_FB_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE2.DEF_REV_GROSS_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_GROSSFBREV) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_FB_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE2.TENT_REV_GROSS,0),0)) TENT_FB_REV_GROSS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE2.TENT_REV_GROSS_BOOKINGPOS,0),0)) TENT_FB_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATY345.DEF_REV_GROSS,0), 
(nvl((select sum(shis.SHIS_GROSSMINIBARREV+shis.SHIS_GROSSMISCREV+shis.SHIS_GROSSTELEREV ) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_OTH_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATY345.DEF_REV_GROSS_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_GROSSMINIBARREV+shis.SHIS_GROSSMISCREV+shis.SHIS_GROSSTELEREV ) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_OTH_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATY345.TENT_REV_GROSS,0),0)) TENT_OTH_REV_GROSS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATY345.TENT_REV_GROSS_BOOKINGPOS,0),0)) TENT_OTH_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.DEF_REV_GROSS,0), 
(nvl((select sum(shis.SHIS_GROSSREV) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.DEF_REV_GROSS_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_GROSSREV) from shis where SHIS.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_REV_GROSS_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.TENT_REV_GROSS,0), 
0)) TENT_REV_GROSS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.TENT_REV_GROSS_BOOKINGPOS,0), 
0)) TENT_REV_GROSS_BOOKINGPOS,
--nvl((select sbud.SBUD_VALUE from sbud where sbud.SBUD_DATE=STAT_T.WDAT_DATE and upper(sbud.SBUD_TABLENAME)='YRAS' and sbud.SBUD_TABLE_ID=STAT_T.TABLE_ID and sbud.SBUD_TYPE=2),0) BUD_REV_GROSS, -- No Channal Budget available
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE1.DEF_REV_NET,0), 
(nvl((select sum(shis.SHIS_NETROOMREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_RM_REV_NET,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE1.DEF_REV_NET_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_NETROOMREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_RM_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE1.TENT_REV_NET,0), 0)) TENT_RM_REV_NET,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE1.TENT_REV_NET_BOOKINGPOS,0), 0)) TENT_RM_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE2.DEF_REV_NET,0), 
(nvl((select sum(shis.SHIS_NETFBREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_FB_REV_NET,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE2.DEF_REV_NET_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_NETFBREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_FB_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE2.TENT_REV_NET,0), 0)) TENT_FB_REV_NET,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATYPE2.TENT_REV_NET_BOOKINGPOS,0), 0)) TENT_FB_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATY345.DEF_REV_NET,0), 
(nvl((select sum(shis.SHIS_NETMINIBARREV+shis.SHIS_NETMISCREV+shis.SHIS_NETTELEREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_OTH_REV_NET,
(decode(STAT_T.FUTURE,1, 
nvl(V8_REP_YPOS_FC_YRAS_C_STATY345.DEF_REV_NET_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_NETMINIBARREV+shis.SHIS_NETMISCREV+shis.SHIS_NETTELEREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_OTH_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATY345.TENT_REV_NET,0), 0)) TENT_OTH_REV_NET,
(decode(STAT_T.FUTURE,1,nvl(V8_REP_YPOS_FC_YRAS_C_STATY345.TENT_REV_NET_BOOKINGPOS,0), 0)) TENT_OTH_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.DEF_REV_NET,0), 
(nvl((select sum(shis.SHIS_NETREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_REV_NET,
(decode(STAT_T.FUTURE,1, 
nvl(STAT_R.DEF_REV_NET_BOOKINGPOS,0), 
(nvl((select sum(shis.SHIS_NETREV) from shis where shis.SHIS_CHANNEL_ID=STAT_T.TABLE_ID and shis.SHIS_WDAT_DATE=STAT_T.WDAT_DATE),0)))) DEF_REV_NET_BOOKINGPOS,
(decode(STAT_T.FUTURE,1,nvl(STAT_R.TENT_REV_NET,0), 0)) TENT_REV_NET,
(decode(STAT_T.FUTURE,1,nvl(STAT_R.TENT_REV_NET_BOOKINGPOS,0), 0)) TENT_REV_NET_BOOKINGPOS,
--nvl((select sbud.SBUD_VALUE from sbud where sbud.SBUD_DATE=STAT_T.WDAT_DATE and upper(sbud.SBUD_TABLENAME)='YRAS' and sbud.SBUD_TABLE_ID=STAT_T.TABLE_ID and sbud.SBUD_TYPE=1),0) BUD_REV_NET,  -- No Channal Budget available
STAT_T.FUTURE FUTURE 
FROM 
V8_REP_WDAT_DATE_YRAS_CHANNEL STAT_T, 
V8_REP_YPOS_FC_CHANNEL STAT_R, 
V8_REP_YPOS_FC_YRAS_C_STATYPE1, 
V8_REP_YPOS_FC_YRAS_C_STATYPE2, 
V8_REP_YPOS_FC_YRAS_C_STATY345 
where 
STAT_T.WDAT_DATE=STAT_R.YPOS_DATE(+) and 
STAT_T.TABLE_ID=STAT_R.TABLE_ID(+)and 
STAT_T.WDAT_DATE=V8_REP_YPOS_FC_YRAS_C_STATYPE1.YPOS_DATE(+)and   
STAT_T.TABLE_ID=V8_REP_YPOS_FC_YRAS_C_STATYPE1.YRAS_CHANNEL_ID(+)and 
STAT_T.WDAT_DATE=V8_REP_YPOS_FC_YRAS_C_STATYPE2.YPOS_DATE(+)and 
STAT_T.TABLE_ID=V8_REP_YPOS_FC_YRAS_C_STATYPE2.YRAS_CHANNEL_ID(+)and 
STAT_T.WDAT_DATE=V8_REP_YPOS_FC_YRAS_C_STATY345.YPOS_DATE(+)and
STAT_T.TABLE_ID=V8_REP_YPOS_FC_YRAS_C_STATY345.YRAS_CHANNEL_ID(+)
order by STAT_T.WDAT_DATE;



CREATE OR REPLACE FORCE VIEW V8_REP_WDAT_DATE_TRAVEL
AS
SELECT 
wdat.WDAT_DATE WDAT_DATE,
xcms.XCMS_ID TABLE_ID,
(SELECT v8_rep_name.NAME FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=xcms.XCMS_ID) NAME,
(CASE WHEN wdat.WDAT_DATE<V8_sys_fideliodate THEN 0 ELSE 1 END) FUTURE
FROM 
wdat,xcms,xcca
WHERE 
xcms.XCMS_XCCA_ID=xcca.XCCA_ID
AND xcca.XCCA_INTERNALCATEGORY=2;


CREATE OR REPLACE FORCE VIEW V8_REP_XCMS_TRAVEL_HISTFOR
AS
  SELECT STAT_T.WDAT_DATE WDAT_DATE,
    STAT_T.WDAT_DATE WDAT_DATE2,
    STAT_T.WDAT_DATE WDAT_DATE3,
    STAT_T.TABLE_ID TABLE_ID,
    STAT_T.NAME NAME,
    (DECODE(STAT_T.FUTURE,1, (NVL(
    (SELECT OB.DEFNOOFROOMS
    FROM V8_REP_YBLD_OCC_TRAVEL OB
    WHERE OB.TABLE_ID=STAT_T.TABLE_ID
    AND OB.YBLD_DATE =STAT_T.WDAT_DATE
    ),0)+NVL(
    (SELECT ORR.DEFNOOFROOMS
    FROM V8_REP_YDET_OCC_WB_TRAVEL ORR
    WHERE ORR.TABLE_ID=STAT_T.TABLE_ID
    AND ORR.YDET_DATE =STAT_T.WDAT_DATE
    ),0)), (NVL(
    (SELECT SUM(DECODE(shis.SHIS_YRES_RESSTATUS,3,0,DECODE(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_NOOFROOMS)))
    FROM shis
    WHERE shis.SHIS_YCLN_2_XCMS_ID =STAT_T.TABLE_ID
    AND shis.SHIS_WDAT_DATE        =STAT_T.WDAT_DATE
    ),0)))) DEF_NOOFRROMS,
    (DECODE(STAT_T.FUTURE,1, (NVL(
    (SELECT OB.TENTNOOFROOMS
    FROM V8_REP_YBLD_OCC_TRAVEL OB
    WHERE OB.TABLE_ID=STAT_T.TABLE_ID
    AND OB.YBLD_DATE =STAT_T.WDAT_DATE
    ),0)+(NVL(
    (SELECT ORR.TENTNOOFROOMS
    FROM V8_REP_YDET_OCC_WB_TRAVEL ORR
    WHERE ORR.TABLE_ID=STAT_T.TABLE_ID
    AND ORR.YDET_DATE =STAT_T.WDAT_DATE
    ),0)-NVL(
    (SELECT ORR.TENTBLOCKDEFRESROOM
    FROM V8_REP_YDET_OCC_WB_TRAVEL ORR
    WHERE ORR.TABLE_ID=STAT_T.TABLE_ID
    AND ORR.YDET_DATE =STAT_T.WDAT_DATE
    ),0))), 0)) TENT_NOOFRROMS,
    (DECODE(STAT_T.FUTURE,1, (NVL(
    (SELECT OB.DEFNOOFADULTS
    FROM V8_REP_YBLD_OCC_TRAVEL OB
    WHERE OB.TABLE_ID=STAT_T.TABLE_ID
    AND OB.YBLD_DATE =STAT_T.WDAT_DATE
    ),0)+NVL(
    (SELECT ORA.DEFADULTNO
    FROM V8_REP_YDET_ADULT_WB_TRAVEL ORA
    WHERE ORA.TABLE_ID=STAT_T.TABLE_ID
    AND ORA.YDET_DATE =STAT_T.WDAT_DATE
    ),0)), (NVL(
    (SELECT SUM(DECODE(shis.SHIS_YRES_RESSTATUS,3,0,DECODE(shis.SHIS_DEP_ID,1,0,shis.SHIS_YDET_ADULTNO)))
    FROM shis
    WHERE shis.SHIS_YCLN_2_XCMS_ID=STAT_T.TABLE_ID
    AND shis.SHIS_WDAT_DATE       =STAT_T.WDAT_DATE
    ),0)))) DEF_NOOFADULTS,
    (DECODE(STAT_T.FUTURE,1, (NVL(
    (SELECT OB.TENTNOOFADULTS
    FROM V8_REP_YBLD_OCC_TRAVEL OB
    WHERE OB.TABLE_ID=STAT_T.TABLE_ID
    AND OB.YBLD_DATE =STAT_T.WDAT_DATE
    ),0)+NVL(
    (SELECT ORA.TENTADULTNO
    FROM V8_REP_YDET_ADULT_WB_TRAVEL ORA
    WHERE ORA.TABLE_ID=STAT_T.TABLE_ID
    AND ORA.YDET_DATE =STAT_T.WDAT_DATE
    ),0)), 0)) TENT_NOOFADULTS,
    (DECODE(STAT_T.FUTURE,1, NVL(STAT_R.DEF_REV_GROSS,0), (NVL(
    (SELECT SUM(shis.SHIS_GROSSREV)
    FROM shis
    WHERE shis.SHIS_YCLN_2_XCMS_ID=STAT_T.TABLE_ID
    AND shis.SHIS_WDAT_DATE       =STAT_T.WDAT_DATE
    ),0)))) DEF_REV_GROSS,
    (DECODE(STAT_T.FUTURE,1, NVL(STAT_R.TENT_REV_GROSS,0), 0)) TENT_REV_GROSS,
    (DECODE(STAT_T.FUTURE,1, NVL(STAT_R.DEF_REV_NET,0), (NVL(
    (SELECT SUM(shis.SHIS_NETREV)
    FROM shis
    WHERE shis.SHIS_YCLN_2_XCMS_ID=STAT_T.TABLE_ID
    AND shis.SHIS_WDAT_DATE       =STAT_T.WDAT_DATE
    ),0)))) DEF_REV_NET,
    (DECODE(STAT_T.FUTURE,1, NVL(STAT_R.TENT_REV_NET,0), 0)) TENT_REV_NET,
    STAT_T.FUTURE FUTURE
  FROM V8_REP_WDAT_DATE_TRAVEL STAT_T,
    V8_REP_YPOS_FC_TRAVEL STAT_R
  WHERE STAT_T.WDAT_DATE=STAT_R.YPOS_DATE(+)
  AND STAT_T.TABLE_ID   =STAT_R.TABLE_ID(+)
  ORDER BY STAT_T.WDAT_DATE;


-- Mhoffmann 05.02.2014 new view for reports instead of V8_SYS_ROOM

CREATE OR REPLACE VIEW V8_REP_ROOMS
AS 
select
sum(nvl(v1.NOOFROOMS,0)-nvl(V8_SYS_NOOFOUTOFORDER.OOO,0)) ROOMSAVAILHOTEL ,
sum(nvl(v1.NOOFROOMS,0)) ROOMSINHOTEL,
to_char(v1.WDAT_DATE,'YYYY.MM') DATE_MON,
to_char(v1.WDAT_DATE,'MM') MM,
to_char(v1.WDAT_DATE,'YYYY') DATE_YEAR,
(select sum(SBUD_VALUE) from sbud where sbud_type=3 and TO_CHAR(SBUD.SBUD_DATE,'YYYY.MM')=TO_CHAR(V1.WDAT_DATE,'YYYY.MM') group by to_char(sbud_date,'YYYY.mm')) SBUD_ROOMS
from
v8_sys_nooftotalrooms_all v1, v8_sys_noofoutoforder
where
v1.wdat_date=v8_sys_noofoutoforder.wdat_date (+)
--and to_char(v1.WDAT_DATE,'YYYY.MM')='2011.09'
group by
to_char(v1.WDAT_DATE,'YYYY'),
to_char(v1.WDAT_DATE,'MM'),
to_char(v1.WDAT_DATE,'YYYY.MM');


CREATE OR REPLACE VIEW V8_THREEMON_SUB
AS 
select 
VS.CALC_VALUE,
VS.DATE_YM_C,
VS.DEF_NOOFRROMS,
VS.TENT_NOOFRROMS,
VAR.SBUD_ROOMS,
VAR.ROOMSAVAILHOTEL,
VAR.ROOMSINHOTEL
from 
((select 
DATE_YM_C ,
SUM(V1.DEF_NOOFRROMS) DEF_NOOFRROMS, 
SUM(V1.TENT_NOOFRROMS) TENT_NOOFRROMS, 
SUM(V1.DEF_NETREV) DEF_NETREV, 
SUM(V1.TENT_NETREV) TENT_NETREV, 
SUM(V1.DEF_GROSSREV) DEF_GROSSREV, 
SUM(V1.TENT_GROSSREV) TENT_GROSSREV,
V1.CALC_VALUE
from 
V8_REP_THREEMON_INFOS_EX_BLOCK V1
--DATE_YM_C = '2011.10'
group by Date_YM_C, CALC_VALUE)) VS,
(SELECT 
TO_CHAR(WDAT.WDAT_DATE,'YYYY.MM') DATE_ROOMHOT, 
DECODE( 
V1.DATE_MON, 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,-12),'YYYY.MM'),-1, 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,-11),'YYYY.MM'),-2, 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,-10),'YYYY.MM'),-3, 
TO_CHAR(WDAT.WDAT_DATE,'YYYY.MM'),1, 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,1),'YYYY.MM'),2, 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,2),'YYYY.MM'),3 
,0) CALC_VALUE, 
V1.ROOMSAVAILHOTEL,
V1.ROOMSINHOTEL,
V1.SBUD_ROOMS
FROM 
WDAT, 
V8_REP_ROOMS V1 -- changed from v8_sys_rooms to v8_rep_rooms because of wrong SBUD_ROOMS NS 19.12.2013
WHERE WDAT.WDAT_DATE=LAST_DAY(WDAT.WDAT_DATE) 
--AND TO_CHAR(WDAT.WDAT_DATE,'YYYY.MM')='2011.10' 
AND 
( 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,-12),'YYYY.MM')=V1.DATE_MON 
OR 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,-11),'YYYY.MM')=V1.DATE_MON 
OR 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,-10),'YYYY.MM')=V1.DATE_MON 
OR 
TO_CHAR(WDAT.WDAT_DATE,'YYYY.MM')=V1.DATE_MON 
OR 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,1),'YYYY.MM')=V1.DATE_MON 
OR 
TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,2),'YYYY.MM')=V1.DATE_MON 
)) VAR
where 
VS.CALC_VALUE=VAR.CALC_VALUE and
VS.DATE_YM_C=VAR.DATE_ROOMHOT
--and VS.DATE_YM_C='2011.09'
;


CREATE OR REPLACE VIEW V8_EDITOR_RES
AS
SELECT
  YRES.YRES_ID RESERVATION_ID,
  YRES.YRES_YBOM_ID BOOKINGMASTER_ID,
  YRES.YRES_CURRENT_YDET_ID RESERVATION_DETAIL_ID,
  YRES.YRES_EXPARRTIME ARRIVAL,
  YRES.YRES_EXPDEPTIME DEPARTURE,
  (YRES.YRES_EXPDEPTIME) - (YRES.YRES_EXPARRTIME) NIGHTS,
  TO_CHAR(YRES.YRES_EXPARRHOUR,'HH24:MI')                     ARRIVALTIME,
  TO_CHAR(YRES.YRES_EXPDEPHOUR,'HH24:MI')                     DEPARTURETIME,
  (select YDET.YDET_YCAT_ID from ydet where ydet.ydet_id=YRES.YRES_CURRENT_YDET_ID) YCAT_ID,
  (SELECT YDET.YDET_NOOFROOMS FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                               NOOFROOMS,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMTYPE,
  (SELECT YCAT.YCAT_LONGDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMTYPE_DESC,
  WTRL_TRANSLATE (
            'YCAT',
            (SELECT   YDET.YDET_YCAT_ID
               FROM   YDET
              WHERE   YDET.YDET_ID = YRES.YRES_CURRENT_YDET_ID),
            'YCAT_LONGDESC',
            NVL ( (SELECT   XCMS.XCMS_WLAN_ID
                     FROM   XCMS
                    WHERE   XCMS.XCMS_ID = YRES.YRES_XCMS_ID),
                 (SELECT   ygrp.YGRP_WLAN_ID
                    FROM   ygrp
                   WHERE   ygrp.YGRP_ID = yres.YRES_YGRP_ID))
         )
            TRANSLATED_TYPE_DESC,
  (SELECT YGCO.YGCO_SHORTDESC FROM YGCO WHERE YGCO.YGCO_ID=YRES.YRES_YGCO_ID)                                                        GUARANTEECODE,
  (SELECT YGCO.YGCO_LONGDESC FROM YGCO WHERE YGCO.YGCO_ID=YRES.YRES_YGCO_ID)                                                      GUARANTEECODE_DESC,
  DECODE(YRES.YRES_RESSTATUS,0,'TENTATIVE', 1,'DEFINITE',3,'NOT')                                                                 RESSTATUS,
  DECODE(YRES.YRES_NOAVAILREASON,1,'WAITLIST',2,'OFFER',3,'CANCELED',4,'NOSHOW')                                                  NOAVAILREASON,
  YRES.YRES_WALKIN                                                                                                                  WALKIN,
  YRES.YRES_FREETEXT1                                                                                                              FREETEXT1,
  YRES.YRES_FREETEXT2                                                                                                              FREETEXT2,
  NVL(YRES.YRES_HASSHARE,0)                                                                                                      HASSHARE,
  DECODE(YRES.YRES_HASSHARE,1,(DECODE(YRES.YRES_SHARETYPE,0,'INDEPENDANT',1,'MASTER',2,'SLAVE')),' ')                             SHARETYPE,
  (SELECT YDET.YDET_ADULTNO FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                              ADULTNO,
  NVL((SELECT SUM(YRGL_NUMBER) FROM YRGL,YDET WHERE YRGL.YRGL_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID ),0) CHILDNO,
  (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                   ROOM_ID,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMNUMBER,
  (SELECT YRMS.YRMS_LONGDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMNUMBER_LDESC,  
  (SELECT YDET.YDET_YCAT_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                ROOMTYPE_ID,
  (SELECT YDET.YDET_YCAT_ID_RATE FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                          RATEROOMTYPE_ID,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID_RATE AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) RATEROOMTYPE,
  (SELECT YCAT.YCAT_LONGDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID_RATE AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) RATEROOMTYPE_DESC,
  (SELECT YBLH.YBLH_ID FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BLOCK_ID,
  (SELECT YBLH.YBLH_SHORTDESC FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BLOCK_DESC,
  NVL((SELECT YDET.YDET_RATEAMOUNT FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                          RATEAMOUNT, --Daily rate per room + all packages--
  NVL((SELECT YDET.YDET_RATEDIFF FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                      RATEDIFF,
  NVL((SELECT YDET.YDET_RATEDISCOUNTPERC FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                              RATEDISCOUNTPERC,
  (SELECT NVL(YDET.YDET_RATEMANUAL,0) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                      RATEMANUAL, --Manual flagged rate.If add on package is flagged it is automatically added--
  (SELECT getYDetDiscountedAmountForeign(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_foreignrateamount,ydet_foreignratemanual,ydet_foreignratediff,ydet_foreignnodiscountpackage,ydet_finalAmount,ydet_finalForeignAmount)         
   FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) CONF_RATE, --Daily rate per room + all packages--
  V8_FNC_NUM_TO_CHAR_FORMATING((SELECT getYDetDiscountedAmountForeign(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_foreignrateamount,ydet_foreignratemanual,ydet_foreignratediff,ydet_foreignnodiscountpackage,ydet_finalAmount,ydet_finalForeignAmount)         
   FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)) CONF_RATE_GS,-- Formatted Daily rate per room + all packages--
  V8_FNC_VALCURR_FORMATING(
  (SELECT getYDetDiscountedAmountForeign(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_foreignrateamount,ydet_foreignratemanual,ydet_foreignratediff,ydet_foreignnodiscountpackage,ydet_finalAmount,ydet_finalForeignAmount)         
   FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),(SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id)) FCONF_RATE_GS, -- Formatted Daily rate per room + all packages with currency--
   (SELECT SUM(YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID = YRES.YRES_ID AND YPOS.YPOS_TYPE <> 1) CONF_RATE_TOTAL, --Rate + all packages per stay per room--
   (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1) CONF_RATE_STAY, --Total rate per stay + all packages--
   V8_FNC_NUM_TO_CHAR_FORMATING((SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1)) CONF_RATE_STAY_GS, -- Formatted total rate per stay + all packages--
   V8_FNC_VALCURR_FORMATING((SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1),
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id)) FCONF_RATE_STAY_GS, -- Formatted total rate per stay + all package with currency--
   (SELECT  getYDetDiscountedAmountForeign(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_foreignrateamount,ydet_foreignratemanual,ydet_foreignratediff,ydet_foreignnodiscountpackage,ydet_finalAmount,ydet_finalForeignAmount)           
   FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) 
   + NVL((SELECT SUM(decode(ydet_foreignrateamount,null,yfxc_amount,nvl(yfxc_foreignamount,yfxc_amount))*YFXC.YFXC_QUANTITY) FROM YFXC,YDET WHERE YFXC.YFXC_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) CONF_RATE_FIXCHARGE, -- Conf_rate + daily fixed charges
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID) CONF_RATE_FIXCHARGE_STAY, --Total rate per stay + all packages + fixed charges --
  (SELECT SUM (YPOS.YPOS_UNITPRICE) FROM YPOS,YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND YPOS.YPOS_YRES_ID = YRES.YRES_ID AND YDET.YDET_DATE=YPOS.YPOS_DATE AND YPOS.YPOS_DT <> 2 AND YPOS.YPOS_TYPE<>1) CONF_RATE_NET,--Daily rate per room + all packages Net --
  (SELECT SUM(YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID = YRES.YRES_ID AND YPOS.YPOS_TYPE <> 1 AND YPOS.YPOS_DT <> 2 ) CONF_RATE_NET_TOTAL, --Rate + all packages per stay per room Net-- 
  (SELECT SUM (YPOS.YPOS_QUANTITY * YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID = YRES.YRES_ID AND YPOS.YPOS_TYPE <> 1 AND YPOS.YPOS_DT <> 2 ) CONF_RATE_NET_STAY, --Total rate per stay + all packages Net--  
  (SELECT getYDetDiscountedAmountForeign(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_foreignrateamount,ydet_foreignratemanual,ydet_foreignratediff,ydet_foreignnodiscountpackage,ydet_finalAmount,ydet_finalForeignAmount)
         -NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED, 0, decode(ydet_foreignrateamount,null,YPAL_VALUE,ypal_foreignvalue)*ypal_multiplier, 0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0) 
   FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) CONF_RATE_NOADDON, -- Conf_rate excluding add on package--
  V8_FNC_NUM_TO_CHAR_FORMATING(
  (SELECT getYDetDiscountedAmountForeign(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_foreignrateamount,ydet_foreignratemanual,ydet_foreignratediff,ydet_foreignnodiscountpackage,ydet_finalAmount,ydet_finalForeignAmount)
         -NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED, 0, decode(ydet_foreignrateamount,null,YPAL_VALUE,ypal_foreignvalue)*ypal_multiplier, 0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0) 
   FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)) CONF_RATE_NOADDON_GS, --Formatted Conf_rate excluding add on package--
  (SELECT  getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount)
          -NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,decode(ydet_foreignrateamount,null,YPAL_VALUE,ypal_foreignvalue)*YPAL.YPAL_MULTIPLIER,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0) 
   FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) 
   + NVL((SELECT SUM(decode(ydet_foreignrateamount,null,yfxc_amount,nvl(yfxc_foreignamount,yfxc_amount))*YFXC.YFXC_QUANTITY) FROM YFXC,YDET WHERE YFXC.YFXC_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) CONF_RATE_NOADDON_FIXCHARGE, --Conf_rate excluding add on  packages + all fixed charges--
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id) Currency,
  ((SELECT getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount)  
    FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)) CALC_PRICE, -- Same as conf_rate Depreciated, please use conf_rate.
  (SELECT YDET_FINALAMOUNT FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) FINAL_AMOUNT, --added just for controlling purpose
  (SELECT (NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                         ADD_ON_PACKAGES, -- Total add on packages first night--
  NVL((SELECT SUM(YFXC.YFXC_AMOUNT*YFXC.YFXC_QUANTITY) FROM YFXC,YDET WHERE YFXC.YFXC_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                                                     TOTAL_FIX_CHARGES, -- Total fidex charges first night--
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE=1) FIXCHARGE_STAY, --Total fixed charges for the stay--
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS,YDET WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID and ydet_id=nvl(YRES.YRES_CURRENT_YDET_ID,YRES.YRES_LAST_YDET_ID) and YDET.YDET_DATE=YPOS.YPOS_DATE AND YPOS.YPOS_DT<>2 )        RATE_NET, -- Daily Rate + all packages + Fixed charges All rooms Net-- 
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID  AND YPOS.YPOS_DT<>2 )                                                                                                                  RATE_NET_STAY,--Total rate per stay + all packages + fixed charges Net (CONF_RATE_FIXCHARGE_STAY NET)--
  (SELECT YDET.YDET_YRCH_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                                               RATECODE_ID,
  (SELECT YRCH.YRCH_SHORTDESC FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE,
  (SELECT YRCH.YRCH_LONGDESC FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_DESC,
  (SELECT YRCH.YRCH_RATEINFO FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_INFO,
DECODE ((SELECT YRCH.YRCH_BOARDTYPE FROM YRCH, YDET WHERE YRCH.YRCH_ID = YDET.YDET_YRCH_ID AND YDET.YDET_ID = YRES.YRES_CURRENT_YDET_ID),
               0,'No Board',1,'Breakfast',2,'Breakfast and Lunch',3,'Breakfast and Dinner',4,'Full Board',5,'All Inclusive')BOARDTYPE,
  (SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_TEXT,
  DECODE((NVL((SELECT YRCH.YRCH_COMPSTATUS FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)),1,'COMP',2,'HOUSE USE','')                                                     COMPSTATUS,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN 1 ELSE 0 END)                                                                                                                                       UNSPLITTED_GROUP_RES,
  YRES.YRES_XCMS_ID                                                                                                                                                                                                                   GUEST_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID)                                                                                                                                     GUEST_NAME,
  (SELECT V8_REP_NAME.NAME3 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID)                                                                                                                                     GUEST_GREET_NAME,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'DD')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_DAY,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'MM')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_MONTH,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'YYYY')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_YEAR,
  (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID)                                                                                                                                                     GROUP_RESERVATION_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID) END) NAME,
  (SELECT YGRP.YGRP_NAME FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                  GROUP_DETAIL_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT YGRP.YGRP_NAME FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) END) GROUP_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT YGRP.YGRP_ID FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) END) GROUP_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) COMPANY_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) TRAVELAGENT_NAME,
  (SELECT XCMS_ID FROM XCMS,YCLN,YDET WHERE XCMS.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                         TRAVELAGENT_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=3 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) SOURCE_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=4 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BOOKER_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=5 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) VENDOR_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=6 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) MPLANER_NAME,
  (SELECT YDET.YDET_XCMA_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                                               MARKETCODE_ID,
  (SELECT XCMA.XCMA_SHORTDESC FROM XCMA,YDET WHERE XCMA.XCMA_ID=YDET.YDET_XCMA_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                             MARKETCODE_DESC,
  (SELECT YRAS.YRAS_ID FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                                   SOURCECODE_ID,
  (SELECT YRAS.YRAS_SHORTDESC FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                           SOURCECODE_DESC,
  (SELECT YRAS.YRAS_ID FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                                   CHANNELCODE_ID,
  (SELECT YRAS.YRAS_SHORTDESC FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                           CHANNELCODE_DESC,
  (SELECT COUNT(YRES_S.YRES_ID) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                     NO_OF_PREV_STAYS,
  (SELECT COUNT(YDET.YDET_ID) FROM YDET,YRES YRES_S WHERE YDET.YDET_YRES_ID=YRES_S.YRES_ID AND YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)         NO_OF_PREV_ROOM_NIGHTS,
  (SELECT XPDA.XPDA_SHORTDESC FROM XPDA,XPAL WHERE XPDA.XPDA_ID=XPAL.XPAL_XPDA_ID AND XPAL.XPAL_XPAC_ID=11 AND XPAL.XPAL_XCMS_ID=YRES.YRES_XCMS_ID)                                                                       VIPCODE,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN 1 ELSE 0 END)                                                                                     EXP_ROOM_MOVE_TODAY,
  (CASE WHEN YRES.YRES_YRMS_ID=(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) AND YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) THEN 1 ELSE 0 END) ROOM_MOVE_DONE_TODAY,
  (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate)                                                                                             TODAY_ROOM_MOVE_TO_ROOM_ID,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate)                                         TODAY_ROOM_MOVE_TO_ROOM,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN YRES.YRES_YRMS_ID ELSE (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) END) TODAY_ROOM_MOVE_FROM_ROOM_ID,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN (SELECT YRMS.YRMS_SHORTDESC FROM YRMS WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID) ELSE (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) END) TODAY_ROOM_MOVE_FROM_ROOM,
  (SELECT MIN(TRUNC(YRES_S.YRES_ACTARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    FIRST_ARRIVAL,
  (SELECT MAX(TRUNC(YRES_S.YRES_ACTARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    LAST_ARRIVAL,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET YDET_S WHERE YRMS.YRMS_ID=YDET_S.YDET_YRMS_ID AND YDET_S.YDET_ID=(SELECT MAX(YDET.YDET_ID) FROM YDET WHERE YDET.YDET_YRES_ID=(SELECT YRES_S2.YRES_ID FROM YRES YRES_S2 WHERE TRUNC(YRES_S2.YRES_ACTDEPTIME)=(SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3) AND ROWNUM=1))) LAST_ROOMNUMBER,
  (SELECT MIN(TRUNC(YRES_S.YRES_EXPARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPARRTIME>YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    NEXT_ARRIVAL,
  (SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                 LAST_DEPARTURE,
  (SELECT  getYDetDiscountedAmountForeign(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_foreignrateamount,ydet_foreignratemanual,ydet_foreignratediff,ydet_foreignnodiscountpackage,ydet_finalAmount,ydet_finalForeignAmount)           
   FROM YDET WHERE YDET.YDET_ID=(SELECT MAX(YDET.YDET_ID) FROM YDET WHERE YDET.YDET_YRES_ID=(SELECT YRES_S2.YRES_ID FROM YRES YRES_S2 WHERE TRUNC(YRES_S2.YRES_ACTDEPTIME)=(SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3) AND ROWNUM=1))) LAST_CONF_RATE,  YRES.YRES_OPTION                                                                                                                                                                                                                         OPTIONDATE,
  (SELECT YCXL_3.YCXL_NUMBER FROM YCXL YCXL_3 WHERE YCXL_3.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                             CANCELLATION_NUMBER,
  (SELECT YCXL.YCXL_DATE FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                          CANCELLATION_DATE,
  (SELECT YCXL.YCXL_REASON FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                          CANCELLATION_REASON,
  (SELECT TRUNC(YCXL.YCXL_PCDATE) FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                  CANCELLATION_PCDATE,
  (SELECT TO_CHAR(YCXL.YCXL_PCDATE,'HH24:MI') FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                     CANCELLATION_PCTIME,
  DECODE(NVL((SELECT MAX(YWLT.YWLT_ID) FROM YWLT WHERE YWLT.YWLT_YRES_ID=YRES.YRES_ID AND YWLT.YWLT_ACCEPTEDDATE IS NULL),0),0,0,1)                                                                                           IS_WAITLIST,
  (SELECT YWLP.YWLP_LONGDESC FROM YWLP WHERE YWLP.YWLP_ID=(SELECT YWLT.YWLT_YWLP_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))  WAITLIST_DESC,
  (SELECT YWLP.YWLP_PRIORITY FROM YWLP WHERE YWLP.YWLP_ID=(SELECT YWLT.YWLT_YWLP_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))  WAITLIST_PRIORITY,
  (SELECT XCOM.XCOM_VALUE FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))    WAITLIST_COMM,
  (SELECT XCMT.XCMT_LONGDESC FROM XCMT WHERE XCMT.XCMT_ID=(SELECT XCOM.XCOM_XCMT_ID FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))) WAITLIST_COMM_TYPE,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END)                                                                             COUNTRY_ID,
  (SELECT XCOU.XCOU_CUSTSHORTDESC FROM XCOU WHERE XCOU.XCOU_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END)) COUNTRY_SHORTDESC,
  (SELECT XCOU.XCOU_LONGDESC FROM XCOU WHERE XCOU.XCOU_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END))      COUNTRY_LONGDESC,
  (SELECT SUM(YDEP.YDEP_AMOUNT_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID)                  DEPOSIT_REQUESTED,
  (SELECT MAX(YDEP.YDEP_DUEDATE_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID)              DEPOSIT_LIMIT,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.YRES_ID) DEPOSIT_PAID,
  (SELECT MAX(ZPOS.ZPOS_POSTDATE) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.YRES_ID)                                DEPOSIT_POSTDATE,
  (SELECT XCED.XCED_INITIALS FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                     ENTERED_BY_USER,
  (SELECT DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1)) FROM WMLG, XCED, XCMS WHERE XCMS.XCMS_ID=XCED.XCED_ID AND WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                     ENTERED_BY_USERNAME,
  (SELECT TO_DATE(WMLG.WMLG_TIME_CREATION) FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                ENTERED_WHEN,
  (SELECT TO_CHAR(WMLG.WMLG_TIME_CREATION,'HH24:MI') FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                ENTERED_WHEN_TIME,
  (SELECT TRUNC(WMLG.WMLG_TIME_UPDATE) FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                UPDATED_WHEN_DATE,
  (SELECT XPDC.XPDC_VALUE FROM XPDC, XPDG WHERE XPDC.XPDC_XPDG_ID = XPDG.XPDG_ID AND XPDG.XPDG_ROLE=1 AND XPDC.XPDC_XCMS_ID = YRES.YRES_XCMS_ID AND ROWNUM=1)                    PASSPORT,
  (SELECT XCOU.XCOU_NATIONALITY FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                   NATIONALITY,
  (SELECT XCOU.XCOU_ISO2 FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                       NATIONALITY_ISO2,
  (SELECT XCOU.XCOU_ISO3 FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                       NATIONALITY_ISO3,
  (SELECT COUNT(YRES_ID) FROM YRES NS WHERE NS.YRES_NOAVAILREASON = 4 AND NS.YRES_XCMS_ID = YRES.YRES_XCMS_ID)                                                        NOSHOWS,
  (SELECT COUNT(YRES_ID) FROM YRES NS WHERE NS.YRES_NOAVAILREASON = 3 AND NS.YRES_XCMS_ID = YRES.YRES_XCMS_ID) CANCELLATIONS,
  YRES_YRFR_ID,
  (select ZDCO_LONGDESC from ZDCO where YRES.YRES_DEFPAYMETH_ZDCO_ID  = zdco_id) Payment_Method,
  V8_Fnc_Ypal_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,1) PACKAGES_SHORT,
  V8_Fnc_Ypal_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,2) PACKAGES_LONG,
  decode(yres_printrate,1,'Y','N') PRINT_RATE,
  V8_Fnc_Yrft_Rep_Infos(YRES.YRES_ID,1,1) YRES_YRFT_SHORT,
  V8_Fnc_Yrft_Rep_Infos(YRES.YRES_ID,1,2) YRES_YRFT_LONG,
  V8_Fnc_Xpda_Rep_Infos((DECODE((SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18),0,YRES.YRES_XCMS_ID,1,YRES.YRES_ID,2,YRES.YRES_CURRENT_YDET_ID)),18,1,(SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18)) RES_SPECIALS_SHORT,
  V8_Fnc_Xpda_Rep_Infos((DECODE((SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18),0,YRES.YRES_XCMS_ID,1,YRES.YRES_ID,2,YRES.YRES_CURRENT_YDET_ID)),18,2,(SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18)) RES_SPECIALS_LONG,
  (select WFLT_CODE from V8_REP_TRANSPORTATION where V8_REP_TRANSPORTATION.YRFL_YRES_ID = yres_id and V8_REP_TRANSPORTATION.YRFL_ARRDEP = 0) TRANSPORT_ARR_CODE,
  (select WFLT_CODE from V8_REP_TRANSPORTATION where V8_REP_TRANSPORTATION.YRFL_YRES_ID = yres_id and V8_REP_TRANSPORTATION.YRFL_ARRDEP = 1) TRANSPORT_DEP_CODE,
   YRES.YRES_YPRT_ID YPRT_ID,
  (SELECT YPRT.YPRT_LONGDESC FROM YPRT WHERE YPRT.YPRT_ID=YRES.YRES_YPRT_ID) YPRT_LONGDESC,
  (SELECT YPRT.YPRT_COMMENT FROM YPRT WHERE YPRT.YPRT_ID=YRES.YRES_YPRT_ID) YPRT_COMMENT,
  (SELECT COUNT(yres_2.YRES_ID) FROM YRES yres_2 WHERE yres_2.YRES_YPRT_ID=YRES.YRES_YPRT_ID AND yres_2.YRES_RESSTATUS<>3) TOTAL_PARTY_MEMBERS,
  V8_Fnc_Yprt_Rep_Infos(YRES.YRES_ID,YRES.YRES_YPRT_ID,3) PARTY_MEMBER
FROM
YRES;


-- View to get Revenue Information, this is for the detailed Folio Style

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO
AS 
SELECT
--  distinct
  ZPOS.ZPOS_POSTDATE DAT,
  ZPOS.ZPOS_CDT CDT,
  ZPOS.ZPOS_ID ZPOS_ID,
  DECODE (ZDCO.ZDCO_PAYM_TYPE,2,1,0) IS_CL,
  nvl2((to_char((SELECT trunc(YRES.YRES_EXPDEPTIME) FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),'J')),'-',NULL) MINUSI,
  (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)))                                       GROSS,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))                                                                                                  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)))                                                                          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))                                                                                                                         UNIT,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*((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)))/ZPOS.ZPOS_QUANTITY))                                                                                                         FOREIGN_UNIT,
  zpos_exchangerate EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,1)) CURRENCY,      
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                                              SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_CL,
--  (CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
--  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                     SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
 -- (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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),(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))),/*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))))))) ELSE ZPOS.ZPOS_DESCRIPT END) DDESC,
 -- RPAD((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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),(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))),/*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))))))) ELSE ZPOS.ZPOS_DESCRIPT END),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((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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),(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))),/*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))))))) ELSE ZPOS.ZPOS_DESCRIPT END),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((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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),(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))),/*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))))))) ELSE ZPOS.ZPOS_DESCRIPT END),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,
decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_ORG_NAME,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_COMMENT_ORG,
  RPAD(decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select yrch_LONGDESC from yrch where yrch_id=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_LONGDESC from YPAC where YPAC_id=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_foliotext from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      DDESC_LF_COM_ORG_ALL,
  ZPOS.ZPOS_PHONENR                                                                   PHONE,
  (case when nvl(ZPOS.ZPOS_PHONENR,'0')<>'0' then SUBSTR(ZPOS.ZPOS_PHONENR,1,LENGTH(ZPOS.ZPOS_PHONENR)-3)||'XXX' else ZPOS.ZPOS_PHONENR END) PHONE_XXX,
  ZDCO.ZDCO_ID                                                                        ZDCO_ID,
  ZDCO.ZDCO_NUMERICDESC                                                            NDESC,
  ZPOS.ZPOS_TAXLINK_ID                                                                   TAXLINK_ID,
  --(DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),/*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(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))
                                       )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,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YRES.YRES_CURRENT_YDET_ID=YDET.YDET_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(NVL((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 ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE 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,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YRES.YRES_CURRENT_YDET_ID=YDET.YDET_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(NVL((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 ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE 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,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YRES.YRES_CURRENT_YDET_ID=YDET.YDET_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(NVL((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 ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE 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,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YRES.YRES_CURRENT_YDET_ID=YDET.YDET_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(NVL((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 ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE 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)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)*100,'999999990,09') FSPAY_CL,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID,
  (SELECT XCCS.XCCS_NUMBER FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) CreditCardNumber,
  (SELECT 'XXXX XXXX XXXX '||SUBSTR(XCCS.XCCS_NUMBER,LENGTH(XCCS.XCCS_NUMBER)-3,4) FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) HiddenCreditCardNumber,
  (SELECT YRES.YRES_CRSRESNR FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) CRSRESNR,
  (SELECT TRUNC(YRES.YRES_EXPARRTIME) FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) ARRIVAL,
  (SELECT trunc(YRES.YRES_EXPDEPTIME) FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) DEPARTURE,
  (SELECT max(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zpos.zpos_zdco_id AND zpos.zpos_postdate BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime) taxcode,
  (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS.ZPOS_ID 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=ZPOS.ZPOS_ID and Z2.ZPOS_ZTCD_ID is not null)) end ) TAXCODE_DESC,
  (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS.ZPOS_ID and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ) MULTI_TAXCODE_EXISTS,
  decode(nvl(ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS_ORIGINATED_YRES_ID,ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID,
  decode(ZPOS.ZPOS_CDT,5,CHR(10)||'     ' || DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),to_char(null)) PAY_LF_COMMENT,
  decode(nvl(zpos_originated_yres_id,0),0,1,0) ZFAC_OR_YRES,
  -- (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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),(DECODE(NVL(ZDCO.ZDCO_LONGDESC,' '),' ',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))),/*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_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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))))))) ELSE ZPOS.ZPOS_DESCRIPT END) ZDCO_LONGDESC,
 -- RPAD((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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),(DECODE(NVL(ZDCO.ZDCO_LONGDESC,' '),' ',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))),/*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_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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))))))) ELSE ZPOS.ZPOS_DESCRIPT END),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))))) ZDCO_LONGDESC_LF_ORG_NAME,
  --  RPAD((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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),(DECODE(NVL(ZDCO.ZDCO_LONGDESC,' '),' ',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))),/*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_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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))))))) ELSE ZPOS.ZPOS_DESCRIPT END),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))))) ZDCO_LONGDESC_LF_COMMENT_ORG,
   -- RPAD((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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),(DECODE(NVL(ZDCO.ZDCO_LONGDESC,' '),' ',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))),/*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_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))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))))))) ELSE ZPOS.ZPOS_DESCRIPT END),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))) ZDCO_LONGDESC_LF_COM_ORG_ALL
  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))
                                       )ZDCO_LONGDESC,
  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))))) 
                                      ZDCO_LONGDESC_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))))) 
                                     ZDCO_LONGDESC_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))) 
                                     ZDCO_LONGDESC_LF_COM_ORG_ALL
FROM
  ZPOS,
  ZORG,
  ZWIN,
  ZDCO
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS.ZPOS_ID and
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0));


-- View to get Revenue Information, this is for the Arrangement together per Day Folio Style

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_ARR_PD
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, 
  ZDCO_ID_S ZDCO_ID, 
  REC_COUNT_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, NVL( WTRL_TRANSLATE('ZARR',(select ZARR_ID from ZARR where ZARR_ID=(select ZPOS_ZARR_ID from ZPOS where ZPOS.ZPOS_ZARR_ID=ZARR_ID_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_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_ZDCO_ID) ZDCO_ID_S,
  NTD.ZPOS_ZARR_ID ZARR_ID_S, 
  COUNT(1) REC_COUNT_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_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_ZARR_ID, ntd.ZPOS_POSTDATE, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID 
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0)) 
--and ZWIN_ID_S=:zwin_id 
 ORDER BY CDT ASC;


-- View to get Revenue Information, this is for the Arrangement together per Stay Folio Style

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_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, 
  ZDCO_ID_S ZDCO_ID, 
  REC_COUNT_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, 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 
  MAX(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S, 
  MIN(ntd.ZPOS_ID) ZPOS_ID_S, 
  MIN(ntd.ZPOS_ZDCO_ID) ZDCO_ID_S, 
  COUNT(1) REC_COUNT_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_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, 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 V8_ED_FOLIO_REV_INFO_REV_GCHK to realize detailed Folio, with Guest Cheque Postings together

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_REV_GCHK
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  (DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET,YRES WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=NVL(YRES.YRES_CURRENT_YDET_ID,YRES.YRES_LAST_YDET_ID) AND YRES.YRES_ID=(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S)),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S)))) ORG_ROOM,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  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,
  TAXLINK_ID_S TAXLINK_ID,
  NVL(PAY_S,0) PAY,
  NVL(REV_S,0) REV,
  TO_CHAR(NVL(PAY_S,0)*100,'999999990,09') FPAY,
  TO_CHAR(NVL(REV_S,0)*100,'999999990,09') FREV,
  NVL(REV_S,0)/(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)) UNIT,
  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 ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) SPAY,
  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) SPAY_CL,
  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) 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=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,
RPAD(
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)))))
            ,30,' '||(DECODE((select ZPOS.ZPOS_XCMS_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s),' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from ZPOS where ZPOS_ID=ZPOS_ID_S))||(DECODE((DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=ZPOS_ID_S),0),0,(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from ZPOS where ZPOS_ID=ZPOS_ID_S))),(DECODE(NVL((select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=ZPOS_ID_S),0),0,(select ZPOS.ZPOS_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s))),'',(DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s)),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s)))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))))) 
          ) DDESC_LF_ORG_NAME,
RPAD(
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)))))
            ,30,' '||nvl2(DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id_S), 0 ), 0, DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),  getccmzposcommenttranslated((select ZPOS.ZPOS_ID from zpos where zpos_id=zpos_id_s)) ),TO_CHAR(NULL))||(DECODE((select ZPOS.ZPOS_XCMS_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s),' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s))||(DECODE((DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s))),(DECODE(NVL((select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s))),'',(DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_ID=ZPOS_ID_S),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s)),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s)))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))))) 
          )DDESC_LF_COMMENT_ORG,
RPAD(
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)))))
              ,30,' '||nvl2(DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s))||(DECODE((DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s))),(DECODE(NVL((select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s))),'',(DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s)),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s)))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))) 
            ) DDESC_LF_COM_ORG_ALL,
  (SELECT ZPOS.ZPOS_PHONENR FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) PHONE,
  (SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=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=ZPOS_ID_S)) NDESC,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name,ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S AND v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))) ORG_NAME,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name,ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S AND v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL((SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S))),(DECODE(NVL((SELECT ZPOS.ZPOS_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT ZPOS.ZPOS_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S))),'',(DECODE(NVL((SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES,ZPOS WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC,ZPOS WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))) ORG_NAME_ROOM,
  (DECODE((SELECT ZPOS.ZPOS_XCMS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ORIGINATED_XCMS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),TO_CHAR(NULL),
   (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name,ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S AND v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL((SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S))),(DECODE(NVL((SELECT ZPOS.ZPOS_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT ZPOS.ZPOS_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S))),'',(DECODE(NVL((SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES,ZPOS WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC,ZPOS WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))))) ORG_ROOM_IF,
  DECODE(INDICATE_ZPOS_ID,0,(DECODE(NVL((SELECT ZDCO.ZDCO_COMMENTDISABLED FROM ZDCO WHERE ZDCO.ZDCO_ID=(SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S)),0),0,(SELECT ZPOS.ZPOS_COMMENT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),TO_CHAR(NULL))),TO_CHAR(NULL)) ZCHK_COMMENT,
 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,
 (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' 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=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)) end ) TAXCODE_DESC,
 (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ) MULTI_TAXCODE_EXISTS,
 ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(ntd.ZPOS_TAXLINK_ID) TAXLINK_ID_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR) ZCHK_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_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR), DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0)
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
order by CDT ASC;


-- VIEW V8_ED_FOLIO_REV_INFO_LB added to use the functionality from TT 10031

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_LB
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)))                                       GROSS,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))                                                                                                  PAY,
  TO_CHAR((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))*100,'999999990,09')                                                                      FPAY,
  (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,
  TO_CHAR((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)))*100,'999999990,09')                                                                           FREV,
  (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))                                                                                                                         UNIT,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                                              SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                     SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)*100,'999999990,09') FSPAY_CL,
  decode(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,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZDCO.ZDCO_SUBFUNCTION,0)=0 AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
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,
  NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)+NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)                                       GROSS,
  TO_NUMBER(0)                                                                                                  PAY,
  TO_CHAR(TO_NUMBER(0)*100,'999999990,09')                                                                   FPAY,
  NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)+NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)                                                                         REV,
  TO_CHAR((NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)+NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0))*100,'999999990,09')                                             FREV,
  ((NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)+NVL((SELECT SUM(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND NVL(zpos_s.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=ZDCO.ZDCO_ID AND ZDCO.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=ZPOS.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0))/(DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY)))                                                                                                                UNIT,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                                              SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                     SREV,
--   (SELECT sum(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND nvl(zpos_s.ZPOS_HIDDEN,0)=0 AND nvl(zpos.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=zdco.ZDCO_ID AND zdco.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=zpos.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = zwin.ZWIN_ID) SUM_SUB2,
--   (SELECT sum(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND nvl(zpos_s.ZPOS_HIDDEN,0)=0 AND nvl(zpos.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=zdco.ZDCO_ID AND zdco.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=zpos.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = zwin.ZWIN_ID) SUM_SUB1,
--   nvl((SELECT sum(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND nvl(zpos_s.ZPOS_HIDDEN,0)=0 AND nvl(zpos.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=zdco.ZDCO_ID AND zdco.ZDCO_SUBFUNCTION=2 AND zpos_s.ZPOS_POSTDATE-1=zpos.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = zwin.ZWIN_ID),0)+nvl((SELECT sum(DECODE(zpos_s.zpos_cdt,5,DECODE(zpos_s.zpos_paidout,1, -zpos_s.zpos_unitprice*zpos_s.zpos_quantity, NULL),DECODE(zpos_s.ZPOS_CDT,5,0, zpos_s.zpos_sumupamount))) FROM ZPOS ZPOS_S,ZDCO WHERE (zpos_s.zpos_zall_id IS NULL OR zpos_s.zpos_zall_id=0) AND zpos_s.zpos_id=zpos_s.zpos_internalsplit_zpos_id AND nvl(zpos_s.ZPOS_HIDDEN,0)=0 AND nvl(zpos.ZPOS_NOREPRINT,0)<>1 AND zpos_s.ZPOS_ZDCO_ID=zdco.ZDCO_ID AND zdco.ZDCO_SUBFUNCTION=1 AND zpos_s.ZPOS_POSTDATE=zpos.ZPOS_POSTDATE AND zpos_s.ZPOS_ZWIN_ID = zwin.ZWIN_ID),0) SUM_SUB12,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)*100,'999999990,09') FSPAY_CL,
  decode(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,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZDCO.ZDCO_SUBFUNCTION,0)=1 AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1;
  
  
  
-- View for folio style Department code per Day for Internal Editor

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_DCO_PD
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  DECODE(ZDCO_PAYM_TYPE_S,2,1,0) IS_CL,
  ZDCO_ID_S ZDCO_ID,
  TAXLINK_ID_S TAXLINK_ID,
  COUNTER_S QTY,
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
   TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
   AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
   AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO WHERE ZDCO.ZDCO_ID=ZDCO_ID_S) ZDCO_NUMERICDESC,
  Wtrl_Translate('ZDCO',ZDCO_ID_S,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) ZDCO_LONGDESC,
  Wtrl_Translate('ZDCO',ZDCO_ID_S,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) ZDCO_FOLIOTEXT,
--  nvl((decode((select xcca.XCCA_XCMSTYPE from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=(select zwin.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.ZWIN_ID=ZWIN_ID_S)),2,(select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=(select zwin.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.ZWIN_ID=ZWIN_ID_S)),(select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=(select zwin.ZWIN_INVOICE_TO_CONTACT_ID from zwin where zwin.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S))) WLANID,
(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,
  (CASE WHEN (SELECT max(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime) = (SELECT min(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime) THEN (SELECT max(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime) ELSE 0 END) taxcode, 
  (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ) MULTI_TAXCODE_EXISTS
FROM
(
SELECT
--  distinct
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_TAXLINK_ID) TAXLINK_ID_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(ZDCO_PAYM_TYPE) ZDCO_PAYM_TYPE_S,
  COUNT(zdco_id) COUNTER_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  zdco_id ZDCO_ID_S
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=ntd.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
GROUP BY ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSTDATE, zdco_id 
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0)) ORDER BY CDT ASC;


   
-- View for folio style Department code per Stay for Internal Editor   
   
CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_DCO_PS
AS 
SELECT
  MIN_ZPOS_POSTDATE_S MIN_DAT,
  MAX_ZPOS_POSTDATE_S MAX_DAT,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  DECODE(ZDCO_PAYM_TYPE_S,2,1,0) IS_CL,
  ZDCO_ID_S ZDCO_ID,
  TAXLINK_ID_S TAXLINK_ID,
  COUNTER_S QTY,
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
   TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
   AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
   AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO WHERE ZDCO.ZDCO_ID=ZDCO_ID_S) ZDCO_NUMERICDESC,
  Wtrl_Translate('ZDCO',ZDCO_ID_S,'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) ZDCO_LONGDESC,
  Wtrl_Translate('ZDCO',ZDCO_ID_S,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) ZDCO_FOLIOTEXT,
--  nvl((decode((select xcca.XCCA_XCMSTYPE from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=(select zwin.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.ZWIN_ID=ZWIN_ID_S)),2,(select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=(select zwin.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.ZWIN_ID=ZWIN_ID_S)),(select xcms.XCMS_WLAN_ID from xcms where xcms.XCMS_ID=(select zwin.ZWIN_INVOICE_TO_CONTACT_ID from zwin where zwin.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S))) WLANID,
(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,
  (CASE WHEN (SELECT max(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND (max_zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime OR zdtl.zdtl_fromtime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s OR zdtl.zdtl_totime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s)) = (SELECT min(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND (max_zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime OR zdtl.zdtl_fromtime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s OR zdtl.zdtl_totime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s)) THEN (SELECT max(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zdco_id_s AND (max_zpos_postdate_s BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime OR zdtl.zdtl_fromtime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s OR zdtl.zdtl_totime BETWEEN min_zpos_postdate_s AND max_zpos_postdate_s)) ELSE 0 END) taxcode, 
  (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ) MULTI_TAXCODE_EXISTS
FROM
(
SELECT
--  distinct
  MIN(ntd.ZPOS_POSTDATE) MIN_ZPOS_POSTDATE_S,
  MAX(ntd.ZPOS_POSTDATE) MAX_ZPOS_POSTDATE_S,  
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(ZDCO_PAYM_TYPE) ZDCO_PAYM_TYPE_S,
  COUNT(zdco_id) COUNTER_S,
  MIN(ntd.ZPOS_TAXLINK_ID) TAXLINK_ID_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  zdco_id ZDCO_ID_S
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
GROUP BY ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID, zdco_id 
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0)) ORDER BY CDT ASC;


-- View for Folio Arrangement Code per Guest and Day

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_XCMS_ARR_PD
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  -- 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,
  (SELECT v8_rep_name.NAME6 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ORI_XCMS_ID) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZARR',(select ZARR_ID from ZARR where ZARR_ID=(select ZPOS_ZARR_ID from ZPOS where ZPOS.ZPOS_ZARR_ID=ZARR_or_zpos_S and ZPOS.ZPOS_ID=ZPOS_ID_S)),'ZARR_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode((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
--  distinct
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  ntd.ZPOS_ORIGINATED_XCMS_ID ORI_XCMS_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
ZORG.ZORG_ZPOS_ID=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
GROUP BY ZWIN.ZWIN_ID,  ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSTDATE,ntd.ZPOS_ORIGINATED_XCMS_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0)) ORDER BY CDT ASC;



-- View for Folio Arrangement per Guest and Stay

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_XCMS_ARR_PS
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
  (SELECT v8_rep_name.NAME6 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ORI_XCMS_ID) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZARR',(select ZARR_ID from ZARR where ZARR_ID=(select ZPOS_ZARR_ID from ZPOS where ZPOS.ZPOS_ZARR_ID=ZARR_or_zpos_S and ZPOS.ZPOS_ID=ZPOS_ID_S)),'ZARR_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode((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
(
