
ALTER TABLE dat_m_nyukin ADD COLUMN IF NOT EXISTS nyukinkb character varying(2);
ALTER TABLE dat_rm_nyukin ADD COLUMN IF NOT EXISTS nyukinkb character varying(2);
ALTER TABLE dat_m_shukkin ADD COLUMN IF NOT EXISTS shukkinkb character varying(2);
ALTER TABLE dat_rm_shukkin ADD COLUMN IF NOT EXISTS shukkinkb character varying(2);
ALTER TABLE mst_denpyoshiwake ADD COLUMN IF NOT EXISTS nyushukkinkb character varying(2) DEFAULT ' ' NOT NULL;
ALTER TABLE mst_denpyoshiwake DROP CONSTRAINT mst_denpyoshiwake_pkey;
ALTER TABLE mst_denpyoshiwake ADD CONSTRAINT mst_denpyoshiwake_pkey PRIMARY KEY(denpyoshuruicd,torihikisakibunruicd,hinmokubunruicd,kazeikb,kozacd,kessaikb,nyushukkinkb);

insert into mst_kbn(kbtype,kbcd,kbmei,hyojijun,hyojimei,biko,haishifg,sakuseiuserid,sakuseinichiji,koshinuserid,koshinnichiji) select 'SHUKKAJOTAI','7','定期売上',7,'定期売上','定期売上',0,'admin',clock_timestamp(),'admin',clock_timestamp()  where not exists (select 0 from mst_kbn where kbtype = 'SHUKKAJOTAI' and kbcd = '7');
insert into mst_kbn(kbtype,kbcd,kbmei,hyojijun,hyojimei,biko,haishifg,sakuseiuserid,sakuseinichiji,koshinuserid,koshinnichiji) select 'KAIKEIBUNRUIKB','3','定期売上',3,'定期売上','定期売上',0,'admin',clock_timestamp(),'admin',clock_timestamp() where not exists (select 0 from mst_kbn where kbtype = 'KAIKEIBUNRUIKB' and kbcd = '3');
insert into mst_kbn(kbtype,kbcd,kbmei,hyojijun,hyojimei,biko,haishifg,sakuseiuserid,sakuseinichiji,koshinuserid,koshinnichiji) values 
    ('NYUSHUKKINKB','1','後金',1,'後金','通常の入出金',0,'admin',clock_timestamp(),'admin',clock_timestamp())
  , ('NYUSHUKKINKB','2','前金',2,'前金','前払（前受）',0,'admin',clock_timestamp(),'admin',clock_timestamp())
  , ('DENPYOSHURUIKB','SHIHARAI','支払',18,'支払','支払',0,'admin',clock_timestamp(),'admin',clock_timestamp());


update mst_denpyoshiwake set nyushukkinkb = '1' where denpyoshuruicd in ('NYUKIN', 'SHUKKIN');

insert into mst_denpyoshiwake(denpyoshuruicd,torihikisakibunruicd,hinmokubunruicd,kazeikb,kozacd,kessaikb,nyushukkinkb,karikatakamokucd,kashikatakamokucd,haishifg,sakuseiuserid,sakuseinichiji,koshinuserid,koshinnichiji) values 
    ('NYUKIN','1',' ',' ',' ','1','2','10001','20004',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('NYUKIN','1',' ',' ','1','2','2','10004','20004',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('SHUKKIN','1',' ',' ',' ','1','2','10018','10001',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('SHUKKIN','1',' ',' ','1','2','2','10018','10004',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('SEIKYU','1',' ','1',' ',' ',' ','10007','20016',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('SEIKYU','1',' ','2',' ',' ',' ','50002','20016',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('SHIHARAI','1',' ','1',' ',' ',' ','10024','20002',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('SHIHARAI','1',' ','2',' ',' ',' ','10024','40001',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('SHIIRE','1','3','1',' ',' ',' ','40001','20002',0,'admin',clock_timestamp(),'admin',clock_timestamp()),
    ('SHIIRE','1','3','2',' ',' ',' ','40001','10018',0,'admin',clock_timestamp(),'admin',clock_timestamp());

