Added DataTables 1.9.0, minus examples and documentation.
[openemr.git] / sql / 4_1_0-to-4_1_1_upgrade.sql
blob747a9a40317a8be0dc1c940e40326ca1ccab9efc
1 --
2 --  Comment Meta Language Constructs:
3 --
4 --  #IfNotTable
5 --    argument: table_name
6 --    behavior: if the table_name does not exist,  the block will be executed
8 --  #IfTable
9 --    argument: table_name
10 --    behavior: if the table_name does exist, the block will be executed
12 --  #IfMissingColumn
13 --    arguments: table_name colname
14 --    behavior:  if the colname in the table_name table does not exist,  the block will be executed
16 --  #IfNotColumnType
17 --    arguments: table_name colname value
18 --    behavior:  If the table table_name does not have a column colname with a data type equal to value, then the block will be executed
20 --  #IfNotRow
21 --    arguments: table_name colname value
22 --    behavior:  If the table table_name does not have a row where colname = value, the block will be executed.
24 --  #IfNotRow2D
25 --    arguments: table_name colname value colname2 value2
26 --    behavior:  If the table table_name does not have a row where colname = value AND colname2 = value2, the block will be executed.
28 --  #IfNotRow3D
29 --    arguments: table_name colname value colname2 value2 colname3 value3
30 --    behavior:  If the table table_name does not have a row where colname = value AND colname2 = value2 AND colname3 = value3, the block will be executed.
32 --  #IfNotRow4D
33 --    arguments: table_name colname value colname2 value2 colname3 value3 colname4 value4
34 --    behavior:  If the table table_name does not have a row where colname = value AND colname2 = value2 AND colname3 = value3 AND colname4 = value4, the block will be executed.
36 --  #IfNotRow2Dx2
37 --    desc:      This is a very specialized function to allow adding items to the list_options table to avoid both redundant option_id and title in each element.
38 --    arguments: table_name colname value colname2 value2 colname3 value3
39 --    behavior:  The block will be executed if both statements below are true:
40 --               1) The table table_name does not have a row where colname = value AND colname2 = value2.
41 --               2) The table table_name does not have a row where colname = value AND colname3 = value3.
43 --  #IfRow2D
44 --    arguments: table_name colname value colname2 value2
45 --    behavior:  If the table table_name does have a row where colname = value AND colname2 = value2, the block will be executed.
47 --  #IfNotIndex
48 --    desc:      This function will allow adding of indexes/keys.
49 --    arguments: table_name colname
50 --    behavior:  If the index does not exist, it will be created
52 --  #EndIf
53 --    all blocks are terminated with and #EndIf statement.
56 #IfNotIndex lists type
57 CREATE INDEX `type` ON `lists` (`type`);
58 #EndIf
60 #IfNotIndex lists pid
61 CREATE INDEX `pid` ON `lists` (`pid`);
62 #EndIf
64 #IfNotIndex form_vitals pid
65 CREATE INDEX `pid` ON `form_vitals` (`pid`);
66 #EndIf
68 #IfNotIndex forms pid
69 CREATE INDEX `pid` ON `forms` (`pid`);
70 #EndIf
72 #IfNotIndex form_encounter pid
73 CREATE INDEX `pid` ON `form_encounter` (`pid`);
74 #EndIf
76 #IfNotIndex immunizations patient_id
77 CREATE INDEX `patient_id` ON `immunizations` (`patient_id`);
78 #EndIf
80 #IfNotIndex procedure_order patient_id
81 CREATE INDEX `patient_id` ON `procedure_order` (`patient_id`);
82 #EndIf
84 #IfNotIndex pnotes pid
85 CREATE INDEX `pid` ON `pnotes` (`pid`);
86 #EndIf
88 #IfNotIndex transactions pid
89 CREATE INDEX `pid` ON `transactions` (`pid`);
90 #EndIf
92 #IfNotIndex extended_log patient_id
93 CREATE INDEX `patient_id` ON `extended_log` (`patient_id`);
94 #EndIf
96 #IfNotIndex prescriptions patient_id
97 CREATE INDEX `patient_id` ON `prescriptions` (`patient_id`);
98 #EndIf
100 #IfMissingColumn version v_realpatch
101 ALTER TABLE `version` ADD COLUMN `v_realpatch` int(11) NOT NULL DEFAULT 0;
102 #EndIf
104 #IfMissingColumn prescriptions drug_info_erx
105 ALTER TABLE `prescriptions` ADD COLUMN `drug_info_erx` TEXT DEFAULT NULL;
106 #EndIf
108 #IfNotRow2D list_options list_id lists option_id nation_notes_replace_buttons
109 INSERT INTO `list_options` (`list_id`, `option_id`, `title`, `seq`) VALUES ('lists','nation_notes_replace_buttons','Nation Notes Replace Buttons',1);
110 INSERT INTO `list_options` (`list_id`, `option_id`, `title`, `seq`) VALUES ('nation_notes_replace_buttons','Yes','Yes',10);
111 INSERT INTO `list_options` (`list_id`, `option_id`, `title`, `seq`) VALUES ('nation_notes_replace_buttons','No','No',20);
112 INSERT INTO `list_options` (`list_id`, `option_id`, `title`, `seq`) VALUES ('nation_notes_replace_buttons','Normal','Normal',30);
113 INSERT INTO `list_options` (`list_id`, `option_id`, `title`, `seq`) VALUES ('nation_notes_replace_buttons','Abnormal','Abnormal',40);
114 #EndIf
116 #IfMissingColumn insurance_data policy_type
117 ALTER TABLE `insurance_data` ADD COLUMN `policy_type` varchar(25) NOT NULL default '';
118 #EndIf
120 #IfMissingColumn drugs max_level
121 ALTER TABLE drugs ADD max_level float NOT NULL DEFAULT 0.0;
122 ALTER TABLE drugs CHANGE reorder_point reorder_point float NOT NULL DEFAULT 0.0;
123 #EndIf
125 #IfNotTable product_warehouse
126 CREATE TABLE `product_warehouse` (
127   `pw_drug_id`   int(11) NOT NULL,
128   `pw_warehouse` varchar(31) NOT NULL,
129   `pw_min_level` float       DEFAULT 0,
130   `pw_max_level` float       DEFAULT 0,
131   PRIMARY KEY  (`pw_drug_id`,`pw_warehouse`)
132 ) ENGINE=MyISAM;
133 #EndIf
135 # Increase size from 5 to 12 to support 4 modifiers with colon separation
136 #IfNotColumnType billing modifier varchar(12)
137    ALTER TABLE `billing` MODIFY `modifier` varchar(12);
138    UPDATE `code_types` SET `ct_mod` = '12' where ct_key = 'CPT4' OR ct_key = 'HCPCS';
139 #Endif
141 #IfMissingColumn billing notecodes
142 ALTER TABLE `billing` ADD `notecodes` varchar(25) NOT NULL default '';
143 #EndIf
147 #IfNotTable dated_reminders
148 CREATE TABLE `dated_reminders` (
149             `dr_id` int(11) NOT NULL AUTO_INCREMENT,
150             `dr_from_ID` int(11) NOT NULL,
151             `dr_message_text` varchar(160) NOT NULL,
152             `dr_message_sent_date` datetime NOT NULL,
153             `dr_message_due_date` date NOT NULL,
154             `pid` int(11) NOT NULL,
155             `message_priority` tinyint(1) NOT NULL,
156             `message_processed` tinyint(1) NOT NULL DEFAULT '0',
157             `processed_date` timestamp NULL DEFAULT NULL,
158             `dr_processed_by` int(11) NOT NULL,
159             PRIMARY KEY (`dr_id`),
160             KEY `dr_from_ID` (`dr_from_ID`,`dr_message_due_date`)
161           ) ENGINE=MyISAM AUTO_INCREMENT=1;
162 #EndIf
164 #IfNotTable dated_reminders_link
165 CREATE TABLE `dated_reminders_link` (
166             `dr_link_id` int(11) NOT NULL AUTO_INCREMENT,
167             `dr_id` int(11) NOT NULL,
168             `to_id` int(11) NOT NULL,
169             PRIMARY KEY (`dr_link_id`),
170             KEY `to_id` (`to_id`),
171             KEY `dr_id` (`dr_id`)
172           ) ENGINE=MyISAM AUTO_INCREMENT=1;
173 #EndIf
175 #IfMissingColumn x12_partners x12_gs03
176 ALTER TABLE `x12_partners` ADD COLUMN `x12_gs03` VARCHAR(15) NOT NULL DEFAULT '';
177 #EndIf
179 #IfNotTable payment_gateway_details
180 CREATE TABLE `payment_gateway_details` (
181   `id` int(11) NOT NULL AUTO_INCREMENT,
182   `service_name` varchar(100) DEFAULT NULL,
183   `login_id` varchar(255) DEFAULT NULL,
184   `transaction_key` varchar(255) DEFAULT NULL,
185   `md5` varchar(255) DEFAULT NULL,
186   PRIMARY KEY (`id`)
187 ) ENGINE=InnoDB;
188 #EndIf
190 #IfNotRow2D list_options list_id lists option_id payment_gateways
191 insert into `list_options` (`list_id`, `option_id`, `title`, `seq`, `is_default`, `option_value`, `mapping`, `notes`) values('lists','payment_gateways','Payment Gateways','297','1','0','','');
192 insert into `list_options` (`list_id`, `option_id`, `title`, `seq`, `is_default`, `option_value`, `mapping`, `notes`) values('payment_gateways','authorize_net','Authorize.net','1','0','0','','');
193 #EndIf
195 #IfNotRow2D list_options list_id payment_method option_id authorize_net
196 insert into `list_options` (`list_id`, `option_id`, `title`, `seq`, `is_default`, `option_value`, `mapping`, `notes`) values('payment_method','authorize_net','Authorize.net','60','0','0','','');
197 #EndIf
199 #IfMissingColumn patient_access_offsite authorize_net_id
200 ALTER TABLE `patient_access_offsite` ADD COLUMN `authorize_net_id` VARCHAR(20) COMMENT 'authorize.net profile id';
201 #EndIf
203 #IfMissingColumn facility website
204 ALTER TABLE `facility` ADD COLUMN `website` varchar(255) default NULL;
205 #EndIf
207 #IfMissingColumn facility email
208 ALTER TABLE `facility` ADD COLUMN `email` varchar(255) default NULL;
209 #EndIf
211 #IfMissingColumn code_types ct_active
212 ALTER TABLE `code_types` ADD COLUMN `ct_active` tinyint(1) NOT NULL default 1 COMMENT '1 if this is active';
213 #EndIf
215 #IfMissingColumn code_types ct_label
216 ALTER TABLE `code_types` ADD COLUMN `ct_label` varchar(31) NOT NULL default '' COMMENT 'label of this code type';
217 UPDATE `code_types` SET ct_label = ct_key;
218 #EndIf
220 #IfMissingColumn code_types ct_external
221 ALTER TABLE `code_types` ADD COLUMN `ct_external` tinyint(1) NOT NULL default 0 COMMENT '0 if stored codes in codes tables, 1 or greater if codes stored in external tables';
222 #EndIf
224 #IfNotRow code_types ct_key DSMIV
225 DROP TABLE IF EXISTS `temp_table_one`;
226 CREATE TABLE `temp_table_one` (
227   `id` int(11) NOT NULL DEFAULT '0',
228   `seq` int(11) NOT NULL DEFAULT '0'
229 ) ENGINE=MyISAM ;
230 INSERT INTO `temp_table_one` (`id`, `seq`) VALUES ( IF( ((SELECT MAX(`ct_id`) FROM `code_types`)>=100), ((SELECT MAX(`ct_id`) FROM `code_types`) + 1), 100 ) , IF( ((SELECT MAX(`ct_seq`) FROM `code_types`)>=100), ((SELECT MAX(`ct_seq`) FROM `code_types`) + 1), 100 )  );
231 INSERT INTO code_types (ct_key, ct_id, ct_seq, ct_mod, ct_just, ct_fee, ct_rel, ct_nofs, ct_diag, ct_active, ct_label, ct_external ) VALUES ('DSMIV' , (SELECT MAX(`id`) FROM `temp_table_one`), (SELECT MAX(`seq`) FROM `temp_table_one`), 2, '', 0, 0, 0, 1, 0, 'DSMIV', 0);
232 DROP TABLE `temp_table_one`;
233 #EndIf
235 #IfNotRow code_types ct_key ICD10
236 DROP TABLE IF EXISTS `temp_table_one`;
237 CREATE TABLE `temp_table_one` (
238   `id` int(11) NOT NULL DEFAULT '0',
239   `seq` int(11) NOT NULL DEFAULT '0'
240 ) ENGINE=MyISAM ;
241 INSERT INTO `temp_table_one` (`id`, `seq`) VALUES ( IF( ((SELECT MAX(`ct_id`) FROM `code_types`)>=100), ((SELECT MAX(`ct_id`) FROM `code_types`) + 1), 100 ) , IF( ((SELECT MAX(`ct_seq`) FROM `code_types`)>=100), ((SELECT MAX(`ct_seq`) FROM `code_types`) + 1), 100 )  );
242 INSERT INTO code_types (ct_key, ct_id, ct_seq, ct_mod, ct_just, ct_fee, ct_rel, ct_nofs, ct_diag, ct_active, ct_label, ct_external ) VALUES ('ICD10' , (SELECT MAX(`id`) FROM `temp_table_one`), (SELECT MAX(`seq`) FROM `temp_table_one`), 2, '', 0, 0, 0, 1, 0, 'ICD10', 1);
243 DROP TABLE `temp_table_one`;
244 #EndIf
246 #IfNotRow code_types ct_key SNOMED
247 DROP TABLE IF EXISTS `temp_table_one`;
248 CREATE TABLE `temp_table_one` (
249   `id` int(11) NOT NULL DEFAULT '0',
250   `seq` int(11) NOT NULL DEFAULT '0'
251 ) ENGINE=MyISAM ;
252 INSERT INTO `temp_table_one` (`id`, `seq`) VALUES ( IF( ((SELECT MAX(`ct_id`) FROM `code_types`)>=100), ((SELECT MAX(`ct_id`) FROM `code_types`) + 1), 100 ) , IF( ((SELECT MAX(`ct_seq`) FROM `code_types`)>=100), ((SELECT MAX(`ct_seq`) FROM `code_types`) + 1), 100 )  );
253 INSERT INTO code_types (ct_key, ct_id, ct_seq, ct_mod, ct_just, ct_fee, ct_rel, ct_nofs, ct_diag, ct_active, ct_label, ct_external ) VALUES ('SNOMED' , (SELECT MAX(`id`) FROM `temp_table_one`), (SELECT MAX(`seq`) FROM `temp_table_one`), 2, '', 0, 0, 0, 1, 0, 'SNOMED', 2);
254 DROP TABLE `temp_table_one`;
255 #EndIf
257 #IfRow2D billing code_type COPAY activity 1
258 DROP TABLE IF EXISTS `temp_table_one`;
259 CREATE TABLE `temp_table_one` (
260   id             int unsigned  NOT NULL AUTO_INCREMENT,
261   session_id     int unsigned  NOT NULL,
262   payer_id       int(11)       NOT NULL DEFAULT 0,
263   user_id        int(11)       NOT NULL,
264   pay_total      decimal(12,2) NOT NULL DEFAULT 0,
265   payment_type varchar( 50 ) NOT NULL DEFAULT 'patient',
266   description text NOT NULL,
267   adjustment_code varchar( 50 ) NOT NULL DEFAULT 'patient_payment',
268   post_to_date date NOT NULL,
269   patient_id int( 11 ) NOT NULL,
270   payment_method varchar( 25 ) NOT NULL DEFAULT 'cash',
271   pid            int(11)       NOT NULL,
272   encounter      int(11)       NOT NULL,
273   code           varchar(9)    NOT NULL,
274   modifier       varchar(5)    NOT NULL DEFAULT '',
275   payer_type     int           NOT NULL DEFAULT 0,
276   post_time      datetime      NOT NULL,
277   post_user      int(11)       NOT NULL,
278   pay_amount     decimal(12,2) NOT NULL DEFAULT 0,
279   account_code varchar(15) NOT NULL DEFAULT 'PCP',
280   PRIMARY KEY (id)
281 ) ENGINE=MyISAM AUTO_INCREMENT=1;
282 INSERT INTO `temp_table_one` (`user_id`, `pay_total`, `patient_id`, `post_to_date`, `pid`, `encounter`, `post_time`, `post_user`, `pay_amount`, `description`) SELECT `user`, (`fee`*-1), `pid`, `date`, `pid`, `encounter`, `date`, `user`, (`fee`*-1), 'COPAY' FROM `billing` WHERE `code_type`='COPAY' AND `activity`!=0;
283 UPDATE `temp_table_one` SET `session_id`= ((SELECT MAX(session_id) FROM ar_session)+`id`);
284 UPDATE `billing`, `code_types`, `temp_table_one` SET temp_table_one.code=billing.code, temp_table_one.modifier=billing.modifier WHERE billing.code_type=code_types.ct_key AND code_types.ct_fee=1 AND temp_table_one.pid=billing.pid AND temp_table_one.encounter=billing.encounter AND billing.activity!=0;
285 INSERT INTO `ar_session` (`payer_id`, `user_id`, `pay_total`, `payment_type`, `description`, `patient_id`, `payment_method`, `adjustment_code`, `post_to_date`) SELECT `payer_id`, `user_id`, `pay_total`, `payment_type`, `description`, `patient_id`, `payment_method`, `adjustment_code`, `post_to_date` FROM `temp_table_one`;
286 INSERT INTO `ar_activity` (`pid`, `encounter`, `code`, `modifier`, `payer_type`, `post_time`, `post_user`, `session_id`, `pay_amount`, `account_code`) SELECT `pid`, `encounter`, `code`, `modifier`, `payer_type`, `post_time`, `post_user`, `session_id`, `pay_amount`, `account_code` FROM `temp_table_one`;
287 UPDATE `billing` SET `activity`=0 WHERE `code_type`='COPAY';
288 DROP TABLE IF EXISTS `temp_table_one`;
289 #EndIf
291 #IfNotTable facility_user_ids
292 CREATE TABLE  `facility_user_ids` (
293   `id` bigint(20) NOT NULL AUTO_INCREMENT,
294   `uid` bigint(20) DEFAULT NULL,
295   `facility_id` bigint(20) DEFAULT NULL,
296   `field_id`    varchar(31)  NOT NULL COMMENT 'references layout_options.field_id',
297   `field_value` TEXT NOT NULL,
298   PRIMARY KEY (`id`),
299   KEY `uid` (`uid`,`facility_id`,`field_id`)
300 ) ENGINE=MyISAM  AUTO_INCREMENT=1 ;
301 #EndIf
303 #IfNotRow layout_options form_id FACUSR
304 INSERT INTO `layout_options` (form_id, field_id, group_name, title, seq, data_type, uor, fld_length, max_length, list_id, titlecols, datacols, default_value, edit_options, description) VALUES ('FACUSR', 'provider_id', '1General', 'Provider ID', 1, 2, 1, 15, 63, '', 1, 1, '', '', 'Provider ID at Specified Facility');
305 #EndIf
307 #IfMissingColumn patient_data ref_providerID
308 ALTER TABLE `patient_data` ADD COLUMN `ref_providerID` int(11) default NULL;
309 UPDATE `patient_data` SET `ref_providerID`=`providerID`;
310 INSERT INTO `layout_options` (form_id, field_id, group_name, title, seq, data_type, uor, fld_length, max_length, list_id, titlecols, datacols, default_value, edit_options, description) VALUES ('DEM', 'ref_providerID', '3Choices', 'Referring Provider', 2, 11, 1, 0, 0, '', 1, 3, '', '', 'Referring Provider');
311 UPDATE `layout_options` SET `description`='Provider' WHERE `form_id`='DEM' AND `field_id`='providerID';
312 UPDATE `layout_options` SET `seq`=(1+`seq`) WHERE `form_id`='DEM' AND `group_name` LIKE '%Choices' AND `field_id` != 'providerID' AND `field_id` != 'ref_providerID';
313 #EndIf
315 #IfMissingColumn documents couch_docid
316 ALTER TABLE `documents` ADD COLUMN `couch_docid` VARCHAR(100) NULL;
317 #EndIf
319 #IfMissingColumn documents couch_revid
320 ALTER TABLE `documents` ADD COLUMN `couch_revid` VARCHAR(100) NULL;
321 #EndIf
323 #IfMissingColumn documents storagemethod
324 ALTER TABLE `documents` ADD COLUMN `storagemethod` TINYINT(4) DEFAULT '0' NOT NULL COMMENT '0->Harddisk,1->CouchDB';
325 #EndIf