insert into keys ( label ) values ( 'printer_items_etichete_lot');
insert into messages ( id_lang, id_key, label ) values ( 1, (select min(id) from keys where keys.label = 'printer_items_etichete_lot'), 'Etichete LOT');
insert into printer_items ( id_key ) values ( (select min(id) from keys where keys.label = 'printer_items_etichete_lot') );


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


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


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

create table loturi(
    id serial unique,
    lot_codbare text not null,
    lot_nr text not null,
    lot_expirare timestamp not null);

create index loturi__lotnr on loturi(lot_nr);
create index loturi__expirare on loturi(lot_expirare);
create index loturi__lotcodbare on loturi(lot_codbare);


alter table factura_entries add column id_lot int constraint FK_factura_entries__loturi references loturi(id) ;

alter table facturi_emise_items add column id_lot int constraint FK_facturi_emise_items__loturi references loturi(id) ;

alter table transfer_items add column id_lot int constraint FK_transfer_items__loturi references loturi(id) ;

alter table bonuri_consum_items add column id_lot int constraint FK_bonuri_consum_items__loturi references loturi(id) ;

alter table corectii_items add column id_lot int constraint FK_corectii_items__loturi references loturi(id) ;

alter table sales add column id_lot int constraint FK_sales__loturi references loturi(id) ;

alter table fabricare_items add column id_lot int constraint FK_fabricare_items__loturi references loturi(id) ;

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

alter table loturi add column id_furnizor int not null constraint FK_loturi__clienti references clienti(id) ;

alter table loturi add column lot_receptie timestamp not null;

alter table loturi add column id_stock int not null constraint FK_loturi__stocks references stocks(id) ;

alter table fisa_magazie add column id_lot int constraint FK_fisa_magazie__loturi references loturi(id) ;

alter table magazii add column req_vz_lot boolean not null default false;


drop view fise_active;

create view fise_active as
    select
    fisa_magazie.id as id_fisa,
    fisa_magazie.time_document as add_time,
    fisa_magazie.type_document as type_document,
    id_stock,
    id_magazie,
    id_lot,
    fisa_magazie.buy_price as buy_price,
    fisa_magazie.cod_bare as cod_bare,
    fisa_magazie.denumire_la_furnizor as denumire_la_furnizor,
     intrata
        - coalesce(( select sum(quantity) from stock_consum where id_fisa_sursa = fisa_magazie.id ) , 0)
      as disponibil
     from fisa_magazie
    where intrata > 0;


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

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

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

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

update configs set value = '2017006' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017007' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017008' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017009' where id_key = ( select min(id) from keys where label = 'currentVersion' );


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

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

alter table calendar add column avans double precision not null default 0;

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

update configs set value = '2017012' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017013' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017014' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017015' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017016' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017017' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017018' where id_key = ( select min(id) from keys where label = 'currentVersion' );

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

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

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


update configs set value = '2017021' 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 = '2017022' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017023' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017024' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017025' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017026' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017027' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017028' where id_key = ( select min(id) from keys where label = 'currentVersion' );
update configs set value = '2017029' where id_key = ( select min(id) from keys where label = 'currentVersion' );


insert into keys ( label ) values ('gui_articole_report');
insert into messages ( id_lang, id_key, label ) values ( 1, (select min(id) from keys where keys.label = 'gui_articole_report'), 'Articole');
insert into gui_panels ( id_key, id_parent, classname ) values ( (select min(id) from keys where keys.label = 'gui_articole_report'), (select id from gui_panels where id_parent = -1), 'ro.atreides.restaurant.gui.admin.articole.ArticoleReport');


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