DROP FUNCTION insert_tetel_egysegar();

CREATE OR REPLACE FUNCTION insert_tetel_egysegar()
  RETURNS void AS
'INSERT INTO tetel_egysegar
   (Tetel_kod, Egysegar_kod, afa_kulcs)
Select tetel.kod, egysegarak.kod, tetel.afa_kulcs
  FROM tetel CROSS JOIN egysegarak
  left outer
  join tetel_egysegar
  on (tetel.kod = tetel_egysegar.Tetel_kod) and
     (egysegarak.kod = tetel_egysegar.Egysegar_kod)
  where tetel_egysegar.Tetel_kod is null'
  LANGUAGE 'sql' VOLATILE;

ALTER TABLE tetel_kepek ADD COLUMN tipus integer;
ALTER TABLE tetel_kepek ALTER COLUMN tipus SET STORAGE PLAIN;
ALTER TABLE tetel_kepek ALTER COLUMN tipus SET DEFAULT 0;

ALTER TABLE _setup_main ADD COLUMN tetel_kep_max_meret integer;
ALTER TABLE _setup_main ALTER COLUMN tetel_kep_max_meret SET STORAGE PLAIN;
ALTER TABLE _setup_main ALTER COLUMN tetel_kep_max_meret SET DEFAULT 0;

update _setup_main set tetel_kep_max_meret = 50 where tetel_kep_max_meret is null;