Refactor previous name into dedicated service (#7571)
[openemr.git] / interface / practice / ins_search.php
blobb8c1dcb70344143f664e10e0f33bb870101a9aff
1 <?php
3 /**
4 * This module is used to find and add insurance companies.
5 * It is opened as a popup window. The opener may have a
6 * JavaScript function named set_insurance(id, name), in which
7 * case selecting or adding an insurance company will cause the
8 * function to be called passing the ID and name of that company.
10 * When used for searching, this module will in turn open another
11 * popup window ins_list.php, which lists the matched results and
12 * permits selection of one of them via the same set_insurance()
13 * function.
15 * @package OpenEMR
16 * @link http://www.open-emr.org
17 * @author Rod Roark <rod@sunsetsystems.com>
18 * @author Brady Miller <brady.g.miller@gmail.com>
19 * @author Tyler Wrenn <tyler@tylerwrenn.com>
20 * @author Stephen Nielson <snielson@discoverandchange.com>
21 * @copyright Copyright (c) 2005 Rod Roark <rod@sunsetsystems.com>
22 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
23 * @copyright Copyright (c) 2020 Tyler Wrenn <tyler@tylerwrenn.com>
24 * @copyright Copyright (c) 2024 Care Management Solutions, Inc. <stephen.waite@cmsvt.com>
25 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
28 require_once("../globals.php");
30 use OpenEMR\Common\Csrf\CsrfUtils;
31 use OpenEMR\Core\Header;
32 use OpenEMR\Services\{
33 AddressService,
34 InsuranceCompanyService,
35 PhoneNumberService
38 // Putting a message here will cause a popup window to display it.
39 $info_msg = "";
41 // Grab insurance type codes from service
42 $insuranceCompany = new InsuranceCompanyService();
43 $phoneNumber = new PhoneNumberService();
44 $ins_type_code_array = $insuranceCompany->getInsuranceTypes();
47 <html>
48 <head>
49 <title><?php echo xlt('Insurance Company Search/Add');?></title>
51 <?php Header::setupHeader(['opener','topdialog']); ?>
53 <style>
54 td {
55 font-size: 0.8125rem;
58 #form_entry {
59 display: block;
62 #form_list {
63 display: none;
66 </style>
68 <script>
70 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
72 // This is invoked when our Search button is clicked.
73 function dosearch() {
75 $("#form_entry").hide();
76 var f = document.forms[0];
77 var search_list = 'ins_list.php' +
78 '?form_name=' + encodeURIComponent(f.form_name.value ) +
79 '&form_attn=' + encodeURIComponent(f.form_attn.value ) +
80 '&form_addr1=' + encodeURIComponent(f.form_addr1.value ) +
81 '&form_addr2=' + encodeURIComponent(f.form_addr2.value ) +
82 '&form_city=' + encodeURIComponent(f.form_city.value ) +
83 '&form_state=' + encodeURIComponent(f.form_state.value ) +
84 '&form_zip=' + encodeURIComponent(f.form_zip.value ) +
85 '&form_phone=' + encodeURIComponent(f.form_phone.value ) +
86 '&form_cms_id=' + encodeURIComponent(f.form_cms_id.value) +
87 '&csrf_token_form=' + <?php echo js_url(CsrfUtils::collectCsrfToken()); ?>;
89 top.restoreSession();
90 $("#form_list").load( search_list ).show();
92 return false;
95 // The ins_list.php window calls this to set the selected insurance.
96 function set_insurance(ins_id, ins_name) {
97 window.top.restoreSession(); // make sure to restore the session before we do anything else
98 if (!window.opener) {
99 return; // nothing to do here as somehow we got here without the opener
101 let postMessage = {
102 action: 'insurance-search-set-insurance'
103 ,insuranceId: ins_id
104 ,insuranceName: ins_name
106 // fire off a message so we can decouple things so we don't have to have a specific function
107 // name in the global scope of the opener
108 opener.postMessage(postMessage, window.location.origin);
109 if (opener.closed) {
110 alert('The target form was closed; I cannot apply your selection.');
112 else if (opener.set_insurance) {
113 opener.set_insurance(ins_id, ins_name);
114 dlgclose('InsSaveClose', false);
115 } else {
116 // if we don't have a set_insurance function then we will just close the window as the opener is
117 // using post message to receive events.
118 dlgclose('InsSaveClose', false);
122 // This is set to true on a mousedown of the Save button. The
123 // reason is so we can distinguish between clicking on the Save
124 // button vs. hitting the Enter key, as we prefer the "default"
125 // action to be search and not save.
126 var save_clicked = false;
127 let update_clicked = false;
129 // Onsubmit handler.
130 function validate(f) {
131 // If save was not clicked then default to searching.
132 if (!(save_clicked || update_clicked)) return dosearch();
133 save_clicked = false;
135 msg = '';
136 if (update_clicked && !f.form_id.value.length) msg += 'Id is missing for Update \n';
137 if (! f.form_name.value.length ) msg += 'Company name is missing. ';
138 if (! f.form_addr1.value.length) msg += 'Address is missing. ';
139 if (! f.form_city.value.length ) msg += 'City is missing. ';
140 if (! f.form_state.value.length) msg += 'State is missing. ';
141 if (! f.form_zip.value.length ) msg += 'Zip is missing.';
142 update_clicked = false;
144 if (msg) {
145 alert(msg);
146 return false;
149 top.restoreSession();
150 return true;
153 function clearForm() {
154 let f = document.forms[0];
155 f.form_id.value = '';
156 f.form_name.value = '';
157 f.form_attn.value = '';
158 f.form_addr1.value = '';
159 f.form_addr2.value = '';
160 f.form_city.value = '';
161 f.form_state.value = '';
162 f.form_country.value = '';
163 f.form_zip.value = '';
164 f.form_phone.value = '';
165 f.form_cms_id.value = '';
166 f.form_ins_type_code.value = '';
167 f.form_partner.value = '';
168 f.form_cqm_sop.value = '';
171 </script>
173 </head>
175 <body class="body_top">
176 <?php
177 // If we are saving, then save and close the window.
179 if (
180 ($_POST['form_save'] ?? '')
181 || ($_POST['form_update'] ?? '')
183 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
184 CsrfUtils::csrfNotVerified();
187 if (($_POST['form_save'] ?? '') == 'Save as New') {
188 $ins_id = '';
189 } else {
190 $ins_id = $_POST['form_id'];
192 $ins_name = $_POST['form_name'];
194 if ($ins_id) {
195 // sql for updating could go here if this script is enhanced to support
196 // editing of existing insurance companies.
197 $insuranceCompany->update(
198 array(
199 'name' => $ins_name,
200 'attn' => $_POST['form_attn'],
201 'cms_id' => $_POST['form_cms_id'],
202 'ins_type_code' => $_POST['form_ins_type_code'],
203 'x12_receiver_id' => $_POST['form_x12_receiver'] ?? null,
204 'x12_default_partner_id' => $_POST['form_partner'],
205 'alt_cms_id' => null,
206 'line1' => $_POST['form_addr1'],
207 'line2' => $_POST['form_addr2'],
208 'city' => $_POST['form_city'],
209 'state' => $_POST['form_state'],
210 'zip' => $_POST['form_zip'],
211 'country' => $_POST['form_country'],
212 'phone' => $_POST['form_phone'],
213 'foreign_id' => $ins_id,
214 'cqm_sop' => $_POST['form_cqm_sop']
216 $ins_id
218 } else {
219 $ins_id = $insuranceCompany->insert(
220 array(
221 'name' => $ins_name,
222 'attn' => $_POST['form_attn'],
223 'cms_id' => $_POST['form_cms_id'],
224 'ins_type_code' => $_POST['form_ins_type_code'],
225 'x12_receiver_id' => $_POST['form_receiver'] ?? null,
226 'x12_default_partner_id' => $_POST['form_partner'],
227 'alt_cms_id' => null,
228 'line1' => $_POST['form_addr1'],
229 'line2' => $_POST['form_addr2'],
230 'city' => $_POST['form_city'],
231 'state' => $_POST['form_state'],
232 'zip' => $_POST['form_zip'],
233 'country' => $_POST['form_country'],
234 'phone' => $_POST['form_phone'],
235 'foreign_id' => $ins_id,
236 'cqm_sop' => $_POST['form_cqm_sop']
241 // Close this window and tell our opener to select the new company.
243 echo "<script>\n";
244 if ($info_msg) {
245 echo " alert(" . js_escape($info_msg) . ");\n";
248 // we need to follow the global settings for the display of this name so we will return the name in the set_insurance method
249 $ins_name = (new InsuranceCompanyService())->getInsuranceDisplayName($ins_id);
250 // call the set_insurance method in our header
251 echo " set_insurance(" . js_escape($ins_id) . "," . js_escape($ins_name) . ");\n";
252 echo "</script></body></html>\n";
253 exit();
254 } else {
255 $ins_co = (new InsuranceCompanyService())->getOneById($_GET['ins']) ?? null;
256 $ins_co_address = (new AddressService())->getOneByForeignId($_GET['ins']) ?? null;
257 $ins_co_phone = (new PhoneNumberService())->getOneByForeignId($_GET['ins']) ?? null;
260 // Query x12_partners.
261 $xres = sqlStatement(
262 "SELECT id, name FROM x12_partners ORDER BY name"
265 <div id="form_entry">
267 <form method='post' name='theform' action='ins_search.php' onsubmit='return validate(this)'>
268 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
269 <center>
272 <table class="w-100 border-0">
273 <tr>
274 <td class="font-weight-bold" width='1%' nowrap><?php echo xlt('Id'); ?>:</td>
275 <td>
276 <input type='text' size='20' name='form_id' maxlength='35' class='form-control form-control-sm' title='<?php echo xla('Id of insurance company'); ?>'
277 readonly='readonly' value='<?php echo attr($ins_co['id'] ?? ''); ?>' />
278 </td>
279 </tr>
280 <tr>
281 <td class="font-weight-bold" width='1%' nowrap><?php echo xlt('Name'); ?>:</td>
282 <td>
283 <input type='text' size='20' name='form_name' maxlength='35' class='form-control form-control-sm' title='<?php echo xla('Name of insurance company'); ?>'
284 value='<?php echo attr($ins_co['name'] ?? ''); ?>' />
285 </td>
286 </tr>
288 <tr>
289 <td class="font-weight-bold" nowrap><?php echo xlt('Attention');?>:</td>
290 <td>
291 <input type='text' size='20' name='form_attn' maxlength='35' class='form-control form-control-sm' title='<?php echo xla('Contact name'); ?>'
292 value='<?php echo attr($ins_co['attn'] ?? ''); ?>' />
293 </td>
294 </tr>
296 <tr>
297 <td class="font-weight-bold" nowrap><?php echo xlt('Address1'); ?>:</td>
298 <td>
299 <input type='text' size='20' name='form_addr1' maxlength='35' class='form-control form-control-sm' title='First address line'
300 value='<?php echo attr($ins_co_address['line1'] ?? ''); ?>' />
301 </td>
302 </tr>
304 <tr>
305 <td class="font-weight-bold" nowrap><?php echo xlt('Address2'); ?>:</td>
306 <td>
307 <input type='text' size='20' name='form_addr2' maxlength='35' class='form-control form-control-sm' title='Second address line, if any'
308 value='<?php echo attr($ins_co_address['line2'] ?? ''); ?>' />
309 </td>
310 </tr>
312 <tr>
313 <td class="font-weight-bold" nowrap><?php echo xlt('City/State'); ?>:</td>
314 <td class="form-row">
315 <div class="col">
316 <input type='text' size='20' name='form_city' maxlength='25' class='form-control form-control-sm' title='City name'
317 value='<?php echo attr($ins_co_address['city'] ?? ''); ?>' />
318 </div>
319 <div class="col">
320 <input type='text' size='3' name='form_state' maxlength='35' class='form-control form-control-sm' title='State or locality'
321 value='<?php echo attr($ins_co_address['state'] ?? ''); ?>' />
322 </div>
323 </td>
324 </tr>
326 <tr>
327 <td class="font-weight-bold" nowrap><?php echo xlt('Zip/Country:'); ?></td>
328 <td class="form-row">
329 <div class="col">
330 <input type='text' size='20' name='form_zip' maxlength='10' class='form-control form-control-sm' title='Postal code'
331 value='<?php echo attr(($ins_co_address['zip'] ?? '') . ($ins_co_address['plus_four'] ?? '')); ?>' />
332 </div>
333 <div class="col">
334 <input type='text' size='20' class="form-control form-control-sm" name='form_country' value='USA' maxlength='35' title='Country name'
335 value='<?php echo attr($ins_co_address['country'] ?? ''); ?>' />
336 </div>
337 </td>
338 </tr>
340 <tr>
341 <td class="font-weight-bold" nowrap><?php echo xlt('Phone'); ?>:</td>
342 <td>
343 <input type='text' size='20' name='form_phone' maxlength='20' class='form-control form-control-sm' title='Telephone number'
344 value='<?php echo attr((
345 ($ins_co_phone['area_code'] ?? '') .
346 ($ins_co_phone['prefix'] ?? '') .
347 ($ins_co_phone['number'] ?? '')
348 )); ?>'
350 </td>
351 </tr>
352 <tr>
353 <td class="font-weight-bold" nowrap><?php echo xlt('Payer ID'); ?>:</td>
354 <td>
355 <input type='text' size='20' name='form_cms_id' maxlength='15' class='form-control form-control-sm' title='Identifier assigned by CMS'
356 value='<?php echo attr($ins_co['cms_id'] ?? ''); ?>' />
357 </td>
358 </tr>
360 <tr>
361 <td class="font-weight-bold" nowrap><?php echo xlt('Payer Type'); ?>:</td>
362 <td>
363 <select name='form_ins_type_code' class="form-control form-control-sm">
364 <?php
365 for ($i = 1; $i < count($ins_type_code_array); ++$i) {
366 echo " <option value='" . attr($i) . "'";
367 if (!empty($ins_co)) {
368 if ($i == $ins_co['ins_type_code'] ?? '') {
369 echo " selected";
372 echo ">" . text($ins_type_code_array[$i]) . "\n";
375 </select>
376 </td>
377 </tr>
379 <tr>
380 <td class="font-weight-bold" nowrap><?php echo xlt('X12 Partner'); ?>:</td>
381 <td>
382 <select name='form_partner' title='Default X12 Partner' class="form-control form-control-sm">
383 <option value=""><?php echo '-- ' . xlt('None{{Partner}}') . ' --'; ?></option>
384 <?php
385 while ($xrow = sqlFetchArray($xres)) {
386 echo " <option value='" . attr($xrow['id']) . "'";
387 if ($xrow['id'] == $ins_co['x12_default_partner_id']) {
388 echo " selected";
390 echo ">" . text($xrow['name']) . "</option>\n";
393 </select>
394 </td>
395 </tr>
397 <tr>
398 <td class="font-weight-bold" nowrap><?php echo xlt('CQM Source of Payment'); ?>:</td>
399 <td>
400 <select name='form_cqm_sop' title='CQM Source of Payment' class="form-control form-control-sm">
401 <option value=""><?php echo '-- ' . xlt('None{{CQM SOP}}') . ' --'; ?></option>
402 <?php
403 $cqm_sop_array = $insuranceCompany->getInsuranceCqmSop();
404 foreach ($cqm_sop_array as $key => $value) {
405 echo " <option value='" . attr($key) . "'";
406 echo ">" . text($value) . "</option>\n";
409 </select>
410 </td>
411 </tr>
413 </table>
415 <input type='button' value='<?php echo xla('Search'); ?>' class='btn btn-primary' onclick='dosearch()' />
416 <input type='submit' value='<?php echo xla('Save as New'); ?>' class='btn btn-primary' name='form_save' onmousedown='save_clicked=true' />
417 <input type='submit' value='<?php echo xla('Update'); ?>' class='btn btn-primary' name='form_update' onmousedown='update_clicked=true' />
418 <input type='button' value='<?php echo xla('Clear'); ?>' class='btn btn-primary' onclick='clearForm()' />
419 <input type='button' value='<?php echo xla('Cancel'); ?>' class='btn btn-primary' onclick='window.close();'/>
421 </center>
422 </form>
423 </div>
425 <div id="form_list">
426 </div>
428 </body>
429 </html>