-- drop dependencies
alter table document drop constraint if exists ck_document_type_of_doc;
alter table product drop constraint if exists ck_product_uom;
-- apply changes
create table req_for_quote (
  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,
  potential_vendors             bigint[],
  open_till                     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,
  status                        varchar(9),
  products                      jsonb,
  req_for_quote_num             varchar(255),
  created_by                    varchar(255) not null,
  modified_by                   varchar(255) not null,
  constraint ck_req_for_quote_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')),
  constraint ck_req_for_quote_status check ( status in ('DELIVERED','PO','QUOTE','CANCELLED')),
  constraint pk_req_for_quote primary key (sys_pk)
);

-- apply alter tables
alter table document alter column type_of_doc type varchar(7) using type_of_doc::varchar(7);
alter table document alter column type_of_doc drop not null;
alter table document add column if not exists ref_id varchar(255);
alter table document add column if not exists doc_date date;
alter table product add column if not exists id bigint;
alter table purchase_order alter column reference_quotation drop not null;
alter table purchase_order alter column total_amount type float using total_amount::float;
alter table quotation alter column total_amount type float using total_amount::float;
alter table quotation add column if not exists req_for_quote_num varchar(255);
-- apply post alter
alter table document add constraint ck_document_type_of_doc check ( type_of_doc in ('PO','QUOTE','INVOICE','ALL'));
alter table product add constraint ck_product_uom check ( uom in ('NOS','LTR','MTR','ALL'));
