Here is the updated integration of the DDI, take 2
[openemr.git] / library / payment.inc.php
blob535b7f91134dd8e18be5a7f3ad6195e318ee7647
1 <?php
2 /**
4 * @package OpenEMR
5 * @author Eldho Chacko <eldho@zhservices.com>
6 * @author Paul Simon K <paul@zhservices.com>
7 * @author Stephen Waite <stephen.waite@cmsvt.com>
8 * @copyright Copyright (c) 2010 Z&H Consultancy Services Private Limited <sam@zhservices.com>
9 * @copyright Copyright (c) 2018 Stephen Waite <stephen.waite@cmsvt.com>
10 * @link http://www.open-emr.org
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 use OpenEMR\Billing\SLEOB;
16 // Post a payment to the payments table.
18 function frontPayment($patient_id, $encounter, $method, $source, $amount1, $amount2, $timestamp, $auth = "")
21 if (empty($auth)) {
22 $auth=$_SESSION['authUser'];
25 $tmprow = sqlQuery(
26 "SELECT date FROM form_encounter WHERE " .
27 "encounter=? and pid=?",
28 array($encounter,$patient_id)
30 //the manipulation is done to insert the amount paid into payments table in correct order to show in front receipts report,
31 //if the payment is for today's encounter it will be shown in the report under today field and otherwise shown as previous
32 $tmprowArray=explode(' ', $tmprow['date']);
33 if (date('Y-m-d')==$tmprowArray[0]) {
34 if ($amount1==0) {
35 $amount1=$amount2;
36 $amount2=0;
38 } else {
39 if ($amount2==0) {
40 $amount2=$amount1;
41 $amount1=0;
45 $payid = sqlInsert("INSERT INTO payments ( " .
46 "pid, encounter, dtime, user, method, source, amount1, amount2 " .
47 ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)", array($patient_id,$encounter,$timestamp,$auth,$method,$source,$amount1,$amount2));
48 return $payid;
51 //===============================================================================
52 //This section handles the common functins of payment screens.
53 //===============================================================================
54 function DistributionInsert($CountRow, $created_time, $user_id)
56 //Function inserts the distribution.Payment,Adjustment,Deductible,Takeback & Follow up reasons are inserted as seperate rows.
57 //It automatically pushes to next insurance for billing.
58 //In the screen a drop down of Ins1,Ins2,Ins3,Pat are given.The posting can be done for any level.
59 $Affected='no';
60 if (isset($_POST["Payment$CountRow"]) && $_POST["Payment$CountRow"]*1>0) {
61 if (trim(formData('type_name'))=='insurance') {
62 if (trim(formData("HiddenIns$CountRow"))==1) {
63 $AccountCode="IPP";
66 if (trim(formData("HiddenIns$CountRow"))==2) {
67 $AccountCode="ISP";
70 if (trim(formData("HiddenIns$CountRow"))==3) {
71 $AccountCode="ITP";
73 } elseif (trim(formData('type_name'))=='patient') {
74 $AccountCode="PP";
77 sqlBeginTrans();
78 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow"))));
79 sqlStatement("insert into ar_activity set " .
80 "pid = '" . trim(formData('hidden_patient_code')) .
81 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
82 "', sequence_no = '" . $sequence_no['increment'] .
83 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
84 "', code = '" . trim(formData("HiddenCode$CountRow")) .
85 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
86 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
87 "', post_time = '" . trim($created_time) .
88 "', post_user = '" . trim($user_id) .
89 "', session_id = '" . trim(formData('payment_id')) .
90 "', modified_time = '" . trim($created_time) .
91 "', pay_amount = '" . trim(formData("Payment$CountRow")) .
92 "', adj_amount = '" . 0 .
93 "', account_code = '" . "$AccountCode" .
94 "'");
95 sqlCommitTrans();
96 $Affected='yes';
99 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0) {
100 if (trim(formData('type_name'))=='insurance') {
101 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow"));
102 $AccountCode="IA";
103 } elseif (trim(formData('type_name'))=='patient') {
104 $AdjustString="Pt adjust";
105 $AccountCode="PA";
108 sqlBeginTrans();
109 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow"))));
110 sqlInsert("insert into ar_activity set " .
111 "pid = '" . trim(formData('hidden_patient_code')) .
112 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
113 "', sequence_no = '" . $sequence_no['increment'] .
114 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
115 "', code = '" . trim(formData("HiddenCode$CountRow")) .
116 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
117 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
118 "', post_time = '" . trim($created_time) .
119 "', post_user = '" . trim($user_id) .
120 "', session_id = '" . trim(formData('payment_id')) .
121 "', modified_time = '" . trim($created_time) .
122 "', pay_amount = '" . 0 .
123 "', adj_amount = '" . trim(formData("AdjAmount$CountRow")) .
124 "', memo = '" . "$AdjustString" .
125 "', account_code = '" . "$AccountCode" .
126 "'");
127 sqlCommitTrans();
128 $Affected='yes';
131 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0) {
132 sqlBeginTrans();
133 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow"))));
134 sqlInsert("insert into ar_activity set " .
135 "pid = '" . trim(formData('hidden_patient_code')) .
136 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
137 "', sequence_no = '" . $sequence_no['increment'] .
138 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
139 "', code = '" . trim(formData("HiddenCode$CountRow")) .
140 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
141 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
142 "', post_time = '" . trim($created_time) .
143 "', post_user = '" . trim($user_id) .
144 "', session_id = '" . trim(formData('payment_id')) .
145 "', modified_time = '" . trim($created_time) .
146 "', pay_amount = '" . 0 .
147 "', adj_amount = '" . 0 .
148 "', memo = '" . "Deductible $".trim(formData("Deductible$CountRow")) .
149 "', account_code = '" . "Deduct" .
150 "'");
151 sqlCommitTrans();
152 $Affected='yes';
155 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0) {
156 sqlBeginTrans();
157 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow"))));
158 sqlInsert("insert into ar_activity set " .
159 "pid = '" . trim(formData('hidden_patient_code')) .
160 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
161 "', sequence_no = '" . $sequence_no['increment'] .
162 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
163 "', code = '" . trim(formData("HiddenCode$CountRow")) .
164 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
165 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
166 "', post_time = '" . trim($created_time) .
167 "', post_user = '" . trim($user_id) .
168 "', session_id = '" . trim(formData('payment_id')) .
169 "', modified_time = '" . trim($created_time) .
170 "', pay_amount = '" . trim(formData("Takeback$CountRow"))*-1 .
171 "', adj_amount = '" . 0 .
172 "', account_code = '" . "Takeback" .
173 "'");
174 sqlCommitTrans();
175 $Affected='yes';
178 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y') {
179 sqlBeginTrans();
180 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow"))));
181 sqlInsert("insert into ar_activity set " .
182 "pid = '" . trim(formData('hidden_patient_code')) .
183 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
184 "', sequence_no = '" . $sequence_no['increment'] .
185 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
186 "', code = '" . trim(formData("HiddenCode$CountRow")) .
187 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
188 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
189 "', post_time = '" . trim($created_time) .
190 "', post_user = '" . trim($user_id) .
191 "', session_id = '" . trim(formData('payment_id')) .
192 "', modified_time = '" . trim($created_time) .
193 "', pay_amount = '" . 0 .
194 "', adj_amount = '" . 0 .
195 "', follow_up = '" . "y" .
196 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow")) .
197 "'");
198 sqlCommitTrans();
199 $Affected='yes';
202 if ($Affected=='yes') {
203 if (trim(formData('type_name'))!='patient') {
204 $ferow = sqlQuery("select last_level_closed from form_encounter where
205 pid ='".trim(formData('hidden_patient_code'))."' and encounter='".trim(formData("HiddenEncounter$CountRow"))."'");
206 //multiple charges can come.
207 if ($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow"))) {
208 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow"))."' where
209 pid ='".trim(formData('hidden_patient_code'))."' and encounter='".trim(formData("HiddenEncounter$CountRow"))."'");
210 //last_level_closed gets increased.
211 //-----------------------------------
212 // Determine the next insurance level to be billed.
213 $ferow = sqlQuery("SELECT date, last_level_closed " .
214 "FROM form_encounter WHERE " .
215 "pid = '".trim(formData('hidden_patient_code'))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow"))."'");
216 $date_of_service = substr($ferow['date'], 0, 10);
217 $new_payer_type = 0 + $ferow['last_level_closed'];
218 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0) {
219 ++$new_payer_type;
222 $new_payer_id = SLEOB::arGetPayerID(trim(formData('hidden_patient_code')), $date_of_service, $new_payer_type);
223 if ($new_payer_id>0) {
224 SLEOB::arSetupSecondary(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow")), 0);
227 //-----------------------------------
232 //===============================================================================
233 // Delete rows, with logging, for the specified table using the
234 // specified WHERE clause. Borrowed from deleter.php.
236 function row_delete($table, $where)
238 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
239 $count = 0;
240 while ($trow = sqlFetchArray($tres)) {
241 $logstring = "";
242 foreach ($trow as $key => $value) {
243 if (! $value || $value == '0000-00-00 00:00:00') {
244 continue;
247 if ($logstring) {
248 $logstring .= " ";
251 $logstring .= $key . "='" . addslashes($value) . "'";
254 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
255 ++$count;
258 if ($count) {
259 $query = "DELETE FROM $table WHERE $where";
260 sqlStatement($query);
263 //===============================================================================