minor fix to HL7 viewer by ensoftek
[openemr.git] / library / payment.inc.php
blobfab99a044be3fbd8dfb42630e0b88ff912957d69
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2010 Z&H Consultancy Services Private Limited <sam@zhservices.com>
4 //
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 //
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Paul Simon K <paul@zhservices.com>
26 // +------------------------------------------------------------------------------+
27 //===============================================================================
28 //This section handles the common functins of payment screens.
29 //===============================================================================
30 function DistributionInsert($CountRow,$created_time,$user_id)
31 {//Function inserts the distribution.Payment,Adjustment,Deductable,Takeback & Follow up reasons are inserted as seperate rows.
32 //It automatically pushes to next insurance for billing.
33 //In the screen a drop down of Ins1,Ins2,Ins3,Pat are given.The posting can be done for any level.
34 $Affected='no';
35 if (isset($_POST["Payment$CountRow"]) && $_POST["Payment$CountRow"]*1>0)
37 if(trim(formData('type_name' ))=='insurance')
39 if(trim(formData("HiddenIns$CountRow" ))==1)
41 $AccountCode="IPP";
43 if(trim(formData("HiddenIns$CountRow" ))==2)
45 $AccountCode="ISP";
47 if(trim(formData("HiddenIns$CountRow" ))==3)
49 $AccountCode="ITP";
52 elseif(trim(formData('type_name' ))=='patient')
54 $AccountCode="PP";
56 sqlStatement("insert into ar_activity set " .
57 "pid = '" . trim(formData('hidden_patient_code' )) .
58 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
59 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
60 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
61 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
62 "', post_time = '" . trim($created_time ) .
63 "', post_user = '" . trim($user_id ) .
64 "', session_id = '" . trim(formData('payment_id')) .
65 "', modified_time = '" . trim($created_time ) .
66 "', pay_amount = '" . trim(formData("Payment$CountRow" )) .
67 "', adj_amount = '" . 0 .
68 "', account_code = '" . "$AccountCode" .
69 "'");
70 $Affected='yes';
72 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0)
74 if(trim(formData('type_name' ))=='insurance')
76 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow" ));
77 $AccountCode="IA";
79 elseif(trim(formData('type_name' ))=='patient')
81 $AdjustString="Pt adjust";
82 $AccountCode="PA";
86 idSqlStatement("insert into ar_activity set " .
87 "pid = '" . trim(formData('hidden_patient_code' )) .
88 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
89 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
90 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
91 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
92 "', post_time = '" . trim($created_time ) .
93 "', post_user = '" . trim($user_id ) .
94 "', session_id = '" . trim(formData('payment_id')) .
95 "', modified_time = '" . trim($created_time ) .
96 "', pay_amount = '" . 0 .
97 "', adj_amount = '" . trim(formData("AdjAmount$CountRow" )) .
98 "', memo = '" . "$AdjustString" .
99 "', account_code = '" . "$AccountCode" .
100 "'");
101 $Affected='yes';
103 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0)
105 idSqlStatement("insert into ar_activity set " .
106 "pid = '" . trim(formData('hidden_patient_code' )) .
107 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
108 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
109 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
110 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
111 "', post_time = '" . trim($created_time ) .
112 "', post_user = '" . trim($user_id ) .
113 "', session_id = '" . trim(formData('payment_id')) .
114 "', modified_time = '" . trim($created_time ) .
115 "', pay_amount = '" . 0 .
116 "', adj_amount = '" . 0 .
117 "', memo = '" . "Deductable $".trim(formData("Deductible$CountRow" )) .
118 "', account_code = '" . "Deduct" .
119 "'");
120 $Affected='yes';
122 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0)
124 idSqlStatement("insert into ar_activity set " .
125 "pid = '" . trim(formData('hidden_patient_code' )) .
126 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
127 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
128 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
129 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
130 "', post_time = '" . trim($created_time ) .
131 "', post_user = '" . trim($user_id ) .
132 "', session_id = '" . trim(formData('payment_id')) .
133 "', modified_time = '" . trim($created_time ) .
134 "', pay_amount = '" . trim(formData("Takeback$CountRow" ))*-1 .
135 "', adj_amount = '" . 0 .
136 "', account_code = '" . "Takeback" .
137 "'");
138 $Affected='yes';
140 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y')
142 idSqlStatement("insert into ar_activity set " .
143 "pid = '" . trim(formData('hidden_patient_code' )) .
144 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
145 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
146 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
147 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
148 "', post_time = '" . trim($created_time ) .
149 "', post_user = '" . trim($user_id ) .
150 "', session_id = '" . trim(formData('payment_id')) .
151 "', modified_time = '" . trim($created_time ) .
152 "', pay_amount = '" . 0 .
153 "', adj_amount = '" . 0 .
154 "', follow_up = '" . "y" .
155 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow" )) .
156 "'");
157 $Affected='yes';
159 if($Affected=='yes')
161 if(trim(formData('type_name' ))!='patient')
163 $ferow = sqlQuery("select last_level_closed from form_encounter where
164 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
165 //multiple charges can come.
166 if($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow" )))
168 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow" ))."' where
169 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
170 //last_level_closed gets increased.
171 //-----------------------------------
172 // Determine the next insurance level to be billed.
173 $ferow = sqlQuery("SELECT date, last_level_closed " .
174 "FROM form_encounter WHERE " .
175 "pid = '".trim(formData('hidden_patient_code' ))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow" ))."'");
176 $date_of_service = substr($ferow['date'], 0, 10);
177 $new_payer_type = 0 + $ferow['last_level_closed'];
178 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0)
179 ++$new_payer_type;
180 $new_payer_id = arGetPayerID(trim(formData('hidden_patient_code' )), $date_of_service, $new_payer_type);
181 if($new_payer_id>0)
183 arSetupSecondary(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" )),0);
185 //-----------------------------------
190 //===============================================================================
191 // Delete rows, with logging, for the specified table using the
192 // specified WHERE clause. Borrowed from deleter.php.
194 function row_delete($table, $where) {
195 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
196 $count = 0;
197 while ($trow = sqlFetchArray($tres)) {
198 $logstring = "";
199 foreach ($trow as $key => $value) {
200 if (! $value || $value == '0000-00-00 00:00:00') continue;
201 if ($logstring) $logstring .= " ";
202 $logstring .= $key . "='" . addslashes($value) . "'";
204 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
205 ++$count;
207 if ($count) {
208 $query = "DELETE FROM $table WHERE $where";
209 sqlStatement($query);
212 //===============================================================================