delete from current_version;

insert into current_Version ( major_version ) values ( '2024');

create table produse_garantie ( id serial unique, id_parent int not null references products(id), id_child int not null references products(id) );


insert into keys ( label ) values ( 'locatiiReturo');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'locatiiReturo'), '','', 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 = 'locatiiReturo') ;



update configs set value = '2024001' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024003' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024004' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024005' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024006' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024008' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024009' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024010' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024011' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024012' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'productieQuickRefreshSeconds');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieQuickRefreshSeconds'), '15','15', 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 = 'productieQuickRefreshSeconds') ;




update configs set value = '2024013' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024014' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024015' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024016' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024017' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024018' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024019' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024021' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024022' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024023' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024024' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024025' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024026' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024027' where id_key = ( select min(id) from keys where label = 'currentVersion' );

insert into keys ( label ) values ( 'transferCuAvizSerie');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'transferCuAvizSerie'), 'AVIZ','AVIZ', 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 = 'transferCuAvizSerie') ;

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

update messages set label = 'Facturare-Avize' where id_key in ( select id_key from gui_panels where classname = 'ro.atreides.facturi.gui.FacturiPanel' );

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

alter table orders alter column reducere_valorica set data type float8;

update configs set value = '2024032' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024033' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024034' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024036' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024037' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024039' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024040' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024041' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024042' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024044' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024045' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024046' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024047' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'returoTipPlata');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'returoTipPlata'), '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 = 'returoTipPlata') ;


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

insert into keys ( label ) values ( 'printer_items_voucher_returo');
insert into messages ( id_lang, id_key, label ) values ( 1, (select min(id) from keys where keys.label = 'printer_items_voucher_returo'), 'Voucher Returo');
insert into printer_items (id_key) (select min(id) from keys where keys.label = 'printer_items_voucher_returo');

create table vouchers(
    id serial unique,
    unique_id uuid NOT NULL,
    expiry timestamp not null,
    issued timestamp not null default current_timestamp,
    value float4 not null,
    used timestamp,
    barcode text not null);

create index vouchers__uniqueid on vouchers(unique_id);
create index vouchers__barcode on vouchers(barcode);

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

insert into keys ( label ) values ( 'productieQuickOrderFontSize');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'productieQuickOrderFontSize'), '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 = 'productieQuickOrderFontSize') ;


update configs set value = '2024053' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024054' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024055' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024057' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024060' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024061' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024062' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024063' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024065' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024066' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024067' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024068' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024069' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024070' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024071' where id_key = ( select min(id) from keys where label = 'currentVersion' );


create table if not exists global_event (
    id serial unique,
    created_at timestamp not null,
    topic text,
    operation_type text default 'REGULAR',
    filters jsonb,
    payload jsonb
);

create index IDX_global_event__created_at on global_event(created_at);
create index IDX_global_event__topic on global_event(topic);
create index IDX_global_event__operation_type on global_event(operation_type);


alter table niruri add column have_nrcrt_set boolean default false;

alter table nir_entries add column nr_crt int ;
alter table nir_entries add column parent_nr_crt int ;
alter table nir_entries add column is_child_line boolean default false;
alter table nir_entries add column is_correction_child_line boolean default false;

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


alter table user_log alter column text set data type text;

update configs set value = '2024074' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024075' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024076' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024077' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024078' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024082' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024083' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024085' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024086' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024087' where id_key = ( select min(id) from keys where label = 'currentVersion' );

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

update configs set value = '2024088' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024089' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024090' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024091' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024092' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024093' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024094' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ( 'separateSalesAtBonFiscal');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'separateSalesAtBonFiscal'), '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 = 'separateSalesAtBonFiscal') ;

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

insert into keys ( label ) values ( 'eFacturaAllowIgnoreOption');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'eFacturaAllowIgnoreOption'), '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 = 'eFacturaAllowIgnoreOption') ;

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


insert into keys ( label ) values ( 'eFacturaPunctLucru');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'eFacturaPunctLucru'), '','', 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 = 'eFacturaPunctLucru') ;


insert into keys ( label ) values ( 'eFacturaEmail');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'eFacturaEmail'), '','', 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 = 'eFacturaEmail') ;

insert into keys ( label ) values ( 'eFacturaName');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'eFacturaName'), '','', 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 = 'eFacturaName') ;

insert into keys ( label ) values ( 'eFacturaPhone');
insert into configs ( id_key , value, default_value, visibility ) values ((select min(id) from keys where keys.label = 'eFacturaPhone'), '','', 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 = 'eFacturaPhone') ;




update configs set value = '2024101' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024102' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024103' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024104' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024105' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024106' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024107' where id_key = ( select min(id) from keys where label = 'currentVersion' );

alter table external_codes add column if not exists external_unit text not null default '';
alter table external_codes add column if not exists local_unit text not null default '';
alter table external_codes add column if not exists ratio float8 not null default 1;
alter table external_codes add column if not exists magazie_preferred int ;

alter table nir_entries add column if not exists barcode text not null default '';

update configs set value = '2024108' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024109' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024110' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024111' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2024112' where id_key = ( select min(id) from keys where label = 'currentVersion' );


alter table external_codes drop constraint external_codes__upsert;

alter table external_codes add constraint external_codes__unique unique ( local_resource, resource_type, external_source, external_unit ) ;

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


alter table facturi_emise add column customer_specific_headers jsonb not null default '{}';

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

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


alter table magazii add column if not exists centru_cost text not null default '';
alter table magazii add column if not exists centru_profit text not null default '';

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