-- apply changes
create table incoming_inventory (
  sys_pk                        bigint generated by default as identity not null,
  deleted_on                    timestamp,
  current_approval_level        integer default 0 not null,
  required_approval_levels      integer default 0 not null,
  date                          date,
  vendor_bill_amount            float not null,
  vendor_sys_pk                 bigint,
  deleted                       boolean default false not null,
  version                       integer default 1 not null,
  created_at                    timestamp default 'now()' not null,
  modified_at                   timestamp default 'now()' not null,
  deleted_by                    varchar(255),
  approval_status               varchar(8) default 'APPROVED' not null,
  tags                          varchar[] default '{}' not null,
  comments                      jsonb default '[]' not null,
  tenant_id                     varchar(255) not null,
  mdn                           varchar(255) not null,
  vendor_bil_num                varchar(255) not null,
  vehicle                       varchar(255) not null,
  products                      jsonb,
  created_by                    varchar(255) not null,
  modified_by                   varchar(255) not null,
  constraint ck_incoming_inventory_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')),
  constraint pk_incoming_inventory primary key (sys_pk)
);

create table outgoing_inventory (
  sys_pk                        bigint generated by default as identity not null,
  deleted_on                    timestamp,
  current_approval_level        integer default 0 not null,
  required_approval_levels      integer default 0 not null,
  date                          date,
  deleted                       boolean default false not null,
  version                       integer default 1 not null,
  created_at                    timestamp default 'now()' not null,
  modified_at                   timestamp default 'now()' not null,
  deleted_by                    varchar(255),
  approval_status               varchar(8) default 'APPROVED' not null,
  tags                          varchar[] default '{}' not null,
  comments                      jsonb default '[]' not null,
  tenant_id                     varchar(255) not null,
  mrn                           varchar(255) not null,
  purpose                       varchar(255),
  out_mode                      varchar(7),
  person                        varchar(255),
  vehicle                       varchar(255),
  created_by                    varchar(255) not null,
  modified_by                   varchar(255) not null,
  constraint ck_outgoing_inventory_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')),
  constraint ck_outgoing_inventory_out_mode check ( out_mode in ('PERSON','VEHICLE','ALL')),
  constraint pk_outgoing_inventory primary key (sys_pk)
);

-- apply alter tables
alter table product add column if not exists type varchar(12);
alter table quotation add column if not exists taxes_included boolean;
-- apply post alter
alter table product add constraint ck_product_type check ( type in ('RAW_MATERIAL'));
-- foreign keys and indices
create index ix_incoming_inventory_vendor_sys_pk on incoming_inventory (vendor_sys_pk);
alter table incoming_inventory add constraint fk_incoming_inventory_vendor_sys_pk foreign key (vendor_sys_pk) references vendor (sys_pk) on delete restrict on update restrict;

