
insert into keys ( label ) values ( 'futureStocksEnabled');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'futureStocksEnabled'), '0','0', 0);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'futureStocksEnabled') ;


alter table documente add column comanda_type int default 1;

alter table fisa_magazie add column comanda_type int default 1;

insert into keys ( label ) values ( 'xComboWaitForEnter');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'xComboWaitForEnter'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'xComboWaitForEnter') ;

 drop table blocked_stocks;

alter table stocks add column eta int;


update configs set value = '2016002' where id_key = ( select min(id) from keys where label = 'currentVersion' );



ALTER TABLE files ALTER COLUMN filename TYPE text;

ALTER TABLE files ALTER COLUMN title TYPE text;

alter table facturi_emise_items alter column produs_factura type text;

update configs set value = '2016003' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'futureStocksEnabledFacturiEmise');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'futureStocksEnabledFacturiEmise'), '0','0', 0);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'futureStocksEnabledFacturiEmise') ;



update configs set value = '2016004' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'printEticheteAtNir');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'printEticheteAtNir'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'printEticheteAtNir') ;

insert into keys ( label ) values ( 'frontofficeTransferConsumMagazieDepozit');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'frontofficeTransferConsumMagazieDepozit'), 'Depozit','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'frontofficeTransferConsumMagazieDepozit') ;

update configs set value = '2016005' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table stock_transfer add column nr_bon int not null default 1;

INSERT INTO registre_nomenclator (nume_registru,uniquekey) VALUES ('Bon Consum Transfer intern','bon_consum_transfer_intern');

insert into registre_numere (id_registru,numar,dateadd) values (
    (select id from registre_nomenclator where uniquekey = 'bon_consum_transfer_intern'),
    (select coalesce(max(nr_bon_consum),0) from bonuri_consum) , current_timestamp);



update configs set value = '2016006' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table products alter column label type text;

alter table products alter column short_label type text;

alter table products alter column sectie_label type text;

insert into keys ( label ) values ( 'showReducereValoricaButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showReducereValoricaButton'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'showReducereValoricaButton') ;

alter table orders add column reducere_valorica integer default -1 ;

alter table orders add column cod_fiscal text default null ;


insert into keys(label) values ('promo_x_plus_unu') ;
insert into messages(id_key, id_lang, label) values ((select min(id) from keys where label='promo_x_plus_unu'), 1, 'X+1 GRATIS') ;

update configs set value = '2016007' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016008' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'facturareSeriiChitante');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'facturareSeriiChitante'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'facturareSeriiChitante') ;


update configs set value = '2016009' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016010' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016011' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016012' where id_key = ( select min(id) from keys where label = 'currentVersion' );


update configs set value = '2016013' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016014' where id_key = ( select min(id) from keys where label = 'currentVersion' );



update configs set value = '2016015' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016016' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016017' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'showTransferIntreMagaziiButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showTransferIntreMagaziiButton'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'showTransferIntreMagaziiButton') ;

insert into keys ( label ) values ( 'showOptiuniButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showOptiuniButton'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'showOptiuniButton') ;

insert into keys ( label ) values ( 'showMeseButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showMeseButton'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'showMeseButton') ;

insert into keys ( label ) values ( 'showLogoutButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showLogoutButton'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'showLogoutButton') ;

insert into keys ( label ) values ( 'showNotaButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showNotaButton'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'showNotaButton') ;

update configs set value = '2016018' where id_key = ( select min(id) from keys where label = 'currentVersion' );



create table valute ( id serial unique, denumire text, principala int not null, paritate float);

insert into valute (denumire, principala, paritate) values ('RON', 1, 1);

alter table subcategories add column id_valuta int constraint FK_subcategories__valuta references valute(id);

alter table stocks add column id_valuta int constraint FK_stocks__valuta references valute(id) default null;

update subcategories set id_valuta = (select id from valute where denumire = 'RON');




update configs set value = '2016019' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'addTvaLaOfertaSauFactura');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'addTvaLaOfertaSauFactura'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'addTvaLaOfertaSauFactura') ;

update configs set value = '2016020' where id_key = ( select min(id) from keys where label = 'currentVersion' );
alter table sales add column ignore_promo int default 0;



update configs set value = '2016021' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016022' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'facturareProdusGeneric');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'facturareProdusGeneric'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'facturareProdusGeneric') ;

update configs set value = '2016023' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016024' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016025' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016025' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016025' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016025' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016026' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016027' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016028' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016029' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016030' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016031' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'numbersUseThousandsSeparator');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'numbersUseThousandsSeparator'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'numbersUseThousandsSeparator') ;


update configs set value = '2016032' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016033' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016034' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016035' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016036' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016037' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016038' where id_key = ( select min(id) from keys where label = 'currentVersion' );


create index stocks__status on stocks(status);

create index stocks__label on stocks(label);

alter table facturi_emise_items alter column produs_factura type text;


insert into keys ( label ) values ( 'ordersReportingLimit');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'ordersReportingLimit'), '48000','48000', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'ordersReportingLimit') ;

update configs set value = '2016039' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016040' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016041' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'returModifyMode');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'returModifyMode'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'returModifyMode') ;

insert into keys ( label ) values ( 'selector2Level1MinHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level1MinHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level1MinHeight') ;

insert into keys ( label ) values ( 'selector2Level2MinHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level2MinHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level2MinHeight') ;

insert into keys ( label ) values ( 'selector2Level3MinHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level3MinHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level3MinHeight') ;


update configs set value = '2016042' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016043' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016044' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016045' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016046' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016047' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016048' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016048' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016048' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016049' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016050' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'facturareRoundDecimals');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'facturareRoundDecimals'), '2','2', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'facturareRoundDecimals') ;

insert into keys ( label ) values ( 'forceShowAllDecimals');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'forceShowAllDecimals'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'forceShowAllDecimals') ;

update configs set value = '2016051' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016052' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016053' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016054' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016055' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016056' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016057' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016058' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table bon_entries alter column "text" type text;

update configs set value = '2016059' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016060' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'raportInchiderePreambul');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'raportInchiderePreambul'), '', '', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'raportInchiderePreambul') ;

update configs set value = '2016061' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016062' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016063' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'qtyCheckingsEnabled');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'qtyCheckingsEnabled'), '0', '0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'qtyCheckingsEnabled') ;


update configs set value = '2016064' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'preorderProductLineHtml');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'preorderProductLineHtml'), '', '', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'preorderProductLineHtml') ;

 insert into keys ( label ) values ( 'orderProductLineHtml');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'orderProductLineHtml'), '', '', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'orderProductLineHtml') ;


insert into keys ( label ) values ( 'orderQtyHtml');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'orderQtyHtml'), '', '', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'orderQtyHtml') ;

  insert into keys ( label ) values ( 'preorderQtyHtml');
   insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'preorderQtyHtml'), '', '', 1);
   insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'preorderQtyHtml') ;

  insert into keys ( label ) values ( 'orderValueHtml');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'orderValueHtml'), '', '', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'orderValueHtml') ;


  insert into keys ( label ) values ( 'preorderValueHtml');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'preorderValueHtml'), '', '', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'preorderValueHtml') ;

update configs set value = '2016065' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016066' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016067' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'maxDiscountValuePerDay');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'maxDiscountValuePerDay'), '', '', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'maxDiscountValuePerDay') ;

insert into keys ( label ) values ( 'maxDiscountOrdersNoPerDay');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'maxDiscountOrdersNoPerDay'), '', '', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'maxDiscountOrdersNoPerDay') ;


insert into keys ( label ) values ( 'bonFiscalIgnoreProduseCuPretZero');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonFiscalIgnoreProduseCuPretZero'), '1', '', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'bonFiscalIgnoreProduseCuPretZero') ;

update configs set value = '2016068' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'quickUserSwitchEnabled');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'quickUserSwitchEnabled'), '0', '0', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'quickUserSwitchEnabled') ;

update configs set value = '2016069' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016070' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016071' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016072' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'touch6DontShowClientDialog');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touch6DontShowClientDialog'), '1', '0', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'touch6DontShowClientDialog') ;


update configs set value = '2016073' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016074' where id_key = ( select min(id) from keys where label = 'currentVersion' );

drop table if exists cereri_aprovizionare;

create table cereri_aprovizionare (
    id serial unique,
    id_stock int not null constraint FK_CERERI_APROVIZIONARE__STOCKS references stocks(id),
    id_operator int not null constraint FK_CERERI_APROVIZIONARE__USERS_OPERATOR references users(id),
    id_supervizor int constraint FK_CERERI_APROVIZIONARE__USERS_SUPERVIZOR references users(id),
    id_magazie int not null constraint FK_CERERI_APROVIZIONARE__MAGAZII references magazii(id),
    id_furnizor int constraint FK_CERERI_APROVIZIONARE__CLIENTI references clienti(id),
    status int,
    cantitate_ceruta double precision not null,
    pret_negociat double precision,
    nr_zile_comanda double precision,
    cantitate_recomandata_minim double precision,
    cantitate_recomandata_maxim double precision,
    cantitate_iesita_referinta double precision,
    cantitate_intrata_medie_per_comanda_referinta double precision,
    cantitate_existenta_magazie double precision not null,
    cantitate_existenta_totala double precision not null,
    cantitate_deja_comandata double precision,
    referinta_start timestamp not null,
    referinta_stop timestamp not null,
    data_cerere timestamp not null);

update configs set value = '2016075' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016076' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016077' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016078' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016079' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'group_manager_aprovizionare');
insert into groups ( id_key ) values ((select min(id) from keys where keys.label = 'group_manager_aprovizionare'));
insert into messages ( id_lang, id_key, label ) values ( 1, (select min(id) from keys where keys.label = 'group_manager_aprovizionare'), 'Manager aprovizionare');

create table preturi_furnizori (
    id serial unique,
    id_stock int not null constraint FK_PRETURI_FURNIZORI__STOCKS references stocks(id),
    id_furnizor int not null constraint FK_PRETURI_FURNIZORI__CLIENTI references clienti(id),
    pret_negociat double precision not null);

update configs set value = '2016080' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016081' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016082' where id_key = ( select min(id) from keys where label = 'currentVersion' );


drop table if exists cereri_aprovizionare;

create table cereri_aprovizionare (
    id serial unique,
    id_stock int not null constraint FK_CERERI_APROVIZIONARE__STOCKS references stocks(id),
    id_operator int not null constraint FK_CERERI_APROVIZIONARE__USERS_OPERATOR references users(id),
    id_supervizor int constraint FK_CERERI_APROVIZIONARE__USERS_SUPERVIZOR references users(id),
    id_magazie int constraint FK_CERERI_APROVIZIONARE__MAGAZII references magazii(id),
    id_furnizor int constraint FK_CERERI_APROVIZIONARE__CLIENTI references clienti(id),
    status int,
    cantitate_ceruta double precision not null,
    pret_negociat double precision,
    nr_zile_comanda double precision,
    cantitate_recomandata_minim double precision,
    cantitate_recomandata_maxim double precision,
    cantitate_iesita_referinta double precision,
    cantitate_intrata_medie_per_comanda_referinta double precision,
    cantitate_existenta_magazie double precision not null,
    cantitate_existenta_total double precision not null,
    cantitate_deja_comandata double precision,
    referinta_start timestamp not null,
    referinta_stop timestamp not null,
    data_cerere timestamp not null);

update configs set value = '2016083' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016084' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016085' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table stocks alter column id_tip_articol set default 1;

update stocks set id_tip_articol = 1 where id_tip_articol is null;

update configs set value = '2016086' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016087' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016088' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016089' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'frontOfficeRoundDecimals');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'frontOfficeRoundDecimals'), '2','2', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'frontOfficeRoundDecimals') ;

update configs set value = '2016090' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016091' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016092' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016092' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016092' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016092' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016093' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016094' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table transfer_items add column img_src text;

create table sales_pictures (
id serial unique,
id_sale int not null constraint FK_SALES__ID_SALE references sales(id) on delete cascade,
picture_name text not null
);

create table tags (
id serial unique,
label text not null
);

create table stocks_tags(
id serial unique,
id_stock int not null constraint FK_STOCKS_TAGS__STOCKS references stocks(id),
id_tag int not null constraint FK_STOCKS_TAGS__TAGS references tags(id)
);



update configs set value = '2016095' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016096' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table regcasa alter column anexa type text;


insert into keys ( label ) values ( 'hotelSaleFacturataBeforeIncasata');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'hotelSaleFacturataBeforeIncasata'), '0', '0', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'hotelSaleFacturataBeforeIncasata') ;

  update configs set value = '2016097' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016098' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016099' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'contaExportMonetareTichet');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportMonetareTichet'), 'Tichet', 'Tichet', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'contaExportMonetareTichet') ;

  insert into keys ( label ) values ( 'contaExportMonetareCard');
   insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportMonetareCard'), 'Card', 'Card', 1);
   insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'contaExportMonetareCard') ;


    insert into keys ( label ) values ( 'contaExportMonetareCec');
     insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportMonetareCec'), 'Cec', 'Cec', 1);
     insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
      and id_key = (select min(id) from keys where keys.label = 'contaExportMonetareCec') ;

update configs set value = '2016100' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table facturi_emise_items add column produs_line_1 text default '';

update configs set value = '2016101' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016102' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016103' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016104' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'facturareCodProdusDiscount');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'facturareCodProdusDiscount'), '', '', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'facturareCodProdusDiscount') ;


insert into keys ( label ) values ( 'eticheteA4UnitateMasura');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'eticheteA4UnitateMasura'), '100gr', '100gr', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'eticheteA4UnitateMasura') ;

update configs set value = '2016105' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016106' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016107' where id_key = ( select min(id) from keys where label = 'currentVersion' );



insert into keys ( label ) values ( 'nirRoundDecimals');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'nirRoundDecimals'), '2', '2', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'nirRoundDecimals') ;

update configs set value = '2016108' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016109' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016110' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016111' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016112' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table stock_transfer add column id_user int default null;

update configs set value = '2016113' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'contaExportUseCoduriGenerice');
     insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportUseCoduriGenerice'), '1', '1', 1);
     insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
      and id_key = (select min(id) from keys where keys.label = 'contaExportUseCoduriGenerice') ;

insert into keys ( label ) values ( 'contaExportUseCoduriExterne');
     insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportUseCoduriExterne'), '1', '1', 1);
     insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
      and id_key = (select min(id) from keys where keys.label = 'contaExportUseCoduriExterne') ;


insert into keys ( label ) values ( 'productieSemifabricateQuantityMultiplier');
     insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieSemifabricateQuantityMultiplier'), '', '', 1);
     insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
      and id_key = (select min(id) from keys where keys.label = 'productieSemifabricateQuantityMultiplier') ;


update configs set value = '2016114' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016115' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016116' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016117' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'touch2Selector2RedrawAtTableOpen');
     insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touch2Selector2RedrawAtTableOpen'), '0', '0', 1);
     insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
      and id_key = (select min(id) from keys where keys.label = 'touch2Selector2RedrawAtTableOpen') ;

update configs set value = '2016118' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016119' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016120' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016121' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016122' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016123' where id_key = ( select min(id) from keys where label = 'currentVersion' );

create table ev_def (
    id serial unique,
    label text not null,
    tip int not null,
    activ boolean not null default true,
    tip_cont int not null constraint FK_ev_def__tip_cont_client references tip_cont_client(id),
    puncte_def int not null,
    delta_t_inainte int not null default 0,
    delta_t_dupa int not null default 0,
    notify_sms_period int not null,
    notify_email_period int not null,
    cod_sync text);


create table ev_instance(
    id serial unique,
    id_locatie int not null constraint FK_ev_locatii_mapping__locations references locations(id),
    id_ev_def int not null constraint FK_ev_instance__ev_def references ev_def (id),
    data_start timestamp not null,
    data_stop timestamp not null,
    cod_sync text);


create table ev_clienti (
    id serial unique,
    id_ev_def int not null constraint FK_ev_clienti__ev_def references ev_def (id),
    id_client int not null constraint FK_ev_clienti__clienti references clienti (id),
    id_locatie int not null constraint FK_ev_clienti__locations references locations(id),
    puncte_client int not null ,
    data_puncte timestamp not null,
    nr_card text not null,
    tip_card int not null constraint FK_ev_clienti__tip_cont_client references tip_cont_client(id),
    cod_sync text);

alter table tip_cont_client add column cod_sync text ;

update tip_cont_client set cod_sync = 'SRV_' || id;



insert into keys ( label ) values ( 'productieSemifabricateMagazieProductie');
     insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieSemifabricateMagazieProductie'), '', '', 1);
     insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
      and id_key = (select min(id) from keys where keys.label = 'productieSemifabricateMagazieProductie') ;


insert into keys ( label ) values ( 'productieSemifabricateMagazieBonConsum');
     insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieSemifabricateMagazieBonConsum'), '', '', 1);
     insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
      and id_key = (select min(id) from keys where keys.label = 'productieSemifabricateMagazieBonConsum') ;

update configs set value = '2016124' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016125' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016126' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016127' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016128' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016129' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016130' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016131' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016132' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016133' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016134' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016135' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016136' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table mapping_printer_nota drop constraint fk_mapping_prin_nota__printers;

alter table mapping_printer_nota add constraint fk_mapping_printer_nota__printers foreign key (id_printer) references printers (id) on delete cascade;


alter table mapping_printers_items drop constraint fk_mapping_prin_items_printers;

alter table mapping_printers_items add constraint fk_mapping_printer_items__printers foreign key (id_printer) references printers (id) on delete cascade;


alter table printer_categories drop constraint fk_printer_categories_printers;

alter table printer_categories add constraint fk_printer_categories__printers foreign key (id_printer) references printers (id) on delete cascade;





update configs set value = '2016137' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016138' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016139' where id_key = ( select min(id) from keys where label = 'currentVersion' );


update configs set value = '2016140' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'deleteFacturaFileAfterSave');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'deleteFacturaFileAfterSave'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'deleteFacturaFileAfterSave') ;


update configs set value = '2016141' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016142' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'touch2ShowQuickTableItemsPerLine');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touch2ShowQuickTableItemsPerLine'), '4','4', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'touch2ShowQuickTableItemsPerLine') ;

update configs set value = '2016143' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016144' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016145' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016146' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016147' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016148' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016149' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016150' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table clienti add column cod_sync text;
alter table persoane_fizice add column cod_sync text;

alter table persoane_juridice add column cod_sync text;


insert into keys ( label ) values ( 'codUnicDatabase');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'codUnicDatabase'), 'changeme_if_use_database_sync','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'codUnicDatabase') ;


update configs set value = '2016151' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table clienti rename column cod_sync to cod_sync_client;

alter table persoane_Fizice rename column cod_sync to cod_sync_pf;

alter table cont_client add column cod_sync text ;

alter table promotii_clienti add column cod_sync text ;

update configs set value = '2016152' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016153' where id_key = ( select min(id) from keys where label = 'currentVersion' );



create table clients_locations (
    id serial unique,
    id_client int not null constraint FK_clients_locations__clienti references clienti (id),
    id_location int  constraint FK_clients_locations__locations references locations(id),
    cod_sync text );

create index clients_locations__cod_sync on clients_locations(cod_sync);


insert into keys ( label ) values ( 'clientFilterClassname');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'clientFilterClassname'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'clientFilterClassname') ;

update configs set value = '2016154' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016155' where id_key = ( select min(id) from keys where label = 'currentVersion' );



insert into keys ( label ) values ( 'contClientLocationOnDetalii');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contClientLocationOnDetalii'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'contClientLocationOnDetalii') ;

alter table persoane_fizice add column data_numelui date default current_timestamp;

update configs set value = '2016156' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016157' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table persoane_fizice add column observatii text;

update configs set value = '2016158' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016159' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016160' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016161' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table buletine add column cod_sync text;

alter table pasapoarte add column cod_sync text;

update configs set value = '2016162' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016163' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016164' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016165' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016166' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016167' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table persoane_juridice add column must_sync boolean not null default false;

alter table persoane_fizice add column must_sync boolean not null default false;

alter table clienti add column must_sync boolean not null default false;

alter table buletine add column must_sync boolean not null default false;

alter table pasapoarte add column must_sync boolean not null default false;


insert into keys ( label ) values ( 'group_manager_clienti');
insert into groups ( id_key ) values ((select min(id) from keys where keys.label = 'group_manager_clienti'));
insert into messages ( id_lang, id_key, label ) values ( 1, (select min(id) from keys where keys.label = 'group_manager_clienti'), 'Manager clienti');


insert into keys ( label ) values ('gui_fidelity_operator');
insert into messages ( id_lang, id_key, label ) values ( 1, (select max(id) from keys), 'Fidelity Operator');
insert into gui_panels ( id_key, id_parent, classname ) values ( (select max(id) from keys), (select id from gui_panels where id_parent = -1), 'ro.atreides.restaurant.modules.fidelitybo.FidelityOperatorPanel');


insert into keys ( label ) values ( 'group_manager_evenimente');
insert into groups ( id_key ) values ((select min(id) from keys where keys.label = 'group_manager_evenimente'));
insert into messages ( id_lang, id_key, label ) values ( 1, (select min(id) from keys where keys.label = 'group_manager_evenimente'), 'Manager evenimente');

alter table ev_instance add column deleted boolean default false;


insert into keys ( label ) values ( 'passwordAtStergereClient');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'passwordAtStergereClient'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'passwordAtStergereClient') ;

alter table triggered_data alter remote set data type text;

alter table persoane_fizice drop column external_id;

update configs set value = '2016168' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016169' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016170' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016171' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016172' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016173' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016174' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016175' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016176' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016177' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016178' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016179' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016180' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016181' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016182' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016183' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016184' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016185' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016186' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016187' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016188' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016189' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016190' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016191' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016192' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016193' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016194' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016195' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016196' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016197' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ('backoffice_manager_panel');
insert into messages ( id_lang, id_key, label ) values ( 1, (select max(id) from keys), 'Backoffice Manager');
insert into gui_panels ( id_key, id_parent, classname ) values ( (select max(id) from keys), (select id from gui_panels where id_parent = -1), 'ro.atreides.restaurant.modules.fidelitybo.BackofficeManagerPanel');


create table sms_queue (
id serial unique,
mesaj text,
destinatar text,
status int,
eroare text
);

update configs set value = '2016198' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016199' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table ev_def add column status int default 1;
update configs set value = '2016200' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016201' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016202' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'clientRequirementOnAsociereCard');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'clientRequirementOnAsociereCard'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'clientRequirementOnAsociereCard') ;

update configs set value = '2016203' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016204' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016205' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'addTranzactieClientAtCloseOrder');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'addTranzactieClientAtCloseOrder'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'addTranzactieClientAtCloseOrder') ;

update configs set value = '2016206' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016207' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016208' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016209' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'jsonLicensing');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'jsonLicensing'), '0','0', 0);


insert into keys ( label ) values ( 'clientName');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'clientName'), '','', 1);

insert into keys ( label ) values ( 'partnerName');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'partnerName'), '','', 1);

insert into keys ( label ) values ( 'bdId');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bdId'), '','', 1);


update configs set value = '2016210' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016211' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016212' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016213' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016214' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016215' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016216' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016217' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016218' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016219' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016220' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016221' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016222' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'bonConsumNrDocManual');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonConsumNrDocManual'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'bonConsumNrDocManual') ;

 ALTER TABLE bonuri_consum ADD column observatii varchar(50);

insert into keys ( label ) values ( 'forceAllMenuOptions');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'forceAllMenuOptions'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'forceAllMenuOptions') ;

insert into keys ( label ) values ( 'selectMultipleMagazii');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selectMultipleMagazii'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selectMultipleMagazii') ;

insert into keys ( label ) values ( 'winmentorMagaziiExcluse');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'winmentorMagaziiExcluse'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'winmentorMagaziiExcluse') ;

insert into keys ( label ) values ( 'dataSyncIgnoreRemoteRecipeItems');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'dataSyncIgnoreRemoteRecipeItems'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'dataSyncIgnoreRemoteRecipeItems') ;

update configs set value = '2016223' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016224' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016225' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016226' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016227' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016228' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016229' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016230' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016231' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'anafGsonServerConf');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'anafGsonServerConf'), 'arhiva.deversoft.ro=9091','arhiva.deversoft.ro=9091', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'anafGsonServerConf') ;

update configs set value = '2016232' where id_key = ( select min(id) from keys where label = 'currentVersion' );



update configs set value = '2016233' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016234' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016235' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016236' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016237' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016238' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016239' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016240' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'touchProductSelectionDelayMillis');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touchProductSelectionDelayMillis'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'touchProductSelectionDelayMillis') ;

update configs set value = '2016241' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016242' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016243' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016244' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016245' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016246' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table bonfiscal_numbers drop constraint fk_bonfiscal_number__orders;

alter table bonfiscal_numbers add constraint fk_bonfiscal_number__orders foreign key (id_order) references orders (id) on delete cascade;

update configs set value = '2016247' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016248' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016249' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016250' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016251' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table promotii_clienti add column dateadded timestamp default current_timestamp;

update promotii_clienti set dateadded = (select last_updated from clienti where clienti.id = promotii_clienti.id_client);

update configs set value = '2016252' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016253' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016254' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016255' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016256' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016257' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016258' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016259' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016260' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016261' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016262' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016263' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016264' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016265' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016266' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016267' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016268' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016269' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016270' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016270' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016271' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016272' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016273' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016274' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016275' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016276' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016277' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016278' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016279' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016279' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016280' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016281' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016282' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016283' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016284' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016285' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016286' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016287' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table conta_export_mapping add column cota_tva numeric ;
update configs set value = '2016288' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table facturi add column data_scadenta timestamp ;
update facturi set data_Scadenta = dateadd;
alter table facturi alter column data_scadenta set not null;

update configs set value = '2016289' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016290' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016291' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016292' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table bonfiscal_numbers drop constraint if exists fk_bonfiscal_number__orders;

update configs set value = '2016293' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016294' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016295' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016296' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table corrections add column id_sectie int constraint FK_corrections__categories references categories(id) default null;


insert into keys ( label ) values ( 'fidelitatePuncteLaNota');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'fidelitatePuncteLaNota'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'fidelitatePuncteLaNota') ;

alter table orders add column puncte_consumate double precision default -1 ;


update configs set value = '2016297' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016298' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'showImportJsonOrdersButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showImportJsonOrdersButton'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'showImportJsonOrdersButton') ;

update configs set value = '2016352' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016353' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016354' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016355' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016356' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'nirShowCodProdus');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'nirShowCodProdus'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'nirShowCodProdus') ;


update configs set value = '2016357' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016358' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016359' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016360' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table tables add column id_produs_automat int constraint FK_tables__products references products(id) default null;

alter table tables add column id_promotie_automata int constraint FK_tables__promotions references promotions(id) default null;

alter table locations add column allow_autoclose_laplata_orders boolean default false;

update configs set value = '2016361' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016362' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016363' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016364' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016365' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'zPrintSplitProduseByCategorie');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'zPrintSplitProduseByCategorie'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'zPrintSplitProduseByCategorie') ;

update configs set value = '2016366' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016367' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016368' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016369' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016370' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'contaNirTotaluriPerTipProdus');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaNirTotaluriPerTipProdus'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'contaNirTotaluriPerTipProdus') ;

update configs set value = '2016371' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'punctlucru');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'punctlucru'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'punctlucru') ;

update configs set value = '2016372' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016373' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016374' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016375' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'touch2ShowInchideComenziButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touch2ShowInchideComenziButton'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'touch2ShowInchideComenziButton') ;


update configs set value = '2016376' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016377' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016378' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016379' where id_key = ( select min(id) from keys where label = 'currentVersion' );


create table tombola_tichete (
	id serial unique,
	nr_tichete int not null,
	id_client int not null constraint FK_tombola_tichete__clienti references clienti(id),
	id_location int not null constraint FK_tombola_tichete__locatii references locations(id),
	data_emiterii timestamp not null default current_timestamp,
	card text not null,
	data_expirarii timestamp ,
	cod_sync text
	);

update configs set value = '2016380' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016381' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016382' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'tombolaCurentaName');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'tombolaCurentaName'), 'Tombola Merkur','Tombola Merkur', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'tombolaCurentaName') ;

insert into keys ( label ) values ( 'tombolaCurentaExpireDate');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'tombolaCurentaExpireDate'), '01-01-2018','01-01-2018', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'tombolaCurentaExpireDate') ;

update configs set value = '2016383' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016384' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016385' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016386' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016387' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016388' where id_key = ( select min(id) from keys where label = 'currentVersion' );


create index cont_client__detalii on cont_client(detalii);


insert into keys ( label ) values ( 'fidelityAllLocationsList');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'fidelityAllLocationsList'), '1 mai,Prosper,Constanta 1,Constanta 3,Craiova,Ploiesti,Buzau,Sibiu,Railway station','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'fidelityAllLocationsList') ;

update configs set value = '2016389' where id_key = ( select min(id) from keys where label = 'currentVersion' );


update configs set value = '2016390' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016391' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table triggered_data add column text_filter text not null default '';

update configs set value = '2016392' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'retetarShowFontCombo');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'retetarShowFontCombo'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'retetarShowFontCombo') ;

update configs set value = '2016393' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016394' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016394' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016395' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016396' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016397' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016398' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016399' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016400' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016401' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016402' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016403' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016404' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'adminPanelsUseRights');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'adminPanelsUseRights'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'adminPanelsUseRights') ;

update keys set label = 'permissions_manage' where label = 'permissions_remove';

update messages set label = 'Manage' where id_key in ( select id from keys where label = 'permissions_manage');


update configs set value = '2016405' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016406' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016407' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016408' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016409' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016410' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016411' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016412' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'etichetePretKgDivisor');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'etichetePretKgDivisor'), '10','10', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'etichetePretKgDivisor') ;

update configs set value = '2016413' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016414' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016415' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'lotBarcodeStripChecksum');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'lotBarcodeStripChecksum'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'lotBarcodeStripChecksum') ;


insert into keys ( label ) values ( 'barcodePrinterManagesMultipleCopies');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'barcodePrinterManagesMultipleCopies'), '1','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'barcodePrinterManagesMultipleCopies') ;


alter table products add column cantar_order int;

update configs set value = '2016416' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016417' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'barcodeInternalDefaultPrefix');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'barcodeInternalDefaultPrefix'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'barcodeInternalDefaultPrefix') ;

update configs set value = '2016418' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016419' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016420' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016421' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016422' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016423' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016424' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'barcodePictureMaxWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'barcodePictureMaxWidth'), '226','226', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'barcodePictureMaxWidth') ;


insert into keys ( label ) values ( 'barcodePictureMaxHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'barcodePictureMaxHeight'), '50','50', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'barcodePictureMaxHeight') ;

update configs set value = '2016425' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table sales add column id_parentsale int constraint FK_sales__sales_parentsale references sales(id);

alter table productie add column id_parent int;

update configs set value = '2016426' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016427' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016428' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016429' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016430' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016431' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016432' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'cantarDistinctChars');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'cantarDistinctChars'), '27','27', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'cantarDistinctChars') ;

update configs set value = '2016433' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016434' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016435' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'meniuriMultipleSelectionAllowed');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'meniuriMultipleSelectionAllowed'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'meniuriMultipleSelectionAllowed') ;

insert into keys ( label ) values ( 'livrariShowSectiiInSelector');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'livrariShowSectiiInSelector'), '1','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'livrariShowSectiiInSelector') ;

insert into keys ( label ) values ( 'livrariMarcajAfterMeniuAdd');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'livrariMarcajAfterMeniuAdd'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'livrariMarcajAfterMeniuAdd') ;

insert into keys ( label ) values ( 'touchMarcajAfterMeniuAdd');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touchMarcajAfterMeniuAdd'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'touchMarcajAfterMeniuAdd') ;


update configs set value = '2016436' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016437' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016438' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016439' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016440' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'tipPlataFixedValues');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'tipPlataFixedValues'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'tipPlataFixedValues') ;

update configs set value = '2016441' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016442' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016443' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016444' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016445' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016446' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016447' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'incarcaCasaMaxValue');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'incarcaCasaMaxValue'), '1000','1000', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'incarcaCasaMaxValue') ;

update configs set value = '2016448' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016449' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'clientPersFiziceAllowSameNameAndBirthDay');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'clientPersFiziceAllowSameNameAndBirthDay'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'clientPersFiziceAllowSameNameAndBirthDay') ;

update configs set value = '2016450' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016451' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016452' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016453' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016454' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016455' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016456' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016457' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016458' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016459' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016460' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016461' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016462' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016463' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016464' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'terminalInventariereCatalogFormat');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'terminalInventariereCatalogFormat'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'terminalInventariereCatalogFormat') ;

insert into keys ( label ) values ( 'terminalInventariereCatalogCodInterPrefix');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'terminalInventariereCatalogCodInterPrefix'), 'CI','CI', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'terminalInventariereCatalogCodInterPrefix') ;

insert into keys ( label ) values ( 'terminalInventariereCatalogMateriiPrimePrefix');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'terminalInventariereCatalogMateriiPrimePrefix'), 'MP','MP', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'terminalInventariereCatalogMateriiPrimePrefix') ;

insert into keys ( label ) values ( 'terminalInventariereScanFormat');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'terminalInventariereScanFormat'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'terminalInventariereScanFormat') ;

insert into keys ( label ) values ( 'terminalInventariereScanItemSeparator');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'terminalInventariereScanItemSeparator'), '' , '', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'terminalInventariereScanItemSeparator') ;

update configs set value = '2016465' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016466' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016467' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016468' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016469' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016470' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016471' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016472' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016472' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016473' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016474' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016475' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016476' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016477' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016478' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'confirmareNrPersoaneAtNota');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'confirmareNrPersoaneAtNota'), '0','0', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'confirmareNrPersoaneAtNota') ;

insert into keys ( label ) values ( 'requestCardAtNota');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'requestCardAtNota'), '0','0', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'requestCardAtNota') ;

update configs set value = '2016479' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016480' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016481' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016482' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016483' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016484' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016485' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'clienti_manager');
insert into groups ( id_key ) values ((select min(id) from keys where keys.label = 'clienti_manager'));
insert into messages ( id_lang, id_key, label ) values ( 1, (select min(id) from keys where keys.label = 'clienti_manager'), 'Manager clienti management online');

alter table persoane_fizice add column discount int not null default 1;


insert into keys ( label ) values ( 'addClientIfNotFound');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'addClientIfNotFound'), '1','0', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'addClientIfNotFound') ;

update configs set value = '2016486' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table persoane_fizice add column password text not null default '';

update configs set value = '2016487' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table persoane_fizice alter adresa set default '';

update configs set value = '2016488' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table products_Barcodes alter column barcode set data type text;

update configs set value = '2016489' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016490' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016491' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016492' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table fisa_magazie drop constraint if exists fk_fisa_magazie__sales ;

alter table fisa_magazie rename column id_sale to id_document_item;

alter table fisa_magazie add column id_document int ;

update configs set value = '2016493' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table cont_client add column id_order int ;

alter table cont_client add CONSTRAINT FK_CONT_CLIENT__ORDERS FOREIGN KEY (id_order) references orders(id) on delete cascade;

update configs set value = '2016493' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016493' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016493' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016493' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016494' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016495' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016496' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016497' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016498' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016499' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016500' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016501' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016502' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016503' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016504' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016505' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016507' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'selector2Level1MaxHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level1MaxHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level1MaxHeight') ;

insert into keys ( label ) values ( 'selector2Level2MaxHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level2MaxHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level2MaxHeight') ;

insert into keys ( label ) values ( 'selector2Level3MaxHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level3MaxHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level3MaxHeight') ;



insert into keys ( label ) values ( 'selector2Level1MaxWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level1MaxWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level1MaxWidth') ;

insert into keys ( label ) values ( 'selector2Level2MaxWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level2MaxWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level2MaxWidth') ;

insert into keys ( label ) values ( 'selector2Level3MaxWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level3MaxWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level3MaxWidth') ;


update configs set value = '2016508' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016509' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016510' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016511' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'selector2Level1PrefHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level1PrefHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level1PrefHeight') ;

insert into keys ( label ) values ( 'selector2Level2PrefHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level2PrefHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level2PrefHeight') ;

insert into keys ( label ) values ( 'selector2Level3PrefHeight');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level3PrefHeight'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level3PrefHeight') ;



insert into keys ( label ) values ( 'selector2Level1PrefWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level1PrefWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level1PrefWidth') ;

insert into keys ( label ) values ( 'selector2Level2PrefWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level2PrefWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level2PrefWidth') ;

insert into keys ( label ) values ( 'selector2Level3PrefWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level3PrefWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level3PrefWidth') ;


update configs set value = '2016512' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016513' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016514' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'selector2Level1MinWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level1MinWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level1MinWidth') ;

insert into keys ( label ) values ( 'selector2Level2MinWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level2MinWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level2MinWidth') ;

insert into keys ( label ) values ( 'selector2Level3MinWidth');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level3MinWidth'), '-1','-1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level3MinWidth') ;


insert into keys ( label ) values ( 'selector2Level1FontStyle');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level1FontStyle'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level1FontStyle') ;

insert into keys ( label ) values ( 'selector2Level2FontStyle');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level2FontStyle'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level2FontStyle') ;

insert into keys ( label ) values ( 'selector2Level3FontStyle');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level3FontStyle'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level3FontStyle') ;


insert into keys ( label ) values ( 'selector2Level1FontName');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level1FontName'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level1FontName') ;

insert into keys ( label ) values ( 'selector2Level2FontName');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level2FontName'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level2FontName') ;

insert into keys ( label ) values ( 'selector2Level3FontName');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'selector2Level3FontName'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'selector2Level3FontName') ;update configs set value = '2016515' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016516' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016517' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016518' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016519' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016520' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016521' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016522' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016523' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016524' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016525' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016526' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016527' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016528' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016529' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016530' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016531' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016532' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'showComandaAprovizionareButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showComandaAprovizionareButton'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'showComandaAprovizionareButton') ;

update configs set value = '2016533' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016534' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016535' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ('gui_comenzi_aprovizionare');
insert into messages ( id_lang, id_key, label ) values ( 1, (select max(id) from keys), 'Comenzi Aprovizionare');
insert into gui_panels ( id_key, id_parent, classname ) values ( (select max(id) from keys), (select id from gui_panels where id_parent = -1), 'ro.atreides.restaurant.gui.reports.CentralizareComenziReport');

update configs set value = '2016536' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016537' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016538' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016539' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'gdprKeyAlreadyGenerated');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'gdprKeyAlreadyGenerated'), '0','0', 0);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'gdprKeyAlreadyGenerated') ;



alter table persoane_fizice disable trigger all;

alter table buletine disable trigger all;

alter table pasapoarte disable trigger all;

alter table clienti disable trigger all;

alter table file_versions disable trigger all;



ALTER TABLE persoane_fizice ADD COLUMN CRYPT TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN nume SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN prenume SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN adresa SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN oras SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN telefon_fix SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN fax SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN telefon_mobil SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN email SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN pagina_web SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN locul_nasterii SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN cetatenie SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ALTER COLUMN nationalitate SET DATA TYPE TEXT;

ALTER TABLE persoane_fizice ADD COLUMN data_nasterii_text TEXT;

ALTER TABLE persoane_fizice ADD COLUMN data_numelui_text TEXT;

UPDATE persoane_fizice SET data_nasterii_text = data_nasterii;

UPDATE persoane_fizice SET data_numelui_text = data_numelui;

ALTER table persoane_fizice alter column data_nasterii drop not null;

ALTER table persoane_fizice alter column data_numelui drop not null;



CREATE OR REPLACE FUNCTION no_crypt_value(coloana TEXT, crypt TEXT, pass TEXT) RETURNS TEXT AS $$
DECLARE
	value TEXT;
BEGIN
	IF crypt = 'C' THEN
		value := convert_from(decrypt(CAST(coloana as bytea), CAST(pass as bytea),'aes'), 'UTF-8');
	ELSE
		value := coloana;
	END IF;
	return value;
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION crypt_persoanefizice(keyEncryption TEXT) RETURNS void AS $$
DECLARE
	rec record;
	byteKeyEnc bytea := CAST(keyEncryption as bytea);
 	cur_persoane CURSOR FOR SELECT * FROM persoane_fizice WHERE crypt IS NULL;
BEGIN
	OPEN cur_persoane;

	LOOP

		FETCH cur_persoane INTO rec;

		EXIT WHEN NOT FOUND;

		UPDATE persoane_fizice SET nume = encrypt(CAST(replace(nume,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        prenume = encrypt(CAST(replace(prenume,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        adresa = encrypt(CAST(replace(adresa,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        oras = encrypt(CAST(replace(oras,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        telefon_fix = encrypt(CAST(replace(telefon_fix,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        fax = encrypt(CAST(replace(fax,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        telefon_mobil = encrypt(CAST(replace(telefon_mobil,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        email = encrypt(CAST(replace(email,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        pagina_web = encrypt(CAST(replace(pagina_web,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        locul_nasterii = encrypt(CAST(replace(locul_nasterii,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        cetatenie = encrypt(CAST(replace(cetatenie,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        nationalitate = encrypt(CAST(replace(nationalitate,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        data_nasterii_text = encrypt(CAST(  data_nasterii_text as bytea) ,byteKeyEnc,'aes'),
        data_numelui_text = encrypt(CAST(data_numelui_text as bytea) ,byteKeyEnc,'aes'),
        crypt = 'C' WHERE CURRENT OF cur_persoane;

	END LOOP;
	CLOSE cur_persoane;

END;
$$ LANGUAGE plpgsql;




ALTER TABLE buletine ALTER COLUMN cnp SET DATA TYPE TEXT;
ALTER TABLE buletine ALTER COLUMN seria SET DATA TYPE TEXT;
ALTER TABLE buletine ALTER COLUMN numarul SET DATA TYPE TEXT;
ALTER TABLE buletine ALTER COLUMN sectia SET DATA TYPE TEXT;

ALTER TABLE buletine ADD COLUMN CRYPT TEXT;

ALTER TABLE buletine ADD COLUMN eliberat_la_text TEXT;

ALTER TABLE buletine ADD COLUMN expira_la_text TEXT;

UPDATE buletine SET eliberat_la_text = eliberat_la;

UPDATE buletine SET expira_la_text = expira_la;



ALTER TABLE pasapoarte ALTER COLUMN seria SET DATA TYPE TEXT;
ALTER TABLE pasapoarte ALTER COLUMN tip_act SET DATA TYPE TEXT;


ALTER TABLE pasapoarte ADD COLUMN eliberat_la_text TEXT;

ALTER TABLE pasapoarte ADD COLUMN expira_la_text TEXT;

ALTER TABLE pasapoarte ADD COLUMN CRYPT TEXT;

UPDATE pasapoarte SET eliberat_la_text = eliberat_la;

UPDATE pasapoarte SET expira_la_text = expira_la;


CREATE OR REPLACE FUNCTION crypt_buletine(keyEncryption TEXT) RETURNS void AS $$
DECLARE
	rec record;
	byteKeyEnc bytea := CAST(keyEncryption as bytea);
 	cur_buletine CURSOR FOR SELECT * FROM buletine WHERE crypt IS NULL;
BEGIN
	OPEN cur_buletine;

	LOOP

		FETCH cur_buletine INTO rec;

		EXIT WHEN NOT FOUND;

		UPDATE buletine SET cnp = encrypt(CAST(replace(cnp,'\', '\\') as bytea), byteKeyEnc, 'aes'),
        seria = encrypt(CAST(replace(seria,'\', '\\') as bytea), byteKeyEnc, 'aes'),
        numarul = encrypt(CAST(replace(numarul,'\', '\\') as bytea), byteKeyEnc, 'aes'),
        sectia = encrypt(CAST(replace(sectia,'\', '\\') as bytea), byteKeyEnc, 'aes'),
        eliberat_la_text = encrypt(CAST(eliberat_la_text as bytea), byteKeyEnc, 'aes'),
        expira_la_text = encrypt(CAST(expira_la_text as bytea), byteKeyEnc, 'aes'),
        crypt = 'C' WHERE CURRENT OF cur_buletine;

	END LOOP;
	CLOSE cur_buletine;

END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION crypt_pasapoarte(keyEncryption TEXT) RETURNS void AS $$
DECLARE
	rec record;
	byteKeyEnc bytea := CAST(keyEncryption as bytea);
 	cur_pasapoarte CURSOR FOR SELECT * FROM pasapoarte WHERE crypt IS NULL;
BEGIN
	OPEN cur_pasapoarte;

	LOOP

		FETCH cur_pasapoarte INTO rec;

		EXIT WHEN NOT FOUND;

		UPDATE pasapoarte SET seria = encrypt(CAST(replace(seria,'\', '\\') as bytea), byteKeyEnc, 'aes'),
        tip_act = encrypt(CAST(replace(tip_act,'\', '\\') as bytea), byteKeyEnc, 'aes'),
        eliberat_la_text = encrypt(CAST(eliberat_la_text as bytea), byteKeyEnc, 'aes'),
        expira_la_text = encrypt(CAST(expira_la_text as bytea), byteKeyEnc, 'aes'),
        crypt = 'C' WHERE CURRENT OF cur_pasapoarte;

	END LOOP;
	CLOSE cur_pasapoarte;

END;
$$ LANGUAGE plpgsql;



alter table persoane_fizice enable trigger all;

alter table buletine enable trigger all;

alter table pasapoarte enable trigger all;

alter table clienti enable trigger all;

alter table file_versions enable trigger all;


update configs set value = '2016542' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016543' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016544' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016545' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016545' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016546' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016547' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016548' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016549' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016550' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016551' where id_key = ( select min(id) from keys where label = 'currentVersion' );


ALTER TABLE file_versions ADD COLUMN crypt TEXT;

CREATE OR REPLACE FUNCTION encrypt_blob_file_versions(keyEncryption TEXT) RETURNS boolean AS $$
DECLARE
	rec record;
	byteKeyEnc bytea := CAST(keyEncryption as bytea);
	cur_file_v cursor FOR SELECT * FROM file_versions WHERE crypt is null limit 100;
	contor integer;
BEGIN
	OPEN cur_file_v;
	LOOP

		FETCH cur_file_v INTO rec;
		EXIT WHEN NOT FOUND;

		UPDATE file_versions SET bytes = encrypt(rec.bytes, byteKeyEnc, 'aes'), crypt = 'C' WHERE CURRENT OF cur_file_v;
	END LOOP;
	CLOSE cur_file_v;

	SELECT count(*) into contor FROM file_versions WHERE crypt is null;

	IF contor <> 0 THEN
		RETURN TRUE;
	ELSE
		RETURN FALSE;
	END IF;
END;
$$ LANGUAGE plpgsql;




ALTER TABLE persoane_juridice ADD crypt TEXT;

ALTER TABLE persoane_juridice ALTER COLUMN nume SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN cui SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN j SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN adresa SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN oras SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN banca SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN iban SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN punct_lucru SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN telefon_fix SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN fax SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN telefon_mobil SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN email SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN pagina_web SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN persoana_contact SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN observatii SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN analitic_furnizor SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN analitic_client SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN alias SET DATA TYPE TEXT;
ALTER TABLE persoane_juridice ALTER COLUMN grad_risc SET DATA TYPE TEXT;

CREATE OR REPLACE FUNCTION crypt_persoane_juridice(keyEncryption TEXT) RETURNS void AS $$
DECLARE
	rec record;
	byteKeyEnc bytea := CAST(keyEncryption as bytea);
 	cur_persoane CURSOR FOR SELECT * FROM persoane_juridice WHERE crypt IS NULL;
BEGIN
	OPEN cur_persoane;

	LOOP

		FETCH cur_persoane INTO rec;

		EXIT WHEN NOT FOUND;

		UPDATE persoane_juridice SET nume = encrypt(CAST(replace(rec.nume,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        cui = encrypt(CAST(replace(rec.cui,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        j = encrypt(CAST(replace(rec.j,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        adresa = encrypt(CAST(replace(rec.adresa,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        oras = encrypt(CAST(replace(rec.oras,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        banca = encrypt(CAST(replace(rec.banca,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        iban = encrypt(CAST(replace(rec.iban,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        punct_lucru = encrypt(CAST(replace(rec.punct_lucru,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        telefon_fix = encrypt(CAST(replace(rec.telefon_fix,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        fax = encrypt(CAST(replace(rec.fax,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        telefon_mobil = encrypt(CAST(replace(rec.telefon_mobil,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        email = encrypt(CAST(replace(rec.email,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        pagina_web = encrypt(CAST(replace(rec.pagina_web,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        persoana_contact = encrypt(CAST(replace(rec.persoana_contact,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        observatii = encrypt(CAST(replace(rec.observatii,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        analitic_furnizor = encrypt(CAST(replace(rec.analitic_furnizor,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        analitic_client = encrypt(CAST(replace(rec.analitic_client,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        grad_risc = encrypt(CAST(replace(rec.grad_risc,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        alias = encrypt(CAST(replace(rec.alias,'\', '\\') as bytea) ,byteKeyEnc,'aes'),
        crypt = 'C' WHERE CURRENT OF cur_persoane;

	END LOOP;
	CLOSE cur_persoane;

END;
$$ LANGUAGE plpgsql;


update configs set value = '2016559' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016560' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016561' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016562' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016564' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016566' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016567' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016578' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016580' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016581' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016582' where id_key = ( select min(id) from keys where label = 'currentVersion' );



create index persoane_fizice__crypt on persoane_fizice(crypt);
create index persoane_juridice__crypt on persoane_juridice(crypt);
create index buletine__crypt on buletine(crypt);
create index pasapoarte__crypt on pasapoarte(crypt);
create index file_versions__crypt on file_versions(crypt);

update configs set value = '2016583' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016584' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016585' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016586' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016588' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016589' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016590' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016591' where id_key = ( select min(id) from keys where label = 'currentVersion' );

create table notifications(id serial, imp_level integer, description text, data_add date default current_timestamp, status text,
 user_saw boolean default false, handle_type text, user_can_dismiss boolean default false, notification_type text);

alter table configs alter column default_value set DATA TYPE text;
alter table configs alter column value set DATA TYPE text;

insert into keys ( label ) values ( 'clientPersFizicaRights');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'clientPersFizicaRights'), 'nume#*,prenume#*,data_nasterii_text#*,pagina_web,locul_nasterii#*,data_numelui_text,cetatenie,nationalitate,telefon_fix,fax,telefon_mobil,email,observatii,adresa,oras,table=persoane_fizice,cnp,seria,numarul,table=buletine,seria,tip_act,table=pasapoarte,picture,table=files','nume#*,prenume#*,data_nasterii_text#*,pagina_web,locul_nasterii#*,data_numelui_text,cetatenie,nationalitate,telefon_fix,fax,telefon_mobil,email,observatii,adresa,oras,table=persoane_fizice,cnp,seria,numarul,table=buletine,seria,tip_act,table=pasapoarte,picture,table=files', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'clientPersFizicaRights') ;

insert into keys ( label ) values ('gdprKeyHash');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'gdprKeyHash'), '','', 0);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'gdprKeyHash') ;



update configs set value = '2016593' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016594' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016595' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016596' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016597' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016598' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016599' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table rest_incasari alter column comment set data type text;

update configs set value = '2016600' where id_key = ( select min(id) from keys where label = 'currentVersion' );


create index persoane_fizice__Status on persoane_fizice(status);

create index clients_groups__idgroup on clients_groups(id_group);

create index clients_groups__idclient on clients_groups(id_client);



insert into keys ( label ) values ('tvaPerLocatiiRewriteCota');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'tvaPerLocatiiRewriteCota'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'tvaPerLocatiiRewriteCota') ;



insert into keys ( label ) values ('tvaPerLocatiiRewriteDepartment');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'tvaPerLocatiiRewriteDepartment'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'tvaPerLocatiiRewriteDepartment') ;


update configs set value = '2016601' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016602' where id_key = ( select min(id) from keys where label = 'currentVersion' );


update configs set value = '2016603' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016604' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016605' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016606' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016608' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016609' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016610' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016611' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016612' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016613' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016614' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016615' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016616' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016617' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016618' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016619' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016620' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016622' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016624' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016625' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016626' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016627' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016628' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016629' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table tva add column last_updated timestamp not null default current_timestamp;

alter table locations add column last_updated timestamp not null default current_timestamp;

alter table categories add column last_updated timestamp not null default current_timestamp;

alter table subcategories add column last_updated timestamp not null default current_timestamp;

alter table products add column last_updated timestamp not null default current_timestamp;

alter table products_barcodes add column last_updated timestamp not null default current_timestamp;

alter table produse_reteta add column last_updated timestamp not null default current_timestamp;

alter table stocks add column last_updated timestamp not null default current_timestamp;

alter table users add column last_updated timestamp not null default current_timestamp;

alter table servire add column last_updated timestamp not null default current_timestamp;

update configs set value = '2016633' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table keys alter column label set data type text;

alter table configs alter column value set data type text;

alter table configs alter column default_value set data type text;

update configs set value = '2016635' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016636' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016637' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ('touch6promotionTextFormat');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touch6promotionTextFormat'),
'<html>
  <body>
  <font color="red">%%retur%%</font>
  <font color="green">%%protocol%%</font><br>
  <font color="green">%%promo_name%%</font><br>
  <font color="blue">%%client_name%%</font><br>
  <font color="blue">%%available_discount%%</font>
  <font color="blue">%%cont_value%%</font><br>
  </body>
  </html>','<html>
             <body>
             <font color="red">%%retur%%</font>
             <font color="green">%%protocol%%</font><br>
             <font color="green">%%promo_name%%</font><br>
             <font color="blue">%%client_name%%</font><br>
             <font color="blue">%%available_discount%%</font>
             <font color="blue">%%cont_value%%</font><br>
             </body>
             </html>', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'touch6promotionTextFormat') ;



insert into keys ( label ) values ('showIncasatButton');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'showIncasatButton'),
'1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'showIncasatButton') ;

update configs set value = '2016638' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016639' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ('notaPrintZeroPriceFilter');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'notaPrintZeroPriceFilter'),
'NONZERO_ORIGINAL_PRICE','NONZERO_ORIGINAL_PRICE', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'notaPrintZeroPriceFilter') ;



update configs set value = '2016640' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016641' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ('productieQuickMasaTextFormat');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieQuickMasaTextFormat'),
'<html><font color="red" size="+2">%%masa%%</font></html>',
'<html><font color="red" size="+2">%%masa%%</font></html>', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'productieQuickMasaTextFormat') ;

insert into keys ( label ) values ('productieQuickItemTextFormat');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieQuickItemTextFormat'),
'<html><font size="6">%%platou%% %%ora%% %%qty%% BUC * %%produs%%<i> %%detalii%%</i></font></html>',
'<html><font size="6">%%platou%% %%ora%% %%qty%% BUC * %%produs%%<i> %%detalii%%</i></font></html>', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'productieQuickItemTextFormat') ;


insert into keys ( label ) values ('productieQuickLateMinutes');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieQuickLateMinutes'),
'20','20', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'productieQuickLateMinutes') ;

insert into keys ( label ) values ('productieQuickUrgentMinutes');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieQuickUrgentMinutes'),
'40','40', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'productieQuickUrgentMinutes') ;

insert into keys ( label ) values ('productieQuickScrollByColumn');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieQuickScrollByColumn'),
'1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'productieQuickScrollByColumn') ;

insert into keys ( label ) values ('productieQuickZoomedInTimeoutSeconds');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieQuickZoomedInTimeoutSeconds'),
'30','30', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'productieQuickZoomedInTimeoutSeconds') ;


update configs set value = '2016642' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016643' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016644' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016645' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016651' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016653' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016656' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016658' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016659' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ('cardFidelitatePattern');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'cardFidelitatePattern'),
'','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'cardFidelitatePattern') ;

insert into keys ( label ) values ('cardAutentificarePattern');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'cardAutentificarePattern'),
'','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'cardAutentificarePattern') ;

insert into keys ( label ) values ('hotelCardCameraPattern');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'hotelCardCameraPattern'),
'','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'hotelCardCameraPattern') ;

insert into keys ( label ) values ('cardFidelitatePatternGroupIndex');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'cardFidelitatePatternGroupIndex'),
'0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'cardFidelitatePatternGroupIndex') ;

insert into keys ( label ) values ('cardAutentificarePatternGroupIndex');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'cardAutentificarePatternGroupIndex'),
'0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'cardAutentificarePatternGroupIndex') ;

insert into keys ( label ) values ('hotelCardCameraPatternGroupIndex');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'hotelCardCameraPatternGroupIndex'),
'0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'hotelCardCameraPatternGroupIndex') ;

insert into keys ( label ) values ('cardReadTimeoutMillis');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'cardReadTimeoutMillis'),
'500','500', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'cardReadTimeoutMillis') ;


insert into keys ( label ) values ('alimentareContTipPlataAllowed');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'alimentareContTipPlataAllowed'),
'','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'alimentareContTipPlataAllowed') ;

update configs set value = '2016660' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016661' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016662' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016663' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into product_attribute ( label, export_field ) values ( 'ZileExpirare', 'zile_expirare');
insert into mapping_product_attribute (id_product, id_attribute, value) select id, (select id from product_attribute where export_field = 'zile_expirare'), '-1' from products;

update configs set value = '2016664' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016665' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016666' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table pontaj alter column punch_in set default current_timestamp;


update configs set value = '2016667' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016668' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016669' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into product_attribute ( label, export_field ) values ( 'Winmentor Cont Serviciu', 'winmentor_cont_serviciu');
insert into mapping_product_attribute (id_product, id_attribute, value) select id, (select id from product_attribute where export_field = 'winmentor_cont_serviciu'), '' from products;

insert into keys ( label ) values ('winmentorExportFacturiTipCodFurnizor');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'winmentorExportFacturiTipCodFurnizor'),
'id','id', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'winmentorExportFacturiTipCodFurnizor') ;

update configs set value = '2016670' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ('angajatiForImpozit');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'angajatiForImpozit'),
'','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'angajatiForImpozit') ;

insert into keys ( label ) values ('useSlaveSchemasForDiscount');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'useSlaveSchemasForDiscount'),
'0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
and id_key = (select min(id) from keys where keys.label = 'useSlaveSchemasForDiscount') ;

insert into keys ( label ) values ( 'procentImpozitDinDiscount');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'procentImpozitDinDiscount'), 'CONTRIBUTII=0.325,IMPOZIT=0.1','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'procentImpozitDinDiscount') ;

update configs set value = '2016671' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016672' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016673' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016674' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016675' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table facturi_emise add column detalii_plata text not null default '';

update configs set value = '2016676' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table printers add column printer_connection_type text not null default 'LOCAL_PRINT_SYSTEM';

insert into keys ( label ) values ( 'printerDirect2DeviceConfiguration');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'printerDirect2DeviceConfiguration'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'printerDirect2DeviceConfiguration') ;

insert into keys ( label ) values ( 'printerInternalBuzzer');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'printerInternalBuzzer'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'printerInternalBuzzer') ;

update configs set value = '2016677' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'touchShowValoareNotaPePlan');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touchShowValoareNotaPePlan'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'touchShowValoareNotaPePlan') ;

update configs set value = '2016678' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016679' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016680' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016682' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016683' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'societateaNoastraTvaLaIncasare');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'societateaNoastraTvaLaIncasare'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'societateaNoastraTvaLaIncasare') ;

update configs set value = '2016685' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016686' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table tip_articol add column cont_debit text not null default '';

update tip_articol set cont_debit = '707' where cont = '371';
update tip_articol set cont_debit = '708' where cont = '381';

insert into tip_articol ( cod, nume, cont, cont_debit) values ( 3, 'Servicii vandute', '', '704');
insert into tip_articol ( cod, nume, cont, cont_debit) values ( 4, 'Produse finite', '', '7015');


update configs set value = '2016687' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016688' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'contaExportUiPathLineTerminator');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportUiPathLineTerminator'), '','', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'contaExportUiPathLineTerminator') ;

update configs set value = '2016689' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table produse_reteta add column id_tip_articol int constraint FK_produse_reteta__tip_articol references tip_articol(id) not null default 1;
update produse_reteta set id_tip_articol = ( select id from tip_articol where nume = 'Produse finite' ) ;

alter table sales add column cota_tva float8;

create table temp_sales_cotatva ( cota float8, id_sale int, id_tva int );

insert into temp_sales_cotatva ( id_sale, id_tva, cota ) select s.id , tva.id, tva.cota from sales s inner join products p on s.id_product = p.id inner join subcategories sc on p.id_subcategory = sc.id inner join tva on sc.id_tva = tva.id ;

create index temp_sales_cotatva__cota on temp_sales_cotatva(cota);
create index temp_sales_cotatva__idsale on temp_sales_cotatva(id_sale);

update sales set cota_tva = ( select cota from temp_sales_cotatva t where sales.id = t.id_sale );

drop table temp_sales_cotatva;


insert into keys ( label ) values ( 'contaExportUiPathIesiriFallbackMagazieCod');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportUiPathIesiriFallbackMagazieCod'), '4','4', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'contaExportUiPathIesiriFallbackMagazieCod') ;

 update configs set value = '2016690' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016691' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016692' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016693' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016694' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016695' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016696' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table transfer_items add column product_name text not null default '';

update configs set value = '2016697' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'facturareExportXlsFileDirectory');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'facturareExportXlsFileDirectory'), 'export_facturi','export_facturi', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'facturareExportXlsFileDirectory') ;

update configs set value = '2016698' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'touch6TransferAvailableTypes');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'touch6TransferAvailableTypes'),
'TRANSFER_INTERN_FARA_PRINT,TRANSFER_INTERN_PRINT_PRET_ACZ,TRANSFER_INTERN_PRINT_PRET_VZ,TRANSFER_INTERN_PRINT_PRET_ACZ_VZ,AVIZ_INTERN_PRET_ACZ,AVIZ_INTERN_PRET_VZ,AVIZ_EXTERN_PRET_VZ',
'TRANSFER_INTERN_FARA_PRINT,TRANSFER_INTERN_PRINT_PRET_ACZ,TRANSFER_INTERN_PRINT_PRET_VZ,TRANSFER_INTERN_PRINT_PRET_ACZ_VZ,AVIZ_INTERN_PRET_ACZ,AVIZ_INTERN_PRET_VZ,AVIZ_EXTERN_PRET_VZ', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'touch6TransferAvailableTypes') ;

update configs set value = '2016699' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'contaExportUiPathGrupeazaIesiri');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportUiPathGrupeazaIesiri'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'contaExportUiPathGrupeazaIesiri') ;


create table conta_exported_clients ( id serial unique, id_client int not null constraint FK_CONTA_EXPORTED_CLIENTS__CLIENTI references clienti(id), exportdate timestamp not null default current_timestamp);

update configs set value = '2016700' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016701' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016702' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016703' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016704' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016705' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016706' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016707' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016708' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016709' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016710' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table conta_exported_clients add column fields_at_export text not null default '';

update configs set value = '2016711' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016712' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ('group_furnizor_taxare_inversa');
insert into messages ( id_lang, id_key, label ) values ( 1, (select min(id) from keys where keys.label = 'group_furnizor_taxare_inversa'), 'Taxare inversa');
insert into pers_juridice_groups(id_key, status) values ( (select min(id) from keys where keys.label = 'group_furnizor_taxare_inversa') , 1 );


update configs set value = '2016713' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016714' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016715' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016716' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into product_attribute ( label, export_field ) values ( 'Afisat la', 'afisat_la');
insert into mapping_product_attribute (id_product, id_attribute, value) select id, (select id from product_attribute where export_field = 'afisat_la'), 'SelectorProduse,Meniu' from products;

update configs set value = '2016717' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016718' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016719' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016721' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016722' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016723' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016724' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into product_attribute ( label, export_field ) values ( 'ExportContaPretCostManual', 'ExportContaPretCostManual');
insert into mapping_product_attribute (id_product, id_attribute, value) select id, (select id from product_attribute where export_field = 'ExportContaPretCostManual'), '' from products;

alter table mapping_product_attribute drop constraint if exists PRODUCT_ATTR_UNIQUE;
alter table mapping_product_attribute add constraint PRODUCT_ATTR_UNIQUE unique(id_product, id_attribute);

update configs set value = '2016725' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016726' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016727' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'productsFreeText1LanguageCode');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productsFreeText1LanguageCode'), 'EN','EN', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'productsFreeText1LanguageCode') ;

 insert into keys ( label ) values ( 'productsFreeText2LanguageCode');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productsFreeText2LanguageCode'), '','', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'productsFreeText2LanguageCode') ;

 insert into keys ( label ) values ( 'productsMainNameLanguageCode');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productsMainNameLanguageCode'), 'RO','RO', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'productsMainNameLanguageCode') ;

delete from mapping_product_attribute where id_attribute = (select id from product_attribute where export_field = 'afisat_la');
insert into mapping_product_attribute (id_product, id_attribute, value) select id, (select id from product_attribute where export_field = 'afisat_la'), 'SingleProductSalePOS,MenuSale,DeverTopDelivery,DeverTopMenu' from products;

insert into product_attribute ( label, export_field ) values ( 'gramaj', 'gramaj');
insert into mapping_product_attribute (id_product, id_attribute, value) select id, (select id from product_attribute where export_field = 'gramaj'), '' from products;

insert into product_attribute ( label, export_field ) values ( 'calorii', 'calorii');
insert into mapping_product_attribute (id_product, id_attribute, value) select id, (select id from product_attribute where export_field = 'calorii'), '' from products;

create table mobilepos_schema_apikeys ( id serial unique, schema_name text not null, apikey text not null );

 insert into keys ( label ) values ( 'livrariDataOraLivrarePattern');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'livrariDataOraLivrarePattern'), 'yyyy-MM-dd HH:mm','yyyy-MM-dd HH:mm', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'livrariDataOraLivrarePattern') ;

insert into keys ( label ) values ( 'livrariDecreaseQuantityAskForConfirmation');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'livrariDecreaseQuantityAskForConfirmation'), '1','1', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'livrariDecreaseQuantityAskForConfirmation') ;

update configs set value = '2016728' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016730' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'livrariOrderListFormat');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'livrariOrderListFormat'),
    '<html>#{data} / <font color="blue">#{order_id}</font> / <font color="red">#{total} RON</font><br>#{order_adresaLivrare}<br>#{client}</html>',
    '<html>#{data} / <font color="blue">#{order_id}</font> / <font color="red">#{total} RON</font><br>#{order_adresaLivrare}<br>#{client}</html>', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'livrariOrderListFormat') ;


 insert into keys ( label ) values ( 'useIngredientsAsProductDetails');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'useIngredientsAsProductDetails'), '0','0', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'useIngredientsAsProductDetails') ;

insert into product_attribute ( label, export_field ) values ( 'Ingrediente', 'Ingrediente');
insert into mapping_product_attribute (id_product, id_attribute, value) select id, (select id from product_attribute where export_field = 'Ingrediente'), '' from products;

update configs set value = '2016732' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016733' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016735' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016736' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016737' where id_key = ( select min(id) from keys where label = 'currentVersion' );


update configs set value = '<html>#{data} / <font color="blue">#{order}</font> / <font color="red">#{total} RON</font><br>#{adresaLivrare}<br>#{client}</html>'
where id_key in (select keys.id from keys where label = 'livrariOrderListFormat' ) and value = '<html>#{data} / <font color="blue">#{order_id}</font> / <font color="red">#{total} RON</font><br>#{order_adresaLivrare}<br>#{client}</html>';

update configs set default_value = '<html>#{data} / <font color="blue">#{order}</font> / <font color="red">#{total} RON</font><br>#{adresaLivrare}<br>#{client}</html>'
where id_key in (select keys.id from keys where label = 'livrariOrderListFormat' ) and default_value = '<html>#{data} / <font color="blue">#{order_id}</font> / <font color="red">#{total} RON</font><br>#{order_adresaLivrare}<br>#{client}</html>';

update configs set value = '2016739' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016740' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016741' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016742' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'xComboShowProductsWithoutCode');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'xComboShowProductsWithoutCode'), '1','1', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'xComboShowProductsWithoutCode') ;


update configs set value = '2016743' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016744' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016745' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'contaExportUiPathExportMonetarFilenameGrupareKey');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportUiPathExportMonetarFilenameGrupareKey'),
  '#{tipplata}=#{nrdoc} #{locatie} #{tipplata} #{data}','#{tipplata}=#{nrdoc} #{locatie} #{tipplata} #{data}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'contaExportUiPathExportMonetarFilenameGrupareKey') ;

update configs set value = '2016746' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016747' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016748' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016749' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'fidelitateContDefault');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'fidelitateContDefault'), 'Cont','Cont', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'fidelitateContDefault') ;

update configs set value = '2016753' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016754' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016755' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016756' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016757' where id_key = ( select min(id) from keys where label = 'currentVersion' );

create index orders__external_id on orders(external_id);

update configs set value = '2016759' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016760' where id_key = ( select min(id) from keys where label = 'currentVersion' );


create sequence sequence_receiptid_human;

create sequence sequence_orderid_human;

alter table orders add column human_orderid int not null default -1;

alter table bons add column human_receiptid int not null default -1;

insert into keys ( label ) values ( 'orderIdentifierDefaultFormat');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'orderIdentifierDefaultFormat'), '#{orderid}','#{orderid}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'orderIdentifierDefaultFormat') ;


insert into keys ( label ) values ( 'orderIdentifierLabelPrinter');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'orderIdentifierLabelPrinter'), '#{hour}-#{orderid}-#{location}','#{hour}-#{orderid}-#{location}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'orderIdentifierLabelPrinter') ;


insert into keys ( label ) values ( 'bonIdentifierDefaultFormat');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonIdentifierDefaultFormat'), 'Nr #{receiptid}','Nr #{receiptid}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'bonIdentifierDefaultFormat') ;


insert into keys ( label ) values ( 'bonIdentifierAsNota');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonIdentifierAsNota'), 'Comanda Nr #{orderid}','Comanda Nr #{orderid}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'bonIdentifierAsNota') ;


insert into keys ( label ) values ( 'bonIdentifierShortFormat');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonIdentifierShortFormat'), '#{orderid}','#{orderid}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'bonIdentifierShortFormat') ;

insert into keys ( label ) values ( 'bonIdentifierLabelPrinter');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonIdentifierLabelPrinter'), '#{hour}-#{orderid}-#{location}','#{hour}-#{orderid}-#{location}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'bonIdentifierLabelPrinter') ;


insert into keys ( label ) values ( 'resetHumanSequencesAtSeara');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'resetHumanSequencesAtSeara'), '0','0', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'resetHumanSequencesAtSeara') ;

insert into keys ( label ) values ( 'raportNoteFirstColumnFormat');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'raportNoteFirstColumnFormat'), '#{orderdetails},#{user},#{total}','#{orderdetails},#{user},#{total}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'raportNoteFirstColumnFormat') ;

update configs set value = '2016761' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'raportMonetarFirstColumnFormat');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'raportMonetarFirstColumnFormat'), '#{orderid},#{user},#{total}','#{orderid},#{user},#{total}', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'raportMonetarFirstColumnFormat') ;

update configs set value = '2016762' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016763' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016765' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016766' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016767' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016768' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016769' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'barcodeLongFormatSizes');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'barcodeLongFormatSizes'), '13,8','13,8', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'barcodeLongFormatSizes') ;

update configs set value = '2016771' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016772' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016773' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016774' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016775' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'bonMarcajPrintDetails');
 insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonMarcajPrintDetails'), '1','1', 1);
 insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
  and id_key = (select min(id) from keys where keys.label = 'bonMarcajPrintDetails') ;


  insert into keys ( label ) values ( 'bonMarcajPrintSumar');
   insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonMarcajPrintSumar'), '1','1', 1);
   insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'bonMarcajPrintSumar') ;


update configs set value = '2016776' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'contaExportMonetareMapping');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportMonetareMapping'),
   'cash=cash,card=card,online=online,op=op,voucher=voucher','cash=cash,card=card,online=online,op=op,voucher=voucher', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'contaExportMonetareMapping') ;

update configs set value = '2016777' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'contaExportPlatphormaExcludedFromBonConsum');
   insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportPlatphormaExcludedFromBonConsum'),
   '-1','-1', 1);
   insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'contaExportPlatphormaExcludedFromBonConsum') ;

update configs set value = '2016778' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016779' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016780' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016781' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016782' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016783' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016784' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016785' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016786' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'bonFiscalPrintCodFiscalAsComment');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'bonFiscalPrintCodFiscalAsComment'), '0','0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'bonFiscalPrintCodFiscalAsComment') ;

update configs set value = '2016787' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016788' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'productieOrderDispatchedSMSBody');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieOrderDispatchedSMSBody'),
'Comanda Dvs cu nr #{orderid} a fost expediata. Nr Livrator #{phone}. Va multumim! #{businessname}',
'Comanda Dvs cu nr #{orderid} a fost expediata. Nr Livrator #{phone}. Va multumim! #{businessname}', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'productieOrderDispatchedSMSBody') ;


update configs set value = '2016789' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016790' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016791' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016792' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016793' where id_key = ( select min(id) from keys where label = 'currentVersion' );

update configs set value = '2016794' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'livrariDroidIncomingCallAutosearchClient');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'livrariDroidIncomingCallAutosearchClient'),
'1', '1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'livrariDroidIncomingCallAutosearchClient') ;

update configs set value = '2016796' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016797' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016798' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016800' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016801' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016802' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into mobilepos_schema_apikeys(schema_name, apikey) values ( 'public','elqy2d#');

-- atentie , numele locatiei ( CODUL , adica prima denumire a locatiei ) , trebuie sa conincida cu numele dat locatiei in mobilePOS !! )

update configs set value = '2016803' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016804' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table calendar add column avans float8 not null default 0;

alter table calendar add column stornat_avans_bf boolean not null default false;

alter table calendar add column stornat_avans boolean not null default false;

update configs set value = '2016805' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016806' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016807' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016808' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016810' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'incarcaContAllowForgetAnteriorDebt');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'incarcaContAllowForgetAnteriorDebt'),
'0', '0', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'incarcaContAllowForgetAnteriorDebt') ;


insert into keys ( label ) values ( 'hotelRequestSoldZero');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'hotelRequestSoldZero'),
'1', '1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'hotelRequestSoldZero') ;


update configs set value = '2016811' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016812' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'pda5TipPlataAllowed');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'pda5TipPlataAllowed'),
'', '', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
    and id_key = (select min(id) from keys where keys.label = 'pda5TipPlataAllowed') ;

update configs set value = '2016813' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016814' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016815' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016816' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016817' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016818' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table rest_incasari add column suma_inainte_rest float8;

update rest_incasari set suma_inainte_rest = suma;



update configs set value = '2016819' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016820' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016821' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016822' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016823' where id_key = ( select min(id) from keys where label = 'currentVersion' );


create table stock_momentan_exported_events( id serial unique, id_Stock int not null references stocks(id) on delete cascade, export_date timestamp, qty_exported float8 , exported_ok boolean default false);

update configs set value = '2016824' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016826' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table tip_plata add column request_pos_payment integer not null default 0;

update configs set value = '2016827' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016828' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table droid_sms drop column retrycount;

update configs set value = '2016829' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table categories alter column label set data type text;

alter table subcategories alter column label set data type text;

alter table products alter column label set data type text;

update configs set value = '2016830' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016831' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016832' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016833' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016844' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016845' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016846' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016847' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016848' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016849' where id_key = ( select min(id) from keys where label = 'currentVersion' );

delete from messaging where channel like 'pda_%';

delete from messaging where channel like 'productie_%';

alter table messaging alter column text set data type text;

update configs set value = '2016850' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016851' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016852' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'contaExportUiPathGrupeazaIntrari');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'contaExportUiPathGrupeazaIntrari'), '1','1', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'contaExportUiPathGrupeazaIntrari') ;

update configs set value = '2016853' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016854' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016855' where id_key = ( select min(id) from keys where label = 'currentVersion' );

--GPS_LATITUDE, GPS_LONGITUDE, HOUSE_NO, STREET_NAME, STREET_TYPE, CITY, COUNTRY, COUNTY, BUILDING_NO, BUILDING_TYPE,
--APARTMENT_NO, FLOOR, ENTRANCE, ACCESS_CODE, OLD_UNSTRUCTURED_DATA
--Bl. D15, 1, Bulevardul Burebista, Centrul Civic, Sector 3, Bucharest, 031108, Romania

--#{BUILDING-TYPE} #{BUILDING-NO}, #{HOUSE-NO}, #{STREET-NAME}, #{CITY}, #{COUNTRY}

alter table clienti add column use_new_address_system boolean not null default false;

create table clienti_adrese (
 id serial unique,
 id_client int not null constraint FK_clienti_adrese__clienti references clienti(id) ,
 purpose text not null,
 label text not null,
 body text not null);

create unique index clienti_adrese__unique_index on clienti_adrese ( purpose, label, id_client ) ;

ALTER TABLE clienti_adrese ADD CONSTRAINT unique_purpose_label_client  UNIQUE USING INDEX clienti_adrese__unique_index;

insert into keys ( label ) values ( 'structuredDeliveryAddressToStringFormat');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'structuredDeliveryAddressToStringFormat'),
 '#{BUILDINGTYPE} #{BUILDINGNO}, #{HOUSENO}, #{STREETTYPE} #{STREETNAME}, #{CITY}, #{COUNTRY}','#{BUILDINGTYPE} #{BUILDINGNO}, #{HOUSENO}, #{STREETTYPE} #{STREETNAME}, #{CITY}, #{COUNTRY}', 1);
insert into mapping_config_groups (id_config, id_group) select min(configs.id), min(config_groups.id) from config_groups, configs where config_groups.name = 'all'
 and id_key = (select min(id) from keys where keys.label = 'structuredDeliveryAddressToStringFormat') ;


alter table clienti_adrese add column last_updated timestamp not null default current_timestamp;

alter table clienti_adrese add column crypt text;

update configs set value = '2016856' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2016857' where id_key = ( select min(id) from keys where label = 'currentVersion' );
