drop table if exists {p_t00tbl1};
/*peg_end*/

CREATE TABLE {p_t00tbl1}
select nr01 as nr01, 
       p01 as p01, 
       p02 as p02, 
       p610 as p610, 
       p03 as p03
from i01
where nr01=-1;
/*peg_end*/

delete from {p_t00tbl1};
/*peg_end*/

insert into {p_t00tbl1}
(nr01, p01, p02, p610, p03)
SELECT peg_new_nr01('i01', 1) as nr01, 
       p01 as p01, 
       p02 as p02, 
       p610 as p610, 
       sum(p03) as p03
FROM
(
          SELECT i01.p01   as p01,   
                 ifnull(i00.name, '')  as p02,  
                 i01.p610  as p610,  
                 i48.p02 as i48p02, 
                 i48.p03 as i48p03, 
                 sum( ifnull(i01.p012*(i15.p03-i15.p04)*i48.p04, 0) )   as p03
          FROM z_i48{p_t00ext1} as i48, z_i01{p_t00ext1} as i01, i00, i15
          WHERE i01.p01=i00.pno
             && i48.p01=i01.nr01
             && i00.p9904<>0  
             && i00.p9905<>0  
             && i15.p01=i01.p05
          GROUP BY i01.p01, i48.p02, i48.p03, i01.p610
) as tbl1
where p03<>0
GROUP BY p01, p610;
/*peg_end*/

ALTER TABLE {p_t00tbl1} ADD KEY p01 (p01, p610);
/*peg_end*/

INSERT INTO i01
(nr01, p01, p02, p03, p04, p011, p022, p013, p013t, p012, p610, p0601, p05, p07, 
remfdate, remftime, remfuser, remdate, remtime, remuser) 
SELECT nr01 as nr01, 
       p01 as p01, 
       p02 as p02, 
       round(p03, {peg_dekpos}) as p03, 
       0 as p04, 
       0 as p011, 
       0 as p022,        
       '{odbcdtos(p_t00date)}' as p013, 
       '00:00' as p013t, 
       1 as p012, 
       p610 as p610, 
       substr('{p_t00ait}', 1, 100) as p0601, 
       {nt_p301} as p05, 
       -1 as p07, 

       curdate() as remfdate, 
       curtime() as remftime, 
       '{p_t00remf}' as remfuser, 
       curdate() as remdate, 
       curtime() as remtime, 
       '{p_t00remf}' as remuser
FROM {p_t00tbl1};
/*peg_end*/

INSERT INTO i48
(nr01, p01, p02, p03, p04, 
remfdate, remftime, remfuser, remdate, remtime, remuser) 
SELECT peg_new_nr01('i48', 1) as nr01, 
       i01.nr01 as p01, 
       tbl01.i48p02 as p02, 
       tbl01.i48p03 as p03, 
       tbl01.p03 as p04, 
       curdate() as remfdate, 
       curtime() as remftime, 
       '{p_t00remf}' as remfuser, 
       curdate() as remdate, 
       curtime() as remtime, 
       '{p_t00remf}' as remuser
FROM       
(
          SELECT 
                 i01.p01   as p01,   
                 ifnull(i00.name, '')  as p02,  
                 i01.p610  as p610,  
                 i48.p02 as i48p02, 
                 i48.p03 as i48p03, 
                 sum( ifnull(i01.p012*(i15.p03-i15.p04)*i48.p04, 0) )   as p03
          FROM z_i48{p_t00ext1} as i48, z_i01{p_t00ext1} as i01, i00, i15
          WHERE i01.p01=i00.pno
             && i48.p01=i01.nr01
             && i00.p9904<>0  
             && i00.p9905<>0  
             && i15.p01=i01.p05
          GROUP BY i01.p01, i48.p02, i48.p03, i01.p610
) as tbl01, {p_t00tbl1} as i01
WHERE i01.p01=tbl01.p01
   && i01.p610=tbl01.p610
   && tbl01.p03<>0;
/*peg_end*/

drop table if exists {p_t00tbl1};
/*peg_end*/
 