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

create table {p_i07tbl1}
{peg_str};
/*peg_end*/

/***********************
    update old records
***********************/
UPDATE i07, {p_i07tbl1} as i07_in
set i07.p05=i07_in.t_poseis-i07_in.t_posex, 
    i07.p03={if(np_pos, 'i07_in.t_poseis-i07_in.t_posex', 'i07.p03')}, 
    i07.remdate=curdate(), 
    i07.remtime=curtime(), 
    i07.remuser='{user_pswd}'
WHERE i07.nr01=i07_in.inr01;
/*peg_end*/

/***********************
    insert new records
***********************/
INSERT INTO i07
(nr01, p05, p03, p01, p02, vp02, pc01, vpc01, pm01, vpm01, p010, p011, 
remfdate, remftime, remfuser, remdate, remtime, remuser)
SELECT peg_new_nr01('i07', 1) as nr01, 
       i07_in.t_poseis-i07_in.t_posex as p05, 
       {if(np_pos, 'i07_in.t_poseis-i07_in.t_posex', '0')} as p03, 
       i07_in.p610 as p01, 
       i07_in.p01 as p02, 
       i07_in.name as vp02, 
       i07_in.pc01 as pc01, 
       i07_in.vpc01 as vpc01, 
       i07_in.pm01 as pm01, 
       i07_in.vpm01 as vpm01, 
       i07_in.p010 as p010, 
       i07_in.p011 as p011, 
       
       curdate() as remfdate, 
       curtime() as remftime, 
       '{user_pswd}' as remfuser, 
       curdate() as remdate, 
       curtime() as remtime, 
       '{user_pswd}' as remuser 
FROM {p_i07tbl1} as i07_in
WHERE i07_in.inr01=0;
/*peg_end*/
    

update i07
set i07.p06=i07.p03+i07.p03_1-i07.p05,
    i07.remdate=curdate(), 
    i07.remtime=curtime(), 
    i07.remuser='{user_pswd}'
where i07.p06<>i07.p03+i07.p03_1-i07.p05;
/*peg_end*/

