insert into {p_mydt10tbl1}
(tbl, tbl_nr01, p00, p01, p03, 
series, aa, issuedate, invtype, 
is_vat, is_name, is_address, is_city, is_zip, 
cp_vat, cp_name, cp_address, cp_city, cp_zip)

SELECT tbl as tbl, 
       tbl_nr01 as tbl_nr01, 
       p00 as p00, 
       p01 as p01, 
       p03 as p03, 
       series as series, 
       peg_abschr(peg_abschr(aa, ''), "ABCDEFGHIJKLMNOPQRSTUVWXYZ~`@#$%^&*()-+ /?.>,<'") as aa, 
       issuedate as issuedate, 
       invtype as invtype, 
       if( mkfs in (2, 3), '{nt_p101[1]}', a_vat)     as is_vat, 
       if( mkfs in (2, 3), '{nt_p101[2]}', a_name)    as is_name, 
       if( mkfs in (2, 3), '{nt_p101[3]}', a_address) as is_address, 
       if( mkfs in (2, 3), '{nt_p101[4]}', a_city)    as is_city, 
       if( mkfs in (2, 3), '{nt_p101[5]}', a_zip)     as is_zip, 
       if( mkfs not in (2, 3), '{nt_p101[1]}', a_vat)     as cp_vat, 
       if( mkfs not in (2, 3), '{nt_p101[2]}', a_name)    as cp_name, 
       if( mkfs not in (2, 3), '{nt_p101[3]}', a_address) as cp_address, 
       if( mkfs not in (2, 3), '{nt_p101[4]}', a_city)    as cp_city, 
       if( mkfs not in (2, 3), '{nt_p101[5]}', a_zip)     as cp_zip

FROM
(
      /***********************************************
             Παραστατικά προς αποστολή
             q30.nr01=620012
      ***********************************************/
      SELECT l04.p405 as mkfs,   
             '{p_mydt10arr[1]}' as tbl, 
             l92.nr01 as tbl_nr01, 
             if(l04.p405 in (2, 3), 1, 2) as p00,
             1 as p01,
             l92.p03 as p03, 
       
             /**στοιχεία παραστατικού*/
             '' as series, 
             l92.p05 as aa, 
             l92.p01 as issuedate, 
             ifnull(mydt50.p00, '') as invtype, 
             
             /*αντισυμβαλλόμενος*/
             ifnull(l90.p91, '') as a_vat, 
             ifnull(l90.p92, '') as a_name, 
             ifnull(l90.p94, '') as a_address, 
             ifnull(l90.p95, '') as a_city, 
             ifnull(l90.p96, '') as a_zip 
      FROM q31
      LEFT JOIN l04 ON q31.p03=l04.p00
      LEFT JOIN {p_mydt10arr[1]} as l92 ON l92.p02=l04.p00
                                        && l92.p01>=cast('{odbcdtos(nt_p01)}' as date)
                                        && l92.p01<=cast('{odbcdtos(nt_p02)}' as date)
                                        && l92.remdate>=cast('{odbcdtos(nt_p07d)}' as date)
                                        && l92.remdate<=cast('{odbcdtos(nt_p08d)}' as date)
                                        && concat(l92.remdate, l92.remtime)>=concat(cast('{odbcdtos(nt_p07d)}' as date), '{nt_p07t}')
                                        && concat(l92.remdate, l92.remtime)<=concat(cast('{odbcdtos(nt_p08d)}' as date), '{nt_p08t}')
                                        && l92.p03>={nt_p03}
                                        && l92.p03<={nt_p04}
      LEFT JOIN {p_mydt10arr[2]} as l93 ON l93.p00=l92.nr01
      LEFT JOIN l90 ON l90.p001=l93.p01
                    && l90.p910=1                                  
                    && l90.p911=2
      LEFT JOIN mydt50 ON mydt50.p00=q31.p05
      WHERE q31.p01=620012
         && l92.nr01 is not null
         && l04.nr01 is not null
         && {p_mydt10where} 
         && {nt_napost}=1
      GROUP BY l92.nr01
   
      UNION ALL

      /***********************************************
             Παραστατικά προς παραλαβή
             q30.nr01=620013
      ***********************************************/
      SELECT l04.p405 as mkfs,   
             '{p_mydt10arr[1]}' as tbl, 
             l92.nr01 as tbl_nr01, 
             if(l04.p405 in (2, 3), 1, 2) as p00,
             2 as p01,
             l92.p03 as p03, 
       
             /**στοιχεία παραστατικού*/
             '' as series, 
             l92.p05 as aa, 
             l92.p01 as issuedate, 
             ifnull(mydt50.p00, '') as invtype, 
       
             /*αντισυμβαλλόμενος*/
             ifnull(l90.p91, '') as a_vat, 
             ifnull(l90.p92, '') as a_name, 
             ifnull(l90.p94, '') as a_address, 
             ifnull(l90.p95, '') as a_city, 
             ifnull(l90.p96, '') as a_zip
      FROM q31
      LEFT JOIN l04 ON q31.p03=l04.p00
      LEFT JOIN {p_mydt10arr[1]} as l92 ON l92.p02=l04.p00
                                        && l92.p01>=cast('{odbcdtos(nt_p01)}' as date)
                                        && l92.p01<=cast('{odbcdtos(nt_p02)}' as date)
                                        && l92.p03>={nt_p03}
                                        && l92.p03<={nt_p04}
                                        && l92.remdate>=cast('{odbcdtos(nt_p07d)}' as date)
                                        && l92.remdate<=cast('{odbcdtos(nt_p08d)}' as date)
                                        && concat(l92.remdate, l92.remtime)>=concat(cast('{odbcdtos(nt_p07d)}' as date), '{nt_p07t}')
                                        && concat(l92.remdate, l92.remtime)<=concat(cast('{odbcdtos(nt_p08d)}' as date), '{nt_p08t}')
      LEFT JOIN {p_mydt10arr[2]} as l93 ON l93.p00=l92.nr01
      LEFT JOIN l90 ON l90.p001=l93.p01
                    && l90.p910=1                                  
                    && l90.p911=2
      LEFT JOIN mydt50 ON mydt50.p00=q31.p05
      WHERE q31.p01=620013
         && l92.nr01 is not null
         && l04.nr01 is not null
         && {p_mydt10where} 
         && {nt_nparal}=1
      GROUP BY l92.nr01
) as tbl01;   
         
       