minor adjustment to prior commit
[openemr.git] / interface / practice / ins_search.php
blobf4f4d00b7ba7c06dff4c2b54bf058fac6a395de8
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 * @copyright Copyright (c) 2005 Rod Roark <rod@sunsetsystems.com>
21 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
22 * @copyright Copyright (c) 2020 Tyler Wrenn <tyler@tylerwrenn.com>
23 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
26 require_once("../globals.php");
28 use OpenEMR\Common\Csrf\CsrfUtils;
29 use OpenEMR\Core\Header;
30 use OpenEMR\Services\InsuranceCompanyService;
32 // Putting a message here will cause a popup window to display it.
33 $info_msg = "";
35 // Grab insurance type codes from service
36 $insuranceCompany = new InsuranceCompanyService();
37 $ins_type_code_array = $insuranceCompany->getInsuranceTypes();
40 <html>
41 <head>
42 <title><?php echo xlt('Insurance Company Search/Add');?></title>
44 <?php Header::setupHeader(['opener','topdialog']); ?>
46 <style>
47 td {
48 font-size: 0.8125rem;
51 #form_entry {
52 display: block;
55 #form_list {
56 display: none;
59 </style>
61 <script>
63 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
65 // This is invoked when our Search button is clicked.
66 function dosearch() {
68 $("#form_entry").hide();
69 var f = document.forms[0];
70 var search_list = 'ins_list.php' +
71 '?form_name=' + encodeURIComponent(f.form_name.value ) +
72 '&form_attn=' + encodeURIComponent(f.form_attn.value ) +
73 '&form_addr1=' + encodeURIComponent(f.form_addr1.value ) +
74 '&form_addr2=' + encodeURIComponent(f.form_addr2.value ) +
75 '&form_city=' + encodeURIComponent(f.form_city.value ) +
76 '&form_state=' + encodeURIComponent(f.form_state.value ) +
77 '&form_zip=' + encodeURIComponent(f.form_zip.value ) +
78 '&form_phone=' + encodeURIComponent(f.form_phone.value ) +
79 '&form_cms_id=' + encodeURIComponent(f.form_cms_id.value) +
80 '&csrf_token_form=' + <?php echo js_url(CsrfUtils::collectCsrfToken()); ?>;
82 top.restoreSession();
83 $("#form_list").load( search_list ).show();
85 return false;
88 // The ins_list.php window calls this to set the selected insurance.
89 function set_insurance(ins_id, ins_name) {
90 if (opener.closed || ! opener.set_insurance)
91 alert('The target form was closed; I cannot apply your selection.');
92 else
93 opener.set_insurance(ins_id, ins_name);
94 dlgclose('InsSaveClose',false);
97 // This is set to true on a mousedown of the Save button. The
98 // reason is so we can distinguish between clicking on the Save
99 // button vs. hitting the Enter key, as we prefer the "default"
100 // action to be search and not save.
101 var save_clicked = false;
103 // Onsubmit handler.
104 function validate(f) {
105 // If save was not clicked then default to searching.
106 if (! save_clicked) return dosearch();
107 save_clicked = false;
109 msg = '';
110 if (! f.form_name.value.length ) msg += 'Company name is missing. ';
111 if (! f.form_addr1.value.length) msg += 'Address is missing. ';
112 if (! f.form_city.value.length ) msg += 'City is missing. ';
113 if (! f.form_state.value.length) msg += 'State is missing. ';
114 if (! f.form_zip.value.length ) msg += 'Zip is missing.';
116 if (msg) {
117 alert(msg);
118 return false;
121 top.restoreSession();
122 return true;
125 </script>
127 </head>
129 <body class="body_top">
130 <?php
131 // If we are saving, then save and close the window.
133 if ($_POST['form_save']) {
134 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
135 CsrfUtils::csrfNotVerified();
138 $ins_id = '';
139 $ins_name = $_POST['form_name'];
141 if ($ins_id) {
142 // sql for updating could go here if this script is enhanced to support
143 // editing of existing insurance companies.
144 } else {
145 $ins_id = $insuranceCompany->insert(
146 array(
147 'name' => $ins_name,
148 'attn' => $_POST['form_attn'],
149 'cms_id' => $_POST['form_cms_id'],
150 'ins_type_code' => $_POST['form_ins_type_code'],
151 'x12_receiver_id' => $_POST['form_partner'],
152 'x12_default_parter_id' => $_POST['form_partner'],
153 'alt_cms_id' => null
157 sqlStatement("INSERT INTO addresses ( " .
158 "id, line1, line2, city, state, zip, country, foreign_id " .
159 ") VALUES ( " .
160 "'" . add_escape_custom(generate_id()) . "', " .
161 "'" . add_escape_custom($_POST['form_addr1']) . "', " .
162 "'" . add_escape_custom($_POST['form_addr2']) . "', " .
163 "'" . add_escape_custom($_POST['form_city']) . "', " .
164 "'" . add_escape_custom($_POST['form_state']) . "', " .
165 "'" . add_escape_custom($_POST['form_zip']) . "', " .
166 "'" . add_escape_custom($_POST['form_country']) . "', " .
167 "'" . add_escape_custom($ins_id) . "' " .
168 ")");
170 $phone_parts = array();
171 preg_match(
172 "/(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/",
173 $_POST['form_phone'],
174 $phone_parts
177 sqlStatement("INSERT INTO phone_numbers ( " .
178 "id, country_code, area_code, prefix, number, type, foreign_id " .
179 ") VALUES ( " .
180 "'" . add_escape_custom(generate_id()) . "', " .
181 "'+1'" . ", " .
182 "'" . add_escape_custom($phone_parts[1]) . "', " .
183 "'" . add_escape_custom($phone_parts[2]) . "', " .
184 "'" . add_escape_custom($phone_parts[3]) . "', " .
185 "'2'" . ", " .
186 "'" . add_escape_custom($ins_id) . "' " .
187 ")");
190 // Close this window and tell our opener to select the new company.
192 echo "<script>\n";
193 if ($info_msg) {
194 echo " alert(" . js_escape($info_msg) . ");\n";
197 echo " top.restoreSession();\n";
198 echo " if (opener.set_insurance) opener.set_insurance(" . js_escape($ins_id) . "," . js_escape($ins_name) . ");\n";
199 echo " dlgclose();\n";
200 echo "</script></body></html>\n";
201 exit();
204 // Query x12_partners.
205 $xres = sqlStatement(
206 "SELECT id, name FROM x12_partners ORDER BY name"
209 <div id="form_entry">
211 <form method='post' name='theform' action='ins_search.php' onsubmit='return validate(this)'>
212 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
213 <center>
216 <table class="w-100 border-0">
217 <tr>
218 <td class="font-weight-bold" width='1%' nowrap><?php echo xlt('Name'); ?>:</td>
219 <td>
220 <input type='text' size='20' name='form_name' maxlength='35' class='form-control form-control-sm' title='<?php echo xla('Name of insurance company'); ?>' />
221 </td>
222 </tr>
224 <tr>
225 <td class="font-weight-bold" nowrap><?php echo xlt('Attention');?>:</td>
226 <td>
227 <input type='text' size='20' name='form_attn' maxlength='35' class='form-control form-control-sm' title='<?php echo xla('Contact name'); ?>' />
228 </td>
229 </tr>
231 <tr>
232 <td class="font-weight-bold" nowrap><?php echo xlt('Address1'); ?>:</td>
233 <td>
234 <input type='text' size='20' name='form_addr1' maxlength='35' class='form-control form-control-sm' title='First address line' />
235 </td>
236 </tr>
238 <tr>
239 <td class="font-weight-bold" nowrap><?php echo xlt('Address2'); ?>:</td>
240 <td>
241 <input type='text' size='20' name='form_addr2' maxlength='35' class='form-control form-control-sm' title='Second address line, if any' />
242 </td>
243 </tr>
245 <tr>
246 <td class="font-weight-bold" nowrap><?php echo xlt('City/State'); ?>:</td>
247 <td class="form-row">
248 <div class="col">
249 <input type='text' size='20' name='form_city' maxlength='25' class='form-control form-control-sm' title='City name' />
250 </div>
251 <div class="col">
252 <input type='text' size='3' name='form_state' maxlength='35' class='form-control form-control-sm' title='State or locality' />
253 </div>
254 </td>
255 </tr>
257 <tr>
258 <td class="font-weight-bold" nowrap><?php echo xlt('Zip/Country:'); ?></td>
259 <td class="form-row">
260 <div class="col">
261 <input type='text' size='20' name='form_zip' maxlength='10' class='form-control form-control-sm' title='Postal code' />
262 </div>
263 <div class="col">
264 <input type='text' size='20' class="form-control form-control-sm" name='form_country' value='USA' maxlength='35' title='Country name' />
265 </div>
266 </td>
267 </tr>
269 <tr>
270 <td class="font-weight-bold" nowrap><?php echo xlt('Phone'); ?>:</td>
271 <td>
272 <input type='text' size='20' name='form_phone' maxlength='20' class='form-control form-control-sm' title='Telephone number' />
273 </td>
274 </tr>
275 <tr>
276 <td class="font-weight-bold" nowrap><?php echo xlt('Payer ID'); ?>:</td>
277 <td>
278 <input type='text' size='20' name='form_cms_id' maxlength='15' class='form-control form-control-sm' title='Identifier assigned by CMS' />
279 </td>
280 </tr>
282 <tr>
283 <td class="font-weight-bold" nowrap><?php echo xlt('Payer Type'); ?>:</td>
284 <td>
285 <select name='form_ins_type_code' class="form-control form-control-sm">
286 <?php
287 for ($i = 1; $i < count($ins_type_code_array); ++$i) {
288 echo " <option value='" . attr($i) . "'";
289 echo ">" . text($ins_type_code_array[$i]) . "\n";
292 </select>
293 </td>
294 </tr>
296 <tr>
297 <td class="font-weight-bold" nowrap><?php echo xlt('X12 Partner'); ?>:</td>
298 <td>
299 <select name='form_partner' title='Default X12 Partner' class="form-control form-control-sm">
300 <option value=""><?php echo '-- ' . xlt('None{{Partner}}') . ' --'; ?></option>
301 <?php
302 while ($xrow = sqlFetchArray($xres)) {
303 echo " <option value='" . attr($xrow['id']) . "'";
304 echo ">" . text($xrow['name']) . "</option>\n";
307 </select>
308 </td>
309 </tr>
311 </table>
313 <input type='button' value='<?php echo xla('Search'); ?>' class='btn btn-primary' onclick='dosearch()' />
314 <input type='submit' value='<?php echo xla('Save as New'); ?>' class='btn btn-primary' name='form_save' onmousedown='save_clicked=true' />
315 <input type='button' value='<?php echo xla('Cancel'); ?>' class='btn btn-primary' onclick='window.close();'/>
317 </center>
318 </form>
319 </div>
321 <div id="form_list">
322 </div>
324 </body>
325 </html>