updated user manual wiki page link for future 4.1.2
[openemr.git] / controllers / C_InsuranceCompany.class.php
blob313f59bc864268cb3ff05f7603f7f022c5e0a890
1 <?php
3 require_once($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
4 require_once($GLOBALS['fileroot'] . "/library/classes/InsuranceCompany.class.php");
5 require_once($GLOBALS['fileroot'] . "/library/classes/X12Partner.class.php");
6 require_once($GLOBALS['fileroot'] . "/library/classes/WSWrapper.class.php");
8 class C_InsuranceCompany extends Controller {
10 var $template_mod;
11 var $icompanies;
13 function C_InsuranceCompany($template_mod = "general") {
14 parent::Controller();
15 $this->icompanies = array();
16 $this->template_mod = $template_mod;
17 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
18 $this->assign("CURRENT_ACTION", $GLOBALS['webroot']."/controller.php?" . "practice_settings&insurance_company&");
19 $this->assign("STYLE", $GLOBALS['style']);
20 $this->assign("WEB_ROOT", $GLOBALS['webroot'] );
23 function default_action() {
24 return $this->list_action();
27 function edit_action($id = "",$patient_id="",$p_obj = null) {
28 if ($p_obj != null && get_class($p_obj) == "insurancecompany") {
29 $this->icompanies[0] = $p_obj;
31 elseif (get_class($this->icompanies[0]) != "insurancecompany" ) {
32 $this->icompanies[0] = new InsuranceCompany($id);
35 $x = new X12Partner();
36 $this->assign("x12_partners", $x->_utility_array($x->x12_partner_factory()));
38 $this->assign("insurancecompany", $this->icompanies[0]);
39 return $this->fetch($GLOBALS['template_dir'] . "insurance_companies/" . $this->template_mod . "_edit.html");
42 function list_action($sort = "") {
44 if (!empty($sort)) {
45 $this->assign("icompanies", InsuranceCompany::insurance_companies_factory("",$sort));
47 else {
48 $this->assign("icompanies", InsuranceCompany::insurance_companies_factory());
51 return $this->fetch($GLOBALS['template_dir'] . "insurance_companies/" . $this->template_mod . "_list.html");
55 function edit_action_process() {
56 if ($_POST['process'] != "true")
57 return;
58 //print_r($_POST);
59 if (is_numeric($_POST['id'])) {
60 $this->icompanies[0] = new InsuranceCompany($_POST['id']);
62 else {
63 $this->icompanies[0] = new InsuranceCompany();
66 parent::populate_object($this->icompanies[0]);
68 $this->icompanies[0]->persist();
69 $this->icompanies[0]->populate();
71 // Post insurance companies as customers to the accounting system
72 // unless globals.php requests otherwise.
74 if (! $GLOBALS['insurance_companies_are_not_customers'])
75 $this->_sync_ws($this->icompanies[0]);
77 //echo "action processeed";
78 $_POST['process'] = "";
79 header('Location:'.$GLOBALS['webroot']."/controller.php?" . "practice_settings&insurance_company&action=list");//Z&H
82 function _sync_ws($ic) {
84 $db = $GLOBALS['adodb']['db'];
86 $customer_info = array();
88 $sql = "SELECT foreign_id,foreign_table FROM integration_mapping where local_table = 'insurance_companies' and local_id = '" . $ic->get_id() . "'";
89 $result = $db->Execute($sql);
90 if ($result && !$result->EOF) {
91 $customer_info['foreign_update'] = true;
92 $customer_info['foreign_id'] = $result->fields['foreign_id'];
93 $customer_info['foreign_table'] = $result->fields['foreign_table'];
96 ///xml rpc code to connect to accounting package and add user to it
97 $customer_info['firstname'] = "";
98 $customer_info['lastname'] = $ic->get_name();
99 $a = $ic->get_address();
100 $customer_info['address'] = $a->get_line1() . " " . $a->get_line2();
101 $customer_info['suburb'] = $a->get_city();
102 $customer_info['postcode'] = $a->get_zip();
104 //ezybiz wants state as a code rather than abbreviation
105 $customer_info['geo_zone_id'] = "";
106 $sql = "SELECT zone_id from geo_zone_reference where zone_code = '" . strtoupper($a->get_state()) . "'";
107 $db = $GLOBALS['adodb']['db'];
108 $result = $db->Execute($sql);
109 if ($result && !$result->EOF) {
110 $customer_info['geo_zone_id'] = $result->fields['zone_id'];
113 //ezybiz wants country as a code rather than abbreviation
114 $customer_info['country'] = "";
116 //assume USA for insurance companies
117 $country_code = 223;
118 $sql = "SELECT countries_id from geo_country_reference where countries_iso_code_2 = '" . strtoupper($country_code) . "'";
119 $db = $GLOBALS['adodb']['db'];
120 $result = $db->Execute($sql);
121 if ($result && !$result->EOF) {
122 $customer_info['geo_country_id'] = $result->fields['countries_id'];
125 $customer_info['phone1'] = $ic->get_phone();
126 $customer_info['phone1comment'] = "Phone Number";
127 $customer_info['phone2'] = "";
128 $customer_info['phone2comment'] = "";
129 $customer_info['email'] = "";
130 $customer_info['is_payer'] = true;
131 $function['ezybiz.add_customer'] = array(new xmlrpcval($customer_info,"struct"));
132 $ws = new WSWrapper($function);
134 // if the remote patient was added make an entry in the local mapping table to that updates can be made correctly
135 if (is_numeric($ws->value)) {
136 $sql = "REPLACE INTO integration_mapping set id = '" . $db->GenID("sequences") . "', foreign_id ='" . $ws->value . "', foreign_table ='customer', local_id = '" . $ic->get_id() . "', local_table = 'insurance_companies' ";
137 $db->Execute($sql) or die ("error: " . $db->ErrorMsg());