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, 
       p012 as p012 
from i01
where nr01=-1;
/*peg_end*/

delete from {p_t00tbl1};
/*peg_end*/

insert into {p_t00tbl1}
(nr01, p01, p02, p610, p03, p012)
SELECT peg_new_nr01('i01', 1) as nr01, 
       p01 as p01, 
       p02 as p02, 
       p610 as p610, 
       abs(sum(p03)) as p03, 
       if(p03>0, 1, -1) as p012
FROM
(
          SELECT i01.p01   as p01,   
                 ifnull(i00.name, '')  as p02,  
                 i01.p610  as p610,  
                 i32.p02 as i32p02, 
                 sum( ifnull(i01.p012*(i15.p03-i15.p04), 0) )   as p03
          FROM z_i32{p_t00ext1} as i32, z_i01{p_t00ext1} as i01, i00, i15
          WHERE i01.p01=i00.pno
             && i32.p00=i01.nr01
             && i32.p010=1
             && i00.ps01<>0               
             && i15.p01=i01.p05
          GROUP BY i01.p01, i32.p02, i01.p610
) as tbl1
where p03<>0
GROUP BY p01, p610, if(p03>0, 1, -1);
/*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, 
       p012 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 i32
(nr01, p01, p02, p00, p010,
remfdate, remftime, remfuser, remdate, remtime, remuser) 
SELECT peg_new_nr01('i32', 1) as nr01, 
       tbl01.p01 as p01, 
       tbl01.i32p02 as p02, 
       i01.nr01 as p00, 
       1 as p010, 
       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,  
                 i32.p02 as i32p02, 
                 sum( ifnull(i01.p012*(i15.p03-i15.p04), 0) )   as p03
          FROM z_i32{p_t00ext1} as i32, z_i01{p_t00ext1} as i01, i00, i15
          WHERE i01.p01=i00.pno
             && i32.p00=i01.nr01
             && i32.p010=1
             && i00.ps01<>0               
             && i15.p01=i01.p05
          GROUP BY i01.p01, i32.p02, i01.p610
) as tbl01, {p_t00tbl1} as i01
WHERE i01.p01=tbl01.p01
   && i01.p610=tbl01.p610
   && tbl01.p03<>0
   && if(tbl01.p03>0, 1, -1)=i01.p012;
/*peg_end*/

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