-- rename ledgers to accounts and add the is_starred column
CREATE TABLE accounts
(
  id uuid NOT NULL,
  code integer NOT NULL,
  name character varying(255) NOT NULL,
  type integer NOT NULL,
  account_type character varying(50) NOT NULL,
  is_starred boolean NOT NULL,
  is_active boolean NOT NULL,
  is_reconcilable boolean NOT NULL,
  cost_centre_id uuid NOT NULL,
  is_fixture boolean NOT NULL,
  CONSTRAINT accounts_pkey PRIMARY KEY (id),
  CONSTRAINT accounts_cost_centre_id_fkey FOREIGN KEY (cost_centre_id)
      REFERENCES cost_centres ("CostCentreID"),
  CONSTRAINT "accounts_name_key" UNIQUE (name)
);

INSERT INTO accounts(
            id, code, name, type, account_type, is_starred, is_active, 
            is_reconcilable, cost_centre_id, is_fixture)
	SELECT "LedgerID", "Code", "Name", "Type", ledger_type, FALSE, "IsActive", 
            "IsReconcilable", "CostCentreID", "IsFixture"
  FROM ledgers;

ALTER TABLE employees RENAME COLUMN "LedgerID" TO id;
ALTER TABLE employees DROP CONSTRAINT "entities_employees_LedgerID_fkey";
ALTER TABLE employees ADD CONSTRAINT "employees_id_fkey" FOREIGN KEY (id)
	REFERENCES accounts (id);


ALTER TABLE journals RENAME COLUMN "LedgerID" TO account_id;
ALTER TABLE journals DROP CONSTRAINT "entities_journals_LedgerID_fkey";
ALTER TABLE journals ADD CONSTRAINT "journals_account_id_fkey" FOREIGN KEY (account_id)
	REFERENCES accounts (id);

ALTER TABLE products RENAME COLUMN "LedgerID" TO account_id;
ALTER TABLE products DROP CONSTRAINT "products_LedgerID_fkey";
ALTER TABLE products ADD CONSTRAINT "products_account_id_fkey" FOREIGN KEY (account_id)
	REFERENCES accounts (id);

DROP TABLE ledgers;


ALTER TABLE vouchers RENAME TO vouchers_old;

CREATE TABLE vouchers
(
  "VoucherID" uuid NOT NULL,
  date timestamp without time zone NOT NULL,
  narration character varying(1000) NOT NULL,
  creation_date timestamp with time zone NOT NULL,
  last_edit_date timestamp with time zone NOT NULL,
  voucher_type integer NOT NULL,
  is_posted boolean NOT NULL,
  poster_id uuid,
  user_id uuid NOT NULL,
  is_reconciled boolean NOT NULL,
  reconcile_date timestamp without time zone NOT NULL,
  is_starred boolean NOT NULL,
  CONSTRAINT vouchers_pkey PRIMARY KEY ("VoucherID"),
  CONSTRAINT vouchers_poster_id_fkey FOREIGN KEY (poster_id) REFERENCES public.auth_users ("UserID"),
  CONSTRAINT vouchers_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.auth_users ("UserID")
);

INSERT INTO vouchers(
            "VoucherID", date, reconcile_date, narration, creation_date, last_edit_date, 
			voucher_type, is_posted, poster_id, user_id, is_reconciled, is_starred)
	SELECT "VoucherID", "Date", "ReconcileDate", "Narration", "CreationDate", "LastEditDate", 
			"VoucherType", "Posted", "PosterID", "UserID", "IsReconciled", FALSE
  FROM vouchers_old;
  
ALTER TABLE inventories DROP CONSTRAINT "entities_inventories_VoucherID_fkey";
ALTER TABLE inventories ADD CONSTRAINT "inventories_VoucherID_fkey" FOREIGN KEY ("VoucherID")
	REFERENCES vouchers ("VoucherID");

ALTER TABLE journals DROP CONSTRAINT "entities_journals_VoucherID_fkey";
ALTER TABLE journals ADD CONSTRAINT "journals_VoucherID_fkey" FOREIGN KEY ("VoucherID")
	REFERENCES vouchers ("VoucherID");
	
ALTER TABLE salary_deductions DROP CONSTRAINT "entities_salarydeductions_VoucherID_fkey";
ALTER TABLE salary_deductions ADD CONSTRAINT "salary_deductions_VoucherID_fkey" FOREIGN KEY ("VoucherID")
	REFERENCES vouchers ("VoucherID");

ALTER TABLE service_charges DROP CONSTRAINT service_charges_voucher_id_fkey;
ALTER TABLE service_charges ADD CONSTRAINT service_charges_voucher_id_fkey FOREIGN KEY (voucher_id)
	REFERENCES vouchers ("VoucherID");

DROP TABLE vouchers_old;