Advertisement
Guest User

Untitled

a guest
Jul 8th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create table items (
  2.   code varchar(255),
  3.   integration_ref varchar(255),
  4.   classification varchar(255),
  5.   description varchar(255),
  6.   uom varchar(255),
  7.   itemtag varchar(255),
  8.   usage_type varchar(255),
  9.   original_price numeric(16,6),
  10.   suite numeric(16,6),
  11.   private numeric(16,6),
  12.   semi_private numeric(16,6),
  13.   ward numeric(16,6),
  14.   opd numeric(16,6),
  15.   charity numeric(16,6),
  16.   mdrptag varchar(255),
  17.   accounting numeric,
  18.   admitting numeric,
  19.   cathlab numeric,
  20.   rehab numeric,
  21.   ccu numeric,
  22.   csr numeric,
  23.   charityob numeric,
  24.   ctscan numeric,
  25.   cvor numeric,
  26.   cvrr numeric,
  27.   dr numeric,
  28.   dental numeric,
  29.   rehabmed numeric,
  30.   diet numeric,
  31.   endo numeric,
  32.   echo numeric,
  33.   eeg numeric,
  34.   er numeric,
  35.   eye numeric,
  36.   hearing numeric,
  37.   heart numeric,
  38.   satheart numeric,
  39.   hema numeric,
  40.   hemo numeric,
  41.   histo numeric,
  42.   icu numeric,
  43.   lab numeric,
  44.   satlab numeric,
  45.   mammo numeric,
  46.   nicu numeric,
  47.   newborn numeric,
  48.   nucmed numeric,
  49.   operating numeric,
  50.   picu numeric,
  51.   pmc numeric,
  52.   pacu numeric,
  53.   pulmo numeric,
  54.   radio numeric,
  55.   satxray numeric,
  56.   diabetes numeric,
  57.   radonco numeric,
  58.   tele numeric,
  59.   ultra numeric,
  60.   uro numeric,
  61.   wound numeric,
  62.   charity_reader numeric,
  63.   semi_private_reader numeric,
  64.   private_reader numeric,
  65.   suite_reader numeric,
  66.   ward_reader numeric,
  67.   opd_reader numeric
  68. );
  69.  
  70.  
  71. alter table items add ref_service_id bigint;
  72. insert into ref_service (id, active, classification) values (0, true, '');
  73.  
  74. create or replace function pop_ref_service(classification text, description text, uom text, merxcode text, sapcode text, itemtag text, usage_type text, mdrptag text)
  75. returns void as $$
  76. declare
  77. max_id bigint;
  78. begin
  79. select max(id) into max_id from ref_service;
  80. insert into ref_service (id, classification, created_by, created_date, updated_date, active, barcode, code, description, brand_name, uom, item_type, usage_type, integration_ref, doh)
  81. values (case when max_id is null then 0 else max_id + 1 end, CASE WHEN classification = 'CONSIGNED' THEN 'MISCELLANEOUS' ELSE classification END, 'IMPORT_SCRIPT', current_date, current_date, true, '1', merxcode, description, case when classification = 'DRUG' then description else null end, uom, case when classification = 'SERVICE_EQUIPMENT' AND usage_type = 'PER_USE' then 'EQUIPMENT' when classification = 'SERVICE_EQUIPMENT' AND (usage_type is null OR usage_type = '') then 'SERVICE' else null end, usage_type, sapcode, case when mdrptag = 'TRUE' then true else null end);
  82. raise notice 'Inserted: %', merxcode;
  83. select max(id) into max_id from ref_service;
  84. update items set ref_service_id = max_id where code = merxcode;
  85. IF itemtag is not null AND itemtag != '' AND itemtag != '-' THEN
  86.   insert into ref_service_tag (ref_service_id, ref_item_tag) values (max_id, itemtag);
  87. END IF;
  88. end;
  89. $$ language plpgsql;
  90.  
  91. select pop_ref_service(classification, description, uom, code, integration_ref, itemtag, usage_type, mdrptag) from items;
  92.  
  93. create or replace function pop_ref_price(
  94. item_id bigint,
  95. cost_p numeric,
  96. suite_p numeric,
  97. private_p numeric,
  98. semi_private_p numeric,
  99. ward_p numeric,
  100. charity_rate_p numeric,
  101. opd_p numeric,
  102. charity_reader numeric,
  103. semi_private_reader numeric,
  104. private_reader numeric,
  105. suite_reader numeric,
  106. ward_reader numeric,
  107. opd_reader numeric,
  108. item_class text
  109. ) returns void as $$
  110. declare
  111. max_id bigint;
  112. begin
  113.  
  114. select max(id) into max_id from ref_price;
  115. insert into ref_price (id, created_by, created_date, updated_date, active, price, billing_category_id, effective_dt, original_price, version, ref_service_id, readers_fee)
  116. values
  117. (max_id + 1, 'IMPORT_SCRIPT', current_date, current_date, true, opd_p, 20, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  118. cost_p ELSE opd_p END, 1, item_id, null),
  119. (max_id + 2, 'IMPORT_SCRIPT', current_date, current_date, true, opd_p, 19, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  120. cost_p ELSE opd_p END, 1, item_id, null),
  121. (max_id + 3, 'IMPORT_SCRIPT', current_date, current_date, true, ward_p, 1, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  122. cost_p ELSE ward_p END, 1, item_id, ward_reader),
  123. (max_id + 4, 'IMPORT_SCRIPT', current_date, current_date, true, ward_p, 2, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  124. cost_p ELSE ward_p END, 1, item_id, ward_reader),
  125. (max_id + 5, 'IMPORT_SCRIPT', current_date, current_date, true, suite_p, 3, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  126. cost_p ELSE suite_p END, 1, item_id, suite_reader),
  127. (max_id + 6, 'IMPORT_SCRIPT', current_date, current_date, true, suite_p, 4, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  128. cost_p ELSE suite_p END, 1, item_id, suite_reader),
  129. (max_id + 7, 'IMPORT_SCRIPT', current_date, current_date, true, private_p, 5, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  130. cost_p ELSE private_p END, 1, item_id, private_reader),
  131. (max_id + 8, 'IMPORT_SCRIPT', current_date, current_date, true, private_p, 6, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  132. cost_p ELSE private_p END, 1, item_id, private_reader),
  133. (max_id + 9, 'IMPORT_SCRIPT', current_date, current_date, true, semi_private_p, 7, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  134. cost_p ELSE semi_private_p END, 1, item_id, semi_private_reader),
  135. (max_id + 10, 'IMPORT_SCRIPT', current_date, current_date, true, semi_private_p, 8, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  136. cost_p ELSE semi_private_p END, 1, item_id, semi_private_reader),
  137. (max_id + 11, 'IMPORT_SCRIPT', current_date, current_date, true, charity_rate_p, 9, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  138. cost_p ELSE charity_rate_p END, 1, item_id, charity_reader),
  139. (max_id + 12, 'IMPORT_SCRIPT', current_date, current_date, true, charity_rate_p, 10, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  140. cost_p ELSE charity_rate_p END, 1, item_id, charity_reader),
  141. (max_id + 13, 'IMPORT_SCRIPT', current_date, current_date, true, opd_p, 11, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  142. cost_p ELSE opd_p END, 1, item_id, opd_reader),
  143. (max_id + 14, 'IMPORT_SCRIPT', current_date, current_date, true, opd_p, 12, current_date, CASE WHEN item_class = 'DRUG' or item_class = 'NON_DRUG' or item_class = 'CONSIGNED' THEN
  144. cost_p ELSE opd_p END, 1, item_id, opd_reader);
  145. end;
  146. $$ language plpgsql;
  147.  
  148. insert into ref_price (id, active) values (0, true);
  149. select pop_ref_price(ref_service_id, original_price, suite, private, semi_private, ward, charity, opd, charity_reader, semi_private_reader, private_reader, suite_reader, ward_reader, opd_reader, classification) from items;
  150.  
  151. create or replace function pop_ref_service_inventory_item(ref_service_i numeric, department numeric) returns void as $$
  152. declare
  153.     max_id numeric;
  154.     count numeric;
  155.     loc_code text;
  156.     item_code text;
  157. begin
  158. select count(*) into count from ref_service_item where department_id = department AND ref_service_id = ref_service_i;
  159. IF count = 0 THEN
  160.     select max(id) into max_id from ref_service_item;
  161.     select code into loc_code from ref_department where id = department;
  162.     select code into item_code from ref_service where id = ref_service_i;
  163.     insert into ref_service_item (id, created_date, created_by, updated_date, updated_by, active, department_id, ref_service_id) values (max_id + 1, current_date, 'IMPORT_SCRIPT', current_date, 'IMPORT_SCRIPT', true, department, ref_service_i);
  164.     raise notice 'Inserted itemcode:% department code:%', item_code, loc_code;
  165. END IF;
  166. end;
  167. $$ language plpgsql;
  168.  
  169.  
  170. insert into ref_service_item (id,active) values (0, true);
  171. select pop_ref_service_inventory_item(ref_service_id, 47) from items where accounting > 0; 6
  172. select pop_ref_service_inventory_item(ref_service_id, 58) from items where admitting > 0; 45
  173. select pop_ref_service_inventory_item(ref_service_id, 72) from items where cathlab > 0; 1467
  174. select pop_ref_service_inventory_item(ref_service_id, 50) from items where rehab > 0; 71
  175. select pop_ref_service_inventory_item(ref_service_id, 15) from items where ccu > 0; 33
  176. select pop_ref_service_inventory_item(ref_service_id, 32) from items where csr > 0; 236
  177.  
  178. select pop_ref_service_inventory_item(ref_service_id, 105) from items where charityob > 0; 2
  179. select pop_ref_service_inventory_item(ref_service_id, 73) from items where ctscan > 0; 291
  180. select pop_ref_service_inventory_item(ref_service_id, 21) from items where cvor > 0; 1130
  181. select pop_ref_service_inventory_item(ref_service_id, 22) from items where cvrr > 0; 180
  182. select pop_ref_service_inventory_item(ref_service_id, 23) from items where dr > 0; 261
  183. select pop_ref_service_inventory_item(ref_service_id, 1253) from items where dental > 0; 4
  184.  
  185. select pop_ref_service_inventory_item(ref_service_id, 68) from items where rehabmed > 0; 4
  186. select pop_ref_service_inventory_item(ref_service_id, 53) from items where diet > 0; 52
  187. select pop_ref_service_inventory_item(ref_service_id, 74) from items where endo > 0; 249
  188. select pop_ref_service_inventory_item(ref_service_id, 13) from items where echo > 0; 57
  189. select pop_ref_service_inventory_item(ref_service_id, 54) from items where eeg > 0; 45
  190.  
  191. select code, description from ref_department where id in (68,53,74,13,54);
  192.  
  193. select pop_ref_service_inventory_item(ref_service_id, 39) from items where er > 0; 493
  194. select pop_ref_service_inventory_item(ref_service_id, 51) from items where eye > 0; 113
  195. select pop_ref_service_inventory_item(ref_service_id, 55) from items where hearing > 0; 1
  196.  
  197. select code, description from ref_department where id in (39, 51, 55);
  198.  
  199. select pop_ref_service_inventory_item(ref_service_id, 56) from items where heart > 0; 17
  200. select pop_ref_service_inventory_item(ref_service_id, 56) from items where satheart > 0;
  201.  
  202. select code, description from ref_department where id in (56);
  203.  
  204. select pop_ref_service_inventory_item(ref_service_id, 25) from items where hema > 0;  211
  205. select pop_ref_service_inventory_item(ref_service_id, 26) from items where hemo > 0; 139
  206.  
  207. select code, description from ref_department where id in (25,26);
  208.  
  209. select pop_ref_service_inventory_item(ref_service_id, 1152) from items where histo > 0; 26
  210. select pop_ref_service_inventory_item(ref_service_id, 27) from items where icu > 0; 25
  211.  
  212. select code, description from ref_department where id in (1152, 27);
  213.  
  214. select pop_ref_service_inventory_item(ref_service_id, 59) from items where lab > 0; 390
  215. select pop_ref_service_inventory_item(ref_service_id, 59) from items where satlab > 0;
  216.  
  217. select code, description from ref_department where id in (59);
  218.  
  219. select pop_ref_service_inventory_item(ref_service_id, 1252) from items where mammo > 0; 18
  220. select pop_ref_service_inventory_item(ref_service_id, 29) from items where nicu > 0; 24
  221. select pop_ref_service_inventory_item(ref_service_id, 31) from items where newborn > 0; 106
  222. select pop_ref_service_inventory_item(ref_service_id, 62) from items where nucmed > 0; 207
  223. select pop_ref_service_inventory_item(ref_service_id, 35) from items where operating > 0; 2370
  224. select pop_ref_service_inventory_item(ref_service_id, 37) from items where picu > 0; 195
  225.  
  226. select code, description from ref_department where id in (1252, 29, 31, 62, 35, 37);
  227.  
  228. select pop_ref_service_inventory_item(ref_service_id, 63) from items where pmc > 0; 2441
  229. select pop_ref_service_inventory_item(ref_service_id, 36) from items where pacu > 0; 194
  230. select pop_ref_service_inventory_item(ref_service_id, 76) from items where pulmo > 0; 259
  231. select pop_ref_service_inventory_item(ref_service_id, 71) from items where radio > 0; 137
  232. select pop_ref_service_inventory_item(ref_service_id, 71) from items where satxray > 0;
  233.  
  234. select code, description from ref_department where id in (63, 36, 76, 71);
  235.  
  236. select pop_ref_service_inventory_item(ref_service_id, 52) from items where diabetes > 0; 18
  237. select pop_ref_service_inventory_item(ref_service_id, 60) from items where radonco > 0; 27
  238. select pop_ref_service_inventory_item(ref_service_id, 20) from items where tele > 0;  197
  239. select pop_ref_service_inventory_item(ref_service_id, 70) from items where ultra > 0; 247
  240. select pop_ref_service_inventory_item(ref_service_id, 75) from items where uro > 0; 151
  241. select pop_ref_service_inventory_item(ref_service_id, 86) from items where wound > 0; 96
  242.  
  243. select code, description from ref_department where id in (52, 60, 20, 70, 75, 86);
  244.  
  245. create or replace function pop_ref_inventory_item(service_item_id numeric) returns void as $$
  246. begin
  247.     insert into ref_inventory_item (id, on_hand_qty) values (service_item_id, 10000);
  248. end;
  249. $$ language plpgsql;
  250.  
  251.  
  252. create table pkg_items(
  253.     code text,
  254.     package_loc text,
  255.     description text,
  256.     classification text,
  257.     cost_price numeric,
  258.     ward numeric,
  259.     suite numeric,
  260.     private numeric,
  261.     semi_private numeric,
  262.     opd numeric,
  263.     charity numeric
  264. );
  265.  
  266. create or replace function pop_ref_service_pkg_order_set(
  267. item_code text,
  268. pkg_loc text,
  269. description text,
  270. classification text,
  271. cost_p numeric,
  272. ward_p numeric,
  273. suite_p numeric,
  274. private_p numeric,
  275. semi_private_p numeric,
  276. opd_p numeric,
  277. charity_p numeric
  278. ) returns void as $$
  279. declare
  280.     item_id bigint;
  281.     dep_id bigint;
  282.     max_price_id numeric;
  283. begin
  284.  
  285. perform pop_ref_service(classification, description, null, item_code, null, null, null, null);
  286. select id into item_id from ref_service where code = item_code;
  287. select max(id) into max_price_id from ref_price;
  288.  
  289. insert into ref_price (id, created_by, created_date, updated_date, active, price, billing_category_id, effective_dt, original_price, version, ref_service_id, readers_fee)
  290. values
  291. (max_price_id + 1, 'IMPORT_SCRIPT', current_date, current_date, true, CASE WHEN classification = 'ORDER_SET' THEN cost_p ELSE opd_p END, 20, current_date, CASE WHEN classification = 'ORDER_SET' THEN cost_p ELSE opd_p END, 1, item_id, null),
  292. (max_price_id + 2, 'IMPORT_SCRIPT', current_date, current_date, true, CASE WHEN classification = 'ORDER_SET' THEN cost_p ELSE opd_p END, 19, current_date, CASE WHEN classification = 'ORDER_SET' THEN cost_p ELSE opd_p END, 1, item_id, null),
  293. (max_price_id + 3, 'IMPORT_SCRIPT', current_date, current_date, true, ward_p, 1, current_date, ward_p, 1, item_id, null),
  294. (max_price_id + 4, 'IMPORT_SCRIPT', current_date, current_date, true, ward_p, 2, current_date, ward_p, 1, item_id, null),
  295. (max_price_id + 5, 'IMPORT_SCRIPT', current_date, current_date, true, suite_p, 3, current_date, suite_p, 1, item_id, null),
  296. (max_price_id + 6, 'IMPORT_SCRIPT', current_date, current_date, true, suite_p, 4, current_date, suite_p, 1, item_id, null),
  297. (max_price_id + 7, 'IMPORT_SCRIPT', current_date, current_date, true, private_p, 5, current_date, private_p, 1, item_id, null),
  298. (max_price_id + 8, 'IMPORT_SCRIPT', current_date, current_date, true, private_p, 6, current_date, private_p, 1, item_id, null),
  299. (max_price_id + 9, 'IMPORT_SCRIPT', current_date, current_date, true, semi_private_p, 7, current_date, semi_private_p, 1, item_id, null),
  300. (max_price_id + 10, 'IMPORT_SCRIPT', current_date, current_date, true, semi_private_p, 8, current_date, semi_private_p, 1, item_id, null),
  301. (max_price_id + 11, 'IMPORT_SCRIPT', current_date, current_date, true, charity_p, 9, current_date, charity_p, 1, item_id, null),
  302. (max_price_id + 12, 'IMPORT_SCRIPT', current_date, current_date, true, charity_p, 10, current_date, charity_p, 1, item_id, null),
  303. (max_price_id + 13, 'IMPORT_SCRIPT', current_date, current_date, true, opd_p, 11, current_date, opd_p, 1, item_id, null),
  304. (max_price_id + 14, 'IMPORT_SCRIPT', current_date, current_date, true, opd_p, 12, current_date, opd_p, 1, item_id, null);
  305.  
  306. select id into dep_id from ref_department where code = pkg_loc;
  307. IF dep_id is not null THEN
  308.     perform pop_ref_service_inventory_item(item_id, dep_id);
  309. END IF;
  310. end;
  311. $$ language plpgsql;
  312.  
  313. select pop_ref_service_pkg_order_set(code, package_loc, description, classification, cost_price, ward, suite ,private, semi_private, opd, charity) from pkg_items;
  314.  
  315.  
  316. // data for pkg_ref_item_backup in cgh db
  317.  
  318. create table pkg_ref_item_back (
  319. package_code text,
  320. package_loc_code text,
  321. package_loc_desc text,
  322. package_classification text,
  323. package_description text,
  324. package_item_code text,
  325. package_item_description text,
  326. package_item_quantity numeric,
  327. package_item_sap_code text,
  328. department_code text,
  329. department_description text
  330. );
  331.  
  332. create or replace function pop_pkg_ref_item(
  333. pkg_ref_code text,
  334. pkg_ref_class text,
  335. pkg_item_code text,
  336. pkg_item_loc text,
  337. pkg_item_qty numeric
  338. ) returns void as $$
  339. declare
  340.     max_id numeric;
  341.     pkg_id numeric;
  342.     pkg_item_id numeric;
  343.     pkg_item_loc_id numeric;
  344.     pkg_item_service_id numeric;
  345. begin
  346. select id into pkg_id from ref_service where code = pkg_ref_code;
  347. select id into pkg_item_id from ref_service where code = pkg_item_code;
  348. select id into pkg_item_loc_id from ref_department where code = pkg_item_loc;
  349. select id into pkg_item_service_id from ref_service_item where department_id = pkg_item_loc_id and ref_service_id = pkg_item_id;
  350. IF pkg_item_service_id is not null and pkg_id is not null THEN
  351.     select max(id) into max_id from pkg_ref_item;
  352.     insert into pkg_ref_item (id, created_by, created_date, updated_by, updated_date, quantity, pkg_ref_service_id, service_item_id, classification)
  353.         values (max_id + 1, 'IMPORT_SCRIPT', current_date, 'IMPORT_SCRIPT', current_date, pkg_item_qty, pkg_id, pkg_item_service_id, pkg_ref_class);
  354. END IF;
  355. end;
  356. $$ language plpgsql;
  357.  
  358. insert into pkg_ref_item (id) values (0);
  359. select pop_pkg_ref_item(package_code, package_classification, package_item_code, department_code, package_item_quantity) from pkg_ref_item_back;
  360.  
  361. create or replace function clean_package_order_set(r_id numeric, rs_id numeric) returns void as $$
  362. declare
  363.     count numeric;
  364. begin
  365. select count(*) into count from package_order_set where id = rs_id;
  366. IF count != 1 THEN
  367.     delete from package_order_set where row_id = r_id;
  368. END IF;
  369. end;
  370. $$ language plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement