/**beg_i02**/
INSERT INTO i02
(nr01, p00, p01, p02, p03, p04, p05, p08, tax, 
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i02', 1) as nr01, 
       '{nt_tpno}' as p00, 
       p01 as p01, 
       p02 as p02, 
       p03 as p03, 
       curdate() as p04, 
       curtime() as p05, 
       p08 as p08, 
       tax as tax, 

       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i02   
WHERE p00='{nt_fpno}';
/**end_i02**/
/*peg_end*/


/**beg_i04**/
INSERT INTO i04
(nr01, pe01, pe02, pe03, pe04, pe05, pe06, pe07, pe08, pe09, pe010, 
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i04', 1) as nr01, 
       '{nt_tpno}' as pe01, 
       i04.pe02, i04.pe03, i04.pe04, i04.pe05, i04.pe06, i04.pe07, i04.pe08, i04.pe09, i04.pe010, 
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i04   
LEFT JOIN i04 as i04_01 on i04_01.pe01='{nt_tpno}' 
                        && i04_01.pe02=i04.pe02
                        && i04_01.pe03=i04.pe03
                        && i04_01.pe04=i04.pe04
                        && i04_01.pe05=i04.pe05
                        && i04_01.pe06=i04.pe06
                        && i04_01.pe07=i04.pe07
                        && i04_01.pe08=i04.pe08
                        && i04_01.pe09=i04.pe09
WHERE i04.pe01='{nt_fpno}'
   && i04_01.nr01 is null;
/**end_i04**/
/*peg_end*/

/**beg_i05**/
INSERT INTO i05
(nr01, pe01, pe02, pe03, pe04, pe05, pe06, pe07, pe08, p910, p911, 
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i05', 1) as nr01, 
       '{nt_tpno}' as pe01, 
       i05.pe02, i05.pe03, i05.pe04, i05.pe05, i05.pe06, i05.pe07, i05.pe08, i05.p910, i05.p911, 
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i05   
LEFT JOIN i05 as i05_01 on i05_01.pe01='{nt_tpno}' 
                        && i05_01.pe02=i05.pe02
                        && i05_01.pe03=i05.pe03
                        && i05_01.p910=i05.p910
                        && i05_01.p911=i05.p911
WHERE i05.pe01='{nt_fpno}'
   && i05.master=0
   && i05_01.nr01 is null;
/**end_i05**/
/*peg_end*/

/**beg_i06**/
INSERT INTO i06
(nr01, pe101, pe00, pe102, pe103, p300, ep300, p104, p105, p106, p107, pekei1, 
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i06', 1) as nr01, 
       '{nt_tpno}' as pe101, 
       pe00, pe102, pe103, p300, ep300, p104, p105, p106, p107, pekei1, 
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i06   
WHERE pe101='{nt_fpno}';
/**end_i06**/
/*peg_end*/


/**beg_i08**/
INSERT INTO i08
(nr01, pe01, pe02, pe020, pe03, pe04, pc01, pm01, i49p01, pe06, petim1, petim4, petim3, petim2,
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i08', 1) as nr01, 
       '{nt_tpno}' as pe01, 

       i08.pe02, i08.pe020, i08.pe03, i08.pe04, i08.pc01, i08.pm01, i08.i49p01, i08.pe06, i08.petim1, i08.petim4, i08.petim3, i08.petim2, 
       
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i08   
LEFT JOIN i08 as i08_01 on i08_01.pe01='{nt_tpno}' 
                        && i08_01.pe02=i08.pe02
                        && i08_01.pc01=i08.pc01
                        && i08_01.pm01=i08.pm01
WHERE i08.pe01='{nt_fpno}'
   && i08_01.nr01 is null;
/**end_i08**/
/*peg_end*/


/**beg1_i17**/
INSERT INTO i17
(nr01, pe01, pe02, pe03, pe04,
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i17', 1) as nr01, 
       '{nt_tpno}' as pe01, 
       i17.pe02, i17.pe03, i17.pe04,
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i17   
LEFT JOIN i17 as i17_01 ON i17_01.pe01='{nt_tpno}'
                        && i17.pe02=i17_01.pe02
WHERE i17.pe01='{nt_fpno}'
   && i17_01.nr01 is null;
/**end1_i17**/
/*peg_end*/
   
/**beg2_i17**/
UPDATE i17, i17 as i17_01
set i17_01.pe03=i17.pe03,
    i17_01.pe04=i17.pe04,
    i17_01.remdate=curdate(), 
    i17_01.remtime=curtime(), 
    i17_01.remuser='{user_pswd}' 
where i17.pe01='{nt_fpno}'
   && i17_01.pe01='{nt_tpno}'
   && i17.pe02=i17_01.pe02;
/**end2_i17**/
/*peg_end*/



/**beg1_i18**/
INSERT INTO i18
(nr01, p00, p02, p010, p011, pc01, pm01,
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i18', 1) as nr01, 
       '{nt_tpno}' as p00, 
       i18.p02, i18.p010, i18.p011, i18.pc01, i18.pm01,
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i18   
LEFT JOIN i18 as i18_01 ON i18_01.p00='{nt_tpno}'
                        && i18_01.pc01=''
                        && i18_01.pm01=''
WHERE i18.p00='{nt_fpno}'
   && i18.pc01=''
   && i18.pm01=''
   && i18_01.nr01 is null;
/**end1_i18**/
/*peg_end*/
   
/**beg2_i18**/
UPDATE i18, i18 as i18_01
set i18_01.p02=i18.p02,
    i18_01.p010=i18.p010,
    i18_01.p011=i18.p011,
    i18_01.remdate=curdate(), 
    i18_01.remtime=curtime(), 
    i18_01.remuser='{user_pswd}' 
where i18.p00='{nt_fpno}'
   && i18.pc01=''
   && i18.pm01=''
   && i18_01.p00='{nt_tpno}'
   && i18_01.pc01=''
   && i18_01.pm01='';
/**end2_i18**/
/*peg_end*/


/**beg_i19**/
INSERT INTO i19
(nr01, p00, pdia, pc01, pm01, p02, p03, pc02, pm02,
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i19', 1) as nr01, 
       '{nt_tpno}' as p00, 
       pdia, pc01, pm01, p02, p03, pc02, pm02,
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i19   
WHERE p00='{nt_fpno}'
   && pc01=''
   && pm01='';
/**end_i19**/
/*peg_end*/

/**beg_i39**/
INSERT INTO i39
(nr01, p01, p02, p03, p04, p05, p06, p07, p08, 
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i39', 1) as nr01, 
       '{nt_tpno}' as p01, 
       i39.p02, i39.p03, i39.p04, i39.p05, i39.p06, i39.p07, i39.p08, 
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i39   
LEFT JOIN i39 as i39_01 on i39_01.p01='{nt_tpno}' 
                        && i39_01.p02=i39.p02
WHERE i39.p01='{nt_fpno}'
   && i39_01.nr01 is null;
/**end_i39**/
/*peg_end*/


/**beg_i47**/
INSERT INTO i47
(nr01, p01, p02, p03, px01, pm01, p06, p07, p08, 
remdate, remtime, remuser, remfdate, remftime, remfuser)
SELECT peg_new_nr01('i47', 1) as nr01, 
       '{nt_tpno}' as p01, 
       i47.p02, i47.p03, i47.px01, i47.pm01, i47.p06, i47.p07, i47.p08, 
       curdate()     as remdate, 
       curtime()     as remtime, 
       '{user_pswd}' as remuser, 
       curdate()     as remfdate, 
       curtime()     as remftime, 
       '{user_pswd}' as remfuser
FROM i47  
LEFT JOIN i47 as i47_01 ON i47_01.p01='{nt_tpno}'
                        && i47_01.p02=i47.p02 
                        && i47_01.p03=i47.p03 
WHERE i47.p01='{nt_fpno}'
   && i47_01.nr01 is null;
/**end_i47**/
/*peg_end*/
