fixes to prior commit
[openemr.git] / controllers / C_InsuranceCompany.class.php
blob04201b4d9903389875a9525bad88a9d07311593b
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 __construct($template_mod = "general") {
14 parent::__construct();
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'] );
21 $this->InsuranceCompany = new InsuranceCompany();
24 function default_action() {
25 return $this->list_action();
28 function edit_action($id = "",$patient_id="",$p_obj = null) {
29 if ($p_obj != null && get_class($p_obj) == "insurancecompany") {
30 $this->icompanies[0] = $p_obj;
32 elseif (get_class($this->icompanies[0]) != "insurancecompany" ) {
33 $this->icompanies[0] = new InsuranceCompany($id);
36 $x = new X12Partner();
37 $this->assign("x12_partners", $x->_utility_array($x->x12_partner_factory()));
39 $this->assign("insurancecompany", $this->icompanies[0]);
40 return $this->fetch($GLOBALS['template_dir'] . "insurance_companies/" . $this->template_mod . "_edit.html");
43 function list_action($sort = "") {
45 if (!empty($sort)) {
46 $this->assign("icompanies", $this->InsuranceCompany->insurance_companies_factory("",$sort));
48 else {
49 $this->assign("icompanies", $this->InsuranceCompany->insurance_companies_factory());
52 return $this->fetch($GLOBALS['template_dir'] . "insurance_companies/" . $this->template_mod . "_list.html");
56 function edit_action_process() {
57 if ($_POST['process'] != "true")
58 return;
59 //print_r($_POST);
60 if (is_numeric($_POST['id'])) {
61 $this->icompanies[0] = new InsuranceCompany($_POST['id']);
63 else {
64 $this->icompanies[0] = new InsuranceCompany();
67 parent::populate_object($this->icompanies[0]);
69 $this->icompanies[0]->persist();
70 $this->icompanies[0]->populate();
72 // Post insurance companies as customers to the accounting system
73 // unless globals.php requests otherwise.
75 if (! $GLOBALS['insurance_companies_are_not_customers'])
76 $this->_sync_ws($this->icompanies[0]);
78 //echo "action processeed";
79 $_POST['process'] = "";
80 header('Location:'.$GLOBALS['webroot']."/controller.php?" . "practice_settings&insurance_company&action=list");//Z&H
83 function _sync_ws($ic) {
85 $db = $GLOBALS['adodb']['db'];
87 $customer_info = array();
89 $sql = "SELECT foreign_id,foreign_table FROM integration_mapping where local_table = 'insurance_companies' and local_id = '" . $ic->get_id() . "'";
90 $result = $db->Execute($sql);
91 if ($result && !$result->EOF) {
92 $customer_info['foreign_update'] = true;
93 $customer_info['foreign_id'] = $result->fields['foreign_id'];
94 $customer_info['foreign_table'] = $result->fields['foreign_table'];
97 ///xml rpc code to connect to accounting package and add user to it
98 $customer_info['firstname'] = "";
99 $customer_info['lastname'] = $ic->get_name();
100 $a = $ic->get_address();
101 $customer_info['address'] = $a->get_line1() . " " . $a->get_line2();
102 $customer_info['suburb'] = $a->get_city();
103 $customer_info['postcode'] = $a->get_zip();
105 //ezybiz wants state as a code rather than abbreviation
106 $customer_info['geo_zone_id'] = "";
107 $sql = "SELECT zone_id from geo_zone_reference where zone_code = '" . strtoupper($a->get_state()) . "'";
108 $db = $GLOBALS['adodb']['db'];
109 $result = $db->Execute($sql);
110 if ($result && !$result->EOF) {
111 $customer_info['geo_zone_id'] = $result->fields['zone_id'];
114 //ezybiz wants country as a code rather than abbreviation
115 $customer_info['country'] = "";
117 //assume USA for insurance companies
118 $country_code = 223;
119 $sql = "SELECT countries_id from geo_country_reference where countries_iso_code_2 = '" . strtoupper($country_code) . "'";
120 $db = $GLOBALS['adodb']['db'];
121 $result = $db->Execute($sql);
122 if ($result && !$result->EOF) {
123 $customer_info['geo_country_id'] = $result->fields['countries_id'];
126 $customer_info['phone1'] = $ic->get_phone();
127 $customer_info['phone1comment'] = "Phone Number";
128 $customer_info['phone2'] = "";
129 $customer_info['phone2comment'] = "";
130 $customer_info['email'] = "";
131 $customer_info['is_payer'] = true;
132 $function['ezybiz.add_customer'] = array(new xmlrpcval($customer_info,"struct"));
133 $ws = new WSWrapper($function);
135 // if the remote patient was added make an entry in the local mapping table to that updates can be made correctly
136 if (is_numeric($ws->value)) {
137 $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' ";
138 $db->Execute($sql) or die ("error: " . $db->ErrorMsg());