Code type module improvements:
[openemr.git] / library / payment.inc.php
blob7f14f95184a6f1815057b7bb7f07bbe66dbe304e
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_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
60 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
61 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
62 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
63 "', post_time = '" . trim($created_time ) .
64 "', post_user = '" . trim($user_id ) .
65 "', session_id = '" . trim(formData('payment_id')) .
66 "', modified_time = '" . trim($created_time ) .
67 "', pay_amount = '" . trim(formData("Payment$CountRow" )) .
68 "', adj_amount = '" . 0 .
69 "', account_code = '" . "$AccountCode" .
70 "'");
71 $Affected='yes';
73 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0)
75 if(trim(formData('type_name' ))=='insurance')
77 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow" ));
78 $AccountCode="IA";
80 elseif(trim(formData('type_name' ))=='patient')
82 $AdjustString="Pt adjust";
83 $AccountCode="PA";
87 idSqlStatement("insert into ar_activity set " .
88 "pid = '" . trim(formData('hidden_patient_code' )) .
89 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
90 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
91 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
92 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
93 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
94 "', post_time = '" . trim($created_time ) .
95 "', post_user = '" . trim($user_id ) .
96 "', session_id = '" . trim(formData('payment_id')) .
97 "', modified_time = '" . trim($created_time ) .
98 "', pay_amount = '" . 0 .
99 "', adj_amount = '" . trim(formData("AdjAmount$CountRow" )) .
100 "', memo = '" . "$AdjustString" .
101 "', account_code = '" . "$AccountCode" .
102 "'");
103 $Affected='yes';
105 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0)
107 idSqlStatement("insert into ar_activity set " .
108 "pid = '" . trim(formData('hidden_patient_code' )) .
109 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
110 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
111 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
112 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
113 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
114 "', post_time = '" . trim($created_time ) .
115 "', post_user = '" . trim($user_id ) .
116 "', session_id = '" . trim(formData('payment_id')) .
117 "', modified_time = '" . trim($created_time ) .
118 "', pay_amount = '" . 0 .
119 "', adj_amount = '" . 0 .
120 "', memo = '" . "Deductable $".trim(formData("Deductible$CountRow" )) .
121 "', account_code = '" . "Deduct" .
122 "'");
123 $Affected='yes';
125 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0)
127 idSqlStatement("insert into ar_activity set " .
128 "pid = '" . trim(formData('hidden_patient_code' )) .
129 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
130 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
131 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
132 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
133 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
134 "', post_time = '" . trim($created_time ) .
135 "', post_user = '" . trim($user_id ) .
136 "', session_id = '" . trim(formData('payment_id')) .
137 "', modified_time = '" . trim($created_time ) .
138 "', pay_amount = '" . trim(formData("Takeback$CountRow" ))*-1 .
139 "', adj_amount = '" . 0 .
140 "', account_code = '" . "Takeback" .
141 "'");
142 $Affected='yes';
144 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y')
146 idSqlStatement("insert into ar_activity set " .
147 "pid = '" . trim(formData('hidden_patient_code' )) .
148 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
149 "', code_type = '" . trim(formData("HiddenCodetype$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 = '" . 0 .
158 "', adj_amount = '" . 0 .
159 "', follow_up = '" . "y" .
160 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow" )) .
161 "'");
162 $Affected='yes';
164 if($Affected=='yes')
166 if(trim(formData('type_name' ))!='patient')
168 $ferow = sqlQuery("select last_level_closed from form_encounter where
169 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
170 //multiple charges can come.
171 if($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow" )))
173 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow" ))."' where
174 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
175 //last_level_closed gets increased.
176 //-----------------------------------
177 // Determine the next insurance level to be billed.
178 $ferow = sqlQuery("SELECT date, last_level_closed " .
179 "FROM form_encounter WHERE " .
180 "pid = '".trim(formData('hidden_patient_code' ))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow" ))."'");
181 $date_of_service = substr($ferow['date'], 0, 10);
182 $new_payer_type = 0 + $ferow['last_level_closed'];
183 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0)
184 ++$new_payer_type;
185 $new_payer_id = arGetPayerID(trim(formData('hidden_patient_code' )), $date_of_service, $new_payer_type);
186 if($new_payer_id>0)
188 arSetupSecondary(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" )),0);
190 //-----------------------------------
195 //===============================================================================
196 // Delete rows, with logging, for the specified table using the
197 // specified WHERE clause. Borrowed from deleter.php.
199 function row_delete($table, $where) {
200 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
201 $count = 0;
202 while ($trow = sqlFetchArray($tres)) {
203 $logstring = "";
204 foreach ($trow as $key => $value) {
205 if (! $value || $value == '0000-00-00 00:00:00') continue;
206 if ($logstring) $logstring .= " ";
207 $logstring .= $key . "='" . addslashes($value) . "'";
209 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
210 ++$count;
212 if ($count) {
213 $query = "DELETE FROM $table WHERE $where";
214 sqlStatement($query);
217 //===============================================================================