Phyaura Calendar speed inhancement
[openemr.git] / library / payment.inc.php
blobaf566c350f954435ee0a4e96b49d92205d39f998
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2005-2010 Z&H Healthcare Solutions, LLC <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 if(trim(formData('type_name' ))!='patient')
36 $ferow = sqlQuery("select last_level_closed from form_encounter where
37 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
38 //multiple charges can come.
39 if($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow" )))
41 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow" ))."' where
42 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
43 //last_level_closed gets increased.
44 //-----------------------------------
45 // Determine the next insurance level to be billed.
46 $ferow = sqlQuery("SELECT date, last_level_closed " .
47 "FROM form_encounter WHERE " .
48 "pid = '".trim(formData('hidden_patient_code' ))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow" ))."'");
49 $date_of_service = substr($ferow['date'], 0, 10);
50 $new_payer_type = 0 + $ferow['last_level_closed'];
51 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0)
52 ++$new_payer_type;
53 $new_payer_id = arGetPayerID(trim(formData('hidden_patient_code' )), $date_of_service, $new_payer_type);
54 if($new_payer_id>0)
56 arSetupSecondary(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" )),0);
58 //-----------------------------------
61 if (isset($_POST["Payment$CountRow"]) && $_POST["Payment$CountRow"]*1>0)
63 if(trim(formData('type_name' ))=='insurance')
65 if(trim(formData("HiddenIns$CountRow" ))==1)
67 $AccountCode="IPP";
69 if(trim(formData("HiddenIns$CountRow" ))==2)
71 $AccountCode="ISP";
73 if(trim(formData("HiddenIns$CountRow" ))==3)
75 $AccountCode="ITP";
78 elseif(trim(formData('type_name' ))=='patient')
80 $AccountCode="PP";
82 sqlStatement("insert into ar_activity set " .
83 "pid = '" . trim(formData('hidden_patient_code' )) .
84 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
85 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
86 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
87 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
88 "', post_time = '" . trim($created_time ) .
89 "', post_user = '" . trim($user_id ) .
90 "', session_id = '" . trim(formData('payment_id')) .
91 "', modified_time = '" . trim($created_time ) .
92 "', pay_amount = '" . trim(formData("Payment$CountRow" )) .
93 "', adj_amount = '" . 0 .
94 "', account_code = '" . "$AccountCode" .
95 "'");
97 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0)
99 if(trim(formData('type_name' ))=='insurance')
101 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow" ));
102 $AccountCode="IA";
104 elseif(trim(formData('type_name' ))=='patient')
106 $AdjustString="Pt adjust";
107 $AccountCode="PA";
111 idSqlStatement("insert into ar_activity set " .
112 "pid = '" . trim(formData('hidden_patient_code' )) .
113 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
114 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
115 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
116 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
117 "', post_time = '" . trim($created_time ) .
118 "', post_user = '" . trim($user_id ) .
119 "', session_id = '" . trim(formData('payment_id')) .
120 "', modified_time = '" . trim($created_time ) .
121 "', pay_amount = '" . 0 .
122 "', adj_amount = '" . trim(formData("AdjAmount$CountRow" )) .
123 "', memo = '" . "$AdjustString" .
124 "', account_code = '" . "$AccountCode" .
125 "'");
127 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0)
129 idSqlStatement("insert into ar_activity set " .
130 "pid = '" . trim(formData('hidden_patient_code' )) .
131 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
132 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
133 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
134 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
135 "', post_time = '" . trim($created_time ) .
136 "', post_user = '" . trim($user_id ) .
137 "', session_id = '" . trim(formData('payment_id')) .
138 "', modified_time = '" . trim($created_time ) .
139 "', pay_amount = '" . 0 .
140 "', adj_amount = '" . 0 .
141 "', memo = '" . "Deductable $".trim(formData("Deductible$CountRow" )) .
142 "', account_code = '" . "Deduct" .
143 "'");
145 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0)
147 idSqlStatement("insert into ar_activity set " .
148 "pid = '" . trim(formData('hidden_patient_code' )) .
149 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
150 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
151 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
152 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
153 "', post_time = '" . trim($created_time ) .
154 "', post_user = '" . trim($user_id ) .
155 "', session_id = '" . trim(formData('payment_id')) .
156 "', modified_time = '" . trim($created_time ) .
157 "', pay_amount = '" . trim(formData("Takeback$CountRow" ))*-1 .
158 "', adj_amount = '" . 0 .
159 "', account_code = '" . "Takeback" .
160 "'");
162 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y')
164 idSqlStatement("insert into ar_activity set " .
165 "pid = '" . trim(formData('hidden_patient_code' )) .
166 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
167 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
168 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
169 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
170 "', post_time = '" . trim($created_time ) .
171 "', post_user = '" . trim($user_id ) .
172 "', session_id = '" . trim(formData('payment_id')) .
173 "', modified_time = '" . trim($created_time ) .
174 "', pay_amount = '" . 0 .
175 "', adj_amount = '" . 0 .
176 "', follow_up = '" . "y" .
177 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow" )) .
178 "'");
181 //===============================================================================
182 // Delete rows, with logging, for the specified table using the
183 // specified WHERE clause. Borrowed from deleter.php.
185 function row_delete($table, $where) {
186 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
187 $count = 0;
188 while ($trow = sqlFetchArray($tres)) {
189 $logstring = "";
190 foreach ($trow as $key => $value) {
191 if (! $value || $value == '0000-00-00 00:00:00') continue;
192 if ($logstring) $logstring .= " ";
193 $logstring .= $key . "='" . addslashes($value) . "'";
195 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
196 ++$count;
198 if ($count) {
199 $query = "DELETE FROM $table WHERE $where";
200 sqlStatement($query);
203 //===============================================================================