-----------------------PharmacySaleHeader--------------------
--------------------------------------------------------------------
alter table "PharmacySaleHeader" drop column if exists "PayTypeId";
 alter table "PharmacySaleHeader"
 add column if not exists "PayTypeId" integer references "PayType"("PayTypeId");
 ---------------------------
alter table "PharmacySaleHeader" drop column if exists "PaymentNumber";
 alter table "PharmacySaleHeader"
 add column if not exists "PaymentNumber" character varying(50)
--------------------
 update "PharmacySaleHeader" set "PaidVia" = null where "PaidVia"= ''
-----------------------------------------------------

with T as(
 select PSH."PharmacySaleHeaderId",PT."PayTypeId" from "PharmacySaleHeader" PSH
  join "PayType" PT on lower(PSH."PaidVia") = lower(PT."PayTypeValue")
) 
update "PharmacySaleHeader" P set "PayTypeId" = T."PayTypeId" from T where P."PharmacySaleHeaderId" = T."PharmacySaleHeaderId";
--------------------------------------------------------------------------
--------------------------------------------------------
-----------------------------LAbBookingHeader----------------------------------
---------------------------------------------

alter table "LabBookingHeader" drop column if exists "PayTypeId";
 alter table "LabBookingHeader"
 add column if not exists "PayTypeId" integer references "PayType"("PayTypeId");
 ---------------------------
alter table "LabBookingHeader" drop column if exists "PaymentNumber";
 alter table "LabBookingHeader"
 add column if not exists "PaymentNumber" character varying(50)
-------------------------------------------------------
 with T as(
 select LBH."LabBookingHeaderId",PT."PayTypeId" from "LabBookingHeader" LBH
  join "PayType" PT on lower(LBH."PaidVia") = lower(PT."PayTypeValue")
) 
update "LabBookingHeader" L set "PayTypeId" = T."PayTypeId" from T where L."LabBookingHeaderId" = T."LabBookingHeaderId";
-------------------------
 
